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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
|
<!DOCTYPE HTML>
<html>
<head>
<title>Test for bug 1359217</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css">
</head>
<body>
<iframe id="test"></iframe>
<pre id="log">
<script>
/**
* This test checks that getMatchingCSSRules returns correct style set in
* various cases. To avoid effects from UA sheets, most of the tests use
* an element with "unknowntagname".
*/
const InspectorUtils = SpecialPowers.InspectorUtils;
let iframe = document.getElementById("test");
SimpleTest.waitForExplicitFinish();
function getStyleRules(elem) {
return InspectorUtils.getMatchingCSSRules(elem);
}
function getDeclarationWithOrigin(target, origin) {
for (let rule of getStyleRules(target)) {
if (rule.declarationOrigin == origin) {
return rule;
}
}
return null;
}
function checkDeclaration(target, origin, props) {
ok(!!props, "Should pass some properties to check");
let decl = getDeclarationWithOrigin(target, origin);
ok(!!decl, "Should find a declaration for " + origin);
info(decl.style.cssText);
for (let [k, v] of Object.entries(props)) {
is(decl.style[k], v, `Should see ${k}: ${v}`);
}
let modificationThrew = false;
try {
presHintDecl.style.width = "0px";
} catch (e) {
modificationThrew = true;
}
ok(modificationThrew, "Should not be mutable");
}
// This will check that value of z-index property declarations in the
// rules from getMatchingCSSRules matches the given content.
function checkRules(doc, rulesContent, queryStr = "unknowntagname") {
let elem = doc.querySelector(queryStr);
let rules = getStyleRules(elem);
is(rules.length, rulesContent.length, "Rule length should match");
if (rules.length != rulesContent.length) {
return;
}
for (let i = 0; i < rules.length; i++) {
let style = rules[i].style;
let expectation = rulesContent[i].toString();
is(style.length, 1, "Should contain only one declaration");
is(style.zIndex, expectation, "Should match expectation");
}
}
const tests = [
{
title: "Add new stylesheet",
async run(doc) {
checkRules(doc, [1]);
let link = doc.createElement("link");
link.rel = "stylesheet";
link.href = "getMatchingCSSRules-2.css";
let load = new Promise(resolve => { link.onload = resolve; });
doc.head.appendChild(link);
await load;
checkRules(doc, [1, 2]);
},
},
{
title: "Remove stylesheet",
async run(doc) {
checkRules(doc, [1]);
doc.querySelector("link").remove();
checkRules(doc, []);
},
},
{
title: "Enable stylesheet",
async run(doc) {
// Set disabled flag before we invoke the utils.
let link = doc.querySelector("link");
link.sheet.disabled = true;
checkRules(doc, []);
link.sheet.disabled = false;
checkRules(doc, [1]);
},
},
{
title: "Disable stylesheet",
async run(doc) {
checkRules(doc, [1]);
doc.querySelector("link").sheet.disabled = true;
checkRules(doc, []);
},
},
{
title: "Change stylesheet set",
base: "alternate",
async run(doc) {
checkRules(doc, []);
doc.selectedStyleSheetSet = "x";
checkRules(doc, [1]);
doc.selectedStyleSheetSet = "";
checkRules(doc, []);
},
},
{
title: "Add and remove rules",
async run(doc) {
checkRules(doc, [1]);
let sheet = doc.querySelector("link").sheet;
info("Inserting style rule");
sheet.insertRule("unknowntagname { z-index: 3; }", 1);
checkRules(doc, [1, 3]);
info("Removing style rule");
sheet.deleteRule(0);
checkRules(doc, [3]);
info("Inserting media rule");
sheet.insertRule("@media all { unknowntagname { z-index: 4; } }", 1);
checkRules(doc, [3, 4]);
info("Inserting supports rule");
sheet.insertRule(
"@supports (z-index: 0) { unknowntagname { z-index: 5; } }", 1);
checkRules(doc, [3, 5, 4]);
info("Inserting import rule");
sheet.insertRule("@import url(getMatchingCSSRules-2.css);", 0);
// There is no notification we can get when the associated style
// sheet gets loaded, so we have to query it.
while (true) {
try {
sheet.cssRules[0].styleSheet.cssRules;
break;
} catch (e) {
if (e.name == "InvalidAccessError") {
await new Promise(resolve => requestAnimationFrame(resolve));
} else {
throw e;
}
}
}
checkRules(doc, [2, 3, 5, 4]);
info("Removing supports rule");
sheet.deleteRule(2);
checkRules(doc, [2, 3, 4]);
info("Removing media rule");
sheet.deleteRule(2);
checkRules(doc, [2, 3]);
info("Removing import rule");
sheet.deleteRule(0);
checkRules(doc, [3]);
},
},
{
title: "Check UA sheets",
async run(doc) {
doc.querySelector("link").remove();
checkRules(doc, []);
let elem = doc.querySelector("unknowntagname");
elem.setAttribute("dir", "");
let seenUnicodeBidi = false;
for (let rule of getStyleRules(elem)) {
if (rule.style.unicodeBidi == "isolate") {
seenUnicodeBidi = true;
break;
}
}
ok(seenUnicodeBidi, "Should have unicode-bidi " +
"declaration from UA stylesheet html.css");
},
},
{
title: "Check adopted sheets",
async run(doc, win) {
checkRules(doc, [1]);
let sheet = new win.CSSStyleSheet();
sheet.replaceSync(`unknowntagname { z-index: 5 }`);
doc.adoptedStyleSheets.push(sheet);
checkRules(doc, [1, 5]);
},
},
{
title: "Check pres hints",
async run(doc, win) {
checkDeclaration(doc.querySelector("img"), "pres-hints", { width: "10px", height: "15px" });
checkDeclaration(doc.querySelector(".anim"), "animations", {zIndex: "1"});
checkDeclaration(doc.querySelector("[style]"), "style-attribute", {zIndex: "1"});
// TODO: transitions/SMIL are a bit more annoying to test.
},
}
];
add_task(async function runTests() {
for (let i = 0; i < tests.length; i++) {
let test = tests[i];
info(`Test ${i}: ${test.title}`);
iframe.src = "about:blank";
if (!test.base) {
test.base = "default";
}
iframe.src = `file_getMatchingCSSRules-${test.base}.html`;
await new Promise(resolve => { iframe.onload = resolve; });
try {
await test.run(iframe.contentDocument, iframe.contentWindow);
} catch (e) {
ok(false, "JavaScript error: " + e);
}
}
});
</script>
</pre>
</body>
</html>
|