-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add tests for ParentNode#replaceChildren #22524
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
816b0be
Add tests for ParentNode#replaceChildren
saschanaz 3d97d65
more thorough hierarchy validation
saschanaz 77cc044
remove trailing whitespaces
saschanaz ec0a63d
Reformat
saschanaz 0143c3d
Add mutation test
saschanaz 7ba3845
remove dummy implementation
saschanaz 14d2542
add a comment about insert()
saschanaz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
<!DOCTYPE html> | ||
<meta charset=utf-8> | ||
<title>ParentNode.replaceChildren</title> | ||
<link rel=help href="https://dom.spec.whatwg.org/#dom-parentnode-replacechildren"> | ||
<script src="/resources/testharness.js"></script> | ||
<script src="/resources/testharnessreport.js"></script> | ||
<script src="pre-insertion-validation-hierarchy.js"></script> | ||
<script> | ||
Node.prototype.replaceChildren = function (...args) { | ||
const nodes = args.map(arg => { | ||
if (typeof arg === "string") { | ||
return new Text(arg); | ||
} | ||
return arg; | ||
}); | ||
const node = nodes.length === 1 ? nodes[0] : (() => { | ||
const fragment = this.ownerDocument.createDocumentFragment(); | ||
fragment.append(...nodes); | ||
return fragment; | ||
})(); | ||
|
||
this.textContent = ""; // no way to do these two atomically | ||
this.append(node); | ||
} | ||
|
||
preInsertionValidateHierarchy("replaceChildren"); | ||
|
||
function test_replacechildren(node, nodeName) { | ||
test(() => { | ||
const parent = node.cloneNode(); | ||
parent.replaceChildren(); | ||
assert_array_equals(parent.childNodes, []); | ||
}, `${nodeName}.replaceChildren() without any argument, on a parent having no child.`); | ||
|
||
test(() => { | ||
const parent = node.cloneNode(); | ||
parent.replaceChildren(null); | ||
assert_equals(parent.childNodes[0].textContent, 'null'); | ||
}, `${nodeName}.replaceChildren() with null as an argument, on a parent having no child.`); | ||
|
||
test(() => { | ||
const parent = node.cloneNode(); | ||
parent.replaceChildren(undefined); | ||
assert_equals(parent.childNodes[0].textContent, 'undefined'); | ||
}, `${nodeName}.replaceChildren() with undefined as an argument, on a parent having no child.`); | ||
|
||
test(() => { | ||
const parent = node.cloneNode(); | ||
parent.replaceChildren('text'); | ||
assert_equals(parent.childNodes[0].textContent, 'text'); | ||
}, `${nodeName}.replaceChildren() with only text as an argument, on a parent having no child.`); | ||
|
||
test(() => { | ||
const parent = node.cloneNode(); | ||
const x = document.createElement('x'); | ||
parent.replaceChildren(x); | ||
assert_array_equals(parent.childNodes, [x]); | ||
}, `${nodeName}.replaceChildren() with only one element as an argument, on a parent having no child.`); | ||
|
||
test(() => { | ||
const parent = node.cloneNode(); | ||
const child = document.createElement('test'); | ||
parent.appendChild(child); | ||
parent.replaceChildren(null); | ||
assert_equals(parent.childNodes.length, 1); | ||
assert_equals(parent.childNodes[0].textContent, 'null'); | ||
}, `${nodeName}.replaceChildren() with null as an argument, on a parent having a child.`); | ||
|
||
test(() => { | ||
const parent = node.cloneNode(); | ||
const x = document.createElement('x'); | ||
const child = document.createElement('test'); | ||
parent.appendChild(child); | ||
parent.replaceChildren(x, 'text'); | ||
assert_equals(parent.childNodes.length, 2); | ||
assert_equals(parent.childNodes[0], x); | ||
assert_equals(parent.childNodes[1].textContent, 'text'); | ||
}, `${nodeName}.replaceChildren() with one element and text as argument, on a parent having a child.`); | ||
|
||
async_test(t => { | ||
let phase = 0; | ||
|
||
const previousParent = node.cloneNode(); | ||
const insertions = [ | ||
document.createElement("test1"), | ||
document.createElement("test2") | ||
]; | ||
previousParent.append(...insertions); | ||
|
||
const parent = node.cloneNode(); | ||
const children = [ | ||
document.createElement("test3"), | ||
document.createElement("test4") | ||
]; | ||
parent.append(...children); | ||
|
||
const previousObserver = new MutationObserver(mutations => { | ||
t.step(() => { | ||
assert_equals(phase, 0); | ||
assert_equals(mutations.length, 2); | ||
for (const [i, mutation] of Object.entries(mutations)) { | ||
assert_equals(mutation.type, "childList"); | ||
assert_equals(mutation.addedNodes.length, 0); | ||
assert_equals(mutation.removedNodes.length, 1); | ||
assert_equals(mutation.removedNodes[0], insertions[i]); | ||
} | ||
phase = 1; | ||
}); | ||
}); | ||
previousObserver.observe(previousParent, { childList: true }); | ||
|
||
const observer = new MutationObserver(mutations => { | ||
t.step(() => { | ||
assert_equals(phase, 1); | ||
assert_equals(mutations.length, 1); | ||
const mutation = mutations[0]; | ||
assert_equals(mutation.type, "childList"); | ||
assert_equals(mutation.addedNodes.length, 2); | ||
assert_array_equals([...mutation.addedNodes], insertions); | ||
assert_equals(mutation.removedNodes.length, 2); | ||
assert_array_equals([...mutation.removedNodes], children); | ||
}); | ||
t.done(); | ||
}); | ||
observer.observe(parent, { childList: true }); | ||
|
||
parent.replaceChildren(...previousParent.children); | ||
}, `${nodeName}.replaceChildren() should move nodes in the right order`); | ||
} | ||
|
||
test_replacechildren(document.createElement('div'), 'Element'); | ||
test_replacechildren(document.createDocumentFragment(), 'DocumentFragment'); | ||
</script> | ||
|
||
</html> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suspect you meant to remove this, right?