summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/css/css-properties-values-api/at-property.html
blob: 950d9b02d7b7d336262ec82a36a85c32635d47e3 (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
<!DOCTYPE html>
<link rel="help" href="https://drafts.css-houdini.org/css-properties-values-api-1/#at-property-rule">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="./resources/utils.js"></script>
<div id="outer">
  <div id="target"></div>
</div>
<script>

// Parsing:

let uppercase_first = (x) => x.charAt(0).toUpperCase() + x.slice(1);
let to_camel_case = (x) => x.split('-')[0] + x.split('-').slice(1).map(uppercase_first).join('');

function get_cssom_descriptor_value(rule, descriptor) {
  switch (descriptor) {
    case 'syntax':
      return rule.syntax;
    case 'inherits':
      return rule.inherits;
    case 'initial-value':
      return rule.initialValue;
    default:
      assert_true(false, 'Should not reach here');
      return null;
  }
}

// Test that for the given descriptor (e.g. 'syntax'), the specified value
// will yield the expected_value when observed using CSSOM. If the expected_value
// is omitted, it is the same as the specified value.
function test_descriptor(descriptor, specified_value, expected_value, other_descriptors) {
  // Try and build a valid @property form the specified descriptor.
  let at_property = { [to_camel_case(descriptor)]: specified_value };

  // If extra values are specified in other_descriptors, just use them.
  if (typeof(other_descriptors) !== 'unspecified') {
    for (let name in other_descriptors) {
      if (other_descriptors.hasOwnProperty(name)) {
        if (name == descriptor) {
          throw `Unexpected ${name} in other_descriptors`;
        }
        at_property[to_camel_case(name)] = other_descriptors[name];
      }
    }
  }

  if (!('syntax' in at_property)) {
    // The syntax descriptor is required. Use the universal one as a fallback.
    // https://drafts.css-houdini.org/css-properties-values-api-1/#the-syntax-descriptor
    at_property.syntax = '"*"';
  }
  if (!('inherits' in at_property)) {
    // The inherits descriptor is required. Make it true as a fallback.
    // https://drafts.css-houdini.org/css-properties-values-api-1/#inherits-descriptor
    at_property.inherits = true;
  }
  if (!at_property.syntax.match(/^"\s*\*\s*"$/) &&
      !('initialValue' in at_property)) {
    // The initial-value is required for non-universal syntax.
    // Pick a computationally independent value that follows specified syntax.
    // https://drafts.css-houdini.org/css-properties-values-api-1/#the-syntax-descriptor
    at_property.initialValue = (() => {
      let first_syntax_component = specified_value
        .replace(/^"(.*)"$/, '$1') // unquote
        .replace(/[\s\uFEFF\xA0]+/g, ' ') // collapse whitespaces
        .match(/^[^|\#\+]*/)[0] // pick first component
        .trim();
       switch (first_syntax_component) {
         case '<color>': return 'blue';
         case '<length>': return '42px';
         default:
           if (first_syntax_component.startsWith('<')) {
             throw `Unsupported data type name '${first_syntax_component}'`;
           }
           return first_syntax_component; // <custom-ident>
       }
    })();
  }

  if (expected_value === null) {
    test_with_at_property(at_property, (name, rule) => {
      assert_true(!rule);
    }, `Attribute '${descriptor}' makes the @property rule invalid for [${specified_value}]`);
  } else {
    if (typeof(expected_value) === 'undefined')
      expected_value = specified_value;
    test_with_at_property(at_property, (name, rule) => {
      assert_equals(get_cssom_descriptor_value(rule, descriptor), expected_value);
    }, `Attribute '${descriptor}' returns expected value for [${specified_value}]`);
  }
}

// syntax
test_descriptor('syntax', '"<color>"', '<color>');
test_descriptor('syntax', '"<color> | none"', '<color> | none');
test_descriptor('syntax', '"<color># | <image> | none"', '<color># | <image> | none');
test_descriptor('syntax', '"foo | <length>#"', 'foo | <length>#');
test_descriptor('syntax', '"foo | bar | baz"', 'foo | bar | baz');
test_descriptor('syntax', '"notasyntax"', 'notasyntax');

// syntax: universal
for (const syntax of ["*", " * ", "* ", "\t*\t"]) {
  test_descriptor('syntax', `"${syntax}"`, syntax);
}

// syntax: <color> value
test_descriptor('syntax', '"red"', "red"); // treated as <custom-ident>.
test_descriptor('syntax', '"rgb(255, 0, 0)"', null);

// syntax: missing quotes
test_descriptor('syntax', '<color>', null);
test_descriptor('syntax', 'foo | bar', null);

// syntax: invalid <custom-ident>
// https://drafts.csswg.org/css-values-4/#custom-idents
for (const syntax of
  ["default",
   "initial",
   "inherit",
   "unset",
   "revert",
   "revert-layer",
  ]) {
  test_descriptor('syntax', `"${syntax}"`, null);
  test_descriptor('syntax', `"${uppercase_first(syntax)}"`, null);
}

// syntax: pipe between components
test_descriptor('syntax', '"foo bar"', null, {'initial-value': 'foo bar'});
test_descriptor('syntax', '"Foo <length>"', null, {'initial-value': 'Foo 42px'});
test_descriptor('syntax', '"foo, bar"', null, {'initial-value': 'foo, bar'});
test_descriptor('syntax', '"<length> <percentage>"', null, {'initial-value': '42px 100%'});

// syntax: leading bar
test_descriptor('syntax', '"|<length>"', null, {'initial-value': '42px'});

// initial-value
test_descriptor('initial-value', '10px');
test_descriptor('initial-value', 'rgb(1, 2, 3)');
test_descriptor('initial-value', 'red');
test_descriptor('initial-value', 'foo');
test_descriptor('initial-value', 'if(){}');

// initial-value: not computationally independent
test_descriptor('initial-value', '3em', null, {'syntax': '"<length>"'});
test_descriptor('initial-value', 'var(--x)', null);

// inherits
test_descriptor('inherits', 'true', true);
test_descriptor('inherits', 'false', false);

test_descriptor('inherits', 'none', null);
test_descriptor('inherits', '0', null);
test_descriptor('inherits', '1', null);
test_descriptor('inherits', '"true"', null);
test_descriptor('inherits', '"false"', null);
test_descriptor('inherits', 'calc(0)', null);

test_with_style_node('@property foo { }', (node) => {
  assert_equals(node.sheet.rules.length, 0);
}, 'Invalid property name does not parse [foo]');

test_with_style_node('@property -foo { }', (node) => {
  assert_equals(node.sheet.rules.length, 0);
}, 'Invalid property name does not parse [-foo]');

// Applying @property rules

function test_applied(syntax, initial, inherits, expected) {
  test_with_at_property({
    syntax: `"${syntax}"`,
    initialValue: initial,
    inherits: inherits
  }, (name, rule) => {
    let actual = getComputedStyle(target).getPropertyValue(name);
    assert_equals(actual, expected);
  }, `Rule applied [${syntax}, ${initial}, ${inherits}]`);
}

function test_not_applied(syntax, initial, inherits) {
  test_with_at_property({
    syntax: `"${syntax}"`,
    initialValue: initial,
    inherits: inherits
  }, (name, rule) => {
    let actual = getComputedStyle(target).getPropertyValue(name);
    assert_equals(actual, '');
  }, `Rule not applied [${syntax}, ${initial}, ${inherits}]`);
}

// syntax, initialValue, inherits, expected
test_applied('*', 'if(){}', false, 'if(){}');
test_applied('<angle>', '42deg', false, '42deg');
test_applied('<angle>', '1turn', false, '360deg');
test_applied('<color>', 'green', false, 'rgb(0, 128, 0)');
test_applied('<color>', 'rgb(1, 2, 3)', false, 'rgb(1, 2, 3)');
test_applied('<image>', 'url("http://a/")', false, 'url("http://a/")');
test_applied('<integer>', '5', false, '5');
test_applied('<length-percentage>', '10px', false, '10px');
test_applied('<length-percentage>', '10%', false, '10%');
test_applied('<length-percentage>', 'calc(10% + 10px)', false, 'calc(10% + 10px)');
test_applied('<length>', '10px', false, '10px');
test_applied('<number>', '2.5', false, '2.5');
test_applied('<percentage>', '10%', false, '10%');
test_applied('<resolution>', '50dppx', false, '50dppx');
test_applied('<resolution>', '96dpi', false, '1dppx');
test_applied('<time>', '10s', false, '10s');
test_applied('<time>', '1000ms', false, '1s');
test_applied('<transform-function>', 'rotateX(0deg)', false, 'rotateX(0deg)');
test_applied('<transform-list>', 'rotateX(0deg)', false, 'rotateX(0deg)');
test_applied('<transform-list>', 'rotateX(0deg) translateX(10px)', false, 'rotateX(0deg) translateX(10px)');
test_applied('<url>', 'url("http://a/")', false, 'url("http://a/")');

// inherits: true/false
test_applied('<color>', 'tomato', false, 'rgb(255, 99, 71)');
test_applied('<color>', 'tomato', true, 'rgb(255, 99, 71)');

test_with_at_property({ syntax: '"*"', inherits: true }, (name, rule) => {
  try {
    outer.style.setProperty(name, 'foo');
    let actual = getComputedStyle(target).getPropertyValue(name);
    assert_equals(actual, 'foo');
  } finally {
    outer.style = '';
  }
}, 'Rule applied for "*", even with no initial value');

test_not_applied(undefined, 'green', false);
test_not_applied('<color>', undefined, false);
test_not_applied('<color>', 'green', undefined);
test_not_applied('<gandalf>', 'grey', false);
test_not_applied('gandalf', 'grey', false);
test_not_applied('<color>', 'notacolor', false);
test_not_applied('<length>', '10em', false);

test_not_applied('<transform-function>', 'translateX(1em)', false);
test_not_applied('<transform-function>', 'translateY(1lh)', false);
test_not_applied('<transform-list>', 'rotate(10deg) translateX(1em)', false);
test_not_applied('<transform-list>', 'rotate(10deg) translateY(1lh)', false);

// Inheritance

test_with_at_property({
  syntax: '"<length>"',
  inherits: false,
  initialValue: '0px'
}, (name, rule) => {
  try {
    outer.style = `${name}: 40px`;
    assert_equals(getComputedStyle(outer).getPropertyValue(name), '40px');
    assert_equals(getComputedStyle(target).getPropertyValue(name), '0px');
  } finally {
    outer.style = '';
  }
}, 'Non-inherited properties do not inherit');

test_with_at_property({
  syntax: '"<length>"',
  inherits: true,
  initialValue: '0px'
}, (name, rule) => {
  try {
    outer.style = `${name}: 40px`;
    assert_equals(getComputedStyle(outer).getPropertyValue(name), '40px');
    assert_equals(getComputedStyle(target).getPropertyValue(name), '40px');
  } finally {
    outer.style = '';
  }
}, 'Inherited properties inherit');

// Initial values

test_with_at_property({
  syntax: '"<color>"',
  inherits: true,
  initialValue: 'green'
}, (name, rule) => {
  try {
    target.style = `--x:var(${name})`;
    assert_equals(getComputedStyle(target).getPropertyValue(name), 'rgb(0, 128, 0)');
  } finally {
    target.style = '';
  }
}, 'Initial values substituted as computed value');

test_with_at_property({
  syntax: '"<length>"',
  inherits: false,
  initialValue: undefined
}, (name, rule) => {
  try {
    target.style = `${name}: calc(1px + 1px);`;
    assert_equals(getComputedStyle(target).getPropertyValue(name), 'calc(1px + 1px)');
  } finally {
    target.style = '';
  }
}, 'Non-universal registration are invalid without an initial value');

test_with_at_property({
  syntax: '"*"',
  inherits: false,
  initialValue: undefined
}, (name, rule) => {
  try {
    // If the registration suceeded, ${name} does *not* inherit, and hence
    // the computed value on 'target' should be empty.
    outer.style = `${name}: calc(1px + 1px);`;
    assert_equals(getComputedStyle(target).getPropertyValue(name), '');
  } finally {
    outer.style = '';
  }
}, 'Initial value may be omitted for universal registration');

</script>