summaryrefslogtreecommitdiffstats
path: root/devtools/client/inspector/rules/test/browser_rules_variables_02.js
blob: 4100859fb97c04344415723c4a277518868077c1 (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
/* Any copyright is dedicated to the Public Domain.
 http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Test for variables in rule view.

const TEST_URI = URL_ROOT + "doc_variables_2.html";

add_task(async function () {
  await addTab(TEST_URI);
  const { inspector, view } = await openRuleView();

  await testBasic(inspector, view);
  await testNestedCssFunctions(inspector, view);
  await testBorderShorthandAndInheritance(inspector, view);
  await testSingleLevelVariable(inspector, view);
  await testDoubleLevelVariable(inspector, view);
  await testTripleLevelVariable(inspector, view);
});

async function testBasic(inspector, view) {
  info(
    "Test support for basic variable functionality for var() with 2 variables." +
      "Format: var(--var1, var(--var2))"
  );

  await selectNode("#a", inspector);
  const unsetVar = getRuleViewProperty(
    view,
    "#a",
    "font-size"
  ).valueSpan.querySelector(".ruleview-unmatched-variable");
  const setVarParent = unsetVar.nextElementSibling;
  const setVar = getVarFromParent(setVarParent);
  is(
    unsetVar.textContent,
    "--var-not-defined",
    "--var-not-defined is not set correctly"
  );
  is(
    unsetVar.dataset.variable,
    "--var-not-defined is not set",
    "--var-not-defined's dataset.variable is not set correctly"
  );
  is(
    setVarParent.textContent,
    " var(--var-defined-font-size)",
    "var(--var-defined-font-size) parsed incorrectly"
  );
  is(
    setVar.textContent,
    "--var-defined-font-size",
    "--var-defined-font-size is not set correctly"
  );
  is(
    setVar.dataset.variable,
    "--var-defined-font-size = 60px",
    "--bg's dataset.variable is not set correctly"
  );
}

async function testNestedCssFunctions(inspector, view) {
  info(
    "Test support for variable functionality for a var() nested inside " +
      "another CSS function. Format: rgb(0, 0, var(--var1, var(--var2)))"
  );

  await selectNode("#b", inspector);
  const unsetVarParent = getRuleViewProperty(
    view,
    "#b",
    "color"
  ).valueSpan.querySelector(".ruleview-unmatched-variable");
  const unsetVar = getVarFromParent(unsetVarParent);
  const setVar = unsetVarParent.previousElementSibling;
  is(
    unsetVarParent.textContent,
    " var(--var-defined-r-2)",
    "var(--var-defined-r-2) not parsed correctly"
  );
  is(
    unsetVar.textContent,
    "--var-defined-r-2",
    "--var-defined-r-2 is not set correctly"
  );
  is(
    unsetVar.dataset.variable,
    "--var-defined-r-2 = 0",
    "--var-defined-r-2's dataset.variable is not set correctly"
  );
  is(
    setVar.textContent,
    "--var-defined-r-1",
    "--var-defined-r-1 is not set correctly"
  );
  is(
    setVar.dataset.variable,
    "--var-defined-r-1 = 255",
    "--var-defined-r-1's dataset.variable is not set correctly"
  );
}

async function testBorderShorthandAndInheritance(inspector, view) {
  info(
    "Test support for variable functionality for shorthands/CSS styles with spaces " +
      'like "margin: w x y z". Also tests functionality for inherticance of CSS' +
      " variables. Format: var(l, var(m)) var(x) rgb(var(r) var(g) var(b))"
  );

  await selectNode("#c", inspector);
  const unsetVarL = getRuleViewProperty(
    view,
    "#c",
    "border"
  ).valueSpan.querySelector(".ruleview-unmatched-variable");
  const setVarMParent = unsetVarL.nextElementSibling;

  // var(x) is the next sibling of the parent of M
  const setVarXParent = setVarMParent.parentNode.nextElementSibling;

  // var(r) is the next sibling of var(x), and var(g) is the next sibling of var(r), etc.
  const setVarRParent = setVarXParent.nextElementSibling;
  const setVarGParent = setVarRParent.nextElementSibling;
  const setVarBParent = setVarGParent.nextElementSibling;

  const setVarM = getVarFromParent(setVarMParent);
  const setVarX = setVarXParent.firstElementChild;
  const setVarR = setVarRParent.firstElementChild;
  const setVarG = setVarGParent.firstElementChild;
  const setVarB = setVarBParent.firstElementChild;

  is(
    unsetVarL.textContent,
    "--var-undefined",
    "--var-undefined is not set correctly"
  );
  is(
    unsetVarL.dataset.variable,
    "--var-undefined is not set",
    "--var-undefined's dataset.variable is not set correctly"
  );

  is(
    setVarM.textContent,
    "--var-border-px",
    "--var-border-px is not set correctly"
  );
  is(
    setVarM.dataset.variable,
    "--var-border-px = 10px",
    "--var-border-px's dataset.variable is not set correctly"
  );

  is(
    setVarX.textContent,
    "--var-border-style",
    "--var-border-style is not set correctly"
  );
  is(
    setVarX.dataset.variable,
    "--var-border-style = solid",
    "var-border-style's dataset.variable is not set correctly"
  );

  is(
    setVarR.textContent,
    "--var-border-r",
    "--var-defined-r is not set correctly"
  );
  is(
    setVarR.dataset.variable,
    "--var-border-r = 255",
    "--var-defined-r's dataset.variable is not set correctly"
  );

  is(
    setVarG.textContent,
    "--var-border-g",
    "--var-defined-g is not set correctly"
  );
  is(
    setVarG.dataset.variable,
    "--var-border-g = 0",
    "--var-defined-g's dataset.variable is not set correctly"
  );

  is(
    setVarB.textContent,
    "--var-border-b",
    "--var-defined-b is not set correctly"
  );
  is(
    setVarB.dataset.variable,
    "--var-border-b = 0",
    "--var-defined-b's dataset.variable is not set correctly"
  );
}

async function testSingleLevelVariable(inspector, view) {
  info(
    "Test support for variable functionality of a single level of " +
      "undefined variables. Format: var(x, constant)"
  );

  await selectNode("#d", inspector);
  const unsetVar = getRuleViewProperty(
    view,
    "#d",
    "font-size"
  ).valueSpan.querySelector(".ruleview-unmatched-variable");

  is(
    unsetVar.textContent,
    "--var-undefined",
    "--var-undefined is not set correctly"
  );
  is(
    unsetVar.dataset.variable,
    "--var-undefined is not set",
    "--var-undefined's dataset.variable is not set correctly"
  );
}

async function testDoubleLevelVariable(inspector, view) {
  info(
    "Test support for variable functionality of double level of " +
      "undefined variables. Format: var(x, var(y, constant))"
  );

  await selectNode("#e", inspector);
  const allUnsetVars = getRuleViewProperty(
    view,
    "#e",
    "color"
  ).valueSpan.querySelectorAll(".ruleview-unmatched-variable");

  is(allUnsetVars.length, 2, "The number of unset variables is mismatched.");

  const unsetVar1 = allUnsetVars[0];
  const unsetVar2 = allUnsetVars[1];

  is(
    unsetVar1.textContent,
    "--var-undefined",
    "--var-undefined is not set correctly"
  );
  is(
    unsetVar1.dataset.variable,
    "--var-undefined is not set",
    "--var-undefined's dataset.variable is not set correctly"
  );

  is(
    unsetVar2.textContent,
    "--var-undefined-2",
    "--var-undefined is not set correctly"
  );
  is(
    unsetVar2.dataset.variable,
    "--var-undefined-2 is not set",
    "--var-undefined-2's dataset.variable is not set correctly"
  );
}

async function testTripleLevelVariable(inspector, view) {
  info(
    "Test support for variable functionality of triple level of " +
      "undefined variables. Format: var(x, var(y, var(z, constant)))"
  );

  await selectNode("#f", inspector);
  const allUnsetVars = getRuleViewProperty(
    view,
    "#f",
    "border-style"
  ).valueSpan.querySelectorAll(".ruleview-unmatched-variable");

  is(allUnsetVars.length, 3, "The number of unset variables is mismatched.");

  const unsetVar1 = allUnsetVars[0];
  const unsetVar2 = allUnsetVars[1];
  const unsetVar3 = allUnsetVars[2];

  is(
    unsetVar1.textContent,
    "--var-undefined",
    "--var-undefined is not set correctly"
  );
  is(
    unsetVar1.dataset.variable,
    "--var-undefined is not set",
    "--var-undefined's dataset.variable is not set correctly"
  );

  is(
    unsetVar2.textContent,
    "--var-undefined-2",
    "--var-undefined-2 is not set correctly"
  );
  is(
    unsetVar2.dataset.variable,
    "--var-undefined-2 is not set",
    "--var-defined-r-2's dataset.variable is not set correctly"
  );

  is(
    unsetVar3.textContent,
    "--var-undefined-3",
    "--var-undefined-3 is not set correctly"
  );
  is(
    unsetVar3.dataset.variable,
    "--var-undefined-3 is not set",
    "--var-defined-r-3's dataset.variable is not set correctly"
  );
}

function getVarFromParent(varParent) {
  return varParent.firstElementChild.firstElementChild;
}