dom/tests/mochitest/webcomponents/test_shadowroot_style.html

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:daf4ca142467
1 <!DOCTYPE HTML>
2 <html>
3 <!--
4 https://bugzilla.mozilla.org/show_bug.cgi?id=806506
5 -->
6 <head>
7 <title>Test for ShadowRoot styling</title>
8 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
9 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
10 </head>
11 <body>
12 <div class="tall" id="bodydiv"></div>
13 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=806506">Bug 806506</a>
14 <script>
15 // Create ShadowRoot.
16 var elem = document.createElement("div");
17 var root = elem.createShadowRoot();
18
19 // A style element that will be appended into the ShadowRoot.
20 var shadowStyle = document.createElement("style");
21 shadowStyle.innerHTML = ".tall { height: 100px; } .fat { padding-left: inherit; }";
22
23 root.innerHTML = '<div id="divtostyle" class="tall fat"></div>';
24 var divToStyle = root.getElementById("divtostyle");
25
26 // Make sure styleSheet counts are correct after appending a style to the ShadowRoot.
27 is(document.styleSheets.length, 1, "There should only be one style sheet on the document from the test style sheet.");
28 is(root.styleSheets.length, 0, "The ShadowRoot should have no style sheets.");
29 root.appendChild(shadowStyle);
30 is(document.styleSheets.length, 1, "Styles in the ShadowRoot element should not be accessible from the document.");
31 is(root.styleSheets.length, 1, "ShadowRoot should have one style sheet from the appened style.");
32 is(root.styleSheets[0].ownerNode, shadowStyle, "First style in ShadowRoot should match the style that was just appended.");
33
34 var dummyStyle = document.createElement("style");
35 root.appendChild(dummyStyle);
36 is(root.styleSheets.length, 2, "ShadowRoot should have an additional style from appending dummyStyle.");
37 is(root.styleSheets[1].ownerNode, dummyStyle, "Second style in ShadowRoot should be the dummyStyle.");
38 root.removeChild(dummyStyle);
39 is(root.styleSheets.length, 1, "Removing dummyStyle should remove it from the ShadowRoot style sheets.");
40 is(root.styleSheets[0].ownerNode, shadowStyle, "The style sheet remaining in the ShadowRoot should be shadowStyle.");
41
42 // Make sure that elements outside of the ShadowRoot are not affected by the ShadowRoot style.
43 isnot(getComputedStyle(document.getElementById("bodydiv"), null).getPropertyValue("height"), "100px", "Style sheets in ShadowRoot should not apply to elements no in the ShadowRoot.");
44
45 // Make sure that elements in the ShadowRoot are styled according to the ShadowRoot style.
46 is(getComputedStyle(divToStyle, null).getPropertyValue("height"), "100px", "ShadowRoot style sheets should apply to elements in ShadowRoot.");
47
48 // Tests for applyAuthorStyles.
49 var authorStyle = document.createElement("style");
50 authorStyle.innerHTML = ".fat { padding-right: 20px; padding-left: 30px; }";
51 document.body.appendChild(authorStyle);
52
53 is(root.applyAuthorStyles, false, "applyAuthorStyles defaults to false.");
54 isnot(getComputedStyle(divToStyle, null).getPropertyValue("padding-right"), "20px", "Author styles should not apply to ShadowRoot when ShadowRoot.applyAuthorStyles is false.");
55 root.applyAuthorStyles = true;
56 is(root.applyAuthorStyles, true, "applyAuthorStyles was set to true.");
57 is(getComputedStyle(divToStyle, null).getPropertyValue("padding-right"), "20px", "Author styles should apply to ShadowRoot when ShadowRoot.applyAuthorStyles is true.");
58 root.applyAuthorStyles = false;
59 is(root.applyAuthorStyles, false, "applyAuthorStyles was set to false.");
60 isnot(getComputedStyle(divToStyle, null).getPropertyValue("padding-right"), "20px", "Author styles should not apply to ShadowRoot when ShadowRoot.applyAuthorStyles is false.");
61
62 // Test dynamic changes to style in ShadowRoot.
63 root.innerHTML = '<div id="divtostyle" class="dummy"></div>';
64 divToStyle = root.getElementById("divtostyle");
65 var dummyShadowStyle = document.createElement("style");
66 dummyShadowStyle.innerHTML = ".dummy { height: 300px; }";
67 root.appendChild(dummyShadowStyle);
68 is(getComputedStyle(divToStyle, null).getPropertyValue("height"), "300px", "Dummy element in ShadowRoot should be styled by style in ShadowRoot.");
69 dummyShadowStyle.innerHTML = ".dummy { height: 200px; }";
70 is(getComputedStyle(divToStyle, null).getPropertyValue("height"), "200px", "Dynamic changes to styles in ShadowRoot should change style of affected elements.");
71
72 // Test id selector in ShadowRoot style.
73 root.innerHTML = '<style>#divtostyle { padding-top: 10px; }</style><div id="divtostyle"></div>';
74 divToStyle = root.getElementById("divtostyle");
75 is(getComputedStyle(divToStyle, null).getPropertyValue("padding-top"), "10px", "ID selector in style selector should match element.");
76
77 </script>
78 </body>
79 </html>
80

mercurial