From e8b5667a9e54a296942bb6957b234ae817cdeb04 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 11 Jan 2025 15:29:40 +0100 Subject: [PATCH] LibWeb: Add TreeNode::replace_child(new_child, old_child) This will be used to make local patches to layout trees. --- Libraries/LibWeb/TreeNode.h | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/Libraries/LibWeb/TreeNode.h b/Libraries/LibWeb/TreeNode.h index 498b7cedcde..c54a7e67b04 100644 --- a/Libraries/LibWeb/TreeNode.h +++ b/Libraries/LibWeb/TreeNode.h @@ -49,6 +49,8 @@ public: void insert_before(GC::Ref node, GC::Ptr child); void remove_child(GC::Ref node); + void replace_child(GC::Ref new_child, GC::Ref old_child); + T* next_in_pre_order() { if (first_child()) @@ -368,6 +370,28 @@ inline void TreeNode::append_child(GC::Ref node) m_first_child = m_last_child; } +template +inline void TreeNode::replace_child(GC::Ref new_child, GC::Ref old_child) +{ + VERIFY(old_child != new_child); + VERIFY(old_child->m_parent == this); + VERIFY(new_child->m_parent == nullptr); + if (m_first_child == old_child) + m_first_child = new_child; + if (m_last_child == old_child) + m_last_child = new_child; + new_child->m_next_sibling = old_child->m_next_sibling; + if (new_child->m_next_sibling) + new_child->m_next_sibling->m_previous_sibling = new_child; + new_child->m_previous_sibling = old_child->m_previous_sibling; + if (new_child->m_previous_sibling) + new_child->m_previous_sibling->m_next_sibling = new_child; + new_child->m_parent = old_child->m_parent; + old_child->m_next_sibling = nullptr; + old_child->m_previous_sibling = nullptr; + old_child->m_parent = nullptr; +} + template inline void TreeNode::insert_before(GC::Ref node, GC::Ptr child) {