script-access.html (4530B)
1 <!DOCTYPE html> 2 <title>Declarative Shadow DOM</title> 3 <link rel='author' href='mailto:masonf@chromium.org'> 4 <link rel='help' href='https://github.com/whatwg/dom/issues/831'> 5 <script src='/resources/testharness.js'></script> 6 <script src='/resources/testharnessreport.js'></script> 7 8 <body> 9 <script> 10 let templatesSeen = 0; 11 12 function myObserver(mutationsList, observer) { 13 document.querySelectorAll('.host').forEach(n => { 14 templatesSeen++; 15 n.className = 'done'; 16 switch (n.id) { 17 case 'openhost': 18 case 'closedhost': 19 assert_equals(n.querySelector('template'),null,'No template ever for streaming declarative Shadow DOM'); 20 assert_equals(n.innerHTML, "", 'Declarative shadow host innerHTML should be empty - all content is inside shadow'); 21 break; 22 case 'noroot': 23 const template = n.querySelector('template'); 24 assert_true(!!template,'Regular template should still be present'); 25 // Make sure adding 'shadowrootmode' attribute doesn't trigger a shadow root, 26 // even if added before parsing completes. 27 template.setAttribute('shadowrootmode','open'); 28 assert_not_equals(template.content, null, 'Regular template should have content, even after adding shadowrootmode attribute'); 29 assert_not_equals(template.innerHTML, "", 'Regular template should have innerHTML, even after adding shadowrootmode attribute'); 30 break; 31 default: 32 assert_unreached('Unrecognized template'); 33 } 34 }); 35 } 36 const observer = new MutationObserver(myObserver); 37 observer.observe(document.body, { childList: true, subtree: true }); 38 assert_equals(templatesSeen, 0, 'No mutations yet'); 39 </script> 40 41 <div id=openhost class=host><template shadowrootmode=open> 42 <slot></slot> 43 <!-- Ensure observer runs at this point (https://github.com/web-platform-tests/wpt/issues/35393) --> 44 <script> // some content, which shouldn't be necessary </script> 45 </template></div> 46 47 <div id=closedhost class=host><template shadowrootmode=closed> 48 <slot></slot> 49 <!-- Ensure observer runs at this point (https://github.com/web-platform-tests/wpt/issues/35393) --> 50 <script> // some content, which shouldn't be necessary </script> 51 </template></div> 52 53 <div id=noroot class=host><template> 54 <slot></slot> 55 <!-- Ensure observer runs at this point (https://github.com/web-platform-tests/wpt/issues/35393) --> 56 <script> // some content, which shouldn't be necessary </script> 57 </template></div> 58 59 <script> 60 test(t => { 61 t.add_cleanup(function() { observer.disconnect(); }); 62 63 assert_equals(templatesSeen, 3); 64 65 // Open shadow root 66 let host = document.querySelector('#openhost'); 67 assert_equals(host.querySelector('template'), null, 'No template nodes'); 68 assert_true(!!host.shadowRoot, 'Shadow root should exist'); 69 70 // Closed shadow root 71 host = document.querySelector('#closedhost'); 72 assert_equals(host.querySelector('template'), null, 'No template nodes'); 73 assert_true(!host.shadowRoot, 'Closed shadow root (can\'t detect)'); 74 75 // No shadow root 76 host = document.querySelector('#noroot'); 77 assert_true(!!host.querySelector('template'), 'Template node still present for invalid shadowrootmode value'); 78 assert_true(!host.shadowRoot, 'No shadow root'); 79 },'Streaming Declarative Shadow DOM: template .content() should be null'); 80 </script> 81 82 83 <script> 84 let synchronous_events_received = new Set(); 85 function synchronousHandler(e) { 86 synchronous_events_received.add(e.type); 87 } 88 const sync_events = ["DOMAttrModified","DOMAttributeNameChanged","DOMCharacterDataModified", 89 "DOMElementNameChanged","DOMNodeInserted","DOMNodeInsertedIntoDocument","DOMNodeRemoved", 90 "DOMNodeRemovedFromDocument","DOMSubtreeModified"]; 91 function addSyncObserver(evt) { 92 window.addEventListener(evt, synchronousHandler, true); 93 } 94 function removeSyncObserver(evt) { 95 window.removeEventListener(evt, synchronousHandler, true); 96 } 97 sync_events.forEach(e => addSyncObserver(e)); 98 </script> 99 100 <div id=synchost1> 101 <template shadowrootmode=open> 102 <div class=foo>content</div> 103 <slot></slot> 104 </template> 105 </div> 106 107 <div id=synchost2> 108 <template shadowrootmode=closed> 109 <div class=foo>content</div> 110 <slot></slot> 111 </template> 112 </div> 113 114 <script> 115 test(t => { 116 t.add_cleanup(function() { sync_events.forEach(e => removeSyncObserver(e)) }); 117 assert_true(!synchronous_events_received.size,`Synchronous mutation events fired: ${Array.from(synchronous_events_received)}`); 118 },'Synchronous mutation events should not be fired during streaming declarative shadow DOM parsing'); 119 </script>