From b4689233720f95f8673b19834b302207dbf9869b Mon Sep 17 00:00:00 2001 From: Callum Law Date: Thu, 3 Jul 2025 20:39:23 +1200 Subject: [PATCH] LibWeb: Allow creating `Length::ResolutionContext` with provided root Previously we could not create a `Length::ResolutionContext` using the `for_layout_node` method if the provided node's document did not have a layout node. We now provide a workaround for this in the case that the provided layout is that root layout node. This is useful for instance if we want to create a length resolution context when calling `NodeWithStyle::apply_style` on the root node. --- Libraries/LibWeb/CSS/Length.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/Libraries/LibWeb/CSS/Length.cpp b/Libraries/LibWeb/CSS/Length.cpp index 7ef46861b14..f93a5ca1641 100644 --- a/Libraries/LibWeb/CSS/Length.cpp +++ b/Libraries/LibWeb/CSS/Length.cpp @@ -151,13 +151,21 @@ Length::ResolutionContext Length::ResolutionContext::for_window(HTML::Window con Length::ResolutionContext Length::ResolutionContext::for_layout_node(Layout::Node const& node) { - auto const* root_element = node.document().document_element(); - VERIFY(root_element); - VERIFY(root_element->layout_node()); + Layout::Node const* root_layout_node; + + if (is(node.dom_node())) { + root_layout_node = &node; + } else { + auto const* root_element = node.document().document_element(); + VERIFY(root_element); + VERIFY(root_element->layout_node()); + root_layout_node = root_element->layout_node(); + } + return Length::ResolutionContext { .viewport_rect = node.navigable()->viewport_rect(), .font_metrics = { node.computed_values().font_size(), node.first_available_font().pixel_metrics() }, - .root_font_metrics = { root_element->layout_node()->computed_values().font_size(), root_element->layout_node()->first_available_font().pixel_metrics() }, + .root_font_metrics = { root_layout_node->computed_values().font_size(), root_layout_node->first_available_font().pixel_metrics() }, }; }