[csswg-drafts] [css-values-4] Incomplete simplification of calculation sum node containing inverted sum nodes (#13020)

Calme1709 has just created a new issue for https://github.com/w3c/csswg-drafts:

== [css-values-4] Incomplete simplification of calculation sum node containing inverted sum nodes ==
Running the [simplify a calculation tree](https://drafts.csswg.org/css-values-4/#simplify-a-calculation-tree) algorithm on a calculation tree containing a sum node which contains an invert node which in turn contains a sum node that can't be simplified down to a single numeric value results in incomplete simplification.

Take for instance the value `calc(100 - (sign(1em - 1px) - 50))` which produces the following calculation tree:
```
SUM:
  NUMERIC(100)
  NEGATE:
    SUM:
      SIGN:
        SUM:
          NUMERIC(1em)
          NUMERIC(-1px)
      NUMERIC(-50)
```

Chrome and Firefox both correctly simplify this to `calc(150 - sign(1em - 1px))` but the existing algorithm results in no simplification. This is because step 6 only simplifies the Negate node if it's child is a Numeric or Negate node, and step 8 only combines the children of the root Sum node which are Numeric or Sum nodes (not negate).

One way to fix this would be that, when simplifying Negate nodes, we convert a negated sum node into a sum of negated nodes i.e. convert
```
NEGATE:
  SUM:
    SIGN:
      SUM:
        NUMERIC(1em)
        NUMERIC(-1px)
    NUMERIC(-50)
```
into
```
SUM:
  NEGATE:
    SIGN:
      SUM:
        NUMERIC(1em)
        NUMERIC(-1px)
  NUMERIC(50)
```
Which would then be further simplified by step 9 producing an output in line with Chrome and Firefox.

I am happy to take a stab at a PR for this if the above seems like a reasonable approach.

Please view or discuss this issue at https://github.com/w3c/csswg-drafts/issues/13020 using your GitHub account


-- 
Sent via github-notify-ml as configured in https://github.com/w3c/github-notify-ml-config

Received on Monday, 27 October 2025 11:50:01 UTC