From 385c3d273a27bd8afb209f36151c877af7478177 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Wed, 22 Jan 2025 15:00:13 +0000 Subject: [PATCH] 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. --- Libraries/LibWeb/CSS/CalculatedOr.cpp | 59 ++++++------------- Libraries/LibWeb/CSS/CalculatedOr.h | 34 +++++------ Libraries/LibWeb/CSS/MediaQuery.cpp | 36 ++++++----- .../StyleValues/FilterValueListStyleValue.cpp | 4 +- Libraries/LibWeb/CSS/Transformation.cpp | 10 +++- .../LibWeb/HTML/CanvasRenderingContext2D.cpp | 10 +++- Libraries/LibWeb/HTML/SourceSet.cpp | 4 +- .../LibWeb/Layout/InlineLevelIterator.cpp | 18 +++--- Libraries/LibWeb/Layout/Node.cpp | 13 ++-- .../LibWeb/Painting/SVGPathPaintable.cpp | 5 +- Libraries/LibWeb/SVG/SVGGraphicsElement.cpp | 5 +- 11 files changed, 98 insertions(+), 100 deletions(-) diff --git a/Libraries/LibWeb/CSS/CalculatedOr.cpp b/Libraries/LibWeb/CSS/CalculatedOr.cpp index 4ffeb0a439f..470d5996df7 100644 --- a/Libraries/LibWeb/CSS/CalculatedOr.cpp +++ b/Libraries/LibWeb/CSS/CalculatedOr.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Sam Atkins + * Copyright (c) 2023-2025, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ @@ -17,9 +17,9 @@ namespace Web::CSS { -Angle AngleOrCalculated::resolve_calculated(NonnullRefPtr const& calculated, Layout::Node const& layout_node) const +Optional AngleOrCalculated::resolve_calculated(NonnullRefPtr const& calculated, CalculationResolutionContext const& context) const { - return calculated->resolve_angle({ .length_resolution_context = Length::ResolutionContext::for_layout_node(layout_node) }).value(); + return calculated->resolve_angle(context); } NonnullRefPtr AngleOrCalculated::create_style_value() const @@ -27,9 +27,9 @@ NonnullRefPtr AngleOrCalculated::create_style_value() const return AngleStyleValue::create(value()); } -Flex FlexOrCalculated::resolve_calculated(NonnullRefPtr const& calculated, Layout::Node const& layout_node) const +Optional FlexOrCalculated::resolve_calculated(NonnullRefPtr const& calculated, CalculationResolutionContext const& context) const { - return calculated->resolve_flex({ .length_resolution_context = Length::ResolutionContext::for_layout_node(layout_node) }).value(); + return calculated->resolve_flex(context); } NonnullRefPtr FlexOrCalculated::create_style_value() const @@ -37,9 +37,9 @@ NonnullRefPtr FlexOrCalculated::create_style_value() const return FlexStyleValue::create(value()); } -Frequency FrequencyOrCalculated::resolve_calculated(NonnullRefPtr const& calculated, Layout::Node const& layout_node) const +Optional FrequencyOrCalculated::resolve_calculated(NonnullRefPtr const& calculated, CalculationResolutionContext const& context) const { - return calculated->resolve_frequency({ .length_resolution_context = Length::ResolutionContext::for_layout_node(layout_node) }).value(); + return calculated->resolve_frequency(context); } NonnullRefPtr FrequencyOrCalculated::create_style_value() const @@ -47,16 +47,9 @@ NonnullRefPtr FrequencyOrCalculated::create_style_value() const return FrequencyStyleValue::create(value()); } -i64 IntegerOrCalculated::resolve_calculated(NonnullRefPtr const& calculated, Layout::Node const& layout_node) const +Optional IntegerOrCalculated::resolve_calculated(NonnullRefPtr const& calculated, CalculationResolutionContext const& context) const { - return calculated->resolve_integer({ .length_resolution_context = Length::ResolutionContext::for_layout_node(layout_node) }).value(); -} - -i64 IntegerOrCalculated::resolved(Length::ResolutionContext const& context) const -{ - if (is_calculated()) - return calculated()->resolve_integer({ .length_resolution_context = context }).value(); - return value(); + return calculated->resolve_integer(context); } NonnullRefPtr IntegerOrCalculated::create_style_value() const @@ -64,16 +57,9 @@ NonnullRefPtr IntegerOrCalculated::create_style_value() const return IntegerStyleValue::create(value()); } -Length LengthOrCalculated::resolve_calculated(NonnullRefPtr const& calculated, Layout::Node const& layout_node) const +Optional LengthOrCalculated::resolve_calculated(NonnullRefPtr const& calculated, CalculationResolutionContext const& context) const { - return calculated->resolve_length({ .length_resolution_context = Length::ResolutionContext::for_layout_node(layout_node) }).value(); -} - -Length LengthOrCalculated::resolved(Length::ResolutionContext const& context) const -{ - if (is_calculated()) - return calculated()->resolve_length({ .length_resolution_context = context }).value(); - return value(); + return calculated->resolve_length(context); } NonnullRefPtr LengthOrCalculated::create_style_value() const @@ -81,9 +67,9 @@ NonnullRefPtr LengthOrCalculated::create_style_value() const return LengthStyleValue::create(value()); } -double NumberOrCalculated::resolve_calculated(NonnullRefPtr const& calculated, Layout::Node const& layout_node) const +Optional NumberOrCalculated::resolve_calculated(NonnullRefPtr const& calculated, CalculationResolutionContext const& context) const { - return calculated->resolve_number({ .length_resolution_context = Length::ResolutionContext::for_layout_node(layout_node) }).value(); + return calculated->resolve_number(context); } NonnullRefPtr NumberOrCalculated::create_style_value() const @@ -91,9 +77,9 @@ NonnullRefPtr NumberOrCalculated::create_style_value() const return NumberStyleValue::create(value()); } -Percentage PercentageOrCalculated::resolve_calculated(NonnullRefPtr const& calculated, Layout::Node const& layout_node) const +Optional PercentageOrCalculated::resolve_calculated(NonnullRefPtr const& calculated, CalculationResolutionContext const& context) const { - return calculated->resolve_percentage({ .length_resolution_context = Length::ResolutionContext::for_layout_node(layout_node) }).value(); + return calculated->resolve_percentage(context); } NonnullRefPtr PercentageOrCalculated::create_style_value() const @@ -101,16 +87,9 @@ NonnullRefPtr PercentageOrCalculated::create_style_value() const return PercentageStyleValue::create(value()); } -Resolution ResolutionOrCalculated::resolve_calculated(NonnullRefPtr const& calculated, Layout::Node const& layout_node) const +Optional ResolutionOrCalculated::resolve_calculated(NonnullRefPtr const& calculated, CalculationResolutionContext const& context) const { - return calculated->resolve_resolution({ .length_resolution_context = Length::ResolutionContext::for_layout_node(layout_node) }).value(); -} - -Resolution ResolutionOrCalculated::resolved() const -{ - if (is_calculated()) - return calculated()->resolve_resolution({}).value(); - return value(); + return calculated->resolve_resolution(context); } NonnullRefPtr ResolutionOrCalculated::create_style_value() const @@ -118,9 +97,9 @@ NonnullRefPtr ResolutionOrCalculated::create_style_value() const return ResolutionStyleValue::create(value()); } -Time TimeOrCalculated::resolve_calculated(NonnullRefPtr const& calculated, Layout::Node const& layout_node) const +Optional