-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
input-events-typing.html
396 lines (353 loc) · 14.3 KB
/
input-events-typing.html
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
<!DOCTYPE html>
<meta charset="utf-8">
<title>Input Event typing tests</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="/resources/testdriver-actions.js"></script>
<div id="rich" contenteditable></div>
<textarea id="plain"></textarea>
<script>
let inputEventsLog = [];
const rich = document.getElementById('rich');
const plain = document.getElementById('plain');
function log(event) {
const clone = new event.constructor(event.type, event);
clone.state = rich.innerHTML;
inputEventsLog.push(clone);
}
function resetRich() {
inputEventsLog = [];
rich.innerHTML = '';
}
function resetPlain() {
inputEventsLog = [];
plain.innerHTML = '';
}
rich.addEventListener('beforeinput', log);
rich.addEventListener('input', log);
promise_test(async function() {
this.add_cleanup(resetRich);
rich.focus();
const message = 'Hello';
await test_driver.send_keys(rich, message);
// 10 events (5 beforeinput + 5 input events)
assert_equals(inputEventsLog.length, 10);
for (let i = 0; i < inputEventsLog.length; i += 2) {
const beforeInputEvent = inputEventsLog[i];
const inputEvent = inputEventsLog[i + 1];
assert_equals(beforeInputEvent.type, 'beforeinput');
assert_equals(inputEvent.type, 'input');
assert_equals(beforeInputEvent.inputType, 'insertText');
assert_equals(inputEvent.inputType, 'insertText');
assert_equals(beforeInputEvent.data, inputEvent.data);
assert_equals(inputEvent.data, message[i / 2]);
assert_equals(beforeInputEvent.state + message[i / 2], inputEvent.state);
}
}, 'It triggers beforeinput and input events on text typing');
promise_test(async function() {
this.add_cleanup(resetRich);
rich.focus();
await test_driver.send_keys(rich, "\uE006"); // Return
assert_equals(inputEventsLog.length, 2);
const beforeInputEvent = inputEventsLog[0];
const inputEvent = inputEventsLog[1];
assert_equals(beforeInputEvent.type, 'beforeinput');
assert_equals(inputEvent.type, 'input');
assert_equals(beforeInputEvent.inputType, 'insertParagraph');
assert_equals(inputEvent.inputType, 'insertParagraph');
assert_equals(beforeInputEvent.data, inputEvent.data);
}, 'It triggers beforeinput and input events on typing RETURN');
promise_test(async function() {
this.add_cleanup(resetPlain);
const expectedResult = [
// Pressing 'a'
'insertText',
// Return
'insertLineBreak'
];
const result = [];
plain.addEventListener("input", (inputEvent) => {
result.push(inputEvent.inputType);
});
await test_driver.click(plain);
await test_driver.send_keys(plain,"a");
await test_driver.send_keys(plain,'\uE006'); // Return
assert_equals(result.length, expectedResult.length);
expectedResult.forEach((er, index) => assert_equals(result[index], er));
}, 'Newline character in plain text editing should get insertLinebreak input event');
promise_test(async function() {
this.add_cleanup(resetRich);
rich.focus();
await new test_driver.Actions()
.keyDown('\uE008') // Shift
.keyDown('\uE006') // Return
.keyUp('\uE006')
.keyUp('\uE008')
.send();
assert_equals(inputEventsLog.length, 2);
const [beforeInputEvent, inputEvent] = inputEventsLog;
assert_equals(beforeInputEvent.type, 'beforeinput');
assert_equals(inputEvent.type, 'input');
assert_equals(beforeInputEvent.inputType, 'insertLineBreak');
assert_equals(inputEvent.inputType, 'insertLineBreak');
assert_equals(beforeInputEvent.data, inputEvent.data);
}, 'It triggers beforeinput and input events on typing Shift+RETURN');
promise_test(async function() {
this.add_cleanup(resetRich);
rich.innerHTML = '<p>Preexisting <i id="caret">c</i>ontent</p>';
const caret = document.querySelector('#caret');
await test_driver.click(caret);
await test_driver.send_keys(caret, "\uE017"); // Delete
assert_equals(inputEventsLog.length, 2);
const [beforeInputEvent, inputEvent] = inputEventsLog;
assert_equals(beforeInputEvent.type, 'beforeinput');
assert_equals(inputEvent.type, 'input');
assert_equals(beforeInputEvent.inputType, 'deleteContentForward');
assert_equals(inputEvent.inputType, 'deleteContentForward');
assert_equals(beforeInputEvent.data, inputEvent.data);
}, 'It triggers beforeinput and input events on typing DELETE with pre-existing content');
promise_test(async function() {
this.add_cleanup(resetRich);
rich.focus();
await test_driver.send_keys(rich, "\uE017"); // Delete
assert_equals(inputEventsLog.length, 2);
const [beforeInputEvent, inputEvent] = inputEventsLog;
assert_equals(beforeInputEvent.type, 'beforeinput');
assert_equals(inputEvent.type, 'input');
assert_equals(beforeInputEvent.inputType, 'deleteContentForward');
assert_equals(inputEvent.inputType, 'deleteContentForward');
assert_equals(beforeInputEvent.data, inputEvent.data);
}, 'It triggers beforeinput and input events on typing DELETE with no pre-existing content');
promise_test(async function() {
this.add_cleanup(resetRich);
rich.innerHTML = '<p>Preexisting <i id="caret">c</i>ontent</p>';
await test_driver.click(document.querySelector('#caret'));
await test_driver.send_keys(rich, "\uE003"); // Back Space
assert_equals(inputEventsLog.length, 2);
const [beforeInputEvent, inputEvent] = inputEventsLog;
assert_equals(beforeInputEvent.type, 'beforeinput');
assert_equals(inputEvent.type, 'input');
assert_equals(beforeInputEvent.inputType, 'deleteContentBackward');
assert_equals(inputEvent.inputType, 'deleteContentBackward');
assert_equals(beforeInputEvent.data, inputEvent.data);
}, 'It triggers beforeinput and input events on typing BACK_SPACE with pre-existing content');
promise_test(async function () {
this.add_cleanup(resetRich);
rich.innerHTML = '<p>Preexisting <i id="caret">C</i>ontent</p>';
const expectedResult = [
// Pressing 'a', 'b'
'insertText',
'insertText',
// Delete twice
'deleteContentForward',
'deleteContentForward',
// Pressing 'c', 'd'
'insertText',
'insertText',
// Backspace
'deleteContentBackward'
];
const result = [];
rich.addEventListener("input", (inputEvent) => {
result.push(inputEvent.inputType);
});
await test_driver.click(document.querySelector('#caret')); // Preexisting |Content
await test_driver.send_keys(rich, "a"); // Preexisting a|Content
await test_driver.send_keys(rich, "b"); // Preexisting ab|Content
// Delete
await test_driver.send_keys(rich, "\uE017"); // Preexisting ab|ontent
// Delete
await test_driver.send_keys(rich, "\uE017"); // Preexisting ab|ntent
await test_driver.send_keys(rich, "c"); // Preexisting abc|ntent
await test_driver.send_keys(rich, "d"); // Preexisting abcd|ntent
// Backspace
await test_driver.send_keys(rich, "\uE003"); // Preexisting abc|ntent
assert_equals(result.length, expectedResult.length);
expectedResult.forEach((er, index) => assert_equals(result[index], er));
}, 'Input events have correct inputType updated when different inputs are typed');
promise_test(async function () {
this.add_cleanup(resetRich);
rich.innerHTML = '<p>Preexisting <i id="caret">c</i>ontent</p>';
const expectedResult = [
// Remove selected text with Backspace
'deleteContentBackward',
// Remove selected text with Delete
'deleteContentForward'
];
const result = [];
rich.addEventListener("input", (inputEvent) => {
result.push(inputEvent.inputType);
});
const modifierKey = navigator.platform === "MacIntel" ? '\u2318' : '\uE009';
// Click before "content"
await test_driver.click(document.querySelector('#caret')); // Preexisting |content
// Select text to the left
await new test_driver.Actions()
.keyDown(modifierKey)
.keyDown('\uE008') // Shift
.keyDown('\uE012') // Arrow Left
.keyUp('\uE012')
.keyUp('\uE008')
.keyUp(modifierKey)
.send(); // |Preexisting ^content
// Backspace
await test_driver.send_keys(rich, "\uE003"); // |content
// Select text to the right
await new test_driver.Actions()
.keyDown(modifierKey)
.keyDown('\uE008') // Shift
.keyDown('\uE014') // Arrow Right
.keyUp('\uE012')
.keyUp('\uE008')
.keyUp(modifierKey)
.send(); // ^content|
// Delete
await test_driver.send_keys(rich, "\uE017"); // |
assert_equals(result.length, expectedResult.length);
expectedResult.forEach((er, index) => assert_equals(result[index], er));
}, 'Input events have correct inputType when selected text is removed with Backspace or Delete');
promise_test(async function() {
this.add_cleanup(resetRich);
rich.focus();
await test_driver.send_keys(rich, "\uE003"); // Back Space
assert_equals(inputEventsLog.length, 2);
const [beforeInputEvent, inputEvent] = inputEventsLog;
assert_equals(beforeInputEvent.type, 'beforeinput');
assert_equals(inputEvent.type, 'input');
assert_equals(beforeInputEvent.inputType, 'deleteContentBackward');
assert_equals(inputEvent.inputType, 'deleteContentBackward');
assert_equals(beforeInputEvent.data, inputEvent.data);
}, 'It triggers beforeinput and input events on typing BACK_SPACE with no pre-existing content');
promise_test(async function() {
this.add_cleanup(resetRich);
rich.focus();
await test_driver.send_keys(rich, "hello");
// Decide whether to use Key.COMMAND (mac) or Key.CONTROL (everything else)
const modifierKey = navigator.platform === "MacIntel" ? '\u2318' : '\uE009';
// Undo
await new test_driver.Actions()
.keyDown(modifierKey)
.keyDown('z')
.keyUp('z')
.keyUp(modifierKey)
.send();
// Redo
await new test_driver.Actions()
.keyDown(modifierKey)
.keyDown('\uE008') // Shift
.keyDown('z')
.keyUp('z')
.keyUp('\uE008')
.keyUp(modifierKey)
.send();
// Ignore the initial typing of 'hello'
const historyInputEventsLog = inputEventsLog.slice(10);
assert_equals(historyInputEventsLog.length, 4);
const inputTypes = ['historyUndo', 'historyRedo'];
for (let i = 0; i < historyInputEventsLog.length; i += 2) {
// We are increaisng i by 2 as there should always be matching beforeinput and input events.
const beforeInputEvent = historyInputEventsLog[i];
const inputEvent = historyInputEventsLog[i + 1];
assert_equals(beforeInputEvent.type, 'beforeinput');
assert_equals(inputEvent.type, 'input');
assert_equals(beforeInputEvent.inputType, inputTypes[i / 2]);
assert_equals(inputEvent.inputType, inputTypes[i / 2]);
assert_equals(beforeInputEvent.data, inputEvent.data);
}
}, 'It triggers beforeinput and input events on typing Undo and Redo key combinations with an existing history');
promise_test(async function() {
this.add_cleanup(resetRich);
rich.focus();
// Decide whether to use Key.COMMAND (mac) or Key.CONTROL (everything else)
const modifierKey = navigator.platform === "MacIntel" ? '\u2318' : '\uE009';
// Undo
await new test_driver.Actions()
.keyDown(modifierKey)
.keyDown('z')
.keyUp('z')
.keyUp(modifierKey)
.send();
// Redo
await new test_driver.Actions()
.keyDown(modifierKey)
.keyDown('\uE008') // Shift
.keyDown('z')
.keyUp('z')
.keyUp('\uE008')
.keyUp(modifierKey)
.send();
assert_equals(inputEventsLog.length, 4);
const inputTypes = ['historyUndo', 'historyRedo'];
for (let i = 0; i < inputEventsLog.length; i += 2) {
const beforeInputEvent = inputEventsLog[i];
const inputEvent = inputEventsLog[i + 1];
assert_equals(beforeInputEvent.type, 'beforeinput');
assert_equals(inputEvent.type, 'input');
assert_equals(beforeInputEvent.inputType, inputTypes[i / 2]);
assert_equals(inputEvent.inputType, inputTypes[i / 2]);
assert_equals(beforeInputEvent.data, inputEvent.data);
}
}, 'It triggers beforeinput and input events on typing Undo and Redo key combinations without an existing history');
promise_test(async function() {
this.add_cleanup(resetRich);
const expectedResult = [
// Pressing 'a'.
'plain-keydown-a',
'plain-keypress-a',
'plain-beforeinput-a-null',
'plain-input-a-null',
'plain-keyup-a',
// Pressing Shift-'b'.
'plain-keydown-B',
'plain-keypress-B',
'plain-beforeinput-B-null',
'plain-input-B-null',
'plain-keyup-B',
// Pressing 'c'.
'rich-keydown-c',
'rich-keypress-c',
'rich-beforeinput-c-null',
'rich-input-c-null',
'rich-keyup-c',
// Pressing Shift-'d'.
'rich-keydown-D',
'rich-keypress-D',
'rich-beforeinput-D-null',
'rich-input-D-null',
'rich-keyup-D',
];
const result = [];
for (const eventType of ['beforeinput', 'input', 'keydown', 'keypress', 'keyup']) {
const listener = event => {
if (event.key === 'Shift') return;
const eventInfo = [event.target.id, event.type, event.data || event.key];
if (event instanceof InputEvent) eventInfo.push(String(event.dataTransfer));
result.push(eventInfo.join('-'));
}
rich.addEventListener(eventType, listener);
plain.addEventListener(eventType, listener);
}
plain.focus();
await new test_driver.Actions()
.keyDown('a')
.keyUp('a')
.keyDown('\uE008') // Shift
.keyDown('b')
.keyUp('b')
.keyUp('\uE008')
.send();
rich.focus();
await new test_driver.Actions()
.keyDown('c')
.keyUp('c')
.keyDown('\uE008') // Shift
.keyDown('d')
.keyUp('d')
.keyUp('\uE008')
.send();
assert_equals(result.length, expectedResult.length);
expectedResult.forEach((er, index) => assert_equals(result[index], er));
}, 'InputEvents have correct data/order when typing on textarea and contenteditable');
</script>