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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
|
<!DOCTYPE HTML>
<meta charset="UTF-8">
<title>CSS Toggles: activation of toggles</title>
<link rel="author" title="L. David Baron" href="https://dbaron.org/">
<link rel="author" title="Google" href="http://www.google.com/">
<link rel="help" href="https://tabatkins.github.io/css-toggle/#toggle-trigger-property">
<link rel="help" href="https://tabatkins.github.io/css-toggle/#fire-a-toggle-activation">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="support/toggle-helpers.js"></script>
<style>
:toggle(finding-test 0) { --finding-test:0; }
:toggle(finding-test 1) { --finding-test:1; }
:toggle(test-states 0) { --test-states:0; }
:toggle(test-states 1) { --test-states:1; }
:toggle(test-states 2) { --test-states:2; }
:toggle(test-group 0) { --test-group:0; }
:toggle(test-group 1) { --test-group:1; }
:toggle(test-overflow 0) { --test-overflow:0; }
:toggle(test-overflow 1) { --test-overflow:1; }
:toggle(set-num 0) { --set-num:0; }
:toggle(set-num 1) { --set-num:1; }
:toggle(set-num 2) { --set-num:2; }
:toggle(set-names zero) { --set-names:0; }
:toggle(set-names one) { --set-names:1; }
:toggle(set-names two) { --set-names:2; }
:toggle(set-names three) { --set-names:3; }
</style>
<body>
<div id="container"></div>
<script>
let container = document.getElementById("container");
let toggle_finding_tests = [
{
description: "wide toggle on previous sibling",
markup: `<div>
<div id="toggle"></div>
<div id="target"></div>
</div>`,
found: true
},
{
description: "narrow toggle on previous sibling",
markup: `<div>
<div id="toggle"></div>
<div id="target"></div>
</div>`,
found: false,
self: true
},
{
description: "wide toggle on previous sibling with intervening narrow toggle",
markup: `<div>
<div id="toggle"></div>
<div style="toggle: finding-test self"></div>
<div id="target"></div>
</div>`,
found: true
},
{
description: "wide toggle on parent with intervening narrow toggle",
markup: `<div id="toggle">
<div style="toggle: finding-test self"></div>
<div id="target"></div>
</div>`,
found: true
},
{
description: "wide toggle on parent's prior sibling with intervening narrow toggle",
markup: `<div id="toggle"></div>
<div>
<div style="toggle: finding-test self"></div>
<div id="target"></div>
</div>`,
found: true
},
{
description: "wide toggle on later sibling",
markup: `<div id="target"></div>
<div id="toggle"></div>`,
found: false
},
{
description: "wide toggle on child",
markup: `<div id="target">
<div id="toggle"></div>
</div>`,
found: false
}
];
for (let toggle_finding_test of toggle_finding_tests) {
promise_test(async function() {
container.innerHTML = toggle_finding_test.markup;
let toggle = document.getElementById("toggle");
let toggle_cs = getComputedStyle(toggle);
let target = document.getElementById("target");
let toggle_root_style = "finding-test";
if (toggle_finding_test.self) {
toggle_root_style += " self";
}
toggle.style.toggleRoot = toggle_root_style;
target.style.toggleTrigger = "finding-test";
await wait_for_toggle_creation(toggle);
assert_true(toggle.matches(':toggle(finding-test 0)'),
"matches before click");
assert_equals(toggle_cs.getPropertyValue("--finding-test"), "0",
"computed style before click");
target.click();
if (toggle_finding_test.found) {
assert_true(toggle.matches(':toggle(finding-test 1)'),
"matches after click");
assert_equals(toggle_cs.getPropertyValue("--finding-test"), "1",
"computed style after click");
} else {
assert_true(toggle.matches(':toggle(finding-test 0)'),
"matches after click");
assert_equals(toggle_cs.getPropertyValue("--finding-test"), "0",
"computed style after click");
}
}, `finding toggle: ${toggle_finding_test.description}`);
}
promise_test(async function() {
let e = await set_up_single_toggle_in(container, "test-states 1 at 0");
let cs = getComputedStyle(e);
assert_true(e.matches(":toggle(test-states 0)"));
assert_equals(cs.getPropertyValue("--test-states"), "0");
e.click();
assert_true(e.matches(":toggle(test-states 1)"));
assert_equals(cs.getPropertyValue("--test-states"), "1");
e.click();
assert_true(e.matches(":toggle(test-states 0)"));
assert_equals(cs.getPropertyValue("--test-states"), "0");
e.style.toggleRoot = "test-states 2 at 2";
await wait_for_toggle_creation(e);
assert_true(e.matches(":toggle(test-states 0)"));
assert_equals(cs.getPropertyValue("--test-states"), "0");
e.click();
e.click();
assert_true(e.matches(":toggle(test-states 2)"));
assert_equals(cs.getPropertyValue("--test-states"), "2");
e.style.toggleRoot = "";
await wait_for_toggle_creation(e);
assert_true(e.matches(":toggle(test-states 2)"));
assert_equals(cs.getPropertyValue("--test-states"), "2");
e.click();
assert_true(e.matches(":toggle(test-states 0)"));
assert_equals(cs.getPropertyValue("--test-states"), "0");
e.click();
assert_true(e.matches(":toggle(test-states 1)"));
assert_equals(cs.getPropertyValue("--test-states"), "1");
e.click();
assert_true(e.matches(":toggle(test-states 0)"));
assert_equals(cs.getPropertyValue("--test-states"), "0");
}, "states used from toggle or toggle specifier as appropriate (integer)");
promise_test(async function() {
let e = await set_up_single_toggle_in(container, "test-states [one two] at 0");
let cs = getComputedStyle(e);
let t = e.toggles.get("test-states");
assert_equals(t.value, 0);
assert_true(e.matches(":toggle(test-states 0)"));
assert_true(e.matches(":toggle(test-states one)"));
assert_equals(cs.getPropertyValue("--test-states"), "0");
e.click();
assert_equals(t.value, "two");
assert_true(e.matches(":toggle(test-states 1)"));
assert_true(e.matches(":toggle(test-states two)"));
assert_equals(cs.getPropertyValue("--test-states"), "1");
e.click();
assert_equals(t.value, "one");
assert_true(e.matches(":toggle(test-states 0)"));
assert_true(e.matches(":toggle(test-states one)"));
assert_equals(cs.getPropertyValue("--test-states"), "0");
e.style.toggleRoot = "test-states [zero one two] at 2";
await wait_for_toggle_creation(e);
assert_equals(t.value, "one");
assert_true(e.matches(":toggle(test-states 0)"));
assert_true(e.matches(":toggle(test-states one)"));
assert_equals(cs.getPropertyValue("--test-states"), "0");
e.click();
assert_equals(t.value, "two");
assert_true(e.matches(":toggle(test-states 1)"));
assert_true(e.matches(":toggle(test-states two)"));
assert_equals(cs.getPropertyValue("--test-states"), "1");
e.click();
assert_equals(t.value, "zero");
assert_true(e.matches(":toggle(test-states zero)"));
assert_equals(cs.getPropertyValue("--test-states"), "");
e.style.toggleRoot = "";
await wait_for_toggle_creation(e);
assert_equals(t.value, "zero");
assert_true(e.matches(":toggle(test-states zero)"));
assert_equals(cs.getPropertyValue("--test-states"), "");
e.click();
assert_equals(t.value, "one");
assert_true(e.matches(":toggle(test-states 0)"));
assert_true(e.matches(":toggle(test-states one)"));
assert_equals(cs.getPropertyValue("--test-states"), "0");
e.click();
assert_equals(t.value, "two");
assert_true(e.matches(":toggle(test-states 1)"));
assert_true(e.matches(":toggle(test-states two)"));
assert_equals(cs.getPropertyValue("--test-states"), "1");
e.click();
assert_equals(t.value, "one");
assert_true(e.matches(":toggle(test-states 0)"));
assert_true(e.matches(":toggle(test-states one)"));
assert_equals(cs.getPropertyValue("--test-states"), "0");
}, "states used from toggle or toggle specifier as appropriate (names)");
promise_test(async function() {
container.innerHTML = `
<div style="toggle-group: test-group">
<div id="t" style="toggle: test-group 1 at 0 group"></div>
<div id="other" style="toggle: test-group 1 at 0 group"></div>
</div>
`;
let t = document.getElementById("t");
let other = document.getElementById("other");
let t_cs = getComputedStyle(t);
let other_cs = getComputedStyle(other);
await wait_for_toggle_creation(t);
await wait_for_toggle_creation(other);
other.click();
assert_true(t.matches(":toggle(test-group 0)"));
assert_equals(t_cs.getPropertyValue("--test-group"), "0");
assert_true(other.matches(":toggle(test-group 1)"));
assert_equals(other_cs.getPropertyValue("--test-group"), "1");
t.click();
assert_true(t.matches(":toggle(test-group 1)"));
assert_equals(t_cs.getPropertyValue("--test-group"), "1");
assert_true(other.matches(":toggle(test-group 0)"));
assert_equals(other_cs.getPropertyValue("--test-group"), "0");
other.click();
assert_true(t.matches(":toggle(test-group 0)"));
assert_equals(t_cs.getPropertyValue("--test-group"), "0");
assert_true(other.matches(":toggle(test-group 1)"));
assert_equals(other_cs.getPropertyValue("--test-group"), "1");
// Test that we use the group value from the toggle specifier when it's
// different from the toggle, but only when that's the toggle we're
// changing.
t.style.toggleRoot = "test-group 1 at 0";
t.click();
assert_true(t.matches(":toggle(test-group 1)"));
assert_equals(t_cs.getPropertyValue("--test-group"), "1");
assert_true(other.matches(":toggle(test-group 1)"));
assert_equals(other_cs.getPropertyValue("--test-group"), "1");
other.click();
assert_true(t.matches(":toggle(test-group 1)"));
assert_equals(t_cs.getPropertyValue("--test-group"), "1");
assert_true(other.matches(":toggle(test-group 0)"));
assert_equals(other_cs.getPropertyValue("--test-group"), "0");
other.click();
assert_true(t.matches(":toggle(test-group 0)"));
assert_equals(t_cs.getPropertyValue("--test-group"), "0");
assert_true(other.matches(":toggle(test-group 1)"));
assert_equals(other_cs.getPropertyValue("--test-group"), "1");
// Test that we use the group value from the toggle when there is no toggle
// specifier.
t.style.toggleRoot = "";
t.click();
assert_true(t.matches(":toggle(test-group 1)"));
assert_equals(t_cs.getPropertyValue("--test-group"), "1");
assert_true(other.matches(":toggle(test-group 0)"));
assert_equals(other_cs.getPropertyValue("--test-group"), "0");
other.click();
assert_true(t.matches(":toggle(test-group 0)"));
assert_equals(t_cs.getPropertyValue("--test-group"), "0");
assert_true(other.matches(":toggle(test-group 1)"));
assert_equals(other_cs.getPropertyValue("--test-group"), "1");
other.click();
assert_true(t.matches(":toggle(test-group 0)"));
assert_equals(t_cs.getPropertyValue("--test-group"), "0");
assert_true(other.matches(":toggle(test-group 0)"));
assert_equals(other_cs.getPropertyValue("--test-group"), "0");
}, "group used from toggle or toggle specifier as appropriate");
promise_test(async function() {
let e = await set_up_single_toggle_in(container, "test-overflow sticky");
let cs = getComputedStyle(e);
assert_true(e.matches(":toggle(test-overflow 0)"));
assert_equals(cs.getPropertyValue("--test-overflow"), "0");
e.click();
assert_true(e.matches(":toggle(test-overflow 1)"));
assert_equals(cs.getPropertyValue("--test-overflow"), "1");
e.click();
assert_true(e.matches(":toggle(test-overflow 1)"));
assert_equals(cs.getPropertyValue("--test-overflow"), "1");
e.style.toggleRoot = "test-overflow";
await wait_for_toggle_creation(e);
assert_true(e.matches(":toggle(test-overflow 1)"));
assert_equals(cs.getPropertyValue("--test-overflow"), "1");
e.click();
assert_true(e.matches(":toggle(test-overflow 0)"));
assert_equals(cs.getPropertyValue("--test-overflow"), "0");
e.style.toggleRoot = "";
await wait_for_toggle_creation(e);
assert_true(e.matches(":toggle(test-overflow 0)"));
assert_equals(cs.getPropertyValue("--test-overflow"), "0");
e.click();
assert_true(e.matches(":toggle(test-overflow 1)"));
assert_equals(cs.getPropertyValue("--test-overflow"), "1");
e.click();
assert_true(e.matches(":toggle(test-overflow 1)"));
assert_equals(cs.getPropertyValue("--test-overflow"), "1");
}, "overflow used from toggle or toggle specifier as appropriate");
promise_test(async function() {
container.innerHTML = `<div id="t" style="toggle-root: set-num 5 at 1"></div>
<div id="a" style="toggle-trigger: set-num set 2"></div>
<div id="b" style="toggle-trigger: set-num set 0"></div>
<div id="c" style="toggle-trigger: set-num set named-state"></div>
<div id="m"></div>
`;
let t = document.getElementById("t");
let a = document.getElementById("a");
let b = document.getElementById("b");
let c = document.getElementById("c");
let m = document.getElementById("m");
let cs = getComputedStyle(m);
await wait_for_toggle_creation(t);
assert_true(m.matches(':toggle(set-num 1)'), "initial state");
assert_equals(cs.getPropertyValue("--set-num"), "1");
a.click();
assert_true(m.matches(':toggle(set-num 2)'), "state after clicking a");
assert_equals(cs.getPropertyValue("--set-num"), "2");
b.click();
assert_true(m.matches(':toggle(set-num 0)'), "state after clicking b");
assert_equals(cs.getPropertyValue("--set-num"), "0");
c.click();
assert_true(m.matches(':toggle(set-num named-state)'), "state after clicking c");
assert_equals(cs.getPropertyValue("--set-num"), "");
}, "changing with toggle-trigger: set (numbers)");
promise_test(async function() {
container.innerHTML = `<div id="t" style="toggle-root: set-names [zero one two three four five] at two"></div>
<div id="a" style="toggle-trigger: set-names set one"></div>
<div id="b" style="toggle-trigger: set-names set 3"></div>
<div id="c" style="toggle-trigger: set-names set zero"></div>
<div id="d" style="toggle-trigger: set-names set named-state"></div>
<div id="m"></div>
`;
let t = document.getElementById("t");
let a = document.getElementById("a");
let b = document.getElementById("b");
let c = document.getElementById("c");
let d = document.getElementById("d");
let m = document.getElementById("m");
let cs = getComputedStyle(m);
await wait_for_toggle_creation(t);
assert_true(m.matches(':toggle(set-names two)'));
assert_true(m.matches(':toggle(set-names 2)'));
assert_equals(cs.getPropertyValue("--set-names"), "2");
a.click();
assert_true(m.matches(':toggle(set-names one)'));
assert_true(m.matches(':toggle(set-names 1)'));
assert_equals(cs.getPropertyValue("--set-names"), "1");
b.click();
assert_true(m.matches(':toggle(set-names three)'));
assert_true(m.matches(':toggle(set-names 3)'));
assert_equals(cs.getPropertyValue("--set-names"), "3");
c.click();
assert_true(m.matches(':toggle(set-names zero)'));
assert_true(m.matches(':toggle(set-names 0)'));
assert_equals(cs.getPropertyValue("--set-names"), "0");
d.click();
assert_true(m.matches(':toggle(set-names named-state)'));
assert_false(m.matches(':toggle(set-names 0)'));
assert_equals(cs.getPropertyValue("--set-names"), "");
b.click();
assert_true(m.matches(':toggle(set-names 3)'));
assert_equals(cs.getPropertyValue("--set-names"), "3");
d.click();
assert_false(m.matches(':toggle(set-names 3)'));
assert_equals(cs.getPropertyValue("--set-names"), "");
}, "changing with toggle-trigger: set (named states)");
function test_action_and_cycle(states_and_cycle, start, action, result) {
promise_test(async function() {
container.innerHTML = `
<div id="toggle" style="toggle-root: test ${states_and_cycle}"></div>
<div id="start" style="toggle-trigger: test set ${start}"></div>
<div id="action" style="toggle-trigger: test ${action}"></div>
`;
let toggle = document.getElementById("toggle");
await wait_for_toggle_creation(toggle);
document.getElementById("start").click();
assert_true(toggle.matches(`:toggle(test ${start})`), "value after set");
document.getElementById("action").click();
assert_true(toggle.matches(`:toggle(test ${result})`), "value after action");
}, `toggle with "${states_and_cycle}" changing from "${start}" with action "${action}"`);
}
function test_action_and_all_cycles(states, start, action, result_cycle, result_cycle_on, result_sticky) {
test_action_and_cycle(states, start, action, result_cycle);
test_action_and_cycle(`${states} cycle`, start, action, result_cycle);
test_action_and_cycle(`${states} cycle-on`, start, action, result_cycle_on);
test_action_and_cycle(`${states} sticky`, start, action, result_sticky);
}
test_action_and_all_cycles("2", "0", "next", "1", "1", "1");
test_action_and_all_cycles("2", "1", "next", "2", "2", "2");
test_action_and_all_cycles("2", "2", "next", "0", "1", "2");
test_action_and_all_cycles("3", "5", "next", "0", "1", "3");
test_action_and_all_cycles("4", "3", "next", "4", "4", "4");
test_action_and_all_cycles("4", "3", "next 3", "0", "1", "4");
test_action_and_all_cycles("3", "named-value", "next", "0", "1", "3");
test_action_and_all_cycles("[a b c d]", "a", "next", "b", "b", "b");
test_action_and_all_cycles("[a b c d]", "a", "next 5", "a", "b", "d");
test_action_and_all_cycles("[a b c d]", "c", "next", "d", "d", "d");
test_action_and_all_cycles("[a b c d]", "d", "next", "a", "b", "d");
test_action_and_all_cycles("[a b c d]", "extra-state", "next", "a", "b", "d");
test_action_and_all_cycles("2", "0", "prev", "2", "2", "0");
test_action_and_all_cycles("2", "1", "prev", "0", "2", "0");
test_action_and_all_cycles("2", "2", "prev", "1", "1", "1");
test_action_and_all_cycles("2", "5", "prev", "2", "2", "2");
test_action_and_all_cycles("3", "5", "prev 3", "2", "2", "2");
test_action_and_all_cycles("3", "2", "prev 3", "3", "3", "0");
test_action_and_all_cycles("3", "named-value", "prev", "3", "3", "3");
test_action_and_all_cycles("[a b c d]", "a", "prev", "d", "d", "a");
test_action_and_all_cycles("[a b c d]", "b", "prev", "a", "d", "a");
test_action_and_all_cycles("[a b c d]", "d", "prev", "c", "c", "c");
test_action_and_all_cycles("[a b c d]", "c", "prev 5", "d", "d", "a");
test_action_and_all_cycles("[a b c d]", "extra-state", "prev", "d", "d", "d");
// TODO(https://github.com/tabatkins/css-toggle/issues/39): This set of
// tests is testing proposed behavior; the spec currently says something
// that we agree is wrong.
promise_test(async function() {
container.innerHTML = `
<button id="toggle" style="toggle: test"></button>
`;
let toggle = document.getElementById("toggle");
await wait_for_toggle_creation(toggle);
assert_true(!toggle.matches(`:toggle(test)`), "value before click");
toggle.click();
assert_true(toggle.matches(`:toggle(test)`), "value after click");
}, "toggle activation on button with toggle-trigger (1)");
promise_test(async function() {
container.innerHTML = `
<div id="toggle" style="toggle-root: test">
<button id="trigger" style="toggle-trigger: test"></button>
</div>
`;
let toggle = document.getElementById("toggle");
await wait_for_toggle_creation(toggle);
assert_true(!toggle.matches(`:toggle(test)`), "value before click");
document.getElementById("trigger").click();
assert_true(toggle.matches(`:toggle(test)`), "value after click");
}, "toggle activation on button with toggle-trigger (2)");
promise_test(async function() {
container.innerHTML = `
<div id="toggle" style="toggle: test">
<button id="button"></button>
</div>
`;
let toggle = document.getElementById("toggle");
await wait_for_toggle_creation(toggle);
assert_true(!toggle.matches(`:toggle(test)`), "value before click");
document.getElementById("button").click();
assert_true(!toggle.matches(`:toggle(test)`), "value after button click");
toggle.click();
assert_true(toggle.matches(`:toggle(test)`), "value after div click");
}, "toggle activation on button inside element with toggle-trigger");
promise_test(async function() {
container.innerHTML = `
<div id="toggle" style="toggle: test">
<div id="div"></div>
</div>
`;
let toggle = document.getElementById("toggle");
await wait_for_toggle_creation(toggle);
assert_true(!toggle.matches(`:toggle(test)`), "value before click");
document.getElementById("div").click();
assert_true(toggle.matches(`:toggle(test)`), "value after inner div click");
toggle.click();
assert_true(!toggle.matches(`:toggle(test)`), "value after outer div click");
}, "toggle activation on div inside element with toggle-trigger");
</script>
|