LibWeb: Correctly compute consistent type when simplifying hypot

Previously we would never get a valid `consistent_type` as we were
trying to make the node types consistent with the initial empty type
which isn't possible.

Gains us 7 WPT tests.
This commit is contained in:
Callum Law 2025-06-28 18:41:29 +12:00 committed by Andreas Kling
commit 8e9753eadb
Notes: github-actions[bot] 2025-06-30 12:54:14 +00:00
5 changed files with 261 additions and 4 deletions

View file

@ -2096,7 +2096,7 @@ Optional<CalculatedStyleValue::CalculationResult> HypotCalculationNode::run_oper
// <percentage>, but must have a consistent type or else the function is invalid; the results type will be the // <percentage>, but must have a consistent type or else the function is invalid; the results type will be the
// consistent type. // consistent type.
CSSNumericType consistent_type; Optional<CSSNumericType> consistent_type;
double value = 0; double value = 0;
for (auto const& child : m_values) { for (auto const& child : m_values) {
@ -2104,14 +2104,20 @@ Optional<CalculatedStyleValue::CalculationResult> HypotCalculationNode::run_oper
if (!canonical_child.has_value()) if (!canonical_child.has_value())
return {}; return {};
auto maybe_type = consistent_type.consistent_type(canonical_child->type().value()); if (!consistent_type.has_value())
if (!maybe_type.has_value()) consistent_type = canonical_child->type();
else
consistent_type = consistent_type->consistent_type(canonical_child->type().value());
if (!consistent_type.has_value())
return {}; return {};
consistent_type = maybe_type.release_value();
value += canonical_child->value() * canonical_child->value(); value += canonical_child->value() * canonical_child->value();
} }
if (!consistent_type.has_value())
return {};
return CalculatedStyleValue::CalculationResult { sqrt(value), consistent_type }; return CalculatedStyleValue::CalculationResult { sqrt(value), consistent_type };
} }

View file

