File: at-function-cssom.html

package info (click to toggle)
firefox 143.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,617,328 kB
  • sloc: cpp: 7,478,492; javascript: 6,417,157; ansic: 3,720,058; python: 1,396,372; xml: 627,523; asm: 438,677; java: 186,156; sh: 63,477; makefile: 19,171; objc: 13,059; perl: 12,983; yacc: 4,583; cs: 3,846; pascal: 3,405; lex: 1,720; ruby: 1,003; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 53; csh: 10
file content (329 lines) | stat: -rw-r--r-- 11,173 bytes parent folder | download | duplicates (10)
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
<!DOCTYPE html>
<title>CSS Custom Functions: CSSOM</title>
<link rel="help" href="https://drafts.csswg.org/css-mixins-1/#cssom">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>

<script>

test(t => {
  let sheet = new CSSStyleSheet();
  sheet.replaceSync(`
    @function --foo() {}
  `);
  assert_equals(sheet.cssRules.length, 1);
  let functionRule = sheet.cssRules[0];
  assert_true(functionRule instanceof CSSFunctionRule);
  assert_equals(functionRule.cssRules.length, 0);
}, 'Empty CSSFunctionRule');

test(t => {
  let sheet = new CSSStyleSheet();
  sheet.replaceSync(`
    @function --foo() {
      result: 100px;
    }
  `);
  assert_equals(sheet.cssRules.length, 1);
  let functionRule = sheet.cssRules[0];
  assert_true(functionRule instanceof CSSFunctionRule);
  assert_equals(functionRule.cssRules.length, 1);
  let declarationsRule = functionRule.cssRules[0];
  assert_true(declarationsRule instanceof CSSFunctionDeclarations);
  let descriptors = declarationsRule.style;
  assert_true(descriptors instanceof CSSFunctionDescriptors);
}, 'Single CSSFunctionDeclarations');

test(t => {
  let sheet = new CSSStyleSheet();
  sheet.replaceSync(`
    @function --foo() {
      result: 100px;
    }
  `);
  let descriptors = sheet.cssRules[0]?.cssRules[0]?.style;
  assert_true(descriptors instanceof CSSFunctionDescriptors);
  assert_equals(descriptors.result, '100px');
}, 'CSSFunctionDescriptors (result)');

test(t => {
  let sheet = new CSSStyleSheet();
  sheet.replaceSync(`
    @function --foo() {
      result: 100px;
      result: 101px;
    }
  `);
  let descriptors = sheet.cssRules[0]?.cssRules[0]?.style;
  assert_true(descriptors instanceof CSSFunctionDescriptors);
  assert_equals(descriptors.result, '101px');
}, 'CSSFunctionDescriptors (result, repeated)');

test(t => {
  let sheet = new CSSStyleSheet();
  sheet.replaceSync(`
    @function --foo() {
      --x: 1px;
      --y: 2px;
    }
  `);
  let descriptors = sheet.cssRules[0]?.cssRules[0]?.style;
  assert_true(descriptors instanceof CSSFunctionDescriptors);
  assert_equals(descriptors.getPropertyValue('--x'), '1px');
  assert_equals(descriptors.getPropertyValue('--y'), '2px');
  assert_equals(descriptors.getPropertyValue('--unknown'), '');
  assert_equals(descriptors.result, '');
}, 'CSSFunctionDescriptors (local variables)');

test(t => {
  let sheet = new CSSStyleSheet();
  sheet.replaceSync(`
    @function --foo() {
      --x: 1px;
      --y: 2px;
      --x: 3px;
    }
  `);
  let descriptors = sheet.cssRules[0]?.cssRules[0]?.style;
  assert_true(descriptors instanceof CSSFunctionDescriptors);
  assert_equals(descriptors.getPropertyValue('--x'), '3px');
  assert_equals(descriptors.getPropertyValue('--y'), '2px');
  assert_equals(descriptors.getPropertyValue('--unknown'), '');
  assert_equals(descriptors.result, '');
}, 'CSSFunctionDescriptors (local variables, repeated)');

test(t => {
  let sheet = new CSSStyleSheet();
  sheet.replaceSync(`
    @function --foo() {
      --x: 1px;
      --y: 2px;
      result: 3px;
    }
  `);
  let descriptors = sheet.cssRules[0]?.cssRules[0]?.style;
  assert_true(descriptors instanceof CSSFunctionDescriptors);
  assert_equals(descriptors.getPropertyValue('--x'), '1px');
  assert_equals(descriptors.getPropertyValue('--y'), '2px');
  assert_equals(descriptors.getPropertyValue('--unknown'), '');
  assert_equals(descriptors.result, '3px');
}, 'CSSFunctionDescriptors (local variables and result)');

