LibWeb: Add Node.insertBefore(Node node, Node? child)

This commit is contained in:
Andreas Kling 2020-06-21 16:45:21 +02:00
parent 82a75bcf5f
commit 213e2793bd
Notes: sideshowbarker 2024-07-19 05:28:34 +09:00
4 changed files with 43 additions and 0 deletions

View file

@ -200,4 +200,16 @@ RefPtr<Node> Node::append_child(NonnullRefPtr<Node> node, bool notify)
return node;
}
RefPtr<Node> Node::insert_before(NonnullRefPtr<Node> node, RefPtr<Node> child, bool notify)
{
if (!child)
return append_child(move(node), notify);
if (child->parent_node() != this) {
dbg() << "FIXME: Trying to insert_before() a bogus child";
return nullptr;
}
TreeNode<Node>::insert_before(node, child, notify);
return node;
}
}

View file

@ -83,6 +83,7 @@ public:
bool is_parent_node() const { return is_element() || is_document() || is_document_fragment(); }
RefPtr<Node> append_child(NonnullRefPtr<Node>, bool notify = true);
RefPtr<Node> insert_before(NonnullRefPtr<Node> node, RefPtr<Node> child, bool notify = true);
virtual RefPtr<LayoutNode> create_layout_node(const StyleProperties* parent_style) const;

View file

@ -9,5 +9,7 @@ interface Node : EventTarget {
readonly attribute Element? parentElement;
Node appendChild(Node node);
Node insertBefore(Node node, Node? child);
}

View file

@ -110,6 +110,7 @@ public:
void prepend_child(NonnullRefPtr<T> node);
void append_child(NonnullRefPtr<T> node, bool notify = true);
void insert_before(NonnullRefPtr<T> node, RefPtr<T> child, bool notify = true);
NonnullRefPtr<T> remove_child(NonnullRefPtr<T> node);
void donate_all_children_to(T& node);
@ -252,6 +253,33 @@ inline void TreeNode<T>::append_child(NonnullRefPtr<T> node, bool notify)
static_cast<T*>(this)->children_changed();
}
template<typename T>
inline void TreeNode<T>::insert_before(NonnullRefPtr<T> node, RefPtr<T> child, bool notify)
{
if (!child)
return append_child(move(node), notify);
ASSERT(!node->m_parent);
ASSERT(child->parent() == this);
if (!static_cast<T*>(this)->is_child_allowed(*node))
return;
node->m_previous_sibling = child->m_previous_sibling;
node->m_next_sibling = child;
if (m_first_child == child)
m_first_child = node;
node->m_parent = static_cast<T*>(this);
if (notify)
node->inserted_into(static_cast<T&>(*this));
(void)node.leak_ref();
if (notify)
static_cast<T*>(this)->children_changed();
}
template<typename T>
inline void TreeNode<T>::prepend_child(NonnullRefPtr<T> node)
{