LibWeb: Support creating ColorResolutionContext from AbstractElement

No functionality changes.
This commit is contained in:
Callum Law 2025-07-19 15:38:16 +12:00 committed by Sam Atkins
commit db439d224a
Notes: github-actions[bot] 2025-08-04 10:30:29 +00:00
4 changed files with 31 additions and 0 deletions

View file

@ -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 {

View file

@ -89,6 +89,7 @@ struct ColorResolutionContext {
GC::Ptr<DOM::Document const> document;
CalculationResolutionContext calculation_resolution_context;
[[nodiscard]] static ColorResolutionContext for_element(DOM::AbstractElement const&);
[[nodiscard]] static ColorResolutionContext for_layout_node_with_style(Layout::NodeWithStyle const&);
};

View file

@ -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();

View file

@ -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&);