@ -0,0 +1,53 @@
Harness status: OK
Found 47 tests
44 Pass
3 Fail
Pass pow(1,1) should be used-value-equivalent to 1
Pass sqrt(1) should be used-value-equivalent to 1
Pass hypot(1) should be used-value-equivalent to 1
Pass sqrt(pow(1,1)) should be used-value-equivalent to 1
Pass hypot(pow(1, sqrt(1))) should be used-value-equivalent to 1
Pass calc(hypot(pow((1 + sqrt(1)) / 2, sqrt(1)))) should be used-value-equivalent to 1
Pass calc(100px * pow(2, pow(2, 2))) should be used-value-equivalent to 1600px
Pass calc(1px * pow(2, 3)) should be used-value-equivalent to 8px
Pass calc(100px * sqrt(100)) should be used-value-equivalent to 1000px
Pass calc(1px * pow(2, sqrt(100)) should be used-value-equivalent to 1024px
Pass hypot(3px, 4px) should be used-value-equivalent to 5px
Pass hypot(3e+9px, 4e+9px) should be used-value-equivalent to 5e+9px
Pass calc(100px * hypot(3, 4)) should be used-value-equivalent to 500px
Pass hypot(-5px) should be used-value-equivalent to 5px
Pass calc(1px * hypot(-5)) should be used-value-equivalent to 5px
Pass calc(1px * hypot(10000)) should be used-value-equivalent to 10000px
Pass calc(2px * sqrt(100000000)) should be used-value-equivalent to 20000px
Pass calc(3px * pow(20, 4)) should be used-value-equivalent to 480000px
Pass calc(-2 * hypot(3px, 4px)) should be used-value-equivalent to -10px
Pass hypot(0% + 3px, 0% + 4px) should be used-value-equivalent to 5px
Pass hypot(0% + 772.333px) should be used-value-equivalent to calc(0% + 772.333px)
Pass hypot(0% + 772.35px) should be used-value-equivalent to calc(0% + 772.35px)
Pass hypot(0% + 600px, 0% + 800px) should be used-value-equivalent to 1000px
Fail sqrt(sibling-index()) should be used-value-equivalent to 2
Fail calc(1px * sqrt(sibling-index())) should be used-value-equivalent to 2px
Fail sqrt(pow(sibling-index(), 2)) should be used-value-equivalent to 4
Pass hypot(1px) should be used-value-equivalent to 1px
Pass hypot(1cm) should be used-value-equivalent to 1cm
Pass hypot(1mm) should be used-value-equivalent to 1mm
Pass hypot(1Q) should be used-value-equivalent to 1Q
Pass hypot(1in) should be used-value-equivalent to 1in
Pass hypot(1pc) should be used-value-equivalent to 1pc
Pass hypot(1pt) should be used-value-equivalent to 1pt
Pass hypot(1em) should be used-value-equivalent to 1em
Pass hypot(1ex) should be used-value-equivalent to 1ex
Pass hypot(1ch) should be used-value-equivalent to 1ch
Pass hypot(1rem) should be used-value-equivalent to 1rem
Pass hypot(1vh) should be used-value-equivalent to 1vh
Pass hypot(1vw) should be used-value-equivalent to 1vw
Pass hypot(1vmin) should be used-value-equivalent to 1vmin
Pass hypot(1vmax) should be used-value-equivalent to 1vmax
Pass hypot(1s) should be used-value-equivalent to 1s
Pass hypot(1ms) should be used-value-equivalent to 1ms
Pass hypot(1deg) should be used-value-equivalent to 1deg
Pass hypot(1grad) should be used-value-equivalent to 1grad
Pass hypot(1rad) should be used-value-equivalent to 1rad
Pass hypot(1turn) should be used-value-equivalent to 1turn

View file

@ -0,0 +1,55 @@
Harness status: OK
Found 49 tests
47 Pass
2 Fail
Pass e.style['opacity'] = "hypot()" should not set the property value
Pass e.style['opacity'] = "hypot( )" should not set the property value
Pass e.style['opacity'] = "hypot(,)" should not set the property value
Pass e.style['opacity'] = "hypot(1, )" should not set the property value
Pass e.style['opacity'] = "hypot(, 1)" should not set the property value
Pass e.style['opacity'] = "hypot(1 + )" should not set the property value
Pass e.style['opacity'] = "hypot(1 - )" should not set the property value
Pass e.style['opacity'] = "hypot(1 * )" should not set the property value
Pass e.style['opacity'] = "hypot(1 / )" should not set the property value
Fail e.style['opacity'] = "hypot(1 2)" should not set the property value
Pass e.style['opacity'] = "hypot(1, , 2)" should not set the property value
Pass e.style['opacity'] = "sqrt()" should not set the property value
Pass e.style['opacity'] = "sqrt( )" should not set the property value
Pass e.style['opacity'] = "sqrt(,)" should not set the property value
Pass e.style['opacity'] = "sqrt(1, )" should not set the property value
Pass e.style['opacity'] = "sqrt(, 1)" should not set the property value
Pass e.style['opacity'] = "sqrt(1 + )" should not set the property value
Pass e.style['opacity'] = "sqrt(1 - )" should not set the property value
Pass e.style['opacity'] = "sqrt(1 * )" should not set the property value
Pass e.style['opacity'] = "sqrt(1 / )" should not set the property value
Fail e.style['opacity'] = "sqrt(1 2)" should not set the property value
Pass e.style['opacity'] = "sqrt(1, , 2)" should not set the property value
Pass e.style['opacity'] = "sqrt(1, 2)" should not set the property value
Pass e.style['opacity'] = "pow( )" should not set the property value
Pass e.style['opacity'] = "pow(,)" should not set the property value
Pass e.style['opacity'] = "pow(1, )" should not set the property value
Pass e.style['opacity'] = "pow(, 1)" should not set the property value
Pass e.style['opacity'] = "pow(1 + )" should not set the property value
Pass e.style['opacity'] = "pow(1 - )" should not set the property value
Pass e.style['opacity'] = "pow(1 * )" should not set the property value
Pass e.style['opacity'] = "pow(1 / )" should not set the property value
Pass e.style['opacity'] = "pow(1 2)" should not set the property value
Pass e.style['opacity'] = "pow(1, , 2)" should not set the property value
Pass e.style['opacity'] = "pow(2px, 2)" should not set the property value
Pass e.style['opacity'] = "pow(10, 1px)" should not set the property value
Pass e.style['outline-offset'] = "calc(1px * pow(1))" should not set the property value
Pass e.style['outline-offset'] = "calc(1px * pow(2px, 3px))" should not set the property value
Pass e.style['outline-offset'] = "calc(sqrt(100px)" should not set the property value
Pass e.style['outline-offset'] = "hypot(2px, 3)" should not set the property value
Pass e.style['outline-offset'] = "hypot(3, ,4)" should not set the property value
Pass e.style['outline-offset'] = "hypot(1, 2)" should not set the property value
Pass e.style['outline-offset'] = "calc(1px * pow(2 3))" should not set the property value
Pass e.style['outline-offset'] = "hypot()" should not set the property value
Pass e.style['outline-offset'] = "calc(pow(2))" should not set the property value
Pass e.style['outline-offset'] = "pow())" should not set the property value
Pass e.style['outline-offset'] = "pow(1, 2)" should not set the property value
Pass e.style['outline-offset'] = "calc(sqrt())" should not set the property value
Pass e.style['outline-offset'] = "calc(sqrt(100, 200))" should not set the property value
Pass e.style['outline-offset'] = "pow(10px, 1)" should not set the property value

View file

@ -0,0 +1,73 @@
<!DOCTYPE html>
<link rel="help" href="https://drafts.csswg.org/css-values-4/#exponent-funcs">
<link rel="help" href="https://drafts.csswg.org/css-values-4/#numbers">
<link rel="help" href="https://drafts.csswg.org/css-values-4/#calc-type-checking">
<link rel="author" title="Apple Inc">
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<script src="../support/numeric-testcommon.js"></script>
<div>
<div></div>
<div></div>
<div></div>
<div id="target"></div>
</div>
<script>
// Identity tests
test_math_used('pow(1,1)', '1', {type:'integer'});
test_math_used('sqrt(1)', '1', {type:'integer'});
test_math_used('hypot(1)', '1', {type:'integer'});
// Nestings
test_math_used('sqrt(pow(1,1))', '1', {type:'integer'});
test_math_used('hypot(pow(1, sqrt(1)))', '1', {type:'integer'});
test_math_used('calc(hypot(pow((1 + sqrt(1)) / 2, sqrt(1))))', '1', {type:'integer'});
// General calculations
test_math_used('calc(100px * pow(2, pow(2, 2)))','1600px');
test_math_used('calc(1px * pow(2, 3))', '8px')
test_math_used('calc(100px * sqrt(100))', '1000px');
test_math_used('calc(1px * pow(2, sqrt(100))', '1024px');
test_math_used('hypot(3px, 4px)', '5px');
test_math_used('hypot(3e+9px, 4e+9px)', '5e+9px');
test_math_used('calc(100px * hypot(3, 4))', '500px');
test_math_used('hypot(-5px)', '5px');
test_math_used('calc(1px * hypot(-5))', '5px');
test_math_used('calc(1px * hypot(10000))','10000px');
test_math_used('calc(2px * sqrt(100000000))','20000px');
test_math_used('calc(3px * pow(20, 4))', '480000px');
test_math_used('calc(-2 * hypot(3px, 4px))', '-10px');
test_math_used('hypot(0% + 3px, 0% + 4px)', '5px');
test_math_used('hypot(0% + 772.333px)', 'calc(0% + 772.333px)');
test_math_used('hypot(0% + 772.35px)', 'calc(0% + 772.35px)');
test_math_used('hypot(0% + 600px, 0% + 800px)', '1000px');
//Test unresolved at parse time
test_math_used('sqrt(sibling-index())', '2', {type: 'integer'});
test_math_used('calc(1px * sqrt(sibling-index()))', '2px');
test_math_used('sqrt(pow(sibling-index(), 2))', '4', {type: 'integer'});
//Type checking hypot
test_math_used('hypot(1px)', '1px');
test_math_used('hypot(1cm)', '1cm');
test_math_used('hypot(1mm)', '1mm');
test_math_used('hypot(1Q)', '1Q');
test_math_used('hypot(1in)', '1in');
test_math_used('hypot(1pc)', '1pc');
test_math_used('hypot(1pt)', '1pt');
test_math_used('hypot(1em)', '1em');
test_math_used('hypot(1ex)', '1ex');
test_math_used('hypot(1ch)', '1ch');
test_math_used('hypot(1rem)', '1rem');
test_math_used('hypot(1vh)', '1vh');
test_math_used('hypot(1vw)', '1vw');
test_math_used('hypot(1vmin)', '1vmin');
test_math_used('hypot(1vmax)', '1vmax');
test_math_used('hypot(1s)', '1s', {type:'time'});
test_math_used('hypot(1ms)', '1ms', {type:'time'});
test_math_used('hypot(1deg)', '1deg', {type:'angle', approx:0.001});
test_math_used('hypot(1grad)', '1grad', {type:'angle', approx:0.001});
test_math_used('hypot(1rad)', '1rad', {type:'angle', approx:0.001});
test_math_used('hypot(1turn)', '1turn', {type:'angle', approx:0.001});
</script>

View file

@ -0,0 +1,70 @@
<!DOCTYPE html>
<link rel="help" href="https://drafts.csswg.org/css-values-4/#exponent-funcs">
<link rel="help" href="https://drafts.csswg.org/css-values-4/#numbers">
<link rel="help" href="https://drafts.csswg.org/css-values-4/#calc-type-checking">
<link rel="author" title="Apple Inc">
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<script src="../support/parsing-testcommon.js"></script>
<script>
function test_invalid_number(value) {
test_invalid_value('opacity', value);
}
function test_invalid_length(value) {
// 'outline-offset' accepts <length> only, not <percentage> or any mixes.
test_invalid_value('outline-offset', value);
}
// Syntax checking
test_invalid_number('hypot()');
test_invalid_number('hypot( )');
test_invalid_number('hypot(,)');
test_invalid_number('hypot(1, )');
test_invalid_number('hypot(, 1)');
test_invalid_number('hypot(1 + )');
test_invalid_number('hypot(1 - )');
test_invalid_number('hypot(1 * )');
test_invalid_number('hypot(1 / )');
test_invalid_number('hypot(1 2)');
test_invalid_number('hypot(1, , 2)');
test_invalid_number('sqrt()');
test_invalid_number('sqrt( )');
test_invalid_number('sqrt(,)');
test_invalid_number('sqrt(1, )');
test_invalid_number('sqrt(, 1)');
test_invalid_number('sqrt(1 + )');
test_invalid_number('sqrt(1 - )');
test_invalid_number('sqrt(1 * )');
test_invalid_number('sqrt(1 / )');
test_invalid_number('sqrt(1 2)');
test_invalid_number('sqrt(1, , 2)');
test_invalid_number('sqrt(1, 2)');
test_invalid_number('pow( )');
test_invalid_number('pow(,)');
test_invalid_number('pow(1, )');
test_invalid_number('pow(, 1)');
test_invalid_number('pow(1 + )');
test_invalid_number('pow(1 - )');
test_invalid_number('pow(1 * )');
test_invalid_number('pow(1 / )');
test_invalid_number('pow(1 2)');
test_invalid_number('pow(1, , 2)');
test_invalid_number('pow(2px, 2)');
test_invalid_number('pow(10, 1px)');
// General tests
test_invalid_length('calc(1px * pow(1))');
test_invalid_length('calc(1px * pow(2px, 3px))');
test_invalid_length('calc(sqrt(100px)');
test_invalid_length('hypot(2px, 3)');
test_invalid_length('hypot(3, ,4)');
test_invalid_length('hypot(1, 2)');
test_invalid_length('calc(1px * pow(2 3))');
test_invalid_length('hypot()');
test_invalid_length('calc(pow(2))');
test_invalid_length('pow())');
test_invalid_length('pow(1, 2)');
test_invalid_length('calc(sqrt())');
test_invalid_length('calc(sqrt(100, 200))');
test_invalid_length('pow(10px, 1)');
</script>