4 <script src="../../../resources/js-test.js"></script>
5 <script src="resources/shadow-dom.js"></script>
12 <body class='mytheme'>
14 <div id='sandbox'></div>
16 <pre id='console'></pre>
18 description('Test whether :host-context matches a shadow host correctly.');
20 var sandbox = document.getElementById('sandbox');
22 function backgroundColorOf(selector) {
23 return window.getComputedStyle(getNodeInTreeOfTrees(selector)).backgroundColor;
26 function backgroundColorShouldBe(selector, expected) {
27 shouldBeEqualToString('backgroundColorOf(\'' + selector + '\')', expected);
31 sandbox.innerHTML = '';
34 debug(':host-context out of shadow tree should not match any shadow hosts.');
37 createDOM('div', {'id': 'host'},
40 document.createTextNode('Hello')))));
42 backgroundColorShouldBe('host', 'rgba(0, 0, 0, 0)');
46 debug(':host-context with * should not match any shadow hosts.');
49 createDOM('div', {'id': 'host'},
51 createDOM('style', {},
52 document.createTextNode('*:host-context(*) { background-color: red; }')),
54 document.createTextNode('Hello')))));
56 backgroundColorShouldBe('host', 'rgba(0, 0, 0, 0)');
60 debug(':host-context with tag selector should not match any shadow hosts.');
63 createDOM('div', {'id': 'host'},
65 createDOM('style', {},
66 document.createTextNode('div:host-context(*) { background-color: red; }')),
68 document.createTextNode('Hello')))));
70 backgroundColorShouldBe('host', 'rgba(0, 0, 0, 0)');
74 debug(':host-context with class selector should not match any shadow hosts.');
77 createDOM('div', {'id': 'host', 'class': 'host'},
79 createDOM('style', {},
80 document.createTextNode('.host:host-context(*) { background-color: red; }')),
82 document.createTextNode('Hello')))));
84 backgroundColorShouldBe('host', 'rgba(0, 0, 0, 0)');
88 debug(':host-context with id selector should not match any shadow hosts.');
91 createDOM('div', {'id': 'host'},
93 createDOM('style', {},
94 document.createTextNode('#host:host-context(*) { background-color: red; }')),
96 document.createTextNode('Hello')))));
98 backgroundColorShouldBe('host', 'rgba(0, 0, 0, 0)');
102 debug(':host-context with attribute selector should not match any shadow hosts.');
105 createDOM('div', {'id': 'host', 'foo': 'bar'},
107 createDOM('style', {},
108 document.createTextNode('[foo=bar]:host-context(*) { background-color: red; }')),
110 document.createTextNode('Hello')))));
112 backgroundColorShouldBe('host', 'rgba(0, 0, 0, 0)');
116 debug(':host-context in a shadow tree should match its shadow host.');
119 createDOM('div', {'id': 'host'},
121 createDOM('style', {},
122 document.createTextNode(':host-context(body.mytheme) { background-color: green; }')),
124 document.createTextNode('Hello')))));
126 backgroundColorShouldBe('host', 'rgb(0, 128, 0)');
130 debug(':host-context with :host in a shadow tree should match its shadow host.');
133 createDOM('div', {'id': 'host'},
135 createDOM('style', {},
136 document.createTextNode(':host-context(div:host) { background-color: green; }')),
138 document.createTextNode('Hello')))));
140 backgroundColorShouldBe('host', 'rgb(0, 128, 0)');
144 debug(':host-context takes simple selectors and matches when one of the simple selectors matches.');
147 createDOM('div', {'id': 'parentOfHost'},
148 createDOM('div', {'id': 'host'},
150 createDOM('style', {},
151 document.createTextNode(':host-context(body:not(.mytheme), span, div#parentOfHost) { background-color: green; }')),
153 document.createTextNode('Hello'))))));
155 backgroundColorShouldBe('host', 'rgb(0, 128, 0)');
159 debug(':host-context matches a shadow host in just a nested shadow tree, not all enclosing shadow trees.');
162 createDOM('div', {'id': 'host1'},
164 createDOM('div', {'id': 'host2'},
166 createDOM('style', {},
167 document.createTextNode(':host-context(*) { background-color: green; }')),
169 document.createTextNode('Hello')))))));
171 backgroundColorShouldBe('host1', 'rgba(0, 0, 0, 0)');
172 backgroundColorShouldBe('host1/host2', 'rgb(0, 128, 0)');
174 debug(':host-context matches based on a composed tree.');
177 createDOM('div', {'id': 'parentOfHost'},
179 createDOM('span', {'id': 'spanA'},
180 createDOM('content', {}))),
181 createDOM('div', {'id': 'host'},
183 createDOM('style', {},
184 document.createTextNode(':host-context(span#spanA) > div { background-color: green; }')),
185 createDOM('div', {'id': 'target'},
186 document.createTextNode('Hello'))))));
188 backgroundColorShouldBe('host/target', 'rgb(0, 128, 0)');
192 debug(':host-context matches based on a composed tree when having multiple shadow roots.');
195 createDOM('div', {'id': 'parentOfHost'},
197 createDOM('span', {'id': 'spanA'},
198 document.createTextNode('no content, no shadow'))),
200 createDOM('span', {'id': 'spanB'},
201 createDOM('content', {}))),
203 createDOM('span', {'id': 'spanC'},
204 createDOM('shadow', {}))),
205 createDOM('div', {'id': 'host'},
207 createDOM('style', {},
208 document.createTextNode(':host-context(span#spanA) > #targetA { background-color: red; }')),
209 createDOM('style', {},
210 document.createTextNode(':host-context(span#spanB) > #targetB { background-color: green; }')),
211 createDOM('style', {},
212 document.createTextNode(':host-context(span#spanC) > #targetC { background-color: green; }')),
213 createDOM('div', {'id': 'targetA'},
214 document.createTextNode('Hello')),
215 createDOM('div', {'id': 'targetB'},
216 document.createTextNode('Hello')),
217 createDOM('div', {'id': 'targetC'},
218 document.createTextNode('Hello'))))));
220 backgroundColorShouldBe('host/targetA', 'rgba(0, 0, 0, 0)');
221 backgroundColorShouldBe('host/targetB', 'rgb(0, 128, 0)');
222 backgroundColorShouldBe('host/targetC', 'rgb(0, 128, 0)');
226 debug(':host-context is updated when its matched ancestor changes className or id.');
229 createDOM('div', {'id': 'parentOfHost', 'class': 'sometheme' },
230 createDOM('div', {'id': 'host'},
232 createDOM('style', {},
233 document.createTextNode(':host-context(div#parentOfHost.sometheme) { background-color: green; }')),
235 document.createTextNode('Hello'))))));
237 backgroundColorShouldBe('host', 'rgb(0, 128, 0)');
238 document.getElementById('parentOfHost').className = 'mytheme';
239 backgroundColorShouldBe('host', 'rgba(0, 0, 0, 0)');
244 createDOM('div', {'id': 'parentOfHost', 'class': 'sometheme' },
245 createDOM('div', {'id': 'host'},
247 createDOM('style', {},
248 document.createTextNode(':host-context(div#parentOfHost.sometheme) { background-color: green; }')),
250 document.createTextNode('Hello'))))));
252 backgroundColorShouldBe('host', 'rgb(0, 128, 0)');
253 var parentOfHost = document.getElementById('parentOfHost');
254 parentOfHost.id = 'differntIdValue';
255 backgroundColorShouldBe('host', 'rgba(0, 0, 0, 0)');
259 debug('Compare :host-context with :host-context.');
262 createDOM('div', {'id': 'host'},
264 createDOM('style', {},
265 document.createTextNode(':host-context(div:host-context(*), div#sandbox) { background-color: green; }')),
266 createDOM('style', {},
267 document.createTextNode(':host-context(body.mytheme) { background-color: red; }')),
269 document.createTextNode('Hello')))));
271 // :host-context(div:host-context(*), div#sandbox) wins, because div#sandbox > body.mytheme.
272 backgroundColorShouldBe('host', 'rgb(0, 128, 0)');
277 createDOM('div', {'id': 'host'},
279 createDOM('style', {},
280 document.createTextNode(':host-context(div:host-context(*), div#nomatch) { background-color: green; }')),
281 createDOM('style', {},
282 document.createTextNode(':host-context(body.mytheme) { background-color: red; }')),
284 document.createTextNode('Hello')))));
286 // :host-context(body.mytheme) wins, because div:host-context(*) < body.mytheme.
287 backgroundColorShouldBe('host', 'rgb(255, 0, 0)');
291 // Test for specificiy of ":host-context(...) > ...".
293 createDOM('div', {'id': 'host'},
295 createDOM('style', {},
296 document.createTextNode(':host-context(div:host-context(*), div#host:host-context(*)) > div { background-color: green; }')),
297 createDOM('style', {},
298 document.createTextNode(':host-context(body.mytheme) > div { background-color: red; }')),
299 createDOM('div', {'id': 'target'},
300 document.createTextNode('Hello')))));
302 backgroundColorShouldBe('host/target', 'rgb(0, 128, 0)');
307 createDOM('div', {'id': 'host', 'class': 'host'},
309 createDOM('style', {},
310 document.createTextNode(':host-context(div:host-context(*), div#host.host:host-context(*)) > div { background-color: green; }')),
311 createDOM('style', {},
312 document.createTextNode(':host-context(body) > div#target { background-color: red; }')),
313 createDOM('div', {'id': 'target'},
314 document.createTextNode('Hello')))));
316 backgroundColorShouldBe('host/target', 'rgb(0, 128, 0)');
321 createDOM('div', {'id': 'host', 'class': 'host'},
323 createDOM('style', {},
324 document.createTextNode(':host-context(div:host-context(div:host-context(div:host-context(div:host-context(*))))) > div { background-color: green; }')),
325 createDOM('style', {},
326 document.createTextNode(':host-context(div) > div { background-color: red; }')),
327 createDOM('div', {'id': 'target'},
328 document.createTextNode('Hello')))));
330 backgroundColorShouldBe('host/target', 'rgb(0, 128, 0)');
334 debug(':host-context without functional notation should not match any shadow hosts.');
337 createDOM('div', {'id': 'host'},
339 createDOM('style', {},
340 document.createTextNode(':host-context { background-color: red; }')),
342 document.createTextNode('Hello')))));
344 backgroundColorShouldBe('host', 'rgba(0, 0, 0, 0)');