@testing-library/user-event 14.3.0 → 14.4.0
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/cjs/clipboard/copy.js +40 -0
- package/dist/cjs/clipboard/cut.js +40 -0
- package/dist/cjs/clipboard/index.js +13 -0
- package/dist/cjs/clipboard/paste.js +41 -0
- package/dist/cjs/convenience/click.js +40 -0
- package/dist/cjs/convenience/hover.js +37 -0
- package/dist/cjs/convenience/index.js +16 -0
- package/dist/cjs/convenience/tab.js +9 -0
- package/dist/cjs/document/index.js +74 -0
- package/dist/cjs/document/interceptor.js +30 -0
- package/dist/cjs/document/selection.js +107 -0
- package/dist/cjs/document/setRangeText.js +21 -0
- package/dist/cjs/document/value.js +125 -0
- package/dist/cjs/event/behavior/click.js +42 -0
- package/dist/cjs/event/behavior/cut.js +26 -0
- package/dist/cjs/event/behavior/index.js +15 -0
- package/dist/cjs/event/behavior/keydown.js +134 -0
- package/dist/cjs/event/behavior/keypress.js +58 -0
- package/dist/cjs/event/behavior/keyup.js +24 -0
- package/dist/cjs/event/behavior/paste.js +29 -0
- package/dist/cjs/event/behavior/registry.js +7 -0
- package/dist/cjs/event/createEvent.js +193 -0
- package/dist/cjs/event/dispatchEvent.js +37 -0
- package/dist/cjs/event/dom-events.d.js +2 -0
- package/dist/cjs/event/eventMap.js +64 -0
- package/dist/cjs/event/index.js +24 -0
- package/dist/cjs/event/types.js +2 -0
- package/dist/cjs/event/wrapEvent.js +11 -0
- package/dist/cjs/index.js +14 -0
- package/dist/cjs/keyboard/index.js +59 -0
- package/dist/cjs/keyboard/keyMap.js +158 -0
- package/dist/cjs/keyboard/parseKeyDef.js +58 -0
- package/dist/cjs/options.js +42 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/pointer/index.js +88 -0
- package/dist/cjs/pointer/keyMap.js +35 -0
- package/dist/cjs/pointer/parseKeyDef.js +39 -0
- package/dist/cjs/setup/api.js +35 -0
- package/dist/cjs/setup/config.js +7 -0
- package/dist/cjs/setup/directApi.js +77 -0
- package/dist/cjs/setup/index.js +15 -0
- package/dist/cjs/setup/setup.js +97 -0
- package/dist/cjs/setup/wrapAsync.js +13 -0
- package/dist/cjs/system/index.js +31 -0
- package/dist/cjs/system/keyboard.js +135 -0
- package/dist/cjs/system/pointer/buttons.js +71 -0
- package/dist/cjs/system/pointer/device.js +21 -0
- package/dist/cjs/system/pointer/index.js +146 -0
- package/dist/cjs/system/pointer/mouse.js +186 -0
- package/dist/cjs/system/pointer/pointer.js +114 -0
- package/dist/cjs/utility/clear.js +42 -0
- package/dist/cjs/utility/index.js +16 -0
- package/dist/cjs/utility/selectOptions.js +125 -0
- package/dist/cjs/utility/type.js +41 -0
- package/dist/cjs/utility/upload.js +72 -0
- package/dist/cjs/utils/click/isClickableInput.js +22 -0
- package/dist/cjs/utils/dataTransfer/Blob.js +18 -0
- package/dist/cjs/utils/dataTransfer/Clipboard.js +168 -0
- package/dist/cjs/utils/dataTransfer/DataTransfer.js +124 -0
- package/dist/cjs/utils/dataTransfer/FileList.js +26 -0
- package/dist/cjs/utils/edit/buildTimeValue.js +36 -0
- package/dist/cjs/utils/edit/getValue.js +35 -0
- package/dist/cjs/utils/edit/input.js +162 -0
- package/dist/cjs/utils/edit/isContentEditable.js +20 -0
- package/dist/cjs/utils/edit/isEditable.js +35 -0
- package/dist/cjs/utils/edit/isValidDateOrTimeValue.js +11 -0
- package/dist/cjs/utils/edit/maxLength.js +38 -0
- package/dist/cjs/utils/edit/setFiles.js +59 -0
- package/dist/cjs/utils/edit/walkRadio.js +28 -0
- package/dist/cjs/utils/focus/blur.js +16 -0
- package/dist/cjs/utils/focus/copySelection.js +46 -0
- package/dist/cjs/utils/focus/cursor.js +143 -0
- package/dist/cjs/utils/focus/focus.js +26 -0
- package/dist/cjs/utils/focus/getActiveElement.js +24 -0
- package/dist/cjs/utils/focus/getTabDestination.js +82 -0
- package/dist/cjs/utils/focus/isFocusable.js +11 -0
- package/dist/cjs/utils/focus/resolveCaretPosition.js +78 -0
- package/dist/cjs/utils/focus/selectAll.js +54 -0
- package/dist/cjs/utils/focus/selection.js +287 -0
- package/dist/cjs/utils/focus/selector.js +16 -0
- package/dist/cjs/utils/index.js +108 -0
- package/dist/cjs/utils/keyDef/readNextDescriptor.js +92 -0
- package/dist/cjs/utils/misc/cloneEvent.js +9 -0
- package/dist/cjs/utils/misc/dom-helpers.d.js +2 -0
- package/dist/cjs/utils/misc/eventWrapper.js +15 -0
- package/dist/cjs/utils/misc/findClosest.js +16 -0
- package/dist/cjs/utils/misc/getDocumentFromNode.js +12 -0
- package/dist/cjs/utils/misc/getTreeDiff.js +27 -0
- package/dist/cjs/utils/misc/getWindow.js +11 -0
- package/dist/cjs/utils/misc/isDescendantOrSelf.js +16 -0
- package/dist/cjs/utils/misc/isDisabled.js +34 -0
- package/dist/cjs/utils/misc/isElementType.js +22 -0
- package/dist/cjs/utils/misc/isVisible.js +21 -0
- package/dist/cjs/utils/misc/level.js +20 -0
- package/dist/cjs/utils/misc/wait.js +16 -0
- package/dist/cjs/utils/pointer/cssPointerEvents.js +111 -0
- package/dist/esm/clipboard/copy.js +36 -0
- package/dist/esm/clipboard/cut.js +36 -0
- package/dist/esm/clipboard/index.js +3 -0
- package/dist/esm/clipboard/paste.js +37 -0
- package/dist/esm/convenience/click.js +34 -0
- package/dist/esm/convenience/hover.js +32 -0
- package/dist/esm/convenience/index.js +3 -0
- package/dist/esm/convenience/tab.js +5 -0
- package/dist/esm/document/index.js +66 -0
- package/dist/esm/document/interceptor.js +26 -0
- package/dist/esm/document/selection.js +99 -0
- package/dist/esm/document/setRangeText.js +17 -0
- package/dist/esm/document/value.js +115 -0
- package/dist/esm/event/behavior/click.js +40 -0
- package/dist/esm/event/behavior/cut.js +24 -0
- package/dist/esm/event/behavior/index.js +7 -0
- package/dist/esm/event/behavior/keydown.js +132 -0
- package/dist/esm/event/behavior/keypress.js +56 -0
- package/dist/esm/event/behavior/keyup.js +22 -0
- package/dist/esm/event/behavior/paste.js +27 -0
- package/dist/esm/event/behavior/registry.js +3 -0
- package/dist/esm/event/createEvent.js +189 -0
- package/dist/esm/event/dispatchEvent.js +33 -0
- package/dist/esm/event/dom-events.d.js +1 -0
- package/dist/esm/event/eventMap.js +57 -0
- package/dist/esm/event/index.js +19 -0
- package/dist/esm/event/types.js +1 -0
- package/dist/esm/event/wrapEvent.js +7 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/keyboard/index.js +54 -0
- package/dist/esm/keyboard/keyMap.js +154 -0
- package/dist/esm/keyboard/parseKeyDef.js +54 -0
- package/dist/esm/options.js +37 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/pointer/index.js +84 -0
- package/dist/esm/pointer/keyMap.js +31 -0
- package/dist/esm/pointer/parseKeyDef.js +35 -0
- package/dist/esm/setup/api.js +12 -0
- package/dist/esm/setup/config.js +3 -0
- package/dist/esm/setup/directApi.js +58 -0
- package/dist/esm/setup/index.js +10 -0
- package/dist/esm/setup/setup.js +90 -0
- package/dist/esm/setup/wrapAsync.js +9 -0
- package/dist/esm/system/index.js +27 -0
- package/dist/esm/system/keyboard.js +131 -0
- package/dist/esm/system/pointer/buttons.js +63 -0
- package/dist/esm/system/pointer/device.js +17 -0
- package/dist/esm/system/pointer/index.js +141 -0
- package/dist/esm/system/pointer/mouse.js +182 -0
- package/dist/esm/system/pointer/pointer.js +110 -0
- package/dist/esm/utility/clear.js +38 -0
- package/dist/esm/utility/index.js +4 -0
- package/dist/esm/utility/selectOptions.js +120 -0
- package/dist/esm/utility/type.js +37 -0
- package/dist/esm/utility/upload.js +68 -0
- package/dist/esm/utils/click/isClickableInput.js +18 -0
- package/dist/esm/utils/dataTransfer/Blob.js +14 -0
- package/dist/esm/utils/dataTransfer/Clipboard.js +159 -0
- package/dist/esm/utils/dataTransfer/DataTransfer.js +119 -0
- package/dist/esm/utils/dataTransfer/FileList.js +22 -0
- package/dist/esm/utils/edit/buildTimeValue.js +32 -0
- package/dist/esm/utils/edit/getValue.js +31 -0
- package/dist/esm/utils/edit/input.js +158 -0
- package/dist/esm/utils/edit/isContentEditable.js +15 -0
- package/dist/esm/utils/edit/isEditable.js +30 -0
- package/dist/esm/utils/edit/isValidDateOrTimeValue.js +7 -0
- package/dist/esm/utils/edit/maxLength.js +34 -0
- package/dist/esm/utils/edit/setFiles.js +55 -0
- package/dist/esm/utils/edit/walkRadio.js +24 -0
- package/dist/esm/utils/focus/blur.js +12 -0
- package/dist/esm/utils/focus/copySelection.js +42 -0
- package/dist/esm/utils/focus/cursor.js +139 -0
- package/dist/esm/utils/focus/focus.js +22 -0
- package/dist/esm/utils/focus/getActiveElement.js +19 -0
- package/dist/esm/utils/focus/getTabDestination.js +78 -0
- package/dist/esm/utils/focus/isFocusable.js +7 -0
- package/dist/esm/utils/focus/resolveCaretPosition.js +74 -0
- package/dist/esm/utils/focus/selectAll.js +49 -0
- package/dist/esm/utils/focus/selection.js +274 -0
- package/dist/esm/utils/focus/selector.js +12 -0
- package/dist/esm/utils/index.js +36 -0
- package/dist/esm/utils/keyDef/readNextDescriptor.js +88 -0
- package/dist/esm/utils/misc/cloneEvent.js +5 -0
- package/dist/esm/utils/misc/dom-helpers.d.js +1 -0
- package/dist/esm/utils/misc/eventWrapper.js +11 -0
- package/dist/esm/utils/misc/findClosest.js +12 -0
- package/dist/esm/utils/misc/getDocumentFromNode.js +8 -0
- package/dist/esm/utils/misc/getTreeDiff.js +23 -0
- package/dist/esm/utils/misc/getWindow.js +7 -0
- package/dist/esm/utils/misc/isDescendantOrSelf.js +12 -0
- package/dist/esm/utils/misc/isDisabled.js +30 -0
- package/dist/esm/utils/misc/isElementType.js +18 -0
- package/dist/esm/utils/misc/isVisible.js +17 -0
- package/dist/esm/utils/misc/level.js +15 -0
- package/dist/esm/utils/misc/wait.js +12 -0
- package/dist/esm/utils/pointer/cssPointerEvents.js +106 -0
- package/dist/types/event/createEvent.d.ts +2 -8
- package/dist/types/event/eventMap.d.ts +204 -181
- package/dist/types/event/index.d.ts +1 -2
- package/dist/types/event/types.d.ts +6 -0
- package/dist/types/index.d.ts +2 -2
- package/dist/types/keyboard/index.d.ts +2 -6
- package/dist/types/keyboard/keyMap.d.ts +1 -1
- package/dist/types/keyboard/parseKeyDef.d.ts +1 -1
- package/dist/types/options.d.ts +4 -4
- package/dist/types/pointer/index.d.ts +23 -5
- package/dist/types/pointer/keyMap.d.ts +1 -1
- package/dist/types/pointer/parseKeyDef.d.ts +1 -1
- package/dist/types/setup/config.d.ts +3 -7
- package/dist/types/setup/directApi.d.ts +21 -16
- package/dist/types/setup/index.d.ts +1 -2
- package/dist/types/setup/setup.d.ts +7 -1
- package/dist/types/system/index.d.ts +10 -0
- package/dist/types/system/keyboard.d.ts +53 -0
- package/dist/types/system/pointer/buttons.d.ts +23 -0
- package/dist/types/system/pointer/device.d.ts +8 -0
- package/dist/types/system/pointer/index.d.ts +39 -0
- package/dist/types/system/pointer/mouse.d.ts +26 -0
- package/dist/types/system/pointer/pointer.d.ts +30 -0
- package/dist/types/utils/click/isClickableInput.d.ts +15 -1
- package/dist/types/utils/focus/getActiveElement.d.ts +1 -0
- package/dist/types/utils/focus/resolveCaretPosition.d.ts +8 -0
- package/dist/types/utils/focus/selection.d.ts +28 -1
- package/dist/types/utils/index.d.ts +2 -3
- package/dist/types/utils/misc/getTreeDiff.d.ts +1 -0
- package/package.json +9 -15
- package/dist/index.cjs +0 -3104
- package/dist/index.mjs +0 -3155
- package/dist/types/event/eventTypes.d.ts +0 -2
- package/dist/types/keyboard/keyboardAction.d.ts +0 -10
- package/dist/types/keyboard/modifiers.d.ts +0 -9
- package/dist/types/keyboard/types.d.ts +0 -73
- package/dist/types/pointer/firePointerEvents.d.ts +0 -11
- package/dist/types/pointer/pointerAction.d.ts +0 -7
- package/dist/types/pointer/pointerMove.d.ts +0 -6
- package/dist/types/pointer/pointerPress.d.ts +0 -8
- package/dist/types/pointer/resolveSelectionTarget.d.ts +0 -5
- package/dist/types/pointer/types.d.ts +0 -62
- package/dist/types/utils/keyboard/getKeyEventProps.d.ts +0 -5
- package/dist/types/utils/keyboard/getUIEventModifiers.d.ts +0 -15
- package/dist/types/utils/pointer/mouseButtons.d.ts +0 -12
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { dispatchUIEvent } from '../../event/index.js';
|
|
2
|
+
import '../../utils/click/isClickableInput.js';
|
|
3
|
+
import '../../utils/dataTransfer/Clipboard.js';
|
|
4
|
+
import '../../utils/edit/maxLength.js';
|
|
5
|
+
import '../../utils/edit/isEditable.js';
|
|
6
|
+
import '@testing-library/dom';
|
|
7
|
+
import '@testing-library/dom/dist/helpers.js';
|
|
8
|
+
import '../../utils/keyDef/readNextDescriptor.js';
|
|
9
|
+
import { getTreeDiff } from '../../utils/misc/getTreeDiff.js';
|
|
10
|
+
import '../../utils/misc/level.js';
|
|
11
|
+
import { assertPointerEvents, hasPointerEvents } from '../../utils/pointer/cssPointerEvents.js';
|
|
12
|
+
import { isDifferentPointerPosition } from './index.js';
|
|
13
|
+
|
|
14
|
+
class Pointer {
|
|
15
|
+
constructor({ pointerId , pointerType , isPrimary }){
|
|
16
|
+
this.pointerId = pointerId;
|
|
17
|
+
this.pointerType = pointerType;
|
|
18
|
+
this.isPrimary = isPrimary;
|
|
19
|
+
this.isMultitouch = !isPrimary;
|
|
20
|
+
}
|
|
21
|
+
isMultitouch = false;
|
|
22
|
+
isCancelled = false;
|
|
23
|
+
isDown = false;
|
|
24
|
+
isPrevented = false;
|
|
25
|
+
position = {};
|
|
26
|
+
init(config, position) {
|
|
27
|
+
this.position = position;
|
|
28
|
+
const target = this.getTarget(config);
|
|
29
|
+
const [, enter] = getTreeDiff(null, target);
|
|
30
|
+
const init = this.getEventInit();
|
|
31
|
+
assertPointerEvents(config, target);
|
|
32
|
+
dispatchUIEvent(config, target, 'pointerover', init);
|
|
33
|
+
enter.forEach((el)=>dispatchUIEvent(config, el, 'pointerenter', init));
|
|
34
|
+
return this;
|
|
35
|
+
}
|
|
36
|
+
move(config, position) {
|
|
37
|
+
const prevPosition = this.position;
|
|
38
|
+
const prevTarget = this.getTarget(config);
|
|
39
|
+
this.position = position;
|
|
40
|
+
if (!isDifferentPointerPosition(prevPosition, position)) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
const nextTarget = this.getTarget(config);
|
|
44
|
+
const init = this.getEventInit();
|
|
45
|
+
const [leave, enter] = getTreeDiff(prevTarget, nextTarget);
|
|
46
|
+
return {
|
|
47
|
+
leave: ()=>{
|
|
48
|
+
if (hasPointerEvents(config, prevTarget)) {
|
|
49
|
+
if (prevTarget !== nextTarget) {
|
|
50
|
+
dispatchUIEvent(config, prevTarget, 'pointerout', init);
|
|
51
|
+
leave.forEach((el)=>dispatchUIEvent(config, el, 'pointerleave', init));
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
enter: ()=>{
|
|
56
|
+
assertPointerEvents(config, nextTarget);
|
|
57
|
+
if (prevTarget !== nextTarget) {
|
|
58
|
+
dispatchUIEvent(config, nextTarget, 'pointerover', init);
|
|
59
|
+
enter.forEach((el)=>dispatchUIEvent(config, el, 'pointerenter', init));
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
move: ()=>{
|
|
63
|
+
dispatchUIEvent(config, nextTarget, 'pointermove', init);
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
down(config, _keyDef) {
|
|
68
|
+
if (this.isDown) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
const target = this.getTarget(config);
|
|
72
|
+
assertPointerEvents(config, target);
|
|
73
|
+
this.isDown = true;
|
|
74
|
+
this.isPrevented = !dispatchUIEvent(config, target, 'pointerdown', this.getEventInit());
|
|
75
|
+
}
|
|
76
|
+
up(config, _keyDef) {
|
|
77
|
+
if (!this.isDown) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
const target = this.getTarget(config);
|
|
81
|
+
assertPointerEvents(config, target);
|
|
82
|
+
this.isDown = false;
|
|
83
|
+
dispatchUIEvent(config, target, 'pointerup', this.getEventInit());
|
|
84
|
+
}
|
|
85
|
+
release(config) {
|
|
86
|
+
const target = this.getTarget(config);
|
|
87
|
+
const [leave] = getTreeDiff(target, null);
|
|
88
|
+
const init = this.getEventInit();
|
|
89
|
+
// Currently there is no PointerEventsCheckLevel that would
|
|
90
|
+
// make this check not use the *asserted* cached value from `up`.
|
|
91
|
+
/* istanbul ignore else */ if (hasPointerEvents(config, target)) {
|
|
92
|
+
dispatchUIEvent(config, target, 'pointerout', init);
|
|
93
|
+
leave.forEach((el)=>dispatchUIEvent(config, el, 'pointerleave', init));
|
|
94
|
+
}
|
|
95
|
+
this.isCancelled = true;
|
|
96
|
+
}
|
|
97
|
+
getTarget(config) {
|
|
98
|
+
return this.position.target ?? config.document.body;
|
|
99
|
+
}
|
|
100
|
+
getEventInit() {
|
|
101
|
+
return {
|
|
102
|
+
...this.position.coords,
|
|
103
|
+
pointerId: this.pointerId,
|
|
104
|
+
pointerType: this.pointerType,
|
|
105
|
+
isPrimary: this.isPrimary
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export { Pointer };
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import '../setup/index.js';
|
|
2
|
+
import '../utils/click/isClickableInput.js';
|
|
3
|
+
import '../utils/dataTransfer/Clipboard.js';
|
|
4
|
+
import '../event/eventMap.js';
|
|
5
|
+
import '../event/behavior/click.js';
|
|
6
|
+
import '../event/behavior/cut.js';
|
|
7
|
+
import '../event/behavior/keydown.js';
|
|
8
|
+
import '../event/behavior/keypress.js';
|
|
9
|
+
import '../event/behavior/keyup.js';
|
|
10
|
+
import '../event/behavior/paste.js';
|
|
11
|
+
import '@testing-library/dom';
|
|
12
|
+
import { input } from '../utils/edit/input.js';
|
|
13
|
+
import { isEditable } from '../utils/edit/isEditable.js';
|
|
14
|
+
import { focus } from '../utils/focus/focus.js';
|
|
15
|
+
import '@testing-library/dom/dist/helpers.js';
|
|
16
|
+
import { isDisabled } from '../utils/misc/isDisabled.js';
|
|
17
|
+
import { selectAll, isAllSelected } from '../utils/focus/selectAll.js';
|
|
18
|
+
import '../utils/keyDef/readNextDescriptor.js';
|
|
19
|
+
import '../utils/misc/level.js';
|
|
20
|
+
import '../options.js';
|
|
21
|
+
import { Config } from '../setup/config.js';
|
|
22
|
+
|
|
23
|
+
async function clear(element) {
|
|
24
|
+
if (!isEditable(element) || isDisabled(element)) {
|
|
25
|
+
throw new Error('clear()` is only supported on editable elements.');
|
|
26
|
+
}
|
|
27
|
+
focus(element);
|
|
28
|
+
if (element.ownerDocument.activeElement !== element) {
|
|
29
|
+
throw new Error('The element to be cleared could not be focused.');
|
|
30
|
+
}
|
|
31
|
+
selectAll(element);
|
|
32
|
+
if (!isAllSelected(element)) {
|
|
33
|
+
throw new Error('The element content to be cleared could not be selected.');
|
|
34
|
+
}
|
|
35
|
+
input(this[Config], element, '', 'deleteContentBackward');
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export { clear };
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { getConfig } from '@testing-library/dom';
|
|
2
|
+
import '../utils/click/isClickableInput.js';
|
|
3
|
+
import '../utils/dataTransfer/Clipboard.js';
|
|
4
|
+
import '../event/eventMap.js';
|
|
5
|
+
import '../event/behavior/click.js';
|
|
6
|
+
import '../event/behavior/cut.js';
|
|
7
|
+
import '../event/behavior/keydown.js';
|
|
8
|
+
import '../event/behavior/keypress.js';
|
|
9
|
+
import '../event/behavior/keyup.js';
|
|
10
|
+
import '../event/behavior/paste.js';
|
|
11
|
+
import '../utils/edit/maxLength.js';
|
|
12
|
+
import '../utils/edit/isEditable.js';
|
|
13
|
+
import { focus } from '../utils/focus/focus.js';
|
|
14
|
+
import '@testing-library/dom/dist/helpers.js';
|
|
15
|
+
import { isDisabled } from '../utils/misc/isDisabled.js';
|
|
16
|
+
import { isElementType } from '../utils/misc/isElementType.js';
|
|
17
|
+
import '../utils/keyDef/readNextDescriptor.js';
|
|
18
|
+
import '../utils/misc/level.js';
|
|
19
|
+
import { wait } from '../utils/misc/wait.js';
|
|
20
|
+
import { hasPointerEvents } from '../utils/pointer/cssPointerEvents.js';
|
|
21
|
+
import '../setup/index.js';
|
|
22
|
+
import { Config } from '../setup/config.js';
|
|
23
|
+
|
|
24
|
+
async function selectOptions(select, values) {
|
|
25
|
+
return selectOptionsBase.call(this, true, select, values);
|
|
26
|
+
}
|
|
27
|
+
async function deselectOptions(select, values) {
|
|
28
|
+
return selectOptionsBase.call(this, false, select, values);
|
|
29
|
+
}
|
|
30
|
+
async function selectOptionsBase(newValue, select, values) {
|
|
31
|
+
if (!newValue && !select.multiple) {
|
|
32
|
+
throw getConfig().getElementError(`Unable to deselect an option in a non-multiple select. Use selectOptions to change the selection instead.`, select);
|
|
33
|
+
}
|
|
34
|
+
const valArray = Array.isArray(values) ? values : [
|
|
35
|
+
values
|
|
36
|
+
];
|
|
37
|
+
const allOptions = Array.from(select.querySelectorAll('option, [role="option"]'));
|
|
38
|
+
const selectedOptions = valArray.map((val)=>{
|
|
39
|
+
if (typeof val !== 'string' && allOptions.includes(val)) {
|
|
40
|
+
return val;
|
|
41
|
+
} else {
|
|
42
|
+
const matchingOption = allOptions.find((o)=>o.value === val || o.innerHTML === val);
|
|
43
|
+
if (matchingOption) {
|
|
44
|
+
return matchingOption;
|
|
45
|
+
} else {
|
|
46
|
+
throw getConfig().getElementError(`Value "${String(val)}" not found in options`, select);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}).filter((option)=>!isDisabled(option));
|
|
50
|
+
if (isDisabled(select) || !selectedOptions.length) return;
|
|
51
|
+
const selectOption = (option)=>{
|
|
52
|
+
option.selected = newValue;
|
|
53
|
+
this.dispatchUIEvent(select, 'input', {
|
|
54
|
+
bubbles: true,
|
|
55
|
+
cancelable: false,
|
|
56
|
+
composed: true
|
|
57
|
+
});
|
|
58
|
+
this.dispatchUIEvent(select, 'change');
|
|
59
|
+
};
|
|
60
|
+
if (isElementType(select, 'select')) {
|
|
61
|
+
if (select.multiple) {
|
|
62
|
+
for (const option of selectedOptions){
|
|
63
|
+
const withPointerEvents = this[Config].pointerEventsCheck === 0 ? true : hasPointerEvents(this[Config], option);
|
|
64
|
+
// events fired for multiple select are weird. Can't use hover...
|
|
65
|
+
if (withPointerEvents) {
|
|
66
|
+
this.dispatchUIEvent(option, 'pointerover');
|
|
67
|
+
this.dispatchUIEvent(select, 'pointerenter');
|
|
68
|
+
this.dispatchUIEvent(option, 'mouseover');
|
|
69
|
+
this.dispatchUIEvent(select, 'mouseenter');
|
|
70
|
+
this.dispatchUIEvent(option, 'pointermove');
|
|
71
|
+
this.dispatchUIEvent(option, 'mousemove');
|
|
72
|
+
this.dispatchUIEvent(option, 'pointerdown');
|
|
73
|
+
this.dispatchUIEvent(option, 'mousedown');
|
|
74
|
+
}
|
|
75
|
+
focus(select);
|
|
76
|
+
if (withPointerEvents) {
|
|
77
|
+
this.dispatchUIEvent(option, 'pointerup');
|
|
78
|
+
this.dispatchUIEvent(option, 'mouseup');
|
|
79
|
+
}
|
|
80
|
+
selectOption(option);
|
|
81
|
+
if (withPointerEvents) {
|
|
82
|
+
this.dispatchUIEvent(option, 'click');
|
|
83
|
+
}
|
|
84
|
+
await wait(this[Config]);
|
|
85
|
+
}
|
|
86
|
+
} else if (selectedOptions.length === 1) {
|
|
87
|
+
const withPointerEvents1 = this[Config].pointerEventsCheck === 0 ? true : hasPointerEvents(this[Config], select);
|
|
88
|
+
// the click to open the select options
|
|
89
|
+
if (withPointerEvents1) {
|
|
90
|
+
await this.click(select);
|
|
91
|
+
} else {
|
|
92
|
+
focus(select);
|
|
93
|
+
}
|
|
94
|
+
selectOption(selectedOptions[0]);
|
|
95
|
+
if (withPointerEvents1) {
|
|
96
|
+
// the browser triggers another click event on the select for the click on the option
|
|
97
|
+
// this second click has no 'down' phase
|
|
98
|
+
this.dispatchUIEvent(select, 'pointerover');
|
|
99
|
+
this.dispatchUIEvent(select, 'pointerenter');
|
|
100
|
+
this.dispatchUIEvent(select, 'mouseover');
|
|
101
|
+
this.dispatchUIEvent(select, 'mouseenter');
|
|
102
|
+
this.dispatchUIEvent(select, 'pointerup');
|
|
103
|
+
this.dispatchUIEvent(select, 'mouseup');
|
|
104
|
+
this.dispatchUIEvent(select, 'click');
|
|
105
|
+
}
|
|
106
|
+
await wait(this[Config]);
|
|
107
|
+
} else {
|
|
108
|
+
throw getConfig().getElementError(`Cannot select multiple options on a non-multiple select`, select);
|
|
109
|
+
}
|
|
110
|
+
} else if (select.getAttribute('role') === 'listbox') {
|
|
111
|
+
for (const option1 of selectedOptions){
|
|
112
|
+
await this.click(option1);
|
|
113
|
+
await this.unhover(option1);
|
|
114
|
+
}
|
|
115
|
+
} else {
|
|
116
|
+
throw getConfig().getElementError(`Cannot select options on elements that are neither select nor listbox elements`, select);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export { deselectOptions, selectOptions };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import '../utils/click/isClickableInput.js';
|
|
2
|
+
import '../utils/dataTransfer/Clipboard.js';
|
|
3
|
+
import '../event/eventMap.js';
|
|
4
|
+
import '../event/behavior/click.js';
|
|
5
|
+
import '../event/behavior/cut.js';
|
|
6
|
+
import '../event/behavior/keydown.js';
|
|
7
|
+
import '../event/behavior/keypress.js';
|
|
8
|
+
import '../event/behavior/keyup.js';
|
|
9
|
+
import '../event/behavior/paste.js';
|
|
10
|
+
import '@testing-library/dom';
|
|
11
|
+
import '../utils/edit/maxLength.js';
|
|
12
|
+
import '../utils/edit/isEditable.js';
|
|
13
|
+
import { setSelectionRange } from '../utils/focus/selection.js';
|
|
14
|
+
import '@testing-library/dom/dist/helpers.js';
|
|
15
|
+
import '../utils/keyDef/readNextDescriptor.js';
|
|
16
|
+
import '../utils/misc/level.js';
|
|
17
|
+
import '../options.js';
|
|
18
|
+
import { releaseAllKeys } from '../keyboard/index.js';
|
|
19
|
+
import { Config } from '../setup/config.js';
|
|
20
|
+
|
|
21
|
+
async function type(element, text, { skipClick =this[Config].skipClick , skipAutoClose =this[Config].skipAutoClose , initialSelectionStart , initialSelectionEnd } = {}) {
|
|
22
|
+
// TODO: properly type guard
|
|
23
|
+
// we use this workaround for now to prevent changing behavior
|
|
24
|
+
if (element.disabled) return;
|
|
25
|
+
if (!skipClick) {
|
|
26
|
+
await this.click(element);
|
|
27
|
+
}
|
|
28
|
+
if (initialSelectionStart !== undefined) {
|
|
29
|
+
setSelectionRange(element, initialSelectionStart, initialSelectionEnd ?? initialSelectionStart);
|
|
30
|
+
}
|
|
31
|
+
await this.keyboard(text);
|
|
32
|
+
if (!skipAutoClose) {
|
|
33
|
+
await releaseAllKeys(this[Config]);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export { type };
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import '../utils/click/isClickableInput.js';
|
|
2
|
+
import { createFileList } from '../utils/dataTransfer/FileList.js';
|
|
3
|
+
import '../utils/dataTransfer/Clipboard.js';
|
|
4
|
+
import '../event/eventMap.js';
|
|
5
|
+
import '../event/behavior/click.js';
|
|
6
|
+
import '../event/behavior/cut.js';
|
|
7
|
+
import '../event/behavior/keydown.js';
|
|
8
|
+
import '../event/behavior/keypress.js';
|
|
9
|
+
import '../event/behavior/keyup.js';
|
|
10
|
+
import '../event/behavior/paste.js';
|
|
11
|
+
import '@testing-library/dom';
|
|
12
|
+
import '../utils/edit/maxLength.js';
|
|
13
|
+
import '../utils/edit/isEditable.js';
|
|
14
|
+
import { setFiles } from '../utils/edit/setFiles.js';
|
|
15
|
+
import { isDisabled } from '../utils/misc/isDisabled.js';
|
|
16
|
+
import { isElementType } from '../utils/misc/isElementType.js';
|
|
17
|
+
import { getWindow } from '../utils/misc/getWindow.js';
|
|
18
|
+
import '../utils/keyDef/readNextDescriptor.js';
|
|
19
|
+
import '../utils/misc/level.js';
|
|
20
|
+
import '../options.js';
|
|
21
|
+
import '../setup/index.js';
|
|
22
|
+
import { Config } from '../setup/config.js';
|
|
23
|
+
|
|
24
|
+
async function upload(element, fileOrFiles) {
|
|
25
|
+
const input = isElementType(element, 'label') ? element.control : element;
|
|
26
|
+
if (!input || !isElementType(input, 'input', {
|
|
27
|
+
type: 'file'
|
|
28
|
+
})) {
|
|
29
|
+
throw new TypeError(`The ${input === element ? 'given' : 'associated'} ${input?.tagName} element does not accept file uploads`);
|
|
30
|
+
}
|
|
31
|
+
if (isDisabled(element)) return;
|
|
32
|
+
const files = (Array.isArray(fileOrFiles) ? fileOrFiles : [
|
|
33
|
+
fileOrFiles
|
|
34
|
+
]).filter((file)=>!this[Config].applyAccept || isAcceptableFile(file, input.accept)).slice(0, input.multiple ? undefined : 1);
|
|
35
|
+
const fileDialog = ()=>{
|
|
36
|
+
// do not fire an input event if the file selection does not change
|
|
37
|
+
if (files.length === input.files?.length && files.every((f, i)=>f === input.files?.item(i))) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
setFiles(input, createFileList(getWindow(element), files));
|
|
41
|
+
this.dispatchUIEvent(input, 'input');
|
|
42
|
+
this.dispatchUIEvent(input, 'change');
|
|
43
|
+
};
|
|
44
|
+
input.addEventListener('fileDialog', fileDialog);
|
|
45
|
+
await this.click(element);
|
|
46
|
+
input.removeEventListener('fileDialog', fileDialog);
|
|
47
|
+
}
|
|
48
|
+
function isAcceptableFile(file, accept) {
|
|
49
|
+
if (!accept) {
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
const wildcards = [
|
|
53
|
+
'audio/*',
|
|
54
|
+
'image/*',
|
|
55
|
+
'video/*'
|
|
56
|
+
];
|
|
57
|
+
return accept.split(',').some((acceptToken)=>{
|
|
58
|
+
if (acceptToken.startsWith('.')) {
|
|
59
|
+
// tokens starting with a dot represent a file extension
|
|
60
|
+
return file.name.endsWith(acceptToken);
|
|
61
|
+
} else if (wildcards.includes(acceptToken)) {
|
|
62
|
+
return file.type.startsWith(acceptToken.substr(0, acceptToken.length - 1));
|
|
63
|
+
}
|
|
64
|
+
return file.type === acceptToken;
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export { upload };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { isElementType } from '../misc/isElementType.js';
|
|
2
|
+
|
|
3
|
+
var clickableInputTypes;
|
|
4
|
+
(function(clickableInputTypes) {
|
|
5
|
+
clickableInputTypes['button'] = 'button';
|
|
6
|
+
clickableInputTypes['color'] = 'color';
|
|
7
|
+
clickableInputTypes['file'] = 'file';
|
|
8
|
+
clickableInputTypes['image'] = 'image';
|
|
9
|
+
clickableInputTypes['reset'] = 'reset';
|
|
10
|
+
clickableInputTypes['submit'] = 'submit';
|
|
11
|
+
clickableInputTypes['checkbox'] = 'checkbox';
|
|
12
|
+
clickableInputTypes['radio'] = 'radio';
|
|
13
|
+
})(clickableInputTypes || (clickableInputTypes = {}));
|
|
14
|
+
function isClickableInput(element) {
|
|
15
|
+
return isElementType(element, 'button') || isElementType(element, 'input') && element.type in clickableInputTypes;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export { isClickableInput };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// jsdom does not implement Blob.text()
|
|
2
|
+
function readBlobText(blob, FileReader) {
|
|
3
|
+
return new Promise((res, rej)=>{
|
|
4
|
+
const fr = new FileReader();
|
|
5
|
+
fr.onerror = rej;
|
|
6
|
+
fr.onabort = rej;
|
|
7
|
+
fr.onload = ()=>{
|
|
8
|
+
res(String(fr.result));
|
|
9
|
+
};
|
|
10
|
+
fr.readAsText(blob);
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export { readBlobText };
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import '../click/isClickableInput.js';
|
|
2
|
+
import { readBlobText } from './Blob.js';
|
|
3
|
+
import { createDataTransfer, getBlobFromDataTransferItem } from './DataTransfer.js';
|
|
4
|
+
import '../../event/eventMap.js';
|
|
5
|
+
import '../../event/behavior/click.js';
|
|
6
|
+
import '../../event/behavior/cut.js';
|
|
7
|
+
import '../../event/behavior/keydown.js';
|
|
8
|
+
import '../../event/behavior/keypress.js';
|
|
9
|
+
import '../../event/behavior/keyup.js';
|
|
10
|
+
import '../../event/behavior/paste.js';
|
|
11
|
+
import '@testing-library/dom';
|
|
12
|
+
import '../edit/maxLength.js';
|
|
13
|
+
import '../edit/isEditable.js';
|
|
14
|
+
import { getWindow } from '../misc/getWindow.js';
|
|
15
|
+
import '../keyDef/readNextDescriptor.js';
|
|
16
|
+
import '../misc/level.js';
|
|
17
|
+
import '../../options.js';
|
|
18
|
+
|
|
19
|
+
// Clipboard is not available in jsdom
|
|
20
|
+
// MDN lists string|Blob|Promise<Blob|string> as possible types in ClipboardItemData
|
|
21
|
+
// lib.dom.d.ts lists only Promise<Blob|string>
|
|
22
|
+
// https://developer.mozilla.org/en-US/docs/Web/API/ClipboardItem/ClipboardItem#syntax
|
|
23
|
+
function createClipboardItem(window, ...blobs) {
|
|
24
|
+
const dataMap = Object.fromEntries(blobs.map((b)=>[
|
|
25
|
+
typeof b === 'string' ? 'text/plain' : b.type,
|
|
26
|
+
Promise.resolve(b),
|
|
27
|
+
]));
|
|
28
|
+
// use real ClipboardItem if available
|
|
29
|
+
/* istanbul ignore if */ if (typeof window.ClipboardItem !== 'undefined') {
|
|
30
|
+
return new window.ClipboardItem(dataMap);
|
|
31
|
+
}
|
|
32
|
+
return new class ClipboardItem {
|
|
33
|
+
constructor(d){
|
|
34
|
+
this.data = d;
|
|
35
|
+
}
|
|
36
|
+
get types() {
|
|
37
|
+
return Array.from(Object.keys(this.data));
|
|
38
|
+
}
|
|
39
|
+
async getType(type) {
|
|
40
|
+
const value = await this.data[type];
|
|
41
|
+
if (!value) {
|
|
42
|
+
throw new Error(`${type} is not one of the available MIME types on this item.`);
|
|
43
|
+
}
|
|
44
|
+
return value instanceof window.Blob ? value : new window.Blob([
|
|
45
|
+
value
|
|
46
|
+
], {
|
|
47
|
+
type
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
}(dataMap);
|
|
51
|
+
}
|
|
52
|
+
const ClipboardStubControl = Symbol('Manage ClipboardSub');
|
|
53
|
+
function createClipboardStub(window, control) {
|
|
54
|
+
return Object.assign(new class Clipboard extends window.EventTarget {
|
|
55
|
+
items = [];
|
|
56
|
+
async read() {
|
|
57
|
+
return Array.from(this.items);
|
|
58
|
+
}
|
|
59
|
+
async readText() {
|
|
60
|
+
let text = '';
|
|
61
|
+
for (const item of this.items){
|
|
62
|
+
const type = item.types.includes('text/plain') ? 'text/plain' : item.types.find((t)=>t.startsWith('text/'));
|
|
63
|
+
if (type) {
|
|
64
|
+
text += await item.getType(type).then((b)=>readBlobText(b, window.FileReader));
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return text;
|
|
68
|
+
}
|
|
69
|
+
async write(data) {
|
|
70
|
+
this.items = data;
|
|
71
|
+
}
|
|
72
|
+
async writeText(text) {
|
|
73
|
+
this.items = [
|
|
74
|
+
createClipboardItem(window, text)
|
|
75
|
+
];
|
|
76
|
+
}
|
|
77
|
+
}(), {
|
|
78
|
+
[ClipboardStubControl]: control
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
function isClipboardStub(clipboard) {
|
|
82
|
+
return !!clipboard?.[ClipboardStubControl];
|
|
83
|
+
}
|
|
84
|
+
function attachClipboardStubToView(window) {
|
|
85
|
+
if (isClipboardStub(window.navigator.clipboard)) {
|
|
86
|
+
return window.navigator.clipboard[ClipboardStubControl];
|
|
87
|
+
}
|
|
88
|
+
const realClipboard = Object.getOwnPropertyDescriptor(window.navigator, 'clipboard');
|
|
89
|
+
let stub;
|
|
90
|
+
const control = {
|
|
91
|
+
resetClipboardStub: ()=>{
|
|
92
|
+
stub = createClipboardStub(window, control);
|
|
93
|
+
},
|
|
94
|
+
detachClipboardStub: ()=>{
|
|
95
|
+
/* istanbul ignore if */ if (realClipboard) {
|
|
96
|
+
Object.defineProperty(window.navigator, 'clipboard', realClipboard);
|
|
97
|
+
} else {
|
|
98
|
+
Object.defineProperty(window.navigator, 'clipboard', {
|
|
99
|
+
value: undefined,
|
|
100
|
+
configurable: true
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
stub = createClipboardStub(window, control);
|
|
106
|
+
Object.defineProperty(window.navigator, 'clipboard', {
|
|
107
|
+
get: ()=>stub,
|
|
108
|
+
configurable: true
|
|
109
|
+
});
|
|
110
|
+
return stub[ClipboardStubControl];
|
|
111
|
+
}
|
|
112
|
+
function resetClipboardStubOnView(window) {
|
|
113
|
+
if (isClipboardStub(window.navigator.clipboard)) {
|
|
114
|
+
window.navigator.clipboard[ClipboardStubControl].resetClipboardStub();
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
function detachClipboardStubFromView(window) {
|
|
118
|
+
if (isClipboardStub(window.navigator.clipboard)) {
|
|
119
|
+
window.navigator.clipboard[ClipboardStubControl].detachClipboardStub();
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
async function readDataTransferFromClipboard(document) {
|
|
123
|
+
const window = document.defaultView;
|
|
124
|
+
const clipboard = window?.navigator.clipboard;
|
|
125
|
+
const items = clipboard && await clipboard.read();
|
|
126
|
+
if (!items) {
|
|
127
|
+
throw new Error('The Clipboard API is unavailable.');
|
|
128
|
+
}
|
|
129
|
+
const dt = createDataTransfer(window);
|
|
130
|
+
for (const item of items){
|
|
131
|
+
for (const type of item.types){
|
|
132
|
+
dt.setData(type, await item.getType(type).then((b)=>readBlobText(b, window.FileReader)));
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return dt;
|
|
136
|
+
}
|
|
137
|
+
async function writeDataTransferToClipboard(document, clipboardData) {
|
|
138
|
+
const window = getWindow(document);
|
|
139
|
+
const clipboard = window.navigator.clipboard;
|
|
140
|
+
const items = [];
|
|
141
|
+
for(let i = 0; i < clipboardData.items.length; i++){
|
|
142
|
+
const dtItem = clipboardData.items[i];
|
|
143
|
+
const blob = getBlobFromDataTransferItem(window, dtItem);
|
|
144
|
+
items.push(createClipboardItem(window, blob));
|
|
145
|
+
}
|
|
146
|
+
const written = clipboard && await clipboard.write(items).then(()=>true, // Can happen with other implementations that e.g. require permissions
|
|
147
|
+
/* istanbul ignore next */ ()=>false);
|
|
148
|
+
if (!written) {
|
|
149
|
+
throw new Error('The Clipboard API is unavailable.');
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
/* istanbul ignore else */ if (typeof globalThis.afterEach === 'function') {
|
|
153
|
+
globalThis.afterEach(()=>resetClipboardStubOnView(globalThis.window));
|
|
154
|
+
}
|
|
155
|
+
/* istanbul ignore else */ if (typeof globalThis.afterAll === 'function') {
|
|
156
|
+
globalThis.afterAll(()=>detachClipboardStubFromView(globalThis.window));
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export { attachClipboardStubToView, createClipboardItem, detachClipboardStubFromView, readDataTransferFromClipboard, resetClipboardStubOnView, writeDataTransferToClipboard };
|