1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/tests/mochitest/webcomponents/test_shadowroot_style.html Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,80 @@ 1.4 +<!DOCTYPE HTML> 1.5 +<html> 1.6 +<!-- 1.7 +https://bugzilla.mozilla.org/show_bug.cgi?id=806506 1.8 +--> 1.9 +<head> 1.10 + <title>Test for ShadowRoot styling</title> 1.11 + <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> 1.12 + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 1.13 +</head> 1.14 +<body> 1.15 +<div class="tall" id="bodydiv"></div> 1.16 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=806506">Bug 806506</a> 1.17 +<script> 1.18 +// Create ShadowRoot. 1.19 +var elem = document.createElement("div"); 1.20 +var root = elem.createShadowRoot(); 1.21 + 1.22 +// A style element that will be appended into the ShadowRoot. 1.23 +var shadowStyle = document.createElement("style"); 1.24 +shadowStyle.innerHTML = ".tall { height: 100px; } .fat { padding-left: inherit; }"; 1.25 + 1.26 +root.innerHTML = '<div id="divtostyle" class="tall fat"></div>'; 1.27 +var divToStyle = root.getElementById("divtostyle"); 1.28 + 1.29 +// Make sure styleSheet counts are correct after appending a style to the ShadowRoot. 1.30 +is(document.styleSheets.length, 1, "There should only be one style sheet on the document from the test style sheet."); 1.31 +is(root.styleSheets.length, 0, "The ShadowRoot should have no style sheets."); 1.32 +root.appendChild(shadowStyle); 1.33 +is(document.styleSheets.length, 1, "Styles in the ShadowRoot element should not be accessible from the document."); 1.34 +is(root.styleSheets.length, 1, "ShadowRoot should have one style sheet from the appened style."); 1.35 +is(root.styleSheets[0].ownerNode, shadowStyle, "First style in ShadowRoot should match the style that was just appended."); 1.36 + 1.37 +var dummyStyle = document.createElement("style"); 1.38 +root.appendChild(dummyStyle); 1.39 +is(root.styleSheets.length, 2, "ShadowRoot should have an additional style from appending dummyStyle."); 1.40 +is(root.styleSheets[1].ownerNode, dummyStyle, "Second style in ShadowRoot should be the dummyStyle."); 1.41 +root.removeChild(dummyStyle); 1.42 +is(root.styleSheets.length, 1, "Removing dummyStyle should remove it from the ShadowRoot style sheets."); 1.43 +is(root.styleSheets[0].ownerNode, shadowStyle, "The style sheet remaining in the ShadowRoot should be shadowStyle."); 1.44 + 1.45 +// Make sure that elements outside of the ShadowRoot are not affected by the ShadowRoot style. 1.46 +isnot(getComputedStyle(document.getElementById("bodydiv"), null).getPropertyValue("height"), "100px", "Style sheets in ShadowRoot should not apply to elements no in the ShadowRoot."); 1.47 + 1.48 +// Make sure that elements in the ShadowRoot are styled according to the ShadowRoot style. 1.49 +is(getComputedStyle(divToStyle, null).getPropertyValue("height"), "100px", "ShadowRoot style sheets should apply to elements in ShadowRoot."); 1.50 + 1.51 +// Tests for applyAuthorStyles. 1.52 +var authorStyle = document.createElement("style"); 1.53 +authorStyle.innerHTML = ".fat { padding-right: 20px; padding-left: 30px; }"; 1.54 +document.body.appendChild(authorStyle); 1.55 + 1.56 +is(root.applyAuthorStyles, false, "applyAuthorStyles defaults to false."); 1.57 +isnot(getComputedStyle(divToStyle, null).getPropertyValue("padding-right"), "20px", "Author styles should not apply to ShadowRoot when ShadowRoot.applyAuthorStyles is false."); 1.58 +root.applyAuthorStyles = true; 1.59 +is(root.applyAuthorStyles, true, "applyAuthorStyles was set to true."); 1.60 +is(getComputedStyle(divToStyle, null).getPropertyValue("padding-right"), "20px", "Author styles should apply to ShadowRoot when ShadowRoot.applyAuthorStyles is true."); 1.61 +root.applyAuthorStyles = false; 1.62 +is(root.applyAuthorStyles, false, "applyAuthorStyles was set to false."); 1.63 +isnot(getComputedStyle(divToStyle, null).getPropertyValue("padding-right"), "20px", "Author styles should not apply to ShadowRoot when ShadowRoot.applyAuthorStyles is false."); 1.64 + 1.65 +// Test dynamic changes to style in ShadowRoot. 1.66 +root.innerHTML = '<div id="divtostyle" class="dummy"></div>'; 1.67 +divToStyle = root.getElementById("divtostyle"); 1.68 +var dummyShadowStyle = document.createElement("style"); 1.69 +dummyShadowStyle.innerHTML = ".dummy { height: 300px; }"; 1.70 +root.appendChild(dummyShadowStyle); 1.71 +is(getComputedStyle(divToStyle, null).getPropertyValue("height"), "300px", "Dummy element in ShadowRoot should be styled by style in ShadowRoot."); 1.72 +dummyShadowStyle.innerHTML = ".dummy { height: 200px; }"; 1.73 +is(getComputedStyle(divToStyle, null).getPropertyValue("height"), "200px", "Dynamic changes to styles in ShadowRoot should change style of affected elements."); 1.74 + 1.75 +// Test id selector in ShadowRoot style. 1.76 +root.innerHTML = '<style>#divtostyle { padding-top: 10px; }</style><div id="divtostyle"></div>'; 1.77 +divToStyle = root.getElementById("divtostyle"); 1.78 +is(getComputedStyle(divToStyle, null).getPropertyValue("padding-top"), "10px", "ID selector in style selector should match element."); 1.79 + 1.80 +</script> 1.81 +</body> 1.82 +</html> 1.83 +