summaryrefslogtreecommitdiffstats
path: root/devtools/server/tests/xpcshell/test_getRuleText.js
blob: 6e67a5f85bd43bf0953285db716c6b0dc3c6c4f5 (plain)
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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const {
  getRuleText,
} = require("resource://devtools/server/actors/utils/style-utils.js");

const TEST_DATA = [
  {
    desc: "Empty input",
    input: "",
    line: 1,
    column: 1,
    throws: true,
  },
  {
    desc: "Null input",
    input: null,
    line: 1,
    column: 1,
    throws: true,
  },
  {
    desc: "Missing loc",
    input: "#id{color:red;background:yellow;}",
    throws: true,
  },
  {
    desc: "No opening bracket",
    input: "/* hey */",
    line: 1,
    column: 1,
    throws: true,
  },
  {
    desc: "Simplest test case",
    input: "#id{color:red;background:yellow;}",
    line: 1,
    column: 1,
    expected: "color:red;background:yellow;",
  },
  {
    desc: "Multiple rules test case",
    input:
      "#id{color:red;background:yellow;}.class-one .class-two " +
      "{ position:absolute; line-height: 45px}",
    line: 1,
    column: 34,
    expected: " position:absolute; line-height: 45px",
  },
  {
    desc: "Unclosed rule",
    input: "#id{color:red;background:yellow;",
    line: 1,
    column: 1,
    expected: "color:red;background:yellow;",
  },
  {
    desc: "Multi-lines CSS",
    input: [
      "/* this is a multi line css */",
      "body {",
      "  color: green;",
      "  background-repeat: no-repeat",
      "}",
      " /*something else here */",
      "* {",
      "  color: purple;",
      "}       ",
    ].join("\n"),
    line: 7,
    column: 1,
    expected: "\n  color: purple;\n",
  },
  {
    desc: "Multi-lines CSS and multi-line rule",
    input: [
      "/* ",
      "* some comments",
      "*/",
      "",
      "body {",
      "    margin: 0;",
      "    padding: 15px 15px 2px 15px;",
      "    color: red;",
      "}",
      "",
      "#header .btn, #header .txt {",
      "    font-size: 100%;",
      "}",
      "",
      "#header #information {",
      "    color: #dddddd;",
      "    font-size: small;",
      "}",
    ].join("\n"),
    line: 5,
    column: 1,
    expected:
      "\n    margin: 0;\n    padding: 15px 15px 2px 15px;\n    color: red;\n",
  },
  {
    desc: "Content string containing a } character",
    input: "   #id{border:1px solid red;content: '}';color:red;}",
    line: 1,
    column: 4,
    expected: "border:1px solid red;content: '}';color:red;",
  },
  {
    desc: "Attribute selector containing a { character",
    input: `div[data-x="{"]{color: gold}`,
    line: 1,
    column: 1,
    expected: "color: gold",
  },
  {
    desc: "Rule contains no tokens",
    input: "div{}",
    line: 1,
    column: 1,
    expected: "",
  },
  {
    desc: "Rule contains invalid declaration",
    input: `#id{color;}`,
    line: 1,
    column: 1,
    expected: "color;",
  },
  {
    desc: "Rule contains invalid declaration",
    input: `#id{-}`,
    line: 1,
    column: 1,
    expected: "-",
  },
  {
    desc: "Rule contains nested rule",
    input: `#id{background: gold; .nested{color:blue;} color: tomato;  }`,
    line: 1,
    column: 1,
    expected: "background: gold; .nested{color:blue;} color: tomato;  ",
  },
  {
    desc: "Rule contains nested rule with invalid declaration",
    input: `#id{.nested{color;}}`,
    line: 1,
    column: 1,
    expected: ".nested{color;}",
  },
  {
    desc: "Rule contains unicode chars",
    input: `#id /*🙃*/ {content: "☃️";}`,
    line: 1,
    column: 1,
    expected: `content: "☃️";`,
  },
];

function run_test() {
  for (const test of TEST_DATA) {
    info("Starting test: " + test.desc);
    info("Input string " + test.input);
    let output;
    try {
      output = getRuleText(test.input, test.line, test.column);
      if (test.throws) {
        info("Test should have thrown");
        Assert.ok(false);
      }
    } catch (e) {
      info("getRuleText threw an exception with the given input string");
      if (test.throws) {
        info("Exception expected");
        Assert.ok(true);
      } else {
        info("Exception unexpected\n" + e);
        Assert.ok(false);
      }
    }
    if (output) {
      Assert.equal(output, test.expected);
    }
  }
}