tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_getRuleText.js (4158B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 const {
      7  getRuleText,
      8 } = require("resource://devtools/server/actors/utils/style-utils.js");
      9 
     10 const TEST_DATA = [
     11  {
     12    desc: "Empty input",
     13    input: "",
     14    line: 1,
     15    column: 1,
     16    throws: true,
     17  },
     18  {
     19    desc: "Null input",
     20    input: null,
     21    line: 1,
     22    column: 1,
     23    throws: true,
     24  },
     25  {
     26    desc: "Missing loc",
     27    input: "#id{color:red;background:yellow;}",
     28    throws: true,
     29  },
     30  {
     31    desc: "No opening bracket",
     32    input: "/* hey */",
     33    line: 1,
     34    column: 1,
     35    throws: true,
     36  },
     37  {
     38    desc: "Simplest test case",
     39    input: "#id{color:red;background:yellow;}",
     40    line: 1,
     41    column: 1,
     42    expected: "color:red;background:yellow;",
     43  },
     44  {
     45    desc: "Multiple rules test case",
     46    input:
     47      "#id{color:red;background:yellow;}.class-one .class-two " +
     48      "{ position:absolute; line-height: 45px}",
     49    line: 1,
     50    column: 34,
     51    expected: " position:absolute; line-height: 45px",
     52  },
     53  {
     54    desc: "Unclosed rule",
     55    input: "#id{color:red;background:yellow;",
     56    line: 1,
     57    column: 1,
     58    expected: "color:red;background:yellow;",
     59  },
     60  {
     61    desc: "Multi-lines CSS",
     62    input: [
     63      "/* this is a multi line css */",
     64      "body {",
     65      "  color: green;",
     66      "  background-repeat: no-repeat",
     67      "}",
     68      " /*something else here */",
     69      "* {",
     70      "  color: purple;",
     71      "}       ",
     72    ].join("\n"),
     73    line: 7,
     74    column: 1,
     75    expected: "\n  color: purple;\n",
     76  },
     77  {
     78    desc: "Multi-lines CSS and multi-line rule",
     79    input: [
     80      "/* ",
     81      "* some comments",
     82      "*/",
     83      "",
     84      "body {",
     85      "    margin: 0;",
     86      "    padding: 15px 15px 2px 15px;",
     87      "    color: red;",
     88      "}",
     89      "",
     90      "#header .btn, #header .txt {",
     91      "    font-size: 100%;",
     92      "}",
     93      "",
     94      "#header #information {",
     95      "    color: #dddddd;",
     96      "    font-size: small;",
     97      "}",
     98    ].join("\n"),
     99    line: 5,
    100    column: 1,
    101    expected:
    102      "\n    margin: 0;\n    padding: 15px 15px 2px 15px;\n    color: red;\n",
    103  },
    104  {
    105    desc: "Content string containing a } character",
    106    input: "   #id{border:1px solid red;content: '}';color:red;}",
    107    line: 1,
    108    column: 4,
    109    expected: "border:1px solid red;content: '}';color:red;",
    110  },
    111  {
    112    desc: "Attribute selector containing a { character",
    113    input: `div[data-x="{"]{color: gold}`,
    114    line: 1,
    115    column: 1,
    116    expected: "color: gold",
    117  },
    118  {
    119    desc: "Rule contains no tokens",
    120    input: "div{}",
    121    line: 1,
    122    column: 1,
    123    expected: "",
    124  },
    125  {
    126    desc: "Rule contains invalid declaration",
    127    input: `#id{color;}`,
    128    line: 1,
    129    column: 1,
    130    expected: "color;",
    131  },
    132  {
    133    desc: "Rule contains invalid declaration",
    134    input: `#id{-}`,
    135    line: 1,
    136    column: 1,
    137    expected: "-",
    138  },
    139  {
    140    desc: "Rule contains nested rule",
    141    input: `#id{background: gold; .nested{color:blue;} color: tomato;  }`,
    142    line: 1,
    143    column: 1,
    144    expected: "background: gold; .nested{color:blue;} color: tomato;  ",
    145  },
    146  {
    147    desc: "Rule contains nested rule with invalid declaration",
    148    input: `#id{.nested{color;}}`,
    149    line: 1,
    150    column: 1,
    151    expected: ".nested{color;}",
    152  },
    153  {
    154    desc: "Rule contains unicode chars",
    155    input: `#id /*🙃*/ {content: "☃️";}`,
    156    line: 1,
    157    column: 1,
    158    expected: `content: "☃️";`,
    159  },
    160 ];
    161 
    162 function run_test() {
    163  for (const test of TEST_DATA) {
    164    info("Starting test: " + test.desc);
    165    info("Input string " + test.input);
    166    let output;
    167    try {
    168      output = getRuleText(test.input, test.line, test.column);
    169      if (test.throws) {
    170        info("Test should have thrown");
    171        Assert.ok(false);
    172      }
    173    } catch (e) {
    174      info("getRuleText threw an exception with the given input string");
    175      if (test.throws) {
    176        info("Exception expected");
    177        Assert.ok(true);
    178      } else {
    179        info("Exception unexpected\n" + e);
    180        Assert.ok(false);
    181      }
    182    }
    183    if (output) {
    184      Assert.equal(output, test.expected);
    185    }
    186  }
    187 }