@testing-library/user-event 14.0.0-alpha.4 → 14.0.0-alpha.8

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.
Files changed (48) hide show
  1. package/dist/click.d.ts +1 -0
  2. package/dist/click.js +17 -0
  3. package/dist/document/index.d.ts +1 -0
  4. package/dist/document/selection.d.ts +17 -7
  5. package/dist/document/selection.js +51 -25
  6. package/dist/document/value.d.ts +1 -1
  7. package/dist/document/value.js +1 -1
  8. package/dist/keyboard/keyboardImplementation.js +3 -1
  9. package/dist/keyboard/plugins/arrow.js +7 -12
  10. package/dist/keyboard/plugins/character.js +23 -42
  11. package/dist/keyboard/plugins/combination.d.ts +5 -0
  12. package/dist/keyboard/plugins/combination.js +17 -0
  13. package/dist/keyboard/plugins/control.js +4 -12
  14. package/dist/keyboard/plugins/functional.js +9 -14
  15. package/dist/keyboard/plugins/index.js +3 -1
  16. package/dist/paste.js +8 -15
  17. package/dist/pointer/pointerMove.js +28 -17
  18. package/dist/pointer/pointerPress.js +6 -4
  19. package/dist/pointer/resolveSelectionTarget.js +7 -6
  20. package/dist/setup.d.ts +5 -2
  21. package/dist/setup.js +4 -7
  22. package/dist/tab.d.ts +1 -2
  23. package/dist/tab.js +2 -56
  24. package/dist/utils/edit/calculateNewValue.d.ts +14 -5
  25. package/dist/utils/edit/calculateNewValue.js +23 -21
  26. package/dist/utils/edit/fireInputEvent.d.ts +5 -2
  27. package/dist/utils/edit/fireInputEvent.js +17 -11
  28. package/dist/utils/edit/isContentEditable.d.ts +4 -0
  29. package/dist/utils/edit/isContentEditable.js +14 -0
  30. package/dist/utils/edit/isEditable.d.ts +1 -0
  31. package/dist/utils/edit/prepareInput.d.ts +4 -0
  32. package/dist/utils/edit/prepareInput.js +60 -0
  33. package/dist/utils/focus/focus.js +3 -0
  34. package/dist/utils/focus/getTabDestination.d.ts +1 -1
  35. package/dist/utils/focus/getTabDestination.js +4 -5
  36. package/dist/utils/focus/selectAll.d.ts +4 -0
  37. package/dist/utils/focus/selectAll.js +38 -0
  38. package/dist/utils/focus/selection.d.ts +42 -0
  39. package/dist/utils/focus/selection.js +190 -0
  40. package/dist/utils/index.d.ts +3 -3
  41. package/dist/utils/index.js +30 -30
  42. package/package.json +1 -1
  43. package/dist/utils/edit/cursorPosition.d.ts +0 -2
  44. package/dist/utils/edit/cursorPosition.js +0 -35
  45. package/dist/utils/edit/selectionRange.d.ts +0 -5
  46. package/dist/utils/edit/selectionRange.js +0 -64
  47. package/dist/utils/misc/isDocument.d.ts +0 -1
  48. package/dist/utils/misc/isDocument.js +0 -10
package/dist/setup.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { clear } from './clear';
2
- import { click, clickOptions, dblClick } from './click';
2
+ import { click, clickOptions, dblClick, tripleClick } from './click';
3
3
  import { hover, unhover } from './hover';
4
4
  import { keyboard, keyboardOptions } from './keyboard';
5
5
  import type { keyboardState } from './keyboard/types';
