summaryrefslogtreecommitdiff
path: root/Libraries/LibHTML/TreeNode.h
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-11-06 20:25:40 +0100
committerAndreas Kling <awesomekling@gmail.com>2019-11-06 20:26:17 +0100
commit26493a3039c76b9893796227e17dacd53645b115 (patch)
tree1f7ea97b52275e2ea3b4572165128dfb824f9763 /Libraries/LibHTML/TreeNode.h
parent9a5e0652297b0c3f0a3ce4fc519989f52b437807 (diff)
downloadserenity-26493a3039c76b9893796227e17dacd53645b115.zip
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.
Diffstat (limited to 'Libraries/LibHTML/TreeNode.h')
-rw-r--r--Libraries/LibHTML/TreeNode.h24
1 files changed, 24 insertions, 0 deletions
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<T> node, bool call_inserted_into = true);
void append_child(NonnullRefPtr<T> node, bool call_inserted_into = true);
+ NonnullRefPtr<T> remove_child(NonnullRefPtr<T> node, bool call_removed_from = true);
void donate_all_children_to(T& node);
bool is_child_allowed(const T&) const { return true; }
@@ -110,6 +111,29 @@ private:
};
template<typename T>
+inline NonnullRefPtr<T> TreeNode<T>::remove_child(NonnullRefPtr<T> 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<T&>(*this));
+
+ node->deref();
+
+ return node;
+}
+
+template<typename T>
inline void TreeNode<T>::append_child(NonnullRefPtr<T> node, bool call_inserted_into)
{
ASSERT(!node->m_parent);