LibWeb: Implement ParentNode.replaceChildren

This commit is contained in:
Luke Wilde 2022-01-29 21:01:09 +00:00 committed by Andreas Kling
parent 34dfdc3f37
commit d1bc9358c1
Notes: sideshowbarker 2024-07-17 19:56:24 +09:00
5 changed files with 23 additions and 0 deletions

View file

@ -67,6 +67,7 @@ interface Document : Node {
[CEReactions, Unscopable] undefined prepend((Node or DOMString)... nodes);
[CEReactions, Unscopable] undefined append((Node or DOMString)... nodes);
[CEReactions, Unscopable] undefined replaceChildren((Node or DOMString)... nodes);
Element? querySelector(DOMString selectors);
[NewObject] NodeList querySelectorAll(DOMString selectors);

View file

@ -11,6 +11,7 @@ interface DocumentFragment : Node {
[CEReactions, Unscopable] undefined prepend((Node or DOMString)... nodes);
[CEReactions, Unscopable] undefined append((Node or DOMString)... nodes);
[CEReactions, Unscopable] undefined replaceChildren((Node or DOMString)... nodes);
Element? querySelector(DOMString selectors);
[NewObject] NodeList querySelectorAll(DOMString selectors);

View file

@ -40,6 +40,7 @@ interface Element : Node {
[CEReactions, Unscopable] undefined prepend((Node or DOMString)... nodes);
[CEReactions, Unscopable] undefined append((Node or DOMString)... nodes);
[CEReactions, Unscopable] undefined replaceChildren((Node or DOMString)... nodes);
Element? querySelector(DOMString selectors);
[NewObject] NodeList querySelectorAll(DOMString selectors);

View file

@ -191,4 +191,23 @@ ExceptionOr<void> ParentNode::append(Vector<Variant<NonnullRefPtr<Node>, String>
return {};
}
ExceptionOr<void> ParentNode::replace_children(Vector<Variant<NonnullRefPtr<Node>, String>> const& nodes)
{
// 1. Let node be the result of converting nodes into a node given nodes and thiss node document.
auto node_or_exception = convert_nodes_to_single_node(nodes, document());
if (node_or_exception.is_exception())
return node_or_exception.exception();
auto node = node_or_exception.release_value();
// 2. Ensure pre-insertion validity of node into this before null.
auto validity_exception = ensure_pre_insertion_validity(node, nullptr);
if (validity_exception.is_exception())
return validity_exception.exception();
// 3. Replace all with node within this.
replace_all(node);
return {};
}
}

View file

@ -32,6 +32,7 @@ public:
ExceptionOr<void> prepend(Vector<Variant<NonnullRefPtr<Node>, String>> const& nodes);
ExceptionOr<void> append(Vector<Variant<NonnullRefPtr<Node>, String>> const& nodes);
ExceptionOr<void> replace_children(Vector<Variant<NonnullRefPtr<Node>, String>> const& nodes);
protected:
ParentNode(Document& document, NodeType type)