@testing-library/user-event 14.2.5 → 14.4.0

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 (242) hide show
  1. package/dist/cjs/clipboard/copy.js +40 -0
  2. package/dist/cjs/clipboard/cut.js +40 -0
  3. package/dist/cjs/clipboard/index.js +13 -0
  4. package/dist/cjs/clipboard/paste.js +41 -0
  5. package/dist/cjs/convenience/click.js +40 -0
  6. package/dist/cjs/convenience/hover.js +37 -0
  7. package/dist/cjs/convenience/index.js +16 -0
  8. package/dist/cjs/convenience/tab.js +9 -0
  9. package/dist/cjs/document/index.js +74 -0
  10. package/dist/cjs/document/interceptor.js +30 -0
  11. package/dist/cjs/document/selection.js +107 -0
  12. package/dist/cjs/document/setRangeText.js +21 -0
  13. package/dist/cjs/document/value.js +125 -0
  14. package/dist/cjs/event/behavior/click.js +42 -0
  15. package/dist/cjs/event/behavior/cut.js +26 -0
  16. package/dist/cjs/event/behavior/index.js +15 -0
  17. package/dist/cjs/event/behavior/keydown.js +134 -0
  18. package/dist/cjs/event/behavior/keypress.js +58 -0
  19. package/dist/cjs/event/behavior/keyup.js +24 -0
  20. package/dist/cjs/event/behavior/paste.js +29 -0
  21. package/dist/cjs/event/behavior/registry.js +7 -0
  22. package/dist/cjs/event/createEvent.js +193 -0
  23. package/dist/cjs/event/dispatchEvent.js +37 -0
  24. package/dist/cjs/event/dom-events.d.js +2 -0
  25. package/dist/cjs/event/eventMap.js +64 -0
  26. package/dist/cjs/event/index.js +24 -0
  27. package/dist/cjs/event/types.js +2 -0
  28. package/dist/cjs/event/wrapEvent.js +11 -0
  29. package/dist/cjs/index.js +14 -0
  30. package/dist/cjs/keyboard/index.js +59 -0
  31. package/dist/cjs/keyboard/keyMap.js +158 -0
  32. package/dist/cjs/keyboard/parseKeyDef.js +58 -0
  33. package/dist/cjs/options.js +42 -0
  34. package/dist/cjs/package.json +3 -0
  35. package/dist/cjs/pointer/index.js +88 -0
  36. package/dist/cjs/pointer/keyMap.js +35 -0
  37. package/dist/cjs/pointer/parseKeyDef.js +39 -0
  38. package/dist/cjs/setup/api.js +35 -0
  39. package/dist/cjs/setup/config.js +7 -0
  40. package/dist/cjs/setup/directApi.js +77 -0
  41. package/dist/cjs/setup/index.js +15 -0
  42. package/dist/cjs/setup/setup.js +97 -0
  43. package/dist/cjs/setup/wrapAsync.js +13 -0
  44. package/dist/cjs/system/index.js +31 -0
  45. package/dist/cjs/system/keyboard.js +135 -0
  46. package/dist/cjs/system/pointer/buttons.js +71 -0
  47. package/dist/cjs/system/pointer/device.js +21 -0
  48. package/dist/cjs/system/pointer/index.js +146 -0
  49. package/dist/cjs/system/pointer/mouse.js +186 -0
  50. package/dist/cjs/system/pointer/pointer.js +114 -0
  51. package/dist/cjs/utility/clear.js +42 -0
  52. package/dist/cjs/utility/index.js +16 -0
  53. package/dist/cjs/utility/selectOptions.js +125 -0
  54. package/dist/cjs/utility/type.js +41 -0
  55. package/dist/cjs/utility/upload.js +72 -0
  56. package/dist/cjs/utils/click/isClickableInput.js +22 -0
  57. package/dist/cjs/utils/dataTransfer/Blob.js +18 -0
  58. package/dist/cjs/utils/dataTransfer/Clipboard.js +168 -0
  59. package/dist/cjs/utils/dataTransfer/DataTransfer.js +124 -0
  60. package/dist/cjs/utils/dataTransfer/FileList.js +26 -0
  61. package/dist/cjs/utils/edit/buildTimeValue.js +36 -0
  62. package/dist/cjs/utils/edit/getValue.js +35 -0
  63. package/dist/cjs/utils/edit/input.js +162 -0
  64. package/dist/cjs/utils/edit/isContentEditable.js +20 -0
  65. package/dist/cjs/utils/edit/isEditable.js +35 -0
  66. package/dist/cjs/utils/edit/isValidDateOrTimeValue.js +11 -0
  67. package/dist/cjs/utils/edit/maxLength.js +38 -0
  68. package/dist/cjs/utils/edit/setFiles.js +59 -0
  69. package/dist/cjs/utils/edit/walkRadio.js +28 -0
  70. package/dist/cjs/utils/focus/blur.js +16 -0
  71. package/dist/cjs/utils/focus/copySelection.js +46 -0
  72. package/dist/cjs/utils/focus/cursor.js +143 -0
  73. package/dist/cjs/utils/focus/focus.js +26 -0
  74. package/dist/cjs/utils/focus/getActiveElement.js +24 -0
  75. package/dist/cjs/utils/focus/getTabDestination.js +82 -0
  76. package/dist/cjs/utils/focus/isFocusable.js +11 -0
  77. package/dist/cjs/utils/focus/resolveCaretPosition.js +78 -0
  78. package/dist/cjs/utils/focus/selectAll.js +54 -0
  79. package/dist/cjs/utils/focus/selection.js +287 -0
  80. package/dist/cjs/utils/focus/selector.js +16 -0
  81. package/dist/cjs/utils/index.js +108 -0
  82. package/dist/cjs/utils/keyDef/readNextDescriptor.js +92 -0
  83. package/dist/cjs/utils/misc/cloneEvent.js +9 -0
  84. package/dist/cjs/utils/misc/dom-helpers.d.js +2 -0
  85. package/dist/cjs/utils/misc/eventWrapper.js +15 -0
  86. package/dist/cjs/utils/misc/findClosest.js +16 -0
  87. package/dist/cjs/utils/misc/getDocumentFromNode.js +12 -0
  88. package/dist/cjs/utils/misc/getTreeDiff.js +27 -0
  89. package/dist/cjs/utils/misc/getWindow.js +11 -0
  90. package/dist/cjs/utils/misc/isDescendantOrSelf.js +16 -0
  91. package/dist/cjs/utils/misc/isDisabled.js +34 -0
  92. package/dist/cjs/utils/misc/isElementType.js +22 -0
  93. package/dist/cjs/utils/misc/isVisible.js +21 -0
  94. package/dist/cjs/utils/misc/level.js +20 -0
  95. package/dist/cjs/utils/misc/wait.js +16 -0
  96. package/dist/cjs/utils/pointer/cssPointerEvents.js +111 -0
  97. package/dist/esm/clipboard/copy.js +36 -0
  98. package/dist/esm/clipboard/cut.js +36 -0
  99. package/dist/esm/clipboard/index.js +3 -0
  100. package/dist/esm/clipboard/paste.js +37 -0
  101. package/dist/esm/convenience/click.js +34 -0
  102. package/dist/esm/convenience/hover.js +32 -0
  103. package/dist/esm/convenience/index.js +3 -0
  104. package/dist/esm/convenience/tab.js +5 -0
  105. package/dist/esm/document/index.js +66 -0
  106. package/dist/esm/document/interceptor.js +26 -0
  107. package/dist/esm/document/selection.js +99 -0
  108. package/dist/esm/document/setRangeText.js +17 -0
  109. package/dist/esm/document/value.js +115 -0
  110. package/dist/esm/event/behavior/click.js +40 -0
  111. package/dist/esm/event/behavior/cut.js +24 -0
  112. package/dist/esm/event/behavior/index.js +7 -0
  113. package/dist/esm/event/behavior/keydown.js +132 -0
  114. package/dist/esm/event/behavior/keypress.js +56 -0
  115. package/dist/esm/event/behavior/keyup.js +22 -0
  116. package/dist/esm/event/behavior/paste.js +27 -0
  117. package/dist/esm/event/behavior/registry.js +3 -0
  118. package/dist/esm/event/createEvent.js +189 -0
  119. package/dist/esm/event/dispatchEvent.js +33 -0
  120. package/dist/esm/event/dom-events.d.js +1 -0
  121. package/dist/esm/event/eventMap.js +57 -0
  122. package/dist/esm/event/index.js +19 -0
  123. package/dist/esm/event/types.js +1 -0
  124. package/dist/esm/event/wrapEvent.js +7 -0
  125. package/dist/esm/index.js +2 -0
  126. package/dist/esm/keyboard/index.js +54 -0
  127. package/dist/esm/keyboard/keyMap.js +154 -0
  128. package/dist/esm/keyboard/parseKeyDef.js +54 -0
  129. package/dist/esm/options.js +37 -0
  130. package/dist/esm/package.json +3 -0
  131. package/dist/esm/pointer/index.js +84 -0
  132. package/dist/esm/pointer/keyMap.js +31 -0
  133. package/dist/esm/pointer/parseKeyDef.js +35 -0
  134. package/dist/esm/setup/api.js +12 -0
  135. package/dist/esm/setup/config.js +3 -0
  136. package/dist/esm/setup/directApi.js +58 -0
  137. package/dist/esm/setup/index.js +10 -0
  138. package/dist/esm/setup/setup.js +90 -0
  139. package/dist/esm/setup/wrapAsync.js +9 -0
  140. package/dist/esm/system/index.js +27 -0
  141. package/dist/esm/system/keyboard.js +131 -0
  142. package/dist/esm/system/pointer/buttons.js +63 -0
  143. package/dist/esm/system/pointer/device.js +17 -0
  144. package/dist/esm/system/pointer/index.js +141 -0
  145. package/dist/esm/system/pointer/mouse.js +182 -0
  146. package/dist/esm/system/pointer/pointer.js +110 -0
  147. package/dist/esm/utility/clear.js +38 -0
  148. package/dist/esm/utility/index.js +4 -0
  149. package/dist/esm/utility/selectOptions.js +120 -0
  150. package/dist/esm/utility/type.js +37 -0
  151. package/dist/esm/utility/upload.js +68 -0
  152. package/dist/esm/utils/click/isClickableInput.js +18 -0
  153. package/dist/esm/utils/dataTransfer/Blob.js +14 -0
  154. package/dist/esm/utils/dataTransfer/Clipboard.js +159 -0
  155. package/dist/esm/utils/dataTransfer/DataTransfer.js +119 -0
  156. package/dist/esm/utils/dataTransfer/FileList.js +22 -0
  157. package/dist/esm/utils/edit/buildTimeValue.js +32 -0
  158. package/dist/esm/utils/edit/getValue.js +31 -0
  159. package/dist/esm/utils/edit/input.js +158 -0
  160. package/dist/esm/utils/edit/isContentEditable.js +15 -0
  161. package/dist/esm/utils/edit/isEditable.js +30 -0
  162. package/dist/esm/utils/edit/isValidDateOrTimeValue.js +7 -0
  163. package/dist/esm/utils/edit/maxLength.js +34 -0
  164. package/dist/esm/utils/edit/setFiles.js +55 -0
  165. package/dist/esm/utils/edit/walkRadio.js +24 -0
  166. package/dist/esm/utils/focus/blur.js +12 -0
  167. package/dist/esm/utils/focus/copySelection.js +42 -0
  168. package/dist/esm/utils/focus/cursor.js +139 -0
  169. package/dist/esm/utils/focus/focus.js +22 -0
  170. package/dist/esm/utils/focus/getActiveElement.js +19 -0
  171. package/dist/esm/utils/focus/getTabDestination.js +78 -0
  172. package/dist/esm/utils/focus/isFocusable.js +7 -0
  173. package/dist/esm/utils/focus/resolveCaretPosition.js +74 -0
  174. package/dist/esm/utils/focus/selectAll.js +49 -0
  175. package/dist/esm/utils/focus/selection.js +274 -0
  176. package/dist/esm/utils/focus/selector.js +12 -0
  177. package/dist/esm/utils/index.js +36 -0
  178. package/dist/esm/utils/keyDef/readNextDescriptor.js +88 -0
  179. package/dist/esm/utils/misc/cloneEvent.js +5 -0
  180. package/dist/esm/utils/misc/dom-helpers.d.js +1 -0
  181. package/dist/esm/utils/misc/eventWrapper.js +11 -0
  182. package/dist/esm/utils/misc/findClosest.js +12 -0
  183. package/dist/esm/utils/misc/getDocumentFromNode.js +8 -0
  184. package/dist/esm/utils/misc/getTreeDiff.js +23 -0
  185. package/dist/esm/utils/misc/getWindow.js +7 -0
  186. package/dist/esm/utils/misc/isDescendantOrSelf.js +12 -0
  187. package/dist/esm/utils/misc/isDisabled.js +30 -0
  188. package/dist/esm/utils/misc/isElementType.js +18 -0
  189. package/dist/esm/utils/misc/isVisible.js +17 -0
  190. package/dist/esm/utils/misc/level.js +15 -0
  191. package/dist/esm/utils/misc/wait.js +12 -0
  192. package/dist/esm/utils/pointer/cssPointerEvents.js +106 -0
  193. package/dist/types/document/index.d.ts +1 -1
  194. package/dist/types/document/selection.d.ts +1 -0
  195. package/dist/types/document/value.d.ts +4 -5
  196. package/dist/types/event/createEvent.d.ts +2 -8
  197. package/dist/types/event/eventMap.d.ts +204 -181
  198. package/dist/types/event/index.d.ts +1 -2
  199. package/dist/types/event/types.d.ts +6 -0
  200. package/dist/types/index.d.ts +2 -2
  201. package/dist/types/keyboard/index.d.ts +2 -6
  202. package/dist/types/keyboard/keyMap.d.ts +1 -1
  203. package/dist/types/keyboard/parseKeyDef.d.ts +1 -1
  204. package/dist/types/options.d.ts +4 -4
  205. package/dist/types/pointer/index.d.ts +23 -5
  206. package/dist/types/pointer/keyMap.d.ts +1 -1
  207. package/dist/types/pointer/parseKeyDef.d.ts +1 -1
  208. package/dist/types/setup/config.d.ts +3 -7
  209. package/dist/types/setup/directApi.d.ts +21 -16
  210. package/dist/types/setup/index.d.ts +1 -2
  211. package/dist/types/setup/setup.d.ts +7 -1
  212. package/dist/types/system/index.d.ts +10 -0
  213. package/dist/types/system/keyboard.d.ts +53 -0
  214. package/dist/types/system/pointer/buttons.d.ts +23 -0
  215. package/dist/types/system/pointer/device.d.ts +8 -0
  216. package/dist/types/system/pointer/index.d.ts +39 -0
  217. package/dist/types/system/pointer/mouse.d.ts +26 -0
  218. package/dist/types/system/pointer/pointer.d.ts +30 -0
  219. package/dist/types/utils/click/isClickableInput.d.ts +15 -1
  220. package/dist/types/utils/edit/walkRadio.d.ts +4 -0
  221. package/dist/types/utils/focus/getActiveElement.d.ts +1 -0
  222. package/dist/types/utils/focus/resolveCaretPosition.d.ts +8 -0
  223. package/dist/types/utils/focus/selection.d.ts +28 -1
  224. package/dist/types/utils/index.d.ts +3 -3
  225. package/dist/types/utils/misc/getTreeDiff.d.ts +1 -0
  226. package/dist/types/utils/pointer/cssPointerEvents.d.ts +6 -2
  227. package/package.json +9 -15
  228. package/dist/index.cjs +0 -3057
  229. package/dist/index.mjs +0 -3107
  230. package/dist/types/event/eventTypes.d.ts +0 -2
  231. package/dist/types/keyboard/keyboardAction.d.ts +0 -10
  232. package/dist/types/keyboard/modifiers.d.ts +0 -9
  233. package/dist/types/keyboard/types.d.ts +0 -73
  234. package/dist/types/pointer/firePointerEvents.d.ts +0 -11
  235. package/dist/types/pointer/pointerAction.d.ts +0 -7
  236. package/dist/types/pointer/pointerMove.d.ts +0 -6
  237. package/dist/types/pointer/pointerPress.d.ts +0 -8
  238. package/dist/types/pointer/resolveSelectionTarget.d.ts +0 -5
  239. package/dist/types/pointer/types.d.ts +0 -62
  240. package/dist/types/utils/keyboard/getKeyEventProps.d.ts +0 -5
  241. package/dist/types/utils/keyboard/getUIEventModifiers.d.ts +0 -15
  242. package/dist/types/utils/pointer/mouseButtons.d.ts +0 -12
