Skip to content

Commit 3cb4059

Browse files
committed
Bug 1626015 - Implement ParentNode#ReplaceChildren. r=emilio
Differential Revision: https://phabricator.services.mozilla.com/D75891
1 parent dfcd4af commit 3cb4059

File tree

6 files changed

+40
-117
lines changed

6 files changed

+40
-117
lines changed

Diff for: dom/base/nsINode.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -2025,6 +2025,35 @@ void nsINode::Append(const Sequence<OwningNodeOrString>& aNodes,
20252025
AppendChild(*node, aRv);
20262026
}
20272027

2028+
// https://dom.spec.whatwg.org/#dom-parentnode-replacechildren
2029+
void nsINode::ReplaceChildren(const Sequence<OwningNodeOrString>& aNodes,
2030+
ErrorResult& aRv) {
2031+
nsCOMPtr<Document> doc = OwnerDoc();
2032+
nsCOMPtr<nsINode> node = ConvertNodesOrStringsIntoNode(aNodes, doc, aRv);
2033+
if (aRv.Failed()) {
2034+
return;
2035+
}
2036+
2037+
EnsurePreInsertionValidity(*node, nullptr, aRv);
2038+
if (aRv.Failed()) {
2039+
return;
2040+
}
2041+
2042+
// Needed when used in combination with contenteditable (maybe)
2043+
mozAutoDocUpdate updateBatch(doc, true);
2044+
2045+
nsAutoMutationBatch mb(this, true, false);
2046+
2047+
// Replace all with node within this.
2048+
while (mFirstChild) {
2049+
RemoveChildNode(mFirstChild, true);
2050+
}
2051+
mb.RemovalDone();
2052+
2053+
AppendChild(*node, aRv);
2054+
mb.NodesAdded();
2055+
}
2056+
20282057
void nsINode::RemoveChildNode(nsIContent* aKid, bool aNotify) {
20292058
// NOTE: This function must not trigger any calls to
20302059
// Document::GetRootElement() calls until *after* it has removed aKid from

Diff for: dom/base/nsINode.h

+2
Original file line numberDiff line numberDiff line change
@@ -2033,6 +2033,8 @@ class nsINode : public mozilla::dom::EventTarget {
20332033
ErrorResult& aRv);
20342034
MOZ_CAN_RUN_SCRIPT void Append(const Sequence<OwningNodeOrString>& aNodes,
20352035
ErrorResult& aRv);
2036+
MOZ_CAN_RUN_SCRIPT void ReplaceChildren(
2037+
const Sequence<OwningNodeOrString>& aNodes, ErrorResult& aRv);
20362038

20372039
void GetBoxQuads(const BoxQuadOptions& aOptions,
20382040
nsTArray<RefPtr<DOMQuad>>& aResult, CallerType aCallerType,

Diff for: dom/webidl/ParentNode.webidl

+2
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,6 @@ interface mixin ParentNode {
2828
void prepend((Node or DOMString)... nodes);
2929
[CEReactions, Throws, Unscopable]
3030
void append((Node or DOMString)... nodes);
31+
[CEReactions, Throws, Unscopable]
32+
void replaceChildren((Node or DOMString)... nodes);
3133
};

Diff for: testing/web-platform/meta/dom/idlharness.window.js.ini

-34
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,3 @@
66
77
[ShadowRoot interface: attribute onslotchange]
88
expected: FAIL
9-
10-
[Element interface: element must inherit property "replaceChildren((Node or DOMString)...)" with the proper type]
11-
expected: FAIL
12-
13-
[Document interface: calling replaceChildren((Node or DOMString)...) on xmlDoc with too few arguments must throw TypeError]
14-
expected: FAIL
15-
16-
[Document interface: calling replaceChildren((Node or DOMString)...) on new Document() with too few arguments must throw TypeError]
17-
expected: FAIL
18-
19-
[DocumentFragment interface: document.createDocumentFragment() must inherit property "replaceChildren((Node or DOMString)...)" with the proper type]
20-
expected: FAIL
21-
22-
[DocumentFragment interface: operation replaceChildren((Node or DOMString)...)]
23-
expected: FAIL
24-
25-
[Element interface: operation replaceChildren((Node or DOMString)...)]
26-
expected: FAIL
27-
28-
[Document interface: operation replaceChildren((Node or DOMString)...)]
29-
expected: FAIL
30-
31-
[Element interface: calling replaceChildren((Node or DOMString)...) on element with too few arguments must throw TypeError]
32-
expected: FAIL
33-
34-
[Document interface: new Document() must inherit property "replaceChildren((Node or DOMString)...)" with the proper type]
35-
expected: FAIL
36-
37-
[DocumentFragment interface: calling replaceChildren((Node or DOMString)...) on document.createDocumentFragment() with too few arguments must throw TypeError]
38-
expected: FAIL
39-
40-
[Document interface: xmlDoc must inherit property "replaceChildren((Node or DOMString)...)" with the proper type]
41-
expected: FAIL
42-

Diff for: testing/web-platform/meta/dom/nodes/ParentNode-replaceChildren.html.ini

-76
This file was deleted.

Diff for: testing/web-platform/tests/dom/nodes/ParentNode-replaceChildren.html

+7-7
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,14 @@
9494

9595
const observer = new MutationObserver(mutations => {
9696
t.step(() => {
97-
assert_equals(phase, 1);
98-
assert_equals(mutations.length, 1);
97+
assert_equals(phase, 1, "phase");
98+
assert_equals(mutations.length, 1, "mutations.length");
9999
const mutation = mutations[0];
100-
assert_equals(mutation.type, "childList");
101-
assert_equals(mutation.addedNodes.length, 2);
102-
assert_array_equals([...mutation.addedNodes], insertions);
103-
assert_equals(mutation.removedNodes.length, 2);
104-
assert_array_equals([...mutation.removedNodes], children);
100+
assert_equals(mutation.type, "childList", "mutation.type");
101+
assert_equals(mutation.addedNodes.length, 2, "added nodes length");
102+
assert_array_equals([...mutation.addedNodes], insertions, "added nodes");
103+
assert_equals(mutation.removedNodes.length, 2, "removed nodes length");
104+
assert_array_equals([...mutation.removedNodes], children, "removed nodes");
105105
});
106106
t.done();
107107
});

0 commit comments

Comments
 (0)