Skip to content

calc() breaks when using 3+ variable fallbacks #104

@dgp1130

Description

@dgp1130

From downstream issue cssnano/cssnano#880 which is from downstream issue angular/angular-cli#16910.

Using a calc() command with 3+ variable fallbacks seems to break the parser.

For example, paste the following code into the CSS Nano playground (which I'm told uses postcss-calc via cssnano-preset-default:

.some-rule-lg {
  --width-lg: 1px;
  --width-md: 2px;
  --width-sm: 3px;
  --offset-lg: 4px;
  --offset-md: 5px;
  --offset-sm: 6px;
  width: calc(var(--width-lg, var(--width-md, var(--width-sm, 0))) + var(--offset-lg, var(--offset-md, var(--offset-sm, 0))));
}

Then look at the JavaScript console and see:

"Parse error on line 1: 
var(--width-lg, var(--width-md, var(--width-sm, 0))) + var(--...
---------------------------------------------------^
Expecting end of input, "ADD", "SUB", "MUL", "DIV", got unexpected "RPAREN""

Work around is to use intermediary variables:

.some-rule-lg {
  --width-lg: 1px;
  --width-md: 2px;
  --width-sm: 3px;
  --offset-lg: 4px;
  --offset-md: 5px;
  --offset-sm: 6px;

  --temp-calc1: var(--width-lg, var(--width-md, var(--width-sm, 0)));
  --temp-calc2: var(--offset-lg, var(--offset-md, var(--offset-sm, 0)));
  width: calc(var(--temp-calc1) + var(--temp-calc2));
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bad-parserpostcss-calc fails to parse the inputbug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions