Skip to content

Commit

Permalink
Upstream and expand outerText tests
Browse files Browse the repository at this point in the history
This accompanies whatwg/html#6653.

Fixed: 710764
Change-Id: I7168df9bbaeef12ed256b7f2fb9c2c43c9d6b227
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2875725
Reviewed-by: Mason Freed <[email protected]>
Commit-Queue: Domenic Denicola <[email protected]>
Cr-Commit-Position: refs/heads/master@{#880460}
  • Loading branch information
domenic authored and chromium-wpt-export-bot committed May 7, 2021
1 parent e953dff commit b1c25ca
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 14 deletions.
9 changes: 3 additions & 6 deletions custom-elements/reactions/HTMLElement.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,9 @@
customElement.parentNode.innerText = '';
}, 'innerText on HTMLElement');

if ('outerText' in HTMLElement.prototype) {
// Not yet to be in the standard but all but Gecko supports this property: https://github.com/whatwg/html/issues/668
testNodeDisconnector(function (customElement) {
customElement.outerText = '';
}, 'outerText on HTMLElement');
}
testNodeDisconnector(function (customElement) {
customElement.outerText = '';
}, 'outerText on HTMLElement');

</script>
</body>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!DOCTYPE html>
<title>innerText getter test with dynamic style changes</title>
<title>innerText/outerText getter test with dynamic style changes</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id="container"></div>
Expand All @@ -19,7 +19,8 @@
if (!e) {
e = container.firstChild;
}
assert_equals(e.innerText, expectedPlain);
assert_equals(e.innerText, expectedPlain, "innerText");
assert_equals(e.outerText, expectedPlain, "outerText");
container.textContext = '';
}, msg + ' (' + format_value(html) + ')');
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!DOCTYPE html>
<title>Test innerText for a combination of a list item with ::first-letter in multicol</title>
<title>Test innerText/outerText for a combination of a list item with ::first-letter in multicol</title>
<link rel="help" href="https://html.spec.whatwg.org/multipage/dom.html#dom-innertext">
<link rel="help" href="https://crbug.com/1174985">
<script src="/resources/testharness.js"></script>
Expand All @@ -12,6 +12,7 @@
<div id="item" class="col"><div class="col">PASS</div></div>
<script>
test(() => {
assert_equals(item.innerText, "PASS");
assert_equals(item.innerText, "PASS", "innerText");
assert_equals(item.outerText, "PASS", "outerText");
}, "");
</script>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!DOCTYPE html>
<title>innerText getter test</title>
<title>innerText/outerText getter test</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<style>
Expand Down Expand Up @@ -55,7 +55,8 @@
while (e && e.nodeType != Node.ELEMENT_NODE) {
e = e.nextSibling;
}
assert_equals(e.innerText, expectedPlain);
assert_equals(e.innerText, expectedPlain, "innerText");
assert_equals(e.outerText, expectedPlain, "outerText");
cont.textContent = '';
}, msg + ' (' + format_value(html) + ')');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,4 @@
}, msg + ", detached");
}
</script>
<script src="setter-tests.js"></script>
<script src="innertext-setter-tests.js"></script>
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ async_test(t => {
t1.data = "X";
t2.data = " ";
t3.data = "Y";
assert_equals(div.innerText, "X Y");
assert_equals(div.innerText, "X Y", "innerText");
assert_equals(div.outerText, "X Y", "outerText");
t.done();
}, 100);
}, "Ensure multiple text nodes get rendered properly");
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<!DOCTYPE html>
<title>outerText setter test</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>

<ul>
<li>A <span id="testReplacePrevious">B</span></li>
<li><span id="testReplaceFollowing">A</span> B</li>
<li>A <span id="testReplaceBoth">B</span> C</li>
<li><span id="testRemove">Testing</span> removing node using outerText.</li>
</ul>

<div id="container"></div>

<script>
"use strict";

test(() => {
const node = document.getElementById("testReplacePrevious");
const parent = node.parentNode;

node.outerText = "Replaced";

assert_equals(parent.innerHTML, "A Replaced");
assert_equals(parent.childNodes.length, 1, "It got merged with the previous text node");
}, "Replacing a node and merging with the previous text node");

test(() => {
const node = document.getElementById("testReplaceFollowing");
const parent = node.parentNode;

node.outerText = "Replaced";

assert_equals(parent.innerHTML, "Replaced B");
assert_equals(parent.childNodes.length, 1, "It got merged with the following text node");
}, "Replacing a node and merging with the following text node");

test(() => {
const node = document.getElementById("testReplaceBoth");
const parent = node.parentNode;

node.outerText = "Replaced";

assert_equals(parent.innerHTML, "A Replaced C");
assert_equals(parent.childNodes.length, 1, "It got merged with the previous and following text node");
}, "Replacing a node and merging with the previous and following text node");

