Skip to content

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

@Calme1709

Description

@Calme1709

Running the 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions