michael@0: /* vim: set ft=javascript ts=2 et sw=2 tw=80: */ michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: "use strict"; michael@0: michael@0: // Tests that CSS specificity is properly calculated. michael@0: michael@0: const DOMUtils = Cc["@mozilla.org/inspector/dom-utils;1"] michael@0: .getService(Ci.inIDOMUtils); michael@0: const TEST_DATA = [ michael@0: {text: "*", expected: 0}, michael@0: {text: "LI", expected: 1}, michael@0: {text: "UL LI", expected: 2}, michael@0: {text: "UL OL + LI", expected: 3}, michael@0: {text: "H1 + [REL=\"up\"]", expected: 257}, michael@0: {text: "UL OL LI.red", expected: 259}, michael@0: {text: "LI.red.level", expected: 513}, michael@0: {text: ".red .level", expected: 512}, michael@0: {text: "#x34y", expected: 65536}, michael@0: {text: "#s12:not(FOO)", expected: 65537}, michael@0: {text: "body#home div#warning p.message", expected: 131331}, michael@0: {text: "* body#home div#warning p.message", expected: 131331}, michael@0: {text: "#footer :not(nav) li", expected: 65538}, michael@0: {text: "bar:nth-child(n)", expected: 257}, michael@0: {text: "li::-moz-list-number", expected: 1}, michael@0: {text: "a:hover", expected: 257} michael@0: ]; michael@0: michael@0: let test = asyncTest(function*() { michael@0: yield addTab("data:text/html,Computed view specificity test"); michael@0: createDocument(); michael@0: michael@0: info("Creating a CssLogic instance"); michael@0: let cssLogic = new CssLogic(); michael@0: cssLogic.highlight(content.document.body); michael@0: let cssSheet = cssLogic.sheets[0]; michael@0: let cssRule = cssSheet.domSheet.cssRules[0]; michael@0: let selectors = CssLogic.getSelectors(cssRule); michael@0: michael@0: info("Iterating over the test selectors") michael@0: for (let i = 0; i < selectors.length; i++) { michael@0: let selectorText = selectors[i]; michael@0: info("Testing selector " + selectorText); michael@0: michael@0: let selector = new CssSelector(cssRule, selectorText, i); michael@0: let expected = getExpectedSpecificity(selectorText); michael@0: let specificity = DOMUtils.getSpecificity(selector.cssRule, michael@0: selector.selectorIndex) michael@0: is(specificity, expected, michael@0: 'Selector "' + selectorText + '" has a specificity of ' + expected); michael@0: } michael@0: }); michael@0: michael@0: function createDocument() { michael@0: let doc = content.document; michael@0: doc.body.innerHTML = getStylesheetText(); michael@0: doc.title = "Computed view specificity test"; michael@0: } michael@0: michael@0: function getStylesheetText() { michael@0: info("Creating the test stylesheet"); michael@0: let text = TEST_DATA.map(i=>i.text).join(","); michael@0: return '"; michael@0: } michael@0: michael@0: function getExpectedSpecificity(selectorText) { michael@0: return TEST_DATA.filter(i=>i.text === selectorText)[0].expected; michael@0: }