LibWeb: Avoid premature creation of CSSPixels in calc simplification

Previously we were converting lengths to CSSPixels values when we didn't
need to, this had a couple of effects in that:
 - We rounded to CSSPixel resolution prematurely (sometimes giving
   incorrect results)
 - We converted NaN to 0 when we shouldn't have.

We now avoid prematurely converting lengths to CSSPixels values in two
places:
 - `CalculationResult::from_value`
 - `CalculatedStyleValue::resolve_length_deprecated` (the new method
   already avoided rounding).

Gains us 16 WPT tests.
This commit is contained in:
Callum Law 2025-07-31 21:07:03 +12:00 committed by Sam Atkins
parent 3fa7bc1919
commit a44e28fd56
Notes: github-actions[bot] 2025-08-06 14:01:09 +00:00
4 changed files with 74 additions and 17 deletions

View file

@ -2600,7 +2600,7 @@ CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalculationResult:
return 0.0;
if (length.is_absolute())
return length.absolute_length_to_px().to_double();
return length.absolute_length_to_px_without_rounding();
// If we don't have a context, we cant resolve the length, so return NAN
if (!context.length_resolution_context.has_value()) {
@ -2751,7 +2751,7 @@ Optional<Length> CalculatedStyleValue::resolve_length_deprecated(CalculationReso
{
auto result = m_calculation->resolve(context);
if (result.type().has_value() && result.type()->matches_length(m_context.percentages_resolve_as))
return Length::make_px(CSSPixels { result.value() });
return Length::make_px(result.value());
return {};
}