@@ -0,0 +1,139 @@
1
+ import '../click/isClickableInput.js';
2
+ import '../dataTransfer/Clipboard.js';
3
+ import '../../event/eventMap.js';
4
+ import '../../event/behavior/click.js';
5
+ import '../../event/behavior/cut.js';
6
+ import '../../event/behavior/keydown.js';
7
+ import '../../event/behavior/keypress.js';
8
+ import '../../event/behavior/keyup.js';
9
+ import '../../event/behavior/paste.js';
10
+ import '@testing-library/dom';
11
+ import { isContentEditable } from '../edit/isContentEditable.js';
12
+ import '../edit/maxLength.js';
13
+ import '../edit/isEditable.js';
14
+ import { isElementType } from '../misc/isElementType.js';
15
+ import '@testing-library/dom/dist/helpers.js';
16
+ import '../keyDef/readNextDescriptor.js';
17
+ import '../misc/level.js';
18
+ import '../../options.js';
19
+
20
+ function getNextCursorPosition(node, offset, direction, inputType) {
21
+ // The behavior at text node zero offset is inconsistent.
22
+ // When walking backwards:
23
+ // Firefox always moves to zero offset and jumps over last offset.
24
+ // Chrome jumps over zero offset per default but over last offset when Shift is pressed.
25
+ // The cursor always moves to zero offset if the focus area (contenteditable or body) ends there.
26
+ // When walking foward both ignore zero offset.
27
+ // When walking over input elements the cursor moves before or after that element.
28
+ // When walking over line breaks the cursor moves inside any following text node.
29
+ if (isTextNode(node) && offset + direction >= 0 && offset + direction <= node.nodeValue.length) {
30
+ return {
31
+ node,
32
+ offset: offset + direction
33
+ };
34
+ }
35
+ const nextNode = getNextCharacterContentNode(node, offset, direction);
36
+ if (nextNode) {
37
+ if (isTextNode(nextNode)) {
38
+ return {
39
+ node: nextNode,
40
+ offset: direction > 0 ? Math.min(1, nextNode.nodeValue.length) : Math.max(nextNode.nodeValue.length - 1, 0)
41
+ };
42
+ } else if (isElementType(nextNode, 'br')) {
43
+ const nextPlusOne = getNextCharacterContentNode(nextNode, undefined, direction);
44
+ if (!nextPlusOne) {
45
+ // The behavior when there is no possible cursor position beyond the line break is inconsistent.
46
+ // In Chrome outside of contenteditable moving before a leading line break is possible.
47
+ // A leading line break can still be removed per deleteContentBackward.
48
+ // A trailing line break on the other hand is not removed by deleteContentForward.
49
+ if (direction < 0 && inputType === 'deleteContentBackward') {
50
+ return {
51
+ node: nextNode.parentNode,
52
+ offset: getOffset(nextNode)
53
+ };
54
+ }
55
+ return undefined;
56
+ } else if (isTextNode(nextPlusOne)) {
57
+ return {
58
+ node: nextPlusOne,
59
+ offset: direction > 0 ? 0 : nextPlusOne.nodeValue.length
60
+ };
61
+ } else if (direction < 0 && isElementType(nextPlusOne, 'br')) {
62
+ return {
63
+ node: nextNode.parentNode,
64
+ offset: getOffset(nextNode)
65
+ };
66
+ } else {
67
+ return {
68
+ node: nextPlusOne.parentNode,
69
+ offset: getOffset(nextPlusOne) + (direction > 0 ? 0 : 1)
70
+ };
71
+ }
72
+ } else {
73
+ return {
74
+ node: nextNode.parentNode,
75
+ offset: getOffset(nextNode) + (direction > 0 ? 1 : 0)
76
+ };
77
+ }
78
+ }
79
+ }
80
+ function getNextCharacterContentNode(node, offset, direction) {
81
+ const nextOffset = Number(offset) + (direction < 0 ? -1 : 0);
82
+ if (offset !== undefined && isElement(node) && nextOffset >= 0 && nextOffset < node.children.length) {
83
+ node = node.children[nextOffset];
84
+ }
85
+ return walkNodes(node, direction === 1 ? 'next' : 'previous', isTreatedAsCharacterContent);
86
+ }
87
+ function isTreatedAsCharacterContent(node) {
88
+ if (isTextNode(node)) {
89
+ return true;
90
+ }
91
+ if (isElement(node)) {
92
+ if (isElementType(node, [
93
+ 'input',
94
+ 'textarea'
95
+ ])) {
96
+ return node.type !== 'hidden';
97
+ } else if (isElementType(node, 'br')) {
98
+ return true;
99
+ }
100
+ }
101
+ return false;
102
+ }
103
+ function getOffset(node) {
104
+ let i = 0;
105
+ while(node.previousSibling){
106
+ i++;
107
+ node = node.previousSibling;
108
+ }
109
+ return i;
110
+ }
111
+ function isElement(node) {
112
+ return node.nodeType === 1;
113
+ }
114
+ function isTextNode(node) {
115
+ return node.nodeType === 3;
116
+ }
117
+ function walkNodes(node, direction, callback) {
118
+ for(;;){
119
+ const sibling = node[`${direction}Sibling`];
120
+ if (sibling) {
121
+ node = getDescendant(sibling, direction === 'next' ? 'first' : 'last');
122
+ if (callback(node)) {
123
+ return node;
124
+ }
125
+ } else if (node.parentNode && (!isElement(node.parentNode) || !isContentEditable(node.parentNode) && node.parentNode !== node.ownerDocument?.body)) {
126
+ node = node.parentNode;
127
+ } else {
128
+ break;
129
+ }
130
+ }
131
+ }
132
+ function getDescendant(node, direction) {
133
+ while(node.hasChildNodes()){
134
+ node = node[`${direction}Child`];
135
+ }
136
+ return node;
137
+ }
138
+
139
+ export { getNextCursorPosition };
@@ -0,0 +1,22 @@
1
+ import { eventWrapper } from '../misc/eventWrapper.js';
2
+ import { findClosest } from '../misc/findClosest.js';
3
+ import { getActiveElement } from './getActiveElement.js';
4
+ import { isFocusable } from './isFocusable.js';
5
+ import { updateSelectionOnFocus } from './selection.js';
6
+
7
+ /**
8
+ * Focus closest focusable element.
9
+ */ function focus(element) {
10
+ const target = findClosest(element, isFocusable);
11
+ const activeElement = getActiveElement(element.ownerDocument);
12
+ if ((target ?? element.ownerDocument.body) === activeElement) {
13
+ return;
14
+ } else if (target) {
15
+ eventWrapper(()=>target.focus());
16
+ } else {
17
+ eventWrapper(()=>activeElement?.blur());
18
+ }
19
+ updateSelectionOnFocus(target ?? element.ownerDocument.body);
20
+ }
21
+
22
+ export { focus };
@@ -0,0 +1,19 @@
1
+ import { isDisabled } from '../misc/isDisabled.js';
2
+
3
+ function getActiveElement(document) {
4
+ const activeElement = document.activeElement;
5
+ if (activeElement?.shadowRoot) {
6
+ return getActiveElement(activeElement.shadowRoot);
7
+ } else {
8
+ // Browser does not yield disabled elements as document.activeElement - jsdom does
9
+ if (isDisabled(activeElement)) {
10
+ return document.ownerDocument ? /* istanbul ignore next */ document.ownerDocument.body : document.body;
11
+ }
12
+ return activeElement;
13
+ }
14
+ }
15
+ function getActiveElementOrBody(document) {
16
+ return getActiveElement(document) ?? /* istanbul ignore next */ document.body;
17
+ }
18
+
19
+ export { getActiveElement, getActiveElementOrBody };
@@ -0,0 +1,78 @@
1
+ import { isDisabled } from '../misc/isDisabled.js';
2
+ import { isElementType } from '../misc/isElementType.js';
3
+ import { isVisible } from '../misc/isVisible.js';
4
+ import { FOCUSABLE_SELECTOR } from './selector.js';
5
+
6
+ function getTabDestination(activeElement, shift) {
7
+ const document = activeElement.ownerDocument;
8
+ const focusableElements = document.querySelectorAll(FOCUSABLE_SELECTOR);
9
+ const enabledElements = Array.from(focusableElements).filter((el)=>el === activeElement || !(Number(el.getAttribute('tabindex')) < 0 || isDisabled(el)));
10
+ // tabindex has no effect if the active element has negative tabindex
11
+ if (Number(activeElement.getAttribute('tabindex')) >= 0) {
12
+ enabledElements.sort((a, b)=>{
13
+ const i = Number(a.getAttribute('tabindex'));
14
+ const j = Number(b.getAttribute('tabindex'));
15
+ if (i === j) {
16
+ return 0;
17
+ } else if (i === 0) {
18
+ return 1;
19
+ } else if (j === 0) {
20
+ return -1;
21
+ }
22
+ return i - j;
23
+ });
24
+ }
25
+ const checkedRadio = {};
26
+ let prunedElements = [
27
+ document.body
28
+ ];
29
+ const activeRadioGroup = isElementType(activeElement, 'input', {
30
+ type: 'radio'
31
+ }) ? activeElement.name : undefined;
32
+ enabledElements.forEach((currentElement)=>{
33
+ const el = currentElement;
34
+ // For radio groups keep only the active radio
35
+ // If there is no active radio, keep only the checked radio
36
+ // If there is no checked radio, treat like everything else
37
+ if (isElementType(el, 'input', {
38
+ type: 'radio'
39
+ }) && el.name) {
40
+ // If the active element is part of the group, add only that
41
+ if (el === activeElement) {
42
+ prunedElements.push(el);
43
+ return;
44
+ } else if (el.name === activeRadioGroup) {
45
+ return;
46
+ }
47
+ // If we stumble upon a checked radio, remove the others
48
+ if (el.checked) {
49
+ prunedElements = prunedElements.filter((e)=>!isElementType(e, 'input', {
50
+ type: 'radio',
51
+ name: el.name
52
+ }));
53
+ prunedElements.push(el);
54
+ checkedRadio[el.name] = el;
55
+ return;
56
+ }
57
+ // If we already found the checked one, skip
58
+ if (typeof checkedRadio[el.name] !== 'undefined') {
59
+ return;
60
+ }
61
+ }
62
+ prunedElements.push(el);
63
+ });
64
+ for(let index = prunedElements.findIndex((el)=>el === activeElement);;){
65
+ index += shift ? -1 : 1;
66
+ // loop at overflow
67
+ if (index === prunedElements.length) {
68
+ index = 0;
69
+ } else if (index === -1) {
70
+ index = prunedElements.length - 1;
71
+ }
72
+ if (prunedElements[index] === activeElement || prunedElements[index] === document.body || isVisible(prunedElements[index])) {
73
+ return prunedElements[index];
74
+ }
75
+ }
76
+ }
77
+
78
+ export { getTabDestination };
@@ -0,0 +1,7 @@
1
+ import { FOCUSABLE_SELECTOR } from './selector.js';
2
+
3
+ function isFocusable(element) {
4
+ return element.matches(FOCUSABLE_SELECTOR);
5
+ }
6
+
7
+ export { isFocusable };
@@ -0,0 +1,74 @@
1
+ import '../click/isClickableInput.js';
2
+ import '../dataTransfer/Clipboard.js';
3
+ import '../edit/maxLength.js';
4
+ import '../edit/isEditable.js';
5
+ import '@testing-library/dom';
6
+ import { hasOwnSelection } from './selection.js';
7
+ import '@testing-library/dom/dist/helpers.js';
8
+ import '../keyDef/readNextDescriptor.js';
9
+ import '../misc/level.js';
10
+ import '../../options.js';
11
+ import '../../event/eventMap.js';
12
+ import '../../event/behavior/click.js';
13
+ import '../../event/behavior/cut.js';
14
+ import '../../event/behavior/keydown.js';
15
+ import '../../event/behavior/keypress.js';
16
+ import '../../event/behavior/keyup.js';
17
+ import '../../event/behavior/paste.js';
18
+ import { getUIValue } from '../../document/value.js';
19
+
20
+ function resolveCaretPosition({ target , node , offset }) {
21
+ if (hasOwnSelection(target)) {
22
+ return {
23
+ node: target,
24
+ offset: offset ?? getUIValue(target).length
25
+ };
26
+ } else if (node) {
27
+ return {
28
+ node,
29
+ offset: offset ?? (node.nodeType === 3 ? node.nodeValue.length : node.childNodes.length)
30
+ };
31
+ }
32
+ return findNodeAtTextOffset(target, offset);
33
+ }
34
+ function findNodeAtTextOffset(node, offset, isRoot = true) {
35
+ // When clicking after the content the browser behavior can be complicated:
36
+ // 1. If there is textContent after the last element child,
37
+ // the cursor is moved there.
38
+ // 2. If there is textContent in the last element child,
39
+ // the browser moves the cursor to the last non-empty text node inside this element.
40
+ // 3. Otherwise the cursor is moved to the end of the target.
41
+ let i = offset === undefined ? node.childNodes.length - 1 : 0;
42
+ const step = offset === undefined ? -1 : +1;
43
+ while(offset === undefined ? i >= (isRoot ? Math.max(node.childNodes.length - 1, 0) : 0) : i <= node.childNodes.length){
44
+ if (offset && i === node.childNodes.length) {
45
+ throw new Error('The given offset is out of bounds.');
46
+ }
47
+ const c = node.childNodes.item(i);
48
+ const text = String(c.textContent);
49
+ if (text.length) {
50
+ if (offset !== undefined && text.length < offset) {
51
+ offset -= text.length;
52
+ } else if (c.nodeType === 1) {
53
+ return findNodeAtTextOffset(c, offset, false);
54
+ } else {
55
+ // The pre-commit hooks keeps changing this
56
+ // See https://github.com/kentcdodds/kcd-scripts/issues/218
57
+ /* istanbul ignore else */ // eslint-disable-next-line no-lonely-if
58
+ if (c.nodeType === 3) {
59
+ return {
60
+ node: c,
61
+ offset: offset ?? c.nodeValue.length
62
+ };
63
+ }
64
+ }
65
+ }
66
+ i += step;
67
+ }
68
+ return {
69
+ node,
70
+ offset: node.childNodes.length
71
+ };
72
+ }
73
+
74
+ export { resolveCaretPosition };
@@ -0,0 +1,49 @@
1
+ import '../click/isClickableInput.js';
2
+ import '../dataTransfer/Clipboard.js';
3
+ import { getContentEditable } from '../edit/isContentEditable.js';
4
+ import '../edit/maxLength.js';
5
+ import { editableInputTypes } from '../edit/isEditable.js';
6
+ import '@testing-library/dom';
7
+ import { isElementType } from '../misc/isElementType.js';
8
+ import { setSelection } from './selection.js';
9
+ import '@testing-library/dom/dist/helpers.js';
10
+ import '../keyDef/readNextDescriptor.js';
11
+ import '../misc/level.js';
12
+ import '../../options.js';
13
+ import '../../event/eventMap.js';
14
+ import '../../event/behavior/click.js';
15
+ import '../../event/behavior/cut.js';
16
+ import '../../event/behavior/keydown.js';
17
+ import '../../event/behavior/keypress.js';
18
+ import '../../event/behavior/keyup.js';
19
+ import '../../event/behavior/paste.js';
20
+ import { getUISelection } from '../../document/selection.js';
21
+ import { getUIValue } from '../../document/value.js';
22
+
23
+ /**
24
+ * Expand a selection like the browser does when pressing Ctrl+A.
25
+ */ function selectAll(target) {
26
+ if (isElementType(target, 'textarea') || isElementType(target, 'input') && target.type in editableInputTypes) {
27
+ return setSelection({
28
+ focusNode: target,
29
+ anchorOffset: 0,
30
+ focusOffset: getUIValue(target).length
31
+ });
32
+ }
33
+ const focusNode = getContentEditable(target) ?? target.ownerDocument.body;
34
+ setSelection({
35
+ focusNode,
36
+ anchorOffset: 0,
37
+ focusOffset: focusNode.childNodes.length
38
+ });
39
+ }
40
+ function isAllSelected(target) {
41
+ if (isElementType(target, 'textarea') || isElementType(target, 'input') && target.type in editableInputTypes) {
42
+ return getUISelection(target).startOffset === 0 && getUISelection(target).endOffset === getUIValue(target).length;
43
+ }
44
+ const focusNode = getContentEditable(target) ?? target.ownerDocument.body;
45
+ const selection = target.ownerDocument.getSelection();
46
+ return selection?.anchorNode === focusNode && selection.focusNode === focusNode && selection.anchorOffset === 0 && selection.focusOffset === focusNode.childNodes.length;
47
+ }
48
+
49
+ export { isAllSelected, selectAll };
@@ -0,0 +1,274 @@
1
+ import { isElementType } from '../misc/isElementType.js';
2
+ import { isClickableInput } from '../click/isClickableInput.js';
3
+ import '../dataTransfer/Clipboard.js';
4
+ import { isContentEditable, getContentEditable } from '../edit/isContentEditable.js';
5
+ import '../edit/maxLength.js';
6
+ import { editableInputTypes } from '../edit/isEditable.js';
7
+ import '@testing-library/dom';
8
+ import '@testing-library/dom/dist/helpers.js';
9
+ import { getNextCursorPosition } from './cursor.js';
10
+ import { resolveCaretPosition } from './resolveCaretPosition.js';
11
+ import '../keyDef/readNextDescriptor.js';
12
+ import '../misc/level.js';
13
+ import '../../options.js';
14
+ import '../../event/eventMap.js';
15
+ import '../../event/behavior/click.js';
16
+ import '../../event/behavior/cut.js';
17
+ import '../../event/behavior/keydown.js';
18
+ import '../../event/behavior/keypress.js';
19
+ import '../../event/behavior/keyup.js';
20
+ import '../../event/behavior/paste.js';
21
+ import { setUISelection, getUISelection } from '../../document/selection.js';
22
+ import { getUIValue } from '../../document/value.js';
23
+
24
+ /**
25
+ * Backward-compatible selection.
26
+ *
27
+ * Handles input elements and contenteditable if it only contains a single text node.
28
+ */ function setSelectionRange(element, anchorOffset, focusOffset) {
29
+ if (hasOwnSelection(element)) {
30
+ return setSelection({
31
+ focusNode: element,
32
+ anchorOffset,
33
+ focusOffset
34
+ });
35
+ }
36
+ /* istanbul ignore else */ if (isContentEditable(element) && element.firstChild?.nodeType === 3) {
37
+ return setSelection({
38
+ focusNode: element.firstChild,
39
+ anchorOffset,
40
+ focusOffset
41
+ });
42
+ }
43
+ /* istanbul ignore next */ throw new Error('Not implemented. The result of this interaction is unreliable.');
44
+ }
45
+ /**
46
+ * Determine if the element has its own selection implementation
47
+ * and does not interact with the Document Selection API.
48
+ */ function hasOwnSelection(node) {
49
+ return isElement(node) && (isElementType(node, 'textarea') || isElementType(node, 'input') && node.type in editableInputTypes);
50
+ }
51
+ function hasNoSelection(node) {
52
+ return isElement(node) && isClickableInput(node);
53
+ }
54
+ function isElement(node) {
55
+ return node.nodeType === 1;
56
+ }
57
+ /**
58
+ * Determine which selection logic and selection ranges to consider.
59
+ */ function getTargetTypeAndSelection(node) {
60
+ const element = getElement(node);
61
+ if (element && hasOwnSelection(element)) {
62
+ return {
63
+ type: 'input',
64
+ selection: getUISelection(element)
65
+ };
66
+ }
67
+ const selection = element?.ownerDocument.getSelection();
68
+ // It is possible to extend a single-range selection into a contenteditable.
69
+ // This results in the range acting like a range outside of contenteditable.
70
+ const isCE = getContentEditable(node) && selection?.anchorNode && getContentEditable(selection.anchorNode);
71
+ return {
72
+ type: isCE ? 'contenteditable' : 'default',
73
+ selection
74
+ };
75
+ }
76
+ function getElement(node) {
77
+ return node.nodeType === 1 ? node : node.parentElement;
78
+ }
79
+ /**
80
+ * Reset the Document Selection when moving focus into an element
81
+ * with own selection implementation.
82
+ */ function updateSelectionOnFocus(element) {
83
+ const selection = element.ownerDocument.getSelection();
84
+ /* istanbul ignore if */ if (!selection?.focusNode) {
85
+ return;
86
+ }
87
+ // If the focus moves inside an element with own selection implementation,
88
+ // the document selection will be this element.
89
+ // But if the focused element is inside a contenteditable,
90
+ // 1) a collapsed selection will be retained.
91
+ // 2) other selections will be replaced by a cursor
92
+ // 2.a) at the start of the first child if it is a text node
93
+ // 2.b) at the start of the contenteditable.
94
+ if (hasOwnSelection(element)) {
95
+ const contenteditable = getContentEditable(selection.focusNode);
96
+ if (contenteditable) {
97
+ if (!selection.isCollapsed) {
98
+ const focusNode = contenteditable.firstChild?.nodeType === 3 ? contenteditable.firstChild : contenteditable;
99
+ selection.setBaseAndExtent(focusNode, 0, focusNode, 0);
100
+ }
101
+ } else {
102
+ selection.setBaseAndExtent(element, 0, element, 0);
103
+ }
104
+ }
105
+ }
106
+ /**
107
+ * Get the range that would be overwritten by input.
108
+ */ function getInputRange(focusNode) {
109
+ const typeAndSelection = getTargetTypeAndSelection(focusNode);
110
+ if (typeAndSelection.type === 'input') {
111
+ return typeAndSelection.selection;
112
+ } else if (typeAndSelection.type === 'contenteditable') {
113
+ // Multi-range on contenteditable edits the first selection instead of the last
114
+ return typeAndSelection.selection?.getRangeAt(0);
115
+ }
116
+ }
117
+ /**
118
+ * Extend/shrink the selection like with Shift+Arrows or Shift+Mouse
119
+ */ function modifySelection({ focusNode , focusOffset }) {
120
+ const typeAndSelection = getTargetTypeAndSelection(focusNode);
121
+ if (typeAndSelection.type === 'input') {
122
+ return setUISelection(focusNode, {
123
+ anchorOffset: typeAndSelection.selection.anchorOffset,
124
+ focusOffset
125
+ }, 'modify');
126
+ }
127
+ focusNode.ownerDocument?.getSelection()?.extend(focusNode, focusOffset);
128
+ }
129
+ /**
130
+ * Set the selection
131
+ */ function setSelection({ focusNode , focusOffset , anchorNode =focusNode , anchorOffset =focusOffset }) {
132
+ const typeAndSelection = getTargetTypeAndSelection(focusNode);
133
+ if (typeAndSelection.type === 'input') {
134
+ return setUISelection(focusNode, {
135
+ anchorOffset,
136
+ focusOffset
137
+ });
138
+ }
139
+ anchorNode.ownerDocument?.getSelection()?.setBaseAndExtent(anchorNode, anchorOffset, focusNode, focusOffset);
140
+ }
141
+ /**
142
+ * Move the selection
143
+ */ function moveSelection(node, direction) {
144
+ // TODO: implement shift
145
+ if (hasOwnSelection(node)) {
146
+ const selection = getUISelection(node);
147
+ setSelection({
148
+ focusNode: node,
149
+ focusOffset: selection.startOffset === selection.endOffset ? selection.focusOffset + direction : direction < 0 ? selection.startOffset : selection.endOffset
150
+ });
151
+ } else {
152
+ const selection1 = node.ownerDocument.getSelection();
153
+ if (!selection1?.focusNode) {
154
+ return;
155
+ }
156
+ if (selection1.isCollapsed) {
157
+ const nextPosition = getNextCursorPosition(selection1.focusNode, selection1.focusOffset, direction);
158
+ if (nextPosition) {
159
+ setSelection({
160
+ focusNode: nextPosition.node,
161
+ focusOffset: nextPosition.offset
162
+ });
163
+ }
164
+ } else {
165
+ selection1[direction < 0 ? 'collapseToStart' : 'collapseToEnd']();
166
+ }
167
+ }
168
+ }
169
+ function setSelectionPerMouseDown({ document , target , clickCount , node , offset }) {
170
+ if (hasNoSelection(target)) {
171
+ return;
172
+ }
173
+ const targetHasOwnSelection = hasOwnSelection(target);
174
+ // On non-input elements the text selection per multiple click
175
+ // can extend beyond the target boundaries.
176
+ // The exact mechanism what is considered in the same line is unclear.
177
+ // Looks it might be every inline element.
178
+ // TODO: Check what might be considered part of the same line of text.
179
+ const text = String(targetHasOwnSelection ? getUIValue(target) : target.textContent);
180
+ const [start, end] = node ? // which elements might be considered in the same line of text.
181
+ // TODO: support expanding initial range on multiple clicks if node is given
182
+ [
183
+ offset,
184
+ offset
185
+ ] : getTextRange(text, offset, clickCount);
186
+ // TODO: implement modifying selection per shift/ctrl+mouse
187
+ if (targetHasOwnSelection) {
188
+ setUISelection(target, {
189
+ anchorOffset: start ?? text.length,
190
+ focusOffset: end ?? text.length
191
+ });
192
+ return {
193
+ node: target,
194
+ start: start ?? 0,
195
+ end: end ?? text.length
196
+ };
197
+ } else {
198
+ const { node: startNode , offset: startOffset } = resolveCaretPosition({
199
+ target,
200
+ node,
201
+ offset: start
202
+ });
203
+ const { node: endNode , offset: endOffset } = resolveCaretPosition({
204
+ target,
205
+ node,
206
+ offset: end
207
+ });
208
+ const range = target.ownerDocument.createRange();
209
+ try {
210
+ range.setStart(startNode, startOffset);
211
+ range.setEnd(endNode, endOffset);
212
+ } catch (e) {
213
+ throw new Error('The given offset is out of bounds.');
214
+ }
215
+ const selection = document.getSelection();
216
+ selection?.removeAllRanges();
217
+ selection?.addRange(range.cloneRange());
218
+ return range;
219
+ }
220
+ }
221
+ function getTextRange(text, pos, clickCount) {
222
+ if (clickCount % 3 === 1 || text.length === 0) {
223
+ return [
224
+ pos,
225
+ pos
226
+ ];
227
+ }
228
+ const textPos = pos ?? text.length;
229
+ if (clickCount % 3 === 2) {
230
+ return [
231
+ textPos - text.substr(0, pos).match(/(\w+|\s+|\W)?$/)[0].length,
232
+ pos === undefined ? pos : pos + text.substr(pos).match(/^(\w+|\s+|\W)?/)[0].length,
233
+ ];
234
+ }
235
+ // triple click
236
+ return [
237
+ textPos - text.substr(0, pos).match(/[^\r\n]*$/)[0].length,
238
+ pos === undefined ? pos : pos + text.substr(pos).match(/^[^\r\n]*/)[0].length,
239
+ ];
240
+ }
241
+ function modifySelectionPerMouseMove(selectionRange, { document , target , node , offset }) {
242
+ const selectionFocus = resolveCaretPosition({
243
+ target,
244
+ node,
245
+ offset
246
+ });
247
+ if ('node' in selectionRange) {
248
+ // When the mouse is dragged outside of an input/textarea,
249
+ // the selection is extended to the beginning or end of the input
250
+ // depending on pointer position.
251
+ // TODO: extend selection according to pointer position
252
+ /* istanbul ignore else */ if (selectionFocus.node === selectionRange.node) {
253
+ const anchorOffset = selectionFocus.offset < selectionRange.start ? selectionRange.end : selectionRange.start;
254
+ const focusOffset = selectionFocus.offset > selectionRange.end || selectionFocus.offset < selectionRange.start ? selectionFocus.offset : selectionRange.end;
255
+ setUISelection(selectionRange.node, {
256
+ anchorOffset,
257
+ focusOffset
258
+ });
259
+ }
260
+ } else {
261
+ const range = selectionRange.cloneRange();
262
+ const cmp = range.comparePoint(selectionFocus.node, selectionFocus.offset);
263
+ if (cmp < 0) {
264
+ range.setStart(selectionFocus.node, selectionFocus.offset);
265
+ } else if (cmp > 0) {
266
+ range.setEnd(selectionFocus.node, selectionFocus.offset);
267
+ }
268
+ const selection = document.getSelection();
269
+ selection?.removeAllRanges();
270
+ selection?.addRange(range.cloneRange());
271
+ }
272
+ }
273
+
274
+ export { getInputRange, hasNoSelection, hasOwnSelection, modifySelection, modifySelectionPerMouseMove, moveSelection, setSelection, setSelectionPerMouseDown, setSelectionRange, updateSelectionOnFocus };
@@ -0,0 +1,12 @@
1
+ const FOCUSABLE_SELECTOR = [
2
+ 'input:not([type=hidden]):not([disabled])',
3
+ 'button:not([disabled])',
4
+ 'select:not([disabled])',
5
+ 'textarea:not([disabled])',
6
+ '[contenteditable=""]',
7
+ '[contenteditable="true"]',
8
+ 'a[href]',
9
+ '[tabindex]:not([disabled])',
10
+ ].join(', ');
11
+
12
+ export { FOCUSABLE_SELECTOR };