@@ -22,6 +22,7 @@ export declare const userEventApis: {
22
22
  pointer: typeof pointer;
23
23
  selectOptions: typeof selectOptions;
24
24
  tab: typeof tab;
25
+ tripleClick: typeof tripleClick;
25
26
  type: typeof type;
26
27
  unhover: typeof unhover;
27
28
  upload: typeof upload;
@@ -54,6 +55,7 @@ export declare function setup(options?: SetupOptions): {
54
55
  pointer: typeof pointer;
55
56
  selectOptions: typeof selectOptions;
56
57
  tab: typeof tab;
58
+ tripleClick: typeof tripleClick;
57
59
  type: typeof type;
58
60
  unhover: typeof unhover;
59
61
  upload: typeof upload;
@@ -72,12 +74,13 @@ export declare function setup(options?: SetupOptions): {
72
74
  pointer: typeof pointer;
73
75
  selectOptions: typeof selectOptions;
74
76
  tab: typeof tab;
77
+ tripleClick: typeof tripleClick;
75
78
  type: typeof type;
76
79
  unhover: typeof unhover;
77
80
  upload: typeof upload;
78
81
  } & any;
79
82
  };
80
- declare function _setup({ applyAccept, autoModify, delay, document, focusTrap, keyboardMap, pointerMap, skipAutoClose, skipClick, skipHover, skipPointerEventsCheck, }: SetupOptions, { keyboardState, pointerState, }: {
83
+ declare function _setup({ applyAccept, autoModify, delay, document, keyboardMap, pointerMap, skipAutoClose, skipClick, skipHover, skipPointerEventsCheck, }: SetupOptions, { keyboardState, pointerState, }: {
81
84
  keyboardState: keyboardState;
82
85
  pointerState: pointerState;
83
86
  }): UserEventApis & {
package/dist/setup.js CHANGED
@@ -39,6 +39,7 @@ const userEventApis = {
39
39
  pointer: _pointer.pointer,
40
40
  selectOptions: _selectOptions.selectOptions,
41
41
  tab: _tab.tab,
42
+ tripleClick: _click.tripleClick,
42
43
  type: _type.type,
43
44
  unhover: _hover.unhover,
44
45
  upload: _upload.upload
@@ -64,7 +65,6 @@ function _setup({
64
65
  autoModify,
65
66
  delay = 0,
66
67
  document,
67
- focusTrap,
68
68
  keyboardMap,
69
69
  pointerMap,
70
70
  skipAutoClose,
@@ -91,9 +91,6 @@ function _setup({
91
91
  const clickDefaults = {
92
92
  skipHover
93
93
  };
94
- const tabDefaults = {
95
- focusTrap
96
- };
97
94
  const typeDefaults = {
98
95
  delay,
99
96
  skipAutoClose,
@@ -177,11 +174,11 @@ function _setup({
177
174
  });
178
175
  },
179
176
  tab: (...args) => {
180
- args[0] = { ...tabDefaults,
181
- ...args[0]
182
- };
183
177
  return _tab.tab.call(userEvent, ...args);
184
178
  },
179
+ tripleClick: (...args) => {
180
+ return _click.tripleClick.call(userEvent, ...args);
181
+ },
185
182
  // type needs typecasting because of the overloading
186
183
  type: (...args) => {
187
184
  args[2] = { ...typeDefaults,
package/dist/tab.d.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  import type { UserEvent } from './setup';
2
2
  export interface tabOptions {
3
3
  shift?: boolean;
4
- focusTrap?: Document | Element;
5
4
  }
6
- export declare function tab(this: UserEvent, { shift, focusTrap }?: tabOptions): void;
5
+ export declare function tab(this: UserEvent, { shift }?: tabOptions): void;
package/dist/tab.js CHANGED
@@ -5,62 +5,8 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.tab = tab;
7
7
 
8
- var _dom = require("@testing-library/dom");
9
-
10
- var _utils = require("./utils");
11
-
12
8
  function tab({
13
- shift = false,
14
- focusTrap
9
+ shift
15
10
  } = {}) {
16
- var _focusTrap$ownerDocum, _focusTrap;
17
-
18
- const doc = (_focusTrap$ownerDocum = (_focusTrap = focusTrap) == null ? void 0 : _focusTrap.ownerDocument) != null ? _focusTrap$ownerDocum : document;
19
- const previousElement = (0, _utils.getActiveElement)(doc); // Never the case in our test environment
20
- // Only happens if the activeElement is inside a shadowRoot that is not part of `doc`.
21
-
22
- /* istanbul ignore next */
23
-
24
- if (!previousElement) {
25
- return;
26
- }
27
-
28
- if (!focusTrap) {
29
- focusTrap = doc;
30
- }
31
-
32
- const nextElement = (0, _utils.getTabDestination)(previousElement, shift, focusTrap);
33
- const shiftKeyInit = {
34
- key: 'Shift',
35
- keyCode: 16,
36
- shiftKey: true
37
- };
38
- const tabKeyInit = {
39
- key: 'Tab',
40
- keyCode: 9,
41
- shiftKey: shift
42
- };
43
- let continueToTab = true;
44
- if (shift) _dom.fireEvent.keyDown(previousElement, { ...shiftKeyInit
45
- });
46
- continueToTab = _dom.fireEvent.keyDown(previousElement, { ...tabKeyInit
47
- });
48
- const keyUpTarget = continueToTab ? nextElement : previousElement;
49
-
50
- if (continueToTab) {
51
- if (nextElement === doc.body) {
52
- (0, _utils.blur)(previousElement);
53
- } else {
54
- (0, _utils.focus)(nextElement);
55
- }
56
- }
57
-
58
- _dom.fireEvent.keyUp(keyUpTarget, { ...tabKeyInit
59
- });
60
-
61
- if (shift) {
62
- _dom.fireEvent.keyUp(keyUpTarget, { ...shiftKeyInit,
63
- shiftKey: false
64
- });
65
- }
11
+ this.keyboard(shift === true ? '{Shift>}{Tab}{/Shift}' : shift === false ? '[/ShiftLeft][/ShiftRight]{Tab}' : '{Tab}');
66
12
  }
@@ -1,7 +1,16 @@
1
- export declare function calculateNewValue(newEntry: string, element: HTMLElement, value?: string, selectionRange?: {
2
- selectionStart: number | null;
3
- selectionEnd: number | null;
4
- }, deleteContent?: 'backward' | 'forward'): {
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
- newSelectionStart: number;
15
+ newOffset: number;
7
16
  };
@@ -5,44 +5,46 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.calculateNewValue = calculateNewValue;
7
7
 
8
- var _selectionRange = require("./selectionRange");
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
- function calculateNewValue(newEntry, element, value = (() => {
17
- var _getValue;
18
-
19
- return (_getValue = (0, _getValue2.getValue)(element)) != null ? _getValue :
20
- /* istanbul ignore next */
21
- '';
22
- })(), selectionRange = (0, _selectionRange.getSelectionRange)(element), deleteContent) {
23
- const selectionStart = selectionRange.selectionStart === null ? value.length : selectionRange.selectionStart;
24
- const selectionEnd = selectionRange.selectionEnd === null ? value.length : selectionRange.selectionEnd;
25
- const prologEnd = Math.max(0, selectionStart === selectionEnd && deleteContent === 'backward' ? selectionStart - 1 : selectionStart);
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, selectionStart === selectionEnd && deleteContent === 'forward' ? selectionEnd + 1 : selectionEnd);
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}${newEntry}${epilog}`;
30
- const newSelectionStart = prologEnd + newEntry.length;
30
+ let newValue = `${prolog}${inputData}${epilog}`;
31
+ const newOffset = prologEnd + inputData.length;
31
32
 
32
- if (element.type === 'date' && !(0, _isValidDateValue.isValidDateValue)(element, newValue)) {
33
+ if (node.type === 'date' && !(0, _isValidDateValue.isValidDateValue)(node, newValue)) {
33
34
  newValue = value;
34
35
  }
35
36
 
36
- if (element.type === 'time' && !(0, _isValidInputTimeValue.isValidInputTimeValue)(element, newValue)) {
37
- if ((0, _isValidInputTimeValue.isValidInputTimeValue)(element, newEntry)) {
38
- newValue = newEntry;
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
- newSelectionStart
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, newSelectionStart, eventOverrides, }: {
2
+ export declare function fireInputEvent(element: HTMLElement, { newValue, newSelection, eventOverrides, }: {
3
3
  newValue: string;
4
- newSelectionStart: number;
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 _isContentEditable = require("./isContentEditable");
12
+ var _isElementType = require("../misc/isElementType");
15
13
 
16
- var _selectionRange = require("./selectionRange");
14
+ var _selection = require("../focus/selection");
17
15
 
18
16
  function fireInputEvent(element, {
19
17
  newValue,
20
- newSelectionStart,
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, _isContentEditable.isContentEditable)(element)) {
26
- element.textContent = newValue;
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 ((0, _isElementType.isElementType)(element, ['input', 'textarea'])) {
34
- (0, _document.setUIValue)(element, newValue);
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, _selectionRange.setSelectionRange)(element, newSelectionStart, newSelectionStart); // When the input event happens in the browser, React executes all event handlers
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, _selectionRange.setSelectionRange)(element, newSelectionStart, newSelectionStart);
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,4 @@
1
+ export declare function prepareInput(data: string, element: Element, inputType?: string): {
2
+ newValue: string;
3
+ commit: () => void;
4
+ } | undefined;
@@ -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
  }
@@ -1 +1 @@
1
- export declare function getTabDestination(activeElement: Element, shift: boolean, focusTrap: Document | Element): Element;
1
+ export declare function getTabDestination(activeElement: Element, shift: boolean): HTMLElement;
@@ -7,16 +7,15 @@ exports.getTabDestination = getTabDestination;
7
7
 
8
8
  var _isDisabled = require("../misc/isDisabled");
9
9
 
10
- var _isDocument = require("../misc/isDocument");
11
-
12
10
  var _isElementType = require("../misc/isElementType");
13
11
 
14
12
  var _isVisible = require("../misc/isVisible");
15
13
 
16
14
  var _selector = require("./selector");
17
15
 
18
- function getTabDestination(activeElement, shift, focusTrap) {
19
- const focusableElements = focusTrap.querySelectorAll(_selector.FOCUSABLE_SELECTOR);
16
+ function getTabDestination(activeElement, shift) {
17
+ const document = activeElement.ownerDocument;
18
+ const focusableElements = document.querySelectorAll(_selector.FOCUSABLE_SELECTOR);
20
19
  const enabledElements = Array.from(focusableElements).filter(el => el === activeElement || el.getAttribute('tabindex') !== '-1' && !(0, _isDisabled.isDisabled)(el) && // Hidden elements are not tabable
21
20
  (0, _isVisible.isVisible)(el));
22
21
 
@@ -26,7 +25,7 @@ function getTabDestination(activeElement, shift, focusTrap) {
26
25
  }
27
26
 
28
27
  const checkedRadio = {};
29
- let prunedElements = (0, _isDocument.isDocument)(focusTrap) ? [focusTrap.body] : [];
28
+ let prunedElements = [document.body];
30
29
  const activeRadioGroup = (0, _isElementType.isElementType)(activeElement, 'input', {
31
30
  type: 'radio'
32
31
  }) ? activeElement.name : undefined;
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Expand a selection like the browser does when pressing Ctrl+A.
3
+ */
4
+ export declare function selectAll(target: Element): void;
@@ -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;