@testing-library/user-event 14.0.0-alpha.6 → 14.0.0-alpha.7

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.
@@ -7,3 +7,4 @@ declare global {
7
7
  export declare function prepareDocument(document: Document): void;
8
8
  export { getUIValue, setUIValue, startTrackValue, endTrackValue } from './value';
9
9
  export { getUISelection, setUISelection } from './selection';
10
+ export type { UISelectionRange } from './selection';
@@ -1,17 +1,27 @@
1
1
  declare const UISelection: unique symbol;
2
+ export interface UISelectionRange {
3
+ startOffset: number;
4
+ endOffset: number;
5
+ }
6
+ export interface UISelection {
7
+ anchorOffset: number;
8
+ focusOffset: number;
9
+ }
2
10
  declare global {
3
11
  interface Element {
4
- [UISelection]?: {
5
- start: number;
6
- end: number;
7
- };
12
+ [UISelection]?: UISelection;
8
13
  }
9
14
  }
10
15
  export declare function prepareSelectionInterceptor(element: HTMLInputElement | HTMLTextAreaElement): void;
11
- export declare function setUISelection(element: HTMLInputElement | HTMLTextAreaElement, start: number, end: number): void;
16
+ export declare function setUISelection(element: HTMLInputElement | HTMLTextAreaElement, { focusOffset: focusOffsetParam, anchorOffset: anchorOffsetParam, }: {
17
+ anchorOffset?: number;
18
+ focusOffset: number;
19
+ }, mode?: 'replace' | 'modify'): void;
12
20
  export declare function getUISelection(element: HTMLInputElement | HTMLTextAreaElement): {
13
- selectionStart: number | null;
14
- selectionEnd: number | null;
21
+ startOffset: number;
22
+ endOffset: number;
23
+ anchorOffset: number;
24
+ focusOffset: number;
15
25
  };
16
26
  export declare function clearUISelection(element: HTMLInputElement | HTMLTextAreaElement): void;
17
27
  export {};
@@ -8,6 +8,8 @@ exports.getUISelection = getUISelection;
8
8
  exports.prepareSelectionInterceptor = prepareSelectionInterceptor;
9
9
  exports.setUISelection = setUISelection;
10
10
 
11
+ var _ = require(".");
12
+
11
13
  var _interceptor = require("./interceptor");
12
14
 
13
15
  const UISelection = Symbol('Displayed selection in UI');
@@ -15,10 +17,11 @@ const UISelection = Symbol('Displayed selection in UI');
15
17
  function prepareSelectionInterceptor(element) {
16
18
  (0, _interceptor.prepareInterceptor)(element, 'setSelectionRange', function interceptorImpl(start, end, direction = 'none') {
17
19
  const isUI = start && typeof start === 'object' && start[UISelection];
18
- this[UISelection] = isUI ? {
19
- start: start.valueOf(),
20
- end: Number(end)
21
- } : undefined;
20
+
21
+ if (!isUI) {
22
+ this[UISelection] = undefined;
23
+ }
24
+
22
25
  return {
23
26
  realArgs: [Number(start), end, direction]
24
27
  };
@@ -37,35 +40,48 @@ function prepareSelectionInterceptor(element) {
37
40
  });
38
41
  }
39
42
 
40
- function setUISelection(element, start, end) {
43
+ function setUISelection(element, {
44
+ focusOffset: focusOffsetParam,
45
+ anchorOffset: anchorOffsetParam = focusOffsetParam
46
+ }, mode = 'replace') {
47
+ const valueLength = (0, _.getUIValue)(element).length;
48
+
49
+ const sanitizeOffset = o => Math.max(0, Math.min(valueLength, o));
50
+
51
+ const anchorOffset = mode === 'replace' || element[UISelection] === undefined ? sanitizeOffset(anchorOffsetParam) : element[UISelection].anchorOffset;
52
+ const focusOffset = sanitizeOffset(focusOffsetParam);
53
+ const startOffset = Math.min(anchorOffset, focusOffset);
54
+ const endOffset = Math.max(anchorOffset, focusOffset);
41
55
  element[UISelection] = {
42
- start,
43
- end
56
+ anchorOffset,
57
+ focusOffset
44
58
  };
45
59
 
46
- if (element.selectionStart === start && element.selectionEnd === end) {
60
+ if (element.selectionStart === startOffset && element.selectionEnd === endOffset) {
47
61
  return;
48
62
  } // eslint-disable-next-line no-new-wrappers
49
63
 
50
64
 
51
- const startObj = new Number(start);
65
+ const startObj = new Number(startOffset);
52
66
  startObj[UISelection] = UISelection;
53
67
 
54
68
  try {
55
- element.setSelectionRange(startObj, end);
69
+ element.setSelectionRange(startObj, endOffset);
56
70
  } catch {// DOMException for invalid state is expected when calling this
57
71
  // on an element without support for setSelectionRange
58
72
  }
59
73
  }
60
74
 
61
75
  function getUISelection(element) {
62
- const ui = element[UISelection];
63
- return ui === undefined ? {
64
- selectionStart: element.selectionStart,
65
- selectionEnd: element.selectionEnd
66
- } : {
67
- selectionStart: ui.start,
68
- selectionEnd: ui.end
76
+ var _element$UISelection, _element$selectionSta, _element$selectionEnd;
77
+
78
+ const sel = (_element$UISelection = element[UISelection]) != null ? _element$UISelection : {
79
+ anchorOffset: (_element$selectionSta = element.selectionStart) != null ? _element$selectionSta : 0,
80
+ focusOffset: (_element$selectionEnd = element.selectionEnd) != null ? _element$selectionEnd : 0
81
+ };
82
+ return { ...sel,
83
+ startOffset: Math.min(sel.anchorOffset, sel.focusOffset),
84
+ endOffset: Math.max(sel.anchorOffset, sel.focusOffset)
69
85
  };
70
86
  }
71
87
 
@@ -10,7 +10,7 @@ declare global {
10
10
  }
11
11
  export declare function prepareValueInterceptor(element: HTMLInputElement): void;
12
12
  export declare function setUIValue(element: HTMLInputElement | HTMLTextAreaElement, value: string): void;
13
- export declare function getUIValue(element: HTMLInputElement | HTMLTextAreaElement): string | undefined;
13
+ export declare function getUIValue(element: HTMLInputElement | HTMLTextAreaElement): string;
14
14
  export declare function setInitialValue(element: HTMLInputElement | HTMLTextAreaElement): void;
15
15
  export declare function getInitialValue(element: HTMLInputElement | HTMLTextAreaElement): string | undefined;
16
16
  export declare function startTrackValue(element: HTMLInputElement | HTMLTextAreaElement): void;
@@ -49,7 +49,7 @@ function setUIValue(element, value) {
49
49
  }
50
50
 
51
51
  function getUIValue(element) {
52
- return element[UIValue] === undefined ? element.value : element[UIValue];
52
+ return element[UIValue] === undefined ? element.value : String(element[UIValue]);
53
53
  }
54
54
 
55
55
  function setInitialValue(element) {
@@ -7,6 +7,8 @@ exports.keydownBehavior = void 0;
7
7
 
8
8
  var _utils = require("../../utils");
9
9
 
10
+ var _document = require("../../document");
11
+
10
12
  /**
11
13
  * This file should contain behavior for arrow keys as described here:
12
14
  * https://w3c.github.io/uievents-code/#key-arrowpad-section
@@ -15,19 +17,12 @@ const keydownBehavior = [{
15
17
  // TODO: implement for contentEditable
16
18
  matches: (keyDef, element) => (keyDef.key === 'ArrowLeft' || keyDef.key === 'ArrowRight') && (0, _utils.isElementType)(element, ['input', 'textarea']),
17
19
  handle: (keyDef, element) => {
18
- var _ref;
20
+ const selection = (0, _document.getUISelection)(element); // TODO: implement shift/ctrl
19
21
 
20
- const {
21
- selectionStart,
22
- selectionEnd
23
- } = (0, _utils.getSelectionRange)(element);
24
- const direction = keyDef.key === 'ArrowLeft' ? -1 : 1;
25
- const newPos = (_ref = selectionStart === selectionEnd ? (selectionStart != null ? selectionStart :
26
- /* istanbul ignore next */
27
- 0) + direction : direction < 0 ? selectionStart : selectionEnd) != null ? _ref :
28
- /* istanbul ignore next */
29
- 0;
30
- (0, _utils.setSelectionRange)(element, newPos, newPos);
22
+ (0, _utils.setSelection)({
23
+ focusNode: element,
24
+ focusOffset: selection.startOffset === selection.endOffset ? selection.focusOffset + (keyDef.key === 'ArrowLeft' ? -1 : 1) : keyDef.key === 'ArrowLeft' ? selection.startOffset : selection.endOffset
25
+ });
31
26
  }
32
27
  }];
33
28
  exports.keydownBehavior = keydownBehavior;
@@ -36,15 +36,18 @@ const keypressBehavior = [{
36
36
 
37
37
  const {
38
38
  newValue,
39
- newSelectionStart
40
- } = (0, _utils.calculateNewValue)(newEntry, element);
39
+ newOffset
40
+ } = (0, _utils.calculateNewValue)(newEntry, element, (0, _utils.getInputRange)(element));
41
41
  const prevValue = (0, _utils.getValue)(element); // this check was provided by fireInputEventIfNeeded
42
42
  // TODO: verify if it is even needed by this handler
43
43
 
44
44
  if (prevValue !== newValue) {
45
45
  (0, _utils.fireInputEvent)(element, {
46
46
  newValue,
47
- newSelectionStart,
47
+ newSelection: {
48
+ node: element,
49
+ offset: newOffset
50
+ },
48
51
  eventOverrides: {
49
52
  data: keyDef.key,
50
53
  inputType: 'insertText'
@@ -77,15 +80,18 @@ const keypressBehavior = [{
77
80
 
78
81
  const {
79
82
  newValue,
80
- newSelectionStart
81
- } = (0, _utils.calculateNewValue)(newEntry, element);
83
+ newOffset
84
+ } = (0, _utils.calculateNewValue)(newEntry, element, (0, _utils.getInputRange)(element));
82
85
  const prevValue = (0, _utils.getValue)(element); // this check was provided by fireInputEventIfNeeded
83
86
  // TODO: verify if it is even needed by this handler
84
87
 
85
88
  if (prevValue !== newValue) {
86
89
  (0, _utils.fireInputEvent)(element, {
87
90
  newValue,
88
- newSelectionStart,
91
+ newSelection: {
92
+ node: element,
93
+ offset: newOffset
94
+ },
89
95
  eventOverrides: {
90
96
  data: keyDef.key,
91
97
  inputType: 'insertText'
@@ -121,8 +127,8 @@ const keypressBehavior = [{
121
127
 
122
128
  const {
123
129
  newValue,
124
- newSelectionStart
125
- } = (0, _utils.calculateNewValue)(keyDef.key, element); // the browser allows some invalid input but not others
130
+ commit
131
+ } = (0, _utils.prepareInput)(keyDef.key, element); // the browser allows some invalid input but not others
126
132
  // it allows up to two '-' at any place before any 'e' or one directly following 'e'
127
133
  // it allows one '.' at any place before e
128
134
 
@@ -132,54 +138,29 @@ const keypressBehavior = [{
132
138
  return;
133
139
  }
134
140
 
135
- (0, _utils.fireInputEvent)(element, {
136
- newValue,
137
- newSelectionStart,
138
- eventOverrides: {
139
- data: keyDef.key,
140
- inputType: 'insertText'
141
- }
142
- });
141
+ commit();
143
142
  }
144
143
  }, {
145
144
  matches: (keyDef, element) => {
146
145
  var _keyDef$key4;
147
146
 
148
- return ((_keyDef$key4 = keyDef.key) == null ? void 0 : _keyDef$key4.length) === 1 && ((0, _utils.isElementType)(element, ['input', 'textarea'], {
147
+ return ((_keyDef$key4 = keyDef.key) == null ? void 0 : _keyDef$key4.length) === 1 && ((0, _utils.isEditableInput)(element) || (0, _utils.isElementType)(element, 'textarea', {
149
148
  readOnly: false
150
- }) && !(0, _utils.isClickableInput)(element) || (0, _utils.isContentEditable)(element)) && (0, _utils.getSpaceUntilMaxLength)(element) !== 0;
149
+ }) || (0, _utils.isContentEditable)(element)) && (0, _utils.getSpaceUntilMaxLength)(element) !== 0;
151
150
  },
152
151
  handle: (keyDef, element) => {
153
- const {
154
- newValue,
155
- newSelectionStart
156
- } = (0, _utils.calculateNewValue)(keyDef.key, element);
157
- (0, _utils.fireInputEvent)(element, {
158
- newValue,
159
- newSelectionStart,
160
- eventOverrides: {
161
- data: keyDef.key,
162
- inputType: 'insertText'
163
- }
164
- });
152
+ var _prepareInput;
153
+
154
+ (_prepareInput = (0, _utils.prepareInput)(keyDef.key, element)) == null ? void 0 : _prepareInput.commit();
165
155
  }
166
156
  }, {
167
157
  matches: (keyDef, element) => keyDef.key === 'Enter' && ((0, _utils.isElementType)(element, 'textarea', {
168
158
  readOnly: false
169
159
  }) || (0, _utils.isContentEditable)(element)) && (0, _utils.getSpaceUntilMaxLength)(element) !== 0,
170
160
  handle: (keyDef, element, options, state) => {
171
- const {
172
- newValue,
173
- newSelectionStart
174
- } = (0, _utils.calculateNewValue)('\n', element);
175
- const inputType = (0, _utils.isContentEditable)(element) && !state.modifiers.shift ? 'insertParagraph' : 'insertLineBreak';
176
- (0, _utils.fireInputEvent)(element, {
177
- newValue,
178
- newSelectionStart,
179
- eventOverrides: {
180
- inputType
181
- }
182
- });
161
+ var _prepareInput2;
162
+
163
+ (_prepareInput2 = (0, _utils.prepareInput)('\n', element, (0, _utils.isContentEditable)(element) && !state.modifiers.shift ? 'insertParagraph' : 'insertLineBreak')) == null ? void 0 : _prepareInput2.commit();
183
164
  }
184
165
  }];
185
166
  exports.keypressBehavior = keypressBehavior;
@@ -42,19 +42,11 @@ const keydownBehavior = [{
42
42
  }
43
43
  }
44
44
  }, {
45
- matches: (keyDef, element) => keyDef.key === 'Delete' && (0, _utils.isEditable)(element) && !(0, _utils.isCursorAtEnd)(element),
45
+ matches: (keyDef, element) => keyDef.key === 'Delete' && (0, _utils.isEditable)(element),
46
46
  handle: (keDef, element) => {
47
- const {
48
- newValue,
49
- newSelectionStart
50
- } = (0, _utils.calculateNewValue)('', element, undefined, undefined, 'forward');
51
- (0, _utils.fireInputEvent)(element, {
52
- newValue,
53
- newSelectionStart,
54
- eventOverrides: {
55
- inputType: 'deleteContentForward'
56
- }
57
- });
47
+ var _prepareInput;
48
+
49
+ (_prepareInput = (0, _utils.prepareInput)('', element, 'deleteContentForward')) == null ? void 0 : _prepareInput.commit();
58
50
  }
59
51
  }];
60
52
  exports.keydownBehavior = keydownBehavior;
@@ -53,19 +53,11 @@ const keydownBehavior = [{
53
53
  state.modifiers.caps = !state.modifiers.caps;
54
54
  }
55
55
  }, {
56
- matches: (keyDef, element) => keyDef.key === 'Backspace' && (0, _utils.isEditable)(element) && !(0, _utils.isCursorAtStart)(element),
56
+ matches: (keyDef, element) => keyDef.key === 'Backspace' && (0, _utils.isEditable)(element),
57
57
  handle: (keyDef, element) => {
58
- const {
59
- newValue,
60
- newSelectionStart
61
- } = (0, _utils.calculateNewValue)('', element, undefined, undefined, 'backward');
62
- (0, _utils.fireInputEvent)(element, {
63
- newValue,
64
- newSelectionStart,
65
- eventOverrides: {
66
- inputType: 'deleteContentBackward'
67
- }
68
- });
58
+ var _prepareInput;
59
+
60
+ (_prepareInput = (0, _utils.prepareInput)('', element, 'deleteContentBackward')) == null ? void 0 : _prepareInput.commit();
69
61
  }
70
62
  }, {
71
63
  matches: keyDef => keyDef.key === 'Tab',
@@ -78,7 +70,10 @@ const keydownBehavior = [{
78
70
  (0, _utils.focus)(dest);
79
71
 
80
72
  if ((0, _utils.isElementType)(dest, ['input', 'textarea'])) {
81
- (0, _document.setUISelection)(dest, 0, dest.value.length);
73
+ (0, _document.setUISelection)(dest, {
74
+ anchorOffset: 0,
75
+ focusOffset: dest.value.length
76
+ });
82
77
  }
83
78
  }
84
79
  }
package/dist/paste.js CHANGED
@@ -17,6 +17,8 @@ function paste(element, text, init, {
17
17
  initialSelectionStart,
18
18
  initialSelectionEnd
19
19
  } = {}) {
20
+ var _prepareInput;
21
+
20
22
  // TODO: implement for contenteditable
21
23
  if (!isSupportedElement(element)) {
22
24
  throw new TypeError(`The given ${element.tagName} element is currently unsupported.
@@ -47,21 +49,12 @@ function paste(element, text, init, {
47
49
  }
48
50
 
49
51
  text = text.substr(0, (0, _utils.getSpaceUntilMaxLength)(element));
50
- const {
51
- newValue,
52
- newSelectionStart
53
- } = (0, _utils.calculateNewValue)(text, element);
52
+ const inputRange = (0, _utils.getInputRange)(element);
53
+ /* istanbul ignore if */
54
54
 
55
- _dom.fireEvent.input(element, {
56
- inputType: 'insertFromPaste',
57
- target: {
58
- value: newValue
59
- }
60
- });
55
+ if (!inputRange) {
56
+ return;
57
+ }
61
58
 
62
- (0, _utils.setSelectionRange)(element, // TODO: investigate why the selection caused by invalid parameters was expected
63
- {
64
- newSelectionStart,
65
- selectionEnd: newSelectionStart
66
- }, {});
59
+ (_prepareInput = (0, _utils.prepareInput)(text, element, 'insertFromPaste')) == null ? void 0 : _prepareInput.commit();
67
60
  }
@@ -58,31 +58,42 @@ async function pointerMove({
58
58
  fireMove(target, coords);
59
59
 
60
60
  if (selectionRange) {
61
+ // TODO: support extending range (shift)
61
62
  const selectionFocus = (0, _resolveSelectionTarget.resolveSelectionTarget)({
62
63
  target,
63
64
  node,
64
65
  offset
65
66
  });
66
67
 
67
- if ('node' in selectionRange && selectionFocus.node === selectionRange.node) {
68
- (0, _document.setUISelection)(selectionRange.node, Math.min(selectionRange.start, selectionFocus.offset), Math.max(selectionRange.end, selectionFocus.offset));
69
- } else
70
- /* istanbul ignore else */
71
- if ('setEnd' in selectionRange) {
72
- const range = selectionRange.cloneRange();
73
- const cmp = selectionRange.comparePoint(selectionFocus.node, selectionFocus.offset);
74
-
75
- if (cmp < 0) {
76
- range.setStart(selectionFocus.node, selectionFocus.offset);
77
- } else if (cmp > 0) {
78
- range.setEnd(selectionFocus.node, selectionFocus.offset);
79
- } // TODO: support multiple ranges
68
+ if ('node' in selectionRange) {
69
+ // When the mouse is dragged outside of an input/textarea,
70
+ // the selection is extended to the beginning or end of the input
71
+ // depending on pointer position.
72
+ // TODO: extend selection according to pointer position
80
73
 
81
-
82
- const selection = target.ownerDocument.getSelection();
83
- selection.removeAllRanges();
84
- selection.addRange(range.cloneRange());
74
+ /* istanbul ignore else */
75
+ if (selectionFocus.node === selectionRange.node) {
76
+ const anchorOffset = selectionFocus.offset < selectionRange.start ? selectionRange.end : selectionRange.start;
77
+ const focusOffset = selectionFocus.offset > selectionRange.end || selectionFocus.offset < selectionRange.start ? selectionFocus.offset : selectionRange.end;
78
+ (0, _document.setUISelection)(selectionRange.node, {
79
+ anchorOffset,
80
+ focusOffset
81
+ });
85
82
  }
83
+ } else {
84
+ const range = selectionRange.cloneRange();
85
+ const cmp = range.comparePoint(selectionFocus.node, selectionFocus.offset);
86
+
87
+ if (cmp < 0) {
88
+ range.setStart(selectionFocus.node, selectionFocus.offset);
89
+ } else if (cmp > 0) {
90
+ range.setEnd(selectionFocus.node, selectionFocus.offset);
91
+ }
92
+
93
+ const selection = target.ownerDocument.getSelection();
94
+ selection.removeAllRanges();
95
+ selection.addRange(range.cloneRange());
96
+ }
86
97
  }
87
98
 
88
99
  function fireMove(eventTarget, eventCoords) {
@@ -274,10 +274,13 @@ function mousedownDefaultBehavior({
274
274
  const [start, end] = node ? // As offset is describing a DOMOffset it is non-trivial to determine
275
275
  // which elements might be considered in the same line of text.
276
276
  // TODO: support expanding initial range on multiple clicks if node is given
277
- [offset, offset] : getTextRange(text, offset, clickCount);
277
+ [offset, offset] : getTextRange(text, offset, clickCount); // TODO: implement modifying selection per shift/ctrl+mouse
278
278
 
279
279
  if (hasValue) {
280
- (0, _document.setUISelection)(target, start != null ? start : text.length, end != null ? end : text.length);
280
+ (0, _document.setUISelection)(target, {
281
+ anchorOffset: start != null ? start : text.length,
282
+ focusOffset: end != null ? end : text.length
283
+ });
281
284
  position.selectionRange = {
282
285
  node: target,
283
286
  start: start != null ? start : 0,
@@ -303,8 +306,7 @@ function mousedownDefaultBehavior({
303
306
  const range = new Range();
304
307
  range.setStart(startNode, startOffset);
305
308
  range.setEnd(endNode, endOffset);
306
- position.selectionRange = range; // TODO: support multiple ranges
307
-
309
+ position.selectionRange = range;
308
310
  const selection = target.ownerDocument.getSelection();
309
311
  selection.removeAllRanges();
310
312
  selection.addRange(range.cloneRange());
@@ -15,13 +15,9 @@ function resolveSelectionTarget({
15
15
  offset
16
16
  }) {
17
17
  if ((0, _utils.isElementType)(target, ['input', 'textarea'])) {
18
- var _getUIValue;
19
-
20
18
  return {
21
19
  node: target,
22
- offset: offset != null ? offset : ((_getUIValue = (0, _document.getUIValue)(target)) != null ? _getUIValue :
23
- /* istanbul ignore next */
24
- '').length
20
+ offset: offset != null ? offset : (0, _document.getUIValue)(target).length
25
21
  };
26
22
  } else if (node) {
27
23
  return {
@@ -52,8 +48,12 @@ function findNodeAtTextOffset(node, offset, isRoot = true) {
52
48
  offset -= text.length;
53
49
  } else if (c.nodeType === 1) {
54
50
  return findNodeAtTextOffset(c, offset, false);
55
- } else
51
+ } else {
52
+ // The pre-commit hooks keeps changing this
53
+ // See https://github.com/kentcdodds/kcd-scripts/issues/218
54
+
56
55
  /* istanbul ignore else */
56
+ // eslint-disable-next-line no-lonely-if
57
57
  if (c.nodeType === 3) {
58
58
  var _offset;
59
59
 
@@ -62,6 +62,7 @@ function findNodeAtTextOffset(node, offset, isRoot = true) {
62
62
  offset: (_offset = offset) != null ? _offset : c.nodeValue.length
63
63
  };
64
64
  }
65
+ }
65
66
  }
66
67
 
67
68
  i += step;
@@ -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
  }
@@ -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
+ }
@@ -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,13 @@ 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/selectionRange';
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/selection';
18
18
  export * from './focus/selector';
19
19
  export * from './keyDef/readNextDescriptor';
20
20
  export * from './misc/eventWrapper';
@@ -43,19 +43,6 @@ Object.keys(_calculateNewValue).forEach(function (key) {
43
43
  });
44
44
  });
45
45
 
46
- var _cursorPosition = require("./edit/cursorPosition");
47
-
48
- Object.keys(_cursorPosition).forEach(function (key) {
49
- if (key === "default" || key === "__esModule") return;
50
- if (key in exports && exports[key] === _cursorPosition[key]) return;
51
- Object.defineProperty(exports, key, {
52
- enumerable: true,
53
- get: function () {
54
- return _cursorPosition[key];
55
- }
56
- });
57
- });
58
-
59
46
  var _fireInputEvent = require("./edit/fireInputEvent");
60
47
 
61
48
  Object.keys(_fireInputEvent).forEach(function (key) {
@@ -147,15 +134,15 @@ Object.keys(_maxLength).forEach(function (key) {
147
134
  });
148
135
  });
149
136
 
150
- var _selectionRange = require("./edit/selectionRange");
137
+ var _prepareInput = require("./edit/prepareInput");
151
138
 
152
- Object.keys(_selectionRange).forEach(function (key) {
139
+ Object.keys(_prepareInput).forEach(function (key) {
153
140
  if (key === "default" || key === "__esModule") return;
154
- if (key in exports && exports[key] === _selectionRange[key]) return;
141
+ if (key in exports && exports[key] === _prepareInput[key]) return;
155
142
  Object.defineProperty(exports, key, {
156
143
  enumerable: true,
157
144
  get: function () {
158
- return _selectionRange[key];
145
+ return _prepareInput[key];
159
146
  }
160
147
  });
161
148
  });
@@ -225,6 +212,19 @@ Object.keys(_isFocusable).forEach(function (key) {
225
212
  });
226
213
  });
227
214
 
215
+ var _selection = require("./focus/selection");
216
+
217
+ Object.keys(_selection).forEach(function (key) {
218
+ if (key === "default" || key === "__esModule") return;
219
+ if (key in exports && exports[key] === _selection[key]) return;
220
+ Object.defineProperty(exports, key, {
221
+ enumerable: true,
222
+ get: function () {
223
+ return _selection[key];
224
+ }
225
+ });
226
+ });
227
+
228
228
  var _selector = require("./focus/selector");
229
229
 
230
230
  Object.keys(_selector).forEach(function (key) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@testing-library/user-event",
3
- "version": "14.0.0-alpha.6",
3
+ "version": "14.0.0-alpha.7",
4
4
  "description": "Fire events the same way the user does",
5
5
  "main": "dist/index.js",
6
6
  "keywords": [
@@ -1,2 +0,0 @@
1
- export declare function isCursorAtEnd(element: Element): boolean;
2
- export declare function isCursorAtStart(element: Element): boolean;
@@ -1,35 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.isCursorAtEnd = isCursorAtEnd;
7
- exports.isCursorAtStart = isCursorAtStart;
8
-
9
- var _selectionRange = require("./selectionRange");
10
-
11
- var _getValue2 = require("./getValue");
12
-
13
- function isCursorAtEnd(element) {
14
- var _getValue;
15
-
16
- const {
17
- selectionStart,
18
- selectionEnd
19
- } = (0, _selectionRange.getSelectionRange)(element);
20
- return selectionStart === selectionEnd && (selectionStart != null ? selectionStart :
21
- /* istanbul ignore next */
22
- 0) === ((_getValue = (0, _getValue2.getValue)(element)) != null ? _getValue :
23
- /* istanbul ignore next */
24
- '').length;
25
- }
26
-
27
- function isCursorAtStart(element) {
28
- const {
29
- selectionStart,
30
- selectionEnd
31
- } = (0, _selectionRange.getSelectionRange)(element);
32
- return selectionStart === selectionEnd && (selectionStart != null ? selectionStart :
33
- /* istanbul ignore next */
34
- 0) === 0;
35
- }
@@ -1,5 +0,0 @@
1
- export declare function getSelectionRange(element: Element): {
2
- selectionStart: number | null;
3
- selectionEnd: number | null;
4
- };
5
- export declare function setSelectionRange(element: Element, newSelectionStart: number, newSelectionEnd: number): void;
@@ -1,64 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.getSelectionRange = getSelectionRange;
7
- exports.setSelectionRange = setSelectionRange;
8
-
9
- var _isElementType = require("../misc/isElementType");
10
-
11
- var _document = require("../../document");
12
-
13
- function getSelectionRange(element) {
14
- if ((0, _isElementType.isElementType)(element, ['input', 'textarea'])) {
15
- return (0, _document.getUISelection)(element);
16
- }
17
-
18
- const selection = element.ownerDocument.getSelection(); // there should be no editing if the focusNode is outside of element
19
- // TODO: properly handle selection ranges
20
-
21
- if (selection != null && selection.rangeCount && element.contains(selection.focusNode)) {
22
- const range = selection.getRangeAt(0);
23
- return {
24
- selectionStart: range.startOffset,
25
- selectionEnd: range.endOffset
26
- };
27
- } else {
28
- return {
29
- selectionStart: null,
30
- selectionEnd: null
31
- };
32
- }
33
- }
34
-
35
- function setSelectionRange(element, newSelectionStart, newSelectionEnd) {
36
- if ((0, _isElementType.isElementType)(element, ['input', 'textarea'])) {
37
- return (0, _document.setUISelection)(element, newSelectionStart, newSelectionEnd);
38
- }
39
-
40
- const {
41
- selectionStart,
42
- selectionEnd
43
- } = getSelectionRange(element); // Prevent unnecessary select events
44
- // istanbul ignore next
45
-
46
- if (selectionStart === newSelectionStart && selectionEnd === newSelectionEnd) {
47
- return;
48
- }
49
-
50
- const range = element.ownerDocument.createRange();
51
- range.selectNodeContents(element); // istanbul ignore else
52
-
53
- if (element.firstChild) {
54
- range.setStart(element.firstChild, newSelectionStart);
55
- range.setEnd(element.firstChild, newSelectionEnd);
56
- }
57
-
58
- const selection = element.ownerDocument.getSelection(); // istanbul ignore else
59
-
60
- if (selection) {
61
- selection.removeAllRanges();
62
- selection.addRange(range);
63
- }
64
- }