mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 15:57:35 +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:
parent
83c5ff57d8
commit
268b9c5d90
72 changed files with 258 additions and 207 deletions
|
@ -683,7 +683,7 @@ void Document::tear_down_layout_tree()
|
|||
// Gather up all the layout nodes in a vector and detach them from parents
|
||||
// while the vector keeps them alive.
|
||||
|
||||
NonnullRefPtrVector<Layout::Node> layout_nodes;
|
||||
Vector<JS::Handle<Layout::Node>> layout_nodes;
|
||||
|
||||
m_layout_root->for_each_in_inclusive_subtree([&](auto& layout_node) {
|
||||
layout_nodes.append(layout_node);
|
||||
|
@ -691,8 +691,8 @@ void Document::tear_down_layout_tree()
|
|||
});
|
||||
|
||||
for (auto& layout_node : layout_nodes) {
|
||||
if (layout_node.parent())
|
||||
layout_node.parent()->remove_child(layout_node);
|
||||
if (layout_node->parent())
|
||||
layout_node->parent()->remove_child(*layout_node);
|
||||
}
|
||||
|
||||
m_layout_root = nullptr;
|
||||
|
@ -816,7 +816,7 @@ void Document::update_layout()
|
|||
if (!m_layout_root) {
|
||||
m_next_layout_node_serial_id = 0;
|
||||
Layout::TreeBuilder tree_builder;
|
||||
m_layout_root = static_ptr_cast<Layout::InitialContainingBlock>(tree_builder.build(*this));
|
||||
m_layout_root = verify_cast<Layout::InitialContainingBlock>(*tree_builder.build(*this));
|
||||
}
|
||||
|
||||
Layout::LayoutState layout_state;
|
||||
|
|
|
@ -469,7 +469,7 @@ private:
|
|||
|
||||
JS::GCPtr<HTML::Window> m_window;
|
||||
|
||||
RefPtr<Layout::InitialContainingBlock> m_layout_root;
|
||||
JS::GCPtr<Layout::InitialContainingBlock> m_layout_root;
|
||||
|
||||
Optional<Color> m_link_color;
|
||||
Optional<Color> m_active_link_color;
|
||||
|
|
|
@ -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>)
|
||||
|
|
|
@ -142,15 +142,15 @@ public:
|
|||
JS::NonnullGCPtr<Geometry::DOMRect> get_bounding_client_rect() const;
|
||||
JS::NonnullGCPtr<Geometry::DOMRectList> get_client_rects() const;
|
||||
|
||||
virtual RefPtr<Layout::Node> create_layout_node(NonnullRefPtr<CSS::StyleProperties>);
|
||||
virtual JS::GCPtr<Layout::Node> create_layout_node(NonnullRefPtr<CSS::StyleProperties>);
|
||||
|
||||
virtual void did_receive_focus() { }
|
||||
virtual void did_lose_focus() { }
|
||||
|
||||
static RefPtr<Layout::Node> create_layout_node_for_display_type(DOM::Document&, CSS::Display const&, NonnullRefPtr<CSS::StyleProperties>, Element*);
|
||||
static JS::GCPtr<Layout::Node> create_layout_node_for_display_type(DOM::Document&, CSS::Display const&, NonnullRefPtr<CSS::StyleProperties>, Element*);
|
||||
|
||||
void set_pseudo_element_node(Badge<Layout::TreeBuilder>, CSS::Selector::PseudoElement, RefPtr<Layout::Node>);
|
||||
RefPtr<Layout::Node> get_pseudo_element_node(CSS::Selector::PseudoElement) const;
|
||||
void set_pseudo_element_node(Badge<Layout::TreeBuilder>, CSS::Selector::PseudoElement, JS::GCPtr<Layout::Node>);
|
||||
JS::GCPtr<Layout::Node> get_pseudo_element_node(CSS::Selector::PseudoElement) const;
|
||||
void clear_pseudo_element_nodes(Badge<Layout::TreeBuilder>);
|
||||
void serialize_pseudo_elements_as_json(JsonArraySerializer<StringBuilder>& children_array) const;
|
||||
|
||||
|
@ -188,7 +188,7 @@ private:
|
|||
|
||||
Vector<FlyString> m_classes;
|
||||
|
||||
Array<WeakPtr<Layout::Node>, CSS::Selector::PseudoElementCount> m_pseudo_element_nodes;
|
||||
Array<JS::GCPtr<Layout::Node>, CSS::Selector::PseudoElementCount> m_pseudo_element_nodes;
|
||||
};
|
||||
|
||||
template<>
|
||||
|
|
|
@ -92,6 +92,8 @@ void Node::visit_edges(Cell::Visitor& visitor)
|
|||
visitor.visit(m_previous_sibling.ptr());
|
||||
visitor.visit(m_child_nodes);
|
||||
|
||||
visitor.visit(m_layout_node);
|
||||
|
||||
for (auto& registered_observer : m_registered_observer_list)
|
||||
visitor.visit(registered_observer);
|
||||
}
|
||||
|
@ -823,11 +825,16 @@ bool Node::is_editable() const
|
|||
return parent() && parent()->is_editable();
|
||||
}
|
||||
|
||||
void Node::set_layout_node(Badge<Layout::Node>, Layout::Node* layout_node) const
|
||||
void Node::set_layout_node(Badge<Layout::Node>, JS::NonnullGCPtr<Layout::Node> layout_node)
|
||||
{
|
||||
m_layout_node = layout_node;
|
||||
}
|
||||
|
||||
void Node::detach_layout_node(Badge<DOM::Document>)
|
||||
{
|
||||
m_layout_node = nullptr;
|
||||
}
|
||||
|
||||
EventTarget* Node::get_parent(Event const&)
|
||||
{
|
||||
// FIXME: returns the node’s assigned slot, if node is assigned, and node’s parent otherwise.
|
||||
|
|
|
@ -156,7 +156,8 @@ public:
|
|||
Painting::PaintableBox const* paint_box() const;
|
||||
Painting::Paintable const* paintable() const;
|
||||
|
||||
void set_layout_node(Badge<Layout::Node>, Layout::Node*) const;
|
||||
void set_layout_node(Badge<Layout::Node>, JS::NonnullGCPtr<Layout::Node>);
|
||||
void detach_layout_node(Badge<DOM::Document>);
|
||||
|
||||
virtual bool is_child_allowed(Node const&) const { return true; }
|
||||
|
||||
|
@ -623,7 +624,7 @@ protected:
|
|||
virtual void visit_edges(Cell::Visitor&) override;
|
||||
|
||||
JS::GCPtr<Document> m_document;
|
||||
mutable WeakPtr<Layout::Node> m_layout_node;
|
||||
JS::GCPtr<Layout::Node> m_layout_node;
|
||||
NodeType m_type { NodeType::INVALID };
|
||||
bool m_needs_style_update { false };
|
||||
bool m_child_needs_style_update { false };
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue