-
Notifications
You must be signed in to change notification settings - Fork 239
/
getNextKeyDef.ts
147 lines (134 loc) Β· 3.76 KB
/
getNextKeyDef.ts
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
import {keyboardKey, keyboardOptions} from './types'
/**
* Get the next key from keyMap
*
* Keys can be referenced by `{key}` or `{special}` as well as physical locations per `[code]`.
* Everything else will be interpreted as a typed character - e.g. `a`.
* Brackets `{` and `[` can be escaped by doubling - e.g. `foo[[bar` translates to `foo[bar`.
* Keeping the key pressed can be written as `{key>}`.
* Modifiers like `{shift}` imply being kept pressed. This can be turned of per `{shift/}`.
*/
export function getNextKeyDef(
text: string,
options: keyboardOptions,
): {
keyDef: keyboardKey
consumedLength: number
releasePrevious: boolean
releaseSelf: boolean
} {
const startBracket = ['{', '['].includes(text[0]) ? text[0] : ''
const startModifier = startBracket && text[1] === '/' ? '/' : ''
const descriptorStart = startBracket.length + startModifier.length
const descriptor = startBracket
? text[descriptorStart] === startBracket
? startBracket
: text.slice(descriptorStart).match(/^\w+/)?.[0]
: text[descriptorStart]
if (!descriptor) {
throw new Error(
getErrorMessage('key descriptor', text[descriptorStart], text),
)
}
const descriptorEnd = descriptorStart + descriptor.length
const endModifier =
startBracket &&
descriptor !== startBracket &&
['/', '>'].includes(text[descriptorEnd])
? text[descriptorEnd]
: ''
const endBracket =
!startBracket || descriptor === startBracket
? ''
: startBracket === '{'
? '}'
: ']'
if (endBracket && text[descriptorEnd + endModifier.length] !== endBracket) {
throw new Error(
getErrorMessage(
'closing bracket',
text[descriptorEnd + endModifier.length],
text,
),
)
}
const modifiers = {
consumedLength: [
startBracket,
startModifier,
descriptor,
endModifier,
endBracket,
]
.map(c => c.length)
.reduce((a, b) => a + b),
releasePrevious: startModifier === '/',
releaseSelf: hasReleaseSelf(startBracket, descriptor, endModifier),
}
if (isPrintableCharacter(startBracket, descriptor)) {
return {
...modifiers,
keyDef: options.keyboardMap.find(k => k.key === descriptor) ?? {
key: descriptor,
code: 'Unknown',
},
}
} else if (startBracket === '{') {
const key = mapLegacyKey(descriptor)
return {
...modifiers,
keyDef: options.keyboardMap.find(
k => k.key?.toLowerCase() === key.toLowerCase(),
) ?? {key: descriptor, code: 'Unknown'},
}
} else {
return {
...modifiers,
keyDef: options.keyboardMap.find(
k => k.code?.toLowerCase() === descriptor.toLowerCase(),
) ?? {key: 'Unknown', code: descriptor},
}
}
}
function hasReleaseSelf(
startBracket: string,
descriptor: string,
endModifier: string,
) {
if (endModifier === '/' || !startBracket) {
return true
}
if (
startBracket === '{' &&
['alt', 'ctrl', 'meta', 'shift'].includes(descriptor.toLowerCase())
) {
return false
}
return endModifier !== '>'
}
function mapLegacyKey(descriptor: string) {
return (
{
ctrl: 'Control',
del: 'Delete',
esc: 'Escape',
space: ' ',
}[descriptor] ?? descriptor
)
}
function isPrintableCharacter(startBracket: string, descriptor: string) {
return (
!startBracket ||
startBracket === descriptor ||
(startBracket === '{' && descriptor.length === 1)
)
}
function getErrorMessage(
expected: string,
found: string | undefined,
text: string,
) {
return `Expected ${expected} but found "${found ?? ''}" in "${text}"
See https://github.com/testing-library/user-event/blob/master/README.md#keyboardtext-options
for more information about how userEvent parses your input.`
}