mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 23:37:43 +00:00
LibWeb: Add Node.insertBefore(Node node, Node? child)
This commit is contained in:
parent
82a75bcf5f
commit
213e2793bd
4 changed files with 43 additions and 0 deletions
|
@ -200,4 +200,16 @@ RefPtr<Node> Node::append_child(NonnullRefPtr<Node> node, bool notify)
|
||||||
return node;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -83,6 +83,7 @@ public:
|
||||||
bool is_parent_node() const { return is_element() || is_document() || is_document_fragment(); }
|
bool is_parent_node() const { return is_element() || is_document() || is_document_fragment(); }
|
||||||
|
|
||||||
RefPtr<Node> append_child(NonnullRefPtr<Node>, bool notify = true);
|
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;
|
virtual RefPtr<LayoutNode> create_layout_node(const StyleProperties* parent_style) const;
|
||||||
|
|
||||||
|
|
|
@ -9,5 +9,7 @@ interface Node : EventTarget {
|
||||||
readonly attribute Element? parentElement;
|
readonly attribute Element? parentElement;
|
||||||
|
|
||||||
Node appendChild(Node node);
|
Node appendChild(Node node);
|
||||||
|
Node insertBefore(Node node, Node? child);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -110,6 +110,7 @@ public:
|
||||||
|
|
||||||
void prepend_child(NonnullRefPtr<T> node);
|
void prepend_child(NonnullRefPtr<T> node);
|
||||||
void append_child(NonnullRefPtr<T> node, bool notify = true);
|
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);
|
NonnullRefPtr<T> remove_child(NonnullRefPtr<T> node);
|
||||||
void donate_all_children_to(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();
|
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>
|
template<typename T>
|
||||||
inline void TreeNode<T>::prepend_child(NonnullRefPtr<T> node)
|
inline void TreeNode<T>::prepend_child(NonnullRefPtr<T> node)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue