Skip to content

Commit

Permalink
Add a test for Element.textContent + MutationObserver
Browse files Browse the repository at this point in the history
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
Olli Pettay authored and moz-wptsync-bot committed Jan 21, 2025
1 parent 7e21654 commit 88234ea
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions dom/nodes/MutationObserver-textContent.html
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>

0 comments on commit 88234ea

Please sign in to comment.