LibWeb: Properly simplify sum nodes containing negated sum nodes

This is ad-hoc, see https://github.com/w3c/csswg-drafts/issues/13020

Gains us 5 WPT tests
This commit is contained in:
Callum Law 2025-10-28 20:50:20 +13:00 committed by Sam Atkins
parent 9c06d58b2e
commit c2ca712406
Notes: github-actions[bot] 2025-10-30 12:19:35 +00:00
3 changed files with 120 additions and 0 deletions

View file

@ -3070,6 +3070,22 @@ NonnullRefPtr<CalculationNode const> simplify_a_calculation_tree(CalculationNode
if (child.type() == CalculationNode::Type::Negate)
return as<NegateCalculationNode>(child).child();
// AD-HOC: Convert negated sums into sums of negated nodes - see https://github.com/w3c/csswg-drafts/issues/13020
if (child.type() == CalculationNode::Type::Sum) {
Vector<NonnullRefPtr<CalculationNode const>> negated_sum_components;
for (auto const& sum_child : child.children()) {
if (sum_child->type() == CalculationNode::Type::Numeric)
negated_sum_components.append(as<NumericCalculationNode>(*sum_child).negated(context));
else if (sum_child->type() == CalculationNode::Type::Negate)
negated_sum_components.append(as<NegateCalculationNode>(*sum_child).child());
else
negated_sum_components.append(NegateCalculationNode::create(sum_child));
}
return SumCalculationNode::create(negated_sum_components);
}
// 3. Return root.
// NOTE: Because our root is immutable, we have to return a new node if the child was modified.
if (&child == &root_negate.child())