Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* vim: set ft=javascript ts=2 et sw=2 tw=80: */ |
michael@0 | 2 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 3 | http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 4 | |
michael@0 | 5 | "use strict"; |
michael@0 | 6 | |
michael@0 | 7 | // Tests that CSS specificity is properly calculated. |
michael@0 | 8 | |
michael@0 | 9 | const DOMUtils = Cc["@mozilla.org/inspector/dom-utils;1"] |
michael@0 | 10 | .getService(Ci.inIDOMUtils); |
michael@0 | 11 | const TEST_DATA = [ |
michael@0 | 12 | {text: "*", expected: 0}, |
michael@0 | 13 | {text: "LI", expected: 1}, |
michael@0 | 14 | {text: "UL LI", expected: 2}, |
michael@0 | 15 | {text: "UL OL + LI", expected: 3}, |
michael@0 | 16 | {text: "H1 + [REL=\"up\"]", expected: 257}, |
michael@0 | 17 | {text: "UL OL LI.red", expected: 259}, |
michael@0 | 18 | {text: "LI.red.level", expected: 513}, |
michael@0 | 19 | {text: ".red .level", expected: 512}, |
michael@0 | 20 | {text: "#x34y", expected: 65536}, |
michael@0 | 21 | {text: "#s12:not(FOO)", expected: 65537}, |
michael@0 | 22 | {text: "body#home div#warning p.message", expected: 131331}, |
michael@0 | 23 | {text: "* body#home div#warning p.message", expected: 131331}, |
michael@0 | 24 | {text: "#footer :not(nav) li", expected: 65538}, |
michael@0 | 25 | {text: "bar:nth-child(n)", expected: 257}, |
michael@0 | 26 | {text: "li::-moz-list-number", expected: 1}, |
michael@0 | 27 | {text: "a:hover", expected: 257} |
michael@0 | 28 | ]; |
michael@0 | 29 | |
michael@0 | 30 | let test = asyncTest(function*() { |
michael@0 | 31 | yield addTab("data:text/html,Computed view specificity test"); |
michael@0 | 32 | createDocument(); |
michael@0 | 33 | |
michael@0 | 34 | info("Creating a CssLogic instance"); |
michael@0 | 35 | let cssLogic = new CssLogic(); |
michael@0 | 36 | cssLogic.highlight(content.document.body); |
michael@0 | 37 | let cssSheet = cssLogic.sheets[0]; |
michael@0 | 38 | let cssRule = cssSheet.domSheet.cssRules[0]; |
michael@0 | 39 | let selectors = CssLogic.getSelectors(cssRule); |
michael@0 | 40 | |
michael@0 | 41 | info("Iterating over the test selectors") |
michael@0 | 42 | for (let i = 0; i < selectors.length; i++) { |
michael@0 | 43 | let selectorText = selectors[i]; |
michael@0 | 44 | info("Testing selector " + selectorText); |
michael@0 | 45 | |
michael@0 | 46 | let selector = new CssSelector(cssRule, selectorText, i); |
michael@0 | 47 | let expected = getExpectedSpecificity(selectorText); |
michael@0 | 48 | let specificity = DOMUtils.getSpecificity(selector.cssRule, |
michael@0 | 49 | selector.selectorIndex) |
michael@0 | 50 | is(specificity, expected, |
michael@0 | 51 | 'Selector "' + selectorText + '" has a specificity of ' + expected); |
michael@0 | 52 | } |
michael@0 | 53 | }); |
michael@0 | 54 | |
michael@0 | 55 | function createDocument() { |
michael@0 | 56 | let doc = content.document; |
michael@0 | 57 | doc.body.innerHTML = getStylesheetText(); |
michael@0 | 58 | doc.title = "Computed view specificity test"; |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | function getStylesheetText() { |
michael@0 | 62 | info("Creating the test stylesheet"); |
michael@0 | 63 | let text = TEST_DATA.map(i=>i.text).join(","); |
michael@0 | 64 | return '<style type="text/css">' + text + " {color:red;}</style>"; |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | function getExpectedSpecificity(selectorText) { |
michael@0 | 68 | return TEST_DATA.filter(i=>i.text === selectorText)[0].expected; |
michael@0 | 69 | } |