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

test_with_at_property({
  syntax: '"<length>"',
  inherits: false,
  initialValue: '1px'
}, (name) => {
  assert_equals(getComputedStyle(div).getPropertyValue(name), '1px');
}, '@property determines the registration when uncontested');

test_with_at_property({
  syntax: '"<length>"',
  inherits: false,
  initialValue: '2px'
}, (name) => {
  CSS.registerProperty({
    name: name,
    syntax: '<color>',
    inherits: false,
    initialValue: 'green'
  });
  assert_equals(getComputedStyle(div).getPropertyValue(name), 'rgb(0, 128, 0)');
}, 'CSS.registerProperty wins over @property');

test_with_at_property({
  syntax: '"<length>"',
  inherits: false,
  initialValue: '3px'
}, (name1) => {
  with_at_property({
    name: name1,
    syntax: '"<integer>"',
    inherits: false,
    initialValue: '6'
  }, (name2) => {
    assert_equals(name1, name2);
    assert_equals(getComputedStyle(div).getPropertyValue(name2), '6');
  });
}, '@property later in document order wins');

test(() => {
  let name = generate_name();

  with_style_node(`
    @property ${name} {
      syntax: "<length>";
      inherits: false;
      initial-value: 4px;
    }

    @property ${name} {
      syntax: "<color>";
      inherits: false;
      initial-value: red;
    }
  `, () => {
    assert_equals(getComputedStyle(div).getPropertyValue(name), 'rgb(255, 0, 0)');
  });
}, '@property later in stylesheet wins');

test(() => {
  let name = generate_name();
  CSS.registerProperty({
    name: name,
    syntax: '<color>',
    inherits: false,
    initialValue: 'green'
  });
  assert_equals(getComputedStyle(div).getPropertyValue(name), 'rgb(0, 128, 0)');
}, 'CSS.registerProperty determines the registration when uncontested');

test(() => {
  let name = generate_name();

  // ${name} is initially not registered, hence has no initial value.
  assert_equals(getComputedStyle(div).getPropertyValue(name), '');

  with_at_property({
    name: name,
    syntax: '"<length>"',
    inherits: false,
    initialValue: '10px'
  }, () => {
    assert_equals(getComputedStyle(div).getPropertyValue(name), '10px');
  });

  // When the style node is removed, ${name} should be unregistered again.
  assert_equals(getComputedStyle(div).getPropertyValue(name), '');
}, '@property registrations are cleared when rule removed');

test(() => {
  let name = generate_name();

  with_style_node(`div { ${name}: calc(1px + 1px); }`, () => {
    // ${name} should be a token sequence at this point.
    assert_equals(getComputedStyle(div).getPropertyValue(name), 'calc(1px + 1px)');

    with_at_property({
      name: name,
      syntax: '"<length>"',
      inherits: false,
      initialValue: '0px'
    }, () => {
      // ${name} is now a <length>, hence the calc() should be simplified.
      assert_equals(getComputedStyle(div).getPropertyValue(name), '2px');
    });

    // ${name} should be a token sequence again.
    assert_equals(getComputedStyle(div).getPropertyValue(name), 'calc(1px + 1px)');
  });
}, 'Computed value becomes token sequence when @property is removed');

test(() => {
  let name = generate_name();

  with_style_node(`#outer { ${name}: 10px; }`, () => {
    assert_equals(getComputedStyle(div).getPropertyValue(name), '10px');

    with_at_property({
      name: name,
      syntax: '"<length>"',
      inherits: false,
      initialValue: '0px'
    }, () => {
      // ${name} is no longer inherited
      assert_equals(getComputedStyle(div).getPropertyValue(name), '0px');
    });

    assert_equals(getComputedStyle(div).getPropertyValue(name), '10px');
  });
}, 'Inherited status is reflected in computed styles when @property is removed');

test(() => {
  let name = generate_name();

  with_style_node(`
    @property ${name} {
      syntax: "<length>";
      inherits: false;
      initial-value: 1px;
    }

    @property ${name} {
      inherits: false;
      initial-value: green;
    }
  `, () => {
    assert_equals(getComputedStyle(div).getPropertyValue(name), '1px');
  });
}, 'Invalid @property rule (missing syntax) does not overwrite previous valid rule');

test(() => {
  let name = generate_name();

  with_style_node(`
    @property ${name} {
      syntax: "<length>";
      inherits: false;
      initial-value: 1px;
    }

    @property ${name} {
      syntax: "<color>";
      initial-value: green;
    }
  `, () => {
    assert_equals(getComputedStyle(div).getPropertyValue(name), '1px');
  });
}, 'Invalid @property rule (missing inherits descriptor) does not overwrite previous valid rule');

test(() => {
  let name = generate_name();

  with_style_node(`
    @property ${name} {
      syntax: "<length>";
      inherits: false;
      initial-value: 1px;
    }

    @property ${name} {
      syntax: "<color>";
      inherits: false;
    }
  `, () => {
    assert_equals(getComputedStyle(div).getPropertyValue(name), '1px');
  });
}, 'Invalid @property rule (missing initial-value) does not overwrite previous valid rule');

test(() => {
  let name = generate_name();

  with_style_node(`
    @property ${name} {
      syntax: "<color>";
      inherits: false;
    }

    @property ${name} {
      syntax: "<length>";
      inherits: false;
      initial-value: 1px;
    }
  `, () => {
    assert_equals(getComputedStyle(div).getPropertyValue(name), '1px');
  });
}, 'Previous invalid rule does not prevent valid rule from causing registration');

test(() => {
  let name = generate_name();

  with_style_node(`
    @property ${name} {
      syntax: "<length>";
      inherits: false;
      initial-value: 1px;
      quite-unknown: 200;
    }
  `, () => {
    assert_equals(getComputedStyle(div).getPropertyValue(name), '1px');
  });
}, 'Unknown descriptors are ignored and do not invalidate rule');

</script>