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

[Bug] : css nano strips away important factors from multiplications in calc() #107

Closed
Sanderand opened this issue Apr 17, 2020 · 5 comments · Fixed by #114
Closed

[Bug] : css nano strips away important factors from multiplications in calc() #107

Sanderand opened this issue Apr 17, 2020 · 5 comments · Fixed by #114

Comments

@Sanderand
Copy link

COPY OF cssnano/cssnano#901

Describe 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!

@alexander-akait
Copy link
Collaborator

Sorry for delay, feel free to send a PR, it should be easy

@mischnic
Copy link
Contributor

Seems to happen with division as well:

const postcss = require('postcss');
const calc = require('postcss-calc');

const INPUT = `
.a {
    border-width: calc(var(--foo) / 2 - var(--bar) / 2);
}
`;

const output = postcss().use(calc()).process(INPUT).css;

console.log(output.trim());

/*
.a {
    border-width: calc(var(--foo) - var(--bar));
}
*/

@coreyworrell
Copy link

Unfortunately #114 does not fix the following case (--fluid-bp):

Source:

:root {
    --fluid-min-width: 320;
    --fluid-max-width: 1140;
    --fluid-screen: 100vw;
    --fluid-bp: calc(
        (var(--fluid-screen) - ((var(--fluid-min-width) / 16) * 1rem)) /
        ((var(--fluid-max-width) / 16) - (var(--fluid-min-width) / 16))
    );
}

7.0.4 outputs:

:root {
    --fluid-min-width: 320;
    --fluid-max-width: 1140;
    --fluid-screen: 100vw;
    --fluid-bp: calc(
        (var(--fluid-screen) - var(--fluid-min-width) / 16 * 1rem) / 
        (var(--fluid-max-width) - var(--fluid-min-width)) / 16
    );
}

The output should instead be:

:root {
    --fluid-bp: calc(
        (var(--fluid-screen) - (var(--fluid-min-width) / 16) * 1rem)) / 
        ((var(--fluid-max-width) - var(--fluid-min-width)) / 16
    );
}

postcss-calc is removing the necessary parentheses and affecting the operator precedence.

@alexander-akait
Copy link
Collaborator

@coreyworrell please open a new issue and feel free to send a fix

@coreyworrell
Copy link

@evilebottnawi opened #115 just now. I am not familiar enough with the source code to send fix but hopefully someone else will be.

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.

4 participants