summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/webcomponents/test_shadowroot_style.html
blob: f310ff97c467b3c31a1d7dc2b23e18ff74a2d5f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=806506
-->
<head>
  <title>Test for ShadowRoot styling</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<div class="tall" id="bodydiv"></div>
<div id="container"></div>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=806506">Bug 806506</a>
<script>
// Create ShadowRoot.
var container = document.getElementById("container");
var elem = document.createElement("div");
container.appendChild(elem); // Put ShadowRoot host in document.
var root = elem.createShadowRoot();

// A style element that will be appended into the ShadowRoot.
var shadowStyle = document.createElement("style");
shadowStyle.innerHTML = ".tall { height: 100px; } .fat { padding-left: inherit; }";

root.innerHTML = '<div id="divtostyle" class="tall fat"></div>';
var divToStyle = root.getElementById("divtostyle");

// Make sure styleSheet counts are correct after appending a style to the ShadowRoot.
is(document.styleSheets.length, 1, "There should only be one style sheet on the document from the test style sheet.");
is(root.styleSheets.length, 0, "The ShadowRoot should have no style sheets.");
root.appendChild(shadowStyle);
is(document.styleSheets.length, 1, "Styles in the ShadowRoot element should not be accessible from the document.");
is(root.styleSheets.length, 1, "ShadowRoot should have one style sheet from the appened style.");
is(root.styleSheets[0].ownerNode, shadowStyle, "First style in ShadowRoot should match the style that was just appended.");

var dummyStyle = document.createElement("style");
root.appendChild(dummyStyle);
is(root.styleSheets.length, 2, "ShadowRoot should have an additional style from appending dummyStyle.");
is(root.styleSheets[1].ownerNode, dummyStyle, "Second style in ShadowRoot should be the dummyStyle.");
root.removeChild(dummyStyle);
is(root.styleSheets.length, 1, "Removing dummyStyle should remove it from the ShadowRoot style sheets.");
is(root.styleSheets[0].ownerNode, shadowStyle, "The style sheet remaining in the ShadowRoot should be shadowStyle.");

// Make sure that elements outside of the ShadowRoot are not affected by the ShadowRoot style.
isnot(getComputedStyle(document.getElementById("bodydiv"), null).getPropertyValue("height"), "100px", "Style sheets in ShadowRoot should not apply to elements no in the ShadowRoot.");

// Make sure that elements in the ShadowRoot are styled according to the ShadowRoot style.
is(getComputedStyle(divToStyle, null).getPropertyValue("height"), "100px", "ShadowRoot style sheets should apply to elements in ShadowRoot.");

// Tests for applyAuthorStyles.
var authorStyle = document.createElement("style");
authorStyle.innerHTML = ".fat { padding-right: 20px; padding-left: 30px; }";
document.body.appendChild(authorStyle);

is(root.applyAuthorStyles, false, "applyAuthorStyles defaults to false.");
isnot(getComputedStyle(divToStyle, null).getPropertyValue("padding-right"), "20px", "Author styles should not apply to ShadowRoot when ShadowRoot.applyAuthorStyles is false.");
root.applyAuthorStyles = true;
is(root.applyAuthorStyles, true, "applyAuthorStyles was set to true.");
is(getComputedStyle(divToStyle, null).getPropertyValue("padding-right"), "20px", "Author styles should apply to ShadowRoot when ShadowRoot.applyAuthorStyles is true.");
root.applyAuthorStyles = false;
is(root.applyAuthorStyles, false, "applyAuthorStyles was set to false.");
isnot(getComputedStyle(divToStyle, null).getPropertyValue("padding-right"), "20px", "Author styles should not apply to ShadowRoot when ShadowRoot.applyAuthorStyles is false.");

// Test dynamic changes to style in ShadowRoot.
root.innerHTML = '<div id="divtostyle" class="dummy"></div>';
divToStyle = root.getElementById("divtostyle");
var dummyShadowStyle = document.createElement("style");
dummyShadowStyle.innerHTML = ".dummy { height: 300px; }";
root.appendChild(dummyShadowStyle);
is(getComputedStyle(divToStyle, null).getPropertyValue("height"), "300px", "Dummy element in ShadowRoot should be styled by style in ShadowRoot.");
dummyShadowStyle.innerHTML = ".dummy { height: 200px; }";
is(getComputedStyle(divToStyle, null).getPropertyValue("height"), "200px", "Dynamic changes to styles in ShadowRoot should change style of affected elements.");

// Test id selector in ShadowRoot style.
root.innerHTML = '<style>#divtostyle { padding-top: 10px; }</style><div id="divtostyle"></div>';
divToStyle = root.getElementById("divtostyle");
is(getComputedStyle(divToStyle, null).getPropertyValue("padding-top"), "10px", "ID selector in style selector should match element.");

</script>
</body>
</html>