OLD | NEW |
(Empty) | |
| 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 def
ined"> |
| 7 <link rel="help" href="https://w3c.github.io/webcomponents/spec/shadow/#the-shad
owroot-interface"> |
| 8 <script src="../../../resources/testharness.js"></script> |
| 9 <script src="../../../resources/testharnessreport.js"></script> |
| 10 <link rel='stylesheet' href='../../../resources/testharness.css'> |
| 11 </head> |
| 12 <body> |
| 13 <div id="log"></div> |
| 14 <script> |
| 15 |
| 16 test(function () { |
| 17 assert_true('ShadowRoot' in window, '"ShadowRoot" exists on window'); |
| 18 }, 'Check the existence of ShadowRoot interface'); |
| 19 |
| 20 test(function () { |
| 21 assert_equals(ShadowRoot.prototype.__proto__, DocumentFragment.prototype, 'S
hadowRoot must inherit from DocumentFragment'); |
| 22 }, 'ShadowRoot must inherit from DocumentFragment'); |
| 23 |
| 24 test(function () { |
| 25 assert_throws({'name': 'TypeError'}, function () { new ShadowRoot(); }, 'new
ShadowRoot() must throw a TypeError'); |
| 26 }, 'ShadowRoot must not be a constructor'); |
| 27 |
| 28 function testActiveElement(mode) { |
| 29 test(function () { |
| 30 var host = document.createElement('div'); |
| 31 document.body.appendChild(host); |
| 32 var shadowRoot = host.attachShadow({'mode': mode}); |
| 33 shadowRoot.appendChild(document.createElement('input')); |
| 34 assert_equals(shadowRoot.activeElement, null, 'ShadowRoot.host must retu
rn null if an ' + mode + ' shadow tree does not have a focused element'); |
| 35 shadowRoot.firstChild.focus(); |
| 36 assert_equals(shadowRoot.activeElement, shadowRoot.firstChild, 'ShadowRo
ot.host must return the focused element of an ' + mode + ' shadow tree'); |
| 37 host.remove(); |
| 38 assert_equals(shadowRoot.activeElement, null, 'ShadowRoot.host must retu
rn null if an ' + mode + ' shadow tree lost focus'); |
| 39 }, 'ShadowRoot.activeElement must return the focused element of the context
object when shadow root is ' + mode + '.'); |
| 40 } |
| 41 |
| 42 testActiveElement('open'); |
| 43 testActiveElement('closed'); |
| 44 |
| 45 test(function () { |
| 46 var host1 = document.createElement('div'); |
| 47 assert_equals(host1.attachShadow({'mode': 'open'}).host, host1, 'ShadowRoot.
host must return the shadow host of an open shadow tree') |
| 48 |
| 49 var host2 = document.createElement('div'); |
| 50 assert_equals(host2.attachShadow({'mode': 'closed'}).host, host2, 'ShadowRoo
t.host must return the shadow host of a closed shadow tree'); |
| 51 }, 'ShadowRoot.host must return the shadow host of the context object.'); |
| 52 |
| 53 function testInnerHTML(mode) { |
| 54 test(function () { |
| 55 var host = document.createElement('div'); |
| 56 var shadowRoot = host.attachShadow({'mode': mode}); |
| 57 assert_equals(shadowRoot.innerHTML, '', 'ShadowRoot.innerHTML must be an
empty string when the shadow root does not have any children'); |
| 58 |
| 59 shadowRoot.appendChild(document.createTextNode('hello')); |
| 60 assert_equals(shadowRoot.innerHTML, 'hello', 'ShadowRoot.innerHTML must
serialize a text node child'); |
| 61 |
| 62 shadowRoot.appendChild(document.createElement('span')); |
| 63 assert_equals(shadowRoot.innerHTML, 'hello<span></span>', 'ShadowRoot.in
nerHTML must serialize a HTML element child'); |
| 64 }, 'ShadowRoot.innerHTML must return the result of the HTML fragment seriali
zation algorithm when shadow root is ' + mode + '.'); |
| 65 } |
| 66 |
| 67 testInnerHTML('open'); |
| 68 testInnerHTML('closed'); |
| 69 |
| 70 function testSetInnerHTML(mode) { |
| 71 test(function () { |
| 72 var host = document.createElement('div'); |
| 73 var shadowRoot = host.attachShadow({'mode': mode}); |
| 74 shadowRoot.innerHTML = 'hello'; |
| 75 assert_equals(shadowRoot.childNodes.length, 1, 'ShadowRoot.innerHTML = "
hello" must insert a single child (text node)'); |
| 76 assert_true(shadowRoot.firstChild instanceof Text, 'The first child of t
he shadow root after ShadowRoot.innerHTML = "hello" must be a Text node'); |
| 77 assert_equals(shadowRoot.firstChild.data, 'hello', 'The first Text node
should contain the string "hello" after ShadowRoot.innerHTML = "hello"'); |
| 78 |
| 79 shadowRoot.innerHTML = '<b>hello</b>'; |
| 80 assert_equals(shadowRoot.childNodes.length, 1, 'ShadowRoot.innerHTML = "
<b>hello</b>" must insert a single child (b)'); |
| 81 assert_true(shadowRoot.firstChild instanceof HTMLElement, 'The first chi
ld of the shadow root after ShadowRoot.innerHTML = "<b>hello</b>" must be a HTML
element'); |
| 82 assert_equals(shadowRoot.firstChild.localName, 'b', 'The local name of t
he shadow root\'s first child after ShadowRoot.innerHTML = "<b>hello</b>" must b
e "b"'); |
| 83 assert_equals(shadowRoot.innerHTML, '<b>hello</b>', 'ShadowRoot.innerHTM
L must be "<b>hello</b>" after ShadowRoot.innerHTML = "<b>hello</b>"'); |
| 84 |
| 85 shadowRoot.innerHTML = ''; |
| 86 assert_equals(shadowRoot.childNodes.length, 0, 'ShadowRoot.innerHTML = "
" must remove all its children'); |
| 87 }, 'ShadowRoot.innerHTML must replace all with the result of invoking the fr
agment parsing algorithm when shadow root is ' + mode + '.'); |
| 88 } |
| 89 |
| 90 testSetInnerHTML('open'); |
| 91 testSetInnerHTML('closed'); |
| 92 |
| 93 function testStyleSheets(mode) { |
| 94 test(function () { |
| 95 var host = document.createElement('div'); |
| 96 var shadowRoot = host.attachShadow({'mode': mode}); |
| 97 |
| 98 assert_equals(shadowRoot.styleSheets.length, 0, 'shadowRoot.styleSheets
must be empty when the shadow root does not contain any stylesheets'); |
| 99 shadowRoot.innerHTML = '<span></span><style> a.rule {} </style><style> b
.rule {} </style>'; |
| 100 assert_equals(shadowRoot.styleSheets.length, 2, 'shadowRoot.styleSheets
must contain two items when the shadow root has two style elements'); |
| 101 var styles = shadowRoot.querySelectorAll('style'); |
| 102 assert_equals(shadowRoot.styleSheets[0], styles[0].sheet, 'shadowRoot.st
yleSheets[0] must be the first style element in the shadow root'); |
| 103 assert_equals(shadowRoot.styleSheets[1], styles[1].sheet, 'shadowRoot.st
yleSheets[1] must be the second style element in the shadow root'); |
| 104 }, 'ShadowRoot.styleSheets must return a StyleSheetList sequence containing
the shadow root style sheets when shadow root is ' + mode + '.'); |
| 105 } |
| 106 |
| 107 testStyleSheets('open'); |
| 108 testStyleSheets('closed'); |
| 109 |
| 110 </script> |
| 111 </body> |
| 112 </html> |
OLD | NEW |