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

Please do not calculate the percentage in calc() #12

Open
yisibl opened this issue Dec 7, 2021 · 2 comments
Open

Please do not calculate the percentage in calc() #12

yisibl opened this issue Dec 7, 2021 · 2 comments
Labels
bug Something isn't working

Comments

@yisibl
Copy link
Contributor

yisibl commented Dec 7, 2021

Original issue: evanw/esbuild#1821 (esbuild has been resolved).

.foo {
  width: calc(100% / 3);
}

lightningcss minifier output

.foo{width:33.3333%}

esbuild(old version) minifier output

.foo{width:33.33333%}

Any attempt to calculate and then simplify the percentages in calc() can be dangerous! Please turn off this feature by default.

Demo: https://codepen.io/yisi/pen/OJxVXYo

(Mac Chrome 89.0.4389.82)

Although a new version of Chrome switched to TableNG to fix this issue, there are still many other browsers that use older versions of Blink.

Please make sure that the CSS minifier does not break the page layout, in which case the number of compressed bytes is not that important.

See also: https://stackoverflow.com/questions/31719624/displaytable-div-with-percentage-width-1px-bug

@devongovett
Copy link
Member

Interesting. In my tests, browsers round percentage values to 3 decimal places. See https://codepen.io/devongovett/pen/vYeGgzO. I round to 4 just to be safe. I can't seem to reproduce the issue in your original codepen (https://codepen.io/yisi/pen/OJxVXYo) in any browser either...

@yisibl
Copy link
Contributor Author

yisibl commented Dec 7, 2021

Thank you for your reply.

This is a browser bug that only occurs with display: table. I could probably write more examples, but this one is enough to show that simplifying calc() is unsafe.

Chrome 91 later rewrote the rendering of the Table layout, internally called TablesNG, and this bug was fixed. So I tested it with Chrome 89, and it should reproduce stably in this version.

This is a special case, but my point is that CSS is a Pandora's box and we don't know when the bug will be triggered, especially for percentage rendering which is very complex and involves sub-pixel allocation and precision issues.

I hope the CSS compressor doesn't trigger this bug.

@devongovett devongovett added the bug Something isn't working label Dec 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants