[Bug] : css nano strips away important factors from multiplications in calc()
See original GitHub issueDescribe the bug
A CSS input with a calc
function using css variables strips away factors from multiplications:
calc(var(--foo) * 0.4 - var(--bar) * 0.4);
… will get converted to …
calc(var(--foo) - var(--bar));
… and that is incorrect. This happens using cssnanos default
preset.
Input given:
.baz {
--correct-1: calc(var(--foo) * 0.2 - var(--bar) * 0.8);
--correct-2: var(--foo) * 0.4 - var(--bar) * 0.4;
--incorrect-1: calc(var(--foo) * 0.4 - var(--bar) * 0.4);
--incorrect-2: calc(var(--foo) * 0.5 - var(--bar) * 0.5);
--incorrect-3: calc((var(--foo) * 0.5) - (var(--bar) * 0.5));
}
Expected output
.baz {
--correct-1: calc(var(--foo) * 0.2 - var(--bar) * 0.8);
--correct-2: var(--foo) * 0.4 - var(--bar) * 0.4;
--incorrect-1: calc(var(--foo) * 0.4 - var(--bar) * 0.4);
--incorrect-2: calc(var(--foo) * 0.5 - var(--bar) * 0.5);
--incorrect-3: calc((var(--foo) * 0.5) - (var(--bar) * 0.5));
}
Actual output
.baz {
--correct-1: calc(var(--foo) * 0.2 - var(--bar) * 0.8);
--correct-2: var(--foo) * 0.4 - var(--bar) * 0.4;
--incorrect-1: calc(var(--foo) - var(--bar));
--incorrect-2: calc(var(--foo) - var(--bar));
--incorrect-3: calc(var(--foo) - var(--bar));
}
Repo for reproduction https://github.com/Sanderand/css-nano-calc-bug
Additional context
"dependencies": {
"cssnano": "4.1.10",
"postcss-cli": "7.1.0"
},
const cssnano = require('cssnano');
module.exports = {
plugins: [
cssnano({ preset: 'default' }),
],
};
Disabling the calc
optimization is a temporary workaround that I now use. I guess the calc optimization needs to be fixed.
Thanks!
Issue Analytics
- State:
- Created 3 years ago
- Reactions:6
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Add configuration to postcss in Sage 10 - Roots Discourse
COPY OF cssnano/cssnano#901 Describe the bug A CSS input with a calc function using css variables strips away factors from multiplications: calc ......
Read more >https://www.med.upenn.edu/dart/assets/design/files...
... reduction # 7.0.4 - Fixed: strips away important factors from multiplications in calc() ([#107](https://github.com/postcss/postcss-calc/issues/107)) ...
Read more >postcss-calc | Yarn - Package Manager
PostCSS Calc lets you reduce calc() references whenever it's possible. When multiple units are mixed together in the same expression, the calc() statement ......
Read more >calc() - CSS: Cascading Style Sheets - MDN Web Docs
The calc() CSS function lets you perform calculations when specifying CSS property values. It can be used with , , , , ,...
Read more >Does the CSS calc() impact performance? - Quora
So if you're not sure you should use CSS Calc() because of impact on ... and by that I mean back in the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Unfortunately #114 does not fix the following case (
--fluid-bp
):Source:
7.0.4 outputs:
The output should instead be:
postcss-calc
is removing the necessary parentheses and affecting the operator precedence.Seems to happen with division as well: