LibWeb: Add TreeNode<T>::replace_child(new_child, old_child)

This will be used to make local patches to layout trees.
This commit is contained in:
Andreas Kling 2025-01-11 15:29:40 +01:00 committed by Andreas Kling
commit e8b5667a9e
Notes: github-actions[bot] 2025-01-18 20:02:21 +00:00

View file

@ -49,6 +49,8 @@ public:
void insert_before(GC::Ref<T> node, GC::Ptr<T> child);
void remove_child(GC::Ref<T> node);
void replace_child(GC::Ref<T> new_child, GC::Ref<T> old_child);
T* next_in_pre_order()
{
if (first_child())
@ -368,6 +370,28 @@ inline void TreeNode<T>::append_child(GC::Ref<T> node)
m_first_child = m_last_child;
}
template<typename T>
inline void TreeNode<T>::replace_child(GC::Ref<T> new_child, GC::Ref<T> 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<typename T>
inline void TreeNode<T>::insert_before(GC::Ref<T> node, GC::Ptr<T> child)
{