diff --git a/Userland/Libraries/LibWeb/DOM/Document.idl b/Userland/Libraries/LibWeb/DOM/Document.idl index 173b73cca7..275a75941f 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.idl +++ b/Userland/Libraries/LibWeb/DOM/Document.idl @@ -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); diff --git a/Userland/Libraries/LibWeb/DOM/DocumentFragment.idl b/Userland/Libraries/LibWeb/DOM/DocumentFragment.idl index 5309a72957..2735e9c70c 100644 --- a/Userland/Libraries/LibWeb/DOM/DocumentFragment.idl +++ b/Userland/Libraries/LibWeb/DOM/DocumentFragment.idl @@ -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); diff --git a/Userland/Libraries/LibWeb/DOM/Element.idl b/Userland/Libraries/LibWeb/DOM/Element.idl index 66da1ec84f..41d664994d 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.idl +++ b/Userland/Libraries/LibWeb/DOM/Element.idl @@ -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); diff --git a/Userland/Libraries/LibWeb/DOM/ParentNode.cpp b/Userland/Libraries/LibWeb/DOM/ParentNode.cpp index b2587bb396..dde6fc9b43 100644 --- a/Userland/Libraries/LibWeb/DOM/ParentNode.cpp +++ b/Userland/Libraries/LibWeb/DOM/ParentNode.cpp @@ -191,4 +191,23 @@ ExceptionOr ParentNode::append(Vector, String> return {}; } +ExceptionOr ParentNode::replace_children(Vector, String>> const& nodes) +{ + // 1. Let node be the result of converting nodes into a node given nodes and this’s 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 {}; +} + } diff --git a/Userland/Libraries/LibWeb/DOM/ParentNode.h b/Userland/Libraries/LibWeb/DOM/ParentNode.h index e3ee6abf08..1b8c3e3c5c 100644 --- a/Userland/Libraries/LibWeb/DOM/ParentNode.h +++ b/Userland/Libraries/LibWeb/DOM/ParentNode.h @@ -32,6 +32,7 @@ public: ExceptionOr prepend(Vector, String>> const& nodes); ExceptionOr append(Vector, String>> const& nodes); + ExceptionOr replace_children(Vector, String>> const& nodes); protected: ParentNode(Document& document, NodeType type)