Forráskód Böngészése

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.
Andreas Kling 3 éve
szülő
commit
6712bbc0ee

+ 6 - 6
Userland/Libraries/LibWeb/Layout/TreeBuilder.cpp

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

+ 3 - 3
Userland/Libraries/LibWeb/Layout/TreeBuilder.h

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