browser/devtools/styleinspector/test/browser_styleinspector_csslogic-specificity.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial