keyboardImplementation.js
5.81 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
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.keyboardImplementation = keyboardImplementation;
exports.releaseAllKeys = releaseAllKeys;
var _dom = require("@testing-library/dom");
var _utils = require("../utils");
var _getNextKeyDef = require("./getNextKeyDef");
var plugins = _interopRequireWildcard(require("./plugins"));
var _getEventProps = require("./getEventProps");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
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; }
async function keyboardImplementation(text, options, state) {
var _state$repeatKey;
const {
document
} = options;
const getCurrentElement = () => getActive(document);
const {
keyDef,
consumedLength,
releasePrevious,
releaseSelf,
repeat
} = (_state$repeatKey = state.repeatKey) != null ? _state$repeatKey : (0, _getNextKeyDef.getNextKeyDef)(text, options);
const replace = applyPlugins(plugins.replaceBehavior, keyDef, getCurrentElement(), options, state);
if (!replace) {
const pressed = state.pressed.find(p => p.keyDef === keyDef); // Release the key automatically if it was pressed before.
// Do not release the key on iterations on `state.repeatKey`.
if (pressed && !state.repeatKey) {
keyup(keyDef, getCurrentElement, options, state, pressed.unpreventedDefault);
}
if (!releasePrevious) {
const unpreventedDefault = keydown(keyDef, getCurrentElement, options, state);
if (unpreventedDefault && hasKeyPress(keyDef, state)) {
keypress(keyDef, getCurrentElement, options, state);
} // Release the key only on the last iteration on `state.repeatKey`.
if (releaseSelf && repeat <= 1) {
keyup(keyDef, getCurrentElement, options, state, unpreventedDefault);
}
}
}
if (repeat > 1) {
state.repeatKey = {
// don't consume again on the next iteration
consumedLength: 0,
keyDef,
releasePrevious,
releaseSelf,
repeat: repeat - 1
};
} else {
delete state.repeatKey;
}
if (text.length > consumedLength || repeat > 1) {
if (options.delay > 0) {
await (0, _utils.wait)(options.delay);
}
return keyboardImplementation(text.slice(consumedLength), options, state);
}
return void undefined;
}
function getActive(document) {
var _getActiveElement;
return (_getActiveElement = (0, _utils.getActiveElement)(document)) != null ? _getActiveElement :
/* istanbul ignore next */
document.body;
}
function releaseAllKeys(options, state) {
const getCurrentElement = () => getActive(options.document);
for (const k of state.pressed) {
keyup(k.keyDef, getCurrentElement, options, state, k.unpreventedDefault);
}
}
function keydown(keyDef, getCurrentElement, options, state) {
const element = getCurrentElement(); // clear carried characters when focus is moved
if (element !== state.activeElement) {
state.carryValue = undefined;
state.carryChar = '';
}
state.activeElement = element;
applyPlugins(plugins.preKeydownBehavior, keyDef, element, options, state);
const unpreventedDefault = _dom.fireEvent.keyDown(element, (0, _getEventProps.getKeyEventProps)(keyDef, state));
state.pressed.push({
keyDef,
unpreventedDefault
});
if (unpreventedDefault) {
// all default behavior like keypress/submit etc is applied to the currentElement
applyPlugins(plugins.keydownBehavior, keyDef, getCurrentElement(), options, state);
}
return unpreventedDefault;
}
function keypress(keyDef, getCurrentElement, options, state) {
const element = getCurrentElement();
const unpreventedDefault = _dom.fireEvent.keyPress(element, (0, _getEventProps.getKeyEventProps)(keyDef, state));
if (unpreventedDefault) {
applyPlugins(plugins.keypressBehavior, keyDef, getCurrentElement(), options, state);
}
}
function keyup(keyDef, getCurrentElement, options, state, unprevented) {
const element = getCurrentElement();
applyPlugins(plugins.preKeyupBehavior, keyDef, element, options, state);
const unpreventedDefault = _dom.fireEvent.keyUp(element, (0, _getEventProps.getKeyEventProps)(keyDef, state));
if (unprevented && unpreventedDefault) {
applyPlugins(plugins.keyupBehavior, keyDef, getCurrentElement(), options, state);
}
state.pressed = state.pressed.filter(k => k.keyDef !== keyDef);
applyPlugins(plugins.postKeyupBehavior, keyDef, element, options, state);
}
function applyPlugins(pluginCollection, keyDef, element, options, state) {
const plugin = pluginCollection.find(p => p.matches(keyDef, element, options, state));
if (plugin) {
plugin.handle(keyDef, element, options, state);
}
return !!plugin;
}
function hasKeyPress(keyDef, state) {
var _keyDef$key;
return (((_keyDef$key = keyDef.key) == null ? void 0 : _keyDef$key.length) === 1 || keyDef.key === 'Enter') && !state.modifiers.ctrl && !state.modifiers.alt;
}