summaryrefslogtreecommitdiffstats
path: root/devtools/client/inspector/shared/test/browser_styleinspector_output-parser.js
blob: 1aa2879ee235e4894ef1f9e00a84e1d03152e6b2 (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/* Any copyright is dedicated to the Public Domain.
 http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Test expected outputs of the output-parser's parseCssProperty function.

// This is more of a unit test than a mochitest-browser test, but can't be
// tested with an xpcshell test as the output-parser requires the DOM to work.

const OutputParser = require("resource://devtools/client/shared/output-parser.js");

const COLOR_CLASS = "color-class";
const URL_CLASS = "url-class";
const CUBIC_BEZIER_CLASS = "bezier-class";
const ANGLE_CLASS = "angle-class";
const LINEAR_EASING_CLASS = "linear-easing-class";

const TEST_DATA = [
  {
    name: "width",
    value: "100%",
    test: fragment => {
      is(countAll(fragment), 0);
      is(fragment.textContent, "100%");
    },
  },
  {
    name: "width",
    value: "blue",
    test: fragment => {
      is(countAll(fragment), 0);
    },
  },
  {
    name: "content",
    value: "'red url(test.png) repeat top left'",
    test: fragment => {
      is(countAll(fragment), 0);
    },
  },
  {
    name: "content",
    value: '"blue"',
    test: fragment => {
      is(countAll(fragment), 0);
    },
  },
  {
    name: "margin-left",
    value: "url(something.jpg)",
    test: fragment => {
      is(countAll(fragment), 0);
    },
  },
  {
    name: "background-color",
    value: "transparent",
    test: fragment => {
      is(countAll(fragment), 2);
      is(countColors(fragment), 1);
      is(fragment.textContent, "transparent");
    },
  },
  {
    name: "color",
    value: "red",
    test: fragment => {
      is(countColors(fragment), 1);
      is(fragment.textContent, "red");
    },
  },
  {
    name: "color",
    value: "#F06",
    test: fragment => {
      is(countColors(fragment), 1);
      is(fragment.textContent, "#F06");
    },
  },
  {
    name: "border",
    value: "80em dotted pink",
    test: fragment => {
      is(countAll(fragment), 2);
      is(countColors(fragment), 1);
      is(getColor(fragment), "pink");
    },
  },
  {
    name: "color",
    value: "red !important",
    test: fragment => {
      is(countColors(fragment), 1);
      is(fragment.textContent, "red !important");
    },
  },
  {
    name: "background",
    value: "red url(test.png) repeat top left",
    test: fragment => {
      is(countColors(fragment), 1);
      is(countUrls(fragment), 1);
      is(getColor(fragment), "red");
      is(getUrl(fragment), "test.png");
      is(countAll(fragment), 3);
    },
  },
  {
    name: "background",
    value: "blue url(test.png) repeat top left !important",
    test: fragment => {
      is(countColors(fragment), 1);
      is(countUrls(fragment), 1);
      is(getColor(fragment), "blue");
      is(getUrl(fragment), "test.png");
      is(countAll(fragment), 3);
    },
  },
  {
    name: "list-style-image",
    value: 'url("images/arrow.gif")',
    test: fragment => {
      is(countAll(fragment), 1);
      is(getUrl(fragment), "images/arrow.gif");
    },
  },
  {
    name: "list-style-image",
    value: 'url("images/arrow.gif")!important',
    test: fragment => {
      is(countAll(fragment), 1);
      is(getUrl(fragment), "images/arrow.gif");
      is(fragment.textContent, 'url("images/arrow.gif")!important');
    },
  },
  {
    name: "background",
    value:
      "linear-gradient(to right, rgba(183,222,237,1) 0%, " +
      "rgba(33,180,226,1) 30%, rgba(31,170,217,.5) 44%, " +
      "#F06 75%, red 100%)",
    test: fragment => {
      is(countAll(fragment), 10);
      const allSwatches = fragment.querySelectorAll("." + COLOR_CLASS);
      is(allSwatches.length, 5);
      is(allSwatches[0].textContent, "rgba(183,222,237,1)");
      is(allSwatches[1].textContent, "rgba(33,180,226,1)");
      is(allSwatches[2].textContent, "rgba(31,170,217,.5)");
      is(allSwatches[3].textContent, "#F06");
      is(allSwatches[4].textContent, "red");
    },
  },
  {
    name: "background",
    value:
      "radial-gradient(circle closest-side at center, orange 0%, red 100%)",
    test: fragment => {
      is(countAll(fragment), 4);
      const colorSwatches = fragment.querySelectorAll("." + COLOR_CLASS);
      is(colorSwatches.length, 2);
      is(colorSwatches[0].textContent, "orange");
      is(colorSwatches[1].textContent, "red");
    },
  },
  {
    name: "background",
    value: "white  url(http://test.com/wow_such_image.png) no-repeat top left",
    test: fragment => {
      is(countAll(fragment), 3);
      is(countUrls(fragment), 1);
      is(countColors(fragment), 1);
    },
  },
  {
    name: "background",
    value:
      'url("http://test.com/wow_such_(oh-noes)image.png?testid=1&color=red#w00t")',
    test: fragment => {
      is(countAll(fragment), 1);
      is(
        getUrl(fragment),
        "http://test.com/wow_such_(oh-noes)image.png?testid=1&color=red#w00t"
      );
    },
  },
  {
    name: "background-image",
    value: "url(this-is-an-incredible-image.jpeg)",
    test: fragment => {
      is(countAll(fragment), 1);
      is(getUrl(fragment), "this-is-an-incredible-image.jpeg");
    },
  },
  {
    name: "background",
    value:
      'red url(    "http://wow.com/cool/../../../you\'re(doingit)wrong"   ) repeat center',
    test: fragment => {
      is(countAll(fragment), 3);
      is(countColors(fragment), 1);
      is(getUrl(fragment), "http://wow.com/cool/../../../you're(doingit)wrong");
    },
  },
  {
    name: "background-image",
    value:
      "url(../../../look/at/this/folder/structure/../" +
      "../red.blue.green.svg   )",
    test: fragment => {
      is(countAll(fragment), 1);
      is(
        getUrl(fragment),
        "../../../look/at/this/folder/structure/../" + "../red.blue.green.svg"
      );
    },
  },
  {
    name: "transition-timing-function",
    value: "linear",
    test: fragment => {
      is(countCubicBeziers(fragment), 1);
      is(getCubicBezier(fragment), "linear");
    },
  },
  {
    name: "animation-timing-function",
    value: "ease-in-out",
    test: fragment => {
      is(countCubicBeziers(fragment), 1);
      is(getCubicBezier(fragment), "ease-in-out");
    },
  },
  {
    name: "animation-timing-function",
    value: "cubic-bezier(.1, 0.55, .9, -3.45)",
    test: fragment => {
      is(countCubicBeziers(fragment), 1);
      is(getCubicBezier(fragment), "cubic-bezier(.1, 0.55, .9, -3.45)");
    },
  },
  {
    name: "animation-timing-function",
    value: "CUBIC-BEZIER(.1, 0.55, .9, -3.45)",
    test: fragment => {
      is(countCubicBeziers(fragment), 1);
      is(getCubicBezier(fragment), "CUBIC-BEZIER(.1, 0.55, .9, -3.45)");
    },
  },
  {
    name: "animation",
    value: "move 3s cubic-bezier(.1, 0.55, .9, -3.45)",
    test: fragment => {
      is(countCubicBeziers(fragment), 1);
      is(getCubicBezier(fragment), "cubic-bezier(.1, 0.55, .9, -3.45)");
    },
  },
  {
    name: "transition",
    value: "top 1s ease-in",
    test: fragment => {
      is(countCubicBeziers(fragment), 1);
      is(getCubicBezier(fragment), "ease-in");
    },
  },
  {
    name: "animation-timing-function",
    value: "linear(0, 1 50% 100%)",
    test: fragment => {
      is(countLinears(fragment), 1);
      is(getLinear(fragment), "linear(0, 1 50% 100%)");
    },
  },
  {
    name: "animation-timing-function",
    value: "LINEAR(0, 1 50% 100%)",
    test: fragment => {
      is(countLinears(fragment), 1);
      is(getLinear(fragment), "LINEAR(0, 1 50% 100%)");
    },
  },
  {
    name: "transition",
    value: "top 3s steps(4, end)",
    test: fragment => {
      is(countAll(fragment), 0);
    },
  },
  {
    name: "transition",
    value: "top 3s step-start",
    test: fragment => {
      is(countAll(fragment), 0);
    },
  },
  {
    name: "transition",
    value: "top 3s step-end",
    test: fragment => {
      is(countAll(fragment), 0);
    },
  },
  {
    name: "background",
    value: "rgb(255, var(--g-value), 192)",
    test: fragment => {
      is(fragment.textContent, "rgb(255, var(--g-value), 192)");
    },
  },
  {
    name: "background",
    value: "rgb(255, var(--g-value, 0), 192)",
    test: fragment => {
      is(fragment.textContent, "rgb(255, var(--g-value, 0), 192)");
    },
  },
  {
    name: "--url",
    value: "url(())",
    test: fragment => {
      is(countAll(fragment), 0);
      is(fragment.textContent, "url(())");
    },
  },
];

add_task(async function () {
  const cssProperties = getClientCssProperties();
  const parser = new OutputParser(document, cssProperties);
  for (let i = 0; i < TEST_DATA.length; i++) {
    const data = TEST_DATA[i];
    info(
      "Output-parser test data " +
        i +
        ". {" +
        data.name +
        " : " +
        data.value +
        ";}"
    );
    data.test(
      parser.parseCssProperty(data.name, data.value, {
        colorClass: COLOR_CLASS,
        urlClass: URL_CLASS,
        bezierClass: CUBIC_BEZIER_CLASS,
        angleClass: ANGLE_CLASS,
        linearEasingClass: LINEAR_EASING_CLASS,
      })
    );
  }
});

function countAll(fragment) {
  return fragment.querySelectorAll("*").length;
}
function countColors(fragment) {
  return fragment.querySelectorAll("." + COLOR_CLASS).length;
}
function countUrls(fragment) {
  return fragment.querySelectorAll("." + URL_CLASS).length;
}
function countCubicBeziers(fragment) {
  return fragment.querySelectorAll("." + CUBIC_BEZIER_CLASS).length;
}
function countLinears(fragment) {
  return fragment.querySelectorAll("." + LINEAR_EASING_CLASS).length;
}
function getColor(fragment, index) {
  return fragment.querySelectorAll("." + COLOR_CLASS)[index || 0].textContent;
}
function getUrl(fragment, index) {
  return fragment.querySelectorAll("." + URL_CLASS)[index || 0].textContent;
}
function getCubicBezier(fragment, index) {
  return fragment.querySelectorAll("." + CUBIC_BEZIER_CLASS)[index || 0]
    .textContent;
}
function getLinear(fragment, index = 0) {
  return fragment.querySelectorAll("." + LINEAR_EASING_CLASS)[index]
    .textContent;
}