|
| 1 | +<!DOCTYPE html> |
| 2 | +<meta charset="utf-8"> |
| 3 | +<title>Declarative Shadow DOM</title> |
| 4 | +<link rel="author" title="Mason Freed" href="mailto:masonfreed@chromium.org"> |
| 5 | +<link rel="help" href="https://github.com/whatwg/dom/issues/831"> |
| 6 | +<script src="/resources/testharness.js"></script> |
| 7 | +<script src="/resources/testharnessreport.js"></script> |
| 8 | + |
| 9 | +<body> |
| 10 | +<style> |
| 11 | + * { white-space: pre; } |
| 12 | + iframe { display:none; } |
| 13 | +</style> |
| 14 | +<div id=log></div> |
| 15 | + |
| 16 | +<div id=mainpage style="display:none"> |
| 17 | + <div class=wrapper> |
| 18 | + <div class=host> |
| 19 | + <template shadowroot=open> |
| 20 | + <span class=content>Content</span> |
| 21 | + </template> |
| 22 | + </div> |
| 23 | + </div> |
| 24 | +</div> |
| 25 | + |
| 26 | +<script> |
| 27 | +const content = ` |
| 28 | + <html><body> |
| 29 | + <div class=wrapper> |
| 30 | + <div class=host> |
| 31 | + <template shadowroot=open> |
| 32 | + <span class=content>Content</span> |
| 33 | + </template> |
| 34 | + </div> |
| 35 | + </div> |
| 36 | + </body></html> |
| 37 | +`; |
| 38 | + |
| 39 | +function assert_dsd(el,shouldHaveShadow) { |
| 40 | + const wrapper = el.querySelector('.wrapper'); |
| 41 | + assert_true(!!wrapper,'Unable to find wrapper element'); |
| 42 | + const host = wrapper.querySelector('.host'); |
| 43 | + assert_true(!!host,'Unable to find host element'); |
| 44 | + if (shouldHaveShadow) { |
| 45 | + assert_true(!!host.shadowRoot, 'Shadow root NOT FOUND.'); |
| 46 | + assert_true(!!host.shadowRoot.querySelector('.content'),'Unable to locate content'); |
| 47 | + } else { |
| 48 | + assert_true(!host.shadowRoot, 'Shadow root FOUND - none should be present.'); |
| 49 | + const tmpl = host.querySelector('template'); |
| 50 | + assert_true(!!tmpl, 'The template should be left as a <template> element'); |
| 51 | + assert_equals(tmpl.getAttribute('shadowroot'),'open','The shadowroot attribute should still be present'); |
| 52 | + assert_true(!!tmpl.content.querySelector('.content'),'Unable to locate content'); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +test(() => { |
| 57 | + const div = document.getElementById('mainpage'); |
| 58 | + assert_dsd(div,true); |
| 59 | + assert_false(document.allowDeclarativeShadowDom,'The default value for allowDeclarativeShadowDom should be false'); |
| 60 | +}, 'Non-fragment parsing needs no opt-in'); |
| 61 | + |
| 62 | +test(() => { |
| 63 | + const div = document.createElement('div'); |
| 64 | + div.innerHTML = content; |
| 65 | + assert_false(document.allowDeclarativeShadowDom,'The default value for allowDeclarativeShadowDom should be false'); |
| 66 | + assert_dsd(div,false); |
| 67 | + document.allowDeclarativeShadowDom = true; |
| 68 | + div.innerHTML = content; |
| 69 | + assert_dsd(div,true); |
| 70 | + document.allowDeclarativeShadowDom = false; // Don't affect other tests |
| 71 | +}, 'innerHTML on element'); |
| 72 | + |
| 73 | +test(() => { |
| 74 | + const templateContent = `<template id=tmpl>${content}</template>`; |
| 75 | + assert_false(document.allowDeclarativeShadowDom,'Default allowDeclarativeShadowDom should be false'); |
| 76 | + const div = document.createElement('div'); |
| 77 | + div.innerHTML = templateContent; |
| 78 | + assert_dsd(div.querySelector('#tmpl').content,false); |
| 79 | + document.allowDeclarativeShadowDom = true; |
| 80 | + div.innerHTML = templateContent; |
| 81 | + assert_dsd(div.querySelector('#tmpl').content,true); |
| 82 | + // Make sure to set back to avoid affecting other tests |
| 83 | + document.allowDeclarativeShadowDom = false; // Don't affect other tests |
| 84 | +}, 'innerHTML on element, with template content'); |
| 85 | + |
| 86 | +test(() => { |
| 87 | + const temp = document.createElement('template'); |
| 88 | + temp.innerHTML = content; |
| 89 | + assert_dsd(temp.content,false, 'innerHTML by default should not allow declarative shadow content'); |
| 90 | + assert_false(document.allowDeclarativeShadowDom,'The default value for document.allowDeclarativeShadowDom should be false'); |
| 91 | + assert_false(temp.content.allowDeclarativeShadowDom,'The default value for template fragment allowDeclarativeShadowDom should be false'); |
| 92 | + temp.content.allowDeclarativeShadowDom = true; |
| 93 | + assert_false(document.allowDeclarativeShadowDom,'Setting template allowDeclarativeShadowDom shouldn\'t affect document.allowDeclarativeShadowDom'); |
| 94 | + temp.innerHTML = content; |
| 95 | + assert_true(temp.content.allowDeclarativeShadowDom,'Setting allowDeclarativeShadowDom should persist across innerHTML set'); |
| 96 | + assert_dsd(temp.content,true, 'innerHTML should allow declarative shadow content if template.content.allowDeclarativeShadowDom is set'); |
| 97 | + temp.content.allowDeclarativeShadowDom = false; |
| 98 | + document.allowDeclarativeShadowDom = true; |
| 99 | + temp.innerHTML = content; |
| 100 | + assert_false(temp.content.allowDeclarativeShadowDom,'Setting allowDeclarativeShadowDom should persist across innerHTML set'); |
| 101 | + assert_true(document.allowDeclarativeShadowDom,'document.allowDeclarativeShadowDom should still be set'); |
| 102 | + assert_dsd(temp.content,true, 'innerHTML should allow declarative shadow content if document.allowDeclarativeShadowDom is set'); |
| 103 | + document.allowDeclarativeShadowDom = false; // Don't affect other tests |
| 104 | +}, 'Setting template.innerHTML'); |
| 105 | + |
| 106 | +test(() => { |
| 107 | + const templateContent = `<template id=tmpl>${content}</template>`; |
| 108 | + const temp = document.createElement('template'); |
| 109 | + temp.innerHTML = templateContent; |
| 110 | + assert_dsd(temp.content.querySelector('#tmpl').content,false); |
| 111 | + document.allowDeclarativeShadowDom = true; |
| 112 | + temp.innerHTML = templateContent; |
| 113 | + assert_dsd(temp.content.querySelector('#tmpl').content,true); |
| 114 | + document.allowDeclarativeShadowDom = false; // Don't affect other tests |
| 115 | +}, 'Setting template.innerHTML with nested template content'); |
| 116 | + |
| 117 | +test(() => { |
| 118 | + const parser = new DOMParser(); |
| 119 | + let fragment = parser.parseFromString(content,'text/html'); |
| 120 | + assert_dsd(fragment.body,false); |
| 121 | + assert_false(parser.allowDeclarativeShadowDom,'The default value for allowDeclarativeShadowDom should be false'); |
| 122 | + parser.allowDeclarativeShadowDom = true; |
| 123 | + assert_false(document.allowDeclarativeShadowDom,'Setting allowDeclarativeShadowDom shouldn\'t affect document.allowDeclarativeShadowDom'); |
| 124 | + fragment = parser.parseFromString(content,'text/html'); |
| 125 | + assert_dsd(fragment.body,true); |
| 126 | +}, 'DOMParser'); |
| 127 | + |
| 128 | +test(() => { |
| 129 | + const doc = document.implementation.createHTMLDocument("Document"); |
| 130 | + doc.body.innerHTML = content; |
| 131 | + assert_dsd(doc.body,false); |
| 132 | + assert_false(doc.allowDeclarativeShadowDom,'The default value for allowDeclarativeShadowDom should be false'); |
| 133 | + doc.allowDeclarativeShadowDom = true; |
| 134 | + assert_false(document.allowDeclarativeShadowDom,'Setting allowDeclarativeShadowDom shouldn\'t affect document.allowDeclarativeShadowDom'); |
| 135 | + doc.body.innerHTML = content; |
| 136 | + assert_dsd(doc.body,true); |
| 137 | +}, 'createHTMLDocument'); |
| 138 | + |
| 139 | +test(() => { |
| 140 | + const doc = document.implementation.createHTMLDocument("Document"); |
| 141 | + let range = doc.createRange(); |
| 142 | + range.selectNode(doc.body); |
| 143 | + let documentFragment = range.createContextualFragment(content); |
| 144 | + assert_dsd(documentFragment,false); |
| 145 | + doc.allowDeclarativeShadowDom = true; |
| 146 | + assert_false(document.allowDeclarativeShadowDom,'Setting allowDeclarativeShadowDom shouldn\'t affect document.allowDeclarativeShadowDom'); |
| 147 | + documentFragment = range.createContextualFragment(content); |
| 148 | + assert_dsd(documentFragment,true); |
| 149 | +}, 'createContextualFragment'); |
| 150 | + |
| 151 | +async_test((t) => { |
| 152 | + let client = new XMLHttpRequest(); |
| 153 | + client.addEventListener('load', t.step_func_done(() => { |
| 154 | + assert_true(client.status == 200 && client.responseXML != null); |
| 155 | + assert_dsd(client.responseXML.body,false); |
| 156 | + t.done(); |
| 157 | + })); |
| 158 | + client.open("GET", `data:text/html,${content}`); |
| 159 | + client.responseType = 'document'; |
| 160 | + assert_false(client.allowDeclarativeShadowDom,'The default value for allowDeclarativeShadowDom should be false'); |
| 161 | + client.send(); |
| 162 | +}, 'XMLHttpRequest, disabled'); |
| 163 | + |
| 164 | +async_test((t) => { |
| 165 | + let client = new XMLHttpRequest(); |
| 166 | + client.addEventListener('load', t.step_func_done(() => { |
| 167 | + assert_true(client.status == 200 && client.responseXML != null); |
| 168 | + assert_dsd(client.responseXML.body,true); |
| 169 | + t.done(); |
| 170 | + })); |
| 171 | + client.open("GET", `data:text/html,${content}`); |
| 172 | + client.responseType = 'document'; |
| 173 | + client.allowDeclarativeShadowDom = true; |
| 174 | + assert_false(document.allowDeclarativeShadowDom,'Setting allowDeclarativeShadowDom shouldn\'t affect document.allowDeclarativeShadowDom'); |
| 175 | + client.send(); |
| 176 | +}, 'XMLHttpRequest, enabled'); |
| 177 | + |
| 178 | +async_test((t) => { |
| 179 | + const iframe = document.createElement('iframe'); |
| 180 | + iframe.style.display = "none"; |
| 181 | + iframe.sandbox = "allow-same-origin"; |
| 182 | + document.body.appendChild(iframe); |
| 183 | + iframe.addEventListener('load', t.step_func_done(() => { |
| 184 | + assert_dsd(iframe.contentDocument.body,false); |
| 185 | + t.done(); |
| 186 | + })); |
| 187 | + iframe.srcdoc = content; |
| 188 | +}, 'iframe, disabled'); |
| 189 | + |
| 190 | +async_test((t) => { |
| 191 | + const iframe = document.createElement('iframe'); |
| 192 | + iframe.style.display = "none"; |
| 193 | + iframe.sandbox = "allow-same-origin allow-declarative-shadow-dom"; |
| 194 | + document.body.appendChild(iframe); |
| 195 | + iframe.addEventListener('load', t.step_func_done(() => { |
| 196 | + assert_dsd(iframe.contentDocument.body,true); |
| 197 | + t.done(); |
| 198 | + })); |
| 199 | + iframe.allowDeclarativeShadowDom = true; |
| 200 | + assert_false(document.allowDeclarativeShadowDom,'Setting allowDeclarativeShadowDom shouldn\'t affect document.allowDeclarativeShadowDom'); |
| 201 | + iframe.srcdoc = content; |
| 202 | +}, 'iframe, enabled'); |
| 203 | + |
| 204 | +function getHandler(t, name, shouldHaveShadow) { |
| 205 | + return (e) => { |
| 206 | + t.step(() => { |
| 207 | + if (e.data.name == name) { |
| 208 | + assert_false(e.data.error,e.data.msg); |
| 209 | + assert_true(e.data.hasShadow == shouldHaveShadow); |
| 210 | + t.done(); |
| 211 | + } |
| 212 | + }); |
| 213 | + }; |
| 214 | +} |
| 215 | +async_test((t) => { |
| 216 | + window.addEventListener('message', getHandler(t, 'iframe_disallow', false)); |
| 217 | +}, 'iframe without allow-declarative-shadow-dom sandbox flag disallows declarative Shadow DOM'); |
| 218 | + |
| 219 | +async_test((t) => { |
| 220 | + window.addEventListener('message', getHandler(t,'iframe_allow', true)); |
| 221 | +}, 'iframe with allow-declarative-shadow-dom sandbox flag allows declarative Shadow DOM'); |
| 222 | + |
| 223 | +</script> |
| 224 | + |
| 225 | +<iframe name="iframe_disallow" sandbox="allow-scripts" src="support/declarative-child-frame.html" ></iframe> |
| 226 | +<iframe name="iframe_allow" sandbox="allow-scripts allow-declarative-shadow-dom" src="support/declarative-child-frame.html"></iframe> |
0 commit comments