From 26493a3039c76b9893796227e17dacd53645b115 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 6 Nov 2019 20:25:40 +0100 Subject: LibHTML: Add TreeNode::remove_child() This removes a child from the tree and returns it to the caller. It optionally (but by default) calls removed_from(parent) on the child. --- Libraries/LibHTML/TreeNode.h | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/Libraries/LibHTML/TreeNode.h b/Libraries/LibHTML/TreeNode.h index 8461fd3160..ae84db165b 100644 --- a/Libraries/LibHTML/TreeNode.h +++ b/Libraries/LibHTML/TreeNode.h @@ -49,6 +49,7 @@ public: void prepend_child(NonnullRefPtr node, bool call_inserted_into = true); void append_child(NonnullRefPtr node, bool call_inserted_into = true); + NonnullRefPtr remove_child(NonnullRefPtr node, bool call_removed_from = true); void donate_all_children_to(T& node); bool is_child_allowed(const T&) const { return true; } @@ -109,6 +110,29 @@ private: T* m_previous_sibling { nullptr }; }; +template +inline NonnullRefPtr TreeNode::remove_child(NonnullRefPtr node, bool call_removed_from) +{ + ASSERT(node->m_parent == this); + + if (m_first_child == node) + m_first_child = node->m_next_sibling; + + if (m_last_child == node) + m_last_child = node->m_previous_sibling; + + node->m_next_sibling = nullptr; + node->m_previous_sibling = nullptr; + node->m_parent = nullptr; + + if (call_removed_from) + node->removed_from(static_cast(*this)); + + node->deref(); + + return node; +} + template inline void TreeNode::append_child(NonnullRefPtr node, bool call_inserted_into) { -- cgit v1.2.3