@testing-library/user-event 14.2.6 → 14.4.1
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 +41 -0
- package/dist/cjs/clipboard/cut.js +41 -0
- package/dist/cjs/clipboard/index.js +13 -0
- package/dist/cjs/clipboard/paste.js +43 -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 +108 -0
- package/dist/cjs/document/setRangeText.js +21 -0
- package/dist/cjs/document/value.js +127 -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 +138 -0
- package/dist/cjs/event/behavior/keypress.js +58 -0
- package/dist/cjs/event/behavior/keyup.js +25 -0
- package/dist/cjs/event/behavior/paste.js +30 -0
- package/dist/cjs/event/behavior/registry.js +7 -0
- package/dist/cjs/event/createEvent.js +209 -0
- package/dist/cjs/event/dispatchEvent.js +38 -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 +61 -0
- package/dist/cjs/options.js +42 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/pointer/index.js +92 -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 +100 -0
- package/dist/cjs/setup/wrapAsync.js +13 -0
- package/dist/cjs/system/index.js +33 -0
- package/dist/cjs/system/keyboard.js +137 -0
- package/dist/cjs/system/pointer/buttons.js +73 -0
- package/dist/cjs/system/pointer/device.js +23 -0
- package/dist/cjs/system/pointer/index.js +154 -0
- package/dist/cjs/system/pointer/mouse.js +195 -0
- package/dist/cjs/system/pointer/pointer.js +115 -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 +76 -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 +172 -0
- package/dist/cjs/utils/dataTransfer/DataTransfer.js +127 -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 +163 -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 +39 -0
- package/dist/cjs/utils/edit/setFiles.js +61 -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 +144 -0
- package/dist/cjs/utils/focus/focus.js +29 -0
- package/dist/cjs/utils/focus/getActiveElement.js +25 -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 +56 -0
- package/dist/cjs/utils/focus/selection.js +292 -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 +94 -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 +35 -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 +23 -0
- package/dist/cjs/utils/misc/wait.js +16 -0
- package/dist/cjs/utils/pointer/cssPointerEvents.js +118 -0
- package/dist/esm/clipboard/copy.js +37 -0
- package/dist/esm/clipboard/cut.js +37 -0
- package/dist/esm/clipboard/index.js +3 -0
- package/dist/esm/clipboard/paste.js +39 -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 +100 -0
- package/dist/esm/document/setRangeText.js +17 -0
- package/dist/esm/document/value.js +117 -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 +136 -0
- package/dist/esm/event/behavior/keypress.js +56 -0
- package/dist/esm/event/behavior/keyup.js +23 -0
- package/dist/esm/event/behavior/paste.js +28 -0
- package/dist/esm/event/behavior/registry.js +3 -0
- package/dist/esm/event/createEvent.js +205 -0
- package/dist/esm/event/dispatchEvent.js +34 -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 +57 -0
- package/dist/esm/options.js +37 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/pointer/index.js +88 -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 +93 -0
- package/dist/esm/setup/wrapAsync.js +9 -0
- package/dist/esm/system/index.js +29 -0
- package/dist/esm/system/keyboard.js +133 -0
- package/dist/esm/system/pointer/buttons.js +65 -0
- package/dist/esm/system/pointer/device.js +19 -0
- package/dist/esm/system/pointer/index.js +149 -0
- package/dist/esm/system/pointer/mouse.js +191 -0
- package/dist/esm/system/pointer/pointer.js +111 -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 +72 -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 +163 -0
- package/dist/esm/utils/dataTransfer/DataTransfer.js +122 -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 +159 -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 +35 -0
- package/dist/esm/utils/edit/setFiles.js +57 -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 +140 -0
- package/dist/esm/utils/focus/focus.js +25 -0
- package/dist/esm/utils/focus/getActiveElement.js +20 -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 +51 -0
- package/dist/esm/utils/focus/selection.js +279 -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 +90 -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 +31 -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 +18 -0
- package/dist/esm/utils/misc/wait.js +12 -0
- package/dist/esm/utils/pointer/cssPointerEvents.js +113 -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/edit/walkRadio.d.ts +4 -0
- 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 +3 -3
- package/dist/types/utils/misc/getTreeDiff.d.ts +1 -0
- package/package.json +9 -15
- package/dist/index.cjs +0 -3065
- package/dist/index.mjs +0 -3116
- 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,24 @@
|
|
|
1
|
+
import { dispatchUIEvent } from '../../event/index.js';
|
|
2
|
+
import { focus } from '../focus/focus.js';
|
|
3
|
+
import { getWindow } from '../misc/getWindow.js';
|
|
4
|
+
import { isDisabled } from '../misc/isDisabled.js';
|
|
5
|
+
|
|
6
|
+
function walkRadio(config, el, direction) {
|
|
7
|
+
const window = getWindow(el);
|
|
8
|
+
const group = Array.from(el.ownerDocument.querySelectorAll(el.name ? `input[type="radio"][name="${window.CSS.escape(el.name)}"]` : `input[type="radio"][name=""], input[type="radio"]:not([name])`));
|
|
9
|
+
for(let i = group.findIndex((e)=>e === el) + direction;; i += direction){
|
|
10
|
+
if (!group[i]) {
|
|
11
|
+
i = direction > 0 ? 0 : group.length - 1;
|
|
12
|
+
}
|
|
13
|
+
if (group[i] === el) {
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
if (isDisabled(group[i])) {
|
|
17
|
+
continue;
|
|
18
|
+
}
|
|
19
|
+
focus(group[i]);
|
|
20
|
+
dispatchUIEvent(config, group[i], 'click');
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export { walkRadio };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { eventWrapper } from '../misc/eventWrapper.js';
|
|
2
|
+
import { getActiveElement } from './getActiveElement.js';
|
|
3
|
+
import { isFocusable } from './isFocusable.js';
|
|
4
|
+
|
|
5
|
+
function blur(element) {
|
|
6
|
+
if (!isFocusable(element)) return;
|
|
7
|
+
const wasActive = getActiveElement(element.ownerDocument) === element;
|
|
8
|
+
if (!wasActive) return;
|
|
9
|
+
eventWrapper(()=>element.blur());
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export { blur };
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import '../click/isClickableInput.js';
|
|
2
|
+
import { createDataTransfer } from '../dataTransfer/DataTransfer.js';
|
|
3
|
+
import '../dataTransfer/Clipboard.js';
|
|
4
|
+
import '../edit/maxLength.js';
|
|
5
|
+
import '../edit/isEditable.js';
|
|
6
|
+
import '@testing-library/dom';
|
|
7
|
+
import { hasOwnSelection } from './selection.js';
|
|
8
|
+
import { getWindow } from '../misc/getWindow.js';
|
|
9
|
+
import '../keyDef/readNextDescriptor.js';
|
|
10
|
+
import '../misc/level.js';
|
|
11
|
+
import '../../options.js';
|
|
12
|
+
import '../../event/eventMap.js';
|
|
13
|
+
import '../../event/behavior/click.js';
|
|
14
|
+
import '../../event/behavior/cut.js';
|
|
15
|
+
import '../../event/behavior/keydown.js';
|
|
16
|
+
import '../../event/behavior/keypress.js';
|
|
17
|
+
import '../../event/behavior/keyup.js';
|
|
18
|
+
import '../../event/behavior/paste.js';
|
|
19
|
+
import { getUISelection } from '../../document/selection.js';
|
|
20
|
+
import { getUIValue } from '../../document/value.js';
|
|
21
|
+
|
|
22
|
+
function copySelection(target) {
|
|
23
|
+
const data = hasOwnSelection(target) ? {
|
|
24
|
+
'text/plain': readSelectedValueFromInput(target)
|
|
25
|
+
} : {
|
|
26
|
+
'text/plain': String(target.ownerDocument.getSelection())
|
|
27
|
+
};
|
|
28
|
+
const dt = createDataTransfer(getWindow(target));
|
|
29
|
+
for(const type in data){
|
|
30
|
+
if (data[type]) {
|
|
31
|
+
dt.setData(type, data[type]);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return dt;
|
|
35
|
+
}
|
|
36
|
+
function readSelectedValueFromInput(target) {
|
|
37
|
+
const sel = getUISelection(target);
|
|
38
|
+
const val = getUIValue(target);
|
|
39
|
+
return val.substring(sel.startOffset, sel.endOffset);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export { copySelection };
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import '../click/isClickableInput.js';
|
|
2
|
+
import '../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 { isContentEditable } from '../edit/isContentEditable.js';
|
|
12
|
+
import '../edit/maxLength.js';
|
|
13
|
+
import '../edit/isEditable.js';
|
|
14
|
+
import { isElementType } from '../misc/isElementType.js';
|
|
15
|
+
import '@testing-library/dom/dist/helpers.js';
|
|
16
|
+
import '../keyDef/readNextDescriptor.js';
|
|
17
|
+
import '../misc/level.js';
|
|
18
|
+
import '../../options.js';
|
|
19
|
+
|
|
20
|
+
function getNextCursorPosition(node, offset, direction, inputType) {
|
|
21
|
+
// The behavior at text node zero offset is inconsistent.
|
|
22
|
+
// When walking backwards:
|
|
23
|
+
// Firefox always moves to zero offset and jumps over last offset.
|
|
24
|
+
// Chrome jumps over zero offset per default but over last offset when Shift is pressed.
|
|
25
|
+
// The cursor always moves to zero offset if the focus area (contenteditable or body) ends there.
|
|
26
|
+
// When walking foward both ignore zero offset.
|
|
27
|
+
// When walking over input elements the cursor moves before or after that element.
|
|
28
|
+
// When walking over line breaks the cursor moves inside any following text node.
|
|
29
|
+
if (isTextNode(node) && offset + direction >= 0 && offset + direction <= node.nodeValue.length) {
|
|
30
|
+
return {
|
|
31
|
+
node,
|
|
32
|
+
offset: offset + direction
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
const nextNode = getNextCharacterContentNode(node, offset, direction);
|
|
36
|
+
if (nextNode) {
|
|
37
|
+
if (isTextNode(nextNode)) {
|
|
38
|
+
return {
|
|
39
|
+
node: nextNode,
|
|
40
|
+
offset: direction > 0 ? Math.min(1, nextNode.nodeValue.length) : Math.max(nextNode.nodeValue.length - 1, 0)
|
|
41
|
+
};
|
|
42
|
+
} else if (isElementType(nextNode, 'br')) {
|
|
43
|
+
const nextPlusOne = getNextCharacterContentNode(nextNode, undefined, direction);
|
|
44
|
+
if (!nextPlusOne) {
|
|
45
|
+
// The behavior when there is no possible cursor position beyond the line break is inconsistent.
|
|
46
|
+
// In Chrome outside of contenteditable moving before a leading line break is possible.
|
|
47
|
+
// A leading line break can still be removed per deleteContentBackward.
|
|
48
|
+
// A trailing line break on the other hand is not removed by deleteContentForward.
|
|
49
|
+
if (direction < 0 && inputType === 'deleteContentBackward') {
|
|
50
|
+
return {
|
|
51
|
+
node: nextNode.parentNode,
|
|
52
|
+
offset: getOffset(nextNode)
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
return undefined;
|
|
56
|
+
} else if (isTextNode(nextPlusOne)) {
|
|
57
|
+
return {
|
|
58
|
+
node: nextPlusOne,
|
|
59
|
+
offset: direction > 0 ? 0 : nextPlusOne.nodeValue.length
|
|
60
|
+
};
|
|
61
|
+
} else if (direction < 0 && isElementType(nextPlusOne, 'br')) {
|
|
62
|
+
return {
|
|
63
|
+
node: nextNode.parentNode,
|
|
64
|
+
offset: getOffset(nextNode)
|
|
65
|
+
};
|
|
66
|
+
} else {
|
|
67
|
+
return {
|
|
68
|
+
node: nextPlusOne.parentNode,
|
|
69
|
+
offset: getOffset(nextPlusOne) + (direction > 0 ? 0 : 1)
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
} else {
|
|
73
|
+
return {
|
|
74
|
+
node: nextNode.parentNode,
|
|
75
|
+
offset: getOffset(nextNode) + (direction > 0 ? 1 : 0)
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
function getNextCharacterContentNode(node, offset, direction) {
|
|
81
|
+
const nextOffset = Number(offset) + (direction < 0 ? -1 : 0);
|
|
82
|
+
if (offset !== undefined && isElement(node) && nextOffset >= 0 && nextOffset < node.children.length) {
|
|
83
|
+
node = node.children[nextOffset];
|
|
84
|
+
}
|
|
85
|
+
return walkNodes(node, direction === 1 ? 'next' : 'previous', isTreatedAsCharacterContent);
|
|
86
|
+
}
|
|
87
|
+
function isTreatedAsCharacterContent(node) {
|
|
88
|
+
if (isTextNode(node)) {
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
91
|
+
if (isElement(node)) {
|
|
92
|
+
if (isElementType(node, [
|
|
93
|
+
'input',
|
|
94
|
+
'textarea'
|
|
95
|
+
])) {
|
|
96
|
+
return node.type !== 'hidden';
|
|
97
|
+
} else if (isElementType(node, 'br')) {
|
|
98
|
+
return true;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
function getOffset(node) {
|
|
104
|
+
let i = 0;
|
|
105
|
+
while(node.previousSibling){
|
|
106
|
+
i++;
|
|
107
|
+
node = node.previousSibling;
|
|
108
|
+
}
|
|
109
|
+
return i;
|
|
110
|
+
}
|
|
111
|
+
function isElement(node) {
|
|
112
|
+
return node.nodeType === 1;
|
|
113
|
+
}
|
|
114
|
+
function isTextNode(node) {
|
|
115
|
+
return node.nodeType === 3;
|
|
116
|
+
}
|
|
117
|
+
function walkNodes(node, direction, callback) {
|
|
118
|
+
for(;;){
|
|
119
|
+
var ref;
|
|
120
|
+
const sibling = node[`${direction}Sibling`];
|
|
121
|
+
if (sibling) {
|
|
122
|
+
node = getDescendant(sibling, direction === 'next' ? 'first' : 'last');
|
|
123
|
+
if (callback(node)) {
|
|
124
|
+
return node;
|
|
125
|
+
}
|
|
126
|
+
} else if (node.parentNode && (!isElement(node.parentNode) || !isContentEditable(node.parentNode) && node.parentNode !== ((ref = node.ownerDocument) === null || ref === void 0 ? void 0 : ref.body))) {
|
|
127
|
+
node = node.parentNode;
|
|
128
|
+
} else {
|
|
129
|
+
break;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
function getDescendant(node, direction) {
|
|
134
|
+
while(node.hasChildNodes()){
|
|
135
|
+
node = node[`${direction}Child`];
|
|
136
|
+
}
|
|
137
|
+
return node;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export { getNextCursorPosition };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { eventWrapper } from '../misc/eventWrapper.js';
|
|
2
|
+
import { findClosest } from '../misc/findClosest.js';
|
|
3
|
+
import { getActiveElement } from './getActiveElement.js';
|
|
4
|
+
import { isFocusable } from './isFocusable.js';
|
|
5
|
+
import { updateSelectionOnFocus } from './selection.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Focus closest focusable element.
|
|
9
|
+
*/ function focus(element) {
|
|
10
|
+
const target = findClosest(element, isFocusable);
|
|
11
|
+
const activeElement = getActiveElement(element.ownerDocument);
|
|
12
|
+
if ((target !== null && target !== void 0 ? target : element.ownerDocument.body) === activeElement) {
|
|
13
|
+
return;
|
|
14
|
+
} else if (target) {
|
|
15
|
+
eventWrapper(()=>target.focus());
|
|
16
|
+
} else {
|
|
17
|
+
eventWrapper(()=>{
|
|
18
|
+
var ref;
|
|
19
|
+
return (ref = activeElement) === null || ref === void 0 ? void 0 : ref.blur();
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
updateSelectionOnFocus(target !== null && target !== void 0 ? target : element.ownerDocument.body);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export { focus };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { isDisabled } from '../misc/isDisabled.js';
|
|
2
|
+
|
|
3
|
+
function getActiveElement(document) {
|
|
4
|
+
const activeElement = document.activeElement;
|
|
5
|
+
if (activeElement === null || activeElement === void 0 ? void 0 : activeElement.shadowRoot) {
|
|
6
|
+
return getActiveElement(activeElement.shadowRoot);
|
|
7
|
+
} else {
|
|
8
|
+
// Browser does not yield disabled elements as document.activeElement - jsdom does
|
|
9
|
+
if (isDisabled(activeElement)) {
|
|
10
|
+
return document.ownerDocument ? /* istanbul ignore next */ document.ownerDocument.body : document.body;
|
|
11
|
+
}
|
|
12
|
+
return activeElement;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
function getActiveElementOrBody(document) {
|
|
16
|
+
var ref;
|
|
17
|
+
return (ref = getActiveElement(document)) !== null && ref !== void 0 ? ref : /* istanbul ignore next */ document.body;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export { getActiveElement, getActiveElementOrBody };
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { isDisabled } from '../misc/isDisabled.js';
|
|
2
|
+
import { isElementType } from '../misc/isElementType.js';
|
|
3
|
+
import { isVisible } from '../misc/isVisible.js';
|
|
4
|
+
import { FOCUSABLE_SELECTOR } from './selector.js';
|
|
5
|
+
|
|
6
|
+
function getTabDestination(activeElement, shift) {
|
|
7
|
+
const document = activeElement.ownerDocument;
|
|
8
|
+
const focusableElements = document.querySelectorAll(FOCUSABLE_SELECTOR);
|
|
9
|
+
const enabledElements = Array.from(focusableElements).filter((el)=>el === activeElement || !(Number(el.getAttribute('tabindex')) < 0 || isDisabled(el)));
|
|
10
|
+
// tabindex has no effect if the active element has negative tabindex
|
|
11
|
+
if (Number(activeElement.getAttribute('tabindex')) >= 0) {
|
|
12
|
+
enabledElements.sort((a, b)=>{
|
|
13
|
+
const i = Number(a.getAttribute('tabindex'));
|
|
14
|
+
const j = Number(b.getAttribute('tabindex'));
|
|
15
|
+
if (i === j) {
|
|
16
|
+
return 0;
|
|
17
|
+
} else if (i === 0) {
|
|
18
|
+
return 1;
|
|
19
|
+
} else if (j === 0) {
|
|
20
|
+
return -1;
|
|
21
|
+
}
|
|
22
|
+
return i - j;
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
const checkedRadio = {};
|
|
26
|
+
let prunedElements = [
|
|
27
|
+
document.body
|
|
28
|
+
];
|
|
29
|
+
const activeRadioGroup = isElementType(activeElement, 'input', {
|
|
30
|
+
type: 'radio'
|
|
31
|
+
}) ? activeElement.name : undefined;
|
|
32
|
+
enabledElements.forEach((currentElement)=>{
|
|
33
|
+
const el = currentElement;
|
|
34
|
+
// For radio groups keep only the active radio
|
|
35
|
+
// If there is no active radio, keep only the checked radio
|
|
36
|
+
// If there is no checked radio, treat like everything else
|
|
37
|
+
if (isElementType(el, 'input', {
|
|
38
|
+
type: 'radio'
|
|
39
|
+
}) && el.name) {
|
|
40
|
+
// If the active element is part of the group, add only that
|
|
41
|
+
if (el === activeElement) {
|
|
42
|
+
prunedElements.push(el);
|
|
43
|
+
return;
|
|
44
|
+
} else if (el.name === activeRadioGroup) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
// If we stumble upon a checked radio, remove the others
|
|
48
|
+
if (el.checked) {
|
|
49
|
+
prunedElements = prunedElements.filter((e)=>!isElementType(e, 'input', {
|
|
50
|
+
type: 'radio',
|
|
51
|
+
name: el.name
|
|
52
|
+
}));
|
|
53
|
+
prunedElements.push(el);
|
|
54
|
+
checkedRadio[el.name] = el;
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
// If we already found the checked one, skip
|
|
58
|
+
if (typeof checkedRadio[el.name] !== 'undefined') {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
prunedElements.push(el);
|
|
63
|
+
});
|
|
64
|
+
for(let index = prunedElements.findIndex((el)=>el === activeElement);;){
|
|
65
|
+
index += shift ? -1 : 1;
|
|
66
|
+
// loop at overflow
|
|
67
|
+
if (index === prunedElements.length) {
|
|
68
|
+
index = 0;
|
|
69
|
+
} else if (index === -1) {
|
|
70
|
+
index = prunedElements.length - 1;
|
|
71
|
+
}
|
|
72
|
+
if (prunedElements[index] === activeElement || prunedElements[index] === document.body || isVisible(prunedElements[index])) {
|
|
73
|
+
return prunedElements[index];
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export { getTabDestination };
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import '../click/isClickableInput.js';
|
|
2
|
+
import '../dataTransfer/Clipboard.js';
|
|
3
|
+
import '../edit/maxLength.js';
|
|
4
|
+
import '../edit/isEditable.js';
|
|
5
|
+
import '@testing-library/dom';
|
|
6
|
+
import { hasOwnSelection } from './selection.js';
|
|
7
|
+
import '@testing-library/dom/dist/helpers.js';
|
|
8
|
+
import '../keyDef/readNextDescriptor.js';
|
|
9
|
+
import '../misc/level.js';
|
|
10
|
+
import '../../options.js';
|
|
11
|
+
import '../../event/eventMap.js';
|
|
12
|
+
import '../../event/behavior/click.js';
|
|
13
|
+
import '../../event/behavior/cut.js';
|
|
14
|
+
import '../../event/behavior/keydown.js';
|
|
15
|
+
import '../../event/behavior/keypress.js';
|
|
16
|
+
import '../../event/behavior/keyup.js';
|
|
17
|
+
import '../../event/behavior/paste.js';
|
|
18
|
+
import { getUIValue } from '../../document/value.js';
|
|
19
|
+
|
|
20
|
+
function resolveCaretPosition({ target , node , offset }) {
|
|
21
|
+
if (hasOwnSelection(target)) {
|
|
22
|
+
return {
|
|
23
|
+
node: target,
|
|
24
|
+
offset: offset !== null && offset !== void 0 ? offset : getUIValue(target).length
|
|
25
|
+
};
|
|
26
|
+
} else if (node) {
|
|
27
|
+
return {
|
|
28
|
+
node,
|
|
29
|
+
offset: offset !== null && offset !== void 0 ? offset : node.nodeType === 3 ? node.nodeValue.length : node.childNodes.length
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
return findNodeAtTextOffset(target, offset);
|
|
33
|
+
}
|
|
34
|
+
function findNodeAtTextOffset(node, offset, isRoot = true) {
|
|
35
|
+
// When clicking after the content the browser behavior can be complicated:
|
|
36
|
+
// 1. If there is textContent after the last element child,
|
|
37
|
+
// the cursor is moved there.
|
|
38
|
+
// 2. If there is textContent in the last element child,
|
|
39
|
+
// the browser moves the cursor to the last non-empty text node inside this element.
|
|
40
|
+
// 3. Otherwise the cursor is moved to the end of the target.
|
|
41
|
+
let i = offset === undefined ? node.childNodes.length - 1 : 0;
|
|
42
|
+
const step = offset === undefined ? -1 : +1;
|
|
43
|
+
while(offset === undefined ? i >= (isRoot ? Math.max(node.childNodes.length - 1, 0) : 0) : i <= node.childNodes.length){
|
|
44
|
+
if (offset && i === node.childNodes.length) {
|
|
45
|
+
throw new Error('The given offset is out of bounds.');
|
|
46
|
+
}
|
|
47
|
+
const c = node.childNodes.item(i);
|
|
48
|
+
const text = String(c.textContent);
|
|
49
|
+
if (text.length) {
|
|
50
|
+
if (offset !== undefined && text.length < offset) {
|
|
51
|
+
offset -= text.length;
|
|
52
|
+
} else if (c.nodeType === 1) {
|
|
53
|
+
return findNodeAtTextOffset(c, offset, false);
|
|
54
|
+
} else {
|
|
55
|
+
// The pre-commit hooks keeps changing this
|
|
56
|
+
// See https://github.com/kentcdodds/kcd-scripts/issues/218
|
|
57
|
+
/* istanbul ignore else */ // eslint-disable-next-line no-lonely-if
|
|
58
|
+
if (c.nodeType === 3) {
|
|
59
|
+
return {
|
|
60
|
+
node: c,
|
|
61
|
+
offset: offset !== null && offset !== void 0 ? offset : c.nodeValue.length
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
i += step;
|
|
67
|
+
}
|
|
68
|
+
return {
|
|
69
|
+
node,
|
|
70
|
+
offset: node.childNodes.length
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export { resolveCaretPosition };
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import '../click/isClickableInput.js';
|
|
2
|
+
import '../dataTransfer/Clipboard.js';
|
|
3
|
+
import { getContentEditable } from '../edit/isContentEditable.js';
|
|
4
|
+
import '../edit/maxLength.js';
|
|
5
|
+
import { editableInputTypes } from '../edit/isEditable.js';
|
|
6
|
+
import '@testing-library/dom';
|
|
7
|
+
import { isElementType } from '../misc/isElementType.js';
|
|
8
|
+
import { setSelection } from './selection.js';
|
|
9
|
+
import '@testing-library/dom/dist/helpers.js';
|
|
10
|
+
import '../keyDef/readNextDescriptor.js';
|
|
11
|
+
import '../misc/level.js';
|
|
12
|
+
import '../../options.js';
|
|
13
|
+
import '../../event/eventMap.js';
|
|
14
|
+
import '../../event/behavior/click.js';
|
|
15
|
+
import '../../event/behavior/cut.js';
|
|
16
|
+
import '../../event/behavior/keydown.js';
|
|
17
|
+
import '../../event/behavior/keypress.js';
|
|
18
|
+
import '../../event/behavior/keyup.js';
|
|
19
|
+
import '../../event/behavior/paste.js';
|
|
20
|
+
import { getUISelection } from '../../document/selection.js';
|
|
21
|
+
import { getUIValue } from '../../document/value.js';
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Expand a selection like the browser does when pressing Ctrl+A.
|
|
25
|
+
*/ function selectAll(target) {
|
|
26
|
+
if (isElementType(target, 'textarea') || isElementType(target, 'input') && target.type in editableInputTypes) {
|
|
27
|
+
return setSelection({
|
|
28
|
+
focusNode: target,
|
|
29
|
+
anchorOffset: 0,
|
|
30
|
+
focusOffset: getUIValue(target).length
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
var ref;
|
|
34
|
+
const focusNode = (ref = getContentEditable(target)) !== null && ref !== void 0 ? ref : target.ownerDocument.body;
|
|
35
|
+
setSelection({
|
|
36
|
+
focusNode,
|
|
37
|
+
anchorOffset: 0,
|
|
38
|
+
focusOffset: focusNode.childNodes.length
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
function isAllSelected(target) {
|
|
42
|
+
if (isElementType(target, 'textarea') || isElementType(target, 'input') && target.type in editableInputTypes) {
|
|
43
|
+
return getUISelection(target).startOffset === 0 && getUISelection(target).endOffset === getUIValue(target).length;
|
|
44
|
+
}
|
|
45
|
+
var ref;
|
|
46
|
+
const focusNode = (ref = getContentEditable(target)) !== null && ref !== void 0 ? ref : target.ownerDocument.body;
|
|
47
|
+
const selection = target.ownerDocument.getSelection();
|
|
48
|
+
return (selection === null || selection === void 0 ? void 0 : selection.anchorNode) === focusNode && selection.focusNode === focusNode && selection.anchorOffset === 0 && selection.focusOffset === focusNode.childNodes.length;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export { isAllSelected, selectAll };
|