From db439d224adec1ed2e754fde4577affec73422ba Mon Sep 17 00:00:00 2001 From: Callum Law Date: Sat, 19 Jul 2025 15:38:16 +1200 Subject: [PATCH] LibWeb: Support creating ColorResolutionContext from AbstractElement No functionality changes. --- Libraries/LibWeb/CSS/CSSStyleValue.cpp | 14 ++++++++++++++ Libraries/LibWeb/CSS/CSSStyleValue.h | 1 + Libraries/LibWeb/CSS/Length.cpp | 15 +++++++++++++++ Libraries/LibWeb/CSS/Length.h | 1 + 4 files changed, 31 insertions(+) diff --git a/Libraries/LibWeb/CSS/CSSStyleValue.cpp b/Libraries/LibWeb/CSS/CSSStyleValue.cpp index 5a285d04dd6..907e7275976 100644 --- a/Libraries/LibWeb/CSS/CSSStyleValue.cpp +++ b/Libraries/LibWeb/CSS/CSSStyleValue.cpp @@ -75,6 +75,20 @@ namespace Web::CSS { +ColorResolutionContext ColorResolutionContext::for_element(DOM::AbstractElement const& element) +{ + auto color_scheme = element.computed_properties()->color_scheme(element.document().page().preferred_color_scheme(), element.document().supported_color_schemes()); + + CalculationResolutionContext calculation_resolution_context { .length_resolution_context = Length::ResolutionContext::for_element(element) }; + + return { + .color_scheme = color_scheme, + .current_color = element.computed_properties()->color_or_fallback(PropertyID::Color, { color_scheme, CSS::InitialValues::color(), element.document(), calculation_resolution_context }, CSS::InitialValues::color()), + .document = element.document(), + .calculation_resolution_context = calculation_resolution_context + }; +} + ColorResolutionContext ColorResolutionContext::for_layout_node_with_style(Layout::NodeWithStyle const& layout_node) { return { diff --git a/Libraries/LibWeb/CSS/CSSStyleValue.h b/Libraries/LibWeb/CSS/CSSStyleValue.h index 81c5b266716..675a650df23 100644 --- a/Libraries/LibWeb/CSS/CSSStyleValue.h +++ b/Libraries/LibWeb/CSS/CSSStyleValue.h @@ -89,6 +89,7 @@ struct ColorResolutionContext { GC::Ptr document; CalculationResolutionContext calculation_resolution_context; + [[nodiscard]] static ColorResolutionContext for_element(DOM::AbstractElement const&); [[nodiscard]] static ColorResolutionContext for_layout_node_with_style(Layout::NodeWithStyle const&); }; diff --git a/Libraries/LibWeb/CSS/Length.cpp b/Libraries/LibWeb/CSS/Length.cpp index 158a065e30c..3feb8614489 100644 --- a/Libraries/LibWeb/CSS/Length.cpp +++ b/Libraries/LibWeb/CSS/Length.cpp @@ -137,6 +137,21 @@ CSSPixels Length::viewport_relative_length_to_px(CSSPixelRect const& viewport_re } } +Length::ResolutionContext Length::ResolutionContext::for_element(DOM::AbstractElement const& element) +{ + auto const* root_element = element.element().document().document_element(); + + VERIFY(element.computed_properties()); + VERIFY(root_element); + VERIFY(root_element->computed_properties()); + + return Length::ResolutionContext { + .viewport_rect = element.element().navigable()->viewport_rect(), + .font_metrics = { element.computed_properties()->font_size(), element.computed_properties()->first_available_computed_font().pixel_metrics() }, + .root_font_metrics = { root_element->computed_properties()->font_size(), root_element->computed_properties()->first_available_computed_font().pixel_metrics() } + }; +} + Length::ResolutionContext Length::ResolutionContext::for_window(HTML::Window const& window) { auto const& initial_font = window.associated_document().style_computer().initial_font(); diff --git a/Libraries/LibWeb/CSS/Length.h b/Libraries/LibWeb/CSS/Length.h index b11b8a8d28c..4f84b938d0b 100644 --- a/Libraries/LibWeb/CSS/Length.h +++ b/Libraries/LibWeb/CSS/Length.h @@ -162,6 +162,7 @@ public: StringView unit_name() const; struct ResolutionContext { + [[nodiscard]] static ResolutionContext for_element(DOM::AbstractElement const&); [[nodiscard]] static ResolutionContext for_window(HTML::Window const&); [[nodiscard]] static ResolutionContext for_layout_node(Layout::Node const&);