test(t => {
const container = document.getElementById("container");
t.add_cleanup(() => { container.textContent = ""; });

container.append("A", "B", document.createElement("span"), "D", "E");
assert_equals(container.childNodes.length, 5, "Precondition check: five separate nodes");

const node = container.childNodes[2];
node.outerText = "Replaced";

assert_equals(container.innerHTML, "ABReplacedDE");
assert_equals(container.childNodes.length, 3, "It got merged with the previous and following text node");
assert_equals(container.childNodes[0].data, "A");
assert_equals(container.childNodes[1].data, "BReplacedD");
assert_equals(container.childNodes[2].data, "E");
}, "Only merges with the previous and following text nodes, does not completely normalize");

test(() => {
const node = document.getElementById("testRemove");
const parent = node.parentNode;

node.outerText = "";

assert_equals(parent.innerHTML, " removing node using outerText.");
}, "Removing a node");

test(() => {
const node = document.createElement("span");

assert_throws_dom("NoModificationAllowedError", () => { node.outerText = ""; });
}, "Detached node");

testText("<div>", "abc", "abc", "Simplest possible test");
testHTML("<div>", "abc\ndef", "abc<br>def", "Newlines convert to <br> in non-white-space:pre elements");
testHTML("<pre>", "abc\ndef", "abc<br>def", "Newlines convert to <br> in <pre> element");
testHTML("<textarea>", "abc\ndef", "abc<br>def", "Newlines convert to <br> in <textarea> element");
testHTML("<div style='white-space:pre'>", "abc\ndef", "abc<br>def", "Newlines convert to <br> in white-space:pre element");
testHTML("<div>", "abc\rdef", "abc<br>def", "CRs convert to <br> in non-white-space:pre elements");
testHTML("<pre>", "abc\rdef", "abc<br>def", "CRs convert to <br> in <pre> element");
testHTML("<div>", "abc\r\ndef", "abc<br>def", "Newline/CR pair converts to <br> in non-white-space:pre element");
testHTML("<div>", "abc\n\ndef", "abc<br><br>def", "Newline/newline pair converts to two <br>s in non-white-space:pre element");
testHTML("<div>", "abc\r\rdef", "abc<br><br>def", "CR/CR pair converts to two <br>s in non-white-space:pre element");
testHTML("<div style='white-space:pre'>", "abc\rdef", "abc<br>def", "CRs convert to <br> in white-space:pre element");
testText("<div>", "abc<def", "abc<def", "< preserved");
testText("<div>", "abc>def", "abc>def", "> preserved");
testText("<div>", "abc&", "abc&", "& preserved");
testText("<div>", "abc\"def", "abc\"def", "\" preserved");
testText("<div>", "abc\'def", "abc\'def", "\' preserved");
testHTML("<svg>", "abc", "<svg></svg>", "outerText not supported on SVG elements");
testHTML("<math>", "abc", "<math></math>", "outerText not supported on MathML elements");
testText("<div>", "abc\0def", "abc\0def", "Null characters preserved");
testText("<div>", "abc\tdef", "abc\tdef", "Tabs preserved");
testText("<div>", " abc", " abc", "Leading whitespace preserved");
testText("<div>", "abc ", "abc ", "Trailing whitespace preserved");
testText("<div>", "abc def", "abc def", "Whitespace not compressed");
testText("<div>abc\n\n", "abc", "abc", "Existing text deleted");
testText("<div><br>", "abc", "abc", "Existing <br> deleted");
testHTML("<div>", "", "", "Assigning the empty string");
testHTML("<div>", null, "", "Assigning null");
testHTML("<div>", undefined, "undefined", "Assigning undefined");
testHTML("<div>", "\rabc", "<br>abc", "Start with CR");
testHTML("<div>", "\nabc", "<br>abc", "Start with LF");
testHTML("<div>", "\r\nabc", "<br>abc", "Start with CRLF");
testHTML("<div>", "abc\r", "abc<br>", "End with CR");
testHTML("<div>", "abc\n", "abc<br>", "End with LF");
testHTML("<div>", "abc\r\n", "abc<br>", "End with CRLF");

function testText(startingHTML, outerText, expected, description) {
test(t => {
const container = document.getElementById("container");
t.add_cleanup(() => { container.textContent = ""; });

container.innerHTML = startingHTML;
const elementToReplace = container.firstElementChild;

elementToReplace.outerText = outerText;
assert_equals(container.textContent, expected);
}, description);
}

function testHTML(startingHTML, outerText, expected, description) {
test(t => {
const container = document.getElementById("container");
t.add_cleanup(() => { container.textContent = ""; });

container.innerHTML = startingHTML;
const elementToReplace = container.firstElementChild;

elementToReplace.outerText = outerText;
assert_equals(container.innerHTML, expected);
}, description);
}
</script>

0 comments on commit b1c25ca

Please sign in to comment.