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.
This commit is contained in:
Callum Law 2025-07-19 13:28:14 +12:00 committed by Sam Atkins
commit a19a6deaa4
Notes: github-actions[bot] 2025-08-04 10:30:45 +00:00
3 changed files with 8 additions and 8 deletions

View file

@ -224,12 +224,12 @@ LengthBox ComputedProperties::length_box(PropertyID left_id, PropertyID top_id,
return box; 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); auto const& value = property(id);
if (!value.has_color()) if (!value.has_color())
return fallback; 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 // https://drafts.csswg.org/css-color-adjust-1/#determine-the-used-color-scheme

View file

@ -77,7 +77,7 @@ public:
LengthPercentage length_percentage_or_fallback(PropertyID, LengthPercentage const& fallback) const; LengthPercentage length_percentage_or_fallback(PropertyID, LengthPercentage const& fallback) const;
Optional<LengthPercentage> length_percentage(PropertyID) const; Optional<LengthPercentage> length_percentage(PropertyID) const;
LengthBox length_box(PropertyID left_id, PropertyID top_id, PropertyID right_id, PropertyID bottom_id, Length const& default_value) 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<Vector<String> const&> document_supported_schemes) const; PreferredColorScheme color_scheme(PreferredColorScheme, Optional<Vector<String> const&> document_supported_schemes) const;
TextAnchor text_anchor() const; TextAnchor text_anchor() const;
TextAlign text_align() const; TextAlign text_align() const;

View file

@ -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 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. // 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()); 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_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()); 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, // 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 // we just manually grab the value from `color`. This makes it dependent on `color` being
// specified first, so it's far from ideal. // 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()) 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_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)); 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, // 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 // we just manually grab the value from `color`. This makes it dependent on `color` being
// specified first, so it's far from ideal. // 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); border.line_style = computed_style.line_style(style_property);
// https://w3c.github.io/csswg-drafts/css-backgrounds/#border-style // https://w3c.github.io/csswg-drafts/css-backgrounds/#border-style