-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Expand file tree
/
Copy pathactive-observable.html
More file actions
326 lines (279 loc) · 9.03 KB
/
Copy pathactive-observable.html
File metadata and controls
326 lines (279 loc) · 9.03 KB
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
<!doctype html>
<meta charset="utf-8" />
<title>
Selectors: :active is observable from within pointerdown/mousedown listeners
</title>
<link rel="author" href="mailto:wpt@keithcirkel.co.uk" />
<link rel="help" href="https://drafts.csswg.org/selectors/#the-active-pseudo" />
<link
rel="help"
href="https://html.spec.whatwg.org/multipage/semantics-other.html#selector-active"
/>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-actions.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<style>
.target,
.parent {
min-width: 80px;
min-height: 80px;
}
</style>
<div id="scratch"></div>
<script>
function waitForRender() {
return new Promise((resolve) =>
requestAnimationFrame(() => requestAnimationFrame(resolve)),
);
}
// Use pointer events to perform a click, so each pointerevent
// can be monitored.
function click(element, button = "LEFT", { moveWithin = false } = {}) {
const actions = new test_driver.Actions();
let chain = actions
.pointerMove(0, 0, {})
.pointerMove(0, 0, { origin: element })
.pointerDown({ button: actions.ButtonType[button] });
if (moveWithin) {
chain = chain.pointerMove(5, 5, { origin: element });
}
return chain.pointerUp({ button: actions.ButtonType[button] }).send();
}
async function resetPointer() {
await new test_driver.Actions().pointerMove(0, 0).send();
await waitForRender();
}
const scratch = document.getElementById("scratch");
function makeTarget(t, childTag = "button") {
const parent = document.createElement("div");
parent.className = "parent";
const child = document.createElement(childTag);
child.className = "target";
child.append(childTag === "span" ? "some text" : "target");
parent.append(child);
scratch.append(parent);
t.add_cleanup(() => parent.remove());
return { parent, child };
}
const downEvents = ["pointerdown", "mousedown"];
for (const downEvent of downEvents) {
promise_test(async (t) => {
t.add_cleanup(resetPointer);
const { child: target } = makeTarget(t);
await waitForRender();
let matched = null;
target.addEventListener(
downEvent,
() => {
matched = target.matches(":active");
},
{ signal: t.signal },
);
await click(target);
assert_true(
matched,
`target matches :active synchronously inside its ${downEvent} listener`,
);
}, `Clicked element matches :active within its ${downEvent} listener`);
promise_test(async (t) => {
t.add_cleanup(resetPointer);
const { parent, child: target } = makeTarget(t);
await waitForRender();
let matched = null;
target.addEventListener(
downEvent,
() => {
matched = parent.matches(":active");
},
{ signal: t.signal },
);
await click(target);
assert_true(
matched,
`ancestor matches :active within the ${downEvent} listener of a descendant`,
);
}, `Ancestor of clicked element matches :active within ${downEvent} listener`);
promise_test(async (t) => {
t.add_cleanup(resetPointer);
const { parent, child: textTarget } = makeTarget(t, "span");
await waitForRender();
let matched = null;
parent.addEventListener(
downEvent,
() => {
matched = parent.matches(":active");
},
{ signal: t.signal },
);
await click(textTarget);
assert_true(
matched,
`parent element matches :active when a text node is the press target (${downEvent})`,
);
}, `Text node press target activates its parent element (${downEvent})`);
promise_test(async (t) => {
t.add_cleanup(resetPointer);
const { child: target } = makeTarget(t);
await waitForRender();
let matchedDuringPress = null;
target.addEventListener(
downEvent,
(e) => {
e.preventDefault();
matchedDuringPress = target.matches(":active");
},
{ signal: t.signal },
);
await click(target);
await waitForRender();
assert_true(
matchedDuringPress,
`target matches :active even when the ${downEvent} listener calls preventDefault()`,
);
assert_false(
target.matches(":active"),
`target is no longer :active after release even when ${downEvent} calls preventDefault()`,
);
}, `:active applies on press and clears on release with preventDefault() on ${downEvent}`);
}
promise_test(async (t) => {
t.add_cleanup(resetPointer);
const { child: target } = makeTarget(t);
await waitForRender();
let matchedWhileHeld = null;
target.addEventListener(
"pointerdown",
() => {
matchedWhileHeld = target.matches(":active");
},
{ signal: t.signal },
);
await click(target);
await waitForRender();
assert_true(
matchedWhileHeld,
"target matches :active while the pointer is held down",
);
assert_false(
target.matches(":active"),
"target no longer matches :active after the pointer is released",
);
}, ":active is set while held and cleared after the pointer is released");
const upEvents = ["pointerup", "mouseup"];
for (const upEvent of upEvents) {
promise_test(async (t) => {
t.add_cleanup(resetPointer);
const { child: target } = makeTarget(t);
await waitForRender();
let matchedDuringUp = null;
target.addEventListener(
upEvent,
() => {
matchedDuringUp = target.matches(":active");
},
{ signal: t.signal },
);
await click(target);
await waitForRender();
assert_false(
matchedDuringUp,
`target no longer matches :active synchronously inside its ${upEvent} listener`,
);
assert_false(
target.matches(":active"),
`target no longer matches :active after ${upEvent} handling completes`,
);
}, `Clicked element no longer matches :active within its ${upEvent} listener`);
promise_test(async (t) => {
t.add_cleanup(resetPointer);
const { child: target } = makeTarget(t);
await waitForRender();
target.addEventListener(upEvent, (e) => e.preventDefault(), {
signal: t.signal,
});
let matchedWhileHeld = null;
target.addEventListener(
"pointerdown",
() => {
matchedWhileHeld = target.matches(":active");
},
{ signal: t.signal },
);
await click(target);
await waitForRender();
assert_true(
matchedWhileHeld,
"target is :active while the pointer is held down",
);
assert_false(
target.matches(":active"),
`target is no longer :active after release even when ${upEvent} calls preventDefault()`,
);
}, `:active is cleared on release even when ${upEvent} listener calls preventDefault()`);
}
promise_test(async (t) => {
t.add_cleanup(resetPointer);
const { child: target } = makeTarget(t);
await waitForRender();
let matchedDuringMove = null;
target.addEventListener(
"pointermove",
() => {
matchedDuringMove = target.matches(":active");
},
{ signal: t.signal },
);
await click(target, "LEFT", { moveWithin: true });
await waitForRender();
assert_true(
matchedDuringMove,
"target still matches :active while the held pointer moves within it",
);
}, ":active persists while the held pointer moves within the target");
promise_test(async (t) => {
t.add_cleanup(resetPointer);
const { child: target } = makeTarget(t);
await waitForRender();
let matchedDuringClick = null;
target.addEventListener(
"click",
() => {
matchedDuringClick = target.matches(":active");
},
{ signal: t.signal },
);
await click(target);
await waitForRender();
assert_false(
matchedDuringClick,
"target no longer matches :active inside its click listener",
);
}, "Clicked element no longer matches :active within its click listener");
for (const button of ["RIGHT", "MIDDLE"]) {
promise_test(async (t) => {
t.add_cleanup(resetPointer);
const { child: target } = makeTarget(t);
await waitForRender();
let matchedWhileHeld = null;
target.addEventListener(
"pointerdown",
() => {
matchedWhileHeld = target.matches(":active");
},
{ signal: t.signal },
);
await click(target, button);
await waitForRender();
assert_true(
matchedWhileHeld,
`target matches :active for a ${button.toLowerCase()}-button press`,
);
assert_false(
target.matches(":active"),
`target no longer matches :active after the ${button.toLowerCase()}-button release`,
);
}, `Non-primary (${button.toLowerCase()}) button press activates :active`);
}
</script>