@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
@@ -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,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/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/selectAll';
18
+ export * from './focus/selection';
18
19
  export * from './focus/selector';
19
20
  export * from './keyDef/readNextDescriptor';
20
21
  export * from './misc/eventWrapper';
@@ -23,7 +24,6 @@ export * from './misc/isDescendantOrSelf';
23
24
  export * from './misc/isElementType';
24
25
  export * from './misc/isVisible';
25
26
  export * from './misc/isDisabled';
26
- export * from './misc/isDocument';
27
27
  export * from './misc/wait';
28
28
  export * from './misc/hasPointerEvents';
29
29
  export * from './misc/hasFormSubmit';
@@ -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,32 @@ Object.keys(_isFocusable).forEach(function (key) {
225
212
  });
226
213
  });
227
214
 
215
+ var _selectAll = require("./focus/selectAll");
216
+
217
+ Object.keys(_selectAll).forEach(function (key) {
218
+ if (key === "default" || key === "__esModule") return;
219
+ if (key in exports && exports[key] === _selectAll[key]) return;
220
+ Object.defineProperty(exports, key, {
221
+ enumerable: true,
222
+ get: function () {
223
+ return _selectAll[key];
224
+ }
225
+ });
226
+ });
227
+
228
+ var _selection = require("./focus/selection");
229
+
230
+ Object.keys(_selection).forEach(function (key) {
231
+ if (key === "default" || key === "__esModule") return;
232
+ if (key in exports && exports[key] === _selection[key]) return;
233
+ Object.defineProperty(exports, key, {
234
+ enumerable: true,
235
+ get: function () {
236
+ return _selection[key];
237
+ }
238
+ });
239
+ });
240
+
228
241
  var _selector = require("./focus/selector");
229
242
 
230
243
  Object.keys(_selector).forEach(function (key) {
@@ -329,19 +342,6 @@ Object.keys(_isDisabled).forEach(function (key) {
329
342
  });
330
343
  });
331
344
 
332
- var _isDocument = require("./misc/isDocument");
333
-
334
- Object.keys(_isDocument).forEach(function (key) {
335
- if (key === "default" || key === "__esModule") return;
336
- if (key in exports && exports[key] === _isDocument[key]) return;
337
- Object.defineProperty(exports, key, {
338
- enumerable: true,
339
- get: function () {
340
- return _isDocument[key];
341
- }
342
- });
343
- });
344
-
345
345
  var _wait = require("./misc/wait");
346
346
 
347
347
  Object.keys(_wait).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.4",
3
+ "version": "14.0.0-alpha.8",
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
- }
@@ -1 +0,0 @@
1
- export declare function isDocument(el: Document | Element): el is Document;
@@ -1,10 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.isDocument = isDocument;
7
-
8
- function isDocument(el) {
9
- return el.nodeType === el.DOCUMENT_NODE;
10
- }