test(t => {
  let sheet = new CSSStyleSheet();
  sheet.replaceSync(`
    @function --foo() {
      --x: 1px;
      --y: 2px;
      result: 3px;
    }
  `);
  let descriptors = sheet.cssRules[0]?.cssRules[0]?.style;
  assert_true(descriptors instanceof CSSFunctionDescriptors);
  assert_equals(descriptors.cssText, '--x: 1px; --y: 2px; result: 3px;');
}, 'CSSFunctionDescriptors serialization');

test(t => {
  let sheet = new CSSStyleSheet();
  sheet.replaceSync(`
    @function --foo() {
      --x: 1px;
      syntax: "<length>";
      --y: 2px;
      color: red;
      unknown: unknown;
      result: 3px;
    }
  `);
  let descriptors = sheet.cssRules[0]?.cssRules[0]?.style;
  assert_true(descriptors instanceof CSSFunctionDescriptors);
  assert_equals(descriptors.length, 3);
  assert_equals(descriptors.cssText, '--x: 1px; --y: 2px; result: 3px;');
}, 'Unknown descriptors');

test(t => {
  let sheet = new CSSStyleSheet();
  sheet.replaceSync(`
    @function --foo() {
      --x: 1px;
    }
  `);
  let descriptors = sheet.cssRules[0]?.cssRules[0]?.style;
  assert_true(descriptors instanceof CSSFunctionDescriptors);
  assert_equals(descriptors.length, 1);
  descriptors.cssText = '--x: 1px; color: red; result: 3px; --y: 2px;';
  assert_equals(descriptors.length, 3);
  assert_equals(descriptors.cssText, '--x: 1px; result: 3px; --y: 2px;');
}, 'Unknown descriptors (mutation)');

test(t => {
  let sheet = new CSSStyleSheet();
  sheet.replaceSync(`
    @function --foo() {
     --x: 1px;
     result: 2px;
    }
  `);
  let descriptors = sheet.cssRules[0]?.cssRules[0]?.style;
  assert_true(descriptors instanceof CSSFunctionDescriptors);
  assert_equals(descriptors.length, 2);
  assert_equals(descriptors.item(0), '--x');
  assert_equals(descriptors.item(1), 'result');
}, 'item()');

// https://webidl.spec.whatwg.org/#dfn-indexed-property-getter
test(t => {
  let sheet = new CSSStyleSheet();
  sheet.replaceSync(`
    @function --foo() {
     --x: 1px;
     result: 2px;
    }
  `);
  let descriptors = sheet.cssRules[0]?.cssRules[0]?.style;
  assert_true(descriptors instanceof CSSFunctionDescriptors);
  assert_array_equals(Array.from(descriptors), ['--x', 'result']);;
}, 'Indexed property getter');

// Conditional rules

test(t => {
  let sheet = new CSSStyleSheet();
  sheet.replaceSync(`
    @function --foo() {
      @supports (width: 100px) {
        result: 100px;
      }
      result: 200px;
    }
  `);
  assert_equals(sheet.cssRules.length, 1);
  let functionRule = sheet.cssRules[0];
  assert_true(functionRule instanceof CSSFunctionRule);
  assert_equals(functionRule.cssRules.length, 2);
  let supportsRule = functionRule.cssRules[0];
  assert_true(supportsRule instanceof CSSSupportsRule);
  assert_true(supportsRule.cssRules[0] instanceof CSSFunctionDeclarations);
  assert_equals(supportsRule.cssRules[0].style?.result, '100px');

  let declarationsRule = functionRule.cssRules[1];
  assert_true(declarationsRule instanceof CSSFunctionDeclarations);
  let descriptors = declarationsRule.style;
  assert_true(descriptors instanceof CSSFunctionDescriptors);
  assert_equals(descriptors.result, '200px');
}, '@supports in body');

// CSSFunctionRule

test(t => {
  let sheet = new CSSStyleSheet();
  sheet.replaceSync(`
    @function --foo() {}
  `);
  assert_equals(sheet.cssRules.length, 1);
  let functionRule = sheet.cssRules[0];
  assert_true(functionRule instanceof CSSFunctionRule);
  assert_equals(functionRule.name, '--foo');
}, 'CSSFunctionRule.name');

