ShadowRoot-interface.html (6689B)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title>Shadow DOM: ShadowRoot interface</title> 5 <meta name="author" title="Ryosuke Niwa" href="mailto:rniwa@webkit.org"> 6 <meta name="assert" content="ShadowRoot interface and its attributes must be defined"> 7 <link rel="help" href="https://w3c.github.io/webcomponents/spec/shadow/#the-shadowroot-interface"> 8 <script src="/resources/testharness.js"></script> 9 <script src="/resources/testharnessreport.js"></script> 10 </head> 11 <body> 12 <div id="log"></div> 13 <script> 14 15 test(function () { 16 assert_true('ShadowRoot' in window, '"ShadowRoot" exists on window'); 17 }, 'Check the existence of ShadowRoot interface'); 18 19 test(function () { 20 assert_equals(Object.getPrototypeOf(ShadowRoot.prototype), DocumentFragment.prototype, 'ShadowRoot must inherit from DocumentFragment'); 21 }, 'ShadowRoot must inherit from DocumentFragment'); 22 23 test(function () { 24 assert_throws_js(TypeError, function () { new ShadowRoot(); }, 'new ShadowRoot() must throw a TypeError'); 25 }, 'ShadowRoot must not be a constructor'); 26 27 function testActiveElement(mode) { 28 test(function () { 29 var host = document.createElement('div'); 30 document.body.appendChild(host); 31 var shadowRoot = host.attachShadow({'mode': mode}); 32 shadowRoot.appendChild(document.createElement('input')); 33 assert_equals(shadowRoot.activeElement, null, 'ShadowRoot.host must return null if an ' + mode + ' shadow tree does not have a focused element'); 34 shadowRoot.firstChild.focus(); 35 assert_equals(shadowRoot.activeElement, shadowRoot.firstChild, 'ShadowRoot.host must return the focused element of an ' + mode + ' shadow tree'); 36 host.remove(); 37 assert_equals(shadowRoot.activeElement, null, 'ShadowRoot.host must return null if an ' + mode + ' shadow tree lost focus'); 38 }, 'ShadowRoot.activeElement must return the focused element of the context object when shadow root is ' + mode + '.'); 39 } 40 41 testActiveElement('open'); 42 testActiveElement('closed'); 43 44 test(function () { 45 var host1 = document.createElement('div'); 46 assert_equals(host1.attachShadow({'mode': 'open'}).host, host1, 'ShadowRoot.host must return the shadow host of an open shadow tree') 47 48 var host2 = document.createElement('div'); 49 assert_equals(host2.attachShadow({'mode': 'closed'}).host, host2, 'ShadowRoot.host must return the shadow host of a closed shadow tree'); 50 }, 'ShadowRoot.host must return the shadow host of the context object.'); 51 52 function testInnerHTML(mode) { 53 test(function () { 54 var host = document.createElement('div'); 55 var shadowRoot = host.attachShadow({'mode': mode}); 56 assert_equals(shadowRoot.innerHTML, '', 'ShadowRoot.innerHTML must be an empty string when the shadow root does not have any children'); 57 58 shadowRoot.appendChild(document.createTextNode('hello')); 59 assert_equals(shadowRoot.innerHTML, 'hello', 'ShadowRoot.innerHTML must serialize a text node child'); 60 61 shadowRoot.appendChild(document.createElement('span')); 62 assert_equals(shadowRoot.innerHTML, 'hello<span></span>', 'ShadowRoot.innerHTML must serialize a HTML element child'); 63 }, 'ShadowRoot.innerHTML must return the result of the HTML fragment serialization algorithm when shadow root is ' + mode + '.'); 64 } 65 66 testInnerHTML('open'); 67 testInnerHTML('closed'); 68 69 function testSetInnerHTML(mode) { 70 test(function () { 71 var host = document.createElement('div'); 72 var shadowRoot = host.attachShadow({'mode': mode}); 73 shadowRoot.innerHTML = 'hello'; 74 assert_equals(shadowRoot.childNodes.length, 1, 'ShadowRoot.innerHTML = "hello" must insert a single child (text node)'); 75 assert_true(shadowRoot.firstChild instanceof Text, 'The first child of the shadow root after ShadowRoot.innerHTML = "hello" must be a Text node'); 76 assert_equals(shadowRoot.firstChild.data, 'hello', 'The first Text node should contain the string "hello" after ShadowRoot.innerHTML = "hello"'); 77 78 shadowRoot.innerHTML = '<b>hello</b>'; 79 assert_equals(shadowRoot.childNodes.length, 1, 'ShadowRoot.innerHTML = "<b>hello</b>" must insert a single child (b)'); 80 assert_true(shadowRoot.firstChild instanceof HTMLElement, 'The first child of the shadow root after ShadowRoot.innerHTML = "<b>hello</b>" must be a HTML element'); 81 assert_equals(shadowRoot.firstChild.localName, 'b', 'The local name of the shadow root\'s first child after ShadowRoot.innerHTML = "<b>hello</b>" must be "b"'); 82 assert_equals(shadowRoot.innerHTML, '<b>hello</b>', 'ShadowRoot.innerHTML must be "<b>hello</b>" after ShadowRoot.innerHTML = "<b>hello</b>"'); 83 84 shadowRoot.innerHTML = ''; 85 assert_equals(shadowRoot.childNodes.length, 0, 'ShadowRoot.innerHTML = "" must remove all its children'); 86 }, 'ShadowRoot.innerHTML must replace all with the result of invoking the fragment parsing algorithm when shadow root is ' + mode + '.'); 87 } 88 89 testSetInnerHTML('open'); 90 testSetInnerHTML('closed'); 91 92 function testStyleSheets(mode) { 93 test(function () { 94 var host = document.createElement('div'); 95 var shadowRoot = host.attachShadow({'mode': mode}); 96 97 shadowRoot.innerHTML = '<span></span><style> a.rule {} </style><style> b.rule {} </style>'; 98 assert_equals(shadowRoot.styleSheets.length, 0, 'shadowRoot.styleSheets must be empty when the shadow root is not connected'); 99 var styles = shadowRoot.querySelectorAll('style'); 100 assert_equals(styles[0].sheet, null, "Sheet should be null in a disconnected tree"); 101 assert_equals(styles[1].sheet, null, "Sheet should be null in a disconnected tree"); 102 103 document.body.appendChild(host); 104 assert_equals(shadowRoot.styleSheets.length, 2, 'shadowRoot.styleSheets must contain two items when the shadow root has two style elements'); 105 assert_equals(shadowRoot.styleSheets[0], styles[0].sheet, 'shadowRoot.styleSheets[0] must be the first style element in the shadow root'); 106 assert_equals(shadowRoot.styleSheets[1], styles[1].sheet, 'shadowRoot.styleSheets[1] must be the second style element in the shadow root'); 107 108 host.remove(); 109 assert_equals(shadowRoot.styleSheets.length, 0, 'shadowRoot.styleSheets must be empty when the shadow root is not connected'); 110 assert_equals(styles[0].sheet, null, "Sheet should be null in a disconnected tree"); 111 assert_equals(styles[1].sheet, null, "Sheet should be null in a disconnected tree"); 112 }, 'ShadowRoot.styleSheets must return a StyleSheetList sequence containing the shadow root style sheets when shadow root is ' + mode + '.'); 113 } 114 115 testStyleSheets('open'); 116 testStyleSheets('closed'); 117 118 </script> 119 </body> 120 </html>