From eaf7e6840806a68d38321aa0097946db65abad46 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 10 Aug 2020 13:52:49 +0200 Subject: [PATCH] LibWeb: Move tree iteration helpers from Node/LayoutNode to TreeNode Since these are generally useful in our trees, let's just keep them in TreeNode instead of duplicating the helpers in subclasses. --- Libraries/LibWeb/DOM/Node.h | 26 ----- Libraries/LibWeb/Layout/LayoutNode.h | 138 --------------------------- Libraries/LibWeb/TreeNode.h | 92 ++++++++++++++++++ 3 files changed, 92 insertions(+), 164 deletions(-) diff --git a/Libraries/LibWeb/DOM/Node.h b/Libraries/LibWeb/DOM/Node.h index 5fb56d2c11..c46ccc07e9 100644 --- a/Libraries/LibWeb/DOM/Node.h +++ b/Libraries/LibWeb/DOM/Node.h @@ -106,12 +106,6 @@ public: Element* parent_element(); const Element* parent_element() const; - template - const T* first_child_of_type() const; - - template - const T* first_ancestor_of_type() const; - virtual void inserted_into(Node&) { } virtual void removed_from(Node&) { } virtual void children_changed() { } @@ -144,24 +138,4 @@ protected: bool m_needs_style_update { true }; }; -template -inline const T* Node::first_child_of_type() const -{ - for (auto* child = first_child(); child; child = child->next_sibling()) { - if (is(*child)) - return downcast(child); - } - return nullptr; -} - -template -inline const T* Node::first_ancestor_of_type() const -{ - for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) { - if (is(*ancestor)) - return downcast(ancestor); - } - return nullptr; -} - } diff --git a/Libraries/LibWeb/Layout/LayoutNode.h b/Libraries/LibWeb/Layout/LayoutNode.h index a3c7919b19..67b8e9d1b9 100644 --- a/Libraries/LibWeb/Layout/LayoutNode.h +++ b/Libraries/LibWeb/Layout/LayoutNode.h @@ -69,40 +69,6 @@ public: const LayoutDocument& root() const; LayoutDocument& root(); - template - inline void for_each_child(Callback callback) const - { - for (auto* node = first_child(); node; node = node->next_sibling()) - callback(*node); - } - - template - inline void for_each_child(Callback callback) - { - for (auto* node = first_child(); node; node = node->next_sibling()) - callback(*node); - } - - template - inline void for_each_child_of_type(Callback callback) - { - for (auto* node = first_child(); node; node = node->next_sibling()) { - if (!is(node)) - continue; - callback(downcast(*node)); - } - } - - template - inline void for_each_child_of_type(Callback callback) const - { - for (auto* node = first_child(); node; node = node->next_sibling()) { - if (!is(node)) - continue; - callback(downcast(*node)); - } - } - virtual const char* class_name() const = 0; virtual bool is_root() const { return false; } virtual bool is_text() const { return false; } @@ -171,30 +137,6 @@ public: bool children_are_inline() const { return m_children_are_inline; } void set_children_are_inline(bool value) { m_children_are_inline = value; } - template - const U* next_sibling_of_type() const; - - template - U* next_sibling_of_type(); - - template - const U* previous_sibling_of_type() const; - - template - U* previous_sibling_of_type(); - - template - const T* first_child_of_type() const; - - template - T* first_child_of_type(); - - template - const T* first_ancestor_of_type() const; - - template - T* first_ancestor_of_type(); - Gfx::FloatPoint box_type_agnostic_position() const; float font_size() const; @@ -275,86 +217,6 @@ inline LayoutNodeWithStyle* LayoutNode::parent() return static_cast(TreeNode::parent()); } -template -inline const T* LayoutNode::next_sibling_of_type() const -{ - for (auto* sibling = next_sibling(); sibling; sibling = sibling->next_sibling()) { - if (is(*sibling)) - return &downcast(*sibling); - } - return nullptr; -} - -template -inline T* LayoutNode::next_sibling_of_type() -{ - for (auto* sibling = next_sibling(); sibling; sibling = sibling->next_sibling()) { - if (is(*sibling)) - return &downcast(*sibling); - } - return nullptr; -} - -template -inline const T* LayoutNode::previous_sibling_of_type() const -{ - for (auto* sibling = previous_sibling(); sibling; sibling = sibling->previous_sibling()) { - if (is(*sibling)) - return &downcast(*sibling); - } - return nullptr; -} - -template -inline T* LayoutNode::previous_sibling_of_type() -{ - for (auto* sibling = previous_sibling(); sibling; sibling = sibling->previous_sibling()) { - if (is(*sibling)) - return &downcast(*sibling); - } - return nullptr; -} - -template -inline const T* LayoutNode::first_child_of_type() const -{ - for (auto* child = first_child(); child; child = child->next_sibling()) { - if (is(*child)) - return &downcast(*child); - } - return nullptr; -} - -template -inline T* LayoutNode::first_child_of_type() -{ - for (auto* child = first_child(); child; child = child->next_sibling()) { - if (is(*child)) - return &downcast(*child); - } - return nullptr; -} - -template -inline const T* LayoutNode::first_ancestor_of_type() const -{ - for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) { - if (is(*ancestor)) - return &downcast(*ancestor); - } - return nullptr; -} - -template -inline T* LayoutNode::first_ancestor_of_type() -{ - for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) { - if (is(*ancestor)) - return &downcast(*ancestor); - } - return nullptr; -} - } AK_BEGIN_TYPE_TRAITS(Web::LayoutNodeWithStyle) diff --git a/Libraries/LibWeb/TreeNode.h b/Libraries/LibWeb/TreeNode.h index 61f08a2732..4512863f18 100644 --- a/Libraries/LibWeb/TreeNode.h +++ b/Libraries/LibWeb/TreeNode.h @@ -191,6 +191,98 @@ public: return IterationDecision::Continue; } + template + void for_each_child(Callback callback) const + { + return const_cast(this)->template for_each_child(move(callback)); + } + + template + void for_each_child(Callback callback) + { + for (auto* node = first_child(); node; node = node->next_sibling()) + callback(*node); + } + + template + void for_each_child_of_type(Callback callback) + { + for (auto* node = first_child(); node; node = node->next_sibling()) { + if (is(node)) + callback(downcast(*node)); + } + } + + template + void for_each_child_of_type(Callback callback) const + { + return const_cast(this)->template for_each_child_of_type(move(callback)); + } + + template + const U* next_sibling_of_type() const + { + return const_cast(this)->template next_sibling_of_type(); + } + + template + inline U* next_sibling_of_type() + { + for (auto* sibling = next_sibling(); sibling; sibling = sibling->next_sibling()) { + if (is(*sibling)) + return &downcast(*sibling); + } + return nullptr; + } + + template + const U* previous_sibling_of_type() const + { + return const_cast(this)->template previous_sibling_of_type(); + } + + template + U* previous_sibling_of_type() + { + for (auto* sibling = previous_sibling(); sibling; sibling = sibling->previous_sibling()) { + if (is(*sibling)) + return &downcast(*sibling); + } + return nullptr; + } + + template + const U* first_child_of_type() const + { + return const_cast(this)->template first_child_of_type(); + } + + template + U* first_child_of_type() + { + for (auto* child = first_child(); child; child = child->next_sibling()) { + if (is(*child)) + return &downcast(*child); + } + return nullptr; + } + + template + const U* first_ancestor_of_type() const + { + return const_cast(this)->template first_ancestor_of_type(); + } + + template + U* first_ancestor_of_type() + { + for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) { + if (is(*ancestor)) + return &downcast(*ancestor); + } + return nullptr; + } + protected: TreeNode() { }