@testing-library/user-event 13.0.13 → 13.0.14
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/dist/keyboard/plugins/character.js +45 -34
- package/dist/keyboard/plugins/control.js +11 -7
- package/dist/keyboard/plugins/functional.js +5 -11
- package/dist/keyboard/shared/carryValue.d.ts +2 -0
- package/dist/keyboard/shared/carryValue.js +13 -0
- package/dist/keyboard/shared/{fireInputEventIfNeeded.d.ts → fireInputEvent.d.ts} +2 -5
- package/dist/keyboard/shared/{fireInputEventIfNeeded.js → fireInputEvent.js} +20 -36
- package/dist/keyboard/shared/index.d.ts +2 -1
- package/dist/keyboard/shared/index.js +17 -4
- package/dist/paste.js +21 -18
- package/dist/type/typeImplementation.js +1 -1
- package/dist/utils/edit/calculateNewValue.d.ts +2 -2
- package/dist/utils/edit/calculateNewValue.js +20 -74
- package/dist/utils/edit/cursorPosition.d.ts +2 -0
- package/dist/utils/edit/cursorPosition.js +35 -0
- package/dist/utils/edit/isContentEditable.d.ts +3 -1
- package/dist/utils/edit/isEditable.d.ts +24 -0
- package/dist/utils/edit/isEditable.js +40 -0
- package/dist/utils/edit/maxLength.d.ts +1 -0
- package/dist/utils/edit/maxLength.js +50 -0
- package/dist/utils/index.d.ts +3 -0
- package/dist/utils/index.js +39 -0
- package/package.json +1 -1
- package/dist/keyboard/plugins/control/calculateNewDeleteValue.d.ts +0 -4
- package/dist/keyboard/plugins/control/calculateNewDeleteValue.js +0 -46
- package/dist/keyboard/plugins/functional/calculateBackspaceValue.d.ts +0 -4
- package/dist/keyboard/plugins/functional/calculateBackspaceValue.js +0 -57
|
@@ -19,7 +19,8 @@ const keypressBehavior = [{
|
|
|
19
19
|
var _keyDef$key;
|
|
20
20
|
|
|
21
21
|
return ((_keyDef$key = keyDef.key) == null ? void 0 : _keyDef$key.length) === 1 && (0, _utils.isElementType)(element, 'input', {
|
|
22
|
-
type: 'time'
|
|
22
|
+
type: 'time',
|
|
23
|
+
readOnly: false
|
|
23
24
|
});
|
|
24
25
|
},
|
|
25
26
|
handle: (keyDef, element, options, state) => {
|
|
@@ -37,17 +38,20 @@ const keypressBehavior = [{
|
|
|
37
38
|
newValue,
|
|
38
39
|
newSelectionStart
|
|
39
40
|
} = (0, _utils.calculateNewValue)(newEntry, element);
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
41
|
+
const prevValue = (0, _utils.getValue)(element); // this check was provided by fireInputEventIfNeeded
|
|
42
|
+
// TODO: verify if it is even needed by this handler
|
|
43
|
+
|
|
44
|
+
if (prevValue !== newValue) {
|
|
45
|
+
(0, _shared.fireInputEvent)(element, {
|
|
46
|
+
newValue,
|
|
47
|
+
newSelectionStart,
|
|
48
|
+
eventOverrides: {
|
|
49
|
+
data: keyDef.key,
|
|
50
|
+
inputType: 'insertText'
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
51
55
|
(0, _shared.fireChangeForInputTimeIfValid)(element, prevValue, timeNewEntry);
|
|
52
56
|
state.carryValue = textToBeTyped;
|
|
53
57
|
}
|
|
@@ -56,7 +60,8 @@ const keypressBehavior = [{
|
|
|
56
60
|
var _keyDef$key2;
|
|
57
61
|
|
|
58
62
|
return ((_keyDef$key2 = keyDef.key) == null ? void 0 : _keyDef$key2.length) === 1 && (0, _utils.isElementType)(element, 'input', {
|
|
59
|
-
type: 'date'
|
|
63
|
+
type: 'date',
|
|
64
|
+
readOnly: false
|
|
60
65
|
});
|
|
61
66
|
},
|
|
62
67
|
handle: (keyDef, element, options, state) => {
|
|
@@ -74,15 +79,19 @@ const keypressBehavior = [{
|
|
|
74
79
|
newValue,
|
|
75
80
|
newSelectionStart
|
|
76
81
|
} = (0, _utils.calculateNewValue)(newEntry, element);
|
|
77
|
-
(0,
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
82
|
+
const prevValue = (0, _utils.getValue)(element); // this check was provided by fireInputEventIfNeeded
|
|
83
|
+
// TODO: verify if it is even needed by this handler
|
|
84
|
+
|
|
85
|
+
if (prevValue !== newValue) {
|
|
86
|
+
(0, _shared.fireInputEvent)(element, {
|
|
87
|
+
newValue,
|
|
88
|
+
newSelectionStart,
|
|
89
|
+
eventOverrides: {
|
|
90
|
+
data: keyDef.key,
|
|
91
|
+
inputType: 'insertText'
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
}
|
|
86
95
|
|
|
87
96
|
if (isValidToBeTyped) {
|
|
88
97
|
_dom.fireEvent.change(element, {
|
|
@@ -99,7 +108,8 @@ const keypressBehavior = [{
|
|
|
99
108
|
var _keyDef$key3;
|
|
100
109
|
|
|
101
110
|
return ((_keyDef$key3 = keyDef.key) == null ? void 0 : _keyDef$key3.length) === 1 && (0, _utils.isElementType)(element, 'input', {
|
|
102
|
-
type: 'number'
|
|
111
|
+
type: 'number',
|
|
112
|
+
readOnly: false
|
|
103
113
|
});
|
|
104
114
|
},
|
|
105
115
|
handle: (keyDef, element, options, state) => {
|
|
@@ -116,14 +126,13 @@ const keypressBehavior = [{
|
|
|
116
126
|
newValue,
|
|
117
127
|
newSelectionStart
|
|
118
128
|
} = (0, _utils.calculateNewValue)(keyDef.key, element, oldValue);
|
|
119
|
-
(0, _shared.
|
|
129
|
+
(0, _shared.fireInputEvent)(element, {
|
|
120
130
|
newValue,
|
|
121
131
|
newSelectionStart,
|
|
122
132
|
eventOverrides: {
|
|
123
133
|
data: keyDef.key,
|
|
124
134
|
inputType: 'insertText'
|
|
125
|
-
}
|
|
126
|
-
currentElement: () => element
|
|
135
|
+
}
|
|
127
136
|
});
|
|
128
137
|
const appliedValue = (0, _utils.getValue)(element);
|
|
129
138
|
|
|
@@ -137,38 +146,40 @@ const keypressBehavior = [{
|
|
|
137
146
|
matches: (keyDef, element) => {
|
|
138
147
|
var _keyDef$key4;
|
|
139
148
|
|
|
140
|
-
return ((_keyDef$key4 = keyDef.key) == null ? void 0 : _keyDef$key4.length) === 1 && ((0, _utils.isElementType)(element, ['input', 'textarea']
|
|
149
|
+
return ((_keyDef$key4 = keyDef.key) == null ? void 0 : _keyDef$key4.length) === 1 && ((0, _utils.isElementType)(element, ['input', 'textarea'], {
|
|
150
|
+
readOnly: false
|
|
151
|
+
}) && !(0, _utils.isClickableInput)(element) || (0, _utils.isContentEditable)(element)) && (0, _utils.getSpaceUntilMaxLength)(element) !== 0;
|
|
141
152
|
},
|
|
142
153
|
handle: (keyDef, element) => {
|
|
143
154
|
const {
|
|
144
155
|
newValue,
|
|
145
156
|
newSelectionStart
|
|
146
157
|
} = (0, _utils.calculateNewValue)(keyDef.key, element);
|
|
147
|
-
(0, _shared.
|
|
158
|
+
(0, _shared.fireInputEvent)(element, {
|
|
148
159
|
newValue,
|
|
149
160
|
newSelectionStart,
|
|
150
161
|
eventOverrides: {
|
|
151
162
|
data: keyDef.key,
|
|
152
163
|
inputType: 'insertText'
|
|
153
|
-
}
|
|
154
|
-
currentElement: () => element
|
|
164
|
+
}
|
|
155
165
|
});
|
|
156
166
|
}
|
|
157
167
|
}, {
|
|
158
|
-
matches: (keyDef, element) => keyDef.key === 'Enter' && ((0, _utils.isElementType)(element, 'textarea'
|
|
168
|
+
matches: (keyDef, element) => keyDef.key === 'Enter' && ((0, _utils.isElementType)(element, 'textarea', {
|
|
169
|
+
readOnly: false
|
|
170
|
+
}) || (0, _utils.isContentEditable)(element)) && (0, _utils.getSpaceUntilMaxLength)(element) !== 0,
|
|
159
171
|
handle: (keyDef, element, options, state) => {
|
|
160
172
|
const {
|
|
161
173
|
newValue,
|
|
162
174
|
newSelectionStart
|
|
163
175
|
} = (0, _utils.calculateNewValue)('\n', element);
|
|
164
176
|
const inputType = (0, _utils.isContentEditable)(element) && !state.modifiers.shift ? 'insertParagraph' : 'insertLineBreak';
|
|
165
|
-
(0, _shared.
|
|
177
|
+
(0, _shared.fireInputEvent)(element, {
|
|
166
178
|
newValue,
|
|
167
179
|
newSelectionStart,
|
|
168
180
|
eventOverrides: {
|
|
169
181
|
inputType
|
|
170
|
-
}
|
|
171
|
-
currentElement: () => element
|
|
182
|
+
}
|
|
172
183
|
});
|
|
173
184
|
}
|
|
174
185
|
}];
|
|
@@ -9,8 +9,6 @@ var _utils = require("../../utils");
|
|
|
9
9
|
|
|
10
10
|
var _shared = require("../shared");
|
|
11
11
|
|
|
12
|
-
var _calculateNewDeleteValue = require("./control/calculateNewDeleteValue");
|
|
13
|
-
|
|
14
12
|
/**
|
|
15
13
|
* This file should contain behavior for arrow keys as described here:
|
|
16
14
|
* https://w3c.github.io/uievents-code/#key-controlpad-section
|
|
@@ -31,14 +29,20 @@ const keydownBehavior = [{
|
|
|
31
29
|
}
|
|
32
30
|
}
|
|
33
31
|
}, {
|
|
34
|
-
matches: keyDef => keyDef.key === 'Delete',
|
|
35
|
-
handle: (keDef, element) => {
|
|
36
|
-
|
|
32
|
+
matches: (keyDef, element) => keyDef.key === 'Delete' && (0, _utils.isEditable)(element) && !(0, _utils.isCursorAtEnd)(element),
|
|
33
|
+
handle: (keDef, element, options, state) => {
|
|
34
|
+
const {
|
|
35
|
+
newValue,
|
|
36
|
+
newSelectionStart
|
|
37
|
+
} = (0, _utils.calculateNewValue)('', element, state.carryValue, undefined, 'forward');
|
|
38
|
+
(0, _shared.fireInputEvent)(element, {
|
|
39
|
+
newValue,
|
|
40
|
+
newSelectionStart,
|
|
37
41
|
eventOverrides: {
|
|
38
42
|
inputType: 'deleteContentForward'
|
|
39
|
-
}
|
|
40
|
-
currentElement: () => element
|
|
43
|
+
}
|
|
41
44
|
});
|
|
45
|
+
(0, _shared.carryValue)(element, state, newValue);
|
|
42
46
|
}
|
|
43
47
|
}];
|
|
44
48
|
exports.keydownBehavior = keydownBehavior;
|
|
@@ -13,8 +13,6 @@ var _getEventProps = require("../getEventProps");
|
|
|
13
13
|
|
|
14
14
|
var _shared = require("../shared");
|
|
15
15
|
|
|
16
|
-
var _calculateBackspaceValue = require("./functional/calculateBackspaceValue");
|
|
17
|
-
|
|
18
16
|
/**
|
|
19
17
|
* This file should contain behavior for functional keys as described here:
|
|
20
18
|
* https://w3c.github.io/uievents-code/#key-alphanumeric-functional
|
|
@@ -55,24 +53,20 @@ const keydownBehavior = [{
|
|
|
55
53
|
state.modifiers.caps = !state.modifiers.caps;
|
|
56
54
|
}
|
|
57
55
|
}, {
|
|
58
|
-
matches: keyDef => keyDef.key === 'Backspace',
|
|
56
|
+
matches: (keyDef, element) => keyDef.key === 'Backspace' && (0, _utils.isEditable)(element) && !(0, _utils.isCursorAtStart)(element),
|
|
59
57
|
handle: (keyDef, element, options, state) => {
|
|
60
58
|
const {
|
|
61
59
|
newValue,
|
|
62
60
|
newSelectionStart
|
|
63
|
-
} = (0,
|
|
64
|
-
(0, _shared.
|
|
61
|
+
} = (0, _utils.calculateNewValue)('', element, state.carryValue, undefined, 'backward');
|
|
62
|
+
(0, _shared.fireInputEvent)(element, {
|
|
65
63
|
newValue,
|
|
66
64
|
newSelectionStart,
|
|
67
65
|
eventOverrides: {
|
|
68
66
|
inputType: 'deleteContentBackward'
|
|
69
|
-
}
|
|
70
|
-
currentElement: () => element
|
|
67
|
+
}
|
|
71
68
|
});
|
|
72
|
-
|
|
73
|
-
if (state.carryValue) {
|
|
74
|
-
state.carryValue = (0, _utils.getValue)(element) === newValue ? undefined : newValue;
|
|
75
|
-
}
|
|
69
|
+
(0, _shared.carryValue)(element, state, newValue);
|
|
76
70
|
}
|
|
77
71
|
}];
|
|
78
72
|
exports.keydownBehavior = keydownBehavior;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.carryValue = carryValue;
|
|
7
|
+
|
|
8
|
+
var _utils = require("../../utils");
|
|
9
|
+
|
|
10
|
+
function carryValue(element, state, newValue) {
|
|
11
|
+
const value = (0, _utils.getValue)(element);
|
|
12
|
+
state.carryValue = value !== newValue && value === '' && (0, _utils.hasUnreliableEmptyValue)(element) ? newValue : undefined;
|
|
13
|
+
}
|
|
@@ -1,11 +1,8 @@
|
|
|
1
1
|
import { fireEvent } from '@testing-library/dom';
|
|
2
|
-
export declare function
|
|
3
|
-
currentElement: () => Element | null;
|
|
2
|
+
export declare function fireInputEvent(element: HTMLElement, { newValue, newSelectionStart, eventOverrides, }: {
|
|
4
3
|
newValue: string;
|
|
5
4
|
newSelectionStart: number;
|
|
6
5
|
eventOverrides: Partial<Parameters<typeof fireEvent>[1]> & {
|
|
7
6
|
[k: string]: unknown;
|
|
8
7
|
};
|
|
9
|
-
}):
|
|
10
|
-
prevValue: string | null;
|
|
11
|
-
};
|
|
8
|
+
}): void;
|
|
@@ -3,54 +3,38 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.
|
|
6
|
+
exports.fireInputEvent = fireInputEvent;
|
|
7
7
|
|
|
8
8
|
var _dom = require("@testing-library/dom");
|
|
9
9
|
|
|
10
10
|
var _utils = require("../../utils");
|
|
11
11
|
|
|
12
|
-
function
|
|
13
|
-
currentElement,
|
|
12
|
+
function fireInputEvent(element, {
|
|
14
13
|
newValue,
|
|
15
14
|
newSelectionStart,
|
|
16
15
|
eventOverrides
|
|
17
16
|
}) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
if ((0, _utils.
|
|
24
|
-
|
|
25
|
-
} else
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
setSelectionRangeAfterInput(el, newValue, newSelectionStart);
|
|
35
|
-
|
|
36
|
-
_dom.fireEvent.input(el, { ...eventOverrides
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
setSelectionRangeAfterInputHandler(el, newValue);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
return {
|
|
43
|
-
prevValue
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function isReadonly(element) {
|
|
48
|
-
return (0, _utils.isElementType)(element, ['input', 'textarea'], {
|
|
49
|
-
readOnly: true
|
|
17
|
+
// apply the changes before firing the input event, so that input handlers can access the altered dom and selection
|
|
18
|
+
if ((0, _utils.isContentEditable)(element)) {
|
|
19
|
+
element.textContent = newValue;
|
|
20
|
+
} else
|
|
21
|
+
/* istanbul ignore else */
|
|
22
|
+
if ((0, _utils.isElementType)(element, ['input', 'textarea'])) {
|
|
23
|
+
element.value = newValue;
|
|
24
|
+
} else {
|
|
25
|
+
// TODO: properly type guard
|
|
26
|
+
throw new Error('Invalid Element');
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
setSelectionRangeAfterInput(element, newSelectionStart);
|
|
30
|
+
|
|
31
|
+
_dom.fireEvent.input(element, { ...eventOverrides
|
|
50
32
|
});
|
|
33
|
+
|
|
34
|
+
setSelectionRangeAfterInputHandler(element, newValue);
|
|
51
35
|
}
|
|
52
36
|
|
|
53
|
-
function setSelectionRangeAfterInput(element,
|
|
37
|
+
function setSelectionRangeAfterInput(element, newSelectionStart) {
|
|
54
38
|
(0, _utils.setSelectionRange)(element, newSelectionStart, newSelectionStart);
|
|
55
39
|
}
|
|
56
40
|
|
|
@@ -4,6 +4,19 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
|
|
7
|
+
var _carryValue = require("./carryValue");
|
|
8
|
+
|
|
9
|
+
Object.keys(_carryValue).forEach(function (key) {
|
|
10
|
+
if (key === "default" || key === "__esModule") return;
|
|
11
|
+
if (key in exports && exports[key] === _carryValue[key]) return;
|
|
12
|
+
Object.defineProperty(exports, key, {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function () {
|
|
15
|
+
return _carryValue[key];
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
|
|
7
20
|
var _fireChangeForInputTimeIfValid = require("./fireChangeForInputTimeIfValid");
|
|
8
21
|
|
|
9
22
|
Object.keys(_fireChangeForInputTimeIfValid).forEach(function (key) {
|
|
@@ -17,15 +30,15 @@ Object.keys(_fireChangeForInputTimeIfValid).forEach(function (key) {
|
|
|
17
30
|
});
|
|
18
31
|
});
|
|
19
32
|
|
|
20
|
-
var
|
|
33
|
+
var _fireInputEvent = require("./fireInputEvent");
|
|
21
34
|
|
|
22
|
-
Object.keys(
|
|
35
|
+
Object.keys(_fireInputEvent).forEach(function (key) {
|
|
23
36
|
if (key === "default" || key === "__esModule") return;
|
|
24
|
-
if (key in exports && exports[key] ===
|
|
37
|
+
if (key in exports && exports[key] === _fireInputEvent[key]) return;
|
|
25
38
|
Object.defineProperty(exports, key, {
|
|
26
39
|
enumerable: true,
|
|
27
40
|
get: function () {
|
|
28
|
-
return
|
|
41
|
+
return _fireInputEvent[key];
|
|
29
42
|
}
|
|
30
43
|
});
|
|
31
44
|
});
|
package/dist/paste.js
CHANGED
|
@@ -37,23 +37,26 @@ function paste(element, text, init, {
|
|
|
37
37
|
|
|
38
38
|
_dom.fireEvent.paste(element, init);
|
|
39
39
|
|
|
40
|
-
if (
|
|
41
|
-
|
|
42
|
-
newValue,
|
|
43
|
-
newSelectionStart
|
|
44
|
-
} = (0, _utils.calculateNewValue)(text, element);
|
|
45
|
-
|
|
46
|
-
_dom.fireEvent.input(element, {
|
|
47
|
-
inputType: 'insertFromPaste',
|
|
48
|
-
target: {
|
|
49
|
-
value: newValue
|
|
50
|
-
}
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
(0, _utils.setSelectionRange)(element, // TODO: investigate why the selection caused by invalid parameters was expected
|
|
54
|
-
{
|
|
55
|
-
newSelectionStart,
|
|
56
|
-
selectionEnd: newSelectionStart
|
|
57
|
-
}, {});
|
|
40
|
+
if (element.readOnly) {
|
|
41
|
+
return;
|
|
58
42
|
}
|
|
43
|
+
|
|
44
|
+
text = text.substr(0, (0, _utils.getSpaceUntilMaxLength)(element));
|
|
45
|
+
const {
|
|
46
|
+
newValue,
|
|
47
|
+
newSelectionStart
|
|
48
|
+
} = (0, _utils.calculateNewValue)(text, element);
|
|
49
|
+
|
|
50
|
+
_dom.fireEvent.input(element, {
|
|
51
|
+
inputType: 'insertFromPaste',
|
|
52
|
+
target: {
|
|
53
|
+
value: newValue
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
(0, _utils.setSelectionRange)(element, // TODO: investigate why the selection caused by invalid parameters was expected
|
|
58
|
+
{
|
|
59
|
+
newSelectionStart,
|
|
60
|
+
selectionEnd: newSelectionStart
|
|
61
|
+
}, {});
|
|
59
62
|
}
|
|
@@ -39,7 +39,7 @@ async function typeImplementation(element, text, {
|
|
|
39
39
|
selectionEnd
|
|
40
40
|
} = (0, _utils.getSelectionRange)(element);
|
|
41
41
|
|
|
42
|
-
if (value != null && selectionStart === 0 && selectionEnd === 0) {
|
|
42
|
+
if (value != null && (selectionStart === null || selectionStart === 0) && (selectionEnd === null || selectionEnd === 0)) {
|
|
43
43
|
(0, _utils.setSelectionRange)(currentElement(), initialSelectionStart != null ? initialSelectionStart : value.length, initialSelectionEnd != null ? initialSelectionEnd : value.length);
|
|
44
44
|
}
|
|
45
45
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
export declare function calculateNewValue(newEntry: string, element: HTMLElement, value?: string
|
|
1
|
+
export declare function calculateNewValue(newEntry: string, element: HTMLElement, value?: string, selectionRange?: {
|
|
2
2
|
selectionStart: number | null;
|
|
3
3
|
selectionEnd: number | null;
|
|
4
|
-
}): {
|
|
4
|
+
}, deleteContent?: 'backward' | 'forward'): {
|
|
5
5
|
newValue: string;
|
|
6
6
|
newSelectionStart: number;
|
|
7
7
|
};
|
|
@@ -5,47 +5,29 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.calculateNewValue = calculateNewValue;
|
|
7
7
|
|
|
8
|
-
var _isElementType = require("../misc/isElementType");
|
|
9
|
-
|
|
10
8
|
var _selectionRange = require("./selectionRange");
|
|
11
9
|
|
|
12
|
-
var
|
|
10
|
+
var _getValue2 = require("./getValue");
|
|
13
11
|
|
|
14
12
|
var _isValidDateValue = require("./isValidDateValue");
|
|
15
13
|
|
|
16
14
|
var _isValidInputTimeValue = require("./isValidInputTimeValue");
|
|
17
15
|
|
|
18
|
-
function calculateNewValue(newEntry, element, value = (
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
newValue = `${newEntry}${value}`;
|
|
34
|
-
} else if (selectionStart === (value == null ? void 0 : value.length)) {
|
|
35
|
-
// at the end of the input
|
|
36
|
-
newValue = `${value}${newEntry}`;
|
|
37
|
-
} else {
|
|
38
|
-
// in the middle of the input
|
|
39
|
-
newValue = `${value == null ? void 0 : value.slice(0, selectionStart)}${newEntry}${value == null ? void 0 : value.slice(selectionEnd)}`;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
newSelectionStart = selectionStart + newEntry.length;
|
|
43
|
-
} else {
|
|
44
|
-
// we have something selected
|
|
45
|
-
const firstPart = `${value == null ? void 0 : value.slice(0, selectionStart)}${newEntry}`;
|
|
46
|
-
newValue = `${firstPart}${value == null ? void 0 : value.slice(selectionEnd)}`;
|
|
47
|
-
newSelectionStart = firstPart.length;
|
|
48
|
-
}
|
|
16
|
+
function calculateNewValue(newEntry, element, value = (() => {
|
|
17
|
+
var _getValue;
|
|
18
|
+
|
|
19
|
+
return (_getValue = (0, _getValue2.getValue)(element)) != null ? _getValue :
|
|
20
|
+
/* istanbul ignore next */
|
|
21
|
+
'';
|
|
22
|
+
})(), selectionRange = (0, _selectionRange.getSelectionRange)(element), deleteContent) {
|
|
23
|
+
const selectionStart = selectionRange.selectionStart === null ? value.length : selectionRange.selectionStart;
|
|
24
|
+
const selectionEnd = selectionRange.selectionEnd === null ? value.length : selectionRange.selectionEnd;
|
|
25
|
+
const prologEnd = Math.max(0, selectionStart === selectionEnd && deleteContent === 'backward' ? selectionStart - 1 : selectionStart);
|
|
26
|
+
const prolog = value.substring(0, prologEnd);
|
|
27
|
+
const epilogStart = Math.min(value.length, selectionStart === selectionEnd && deleteContent === 'forward' ? selectionEnd + 1 : selectionEnd);
|
|
28
|
+
const epilog = value.substring(epilogStart, value.length);
|
|
29
|
+
let newValue = `${prolog}${newEntry}${epilog}`;
|
|
30
|
+
const newSelectionStart = prologEnd + newEntry.length;
|
|
49
31
|
|
|
50
32
|
if (element.type === 'date' && !(0, _isValidDateValue.isValidDateValue)(element, newValue)) {
|
|
51
33
|
newValue = value;
|
|
@@ -57,46 +39,10 @@ function calculateNewValue(newEntry, element, value = (0, _getValue.getValue)(el
|
|
|
57
39
|
} else {
|
|
58
40
|
newValue = value;
|
|
59
41
|
}
|
|
60
|
-
} // can't use .maxLength property because of a jsdom bug:
|
|
61
|
-
// https://github.com/jsdom/jsdom/issues/2927
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
const maxLength = getSanitizedMaxLength(element);
|
|
65
|
-
|
|
66
|
-
if (maxLength === undefined) {
|
|
67
|
-
return {
|
|
68
|
-
newValue,
|
|
69
|
-
newSelectionStart
|
|
70
|
-
};
|
|
71
|
-
} else {
|
|
72
|
-
return {
|
|
73
|
-
newValue: newValue.slice(0, maxLength),
|
|
74
|
-
newSelectionStart: newSelectionStart > maxLength ? maxLength : newSelectionStart
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
function getSanitizedMaxLength(element) {
|
|
80
|
-
var _element$getAttribute;
|
|
81
|
-
|
|
82
|
-
if (!supportsMaxLength(element)) {
|
|
83
|
-
return undefined;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
const attr = (_element$getAttribute = element.getAttribute('maxlength')) != null ? _element$getAttribute : '';
|
|
87
|
-
return /^\d+$/.test(attr) && Number(attr) >= 0 ? Number(attr) : undefined;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
function supportsMaxLength(element) {
|
|
91
|
-
if ((0, _isElementType.isElementType)(element, 'textarea')) return true;
|
|
92
|
-
|
|
93
|
-
if ((0, _isElementType.isElementType)(element, 'input')) {
|
|
94
|
-
const type = element.getAttribute('type'); // Missing value default is "text"
|
|
95
|
-
|
|
96
|
-
if (!type) return true; // https://html.spec.whatwg.org/multipage/input.html#concept-input-apply
|
|
97
|
-
|
|
98
|
-
if (type.match(/email|password|search|telephone|text|url/)) return true;
|
|
99
42
|
}
|
|
100
43
|
|
|
101
|
-
return
|
|
44
|
+
return {
|
|
45
|
+
newValue,
|
|
46
|
+
newSelectionStart
|
|
47
|
+
};
|
|
102
48
|
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.isCursorAtEnd = isCursorAtEnd;
|
|
7
|
+
exports.isCursorAtStart = isCursorAtStart;
|
|
8
|
+
|
|
9
|
+
var _selectionRange = require("./selectionRange");
|
|
10
|
+
|
|
11
|
+
var _getValue2 = require("./getValue");
|
|
12
|
+
|
|
13
|
+
function isCursorAtEnd(element) {
|
|
14
|
+
var _getValue;
|
|
15
|
+
|
|
16
|
+
const {
|
|
17
|
+
selectionStart,
|
|
18
|
+
selectionEnd
|
|
19
|
+
} = (0, _selectionRange.getSelectionRange)(element);
|
|
20
|
+
return selectionStart === selectionEnd && (selectionStart != null ? selectionStart :
|
|
21
|
+
/* istanbul ignore next */
|
|
22
|
+
0) === ((_getValue = (0, _getValue2.getValue)(element)) != null ? _getValue :
|
|
23
|
+
/* istanbul ignore next */
|
|
24
|
+
'').length;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function isCursorAtStart(element) {
|
|
28
|
+
const {
|
|
29
|
+
selectionStart,
|
|
30
|
+
selectionEnd
|
|
31
|
+
} = (0, _selectionRange.getSelectionRange)(element);
|
|
32
|
+
return selectionStart === selectionEnd && (selectionStart != null ? selectionStart :
|
|
33
|
+
/* istanbul ignore next */
|
|
34
|
+
0) === 0;
|
|
35
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { isContentEditable } from './isContentEditable';
|
|
2
|
+
declare type GuardedType<T> = T extends (x: any) => x is (infer R) ? R : never;
|
|
3
|
+
export declare function isEditable(element: Element): element is GuardedType<typeof isContentEditable> | GuardedType<typeof isEditableInput> | HTMLTextAreaElement & {
|
|
4
|
+
readOnly: false;
|
|
5
|
+
};
|
|
6
|
+
declare enum editableInputTypes {
|
|
7
|
+
'text' = "text",
|
|
8
|
+
'date' = "date",
|
|
9
|
+
'datetime-local' = "datetime-local",
|
|
10
|
+
'email' = "email",
|
|
11
|
+
'month' = "month",
|
|
12
|
+
'number' = "number",
|
|
13
|
+
'password' = "password",
|
|
14
|
+
'search' = "search",
|
|
15
|
+
'tel' = "tel",
|
|
16
|
+
'time' = "time",
|
|
17
|
+
'url' = "url",
|
|
18
|
+
'week' = "week"
|
|
19
|
+
}
|
|
20
|
+
export declare function isEditableInput(element: Element): element is HTMLInputElement & {
|
|
21
|
+
readOnly: false;
|
|
22
|
+
type: editableInputTypes;
|
|
23
|
+
};
|
|
24
|
+
export {};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.isEditable = isEditable;
|
|
7
|
+
exports.isEditableInput = isEditableInput;
|
|
8
|
+
|
|
9
|
+
var _isElementType = require("../misc/isElementType");
|
|
10
|
+
|
|
11
|
+
var _isContentEditable = require("./isContentEditable");
|
|
12
|
+
|
|
13
|
+
function isEditable(element) {
|
|
14
|
+
return isEditableInput(element) || (0, _isElementType.isElementType)(element, 'textarea', {
|
|
15
|
+
readOnly: false
|
|
16
|
+
}) || (0, _isContentEditable.isContentEditable)(element);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
var editableInputTypes;
|
|
20
|
+
|
|
21
|
+
(function (editableInputTypes) {
|
|
22
|
+
editableInputTypes["text"] = "text";
|
|
23
|
+
editableInputTypes["date"] = "date";
|
|
24
|
+
editableInputTypes["datetime-local"] = "datetime-local";
|
|
25
|
+
editableInputTypes["email"] = "email";
|
|
26
|
+
editableInputTypes["month"] = "month";
|
|
27
|
+
editableInputTypes["number"] = "number";
|
|
28
|
+
editableInputTypes["password"] = "password";
|
|
29
|
+
editableInputTypes["search"] = "search";
|
|
30
|
+
editableInputTypes["tel"] = "tel";
|
|
31
|
+
editableInputTypes["time"] = "time";
|
|
32
|
+
editableInputTypes["url"] = "url";
|
|
33
|
+
editableInputTypes["week"] = "week";
|
|
34
|
+
})(editableInputTypes || (editableInputTypes = {}));
|
|
35
|
+
|
|
36
|
+
function isEditableInput(element) {
|
|
37
|
+
return (0, _isElementType.isElementType)(element, 'input', {
|
|
38
|
+
readOnly: false
|
|
39
|
+
}) && Boolean(editableInputTypes[element.type]);
|
|
40
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function getSpaceUntilMaxLength(element: Element): number | undefined;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.getSpaceUntilMaxLength = getSpaceUntilMaxLength;
|
|
7
|
+
|
|
8
|
+
var _isElementType = require("../misc/isElementType");
|
|
9
|
+
|
|
10
|
+
var _getValue = require("./getValue");
|
|
11
|
+
|
|
12
|
+
var maxLengthSupportedTypes;
|
|
13
|
+
|
|
14
|
+
(function (maxLengthSupportedTypes) {
|
|
15
|
+
maxLengthSupportedTypes["email"] = "email";
|
|
16
|
+
maxLengthSupportedTypes["password"] = "password";
|
|
17
|
+
maxLengthSupportedTypes["search"] = "search";
|
|
18
|
+
maxLengthSupportedTypes["telephone"] = "telephone";
|
|
19
|
+
maxLengthSupportedTypes["text"] = "text";
|
|
20
|
+
maxLengthSupportedTypes["url"] = "url";
|
|
21
|
+
})(maxLengthSupportedTypes || (maxLengthSupportedTypes = {}));
|
|
22
|
+
|
|
23
|
+
function getSpaceUntilMaxLength(element) {
|
|
24
|
+
const value = (0, _getValue.getValue)(element);
|
|
25
|
+
/* istanbul ignore if */
|
|
26
|
+
|
|
27
|
+
if (value === null) {
|
|
28
|
+
return undefined;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const maxLength = getSanitizedMaxLength(element);
|
|
32
|
+
return maxLength ? maxLength - value.length : undefined;
|
|
33
|
+
} // can't use .maxLength property because of a jsdom bug:
|
|
34
|
+
// https://github.com/jsdom/jsdom/issues/2927
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
function getSanitizedMaxLength(element) {
|
|
38
|
+
var _element$getAttribute;
|
|
39
|
+
|
|
40
|
+
if (!supportsMaxLength(element)) {
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const attr = (_element$getAttribute = element.getAttribute('maxlength')) != null ? _element$getAttribute : '';
|
|
45
|
+
return /^\d+$/.test(attr) && Number(attr) >= 0 ? Number(attr) : undefined;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function supportsMaxLength(element) {
|
|
49
|
+
return (0, _isElementType.isElementType)(element, 'textarea') || (0, _isElementType.isElementType)(element, 'input') && Boolean(maxLengthSupportedTypes[element.type]);
|
|
50
|
+
}
|
package/dist/utils/index.d.ts
CHANGED
|
@@ -2,11 +2,14 @@ export * from './click/getMouseEventOptions';
|
|
|
2
2
|
export * from './click/isClickableInput';
|
|
3
3
|
export * from './edit/buildTimeValue';
|
|
4
4
|
export * from './edit/calculateNewValue';
|
|
5
|
+
export * from './edit/cursorPosition';
|
|
5
6
|
export * from './edit/getValue';
|
|
6
7
|
export * from './edit/hasUnreliableEmptyValue';
|
|
7
8
|
export * from './edit/isContentEditable';
|
|
9
|
+
export * from './edit/isEditable';
|
|
8
10
|
export * from './edit/isValidDateValue';
|
|
9
11
|
export * from './edit/isValidInputTimeValue';
|
|
12
|
+
export * from './edit/maxLength';
|
|
10
13
|
export * from './edit/selectionRange';
|
|
11
14
|
export * from './focus/getActiveElement';
|
|
12
15
|
export * from './focus/isFocusable';
|
package/dist/utils/index.js
CHANGED
|
@@ -56,6 +56,19 @@ Object.keys(_calculateNewValue).forEach(function (key) {
|
|
|
56
56
|
});
|
|
57
57
|
});
|
|
58
58
|
|
|
59
|
+
var _cursorPosition = require("./edit/cursorPosition");
|
|
60
|
+
|
|
61
|
+
Object.keys(_cursorPosition).forEach(function (key) {
|
|
62
|
+
if (key === "default" || key === "__esModule") return;
|
|
63
|
+
if (key in exports && exports[key] === _cursorPosition[key]) return;
|
|
64
|
+
Object.defineProperty(exports, key, {
|
|
65
|
+
enumerable: true,
|
|
66
|
+
get: function () {
|
|
67
|
+
return _cursorPosition[key];
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
|
|
59
72
|
var _getValue = require("./edit/getValue");
|
|
60
73
|
|
|
61
74
|
Object.keys(_getValue).forEach(function (key) {
|
|
@@ -95,6 +108,19 @@ Object.keys(_isContentEditable).forEach(function (key) {
|
|
|
95
108
|
});
|
|
96
109
|
});
|
|
97
110
|
|
|
111
|
+
var _isEditable = require("./edit/isEditable");
|
|
112
|
+
|
|
113
|
+
Object.keys(_isEditable).forEach(function (key) {
|
|
114
|
+
if (key === "default" || key === "__esModule") return;
|
|
115
|
+
if (key in exports && exports[key] === _isEditable[key]) return;
|
|
116
|
+
Object.defineProperty(exports, key, {
|
|
117
|
+
enumerable: true,
|
|
118
|
+
get: function () {
|
|
119
|
+
return _isEditable[key];
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
|
|
98
124
|
var _isValidDateValue = require("./edit/isValidDateValue");
|
|
99
125
|
|
|
100
126
|
Object.keys(_isValidDateValue).forEach(function (key) {
|
|
@@ -121,6 +147,19 @@ Object.keys(_isValidInputTimeValue).forEach(function (key) {
|
|
|
121
147
|
});
|
|
122
148
|
});
|
|
123
149
|
|
|
150
|
+
var _maxLength = require("./edit/maxLength");
|
|
151
|
+
|
|
152
|
+
Object.keys(_maxLength).forEach(function (key) {
|
|
153
|
+
if (key === "default" || key === "__esModule") return;
|
|
154
|
+
if (key in exports && exports[key] === _maxLength[key]) return;
|
|
155
|
+
Object.defineProperty(exports, key, {
|
|
156
|
+
enumerable: true,
|
|
157
|
+
get: function () {
|
|
158
|
+
return _maxLength[key];
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
|
|
124
163
|
var _selectionRange = require("./edit/selectionRange");
|
|
125
164
|
|
|
126
165
|
Object.keys(_selectionRange).forEach(function (key) {
|
package/package.json
CHANGED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.calculateNewDeleteValue = calculateNewDeleteValue;
|
|
7
|
-
|
|
8
|
-
var _utils = require("../../../utils");
|
|
9
|
-
|
|
10
|
-
function calculateNewDeleteValue(element) {
|
|
11
|
-
var _getValue;
|
|
12
|
-
|
|
13
|
-
const {
|
|
14
|
-
selectionStart,
|
|
15
|
-
selectionEnd
|
|
16
|
-
} = (0, _utils.getSelectionRange)(element); // istanbul ignore next
|
|
17
|
-
|
|
18
|
-
const value = (_getValue = (0, _utils.getValue)(element)) != null ? _getValue : '';
|
|
19
|
-
let newValue;
|
|
20
|
-
|
|
21
|
-
if (selectionStart === null) {
|
|
22
|
-
// at the end of an input type that does not support selection ranges
|
|
23
|
-
// https://github.com/testing-library/user-event/issues/316#issuecomment-639744793
|
|
24
|
-
newValue = value;
|
|
25
|
-
} else if (selectionStart === selectionEnd) {
|
|
26
|
-
if (selectionStart === 0) {
|
|
27
|
-
// at the beginning of the input
|
|
28
|
-
newValue = value.slice(1);
|
|
29
|
-
} else if (selectionStart === value.length) {
|
|
30
|
-
// at the end of the input
|
|
31
|
-
newValue = value;
|
|
32
|
-
} else {
|
|
33
|
-
// in the middle of the input
|
|
34
|
-
newValue = value.slice(0, selectionStart) + value.slice(selectionEnd + 1);
|
|
35
|
-
}
|
|
36
|
-
} else {
|
|
37
|
-
// we have something selected
|
|
38
|
-
const firstPart = value.slice(0, selectionStart);
|
|
39
|
-
newValue = firstPart + value.slice(selectionEnd);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
return {
|
|
43
|
-
newValue,
|
|
44
|
-
newSelectionStart: selectionStart != null ? selectionStart : 0
|
|
45
|
-
};
|
|
46
|
-
}
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.calculateNewBackspaceValue = calculateNewBackspaceValue;
|
|
7
|
-
|
|
8
|
-
var _utils = require("../../../utils");
|
|
9
|
-
|
|
10
|
-
// yes, calculateNewBackspaceValue and calculateNewValue look extremely similar
|
|
11
|
-
// and you may be tempted to create a shared abstraction.
|
|
12
|
-
// If you, brave soul, decide to so endevor, please increment this count
|
|
13
|
-
// when you inevitably fail: 1
|
|
14
|
-
function calculateNewBackspaceValue(element, value = (() => {
|
|
15
|
-
var _getValue;
|
|
16
|
-
|
|
17
|
-
return (_getValue = (0, _utils.getValue)(element)) != null ? _getValue :
|
|
18
|
-
/* istanbul ignore next */
|
|
19
|
-
'';
|
|
20
|
-
})()) {
|
|
21
|
-
const {
|
|
22
|
-
selectionStart,
|
|
23
|
-
selectionEnd
|
|
24
|
-
} = (0, _utils.getSelectionRange)(element);
|
|
25
|
-
let newValue, newSelectionStart;
|
|
26
|
-
|
|
27
|
-
if (selectionStart === null) {
|
|
28
|
-
// at the end of an input type that does not support selection ranges
|
|
29
|
-
// https://github.com/testing-library/user-event/issues/316#issuecomment-639744793
|
|
30
|
-
newValue = value.slice(0, value.length - 1);
|
|
31
|
-
newSelectionStart = newValue.length;
|
|
32
|
-
} else if (selectionStart === selectionEnd) {
|
|
33
|
-
if (selectionStart === 0) {
|
|
34
|
-
// at the beginning of the input
|
|
35
|
-
newValue = value;
|
|
36
|
-
newSelectionStart = selectionStart;
|
|
37
|
-
} else if (selectionStart === value.length) {
|
|
38
|
-
// at the end of the input
|
|
39
|
-
newValue = value.slice(0, value.length - 1);
|
|
40
|
-
newSelectionStart = selectionStart - 1;
|
|
41
|
-
} else {
|
|
42
|
-
// in the middle of the input
|
|
43
|
-
newValue = value.slice(0, selectionStart - 1) + value.slice(selectionEnd);
|
|
44
|
-
newSelectionStart = selectionStart - 1;
|
|
45
|
-
}
|
|
46
|
-
} else {
|
|
47
|
-
// we have something selected
|
|
48
|
-
const firstPart = value.slice(0, selectionStart);
|
|
49
|
-
newValue = firstPart + value.slice(selectionEnd);
|
|
50
|
-
newSelectionStart = firstPart.length;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
return {
|
|
54
|
-
newValue,
|
|
55
|
-
newSelectionStart
|
|
56
|
-
};
|
|
57
|
-
}
|