diff --git a/components/script/dom/document.rs b/components/script/dom/document.rs index 8b6cce24155..8da8e52b7f6 100644 --- a/components/script/dom/document.rs +++ b/components/script/dom/document.rs @@ -4753,6 +4753,11 @@ impl DocumentMethods for Document { self.upcast::().append(nodes) } + // https://dom.spec.whatwg.org/#dom-parentnode-replacechildren + fn ReplaceChildren(&self, nodes: Vec) -> ErrorResult { + self.upcast::().replace_children(nodes) + } + // https://dom.spec.whatwg.org/#dom-parentnode-queryselector fn QuerySelector(&self, selectors: DOMString) -> Fallible>> { let root = self.upcast::(); diff --git a/components/script/dom/documentfragment.rs b/components/script/dom/documentfragment.rs index 1aea832646a..ac87a9a786e 100644 --- a/components/script/dom/documentfragment.rs +++ b/components/script/dom/documentfragment.rs @@ -100,6 +100,11 @@ impl DocumentFragmentMethods for DocumentFragment { self.upcast::().append(nodes) } + // https://dom.spec.whatwg.org/#dom-parentnode-replacechildren + fn ReplaceChildren(&self, nodes: Vec) -> ErrorResult { + self.upcast::().replace_children(nodes) + } + // https://dom.spec.whatwg.org/#dom-parentnode-queryselector fn QuerySelector(&self, selectors: DOMString) -> Fallible>> { self.upcast::().query_selector(selectors) diff --git a/components/script/dom/element.rs b/components/script/dom/element.rs index 5518ddd3837..6bf7064f048 100644 --- a/components/script/dom/element.rs +++ b/components/script/dom/element.rs @@ -2637,6 +2637,11 @@ impl ElementMethods for Element { self.upcast::().append(nodes) } + // https://dom.spec.whatwg.org/#dom-parentnode-replacechildren + fn ReplaceChildren(&self, nodes: Vec) -> ErrorResult { + self.upcast::().replace_children(nodes) + } + // https://dom.spec.whatwg.org/#dom-parentnode-queryselector fn QuerySelector(&self, selectors: DOMString) -> Fallible>> { let root = self.upcast::(); diff --git a/components/script/dom/node.rs b/components/script/dom/node.rs index 8883b5b33d7..e5ebc92e651 100644 --- a/components/script/dom/node.rs +++ b/components/script/dom/node.rs @@ -934,6 +934,18 @@ impl Node { self.AppendChild(&node).map(|_| ()) } + // https://dom.spec.whatwg.org/#dom-parentnode-replacechildren + pub fn replace_children(&self, nodes: Vec) -> ErrorResult { + // Step 1. + let doc = self.owner_doc(); + let node = doc.node_from_nodes_and_strings(nodes)?; + // Step 2. + Node::ensure_pre_insertion_validity(&node, self, None)?; + // Step 3. + Node::replace_all(Some(&node), self); + Ok(()) + } + // https://dom.spec.whatwg.org/#dom-parentnode-queryselector pub fn query_selector(&self, selectors: DOMString) -> Fallible>> { // Step 1. diff --git a/components/script/dom/webidls/ParentNode.webidl b/components/script/dom/webidls/ParentNode.webidl index ef39ed8a414..a7bcc64cd23 100644 --- a/components/script/dom/webidls/ParentNode.webidl +++ b/components/script/dom/webidls/ParentNode.webidl @@ -20,6 +20,8 @@ interface mixin ParentNode { void prepend((Node or DOMString)... nodes); [CEReactions, Throws, Unscopable] void append((Node or DOMString)... nodes); + [CEReactions, Throws, Unscopable] + void replaceChildren((Node or DOMString)... nodes); [Pure, Throws] Element? querySelector(DOMString selectors); diff --git a/tests/wpt/metadata/dom/idlharness.window.js.ini b/tests/wpt/metadata/dom/idlharness.window.js.ini index 6e282adaef0..6e73346c87f 100644 --- a/tests/wpt/metadata/dom/idlharness.window.js.ini +++ b/tests/wpt/metadata/dom/idlharness.window.js.ini @@ -716,18 +716,6 @@ [Element interface: operation before((Node or DOMString)...)] expected: FAIL - [Element interface: element must inherit property "replaceChildren((Node or DOMString)...)" with the proper type] - expected: FAIL - - [Document interface: calling replaceChildren((Node or DOMString)...) on xmlDoc with too few arguments must throw TypeError] - expected: FAIL - - [Document interface: calling replaceChildren((Node or DOMString)...) on new Document() with too few arguments must throw TypeError] - expected: FAIL - - [DocumentFragment interface: document.createDocumentFragment() must inherit property "replaceChildren((Node or DOMString)...)" with the proper type] - expected: FAIL - [DocumentFragment interface: operation replaceChildren((Node or DOMString)...)] expected: FAIL @@ -737,18 +725,6 @@ [Document interface: operation replaceChildren((Node or DOMString)...)] expected: FAIL - [Element interface: calling replaceChildren((Node or DOMString)...) on element with too few arguments must throw TypeError] - expected: FAIL - - [Document interface: new Document() must inherit property "replaceChildren((Node or DOMString)...)" with the proper type] - expected: FAIL - - [DocumentFragment interface: calling replaceChildren((Node or DOMString)...) on document.createDocumentFragment() with too few arguments must throw TypeError] - expected: FAIL - - [Document interface: xmlDoc must inherit property "replaceChildren((Node or DOMString)...)" with the proper type] - expected: FAIL - [XPathNSResolver interface: document.createNSResolver(document.body) must inherit property "lookupNamespaceURI(DOMString?)" with the proper type] expected: FAIL diff --git a/tests/wpt/metadata/dom/nodes/ParentNode-replaceChildren.html.ini b/tests/wpt/metadata/dom/nodes/ParentNode-replaceChildren.html.ini deleted file mode 100644 index ae9d14fd80b..00000000000 --- a/tests/wpt/metadata/dom/nodes/ParentNode-replaceChildren.html.ini +++ /dev/null @@ -1,76 +0,0 @@ -[ParentNode-replaceChildren.html] - [DocumentFragment.replaceChildren() with null as an argument, on a parent having a child.] - expected: FAIL - - [DocumentFragment.replaceChildren() with only text as an argument, on a parent having no child.] - expected: FAIL - - [Element.replaceChildren() with null as an argument, on a parent having no child.] - expected: FAIL - - [Element.replaceChildren() without any argument, on a parent having no child.] - expected: FAIL - - [If node is a host-including inclusive ancestor of parent, then throw a HierarchyRequestError DOMException.] - expected: FAIL - - [If node is an Element and parent is a document with another element, then throw a HierarchyRequestError DOMException.] - expected: FAIL - - [DocumentFragment.replaceChildren() with null as an argument, on a parent having no child.] - expected: FAIL - - [DocumentFragment.replaceChildren() with one element and text as argument, on a parent having a child.] - expected: FAIL - - [Element.replaceChildren() with null as an argument, on a parent having a child.] - expected: FAIL - - [DocumentFragment.replaceChildren() with only one element as an argument, on a parent having no child.] - expected: FAIL - - [Element.replaceChildren() should move nodes in the right order] - expected: FAIL - - [If node is a Text node and parent is a document, then throw a HierarchyRequestError DOMException.] - expected: FAIL - - [If node is a DocumentFragment with an element and parent is a document with another element, then throw a HierarchyRequestError DOMException.] - expected: FAIL - - [DocumentFragment.replaceChildren() with undefined as an argument, on a parent having no child.] - expected: FAIL - - [If node is a doctype and parent is not a document, then throw a HierarchyRequestError DOMException.] - expected: FAIL - - [If node is a doctype and parent is a document with another doctype, then throw a HierarchyRequestError DOMException.] - expected: FAIL - - [If node is a DocumentFragment with multiple elements and parent is a document, then throw a HierarchyRequestError DOMException.] - expected: FAIL - - [Element.replaceChildren() with undefined as an argument, on a parent having no child.] - expected: FAIL - - [Element.replaceChildren() with only one element as an argument, on a parent having no child.] - expected: FAIL - - [Element.replaceChildren() with one element and text as argument, on a parent having a child.] - expected: FAIL - - [DocumentFragment.replaceChildren() should move nodes in the right order] - expected: FAIL - - [If node is a doctype and parent is a document with an element, then throw a HierarchyRequestError DOMException.] - expected: FAIL - - [Element.replaceChildren() with only text as an argument, on a parent having no child.] - expected: FAIL - - [DocumentFragment.replaceChildren() without any argument, on a parent having no child.] - expected: FAIL - - [If node is not a DocumentFragment, DocumentType, Element, Text, ProcessingInstruction, or Comment node, then throw a HierarchyRequestError DOMException.] - expected: FAIL -