From a19a6deaa4f318da7fc4faf6e32773343414d98a Mon Sep 17 00:00:00 2001 From: Callum Law Date: Sat, 19 Jul 2025 13:28:14 +1200 Subject: [PATCH] LibWeb: Take a ColorResolutionContext in `color_or_fallback` Taking a ColorResolutionContext directly instead of creating one from a layout node allows us to call this from places where we don't have a layout node. --- Libraries/LibWeb/CSS/ComputedProperties.cpp | 4 ++-- Libraries/LibWeb/CSS/ComputedProperties.h | 2 +- Libraries/LibWeb/Layout/Node.cpp | 10 +++++----- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Libraries/LibWeb/CSS/ComputedProperties.cpp b/Libraries/LibWeb/CSS/ComputedProperties.cpp index 6c6c87af5c7..2a3b71b8607 100644 --- a/Libraries/LibWeb/CSS/ComputedProperties.cpp +++ b/Libraries/LibWeb/CSS/ComputedProperties.cpp @@ -224,12 +224,12 @@ LengthBox ComputedProperties::length_box(PropertyID left_id, PropertyID top_id, return box; } -Color ComputedProperties::color_or_fallback(PropertyID id, Layout::NodeWithStyle const& node, Color fallback) const +Color ComputedProperties::color_or_fallback(PropertyID id, ColorResolutionContext color_resolution_context, Color fallback) const { auto const& value = property(id); if (!value.has_color()) return fallback; - return value.to_color(ColorResolutionContext::for_layout_node_with_style(node)).value(); + return value.to_color(color_resolution_context).value(); } // https://drafts.csswg.org/css-color-adjust-1/#determine-the-used-color-scheme diff --git a/Libraries/LibWeb/CSS/ComputedProperties.h b/Libraries/LibWeb/CSS/ComputedProperties.h index 690b5897c39..3fb47f3e35f 100644 --- a/Libraries/LibWeb/CSS/ComputedProperties.h +++ b/Libraries/LibWeb/CSS/ComputedProperties.h @@ -77,7 +77,7 @@ public: LengthPercentage length_percentage_or_fallback(PropertyID, LengthPercentage const& fallback) const; Optional length_percentage(PropertyID) const; LengthBox length_box(PropertyID left_id, PropertyID top_id, PropertyID right_id, PropertyID bottom_id, Length const& default_value) const; - Color color_or_fallback(PropertyID, Layout::NodeWithStyle const&, Color fallback) const; + Color color_or_fallback(PropertyID, ColorResolutionContext, Color fallback) const; PreferredColorScheme color_scheme(PreferredColorScheme, Optional const&> document_supported_schemes) const; TextAnchor text_anchor() const; TextAlign text_align() const; diff --git a/Libraries/LibWeb/Layout/Node.cpp b/Libraries/LibWeb/Layout/Node.cpp index b916ab2da91..023d5f1fe80 100644 --- a/Libraries/LibWeb/Layout/Node.cpp +++ b/Libraries/LibWeb/Layout/Node.cpp @@ -393,7 +393,7 @@ void NodeWithStyle::apply_style(CSS::ComputedProperties const& computed_style) // NOTE: color must be set after color-scheme to ensure currentColor can be resolved in other properties (e.g. background-color). // NOTE: color must be set after font_size as `CalculatedStyleValue`s can rely on it being set for resolving lengths. - computed_values.set_color(computed_style.color_or_fallback(CSS::PropertyID::Color, *this, CSS::InitialValues::color())); + computed_values.set_color(computed_style.color_or_fallback(CSS::PropertyID::Color, CSS::ColorResolutionContext::for_layout_node_with_style(*this), CSS::InitialValues::color())); computed_values.set_vertical_align(computed_style.vertical_align()); @@ -527,7 +527,7 @@ void NodeWithStyle::apply_style(CSS::ComputedProperties const& computed_style) computed_values.set_background_layers(move(layers)); } - computed_values.set_background_color(computed_style.color_or_fallback(CSS::PropertyID::BackgroundColor, *this, CSS::InitialValues::background_color())); + computed_values.set_background_color(computed_style.color_or_fallback(CSS::PropertyID::BackgroundColor, CSS::ColorResolutionContext::for_layout_node_with_style(*this), CSS::InitialValues::background_color())); computed_values.set_box_sizing(computed_style.box_sizing()); @@ -738,11 +738,11 @@ void NodeWithStyle::apply_style(CSS::ComputedProperties const& computed_style) // FIXME: The default text decoration color value is `currentcolor`, but since we can't resolve that easily, // we just manually grab the value from `color`. This makes it dependent on `color` being // specified first, so it's far from ideal. - computed_values.set_text_decoration_color(computed_style.color_or_fallback(CSS::PropertyID::TextDecorationColor, *this, computed_values.color())); + computed_values.set_text_decoration_color(computed_style.color_or_fallback(CSS::PropertyID::TextDecorationColor, CSS::ColorResolutionContext::for_layout_node_with_style(*this), computed_values.color())); if (auto maybe_text_decoration_thickness = computed_style.length_percentage(CSS::PropertyID::TextDecorationThickness); maybe_text_decoration_thickness.has_value()) computed_values.set_text_decoration_thickness(maybe_text_decoration_thickness.release_value()); - computed_values.set_webkit_text_fill_color(computed_style.color_or_fallback(CSS::PropertyID::WebkitTextFillColor, *this, computed_values.color())); + computed_values.set_webkit_text_fill_color(computed_style.color_or_fallback(CSS::PropertyID::WebkitTextFillColor, CSS::ColorResolutionContext::for_layout_node_with_style(*this), computed_values.color())); computed_values.set_text_shadow(computed_style.text_shadow(*this)); @@ -817,7 +817,7 @@ void NodeWithStyle::apply_style(CSS::ComputedProperties const& computed_style) // FIXME: The default border color value is `currentcolor`, but since we can't resolve that easily, // we just manually grab the value from `color`. This makes it dependent on `color` being // specified first, so it's far from ideal. - border.color = computed_style.color_or_fallback(color_property, *this, computed_values.color()); + border.color = computed_style.color_or_fallback(color_property, CSS::ColorResolutionContext::for_layout_node_with_style(*this), computed_values.color()); border.line_style = computed_style.line_style(style_property); // https://w3c.github.io/csswg-drafts/css-backgrounds/#border-style