@testing-library/user-event 14.0.0-alpha.5 → 14.0.0-alpha.9
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/click.d.ts +1 -0
- package/dist/click.js +17 -0
- package/dist/document/index.d.ts +1 -0
- package/dist/document/selection.d.ts +17 -7
- package/dist/document/selection.js +33 -17
- package/dist/document/value.d.ts +1 -1
- package/dist/document/value.js +1 -1
- package/dist/keyboard/plugins/arrow.js +7 -12
- package/dist/keyboard/plugins/character.js +23 -42
- package/dist/keyboard/plugins/combination.d.ts +5 -0
- package/dist/keyboard/plugins/combination.js +17 -0
- package/dist/keyboard/plugins/control.js +4 -12
- package/dist/keyboard/plugins/functional.js +8 -13
- package/dist/keyboard/plugins/index.js +3 -1
- package/dist/paste.js +8 -15
- package/dist/pointer/pointerMove.js +28 -17
- package/dist/pointer/pointerPress.js +6 -4
- package/dist/pointer/resolveSelectionTarget.js +7 -6
- package/dist/setup.d.ts +4 -1
- package/dist/setup.js +4 -0
- package/dist/utils/edit/calculateNewValue.d.ts +14 -5
- package/dist/utils/edit/calculateNewValue.js +23 -21
- package/dist/utils/edit/fireInputEvent.d.ts +5 -2
- package/dist/utils/edit/fireInputEvent.js +17 -11
- package/dist/utils/edit/isContentEditable.d.ts +4 -0
- package/dist/utils/edit/isContentEditable.js +14 -0
- package/dist/utils/edit/isEditable.d.ts +1 -0
- package/dist/utils/edit/prepareInput.d.ts +4 -0
- package/dist/utils/edit/prepareInput.js +60 -0
- package/dist/utils/focus/focus.js +3 -0
- package/dist/utils/focus/selectAll.d.ts +4 -0
- package/dist/utils/focus/selectAll.js +38 -0
- package/dist/utils/focus/selection.d.ts +42 -0
- package/dist/utils/focus/selection.js +190 -0
- package/dist/utils/index.d.ts +3 -3
- package/dist/utils/index.js +30 -30
- package/dist/utils/pointer/firePointerEvents.d.ts +12 -1
- package/dist/utils/pointer/firePointerEvents.js +62 -5
- package/package.json +1 -1
- package/dist/utils/edit/cursorPosition.d.ts +0 -2
- package/dist/utils/edit/cursorPosition.js +0 -35
- package/dist/utils/edit/selectionRange.d.ts +0 -5
- package/dist/utils/edit/selectionRange.js +0 -64
- package/dist/utils/pointer/fakeEvent.d.ts +0 -20
- package/dist/utils/pointer/fakeEvent.js +0 -91
|
@@ -1,7 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { EditableInputType } from './isEditable';
|
|
2
|
+
/**
|
|
3
|
+
* Calculate a new text value.
|
|
4
|
+
*/
|
|
5
|
+
export declare function calculateNewValue(inputData: string, node: (HTMLInputElement & {
|
|
6
|
+
type: EditableInputType;
|
|
7
|
+
}) | HTMLTextAreaElement | (Node & {
|
|
8
|
+
nodeType: 3;
|
|
9
|
+
}) | Text, { startOffset, endOffset, }: {
|
|
10
|
+
startOffset: number;
|
|
11
|
+
endOffset: number;
|
|
12
|
+
}, inputType?: string): {
|
|
13
|
+
oldValue: string;
|
|
5
14
|
newValue: string;
|
|
6
|
-
|
|
15
|
+
newOffset: number;
|
|
7
16
|
};
|
|
@@ -5,44 +5,46 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.calculateNewValue = calculateNewValue;
|
|
7
7
|
|
|
8
|
-
var
|
|
9
|
-
|
|
10
|
-
var _getValue2 = require("./getValue");
|
|
8
|
+
var _document = require("../../document");
|
|
11
9
|
|
|
12
10
|
var _isValidDateValue = require("./isValidDateValue");
|
|
13
11
|
|
|
14
12
|
var _isValidInputTimeValue = require("./isValidInputTimeValue");
|
|
15
13
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
14
|
+
/**
|
|
15
|
+
* Calculate a new text value.
|
|
16
|
+
*/
|
|
17
|
+
// This implementation does not properly calculate a new DOM state.
|
|
18
|
+
// It only handles text values and neither cares for DOM offsets nor accounts for non-character elements.
|
|
19
|
+
// It can be used for text nodes and elements supporting value property.
|
|
20
|
+
// TODO: The implementation of `deleteContent` is brittle and should be replaced.
|
|
21
|
+
function calculateNewValue(inputData, node, {
|
|
22
|
+
startOffset,
|
|
23
|
+
endOffset
|
|
24
|
+
}, inputType) {
|
|
25
|
+
const value = node.nodeType === 3 ? String(node.nodeValue) : (0, _document.getUIValue)(node);
|
|
26
|
+
const prologEnd = Math.max(0, startOffset === endOffset && inputType === 'deleteContentBackward' ? startOffset - 1 : startOffset);
|
|
26
27
|
const prolog = value.substring(0, prologEnd);
|
|
27
|
-
const epilogStart = Math.min(value.length,
|
|
28
|
+
const epilogStart = Math.min(value.length, startOffset === endOffset && inputType === 'deleteContentForward' ? startOffset + 1 : endOffset);
|
|
28
29
|
const epilog = value.substring(epilogStart, value.length);
|
|
29
|
-
let newValue = `${prolog}${
|
|
30
|
-
const
|
|
30
|
+
let newValue = `${prolog}${inputData}${epilog}`;
|
|
31
|
+
const newOffset = prologEnd + inputData.length;
|
|
31
32
|
|
|
32
|
-
if (
|
|
33
|
+
if (node.type === 'date' && !(0, _isValidDateValue.isValidDateValue)(node, newValue)) {
|
|
33
34
|
newValue = value;
|
|
34
35
|
}
|
|
35
36
|
|
|
36
|
-
if (
|
|
37
|
-
if ((0, _isValidInputTimeValue.isValidInputTimeValue)(
|
|
38
|
-
newValue =
|
|
37
|
+
if (node.type === 'time' && !(0, _isValidInputTimeValue.isValidInputTimeValue)(node, newValue)) {
|
|
38
|
+
if ((0, _isValidInputTimeValue.isValidInputTimeValue)(node, inputData)) {
|
|
39
|
+
newValue = inputData;
|
|
39
40
|
} else {
|
|
40
41
|
newValue = value;
|
|
41
42
|
}
|
|
42
43
|
}
|
|
43
44
|
|
|
44
45
|
return {
|
|
46
|
+
oldValue: value,
|
|
45
47
|
newValue,
|
|
46
|
-
|
|
48
|
+
newOffset
|
|
47
49
|
};
|
|
48
50
|
}
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import { fireEvent } from '@testing-library/dom';
|
|
2
|
-
export declare function fireInputEvent(element: HTMLElement, { newValue,
|
|
2
|
+
export declare function fireInputEvent(element: HTMLElement, { newValue, newSelection, eventOverrides, }: {
|
|
3
3
|
newValue: string;
|
|
4
|
-
|
|
4
|
+
newSelection: {
|
|
5
|
+
node: Node;
|
|
6
|
+
offset: number;
|
|
7
|
+
};
|
|
5
8
|
eventOverrides: Partial<Parameters<typeof fireEvent>[1]> & {
|
|
6
9
|
[k: string]: unknown;
|
|
7
10
|
};
|
|
@@ -7,38 +7,40 @@ exports.fireInputEvent = fireInputEvent;
|
|
|
7
7
|
|
|
8
8
|
var _dom = require("@testing-library/dom");
|
|
9
9
|
|
|
10
|
-
var _isElementType = require("../misc/isElementType");
|
|
11
|
-
|
|
12
10
|
var _document = require("../../document");
|
|
13
11
|
|
|
14
|
-
var
|
|
12
|
+
var _isElementType = require("../misc/isElementType");
|
|
15
13
|
|
|
16
|
-
var
|
|
14
|
+
var _selection = require("../focus/selection");
|
|
17
15
|
|
|
18
16
|
function fireInputEvent(element, {
|
|
19
17
|
newValue,
|
|
20
|
-
|
|
18
|
+
newSelection,
|
|
21
19
|
eventOverrides
|
|
22
20
|
}) {
|
|
23
21
|
const oldValue = element.value; // apply the changes before firing the input event, so that input handlers can access the altered dom and selection
|
|
24
22
|
|
|
25
|
-
if ((0,
|
|
26
|
-
element
|
|
23
|
+
if ((0, _isElementType.isElementType)(element, ['input', 'textarea'])) {
|
|
24
|
+
(0, _document.setUIValue)(element, newValue);
|
|
27
25
|
} else {
|
|
28
26
|
// The pre-commit hooks keeps changing this
|
|
29
27
|
// See https://github.com/kentcdodds/kcd-scripts/issues/218
|
|
30
28
|
|
|
31
29
|
/* istanbul ignore else */
|
|
32
30
|
// eslint-disable-next-line no-lonely-if
|
|
33
|
-
if (
|
|
34
|
-
|
|
31
|
+
if (newSelection.node.nodeType === 3) {
|
|
32
|
+
newSelection.node.textContent = newValue;
|
|
35
33
|
} else {
|
|
36
34
|
// TODO: properly type guard
|
|
37
35
|
throw new Error('Invalid Element');
|
|
38
36
|
}
|
|
39
37
|
}
|
|
40
38
|
|
|
41
|
-
(0,
|
|
39
|
+
(0, _selection.setSelection)({
|
|
40
|
+
focusNode: newSelection.node,
|
|
41
|
+
anchorOffset: newSelection.offset,
|
|
42
|
+
focusOffset: newSelection.offset
|
|
43
|
+
}); // When the input event happens in the browser, React executes all event handlers
|
|
42
44
|
// and if they change state of a controlled value, nothing happens.
|
|
43
45
|
// But when we trigger the event handlers in test environment,
|
|
44
46
|
// the changes are rolled back by React before the state update is applied.
|
|
@@ -54,6 +56,10 @@ function fireInputEvent(element, {
|
|
|
54
56
|
const tracked = (0, _document.endTrackValue)(element);
|
|
55
57
|
|
|
56
58
|
if ((tracked == null ? void 0 : tracked.length) === 2 && tracked[0] === oldValue && tracked[1] === newValue) {
|
|
57
|
-
(0,
|
|
59
|
+
(0, _selection.setSelection)({
|
|
60
|
+
focusNode: newSelection.node,
|
|
61
|
+
anchorOffset: newSelection.offset,
|
|
62
|
+
focusOffset: newSelection.offset
|
|
63
|
+
});
|
|
58
64
|
}
|
|
59
65
|
}
|
|
@@ -1,3 +1,7 @@
|
|
|
1
1
|
export declare function isContentEditable(element: Element): element is HTMLElement & {
|
|
2
2
|
contenteditable: 'true';
|
|
3
3
|
};
|
|
4
|
+
/**
|
|
5
|
+
* If a node is a contenteditable or inside one, return that element.
|
|
6
|
+
*/
|
|
7
|
+
export declare function getContentEditable(node: Node): Element | null;
|
|
@@ -3,9 +3,23 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
+
exports.getContentEditable = getContentEditable;
|
|
6
7
|
exports.isContentEditable = isContentEditable;
|
|
7
8
|
|
|
8
9
|
//jsdom is not supporting isContentEditable
|
|
9
10
|
function isContentEditable(element) {
|
|
10
11
|
return element.hasAttribute('contenteditable') && (element.getAttribute('contenteditable') == 'true' || element.getAttribute('contenteditable') == '');
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* If a node is a contenteditable or inside one, return that element.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
function getContentEditable(node) {
|
|
19
|
+
const element = getElement(node);
|
|
20
|
+
return element && (element.closest('[contenteditable=""]') || element.closest('[contenteditable="true"]'));
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function getElement(node) {
|
|
24
|
+
return node.nodeType === 1 ? node : node.parentElement;
|
|
11
25
|
}
|
|
@@ -17,6 +17,7 @@ export declare enum editableInputTypes {
|
|
|
17
17
|
'url' = "url",
|
|
18
18
|
'week' = "week"
|
|
19
19
|
}
|
|
20
|
+
export declare type EditableInputType = keyof typeof editableInputTypes;
|
|
20
21
|
export declare function isEditableInput(element: Element): element is HTMLInputElement & {
|
|
21
22
|
readOnly: false;
|
|
22
23
|
type: editableInputTypes;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.prepareInput = prepareInput;
|
|
7
|
+
|
|
8
|
+
var _utils = require("../../utils");
|
|
9
|
+
|
|
10
|
+
function prepareInput(data, element, inputType = 'insertText') {
|
|
11
|
+
const inputRange = (0, _utils.getInputRange)(element); // TODO: implement for ranges on multiple nodes
|
|
12
|
+
|
|
13
|
+
/* istanbul ignore if */
|
|
14
|
+
|
|
15
|
+
if (!inputRange || 'startContainer' in inputRange && inputRange.startContainer !== inputRange.endContainer) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const node = getNode(element, inputRange);
|
|
20
|
+
const {
|
|
21
|
+
newValue,
|
|
22
|
+
newOffset,
|
|
23
|
+
oldValue
|
|
24
|
+
} = (0, _utils.calculateNewValue)(data, node, inputRange, inputType);
|
|
25
|
+
|
|
26
|
+
if (newValue === oldValue && newOffset === inputRange.startOffset && newOffset === inputRange.endOffset) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return {
|
|
31
|
+
newValue,
|
|
32
|
+
commit: () => (0, _utils.fireInputEvent)(element, {
|
|
33
|
+
newValue,
|
|
34
|
+
newSelection: {
|
|
35
|
+
node,
|
|
36
|
+
offset: newOffset
|
|
37
|
+
},
|
|
38
|
+
eventOverrides: {
|
|
39
|
+
inputType
|
|
40
|
+
}
|
|
41
|
+
})
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function getNode(element, inputRange) {
|
|
46
|
+
if ('startContainer' in inputRange) {
|
|
47
|
+
if (inputRange.startContainer.nodeType === 3) {
|
|
48
|
+
return inputRange.startContainer;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
try {
|
|
52
|
+
return inputRange.startContainer.insertBefore(element.ownerDocument.createTextNode(''), inputRange.startContainer.childNodes.item(inputRange.startOffset));
|
|
53
|
+
} catch {
|
|
54
|
+
/* istanbul ignore next */
|
|
55
|
+
throw new Error('Invalid operation. Can not insert text at this position. The behavior is not implemented yet.');
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return element;
|
|
60
|
+
}
|
|
@@ -11,9 +11,12 @@ var _getActiveElement = require("./getActiveElement");
|
|
|
11
11
|
|
|
12
12
|
var _isFocusable = require("./isFocusable");
|
|
13
13
|
|
|
14
|
+
var _selection = require("./selection");
|
|
15
|
+
|
|
14
16
|
function focus(element) {
|
|
15
17
|
if (!(0, _isFocusable.isFocusable)(element)) return;
|
|
16
18
|
const isAlreadyActive = (0, _getActiveElement.getActiveElement)(element.ownerDocument) === element;
|
|
17
19
|
if (isAlreadyActive) return;
|
|
18
20
|
(0, _eventWrapper.eventWrapper)(() => element.focus());
|
|
21
|
+
(0, _selection.updateSelectionOnFocus)(element);
|
|
19
22
|
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.selectAll = selectAll;
|
|
7
|
+
|
|
8
|
+
var _document = require("../../document");
|
|
9
|
+
|
|
10
|
+
var _isContentEditable = require("../edit/isContentEditable");
|
|
11
|
+
|
|
12
|
+
var _isEditable = require("../edit/isEditable");
|
|
13
|
+
|
|
14
|
+
var _isElementType = require("../misc/isElementType");
|
|
15
|
+
|
|
16
|
+
var _selection = require("./selection");
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Expand a selection like the browser does when pressing Ctrl+A.
|
|
20
|
+
*/
|
|
21
|
+
function selectAll(target) {
|
|
22
|
+
var _getContentEditable;
|
|
23
|
+
|
|
24
|
+
if ((0, _isElementType.isElementType)(target, 'textarea') || (0, _isElementType.isElementType)(target, 'input') && target.type in _isEditable.editableInputTypes) {
|
|
25
|
+
return (0, _selection.setSelection)({
|
|
26
|
+
focusNode: target,
|
|
27
|
+
anchorOffset: 0,
|
|
28
|
+
focusOffset: (0, _document.getUIValue)(target).length
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const focusNode = (_getContentEditable = (0, _isContentEditable.getContentEditable)(target)) != null ? _getContentEditable : target.ownerDocument.body;
|
|
33
|
+
(0, _selection.setSelection)({
|
|
34
|
+
focusNode,
|
|
35
|
+
anchorOffset: 0,
|
|
36
|
+
focusOffset: focusNode.childNodes.length
|
|
37
|
+
});
|
|
38
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { UISelectionRange } from '../../document';
|
|
2
|
+
import { editableInputTypes } from '../edit/isEditable';
|
|
3
|
+
/**
|
|
4
|
+
* Backward-compatible selection.
|
|
5
|
+
*
|
|
6
|
+
* Handles input elements and contenteditable if it only contains a single text node.
|
|
7
|
+
*/
|
|
8
|
+
export declare function setSelectionRange(element: Element, anchorOffset: number, focusOffset: number): void;
|
|
9
|
+
/**
|
|
10
|
+
* Determine if the element has its own selection implementation
|
|
11
|
+
* and does not interact with the Document Selection API.
|
|
12
|
+
*/
|
|
13
|
+
export declare function hasOwnSelection(node: Node): node is HTMLTextAreaElement | (HTMLInputElement & {
|
|
14
|
+
type: editableInputTypes;
|
|
15
|
+
});
|
|
16
|
+
/**
|
|
17
|
+
* Reset the Document Selection when moving focus into an element
|
|
18
|
+
* with own selection implementation.
|
|
19
|
+
*/
|
|
20
|
+
export declare function updateSelectionOnFocus(element: Element): void;
|
|
21
|
+
/**
|
|
22
|
+
* Get the range that would be overwritten by input.
|
|
23
|
+
*/
|
|
24
|
+
export declare function getInputRange(focusNode: Node): UISelectionRange | Range | undefined;
|
|
25
|
+
/**
|
|
26
|
+
* Extend/shrink the selection like with Shift+Arrows or Shift+Mouse
|
|
27
|
+
*/
|
|
28
|
+
export declare function modifySelection({ focusNode, focusOffset, }: {
|
|
29
|
+
focusNode: Node;
|
|
30
|
+
/** DOM Offset */
|
|
31
|
+
focusOffset: number;
|
|
32
|
+
}): void;
|
|
33
|
+
/**
|
|
34
|
+
* Set the selection
|
|
35
|
+
*/
|
|
36
|
+
export declare function setSelection({ focusNode, focusOffset, anchorNode, anchorOffset, }: {
|
|
37
|
+
anchorNode?: Node;
|
|
38
|
+
/** DOM offset */
|
|
39
|
+
anchorOffset?: number;
|
|
40
|
+
focusNode: Node;
|
|
41
|
+
focusOffset: number;
|
|
42
|
+
}): void;
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.getInputRange = getInputRange;
|
|
7
|
+
exports.hasOwnSelection = hasOwnSelection;
|
|
8
|
+
exports.modifySelection = modifySelection;
|
|
9
|
+
exports.setSelection = setSelection;
|
|
10
|
+
exports.setSelectionRange = setSelectionRange;
|
|
11
|
+
exports.updateSelectionOnFocus = updateSelectionOnFocus;
|
|
12
|
+
|
|
13
|
+
var _isElementType = require("../misc/isElementType");
|
|
14
|
+
|
|
15
|
+
var _document = require("../../document");
|
|
16
|
+
|
|
17
|
+
var _isEditable = require("../edit/isEditable");
|
|
18
|
+
|
|
19
|
+
var _isContentEditable = require("../edit/isContentEditable");
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Backward-compatible selection.
|
|
23
|
+
*
|
|
24
|
+
* Handles input elements and contenteditable if it only contains a single text node.
|
|
25
|
+
*/
|
|
26
|
+
function setSelectionRange(element, anchorOffset, focusOffset) {
|
|
27
|
+
var _element$firstChild;
|
|
28
|
+
|
|
29
|
+
if (hasOwnSelection(element)) {
|
|
30
|
+
return setSelection({
|
|
31
|
+
focusNode: element,
|
|
32
|
+
anchorOffset,
|
|
33
|
+
focusOffset
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
/* istanbul ignore else */
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
if ((0, _isContentEditable.isContentEditable)(element) && ((_element$firstChild = element.firstChild) == null ? void 0 : _element$firstChild.nodeType) === 3) {
|
|
40
|
+
return setSelection({
|
|
41
|
+
focusNode: element.firstChild,
|
|
42
|
+
anchorOffset,
|
|
43
|
+
focusOffset
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
/* istanbul ignore next */
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
throw new Error('Not implemented. The result of this interaction is unreliable.');
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Determine if the element has its own selection implementation
|
|
53
|
+
* and does not interact with the Document Selection API.
|
|
54
|
+
*/
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
function hasOwnSelection(node) {
|
|
58
|
+
return isElement(node) && ((0, _isElementType.isElementType)(node, 'textarea') || (0, _isElementType.isElementType)(node, 'input') && node.type in _isEditable.editableInputTypes);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function isElement(node) {
|
|
62
|
+
return node.nodeType === 1;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Determine which selection logic and selection ranges to consider.
|
|
66
|
+
*/
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
function getTargetTypeAndSelection(node) {
|
|
70
|
+
const element = getElement(node);
|
|
71
|
+
|
|
72
|
+
if (element && hasOwnSelection(element)) {
|
|
73
|
+
return {
|
|
74
|
+
type: 'input',
|
|
75
|
+
selection: (0, _document.getUISelection)(element)
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const selection = element == null ? void 0 : element.ownerDocument.getSelection(); // It is possible to extend a single-range selection into a contenteditable.
|
|
80
|
+
// This results in the range acting like a range outside of contenteditable.
|
|
81
|
+
|
|
82
|
+
const isCE = (0, _isContentEditable.getContentEditable)(node) && (selection == null ? void 0 : selection.anchorNode) && (0, _isContentEditable.getContentEditable)(selection.anchorNode);
|
|
83
|
+
return {
|
|
84
|
+
type: isCE ? 'contenteditable' : 'default',
|
|
85
|
+
selection
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function getElement(node) {
|
|
90
|
+
return node.nodeType === 1 ? node : node.parentElement;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Reset the Document Selection when moving focus into an element
|
|
94
|
+
* with own selection implementation.
|
|
95
|
+
*/
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
function updateSelectionOnFocus(element) {
|
|
99
|
+
const selection = element.ownerDocument.getSelection();
|
|
100
|
+
/* istanbul ignore if */
|
|
101
|
+
|
|
102
|
+
if (!(selection != null && selection.focusNode)) {
|
|
103
|
+
return;
|
|
104
|
+
} // If the focus moves inside an element with own selection implementation,
|
|
105
|
+
// the document selection will be this element.
|
|
106
|
+
// But if the focused element is inside a contenteditable,
|
|
107
|
+
// 1) a collapsed selection will be retained.
|
|
108
|
+
// 2) other selections will be replaced by a cursor
|
|
109
|
+
// 2.a) at the start of the first child if it is a text node
|
|
110
|
+
// 2.b) at the start of the contenteditable.
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
if (hasOwnSelection(element)) {
|
|
114
|
+
const contenteditable = (0, _isContentEditable.getContentEditable)(selection.focusNode);
|
|
115
|
+
|
|
116
|
+
if (contenteditable) {
|
|
117
|
+
if (!selection.isCollapsed) {
|
|
118
|
+
var _contenteditable$firs;
|
|
119
|
+
|
|
120
|
+
const focusNode = ((_contenteditable$firs = contenteditable.firstChild) == null ? void 0 : _contenteditable$firs.nodeType) === 3 ? contenteditable.firstChild : contenteditable;
|
|
121
|
+
selection.setBaseAndExtent(focusNode, 0, focusNode, 0);
|
|
122
|
+
}
|
|
123
|
+
} else {
|
|
124
|
+
selection.setBaseAndExtent(element, 0, element, 0);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Get the range that would be overwritten by input.
|
|
130
|
+
*/
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
function getInputRange(focusNode) {
|
|
134
|
+
const typeAndSelection = getTargetTypeAndSelection(focusNode);
|
|
135
|
+
|
|
136
|
+
if (typeAndSelection.type === 'input') {
|
|
137
|
+
return typeAndSelection.selection;
|
|
138
|
+
} else if (typeAndSelection.type === 'contenteditable') {
|
|
139
|
+
var _typeAndSelection$sel;
|
|
140
|
+
|
|
141
|
+
// Multi-range on contenteditable edits the first selection instead of the last
|
|
142
|
+
return (_typeAndSelection$sel = typeAndSelection.selection) == null ? void 0 : _typeAndSelection$sel.getRangeAt(0);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Extend/shrink the selection like with Shift+Arrows or Shift+Mouse
|
|
147
|
+
*/
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
function modifySelection({
|
|
151
|
+
focusNode,
|
|
152
|
+
focusOffset
|
|
153
|
+
}) {
|
|
154
|
+
var _focusNode$ownerDocum, _focusNode$ownerDocum2;
|
|
155
|
+
|
|
156
|
+
const typeAndSelection = getTargetTypeAndSelection(focusNode);
|
|
157
|
+
|
|
158
|
+
if (typeAndSelection.type === 'input') {
|
|
159
|
+
return (0, _document.setUISelection)(focusNode, {
|
|
160
|
+
anchorOffset: typeAndSelection.selection.anchorOffset,
|
|
161
|
+
focusOffset
|
|
162
|
+
}, 'modify');
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
(_focusNode$ownerDocum = focusNode.ownerDocument) == null ? void 0 : (_focusNode$ownerDocum2 = _focusNode$ownerDocum.getSelection()) == null ? void 0 : _focusNode$ownerDocum2.extend(focusNode, focusOffset);
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Set the selection
|
|
169
|
+
*/
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
function setSelection({
|
|
173
|
+
focusNode,
|
|
174
|
+
focusOffset,
|
|
175
|
+
anchorNode = focusNode,
|
|
176
|
+
anchorOffset = focusOffset
|
|
177
|
+
}) {
|
|
178
|
+
var _anchorNode$ownerDocu, _anchorNode$ownerDocu2;
|
|
179
|
+
|
|
180
|
+
const typeAndSelection = getTargetTypeAndSelection(focusNode);
|
|
181
|
+
|
|
182
|
+
if (typeAndSelection.type === 'input') {
|
|
183
|
+
return (0, _document.setUISelection)(focusNode, {
|
|
184
|
+
anchorOffset,
|
|
185
|
+
focusOffset
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
(_anchorNode$ownerDocu = anchorNode.ownerDocument) == null ? void 0 : (_anchorNode$ownerDocu2 = _anchorNode$ownerDocu.getSelection()) == null ? void 0 : _anchorNode$ownerDocu2.setBaseAndExtent(anchorNode, anchorOffset, focusNode, focusOffset);
|
|
190
|
+
}
|
package/dist/utils/index.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
export * from './click/isClickableInput';
|
|
2
2
|
export * from './edit/buildTimeValue';
|
|
3
3
|
export * from './edit/calculateNewValue';
|
|
4
|
-
export * from './edit/cursorPosition';
|
|
5
4
|
export * from './edit/fireInputEvent';
|
|
6
5
|
export * from './edit/getValue';
|
|
7
6
|
export * from './edit/isContentEditable';
|
|
@@ -9,12 +8,14 @@ export * from './edit/isEditable';
|
|
|
9
8
|
export * from './edit/isValidDateValue';
|
|
10
9
|
export * from './edit/isValidInputTimeValue';
|
|
11
10
|
export * from './edit/maxLength';
|
|
12
|
-
export * from './edit/
|
|
11
|
+
export * from './edit/prepareInput';
|
|
13
12
|
export * from './focus/blur';
|
|
14
13
|
export * from './focus/focus';
|
|
15
14
|
export * from './focus/getActiveElement';
|
|
16
15
|
export * from './focus/getTabDestination';
|
|
17
16
|
export * from './focus/isFocusable';
|
|
17
|
+
export * from './focus/selectAll';
|
|
18
|
+
export * from './focus/selection';
|
|
18
19
|
export * from './focus/selector';
|
|
19
20
|
export * from './keyDef/readNextDescriptor';
|
|
20
21
|
export * from './misc/eventWrapper';
|
|
@@ -26,6 +27,5 @@ export * from './misc/isDisabled';
|
|
|
26
27
|
export * from './misc/wait';
|
|
27
28
|
export * from './misc/hasPointerEvents';
|
|
28
29
|
export * from './misc/hasFormSubmit';
|
|
29
|
-
export * from './pointer/fakeEvent';
|
|
30
30
|
export * from './pointer/firePointerEvents';
|
|
31
31
|
export * from './pointer/mouseButtons';
|