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.
This commit is contained in:
Callum Law 2025-07-03 20:39:23 +12:00 committed by Sam Atkins
commit b468923372
Notes: github-actions[bot] 2025-07-04 12:20:21 +00:00

View file

@ -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<DOM::Document>(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() },
};
}