1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 20:47:45 +00:00

LibWeb: Make the layout tree GC-allocated

This removes a set of complex reference cycles between DOM, layout tree
and browsing context.

It also makes lifetimes much easier to reason about, as the DOM and
layout trees are now free to keep each other alive.
This commit is contained in:
Andreas Kling 2022-10-17 14:41:50 +02:00
parent 83c5ff57d8
commit 268b9c5d90
72 changed files with 258 additions and 207 deletions

View file

@ -75,6 +75,8 @@ void Element::visit_edges(Cell::Visitor& visitor)
visitor.visit(m_inline_style.ptr());
visitor.visit(m_class_list.ptr());
visitor.visit(m_shadow_root.ptr());
for (auto& pseudo_element_layout_node : m_pseudo_element_nodes)
visitor.visit(pseudo_element_layout_node);
}
// https://dom.spec.whatwg.org/#dom-element-getattribute
@ -272,7 +274,7 @@ bool Element::has_class(FlyString const& class_name, CaseSensitivity case_sensit
}
}
RefPtr<Layout::Node> Element::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
JS::GCPtr<Layout::Node> Element::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
{
if (local_name() == "noscript" && document().is_scripting_enabled())
return nullptr;
@ -281,41 +283,41 @@ RefPtr<Layout::Node> Element::create_layout_node(NonnullRefPtr<CSS::StylePropert
return create_layout_node_for_display_type(document(), display, move(style), this);
}
RefPtr<Layout::Node> Element::create_layout_node_for_display_type(DOM::Document& document, CSS::Display const& display, NonnullRefPtr<CSS::StyleProperties> style, Element* element)
JS::GCPtr<Layout::Node> Element::create_layout_node_for_display_type(DOM::Document& document, CSS::Display const& display, NonnullRefPtr<CSS::StyleProperties> style, Element* element)
{
if (display.is_table_inside())
return adopt_ref(*new Layout::TableBox(document, element, move(style)));
return document.heap().allocate_without_realm<Layout::TableBox>(document, element, move(style));
if (display.is_list_item())
return adopt_ref(*new Layout::ListItemBox(document, element, move(style)));
return document.heap().allocate_without_realm<Layout::ListItemBox>(document, element, move(style));
if (display.is_table_row())
return adopt_ref(*new Layout::TableRowBox(document, element, move(style)));
return document.heap().allocate_without_realm<Layout::TableRowBox>(document, element, move(style));
if (display.is_table_cell())
return adopt_ref(*new Layout::TableCellBox(document, element, move(style)));
return document.heap().allocate_without_realm<Layout::TableCellBox>(document, element, move(style));
if (display.is_table_row_group() || display.is_table_header_group() || display.is_table_footer_group())
return adopt_ref(*new Layout::TableRowGroupBox(document, element, move(style)));
return document.heap().allocate_without_realm<Layout::TableRowGroupBox>(document, element, move(style));
if (display.is_table_column() || display.is_table_column_group() || display.is_table_caption()) {
// FIXME: This is just an incorrect placeholder until we improve table layout support.
return adopt_ref(*new Layout::BlockContainer(document, element, move(style)));
return document.heap().allocate_without_realm<Layout::BlockContainer>(document, element, move(style));
}
if (display.is_inline_outside()) {
if (display.is_flow_root_inside())
return adopt_ref(*new Layout::BlockContainer(document, element, move(style)));
return document.heap().allocate_without_realm<Layout::BlockContainer>(document, element, move(style));
if (display.is_flow_inside())
return adopt_ref(*new Layout::InlineNode(document, element, move(style)));
return document.heap().allocate_without_realm<Layout::InlineNode>(document, element, move(style));
if (display.is_flex_inside())
return adopt_ref(*new Layout::BlockContainer(document, element, move(style)));
return document.heap().allocate_without_realm<Layout::BlockContainer>(document, element, move(style));
dbgln_if(LIBWEB_CSS_DEBUG, "FIXME: Support display: {}", display.to_string());
return adopt_ref(*new Layout::InlineNode(document, element, move(style)));
return document.heap().allocate_without_realm<Layout::InlineNode>(document, element, move(style));
}
if (display.is_flow_inside() || display.is_flow_root_inside() || display.is_flex_inside() || display.is_grid_inside())
return adopt_ref(*new Layout::BlockContainer(document, element, move(style)));
return document.heap().allocate_without_realm<Layout::BlockContainer>(document, element, move(style));
TODO();
}
@ -709,14 +711,14 @@ void Element::children_changed()
set_needs_style_update(true);
}
void Element::set_pseudo_element_node(Badge<Layout::TreeBuilder>, CSS::Selector::PseudoElement pseudo_element, RefPtr<Layout::Node> pseudo_element_node)
void Element::set_pseudo_element_node(Badge<Layout::TreeBuilder>, CSS::Selector::PseudoElement pseudo_element, JS::GCPtr<Layout::Node> pseudo_element_node)
{
m_pseudo_element_nodes[to_underlying(pseudo_element)] = pseudo_element_node->make_weak_ptr();
m_pseudo_element_nodes[to_underlying(pseudo_element)] = pseudo_element_node;
}
RefPtr<Layout::Node> Element::get_pseudo_element_node(CSS::Selector::PseudoElement pseudo_element) const
JS::GCPtr<Layout::Node> Element::get_pseudo_element_node(CSS::Selector::PseudoElement pseudo_element) const
{
return m_pseudo_element_nodes[to_underlying(pseudo_element)].strong_ref();
return m_pseudo_element_nodes[to_underlying(pseudo_element)];
}
void Element::clear_pseudo_element_nodes(Badge<Layout::TreeBuilder>)