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) {