LibWeb/CSS: Update CalculatedOr API to use CalculationResolutionContext

To be properly compatible with calc(), the resolved() methods all need:
- A length resolution context
- To return an Optional, as the calculation might not be resolvable

A bonus of this is that we can get rid of the overloads of `resolved()`
as they now all behave the same way.

A downside is a scattering of `value_or()` wherever these are used. It
might be the case that all unresolvable calculations have been rejected
before this point, but I'm not confident, and so I'll leave it like
this for now.
This commit is contained in:
Sam Atkins 2025-01-22 15:00:13 +00:00 committed by Andreas Kling
commit 385c3d273a
Notes: github-actions[bot] 2025-01-30 18:33:51 +00:00
11 changed files with 98 additions and 100 deletions

View file

@ -20,8 +20,14 @@ ErrorOr<Gfx::FloatMatrix4x4> Transformation::to_matrix(Optional<Painting::Painta
{
auto count = m_values.size();
auto value = [&](size_t index, CSSPixels const& reference_length = 0) -> ErrorOr<float> {
CalculationResolutionContext context {};
if (paintable_box.has_value())
context.length_resolution_context = Length::ResolutionContext::for_layout_node(paintable_box->layout_node());
return m_values[index].visit(
[&](CSS::LengthPercentage const& value) -> ErrorOr<float> {
context.percentage_basis = Length::make_px(reference_length);
if (paintable_box.has_value())
return value.resolved(paintable_box->layout_node(), reference_length).to_px(paintable_box->layout_node()).to_float();
if (value.is_length()) {
@ -31,8 +37,8 @@ ErrorOr<Gfx::FloatMatrix4x4> Transformation::to_matrix(Optional<Painting::Painta
return Error::from_string_literal("Transform contains non absolute units");
},
[&](CSS::AngleOrCalculated const& value) -> ErrorOr<float> {
if (paintable_box.has_value())
return value.resolved(paintable_box->layout_node()).to_radians();
if (auto resolved = value.resolved(context); resolved.has_value())
return resolved->to_radians();
if (!value.is_calculated())
return value.value().to_radians();
return Error::from_string_literal("Transform contains non absolute units");