@testing-library/user-event 13.4.2 → 13.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md
CHANGED
|
@@ -255,6 +255,7 @@ Thanks goes to these people ([emoji key][emojis]):
|
|
|
255
255
|
<td align="center"><a href="http://www.largetimber.com"><img src="https://avatars.githubusercontent.com/u/10626756?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Eric Wang</b></sub></a><br /><a href="https://github.com/testing-library/user-event/commits?author=fa93hws" title="Code">💻</a></td>
|
|
256
256
|
<td align="center"><a href="https://github.com/jesperorb"><img src="https://avatars.githubusercontent.com/u/21122051?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Jesper Orb</b></sub></a><br /><a href="https://github.com/testing-library/user-event/commits?author=jesperorb" title="Code">💻</a></td>
|
|
257
257
|
<td align="center"><a href="https://johannesfischer.github.io/"><img src="https://avatars.githubusercontent.com/u/28100?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Johannes Fischer</b></sub></a><br /><a href="https://github.com/testing-library/user-event/commits?author=JohannesFischer" title="Code">💻</a></td>
|
|
258
|
+
<td align="center"><a href="https://github.com/777PolarFox777"><img src="https://avatars.githubusercontent.com/u/19393384?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Andrew D.</b></sub></a><br /><a href="https://github.com/testing-library/user-event/commits?author=777PolarFox777" title="Code">💻</a></td>
|
|
258
259
|
</tr>
|
|
259
260
|
</table>
|
|
260
261
|
|
|
@@ -6,6 +6,8 @@ import { keyboardKey, keyboardOptions } from './types';
|
|
|
6
6
|
* Everything else will be interpreted as a typed character - e.g. `a`.
|
|
7
7
|
* Brackets `{` and `[` can be escaped by doubling - e.g. `foo[[bar` translates to `foo[bar`.
|
|
8
8
|
* Keeping the key pressed can be written as `{key>}`.
|
|
9
|
+
* When keeping the key pressed you can choose how long (how many keydown and keypress) the key is pressed `{key>3}`.
|
|
10
|
+
* You can then release the key per `{key>3/}` or keep it pressed and continue with the next key.
|
|
9
11
|
* Modifiers like `{shift}` imply being kept pressed. This can be turned of per `{shift/}`.
|
|
10
12
|
*/
|
|
11
13
|
export declare function getNextKeyDef(text: string, options: keyboardOptions): {
|
|
@@ -13,4 +15,5 @@ export declare function getNextKeyDef(text: string, options: keyboardOptions): {
|
|
|
13
15
|
consumedLength: number;
|
|
14
16
|
releasePrevious: boolean;
|
|
15
17
|
releaseSelf: boolean;
|
|
18
|
+
repeat: number;
|
|
16
19
|
};
|
|
@@ -4,7 +4,23 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.getNextKeyDef = getNextKeyDef;
|
|
7
|
+
var bracketDict;
|
|
7
8
|
|
|
9
|
+
(function (bracketDict) {
|
|
10
|
+
bracketDict["{"] = "}";
|
|
11
|
+
bracketDict["["] = "]";
|
|
12
|
+
})(bracketDict || (bracketDict = {}));
|
|
13
|
+
|
|
14
|
+
var legacyModifiers;
|
|
15
|
+
|
|
16
|
+
(function (legacyModifiers) {
|
|
17
|
+
legacyModifiers["alt"] = "alt";
|
|
18
|
+
legacyModifiers["ctrl"] = "ctrl";
|
|
19
|
+
legacyModifiers["meta"] = "meta";
|
|
20
|
+
legacyModifiers["shift"] = "shift";
|
|
21
|
+
})(legacyModifiers || (legacyModifiers = {}));
|
|
22
|
+
|
|
23
|
+
var legacyKeyMap;
|
|
8
24
|
/**
|
|
9
25
|
* Get the next key from keyMap
|
|
10
26
|
*
|
|
@@ -12,98 +28,143 @@ exports.getNextKeyDef = getNextKeyDef;
|
|
|
12
28
|
* Everything else will be interpreted as a typed character - e.g. `a`.
|
|
13
29
|
* Brackets `{` and `[` can be escaped by doubling - e.g. `foo[[bar` translates to `foo[bar`.
|
|
14
30
|
* Keeping the key pressed can be written as `{key>}`.
|
|
31
|
+
* When keeping the key pressed you can choose how long (how many keydown and keypress) the key is pressed `{key>3}`.
|
|
32
|
+
* You can then release the key per `{key>3/}` or keep it pressed and continue with the next key.
|
|
15
33
|
* Modifiers like `{shift}` imply being kept pressed. This can be turned of per `{shift/}`.
|
|
16
34
|
*/
|
|
17
|
-
function getNextKeyDef(text, options) {
|
|
18
|
-
var _text$slice$match;
|
|
19
35
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
36
|
+
(function (legacyKeyMap) {
|
|
37
|
+
legacyKeyMap["ctrl"] = "Control";
|
|
38
|
+
legacyKeyMap["del"] = "Delete";
|
|
39
|
+
legacyKeyMap["esc"] = "Escape";
|
|
40
|
+
legacyKeyMap["space"] = " ";
|
|
41
|
+
})(legacyKeyMap || (legacyKeyMap = {}));
|
|
24
42
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
43
|
+
function getNextKeyDef(text, options) {
|
|
44
|
+
var _options$keyboardMap$;
|
|
45
|
+
|
|
46
|
+
const {
|
|
47
|
+
type,
|
|
48
|
+
descriptor,
|
|
49
|
+
consumedLength,
|
|
50
|
+
releasePrevious,
|
|
51
|
+
releaseSelf,
|
|
52
|
+
repeat
|
|
53
|
+
} = readNextDescriptor(text);
|
|
54
|
+
const keyDef = (_options$keyboardMap$ = options.keyboardMap.find(def => {
|
|
55
|
+
if (type === '[') {
|
|
56
|
+
var _def$code;
|
|
57
|
+
|
|
58
|
+
return ((_def$code = def.code) == null ? void 0 : _def$code.toLowerCase()) === descriptor.toLowerCase();
|
|
59
|
+
} else if (type === '{') {
|
|
60
|
+
var _def$key;
|
|
61
|
+
|
|
62
|
+
const key = mapLegacyKey(descriptor);
|
|
63
|
+
return ((_def$key = def.key) == null ? void 0 : _def$key.toLowerCase()) === key.toLowerCase();
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return def.key === descriptor;
|
|
67
|
+
})) != null ? _options$keyboardMap$ : {
|
|
68
|
+
key: 'Unknown',
|
|
69
|
+
code: 'Unknown',
|
|
70
|
+
[type === '[' ? 'code' : 'key']: descriptor
|
|
71
|
+
};
|
|
72
|
+
return {
|
|
73
|
+
keyDef,
|
|
74
|
+
consumedLength,
|
|
75
|
+
releasePrevious,
|
|
76
|
+
releaseSelf,
|
|
77
|
+
repeat
|
|
78
|
+
};
|
|
79
|
+
}
|
|
28
80
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
const
|
|
81
|
+
function readNextDescriptor(text) {
|
|
82
|
+
let pos = 0;
|
|
83
|
+
const startBracket = text[pos] in bracketDict ? text[pos] : '';
|
|
84
|
+
pos += startBracket.length; // `foo{{bar` is an escaped char at position 3,
|
|
85
|
+
// but `foo{{{>5}bar` should be treated as `{` pressed down for 5 keydowns.
|
|
86
|
+
|
|
87
|
+
const startBracketRepeated = startBracket ? text.match(new RegExp(`^\\${startBracket}+`))[0].length : 0;
|
|
88
|
+
const isEscapedChar = startBracketRepeated === 2 || startBracket === '{' && startBracketRepeated > 3;
|
|
89
|
+
const type = isEscapedChar ? '' : startBracket;
|
|
90
|
+
return {
|
|
91
|
+
type,
|
|
92
|
+
...(type === '' ? readPrintableChar(text, pos) : readTag(text, pos, type))
|
|
93
|
+
};
|
|
94
|
+
}
|
|
32
95
|
|
|
33
|
-
|
|
34
|
-
|
|
96
|
+
function readPrintableChar(text, pos) {
|
|
97
|
+
const descriptor = text[pos];
|
|
98
|
+
assertDescriptor(descriptor, text, pos);
|
|
99
|
+
pos += descriptor.length;
|
|
100
|
+
return {
|
|
101
|
+
consumedLength: pos,
|
|
102
|
+
descriptor,
|
|
103
|
+
releasePrevious: false,
|
|
104
|
+
releaseSelf: true,
|
|
105
|
+
repeat: 1
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function readTag(text, pos, startBracket) {
|
|
110
|
+
var _text$slice$match, _text$slice$match$, _text$slice$match2;
|
|
111
|
+
|
|
112
|
+
const releasePreviousModifier = text[pos] === '/' ? '/' : '';
|
|
113
|
+
pos += releasePreviousModifier.length;
|
|
114
|
+
const descriptor = (_text$slice$match = text.slice(pos).match(/^\w+/)) == null ? void 0 : _text$slice$match[0];
|
|
115
|
+
assertDescriptor(descriptor, text, pos);
|
|
116
|
+
pos += descriptor.length;
|
|
117
|
+
const repeatModifier = (_text$slice$match$ = (_text$slice$match2 = text.slice(pos).match(/^>\d+/)) == null ? void 0 : _text$slice$match2[0]) != null ? _text$slice$match$ : '';
|
|
118
|
+
pos += repeatModifier.length;
|
|
119
|
+
const releaseSelfModifier = text[pos] === '/' || !repeatModifier && text[pos] === '>' ? text[pos] : '';
|
|
120
|
+
pos += releaseSelfModifier.length;
|
|
121
|
+
const expectedEndBracket = bracketDict[startBracket];
|
|
122
|
+
const endBracket = text[pos] === expectedEndBracket ? expectedEndBracket : '';
|
|
123
|
+
|
|
124
|
+
if (!endBracket) {
|
|
125
|
+
throw new Error(getErrorMessage([!repeatModifier && 'repeat modifier', !releaseSelfModifier && 'release modifier', `"${expectedEndBracket}"`].filter(Boolean).join(' or '), text[pos], text));
|
|
35
126
|
}
|
|
36
127
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
128
|
+
pos += endBracket.length;
|
|
129
|
+
return {
|
|
130
|
+
consumedLength: pos,
|
|
131
|
+
descriptor,
|
|
132
|
+
releasePrevious: !!releasePreviousModifier,
|
|
133
|
+
repeat: repeatModifier ? Math.max(Number(repeatModifier.substr(1)), 1) : 1,
|
|
134
|
+
releaseSelf: hasReleaseSelf(startBracket, descriptor, releaseSelfModifier, repeatModifier)
|
|
41
135
|
};
|
|
136
|
+
}
|
|
42
137
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
return { ...modifiers,
|
|
47
|
-
keyDef: (_options$keyboardMap$ = options.keyboardMap.find(k => k.key === descriptor)) != null ? _options$keyboardMap$ : {
|
|
48
|
-
key: descriptor,
|
|
49
|
-
code: 'Unknown'
|
|
50
|
-
}
|
|
51
|
-
};
|
|
52
|
-
} else if (startBracket === '{') {
|
|
53
|
-
var _options$keyboardMap$2;
|
|
54
|
-
|
|
55
|
-
const key = mapLegacyKey(descriptor);
|
|
56
|
-
return { ...modifiers,
|
|
57
|
-
keyDef: (_options$keyboardMap$2 = options.keyboardMap.find(k => {
|
|
58
|
-
var _k$key;
|
|
59
|
-
|
|
60
|
-
return ((_k$key = k.key) == null ? void 0 : _k$key.toLowerCase()) === key.toLowerCase();
|
|
61
|
-
})) != null ? _options$keyboardMap$2 : {
|
|
62
|
-
key: descriptor,
|
|
63
|
-
code: 'Unknown'
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
} else {
|
|
67
|
-
var _options$keyboardMap$3;
|
|
68
|
-
|
|
69
|
-
return { ...modifiers,
|
|
70
|
-
keyDef: (_options$keyboardMap$3 = options.keyboardMap.find(k => {
|
|
71
|
-
var _k$code;
|
|
72
|
-
|
|
73
|
-
return ((_k$code = k.code) == null ? void 0 : _k$code.toLowerCase()) === descriptor.toLowerCase();
|
|
74
|
-
})) != null ? _options$keyboardMap$3 : {
|
|
75
|
-
key: 'Unknown',
|
|
76
|
-
code: descriptor
|
|
77
|
-
}
|
|
78
|
-
};
|
|
138
|
+
function assertDescriptor(descriptor, text, pos) {
|
|
139
|
+
if (!descriptor) {
|
|
140
|
+
throw new Error(getErrorMessage('key descriptor', text[pos], text));
|
|
79
141
|
}
|
|
80
142
|
}
|
|
81
143
|
|
|
82
|
-
function
|
|
83
|
-
|
|
84
|
-
|
|
144
|
+
function getEnumValue(f, key) {
|
|
145
|
+
return f[key];
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function hasReleaseSelf(startBracket, descriptor, releaseSelfModifier, repeatModifier) {
|
|
149
|
+
if (releaseSelfModifier) {
|
|
150
|
+
return releaseSelfModifier === '/';
|
|
85
151
|
}
|
|
86
152
|
|
|
87
|
-
if (
|
|
153
|
+
if (repeatModifier) {
|
|
88
154
|
return false;
|
|
89
155
|
}
|
|
90
156
|
|
|
91
|
-
|
|
157
|
+
if (startBracket === '{' && getEnumValue(legacyModifiers, descriptor.toLowerCase())) {
|
|
158
|
+
return false;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return true;
|
|
92
162
|
}
|
|
93
163
|
|
|
94
164
|
function mapLegacyKey(descriptor) {
|
|
95
|
-
var
|
|
96
|
-
|
|
97
|
-
return (_ctrl$del$esc$space$d = {
|
|
98
|
-
ctrl: 'Control',
|
|
99
|
-
del: 'Delete',
|
|
100
|
-
esc: 'Escape',
|
|
101
|
-
space: ' '
|
|
102
|
-
}[descriptor]) != null ? _ctrl$del$esc$space$d : descriptor;
|
|
103
|
-
}
|
|
165
|
+
var _getEnumValue;
|
|
104
166
|
|
|
105
|
-
|
|
106
|
-
return !startBracket || startBracket === descriptor || startBracket === '{' && descriptor.length === 1;
|
|
167
|
+
return (_getEnumValue = getEnumValue(legacyKeyMap, descriptor)) != null ? _getEnumValue : descriptor;
|
|
107
168
|
}
|
|
108
169
|
|
|
109
170
|
function getErrorMessage(expected, found, text) {
|
|
@@ -21,6 +21,8 @@ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "functio
|
|
|
21
21
|
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
22
22
|
|
|
23
23
|
async function keyboardImplementation(text, options, state) {
|
|
24
|
+
var _state$repeatKey;
|
|
25
|
+
|
|
24
26
|
const {
|
|
25
27
|
document
|
|
26
28
|
} = options;
|
|
@@ -31,14 +33,16 @@ async function keyboardImplementation(text, options, state) {
|
|
|
31
33
|
keyDef,
|
|
32
34
|
consumedLength,
|
|
33
35
|
releasePrevious,
|
|
34
|
-
releaseSelf
|
|
35
|
-
|
|
36
|
+
releaseSelf,
|
|
37
|
+
repeat
|
|
38
|
+
} = (_state$repeatKey = state.repeatKey) != null ? _state$repeatKey : (0, _getNextKeyDef.getNextKeyDef)(text, options);
|
|
36
39
|
const replace = applyPlugins(plugins.replaceBehavior, keyDef, getCurrentElement(), options, state);
|
|
37
40
|
|
|
38
41
|
if (!replace) {
|
|
39
|
-
const pressed = state.pressed.find(p => p.keyDef === keyDef);
|
|
42
|
+
const pressed = state.pressed.find(p => p.keyDef === keyDef); // Release the key automatically if it was pressed before.
|
|
43
|
+
// Do not release the key on iterations on `state.repeatKey`.
|
|
40
44
|
|
|
41
|
-
if (pressed) {
|
|
45
|
+
if (pressed && !state.repeatKey) {
|
|
42
46
|
keyup(keyDef, getCurrentElement, options, state, pressed.unpreventedDefault);
|
|
43
47
|
}
|
|
44
48
|
|
|
@@ -47,15 +51,29 @@ async function keyboardImplementation(text, options, state) {
|
|
|
47
51
|
|
|
48
52
|
if (unpreventedDefault && hasKeyPress(keyDef, state)) {
|
|
49
53
|
keypress(keyDef, getCurrentElement, options, state);
|
|
50
|
-
}
|
|
54
|
+
} // Release the key only on the last iteration on `state.repeatKey`.
|
|
55
|
+
|
|
51
56
|
|
|
52
|
-
if (releaseSelf) {
|
|
57
|
+
if (releaseSelf && repeat <= 1) {
|
|
53
58
|
keyup(keyDef, getCurrentElement, options, state, unpreventedDefault);
|
|
54
59
|
}
|
|
55
60
|
}
|
|
56
61
|
}
|
|
57
62
|
|
|
58
|
-
if (
|
|
63
|
+
if (repeat > 1) {
|
|
64
|
+
state.repeatKey = {
|
|
65
|
+
// don't consume again on the next iteration
|
|
66
|
+
consumedLength: 0,
|
|
67
|
+
keyDef,
|
|
68
|
+
releasePrevious,
|
|
69
|
+
releaseSelf,
|
|
70
|
+
repeat: repeat - 1
|
|
71
|
+
};
|
|
72
|
+
} else {
|
|
73
|
+
delete state.repeatKey;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (text.length > consumedLength || repeat > 1) {
|
|
59
77
|
if (options.delay > 0) {
|
|
60
78
|
await (0, _utils.wait)(options.delay);
|
|
61
79
|
}
|
package/dist/keyboard/types.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { getNextKeyDef } from './getNextKeyDef';
|
|
1
2
|
/**
|
|
2
3
|
* @internal Do not create/alter this by yourself as this type might be subject to changes.
|
|
3
4
|
*/
|
|
@@ -35,6 +36,10 @@ export declare type keyboardState = {
|
|
|
35
36
|
E.g. ^1
|
|
36
37
|
*/
|
|
37
38
|
carryChar: string;
|
|
39
|
+
/**
|
|
40
|
+
Repeat keydown and keypress event
|
|
41
|
+
*/
|
|
42
|
+
repeatKey?: ReturnType<typeof getNextKeyDef>;
|
|
38
43
|
};
|
|
39
44
|
export declare type keyboardOptions = {
|
|
40
45
|
/** Document in which to perform the events */
|