Auto merge of #27667 - ghostd:parentnode-replacechildren, r=jdm

Implements ParentNode#replaceChildren

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `___` with appropriate data: -->
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [X] These changes fix #26240

<!-- Either: -->
- [X] There are tests for these changes OR
- [ ] These changes do not require tests because ___

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->
This commit is contained in:
bors-servo 2020-09-25 11:05:31 -04:00 committed by GitHub
commit a521de16d3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 29 additions and 100 deletions

View file

@ -4753,6 +4753,11 @@ impl DocumentMethods for Document {
self.upcast::<Node>().append(nodes) self.upcast::<Node>().append(nodes)
} }
// https://dom.spec.whatwg.org/#dom-parentnode-replacechildren
fn ReplaceChildren(&self, nodes: Vec<NodeOrString>) -> ErrorResult {
self.upcast::<Node>().replace_children(nodes)
}
// https://dom.spec.whatwg.org/#dom-parentnode-queryselector // https://dom.spec.whatwg.org/#dom-parentnode-queryselector
fn QuerySelector(&self, selectors: DOMString) -> Fallible<Option<DomRoot<Element>>> { fn QuerySelector(&self, selectors: DOMString) -> Fallible<Option<DomRoot<Element>>> {
let root = self.upcast::<Node>(); let root = self.upcast::<Node>();

View file

@ -100,6 +100,11 @@ impl DocumentFragmentMethods for DocumentFragment {
self.upcast::<Node>().append(nodes) self.upcast::<Node>().append(nodes)
} }
// https://dom.spec.whatwg.org/#dom-parentnode-replacechildren
fn ReplaceChildren(&self, nodes: Vec<NodeOrString>) -> ErrorResult {
self.upcast::<Node>().replace_children(nodes)
}
// https://dom.spec.whatwg.org/#dom-parentnode-queryselector // https://dom.spec.whatwg.org/#dom-parentnode-queryselector
fn QuerySelector(&self, selectors: DOMString) -> Fallible<Option<DomRoot<Element>>> { fn QuerySelector(&self, selectors: DOMString) -> Fallible<Option<DomRoot<Element>>> {
self.upcast::<Node>().query_selector(selectors) self.upcast::<Node>().query_selector(selectors)

View file

@ -2637,6 +2637,11 @@ impl ElementMethods for Element {
self.upcast::<Node>().append(nodes) self.upcast::<Node>().append(nodes)
} }
// https://dom.spec.whatwg.org/#dom-parentnode-replacechildren
fn ReplaceChildren(&self, nodes: Vec<NodeOrString>) -> ErrorResult {
self.upcast::<Node>().replace_children(nodes)
}
// https://dom.spec.whatwg.org/#dom-parentnode-queryselector // https://dom.spec.whatwg.org/#dom-parentnode-queryselector
fn QuerySelector(&self, selectors: DOMString) -> Fallible<Option<DomRoot<Element>>> { fn QuerySelector(&self, selectors: DOMString) -> Fallible<Option<DomRoot<Element>>> {
let root = self.upcast::<Node>(); let root = self.upcast::<Node>();

View file

@ -934,6 +934,18 @@ impl Node {
self.AppendChild(&node).map(|_| ()) self.AppendChild(&node).map(|_| ())
} }
// https://dom.spec.whatwg.org/#dom-parentnode-replacechildren
pub fn replace_children(&self, nodes: Vec<NodeOrString>) -> 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 // https://dom.spec.whatwg.org/#dom-parentnode-queryselector
pub fn query_selector(&self, selectors: DOMString) -> Fallible<Option<DomRoot<Element>>> { pub fn query_selector(&self, selectors: DOMString) -> Fallible<Option<DomRoot<Element>>> {
// Step 1. // Step 1.

View file

@ -20,6 +20,8 @@ interface mixin ParentNode {
void prepend((Node or DOMString)... nodes); void prepend((Node or DOMString)... nodes);
[CEReactions, Throws, Unscopable] [CEReactions, Throws, Unscopable]
void append((Node or DOMString)... nodes); void append((Node or DOMString)... nodes);
[CEReactions, Throws, Unscopable]
void replaceChildren((Node or DOMString)... nodes);
[Pure, Throws] [Pure, Throws]
Element? querySelector(DOMString selectors); Element? querySelector(DOMString selectors);

View file

@ -716,18 +716,6 @@
[Element interface: operation before((Node or DOMString)...)] [Element interface: operation before((Node or DOMString)...)]
expected: FAIL 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)...)] [DocumentFragment interface: operation replaceChildren((Node or DOMString)...)]
expected: FAIL expected: FAIL
@ -737,18 +725,6 @@
[Document interface: operation replaceChildren((Node or DOMString)...)] [Document interface: operation replaceChildren((Node or DOMString)...)]
expected: FAIL 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] [XPathNSResolver interface: document.createNSResolver(document.body) must inherit property "lookupNamespaceURI(DOMString?)" with the proper type]
expected: FAIL expected: FAIL

View file

@ -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