/
utils.js
196 lines (173 loc) Β· 5.06 KB
/
utils.js
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
import {getConfig} from '@testing-library/dom'
function isMousePressEvent(event) {
return (
event === 'mousedown' ||
event === 'mouseup' ||
event === 'click' ||
event === 'dblclick'
)
}
function invert(map) {
const res = {}
for (const key of Object.keys(map)) {
res[map[key]] = key
}
return res
}
// https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/buttons
const BUTTONS_TO_NAMES = {
0: 'none',
1: 'primary',
2: 'secondary',
4: 'auxiliary',
}
const NAMES_TO_BUTTONS = invert(BUTTONS_TO_NAMES)
// https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/button
const BUTTON_TO_NAMES = {
0: 'primary',
1: 'auxiliary',
2: 'secondary',
}
const NAMES_TO_BUTTON = invert(BUTTON_TO_NAMES)
function convertMouseButtons(event, init, property, mapping) {
if (!isMousePressEvent(event)) {
return 0
}
if (init[property] != null) {
return init[property]
}
if (init.buttons != null) {
// not sure how to test this. Feel free to try and add a test if you want.
// istanbul ignore next
return mapping[BUTTONS_TO_NAMES[init.buttons]] || 0
}
if (init.button != null) {
// not sure how to test this. Feel free to try and add a test if you want.
// istanbul ignore next
return mapping[BUTTON_TO_NAMES[init.button]] || 0
}
return property != 'button' && isMousePressEvent(event) ? 1 : 0
}
function getMouseEventOptions(event, init, clickCount = 0) {
init = init || {}
return {
...init,
// https://developer.mozilla.org/en-US/docs/Web/API/UIEvent/detail
detail:
event === 'mousedown' || event === 'mouseup' || event === 'click'
? 1 + clickCount
: clickCount,
buttons: convertMouseButtons(event, init, 'buttons', NAMES_TO_BUTTONS),
button: convertMouseButtons(event, init, 'button', NAMES_TO_BUTTON),
}
}
// Absolutely NO events fire on label elements that contain their control
// if that control is disabled. NUTS!
// no joke. There are NO events for: <label><input disabled /><label>
function isLabelWithInternallyDisabledControl(element) {
return (
element.tagName === 'LABEL' &&
element.control?.disabled &&
element.contains(element.control)
)
}
function getActiveElement(document) {
const activeElement = document.activeElement
if (activeElement?.shadowRoot) {
return getActiveElement(activeElement.shadowRoot)
} else {
return activeElement
}
}
function calculateNewValue(newEntry, element) {
const {selectionStart, selectionEnd, value} = element
// can't use .maxLength property because of a jsdom bug:
// https://github.com/jsdom/jsdom/issues/2927
const maxLength = Number(element.getAttribute('maxlength') ?? -1)
let newValue, newSelectionStart
if (selectionStart === null) {
// at the end of an input type that does not support selection ranges
// https://github.com/testing-library/user-event/issues/316#issuecomment-639744793
newValue = value + newEntry
} else if (selectionStart === selectionEnd) {
if (selectionStart === 0) {
// at the beginning of the input
newValue = newEntry + value
} else if (selectionStart === value.length) {
// at the end of the input
newValue = value + newEntry
} else {
// in the middle of the input
newValue =
value.slice(0, selectionStart) + newEntry + value.slice(selectionEnd)
}
newSelectionStart = selectionStart + newEntry.length
} else {
// we have something selected
const firstPart = value.slice(0, selectionStart) + newEntry
newValue = firstPart + value.slice(selectionEnd)
newSelectionStart = firstPart.length
}
if (maxLength < 0) {
return {newValue, newSelectionStart}
} else {
return {
newValue: newValue.slice(0, maxLength),
newSelectionStart:
newSelectionStart > maxLength ? maxLength : newSelectionStart,
}
}
}
function setSelectionRangeIfNecessary(
element,
newSelectionStart,
newSelectionEnd,
) {
const {selectionStart, selectionEnd} = element
if (!element.setSelectionRange || selectionStart === null) {
// cannot set selection
return
}
if (
selectionStart !== newSelectionStart ||
selectionEnd !== newSelectionStart
) {
element.setSelectionRange(newSelectionStart, newSelectionEnd)
}
}
const FOCUSABLE_SELECTOR = [
'input:not([disabled])',
'button:not([disabled])',
'select:not([disabled])',
'textarea:not([disabled])',
'a[href]',
'[tabindex]:not([disabled])',
].join(', ')
function isFocusable(element) {
return (
!isLabelWithInternallyDisabledControl(element) &&
element?.matches(FOCUSABLE_SELECTOR)
)
}
function wrapInEventWrapper(fn) {
function wrapper(...args) {
let result
getConfig().eventWrapper(() => {
result = fn(...args)
})
return result
}
// give it a helpful name for debugging
Object.defineProperty(wrapper, 'name', {value: `${fn.name}Wrapper`})
return wrapper
}
export {
FOCUSABLE_SELECTOR,
isFocusable,
getMouseEventOptions,
isLabelWithInternallyDisabledControl,
getActiveElement,
calculateNewValue,
setSelectionRangeIfNecessary,
wrapInEventWrapper,
}