summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/dom/elements/the-innertext-and-outertext-properties/outertext-setter.html
blob: 953bedc9a8f0c07b4225e5fc688efdb9551b9baa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<!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>
  <li><span id="testNewlines">Replace this child with lots of newlines</span></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(t => {
  const container = document.getElementById("container");
  t.add_cleanup(() => { container.textContent = ""; });

  container.append(document.createElement("span"));
  const node = container.childNodes[0];
  node.outerText = "";

  assert_equals(container.childNodes.length, 1, "Creates text node for the empty string");
  assert_equals(container.childNodes[0].data, "");
}, "Empty string");

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

  container.append("1", "2", document.createElement("span"), "3", "4");
  const node = container.childNodes[2];
  node.outerText = "";

  assert_equals(container.childNodes.length, 3, "It got merged with the previous and following text node");
  assert_equals(container.childNodes[0].data, "1");
  assert_equals(container.childNodes[1].data, "23");
  assert_equals(container.childNodes[2].data, "4");
}, "Empty string with surrounding text nodes");

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

  node.outerText = "\n\r\n\r";

  assert_equals(parent.innerHTML, "<br><br><br>");
  assert_equals(parent.childNodes.length, 3);
  assert_equals(parent.childNodes[0].localName, "br", "node 1");
  assert_equals(parent.childNodes[1].localName, "br", "node 2");
  assert_equals(parent.childNodes[2].localName, "br", "node 3");
}, "Setting outerText to a bunch of newlines creates a bunch of <br>s with no text nodes");

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>