summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorLuke Wilde <lukew@serenityos.org>2022-01-29 21:01:09 +0000
committerAndreas Kling <kling@serenityos.org>2022-01-31 15:25:36 +0100
commitd1bc9358c1949506bbff7dd381b1dfa1e49e5b44 (patch)
treed1f12f4c2194be3b33232412989e4ac23b2db03e /Userland
parent34dfdc3f37324ab346bb4feb9b15ca99b5d4fca5 (diff)
downloadserenity-d1bc9358c1949506bbff7dd381b1dfa1e49e5b44.zip
LibWeb: Implement ParentNode.replaceChildren
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibWeb/DOM/Document.idl1
-rw-r--r--Userland/Libraries/LibWeb/DOM/DocumentFragment.idl1
-rw-r--r--Userland/Libraries/LibWeb/DOM/Element.idl1
-rw-r--r--Userland/Libraries/LibWeb/DOM/ParentNode.cpp19
-rw-r--r--Userland/Libraries/LibWeb/DOM/ParentNode.h1
5 files changed, 23 insertions, 0 deletions
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<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 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<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)