-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a test for Element.textContent + MutationObserver
Differential Revision: https://phabricator.services.mozilla.com/D234895 bugzilla-url: https://bugzilla.mozilla.org/show_bug.cgi?id=1942666 gecko-commit: 06a675630c5508404a594f3009e5a5c790099c42 gecko-reviewers: dom-core, masayuki
- Loading branch information
1 parent
7e21654
commit 88234ea
Showing
1 changed file
with
76 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<!doctype html> | ||
<meta charset=utf-8> | ||
<title></title> | ||
<script src=/resources/testharness.js></script> | ||
<script src=/resources/testharnessreport.js></script> | ||
<script> | ||
|
||
async function test() { | ||
|
||
let testElement1 = async_test("Setting Element.textContent"); | ||
let el = document.createElement("div"); | ||
let m = new MutationObserver((records) => testElement1.step(()=> { | ||
assert_equals(records.length, 1, "Should have one record"); | ||
assert_equals(records[0].addedNodes[0].nodeType, Node.TEXT_NODE, "Should have gotten a text node as a child."); | ||
assert_equals(records[0].addedNodes[0].data, "foo"); | ||
m.disconnect(); | ||
testElement1.done(); | ||
})); | ||
m.observe(el, { childList: true }); | ||
el.textContent = "foo"; | ||
|
||
await Promise.resolve(); // Run microtasks | ||
|
||
let testElement2 = async_test("Setting Element.textContent to the same value"); | ||
m = new MutationObserver((records) => testElement2.step(()=> { | ||
assert_equals(records.length, 1, "Should have one record"); | ||
assert_equals(records[0].removedNodes[0].nodeType, Node.TEXT_NODE, "Should have removed a text node."); | ||
assert_equals(records[0].removedNodes[0].data, "foo"); | ||
assert_equals(records[0].addedNodes[0].nodeType, Node.TEXT_NODE, "Should have gotten a text node as a child."); | ||
assert_equals(records[0].addedNodes[0].data, "foo"); | ||
m.disconnect(); | ||
testElement2.done(); | ||
})); | ||
m.observe(el, { childList: true }); | ||
testElement2.step(() => { assert_equals(el.textContent, "foo"); }); | ||
el.textContent = "foo"; | ||
|
||
await Promise.resolve(); // Run microtasks | ||
|
||
let testElement3 = async_test("Setting Element.textContent to a different value"); | ||
m = new MutationObserver((records) => testElement3.step(()=> { | ||
assert_equals(records.length, 1, "Should have one record"); | ||
assert_equals(records[0].removedNodes[0].nodeType, Node.TEXT_NODE, "Should have removed a text node."); | ||
assert_equals(records[0].removedNodes[0].data, "foo"); | ||
assert_equals(records[0].addedNodes[0].nodeType, Node.TEXT_NODE, "Should have gotten a text node as a child."); | ||
assert_equals(records[0].addedNodes[0].data, "bar"); | ||
m.disconnect(); | ||
testElement3.done(); | ||
})); | ||
m.observe(el, { childList: true }); | ||
testElement3.step(() => { assert_equals(el.textContent, "foo"); }); | ||
el.textContent = "bar"; | ||
|
||
await Promise.resolve(); // Run microtasks | ||
|
||
let testElement4 = async_test("Setting Element.textContent to the same value when the old node is a CDATASection"); | ||
let xml = new DOMParser().parseFromString("<root></root>", "text/xml"); | ||
el = xml.createElement("somelement"); | ||
el.appendChild(xml.createCDATASection("foo")); | ||
m = new MutationObserver((records) => testElement4.step(()=> { | ||
assert_equals(records.length, 1, "Should have one record"); | ||
assert_equals(records[0].removedNodes[0].nodeType, Node.CDATA_SECTION_NODE, "Should have removed a cdata node."); | ||
assert_equals(records[0].removedNodes[0].data, "foo"); | ||
assert_equals(records[0].addedNodes[0].nodeType, Node.TEXT_NODE, "Should have gotten a text node as a child."); | ||
assert_equals(records[0].addedNodes[0].data, "foo"); | ||
m.disconnect(); | ||
testElement4.done(); | ||
})); | ||
m.observe(el, { childList: true }); | ||
testElement4.step(() => { assert_equals(el.textContent, "foo"); }); | ||
el.textContent = "foo"; | ||
|
||
} | ||
test(); | ||
|
||
</script> |