test(t => {
  let sheet = new CSSStyleSheet();
  sheet.replaceSync(`
    @function --f0() {}
    @function --f1(--x) {}
    @function --f2(--x <length>) {}
    @function --f3(--x: 10px) {}
    @function --f4(--x <length>: 10px) {}
    @function --f5(--x type(<length> | auto): 10px, --y, --z: red) {}
  `);
  assert_equals(sheet.cssRules.length, 6);
  assert_object_equals(sheet.cssRules[0].getParameters(), []);
  assert_object_equals(sheet.cssRules[1].getParameters(), [
    {name: '--x', type: '*'},
  ]);
  assert_object_equals(sheet.cssRules[2].getParameters(), [
    {name: '--x', type: '<length>'},
  ]);
  assert_object_equals(sheet.cssRules[3].getParameters(), [
    {name: '--x', type: '*', defaultValue: '10px'},
  ]);
  assert_object_equals(sheet.cssRules[4].getParameters(), [
    {name: '--x', type: '<length>', defaultValue: '10px'},
  ]);
  assert_object_equals(sheet.cssRules[5].getParameters(), [
    {name: '--x', type: '<length> | auto', defaultValue: '10px'},
    {name: '--y', type: '*'},
    {name: '--z', type: '*', defaultValue: 'red'},
  ], '--f5');
}, 'CSSFunctionRule.getParameters()');

test(t => {
  let sheet = new CSSStyleSheet();
  sheet.replaceSync(`
    @function --f0() {}
    @function --f1() returns <length> {}
    @function --f2() returns <length>+ {}
    @function --f3() returns type(*) {}
    @function --f4() returns type(<length> | auto) {}
  `);
  assert_equals(sheet.cssRules.length, 5);
  assert_equals(sheet.cssRules[0]?.returnType, '*');
  assert_equals(sheet.cssRules[1]?.returnType, '<length>');
  assert_equals(sheet.cssRules[2]?.returnType, '<length>+');
  assert_equals(sheet.cssRules[3]?.returnType, '*');
  assert_equals(sheet.cssRules[4]?.returnType, '<length> | auto');
}, 'CSSFunctionRule.returnType');

test(t => {
  let sheet = new CSSStyleSheet();
  // U+0009 CHARACTER TABULATION
  sheet.replaceSync(`@function --escaped-\\9 -tab(--param-\\9 -tab) { --local-\\9 -tab: 1px; }`);
  assert_equals(sheet.cssRules.length, 1);
  let rule = sheet.cssRules[0];
  assert_equals(rule.name, '--escaped-\t-tab');
  assert_equals(rule.getParameters()[0]?.name, '--param-\t-tab');
  assert_equals(rule.cssRules[0].style.getPropertyValue('--local-\t-tab'), '1px');
}, 'CSSFunctionRule escapes');

function test_cssText(actual, expected) {
  expected ??= actual;
  let name = actual.match(/@function (--[-\w]+)/)[1];
  test(t => {
    let sheet = new CSSStyleSheet();
    sheet.replaceSync(actual);
    assert_equals(sheet.cssRules[0]?.cssText, expected);
  }, `CSSFunctionRule.cssText (${name})`);
}

// The function name is used by the test description; please make sure it's
// unique among all test_cssText cases.
test_cssText(`@function --empty() { }`);

test_cssText(`@function --ret-length() returns <length> { }`);
test_cssText(`@function --ret-length-auto() returns type(<length> | auto) { }`);

test_cssText(`@function --param-single(--x) { }`);
test_cssText(`@function --param-typed(--x <length>) { }`);
test_cssText(`@function --param-typed-default(--x <length>: 10px) { }`);
test_cssText(`@function --param-default(--x: 10px) { }`);
test_cssText(`@function --param-multi(--x, --y) { }`);
test_cssText(`@function --param-multi-mixed(--x: 10px, --y, --z <length>) { }`);

test_cssText(`@function --body-result() { result: 10px; }`);
test_cssText(`@function --body-locals() { --x: 1px; --y: 2px; result: 10px; }`);

// Cases that don't round-trip as-is:
test_cssText(`@function --param-type-fn(--x type(<length>)) { }`,
             `@function --param-type-fn(--x <length>) { }`);
test_cssText(`@function --param-type-fn-uni(--x type(*)) { }`,
             `@function --param-type-fn-uni(--x) { }`);
test_cssText(`@function --ret-type-fn() returns type(*) { }`,
             `@function --ret-type-fn() { }`);
test_cssText(`@function --ret-type-fn-uni() returns type(*) { }`,
             `@function --ret-type-fn-uni() { }`);
test_cssText(`@function --body-result-multi() { result: 10px; result: 20px; }`,
             `@function --body-result-multi() { result: 20px; }`);

// Escapes (U+0009 CHARACTER TABULATION):
test_cssText(`@function --escaped-\\9 -tab(--param-\\9 -tab) { --local-\\9 -tab: 1px; }`);
</script>