@testing-library/user-event 14.0.0-alpha.1 → 14.0.0-alpha.13
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/{src/clear.d.ts → clear.d.ts} +0 -0
- package/dist/clear.js +14 -27
- package/dist/{src/click.d.ts → click.d.ts} +3 -2
- package/dist/click.js +23 -7
- package/dist/document/index.d.ts +10 -0
- package/dist/document/index.js +8 -10
- package/dist/document/interceptor.d.ts +15 -0
- package/dist/document/interceptor.js +7 -4
- package/dist/document/selection.d.ts +27 -0
- package/dist/document/selection.js +51 -28
- package/dist/{src/document → document}/value.d.ts +5 -1
- package/dist/document/value.js +27 -6
- package/dist/{src/hover.d.ts → hover.d.ts} +2 -2
- package/dist/hover.js +2 -2
- package/dist/{src/index.d.ts → index.d.ts} +0 -2
- package/dist/index.js +0 -8
- package/dist/{src/keyboard → keyboard}/getEventProps.d.ts +0 -2
- package/dist/keyboard/getEventProps.js +1 -7
- package/dist/{src/keyboard → keyboard}/getNextKeyDef.d.ts +0 -1
- package/dist/keyboard/getNextKeyDef.js +2 -32
- package/dist/{src/keyboard → keyboard}/index.d.ts +0 -1
- package/dist/keyboard/index.js +0 -8
- package/dist/{src/keyboard → keyboard}/keyMap.d.ts +0 -0
- package/dist/keyboard/keyMap.js +26 -46
- package/dist/{src/keyboard → keyboard}/keyboardImplementation.d.ts +0 -0
- package/dist/keyboard/keyboardImplementation.js +15 -17
- package/dist/{src/keyboard → keyboard}/plugins/arrow.d.ts +0 -0
- package/dist/keyboard/plugins/arrow.js +7 -12
- package/dist/{src/keyboard → keyboard}/plugins/character.d.ts +0 -0
- package/dist/keyboard/plugins/character.js +27 -45
- package/dist/keyboard/plugins/combination.d.ts +5 -0
- package/dist/keyboard/plugins/combination.js +17 -0
- package/dist/{src/keyboard → keyboard}/plugins/control.d.ts +0 -0
- package/dist/keyboard/plugins/control.js +4 -12
- package/dist/{src/keyboard → keyboard}/plugins/functional.d.ts +0 -0
- package/dist/keyboard/plugins/functional.js +23 -11
- package/dist/{src/keyboard → keyboard}/plugins/index.d.ts +0 -1
- package/dist/keyboard/plugins/index.js +4 -11
- package/dist/{src/keyboard → keyboard}/shared/fireChangeForInputTimeIfValid.d.ts +0 -0
- package/dist/{src/keyboard → keyboard}/shared/index.d.ts +0 -0
- package/dist/{src/keyboard → keyboard}/types.d.ts +0 -2
- package/dist/paste.d.ts +6 -0
- package/dist/paste.js +40 -43
- package/dist/{src/pointer → pointer}/index.d.ts +0 -0
- package/dist/{src/pointer → pointer}/keyMap.d.ts +0 -0
- package/dist/{src/pointer → pointer}/parseKeyDef.d.ts +0 -0
- package/dist/{src/pointer → pointer}/pointerAction.d.ts +2 -6
- package/dist/pointer/pointerAction.js +2 -26
- package/dist/pointer/pointerMove.d.ts +5 -0
- package/dist/pointer/pointerMove.js +49 -5
- package/dist/pointer/pointerPress.d.ts +7 -0
- package/dist/pointer/pointerPress.js +135 -29
- package/dist/pointer/resolveSelectionTarget.d.ts +5 -0
- package/dist/pointer/resolveSelectionTarget.js +75 -0
- package/dist/{src/pointer → pointer}/types.d.ts +18 -4
- package/dist/{src/selectOptions.d.ts → selectOptions.d.ts} +2 -2
- package/dist/selectOptions.js +25 -26
- package/dist/{src/setup.d.ts → setup.d.ts} +5 -2
- package/dist/setup.js +32 -21
- package/dist/tab.d.ts +5 -0
- package/dist/tab.js +3 -132
- package/dist/{src/type.d.ts → type.d.ts} +0 -0
- package/dist/type.js +3 -19
- package/dist/{src/upload.d.ts → upload.d.ts} +0 -1
- package/dist/upload.js +1 -1
- package/dist/{src/utils → utils}/click/isClickableInput.d.ts +0 -0
- package/dist/utils/dataTransfer/Blob.d.ts +1 -0
- package/dist/utils/dataTransfer/Blob.js +21 -0
- package/dist/utils/dataTransfer/Clipboard.d.ts +7 -0
- package/dist/utils/dataTransfer/Clipboard.js +143 -0
- package/dist/utils/dataTransfer/DataTransfer.d.ts +1 -0
- package/dist/utils/dataTransfer/DataTransfer.js +143 -0
- package/dist/utils/dataTransfer/FileList.d.ts +1 -0
- package/dist/utils/dataTransfer/FileList.js +13 -0
- package/dist/{src/utils → utils}/edit/buildTimeValue.d.ts +0 -0
- package/dist/utils/edit/calculateNewValue.d.ts +14 -0
- package/dist/utils/edit/calculateNewValue.js +19 -21
- package/dist/utils/edit/editInputElement.d.ts +16 -0
- package/dist/utils/edit/editInputElement.js +53 -0
- package/dist/{src/utils → utils}/edit/getValue.d.ts +0 -0
- package/dist/utils/edit/getValue.js +1 -3
- package/dist/utils/edit/isContentEditable.d.ts +7 -0
- package/dist/utils/edit/isContentEditable.js +14 -0
- package/dist/{src/utils → utils}/edit/isEditable.d.ts +1 -0
- package/dist/{src/utils → utils}/edit/isValidDateValue.d.ts +0 -0
- package/dist/{src/utils → utils}/edit/isValidInputTimeValue.d.ts +0 -0
- package/dist/{src/utils → utils}/edit/maxLength.d.ts +0 -0
- package/dist/utils/edit/prepareInput.d.ts +7 -0
- package/dist/utils/edit/prepareInput.js +77 -0
- package/dist/{src/utils → utils}/focus/blur.d.ts +0 -0
- package/dist/{src/utils → utils}/focus/focus.d.ts +0 -0
- package/dist/utils/focus/focus.js +3 -0
- package/dist/{src/utils → utils}/focus/getActiveElement.d.ts +0 -0
- package/dist/utils/focus/getTabDestination.d.ts +1 -0
- package/dist/utils/focus/getTabDestination.js +71 -0
- package/dist/{src/utils → utils}/focus/isFocusable.d.ts +0 -0
- package/dist/utils/focus/selectAll.d.ts +5 -0
- package/dist/utils/focus/selectAll.js +51 -0
- package/dist/utils/focus/selection.d.ts +42 -0
- package/dist/utils/focus/selection.js +190 -0
- package/dist/{src/utils → utils}/focus/selector.d.ts +0 -0
- package/dist/{src/utils → utils}/index.d.ts +9 -5
- package/dist/utils/index.js +98 -46
- package/dist/{src/utils → utils}/keyDef/readNextDescriptor.d.ts +0 -0
- package/dist/{src/utils → utils}/misc/eventWrapper.d.ts +0 -0
- package/dist/{src/utils → utils}/misc/findClosest.d.ts +0 -0
- package/dist/{src/utils → utils}/misc/hasFormSubmit.d.ts +0 -0
- package/dist/{src/utils → utils}/misc/hasPointerEvents.d.ts +0 -0
- package/dist/{src/utils → utils}/misc/isDescendantOrSelf.d.ts +0 -0
- package/dist/{src/utils → utils}/misc/isDisabled.d.ts +0 -0
- package/dist/{src/utils → utils}/misc/isElementType.d.ts +0 -0
- package/dist/{src/utils → utils}/misc/isVisible.d.ts +0 -0
- package/dist/{src/utils → utils}/misc/wait.d.ts +0 -0
- package/dist/{src/utils → utils}/pointer/firePointerEvents.d.ts +13 -11
- package/dist/utils/pointer/firePointerEvents.js +62 -5
- package/dist/{src/utils → utils}/pointer/mouseButtons.d.ts +0 -0
- package/package.json +2 -2
- package/dist/document/applyNative.js +0 -28
- package/dist/keyboard/specialCharMap.js +0 -30
- package/dist/src/document/applyNative.d.ts +0 -8
- package/dist/src/document/index.d.ts +0 -10
- package/dist/src/document/interceptor.d.ts +0 -5
- package/dist/src/document/selection.d.ts +0 -18
- package/dist/src/keyboard/specialCharMap.d.ts +0 -22
- package/dist/src/paste.d.ts +0 -7
- package/dist/src/pointer/pointerMove.d.ts +0 -5
- package/dist/src/pointer/pointerPress.d.ts +0 -7
- package/dist/src/tab.d.ts +0 -6
- package/dist/src/utils/edit/calculateNewValue.d.ts +0 -7
- package/dist/src/utils/edit/cursorPosition.d.ts +0 -2
- package/dist/src/utils/edit/fireInputEvent.d.ts +0 -8
- package/dist/src/utils/edit/isContentEditable.d.ts +0 -3
- package/dist/src/utils/edit/selectionRange.d.ts +0 -5
- package/dist/src/utils/misc/isDocument.d.ts +0 -1
- package/dist/src/utils/pointer/fakeEvent.d.ts +0 -16
- package/dist/tests/_helpers/custom-element.d.ts +0 -8
- package/dist/tests/_helpers/utils.d.ts +0 -73
- package/dist/tests/_setup-env.d.ts +0 -1
- package/dist/tests/clear.d.ts +0 -1
- package/dist/tests/click/click.d.ts +0 -1
- package/dist/tests/click/dblclick.d.ts +0 -1
- package/dist/tests/document/index.d.ts +0 -1
- package/dist/tests/document/selectionRange.d.ts +0 -1
- package/dist/tests/hover/hover.d.ts +0 -1
- package/dist/tests/hover/unhover.d.ts +0 -1
- package/dist/tests/keyboard/getNextKeyDef.d.ts +0 -1
- package/dist/tests/keyboard/index.d.ts +0 -1
- package/dist/tests/keyboard/keyboardImplementation.d.ts +0 -1
- package/dist/tests/keyboard/plugin/arrow.d.ts +0 -1
- package/dist/tests/keyboard/plugin/character.d.ts +0 -1
- package/dist/tests/keyboard/plugin/control.d.ts +0 -1
- package/dist/tests/keyboard/plugin/functional.d.ts +0 -1
- package/dist/tests/keyboard/shared/fireInputEvent.d.ts +0 -1
- package/dist/tests/paste.d.ts +0 -1
- package/dist/tests/pointer/index.d.ts +0 -1
- package/dist/tests/react/keyboard.d.ts +0 -1
- package/dist/tests/react/type.d.ts +0 -1
- package/dist/tests/selectOptions/deselect.d.ts +0 -1
- package/dist/tests/selectOptions/select.d.ts +0 -1
- package/dist/tests/setup.d.ts +0 -10
- package/dist/tests/tab.d.ts +0 -1
- package/dist/tests/type/index.d.ts +0 -1
- package/dist/tests/type/modifiers.d.ts +0 -1
- package/dist/tests/upload.d.ts +0 -1
- package/dist/tests/utils/edit/calculateNewValue.d.ts +0 -1
- package/dist/tests/utils/edit/isContentEditable.d.ts +0 -1
- package/dist/tests/utils/focus/blur.d.ts +0 -1
- package/dist/tests/utils/focus/focus.d.ts +0 -1
- package/dist/tests/utils/misc/hasPointerEvents.d.ts +0 -1
- package/dist/tests/utils/misc/isDescendantOrSelf.d.ts +0 -1
- package/dist/tests/utils/misc/isElementType.d.ts +0 -1
- package/dist/tests/utils/misc/isVisible.d.ts +0 -1
- package/dist/utils/edit/cursorPosition.js +0 -35
- package/dist/utils/edit/fireInputEvent.js +0 -55
- package/dist/utils/edit/selectionRange.js +0 -64
- package/dist/utils/misc/dom-helpers.d.ts +0 -3
- package/dist/utils/misc/isDocument.js +0 -10
- package/dist/utils/pointer/fakeEvent.js +0 -82
|
@@ -1,10 +1,6 @@
|
|
|
1
|
-
import { Coords } from '../utils';
|
|
2
1
|
import { PointerMoveAction } from './pointerMove';
|
|
3
2
|
import { PointerPressAction } from './pointerPress';
|
|
4
|
-
import { inputDeviceState, pointerOptions } from './types';
|
|
5
|
-
export declare type PointerActionTarget =
|
|
6
|
-
target?: Element;
|
|
7
|
-
coords?: Partial<Coords>;
|
|
8
|
-
};
|
|
3
|
+
import { inputDeviceState, pointerOptions, PointerTarget, SelectionTarget } from './types';
|
|
4
|
+
export declare type PointerActionTarget = Partial<PointerTarget> & Partial<SelectionTarget>;
|
|
9
5
|
export declare type PointerAction = PointerActionTarget & (Omit<PointerPressAction, 'target' | 'coords'> | Omit<PointerMoveAction, 'target' | 'coords'>);
|
|
10
6
|
export declare function pointerAction(actions: PointerAction[], options: pointerOptions, state: inputDeviceState): Promise<unknown[]>;
|
|
@@ -15,14 +15,12 @@ async function pointerAction(actions, options, state) {
|
|
|
15
15
|
const ret = [];
|
|
16
16
|
|
|
17
17
|
for (let i = 0; i < actions.length; i++) {
|
|
18
|
-
var _action$target;
|
|
18
|
+
var _action$target, _action$coords;
|
|
19
19
|
|
|
20
20
|
const action = actions[i];
|
|
21
21
|
const pointerName = 'pointerName' in action && action.pointerName ? action.pointerName : 'keyDef' in action ? action.keyDef.pointerType === 'touch' ? action.keyDef.name : action.keyDef.pointerType : 'mouse';
|
|
22
22
|
const target = (_action$target = action.target) != null ? _action$target : getPrevTarget(pointerName, state.pointerState);
|
|
23
|
-
const coords =
|
|
24
|
-
...action.coords
|
|
25
|
-
});
|
|
23
|
+
const coords = (_action$coords = action.coords) != null ? _action$coords : pointerName in state.pointerState.position ? state.pointerState.position[pointerName].coords : undefined;
|
|
26
24
|
const promise = 'keyDef' in action ? (0, _pointerPress.pointerPress)({ ...action,
|
|
27
25
|
target,
|
|
28
26
|
coords
|
|
@@ -51,26 +49,4 @@ function getPrevTarget(pointerName, state) {
|
|
|
51
49
|
}
|
|
52
50
|
|
|
53
51
|
return state.position[pointerName].target;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
function completeCoords({
|
|
57
|
-
x = 0,
|
|
58
|
-
y = 0,
|
|
59
|
-
clientX = x,
|
|
60
|
-
clientY = y,
|
|
61
|
-
offsetX = x,
|
|
62
|
-
offsetY = y,
|
|
63
|
-
pageX = clientX,
|
|
64
|
-
pageY = clientY
|
|
65
|
-
}) {
|
|
66
|
-
return {
|
|
67
|
-
x,
|
|
68
|
-
y,
|
|
69
|
-
clientX,
|
|
70
|
-
clientY,
|
|
71
|
-
offsetX,
|
|
72
|
-
offsetY,
|
|
73
|
-
pageX,
|
|
74
|
-
pageY
|
|
75
|
-
};
|
|
76
52
|
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { inputDeviceState, PointerTarget, SelectionTarget } from './types';
|
|
2
|
+
export interface PointerMoveAction extends PointerTarget, SelectionTarget {
|
|
3
|
+
pointerName?: string;
|
|
4
|
+
}
|
|
5
|
+
export declare function pointerMove({ pointerName, target, coords, node, offset }: PointerMoveAction, { pointerState, keyboardState }: inputDeviceState): Promise<void>;
|
|
@@ -5,12 +5,18 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.pointerMove = pointerMove;
|
|
7
7
|
|
|
8
|
+
var _document = require("../document");
|
|
9
|
+
|
|
8
10
|
var _utils = require("../utils");
|
|
9
11
|
|
|
12
|
+
var _resolveSelectionTarget = require("./resolveSelectionTarget");
|
|
13
|
+
|
|
10
14
|
async function pointerMove({
|
|
11
15
|
pointerName = 'mouse',
|
|
12
16
|
target,
|
|
13
|
-
coords
|
|
17
|
+
coords,
|
|
18
|
+
node,
|
|
19
|
+
offset
|
|
14
20
|
}, {
|
|
15
21
|
pointerState,
|
|
16
22
|
keyboardState
|
|
@@ -23,7 +29,8 @@ async function pointerMove({
|
|
|
23
29
|
pointerId,
|
|
24
30
|
pointerType,
|
|
25
31
|
target: prevTarget,
|
|
26
|
-
coords: prevCoords
|
|
32
|
+
coords: prevCoords,
|
|
33
|
+
selectionRange
|
|
27
34
|
} = pointerState.position[pointerName];
|
|
28
35
|
|
|
29
36
|
if (prevTarget && prevTarget !== target) {
|
|
@@ -35,9 +42,7 @@ async function pointerMove({
|
|
|
35
42
|
}
|
|
36
43
|
}
|
|
37
44
|
|
|
38
|
-
pointerState.position[pointerName] = {
|
|
39
|
-
pointerId,
|
|
40
|
-
pointerType,
|
|
45
|
+
pointerState.position[pointerName] = { ...pointerState.position[pointerName],
|
|
41
46
|
target,
|
|
42
47
|
coords
|
|
43
48
|
};
|
|
@@ -52,6 +57,45 @@ async function pointerMove({
|
|
|
52
57
|
|
|
53
58
|
fireMove(target, coords);
|
|
54
59
|
|
|
60
|
+
if (selectionRange) {
|
|
61
|
+
// TODO: support extending range (shift)
|
|
62
|
+
const selectionFocus = (0, _resolveSelectionTarget.resolveSelectionTarget)({
|
|
63
|
+
target,
|
|
64
|
+
node,
|
|
65
|
+
offset
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
if ('node' in selectionRange) {
|
|
69
|
+
// When the mouse is dragged outside of an input/textarea,
|
|
70
|
+
// the selection is extended to the beginning or end of the input
|
|
71
|
+
// depending on pointer position.
|
|
72
|
+
// TODO: extend selection according to pointer position
|
|
73
|
+
|
|
74
|
+
/* istanbul ignore else */
|
|
75
|
+
if (selectionFocus.node === selectionRange.node) {
|
|
76
|
+
const anchorOffset = selectionFocus.offset < selectionRange.start ? selectionRange.end : selectionRange.start;
|
|
77
|
+
const focusOffset = selectionFocus.offset > selectionRange.end || selectionFocus.offset < selectionRange.start ? selectionFocus.offset : selectionRange.end;
|
|
78
|
+
(0, _document.setUISelection)(selectionRange.node, {
|
|
79
|
+
anchorOffset,
|
|
80
|
+
focusOffset
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
} else {
|
|
84
|
+
const range = selectionRange.cloneRange();
|
|
85
|
+
const cmp = range.comparePoint(selectionFocus.node, selectionFocus.offset);
|
|
86
|
+
|
|
87
|
+
if (cmp < 0) {
|
|
88
|
+
range.setStart(selectionFocus.node, selectionFocus.offset);
|
|
89
|
+
} else if (cmp > 0) {
|
|
90
|
+
range.setEnd(selectionFocus.node, selectionFocus.offset);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const selection = target.ownerDocument.getSelection();
|
|
94
|
+
selection.removeAllRanges();
|
|
95
|
+
selection.addRange(range.cloneRange());
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
55
99
|
function fireMove(eventTarget, eventCoords) {
|
|
56
100
|
fire(eventTarget, 'pointermove', eventCoords);
|
|
57
101
|
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { inputDeviceState, pointerKey, PointerTarget, SelectionTarget } from './types';
|
|
2
|
+
export interface PointerPressAction extends PointerTarget, SelectionTarget {
|
|
3
|
+
keyDef: pointerKey;
|
|
4
|
+
releasePrevious: boolean;
|
|
5
|
+
releaseSelf: boolean;
|
|
6
|
+
}
|
|
7
|
+
export declare function pointerPress(action: PointerPressAction, state: inputDeviceState): Promise<void>;
|
|
@@ -9,27 +9,31 @@ var _dom = require("@testing-library/dom");
|
|
|
9
9
|
|
|
10
10
|
var _utils = require("../utils");
|
|
11
11
|
|
|
12
|
+
var _document = require("../document");
|
|
13
|
+
|
|
14
|
+
var _resolveSelectionTarget = require("./resolveSelectionTarget");
|
|
15
|
+
|
|
12
16
|
/* eslint-disable complexity */
|
|
13
|
-
async function pointerPress({
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}
|
|
17
|
+
async function pointerPress(action, state) {
|
|
18
|
+
const {
|
|
19
|
+
keyDef,
|
|
20
|
+
target,
|
|
21
|
+
releasePrevious,
|
|
22
|
+
releaseSelf
|
|
23
|
+
} = action;
|
|
20
24
|
const previous = state.pointerState.pressed.find(p => p.keyDef === keyDef);
|
|
21
25
|
const pointerName = keyDef.pointerType === 'touch' ? keyDef.name : keyDef.pointerType;
|
|
22
26
|
const targetIsDisabled = (0, _utils.isDisabled)(target);
|
|
23
27
|
|
|
24
28
|
if (previous) {
|
|
25
|
-
up(state, pointerName,
|
|
29
|
+
up(state, pointerName, action, previous, targetIsDisabled);
|
|
26
30
|
}
|
|
27
31
|
|
|
28
32
|
if (!releasePrevious) {
|
|
29
|
-
const press = down(state, pointerName,
|
|
33
|
+
const press = down(state, pointerName, action, targetIsDisabled);
|
|
30
34
|
|
|
31
35
|
if (releaseSelf) {
|
|
32
|
-
up(state, pointerName,
|
|
36
|
+
up(state, pointerName, action, press, targetIsDisabled);
|
|
33
37
|
}
|
|
34
38
|
}
|
|
35
39
|
}
|
|
@@ -42,7 +46,13 @@ function getNextPointerId(state) {
|
|
|
42
46
|
function down({
|
|
43
47
|
pointerState,
|
|
44
48
|
keyboardState
|
|
45
|
-
}, pointerName,
|
|
49
|
+
}, pointerName, {
|
|
50
|
+
keyDef,
|
|
51
|
+
node,
|
|
52
|
+
offset,
|
|
53
|
+
target,
|
|
54
|
+
coords
|
|
55
|
+
}, targetIsDisabled) {
|
|
46
56
|
var _pointerState$activeC, _pointerState$activeC2, _pointerState$activeC3;
|
|
47
57
|
|
|
48
58
|
const {
|
|
@@ -51,7 +61,7 @@ function down({
|
|
|
51
61
|
button
|
|
52
62
|
} = keyDef;
|
|
53
63
|
const pointerId = pointerType === 'mouse' ? 1 : getNextPointerId(pointerState);
|
|
54
|
-
pointerState.position[pointerName] = {
|
|
64
|
+
pointerState.position[pointerName] = { ...pointerState.position[pointerName],
|
|
55
65
|
pointerId,
|
|
56
66
|
pointerType,
|
|
57
67
|
target,
|
|
@@ -106,9 +116,14 @@ function down({
|
|
|
106
116
|
}
|
|
107
117
|
|
|
108
118
|
if (pointerType === 'mouse' && pressObj.unpreventedDefault) {
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
119
|
+
mousedownDefaultBehavior({
|
|
120
|
+
target,
|
|
121
|
+
targetIsDisabled,
|
|
122
|
+
clickCount,
|
|
123
|
+
position: pointerState.position[pointerName],
|
|
124
|
+
node,
|
|
125
|
+
offset
|
|
126
|
+
});
|
|
112
127
|
}
|
|
113
128
|
|
|
114
129
|
return pressObj;
|
|
@@ -131,9 +146,15 @@ function up({
|
|
|
131
146
|
pointerState,
|
|
132
147
|
keyboardState
|
|
133
148
|
}, pointerName, {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
149
|
+
keyDef: {
|
|
150
|
+
pointerType,
|
|
151
|
+
button
|
|
152
|
+
},
|
|
153
|
+
target,
|
|
154
|
+
coords,
|
|
155
|
+
node,
|
|
156
|
+
offset
|
|
157
|
+
}, pressed, targetIsDisabled) {
|
|
137
158
|
pointerState.pressed = pointerState.pressed.filter(p => p !== pressed);
|
|
138
159
|
const {
|
|
139
160
|
isMultiTouch,
|
|
@@ -144,9 +165,7 @@ function up({
|
|
|
144
165
|
let {
|
|
145
166
|
unpreventedDefault
|
|
146
167
|
} = pressed;
|
|
147
|
-
pointerState.position[pointerName] = {
|
|
148
|
-
pointerId,
|
|
149
|
-
pointerType,
|
|
168
|
+
pointerState.position[pointerName] = { ...pointerState.position[pointerName],
|
|
150
169
|
target,
|
|
151
170
|
coords
|
|
152
171
|
}; // TODO: pointerleave for touch device
|
|
@@ -173,14 +192,18 @@ function up({
|
|
|
173
192
|
}
|
|
174
193
|
|
|
175
194
|
if (unpreventedDefault && pointerType !== 'mouse' && !isMultiTouch) {
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
195
|
+
mousedownDefaultBehavior({
|
|
196
|
+
target,
|
|
197
|
+
targetIsDisabled,
|
|
198
|
+
clickCount,
|
|
199
|
+
position: pointerState.position[pointerName],
|
|
200
|
+
node,
|
|
201
|
+
offset
|
|
202
|
+
});
|
|
182
203
|
}
|
|
183
204
|
|
|
205
|
+
delete pointerState.position[pointerName].selectionRange;
|
|
206
|
+
|
|
184
207
|
if (!targetIsDisabled) {
|
|
185
208
|
if (pointerType === 'mouse' || !isMultiTouch) {
|
|
186
209
|
unpreventedDefault = fire('mouseup') && unpreventedDefault;
|
|
@@ -197,9 +220,9 @@ function up({
|
|
|
197
220
|
const label = target.closest('label');
|
|
198
221
|
|
|
199
222
|
if (label != null && label.control) {
|
|
200
|
-
var
|
|
223
|
+
var _findClosest;
|
|
201
224
|
|
|
202
|
-
(0, _utils.focus)((
|
|
225
|
+
(0, _utils.focus)((_findClosest = (0, _utils.findClosest)(label.control, _utils.isFocusable)) != null ? _findClosest : target.ownerDocument.body);
|
|
203
226
|
}
|
|
204
227
|
}
|
|
205
228
|
|
|
@@ -221,4 +244,87 @@ function up({
|
|
|
221
244
|
pointerType
|
|
222
245
|
});
|
|
223
246
|
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function mousedownDefaultBehavior({
|
|
250
|
+
position,
|
|
251
|
+
target,
|
|
252
|
+
targetIsDisabled,
|
|
253
|
+
clickCount,
|
|
254
|
+
node,
|
|
255
|
+
offset
|
|
256
|
+
}) {
|
|
257
|
+
var _findClosest2;
|
|
258
|
+
|
|
259
|
+
// The closest focusable element is focused when a `mousedown` would have been fired.
|
|
260
|
+
// Even if there was no `mousedown` because the element was disabled.
|
|
261
|
+
// A `mousedown` that preventsDefault cancels this though.
|
|
262
|
+
(0, _utils.focus)((_findClosest2 = (0, _utils.findClosest)(target, _utils.isFocusable)) != null ? _findClosest2 : target.ownerDocument.body); // TODO: What happens if a focus event handler interfers?
|
|
263
|
+
// An unprevented mousedown moves the cursor to the closest character.
|
|
264
|
+
// We try to approximate the behavior for a no-layout environment.
|
|
265
|
+
|
|
266
|
+
if (!targetIsDisabled) {
|
|
267
|
+
const hasValue = (0, _utils.isElementType)(target, ['input', 'textarea']); // On non-input elements the text selection per multiple click
|
|
268
|
+
// can extend beyond the target boundaries.
|
|
269
|
+
// The exact mechanism what is considered in the same line is unclear.
|
|
270
|
+
// Looks it might be every inline element.
|
|
271
|
+
// TODO: Check what might be considered part of the same line of text.
|
|
272
|
+
|
|
273
|
+
const text = String(hasValue ? (0, _document.getUIValue)(target) : target.textContent);
|
|
274
|
+
const [start, end] = node ? // As offset is describing a DOMOffset it is non-trivial to determine
|
|
275
|
+
// which elements might be considered in the same line of text.
|
|
276
|
+
// TODO: support expanding initial range on multiple clicks if node is given
|
|
277
|
+
[offset, offset] : getTextRange(text, offset, clickCount); // TODO: implement modifying selection per shift/ctrl+mouse
|
|
278
|
+
|
|
279
|
+
if (hasValue) {
|
|
280
|
+
(0, _document.setUISelection)(target, {
|
|
281
|
+
anchorOffset: start != null ? start : text.length,
|
|
282
|
+
focusOffset: end != null ? end : text.length
|
|
283
|
+
});
|
|
284
|
+
position.selectionRange = {
|
|
285
|
+
node: target,
|
|
286
|
+
start: start != null ? start : 0,
|
|
287
|
+
end: end != null ? end : text.length
|
|
288
|
+
};
|
|
289
|
+
} else {
|
|
290
|
+
const {
|
|
291
|
+
node: startNode,
|
|
292
|
+
offset: startOffset
|
|
293
|
+
} = (0, _resolveSelectionTarget.resolveSelectionTarget)({
|
|
294
|
+
target,
|
|
295
|
+
node,
|
|
296
|
+
offset: start
|
|
297
|
+
});
|
|
298
|
+
const {
|
|
299
|
+
node: endNode,
|
|
300
|
+
offset: endOffset
|
|
301
|
+
} = (0, _resolveSelectionTarget.resolveSelectionTarget)({
|
|
302
|
+
target,
|
|
303
|
+
node,
|
|
304
|
+
offset: end
|
|
305
|
+
});
|
|
306
|
+
const range = new Range();
|
|
307
|
+
range.setStart(startNode, startOffset);
|
|
308
|
+
range.setEnd(endNode, endOffset);
|
|
309
|
+
position.selectionRange = range;
|
|
310
|
+
const selection = target.ownerDocument.getSelection();
|
|
311
|
+
selection.removeAllRanges();
|
|
312
|
+
selection.addRange(range.cloneRange());
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
function getTextRange(text, pos, clickCount) {
|
|
318
|
+
if (clickCount % 3 === 1 || text.length === 0) {
|
|
319
|
+
return [pos, pos];
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
const textPos = pos != null ? pos : text.length;
|
|
323
|
+
|
|
324
|
+
if (clickCount % 3 === 2) {
|
|
325
|
+
return [textPos - text.substr(0, pos).match(/(\w+|\s+|\W)?$/)[0].length, pos === undefined ? pos : pos + text.substr(pos).match(/^(\w+|\s+|\W)?/)[0].length];
|
|
326
|
+
} // triple click
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
return [textPos - text.substr(0, pos).match(/[^\r\n]*$/)[0].length, pos === undefined ? pos : pos + text.substr(pos).match(/^[^\r\n]*/)[0].length];
|
|
224
330
|
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.resolveSelectionTarget = resolveSelectionTarget;
|
|
7
|
+
|
|
8
|
+
var _document = require("../document");
|
|
9
|
+
|
|
10
|
+
var _utils = require("../utils");
|
|
11
|
+
|
|
12
|
+
function resolveSelectionTarget({
|
|
13
|
+
target,
|
|
14
|
+
node,
|
|
15
|
+
offset
|
|
16
|
+
}) {
|
|
17
|
+
if ((0, _utils.isElementType)(target, ['input', 'textarea'])) {
|
|
18
|
+
return {
|
|
19
|
+
node: target,
|
|
20
|
+
offset: offset != null ? offset : (0, _document.getUIValue)(target).length
|
|
21
|
+
};
|
|
22
|
+
} else if (node) {
|
|
23
|
+
return {
|
|
24
|
+
node,
|
|
25
|
+
offset: offset != null ? offset : node.nodeType === 3 ? node.nodeValue.length : node.childNodes.length
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return findNodeAtTextOffset(target, offset);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function findNodeAtTextOffset(node, offset, isRoot = true) {
|
|
33
|
+
// When clicking after the content the browser behavior can be complicated:
|
|
34
|
+
// 1. If there is textContent after the last element child,
|
|
35
|
+
// the cursor is moved there.
|
|
36
|
+
// 2. If there is textContent in the last element child,
|
|
37
|
+
// the browser moves the cursor to the last non-empty text node inside this element.
|
|
38
|
+
// 3. Otherwise the cursor is moved to the end of the target.
|
|
39
|
+
let i = offset === undefined ? node.childNodes.length - 1 : 0;
|
|
40
|
+
const step = offset === undefined ? -1 : +1;
|
|
41
|
+
|
|
42
|
+
while (offset === undefined ? i >= (isRoot ? Math.max(node.childNodes.length - 1, 0) : 0) : i <= node.childNodes.length) {
|
|
43
|
+
const c = node.childNodes.item(i);
|
|
44
|
+
const text = String(c.textContent);
|
|
45
|
+
|
|
46
|
+
if (text.length) {
|
|
47
|
+
if (offset !== undefined && text.length < offset) {
|
|
48
|
+
offset -= text.length;
|
|
49
|
+
} else if (c.nodeType === 1) {
|
|
50
|
+
return findNodeAtTextOffset(c, offset, false);
|
|
51
|
+
} else {
|
|
52
|
+
// The pre-commit hooks keeps changing this
|
|
53
|
+
// See https://github.com/kentcdodds/kcd-scripts/issues/218
|
|
54
|
+
|
|
55
|
+
/* istanbul ignore else */
|
|
56
|
+
// eslint-disable-next-line no-lonely-if
|
|
57
|
+
if (c.nodeType === 3) {
|
|
58
|
+
var _offset;
|
|
59
|
+
|
|
60
|
+
return {
|
|
61
|
+
node: c,
|
|
62
|
+
offset: (_offset = offset) != null ? _offset : c.nodeValue.length
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
i += step;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return {
|
|
72
|
+
node,
|
|
73
|
+
offset: node.childNodes.length
|
|
74
|
+
};
|
|
75
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { keyboardState } from '../keyboard/types';
|
|
2
|
-
import {
|
|
2
|
+
import { PointerCoords, MouseButton } from '../utils';
|
|
3
3
|
/**
|
|
4
4
|
* @internal Do not create/alter this by yourself as this type might be subject to changes.
|
|
5
5
|
*/
|
|
@@ -26,8 +26,8 @@ export declare type pointerState = {
|
|
|
26
26
|
position: Record<string, {
|
|
27
27
|
pointerId: number;
|
|
28
28
|
pointerType: 'mouse' | 'pen' | 'touch';
|
|
29
|
-
|
|
30
|
-
|
|
29
|
+
} & Partial<PointerTarget> & {
|
|
30
|
+
selectionRange?: Range | SelectionInputRange;
|
|
31
31
|
}>;
|
|
32
32
|
/**
|
|
33
33
|
* Last applied pointer id
|
|
@@ -50,7 +50,21 @@ export interface pointerKey {
|
|
|
50
50
|
}
|
|
51
51
|
export interface PointerTarget {
|
|
52
52
|
target: Element;
|
|
53
|
-
coords
|
|
53
|
+
coords?: PointerCoords;
|
|
54
|
+
}
|
|
55
|
+
export interface SelectionTarget {
|
|
56
|
+
node?: Node;
|
|
57
|
+
/**
|
|
58
|
+
* If `node` is set, this is the DOM offset.
|
|
59
|
+
* Otherwise this is the `textContent`/`value` offset on the `target`.
|
|
60
|
+
*/
|
|
61
|
+
offset?: number;
|
|
62
|
+
}
|
|
63
|
+
/** Describes a selection inside `HTMLInputElement`/`HTMLTextareaElement` */
|
|
64
|
+
export interface SelectionInputRange {
|
|
65
|
+
node: HTMLInputElement | HTMLTextAreaElement;
|
|
66
|
+
start: number;
|
|
67
|
+
end: number;
|
|
54
68
|
}
|
|
55
69
|
export interface inputDeviceState {
|
|
56
70
|
pointerState: pointerState;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { PointerOptions } from './utils';
|
|
2
2
|
import type { UserEvent } from './setup';
|
|
3
|
-
export declare function selectOptions(this: UserEvent, select: Element, values: HTMLElement | HTMLElement[] | string[] | string,
|
|
4
|
-
export declare function deselectOptions(this: UserEvent, select: Element, values: HTMLElement | HTMLElement[] | string[] | string,
|
|
3
|
+
export declare function selectOptions(this: UserEvent, select: Element, values: HTMLElement | HTMLElement[] | string[] | string, options?: PointerOptions): void;
|
|
4
|
+
export declare function deselectOptions(this: UserEvent, select: Element, values: HTMLElement | HTMLElement[] | string[] | string, options?: PointerOptions): void;
|
package/dist/selectOptions.js
CHANGED
|
@@ -10,15 +10,15 @@ var _dom = require("@testing-library/dom");
|
|
|
10
10
|
|
|
11
11
|
var _utils = require("./utils");
|
|
12
12
|
|
|
13
|
-
function selectOptions(select, values,
|
|
14
|
-
return selectOptionsBase.call(this, true, select, values,
|
|
13
|
+
function selectOptions(select, values, options = {}) {
|
|
14
|
+
return selectOptionsBase.call(this, true, select, values, options);
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
function deselectOptions(select, values,
|
|
18
|
-
return selectOptionsBase.call(this, false, select, values,
|
|
17
|
+
function deselectOptions(select, values, options = {}) {
|
|
18
|
+
return selectOptionsBase.call(this, false, select, values, options);
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
function selectOptionsBase(newValue, select, values,
|
|
21
|
+
function selectOptionsBase(newValue, select, values, {
|
|
22
22
|
skipPointerEventsCheck
|
|
23
23
|
}) {
|
|
24
24
|
if (!newValue && !select.multiple) {
|
|
@@ -48,42 +48,42 @@ function selectOptionsBase(newValue, select, values, init, {
|
|
|
48
48
|
const withPointerEvents = skipPointerEventsCheck ? true : (0, _utils.hasPointerEvents)(option); // events fired for multiple select are weird. Can't use hover...
|
|
49
49
|
|
|
50
50
|
if (withPointerEvents) {
|
|
51
|
-
_dom.fireEvent.pointerOver(option
|
|
51
|
+
_dom.fireEvent.pointerOver(option);
|
|
52
52
|
|
|
53
|
-
_dom.fireEvent.pointerEnter(select
|
|
53
|
+
_dom.fireEvent.pointerEnter(select);
|
|
54
54
|
|
|
55
55
|
_dom.fireEvent.mouseOver(option);
|
|
56
56
|
|
|
57
57
|
_dom.fireEvent.mouseEnter(select);
|
|
58
58
|
|
|
59
|
-
_dom.fireEvent.pointerMove(option
|
|
59
|
+
_dom.fireEvent.pointerMove(option);
|
|
60
60
|
|
|
61
|
-
_dom.fireEvent.mouseMove(option
|
|
61
|
+
_dom.fireEvent.mouseMove(option);
|
|
62
62
|
|
|
63
|
-
_dom.fireEvent.pointerDown(option
|
|
63
|
+
_dom.fireEvent.pointerDown(option);
|
|
64
64
|
|
|
65
|
-
_dom.fireEvent.mouseDown(option
|
|
65
|
+
_dom.fireEvent.mouseDown(option);
|
|
66
66
|
}
|
|
67
67
|
|
|
68
68
|
(0, _utils.focus)(select);
|
|
69
69
|
|
|
70
70
|
if (withPointerEvents) {
|
|
71
|
-
_dom.fireEvent.pointerUp(option
|
|
71
|
+
_dom.fireEvent.pointerUp(option);
|
|
72
72
|
|
|
73
|
-
_dom.fireEvent.mouseUp(option
|
|
73
|
+
_dom.fireEvent.mouseUp(option);
|
|
74
74
|
}
|
|
75
75
|
|
|
76
76
|
selectOption(option);
|
|
77
77
|
|
|
78
78
|
if (withPointerEvents) {
|
|
79
|
-
_dom.fireEvent.click(option
|
|
79
|
+
_dom.fireEvent.click(option);
|
|
80
80
|
}
|
|
81
81
|
}
|
|
82
82
|
} else if (selectedOptions.length === 1) {
|
|
83
83
|
const withPointerEvents = skipPointerEventsCheck ? true : (0, _utils.hasPointerEvents)(select); // the click to open the select options
|
|
84
84
|
|
|
85
85
|
if (withPointerEvents) {
|
|
86
|
-
this.click(select,
|
|
86
|
+
this.click(select, {
|
|
87
87
|
skipPointerEventsCheck: true
|
|
88
88
|
});
|
|
89
89
|
} else {
|
|
@@ -95,32 +95,32 @@ function selectOptionsBase(newValue, select, values, init, {
|
|
|
95
95
|
if (withPointerEvents) {
|
|
96
96
|
// the browser triggers another click event on the select for the click on the option
|
|
97
97
|
// this second click has no 'down' phase
|
|
98
|
-
_dom.fireEvent.pointerOver(select
|
|
98
|
+
_dom.fireEvent.pointerOver(select);
|
|
99
99
|
|
|
100
|
-
_dom.fireEvent.pointerEnter(select
|
|
100
|
+
_dom.fireEvent.pointerEnter(select);
|
|
101
101
|
|
|
102
102
|
_dom.fireEvent.mouseOver(select);
|
|
103
103
|
|
|
104
104
|
_dom.fireEvent.mouseEnter(select);
|
|
105
105
|
|
|
106
|
-
_dom.fireEvent.pointerUp(select
|
|
106
|
+
_dom.fireEvent.pointerUp(select);
|
|
107
107
|
|
|
108
|
-
_dom.fireEvent.mouseUp(select
|
|
108
|
+
_dom.fireEvent.mouseUp(select);
|
|
109
109
|
|
|
110
|
-
_dom.fireEvent.click(select
|
|
110
|
+
_dom.fireEvent.click(select);
|
|
111
111
|
}
|
|
112
112
|
} else {
|
|
113
113
|
throw (0, _dom.getConfig)().getElementError(`Cannot select multiple options on a non-multiple select`, select);
|
|
114
114
|
}
|
|
115
115
|
} else if (select.getAttribute('role') === 'listbox') {
|
|
116
116
|
selectedOptions.forEach(option => {
|
|
117
|
-
this.hover(option,
|
|
117
|
+
this.hover(option, {
|
|
118
118
|
skipPointerEventsCheck
|
|
119
119
|
});
|
|
120
|
-
this.click(option,
|
|
120
|
+
this.click(option, {
|
|
121
121
|
skipPointerEventsCheck
|
|
122
122
|
});
|
|
123
|
-
this.unhover(option,
|
|
123
|
+
this.unhover(option, {
|
|
124
124
|
skipPointerEventsCheck
|
|
125
125
|
});
|
|
126
126
|
});
|
|
@@ -133,10 +133,9 @@ function selectOptionsBase(newValue, select, values, init, {
|
|
|
133
133
|
(0, _dom.fireEvent)(select, (0, _dom.createEvent)('input', select, {
|
|
134
134
|
bubbles: true,
|
|
135
135
|
cancelable: false,
|
|
136
|
-
composed: true
|
|
137
|
-
...init
|
|
136
|
+
composed: true
|
|
138
137
|
}));
|
|
139
138
|
|
|
140
|
-
_dom.fireEvent.change(select
|
|
139
|
+
_dom.fireEvent.change(select);
|
|
141
140
|
}
|
|
142
141
|
}
|