-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
Copy pathblock-string-assignment-to-Element-outerHTML.html
96 lines (83 loc) · 3.03 KB
/
block-string-assignment-to-Element-outerHTML.html
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
<!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')
// TrustedHTML assignments do not throw.
test(t => {
let p = createHTML_policy(window, 1);
let html = p.createHTML(INPUTS.HTML);
var d = document.createElement('div');
document.querySelector('#container').appendChild(d);
d.outerHTML = html;
assert_equals(container.innerText, RESULTS.HTML);
while (container.firstChild)
container.firstChild.remove();
}, "outerHTML with html assigned via policy (successful HTML transformation).");
test(t => {
let p = createHTML_policy(window, 1);
assert_throws_dom("NoModificationAllowedError", _ => {
document.documentElement.outerHTML = p.createHTML('<p>Content</p>');
});
}, "`outerHTML = TrustedHTML` throws NoModificationAllowedError when parent is a document.");
// String assignments throw.
test(t => {
var d = document.createElement('div');
container.appendChild(d);
assert_throws_js(TypeError, _ => {
d.outerHTML = "Fail.";
});
assert_equals(container.innerText, "");
while (container.firstChild)
container.firstChild.remove();
}, "`outerHTML = string` throws.");
test(t => {
assert_throws_js(TypeError, _ => {
document.documentElement.outerHTML = '<p>Content</p>';
});
}, "`outerHTML = string` throws TypeError even when parent is a document.");
// Null assignment throws.
test(t => {
var d = document.createElement('div');
container.appendChild(d);
assert_throws_js(TypeError, _ => {
d.outerHTML = null;
});
assert_equals(container.innerText, "");
while (container.firstChild)
container.firstChild.remove();
}, "`outerHTML = null` throws.");
// After default policy creation string assignment implicitly calls createHTML.
test(t => {
let p = window.trustedTypes.createPolicy("default", {
createHTML: (value, type, sink) => {
assert_equals(sink, "Element outerHTML");
return createHTMLJS(value);
}
});
var d = document.createElement('div');
document.querySelector('#container').appendChild(d);
d.outerHTML = INPUTS.HTML;
assert_equals(container.innerText, RESULTS.HTML);
while (container.firstChild)
container.firstChild.remove();
}, "`outerHTML = 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.outerHTML = null;
assert_equals(container.innerText, "");
while (container.firstChild)
container.firstChild.remove();
}, "`outerHTML = null` assigned via default policy does not throw");
</script>
</body>
</html>