-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Expand file tree
/
Copy pathblock-string-assignment-to-Element-insertAdjacentHTML.html
More file actions
207 lines (166 loc) · 6.6 KB
/
Copy pathblock-string-assignment-to-Element-insertAdjacentHTML.html
File metadata and controls
207 lines (166 loc) · 6.6 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<!DOCTYPE html>
<html>
<head>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="support/helper.sub.js"></script>
<meta http-equiv="Content-Security-Policy" content="require-trusted-types-for 'script';">
</head>
<body>
<div id="container"></div>
<script>
var container = document.querySelector('#container');
// Trusted HTML assignments do not throw.
test(t => {
let p = createHTML_policy(window, 1);
let html = p.createHTML(INPUTS.HTML);
let before = 'before';
let after = 'after';
let htmlBefore = p.createHTML(before);
let htmlAfter = p.createHTML(after);
var d = document.createElement('div');
container.appendChild(d);
d.insertAdjacentHTML('beforebegin', html);
assert_equals(d.previousSibling.nodeType, Node.TEXT_NODE);
assert_equals(d.previousSibling.data, RESULTS.HTML);
d.insertAdjacentHTML('afterbegin', htmlBefore);
d.insertAdjacentHTML('beforeend', htmlAfter);
assert_equals(d.innerHTML, before + after);
d.insertAdjacentHTML('afterend', html);
assert_equals(d.nextSibling.nodeType, Node.TEXT_NODE);
assert_equals(d.nextSibling.data, RESULTS.HTML);
while (container.firstChild)
container.firstChild.remove();
}, "insertAdjacentHTML with html assigned via policy (successful HTML transformation).");
test(t => {
let p = createHTML_policy(window, 1);
var d = document.createElement('div');
container.appendChild(d);
assert_throws_dom("SyntaxError", _ => {
d.insertAdjacentHTML('invalid', p.createHTML("<p>Fail</p>"));
});
assert_equals(d.previousSibling, null);
assert_equals(d.firstChild, null);
assert_equals(d.lastChild, null);
assert_equals(d.nextSibling, null);
while (container.firstChild)
container.firstChild.remove();
}, "insertAdjacentHTML(TrustedHTML) throws SyntaxError DOMException when position invalid.");
// String assignments throw.
test(t => {
var d = document.createElement('div');
container.appendChild(d);
assert_throws_js(TypeError, _ => {
d.insertAdjacentHTML('beforebegin', "<p>Fail</p>");
});
assert_throws_js(TypeError, _ => {
d.insertAdjacentHTML('afterbegin', "<p>Fail</p>");
});
assert_throws_js(TypeError, _ => {
d.insertAdjacentHTML('beforeend', "<p>Fail</p>");
});
assert_throws_js(TypeError, _ => {
d.insertAdjacentHTML('afterend', "<p>Fail</p>");
});
assert_equals(d.previousSibling, null);
assert_equals(d.firstChild, null);
assert_equals(d.lastChild, null);
assert_equals(d.nextSibling, null);
while (container.firstChild)
container.firstChild.remove();
}, "`insertAdjacentHTML(string)` throws.");
test(t => {
var d = document.createElement('div');
container.appendChild(d);
assert_throws_js(TypeError, _ => {
d.insertAdjacentHTML('invalid', "<p>Fail</p>");
});
assert_equals(d.previousSibling, null);
assert_equals(d.firstChild, null);
assert_equals(d.lastChild, null);
assert_equals(d.nextSibling, null);
while (container.firstChild)
container.firstChild.remove();
}, "`insertAdjacentHTML(string)` still throws TypeError when position invalid.");
// Null assignment throws.
test(t => {
var d = document.createElement('div');
container.appendChild(d);
assert_throws_js(TypeError, _ => {
d.insertAdjacentHTML('beforebegin', null);
});
assert_throws_js(TypeError, _ => {
d.insertAdjacentHTML('afterbegin', null);
});
assert_throws_js(TypeError, _ => {
d.insertAdjacentHTML('beforeend', null);
});
assert_throws_js(TypeError, _ => {
d.insertAdjacentHTML('afterend', null);
});
assert_equals(d.previousSibling, null);
assert_equals(d.firstChild, null);
assert_equals(d.lastChild, null);
assert_equals(d.nextSibling, null);
}, "`insertAdjacentHTML(null)` throws.");
// Due to a Gecko-specific bug
// (https://bugzilla.mozilla.org/show_bug.cgi?id=1270944), adjacent nodes are
// merged. Remove the children, to let the test pass on Gecko
// too.
function removeChildrenBecauseOfGeckoBug(element) {
element.innerHTML = null;
}
// After default policy creation string assignment implicitly calls createHTML.
test(t => {
const kAssertSinkIsElementInsertAdjacentHTML =
"assertSinkIsElementInsertAdjacentHTML";
let p = window.trustedTypes.createPolicy("default", { createHTML:
(s, _, sink) => {
if (s == kAssertSinkIsElementInsertAdjacentHTML) {
assert_equals(sink, "Element insertAdjacentHTML");
}
return createHTMLJS(s);
}
});
var d = document.createElement('div');
container.appendChild(d);
d.insertAdjacentHTML('beforebegin', INPUTS.HTML);
assert_equals(d.previousSibling.nodeType, Node.TEXT_NODE);
assert_equals(d.previousSibling.data, RESULTS.HTML);
d.insertAdjacentHTML('afterbegin', INPUTS.HTML);
assert_equals(d.firstChild.nodeType, Node.TEXT_NODE);
assert_equals(d.firstChild.data, RESULTS.HTML);
removeChildrenBecauseOfGeckoBug(d);
d.insertAdjacentHTML('beforeend', INPUTS.HTML);
assert_equals(d.lastChild.nodeType, Node.TEXT_NODE);
assert_equals(d.lastChild.data, RESULTS.HTML);
d.insertAdjacentHTML('afterend', INPUTS.HTML);
assert_equals(d.nextSibling.nodeType, Node.TEXT_NODE);
assert_equals(d.nextSibling.data, RESULTS.HTML);
d.insertAdjacentHTML('afterend', kAssertSinkIsElementInsertAdjacentHTML);
while (container.firstChild)
container.firstChild.remove();
}, "`insertAdjacentHTML(string)` assigned via default policy (successful HTML transformation).");
// After default policy creation null assignment implicitly calls createHTML.
test(t => {
var d = document.createElement('div');
container.appendChild(d);
d.insertAdjacentHTML('beforebegin', null);
assert_equals(d.previousSibling.nodeType, Node.TEXT_NODE);
assert_equals(d.previousSibling.data, "null");
d.insertAdjacentHTML('afterbegin', null);
assert_equals(d.firstChild.nodeType, Node.TEXT_NODE);
assert_equals(d.firstChild.data, "null");
removeChildrenBecauseOfGeckoBug(d);
d.insertAdjacentHTML('beforeend', null);
assert_equals(d.lastChild.nodeType, Node.TEXT_NODE);
assert_equals(d.lastChild.data, "null");
d.insertAdjacentHTML('afterend', null);
assert_equals(d.nextSibling.nodeType, Node.TEXT_NODE);
assert_equals(d.nextSibling.data, "null");
while (container.firstChild)
container.firstChild.remove();
}, "`insertAdjacentHTML(null)` assigned via default policy does not throw.");
</script>
</body>
</html>