summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/webcomponents/test_shadowroot_style.html
blob: 824a8decea1306fb635144fc3b537acfa7404a95 (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
84
85
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=806506
-->
<head>
  <title>Test for ShadowRoot styling</title>
  <script type="text/javascript" src="head.js"></script>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=806506">Bug 806506</a>
<script>

SimpleTest.waitForExplicitFinish();

var content = '<div class="tall" id="bodydiv"></div>' +
              '<div id="container"></div>';
createIframe(content)
  .then((aDocument) => {
    var iframeWin = aDocument.defaultView;

    // Create ShadowRoot.
    var container = aDocument.getElementById("container");
    var elem = aDocument.createElement("div");
    container.appendChild(elem); // Put ShadowRoot host in document.
    var root = elem.attachShadow({mode: "open"});

    // A style element that will be appended into the ShadowRoot.
    var shadowStyle = aDocument.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(aDocument.styleSheets.length, 0, "There shouldn't be any style sheet in the test frame document.");
    is(root.styleSheets.length, 0, "The ShadowRoot should have no style sheets.");
    root.appendChild(shadowStyle);
    is(aDocument.styleSheets.length, 0, "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 = aDocument.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(iframeWin.getComputedStyle(aDocument.getElementById("bodydiv")).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(iframeWin.getComputedStyle(divToStyle).getPropertyValue("height"), "100px", "ShadowRoot style sheets should apply to elements in ShadowRoot.");

    // Tests for author styles not applying in a ShadowRoot.
    var authorStyle = aDocument.createElement("style");
    authorStyle.innerHTML = ".fat { padding-right: 20px; padding-left: 30px; }";
    aDocument.body.appendChild(authorStyle);
    isnot(iframeWin.getComputedStyle(divToStyle).getPropertyValue("padding-right"), "20px", "Author styles should not apply to ShadowRoot.");

    // Test dynamic changes to style in ShadowRoot.
    root.innerHTML = '<div id="divtostyle" class="dummy"></div>';
    divToStyle = root.getElementById("divtostyle");
    var dummyShadowStyle = aDocument.createElement("style");
    dummyShadowStyle.innerHTML = ".dummy { height: 300px; }";
    root.appendChild(dummyShadowStyle);
    is(iframeWin.getComputedStyle(divToStyle).getPropertyValue("height"), "300px", "Dummy element in ShadowRoot should be styled by style in ShadowRoot.");
    dummyShadowStyle.innerHTML = ".dummy { height: 200px; }";
    is(iframeWin.getComputedStyle(divToStyle).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(iframeWin.getComputedStyle(divToStyle).getPropertyValue("padding-top"), "10px", "ID selector in style selector should match element.");

    SimpleTest.finish();
  });
</script>
</body>
</html>