Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parsing error of number value in minified CSS #804

Closed
yahooshiken opened this issue Feb 13, 2021 · 0 comments
Closed

Parsing error of number value in minified CSS #804

yahooshiken opened this issue Feb 13, 2021 · 0 comments

Comments

@yahooshiken
Copy link

yahooshiken commented Feb 13, 2021

Hello. First of all, I would like to thank you for this great project!
This issue is trivial and edge case.

Only when I used the "-minify" option ( [email protected] ), my CSS codes were not parsed correctly.
For example

input.css

a {
  opacity: .0;
}

output.css

a{opacity:}

I think this problem is caused by esbuild removing trailing zeros and unnecessary decimal point in internal/css_parser/css_parser.go.

Simply modifying internal/css_parser/css_parser.go as described below can solve this problem.
However, I'm not a specialist in Go lang, so maybe there is a more sophisticated solution.

func mangleNumber(t string) (string, bool) {
	original := t

	if dot := strings.IndexByte(t, '.'); dot != -1 {
+		if dot == 0 {
+			t = "0" + t
+			dot++
+		}

		// Remove trailing zeros
		for len(t) > 0 && t[len(t)-1] == '0' {
			t = t[:len(t)-1]
		}

		// Remove the decimal point if it's unnecessary
		if dot+1 == len(t) {
			t = t[:dot]
		} else {
			// Remove a leading zero
			if len(t) >= 3 && t[0] == '0' && t[1] == '.' && t[2] >= '0' && t[2] <= '9' {
				t = t[1:]
			} else if len(t) >= 4 && (t[0] == '+' || t[0] == '-') && t[1] == '0' && t[2] == '.' && t[3] >= '0' && t[3] <= '9' {
				t = t[0:1] + t[2:]
			}
		}
	}

	return t, t != original
}

Thank you for reading.
Best regards.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant