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
|
<!DOCTYPE html>
<meta charset="utf-8">
<title>Declarative Shadow DOM</title>
<link rel="author" href="mailto:masonf@chromium.org">
<link rel="help" href="https://github.com/whatwg/dom/issues/831">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src='../../html/resources/common.js'></script>
<body>
<style>
* { white-space: pre; }
iframe { display:none; }
</style>
<div id=log></div>
<div id=mainpage style="display:none">
<div class=wrapper>
<div class=host>
<template shadowrootmode=open>
<span class=content>Content</span>
</template>
</div>
</div>
</div>
<script>
const content = `
<html><body>
<div class=wrapper>
<div class=host>
<template shadowrootmode=open>
<span class=content>Content</span>
</template>
</div>
</div>
</body></html>
`;
function assert_dsd(el,shouldHaveShadow) {
const wrapper = el.querySelector('.wrapper');
assert_true(!!wrapper,'Unable to find wrapper element');
const host = wrapper.querySelector('.host');
assert_true(!!host,'Unable to find host element');
if (shouldHaveShadow) {
assert_true(!!host.shadowRoot, 'Shadow root NOT FOUND.');
assert_true(!!host.shadowRoot.querySelector('.content'),'Unable to locate content');
} else {
assert_true(!host.shadowRoot, 'Shadow root FOUND - none should be present.');
const tmpl = host.querySelector('template');
assert_true(!!tmpl, 'The template should be left as a <template> element');
assert_equals(tmpl.getAttribute('shadowrootmode'),'open','The shadowrootmode attribute should still be present');
assert_true(!!tmpl.content.querySelector('.content'),'Unable to locate content');
}
}
test(() => {
const div = document.getElementById('mainpage');
assert_dsd(div,true);
}, 'Non-fragment parsing needs no opt-in');
const noChildElements = ['iframe','noscript','script','select','style','template','textarea','title','colgroup'];
const elements = HTML5_ELEMENTS.filter(el => !noChildElements.includes(el));
for (let elementName of elements) {
var t = test(function() {
const el1 = document.createElement(elementName);
el1.innerHTML = content;
assert_dsd(el1,false);
const templateContent = `<template id=tmpl>${content}</template>`;
const el2 = document.createElement('div');
el2.innerHTML = templateContent;
assert_dsd(el2.querySelector('#tmpl').content,false);
}, `innerHTML on a <${elementName}>`);
}
test(() => {
const temp = document.createElement('template');
temp.innerHTML = content;
assert_dsd(temp.content,false, 'innerHTML should not allow declarative shadow content');
}, 'innerHTML on template');
test(() => {
const templateContent = `<template id=tmpl>${content}</template>`;
const temp = document.createElement('template');
temp.innerHTML = templateContent;
assert_dsd(temp.content.querySelector('#tmpl').content,false);
}, 'innerHTML on template, with nested template content');
test(() => {
const div = document.createElement('div');
const shadow = div.attachShadow({mode: 'open'});
shadow.innerHTML = content;
assert_dsd(shadow,false);
}, 'innerHTML on shadowRoot');
test(() => {
const parser = new DOMParser();
let fragment = parser.parseFromString(content, 'text/html');
assert_dsd(fragment.body,false);
fragment = parser.parseFromString(content, 'text/html', {includeShadowRoots: false});
assert_dsd(fragment.body,false);
fragment = parser.parseFromString(content, 'text/html', {includeShadowRoots: true});
assert_dsd(fragment.body,true);
}, 'DOMParser');
test(() => {
const doc = document.implementation.createHTMLDocument('');
doc.body.innerHTML = content;
assert_dsd(doc.body,false);
}, 'createHTMLDocument with innerHTML - not supported');
test(() => {
const doc = document.implementation.createHTMLDocument('');
let range = doc.createRange();
range.selectNode(doc.body);
let documentFragment = range.createContextualFragment(content);
assert_dsd(documentFragment,false);
}, 'createContextualFragment - not supported');
async_test((t) => {
let client = new XMLHttpRequest();
client.addEventListener('load', t.step_func_done(() => {
assert_true(client.status == 200 && client.responseXML != null);
assert_dsd(client.responseXML.body,false);
t.done();
}));
client.open("GET", `data:text/html,${content}`);
client.responseType = 'document';
client.send();
}, 'XMLHttpRequest - not supported');
test(() => {
const div = document.createElement('div');
div.insertAdjacentHTML('afterbegin',content);
assert_dsd(div,false);
}, 'insertAdjacentHTML on element - not supported');
test(() => {
const id = 'doc-write-1';
document.write(`<div id=${id} style="display:none">${content}</div>`);
assert_dsd(document.getElementById(id),true);
}, 'document.write allowed from synchronous script loaded from main document');
test(() => {
const id = 'doc-write-2';
const doc = document.implementation.createHTMLDocument('');
doc.write(`<div id=${id}>${content}</div>`);
assert_dsd(doc.getElementById(id),false);
}, 'document.write disallowed on fresh document');
async_test((t) => {
const iframe = document.createElement('iframe');
iframe.style.display = "none";
iframe.sandbox = "allow-same-origin";
document.body.appendChild(iframe);
iframe.addEventListener('load', t.step_func_done(() => {
assert_dsd(iframe.contentDocument.body,true);
t.done();
}));
iframe.srcdoc = content;
}, 'iframe');
async_test((t) => {
const iframe = document.createElement('iframe');
iframe.style.display = "none";
document.body.appendChild(iframe);
iframe.addEventListener('load', t.step_func_done(() => {
assert_dsd(iframe.contentDocument.body,true);
t.done();
}));
iframe.srcdoc = content;
}, 'iframe, no sandbox');
function getHandler(t, name, shouldHaveShadow) {
return (e) => {
t.step(() => {
if (e.data.name == name) {
assert_false(e.data.error,e.data.msg);
assert_true(e.data.hasShadow == shouldHaveShadow);
t.done();
}
});
};
}
async_test((t) => {
window.addEventListener('message', getHandler(t, 'iframe-sandbox', true));
}, 'sandboxed iframe allows declarative Shadow DOM');
async_test((t) => {
window.addEventListener('message', getHandler(t,'iframe-no-sandbox', true));
}, 'iframe with no sandbox allows declarative Shadow DOM');
</script>
<iframe name="iframe-sandbox" sandbox="allow-scripts" src="support/declarative-child-frame.html" ></iframe>
<iframe name="iframe-no-sandbox" src="support/declarative-child-frame.html"></iframe>
|