LibWeb: Tidy up Layout::TreeBuilder ancestor stack a little bit

- Make it a vector of references since we never put null pointers on
  the ancestor stack.

- Use Vector::in_reverse() to iterate backwards.
This commit is contained in:
Andreas Kling 2022-04-13 18:44:14 +02:00
parent 118d381091
commit 6712bbc0ee
Notes: sideshowbarker 2024-07-17 11:50:09 +09:00
2 changed files with 9 additions and 9 deletions

View File

@ -146,9 +146,9 @@ void TreeBuilder::create_layout_tree(DOM::Node& dom_node, TreeBuilder::Context&
return; return;
auto insert_node_into_inline_or_block_ancestor = [this](auto& node, bool prepend = false) { auto insert_node_into_inline_or_block_ancestor = [this](auto& node, bool prepend = false) {
if (node->is_inline() && !(node->is_inline_block() && m_parent_stack.last()->computed_values().display().is_flex_inside())) { if (node->is_inline() && !(node->is_inline_block() && m_ancestor_stack.last().computed_values().display().is_flex_inside())) {
// Inlines can be inserted into the nearest ancestor. // Inlines can be inserted into the nearest ancestor.
auto& insertion_point = insertion_parent_for_inline_node(*m_parent_stack.last()); auto& insertion_point = insertion_parent_for_inline_node(m_ancestor_stack.last());
if (prepend) if (prepend)
insertion_point.prepend_child(*node); insertion_point.prepend_child(*node);
else else
@ -157,9 +157,9 @@ void TreeBuilder::create_layout_tree(DOM::Node& dom_node, TreeBuilder::Context&
} else { } else {
// Non-inlines can't be inserted into an inline parent, so find the nearest non-inline ancestor. // Non-inlines can't be inserted into an inline parent, so find the nearest non-inline ancestor.
auto& nearest_non_inline_ancestor = [&]() -> Layout::NodeWithStyle& { auto& nearest_non_inline_ancestor = [&]() -> Layout::NodeWithStyle& {
for (ssize_t i = m_parent_stack.size() - 1; i >= 0; --i) { for (auto& ancestor : m_ancestor_stack.in_reverse()) {
if (!m_parent_stack[i]->is_inline() || m_parent_stack[i]->is_inline_block()) if (!ancestor.is_inline() || ancestor.is_inline_block())
return *m_parent_stack[i]; return ancestor;
} }
VERIFY_NOT_REACHED(); VERIFY_NOT_REACHED();
}(); }();
@ -178,7 +178,7 @@ void TreeBuilder::create_layout_tree(DOM::Node& dom_node, TreeBuilder::Context&
if (!dom_node.parent_or_shadow_host()) { if (!dom_node.parent_or_shadow_host()) {
m_layout_root = layout_node; m_layout_root = layout_node;
} else if (layout_node->is_svg_box()) { } else if (layout_node->is_svg_box()) {
m_parent_stack.last()->append_child(*layout_node); m_ancestor_stack.last().append_child(*layout_node);
} else { } else {
insert_node_into_inline_or_block_ancestor(layout_node); insert_node_into_inline_or_block_ancestor(layout_node);
} }

View File

@ -26,8 +26,8 @@ private:
void create_layout_tree(DOM::Node&, Context&); void create_layout_tree(DOM::Node&, Context&);
void push_parent(Layout::NodeWithStyle& node) { m_parent_stack.append(&node); } void push_parent(Layout::NodeWithStyle& node) { m_ancestor_stack.append(node); }
void pop_parent() { m_parent_stack.take_last(); } void pop_parent() { m_ancestor_stack.take_last(); }
template<CSS::Display::Internal, typename Callback> template<CSS::Display::Internal, typename Callback>
void for_each_in_tree_with_internal_display(NodeWithStyle& root, Callback); void for_each_in_tree_with_internal_display(NodeWithStyle& root, Callback);
@ -41,7 +41,7 @@ private:
void generate_missing_parents(NodeWithStyle& root); void generate_missing_parents(NodeWithStyle& root);
RefPtr<Layout::Node> m_layout_root; RefPtr<Layout::Node> m_layout_root;
Vector<Layout::NodeWithStyle*> m_parent_stack; Vector<Layout::NodeWithStyle&> m_ancestor_stack;
}; };
} }