@testing-library/user-event 14.3.0 → 14.4.2

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 (237) hide show
  1. package/dist/cjs/clipboard/copy.js +41 -0
  2. package/dist/cjs/clipboard/cut.js +41 -0
  3. package/dist/cjs/clipboard/index.js +13 -0
  4. package/dist/cjs/clipboard/paste.js +43 -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 +108 -0
  12. package/dist/cjs/document/setRangeText.js +21 -0
  13. package/dist/cjs/document/value.js +127 -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 +138 -0
  18. package/dist/cjs/event/behavior/keypress.js +58 -0
  19. package/dist/cjs/event/behavior/keyup.js +25 -0
  20. package/dist/cjs/event/behavior/paste.js +30 -0
  21. package/dist/cjs/event/behavior/registry.js +7 -0
  22. package/dist/cjs/event/createEvent.js +209 -0
  23. package/dist/cjs/event/dispatchEvent.js +38 -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 +61 -0
  33. package/dist/cjs/options.js +42 -0
  34. package/dist/cjs/package.json +3 -0
  35. package/dist/cjs/pointer/index.js +92 -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 +100 -0
  43. package/dist/cjs/setup/wrapAsync.js +13 -0
  44. package/dist/cjs/system/index.js +33 -0
  45. package/dist/cjs/system/keyboard.js +137 -0
  46. package/dist/cjs/system/pointer/buttons.js +73 -0
  47. package/dist/cjs/system/pointer/device.js +23 -0
  48. package/dist/cjs/system/pointer/index.js +154 -0
  49. package/dist/cjs/system/pointer/mouse.js +195 -0
  50. package/dist/cjs/system/pointer/pointer.js +115 -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 +76 -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 +172 -0
  59. package/dist/cjs/utils/dataTransfer/DataTransfer.js +127 -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 +163 -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 +39 -0
  68. package/dist/cjs/utils/edit/setFiles.js +61 -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 +144 -0
  73. package/dist/cjs/utils/focus/focus.js +29 -0
  74. package/dist/cjs/utils/focus/getActiveElement.js +25 -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 +56 -0
  79. package/dist/cjs/utils/focus/selection.js +292 -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 +94 -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 +35 -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 +23 -0
  95. package/dist/cjs/utils/misc/wait.js +16 -0
  96. package/dist/cjs/utils/pointer/cssPointerEvents.js +118 -0
  97. package/dist/esm/clipboard/copy.js +37 -0
  98. package/dist/esm/clipboard/cut.js +37 -0
  99. package/dist/esm/clipboard/index.js +3 -0
  100. package/dist/esm/clipboard/paste.js +39 -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 +100 -0
  108. package/dist/esm/document/setRangeText.js +17 -0
  109. package/dist/esm/document/value.js +117 -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 +136 -0
  114. package/dist/esm/event/behavior/keypress.js +56 -0
  115. package/dist/esm/event/behavior/keyup.js +23 -0
  116. package/dist/esm/event/behavior/paste.js +28 -0
  117. package/dist/esm/event/behavior/registry.js +3 -0
  118. package/dist/esm/event/createEvent.js +205 -0
  119. package/dist/esm/event/dispatchEvent.js +34 -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 +57 -0
  129. package/dist/esm/options.js +37 -0
  130. package/dist/esm/package.json +3 -0
  131. package/dist/esm/pointer/index.js +88 -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 +93 -0
  139. package/dist/esm/setup/wrapAsync.js +9 -0
  140. package/dist/esm/system/index.js +29 -0
  141. package/dist/esm/system/keyboard.js +133 -0
  142. package/dist/esm/system/pointer/buttons.js +65 -0
  143. package/dist/esm/system/pointer/device.js +19 -0
  144. package/dist/esm/system/pointer/index.js +149 -0
  145. package/dist/esm/system/pointer/mouse.js +191 -0
  146. package/dist/esm/system/pointer/pointer.js +111 -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 +72 -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 +163 -0
  155. package/dist/esm/utils/dataTransfer/DataTransfer.js +122 -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 +159 -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 +35 -0
  164. package/dist/esm/utils/edit/setFiles.js +57 -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 +140 -0
  169. package/dist/esm/utils/focus/focus.js +25 -0
  170. package/dist/esm/utils/focus/getActiveElement.js +20 -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 +51 -0
  175. package/dist/esm/utils/focus/selection.js +279 -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 +90 -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 +31 -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 +18 -0
  191. package/dist/esm/utils/misc/wait.js +12 -0
  192. package/dist/esm/utils/pointer/cssPointerEvents.js +113 -0
  193. package/dist/types/event/createEvent.d.ts +2 -8
  194. package/dist/types/event/eventMap.d.ts +204 -181
  195. package/dist/types/event/index.d.ts +1 -2
  196. package/dist/types/event/types.d.ts +6 -0
  197. package/dist/types/index.d.ts +2 -2
  198. package/dist/types/keyboard/index.d.ts +2 -6
  199. package/dist/types/keyboard/keyMap.d.ts +1 -1
  200. package/dist/types/keyboard/parseKeyDef.d.ts +1 -1
  201. package/dist/types/options.d.ts +4 -4
  202. package/dist/types/pointer/index.d.ts +23 -5
  203. package/dist/types/pointer/keyMap.d.ts +1 -1
  204. package/dist/types/pointer/parseKeyDef.d.ts +1 -1
  205. package/dist/types/setup/config.d.ts +3 -7
  206. package/dist/types/setup/directApi.d.ts +21 -16
  207. package/dist/types/setup/index.d.ts +1 -2
  208. package/dist/types/setup/setup.d.ts +7 -1
  209. package/dist/types/system/index.d.ts +10 -0
  210. package/dist/types/system/keyboard.d.ts +53 -0
  211. package/dist/types/system/pointer/buttons.d.ts +23 -0
  212. package/dist/types/system/pointer/device.d.ts +8 -0
  213. package/dist/types/system/pointer/index.d.ts +39 -0
  214. package/dist/types/system/pointer/mouse.d.ts +26 -0
  215. package/dist/types/system/pointer/pointer.d.ts +30 -0
  216. package/dist/types/utils/click/isClickableInput.d.ts +15 -1
  217. package/dist/types/utils/focus/getActiveElement.d.ts +1 -0
  218. package/dist/types/utils/focus/resolveCaretPosition.d.ts +8 -0
  219. package/dist/types/utils/focus/selection.d.ts +28 -1
  220. package/dist/types/utils/index.d.ts +2 -3
  221. package/dist/types/utils/misc/getTreeDiff.d.ts +1 -0
  222. package/package.json +32 -14
  223. package/dist/index.cjs +0 -3104
  224. package/dist/index.mjs +0 -3155
  225. package/dist/types/event/eventTypes.d.ts +0 -2
  226. package/dist/types/keyboard/keyboardAction.d.ts +0 -10
  227. package/dist/types/keyboard/modifiers.d.ts +0 -9
  228. package/dist/types/keyboard/types.d.ts +0 -73
  229. package/dist/types/pointer/firePointerEvents.d.ts +0 -11
  230. package/dist/types/pointer/pointerAction.d.ts +0 -7
  231. package/dist/types/pointer/pointerMove.d.ts +0 -6
  232. package/dist/types/pointer/pointerPress.d.ts +0 -8
  233. package/dist/types/pointer/resolveSelectionTarget.d.ts +0 -5
  234. package/dist/types/pointer/types.d.ts +0 -62
  235. package/dist/types/utils/keyboard/getKeyEventProps.d.ts +0 -5
  236. package/dist/types/utils/keyboard/getUIEventModifiers.d.ts +0 -15
  237. package/dist/types/utils/pointer/mouseButtons.d.ts +0 -12
@@ -0,0 +1,24 @@
1
+ import { dispatchUIEvent } from '../../event/index.js';
2
+ import { focus } from '../focus/focus.js';
3
+ import { getWindow } from '../misc/getWindow.js';
4
+ import { isDisabled } from '../misc/isDisabled.js';
5
+
6
+ function walkRadio(config, el, direction) {
7
+ const window = getWindow(el);
8
+ const group = Array.from(el.ownerDocument.querySelectorAll(el.name ? `input[type="radio"][name="${window.CSS.escape(el.name)}"]` : `input[type="radio"][name=""], input[type="radio"]:not([name])`));
9
+ for(let i = group.findIndex((e)=>e === el) + direction;; i += direction){
10
+ if (!group[i]) {
11
+ i = direction > 0 ? 0 : group.length - 1;
12
+ }
13
+ if (group[i] === el) {
14
+ return;
15
+ }
16
+ if (isDisabled(group[i])) {
17
+ continue;
18
+ }
19
+ focus(group[i]);
20
+ dispatchUIEvent(config, group[i], 'click');
21
+ }
22
+ }
23
+
24
+ export { walkRadio };
@@ -0,0 +1,12 @@
1
+ import { eventWrapper } from '../misc/eventWrapper.js';
2
+ import { getActiveElement } from './getActiveElement.js';
3
+ import { isFocusable } from './isFocusable.js';
4
+
5
+ function blur(element) {
6
+ if (!isFocusable(element)) return;
7
+ const wasActive = getActiveElement(element.ownerDocument) === element;
8
+ if (!wasActive) return;
9
+ eventWrapper(()=>element.blur());
10
+ }
11
+
12
+ export { blur };
@@ -0,0 +1,42 @@
1
+ import '../click/isClickableInput.js';
2
+ import { createDataTransfer } from '../dataTransfer/DataTransfer.js';
3
+ import '../dataTransfer/Clipboard.js';
4
+ import '../edit/maxLength.js';
5
+ import '../edit/isEditable.js';
6
+ import '@testing-library/dom';
7
+ import { hasOwnSelection } from './selection.js';
8
+ import { getWindow } from '../misc/getWindow.js';
9
+ import '../keyDef/readNextDescriptor.js';
10
+ import '../misc/level.js';
11
+ import '../../options.js';
12
+ import '../../event/eventMap.js';
13
+ import '../../event/behavior/click.js';
14
+ import '../../event/behavior/cut.js';
15
+ import '../../event/behavior/keydown.js';
16
+ import '../../event/behavior/keypress.js';
17
+ import '../../event/behavior/keyup.js';
18
+ import '../../event/behavior/paste.js';
19
+ import { getUISelection } from '../../document/selection.js';
20
+ import { getUIValue } from '../../document/value.js';
21
+
22
+ function copySelection(target) {
23
+ const data = hasOwnSelection(target) ? {
24
+ 'text/plain': readSelectedValueFromInput(target)
25
+ } : {
26
+ 'text/plain': String(target.ownerDocument.getSelection())
27
+ };
28
+ const dt = createDataTransfer(getWindow(target));
29
+ for(const type in data){
30
+ if (data[type]) {
31
+ dt.setData(type, data[type]);
32
+ }
33
+ }
34
+ return dt;
35
+ }
36
+ function readSelectedValueFromInput(target) {
37
+ const sel = getUISelection(target);
38
+ const val = getUIValue(target);
39
+ return val.substring(sel.startOffset, sel.endOffset);
40
+ }
41
+
42
+ export { copySelection };
@@ -0,0 +1,140 @@
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
+ var ref;
120
+ const sibling = node[`${direction}Sibling`];
121
+ if (sibling) {
122
+ node = getDescendant(sibling, direction === 'next' ? 'first' : 'last');
123
+ if (callback(node)) {
124
+ return node;
125
+ }
126
+ } else if (node.parentNode && (!isElement(node.parentNode) || !isContentEditable(node.parentNode) && node.parentNode !== ((ref = node.ownerDocument) === null || ref === void 0 ? void 0 : ref.body))) {
127
+ node = node.parentNode;
128
+ } else {
129
+ break;
130
+ }
131
+ }
132
+ }
133
+ function getDescendant(node, direction) {
134
+ while(node.hasChildNodes()){
135
+ node = node[`${direction}Child`];
136
+ }
137
+ return node;
138
+ }
139
+
140
+ export { getNextCursorPosition };
@@ -0,0 +1,25 @@
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 !== null && target !== void 0 ? target : element.ownerDocument.body) === activeElement) {
13
+ return;
14
+ } else if (target) {
15
+ eventWrapper(()=>target.focus());
16
+ } else {
17
+ eventWrapper(()=>{
18
+ var ref;
19
+ return (ref = activeElement) === null || ref === void 0 ? void 0 : ref.blur();
20
+ });
21
+ }
22
+ updateSelectionOnFocus(target !== null && target !== void 0 ? target : element.ownerDocument.body);
23
+ }
24
+
25
+ export { focus };
@@ -0,0 +1,20 @@
1
+ import { isDisabled } from '../misc/isDisabled.js';
2
+
3
+ function getActiveElement(document) {
4
+ const activeElement = document.activeElement;
5
+ if (activeElement === null || activeElement === void 0 ? void 0 : 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
+ var ref;
17
+ return (ref = getActiveElement(document)) !== null && ref !== void 0 ? ref : /* istanbul ignore next */ document.body;
18
+ }
19
+
20
+ 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 !== null && offset !== void 0 ? offset : getUIValue(target).length
25
+ };
26
+ } else if (node) {
27
+ return {
28
+ node,
29
+ offset: offset !== null && offset !== void 0 ? 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 !== null && offset !== void 0 ? 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,51 @@
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
+ var ref;
34
+ const focusNode = (ref = getContentEditable(target)) !== null && ref !== void 0 ? ref : target.ownerDocument.body;
35
+ setSelection({
36
+ focusNode,
37
+ anchorOffset: 0,
38
+ focusOffset: focusNode.childNodes.length
39
+ });
40
+ }
41
+ function isAllSelected(target) {
42
+ if (isElementType(target, 'textarea') || isElementType(target, 'input') && target.type in editableInputTypes) {
43
+ return getUISelection(target).startOffset === 0 && getUISelection(target).endOffset === getUIValue(target).length;
44
+ }
45
+ var ref;
46
+ const focusNode = (ref = getContentEditable(target)) !== null && ref !== void 0 ? ref : target.ownerDocument.body;
47
+ const selection = target.ownerDocument.getSelection();
48
+ return (selection === null || selection === void 0 ? void 0 : selection.anchorNode) === focusNode && selection.focusNode === focusNode && selection.anchorOffset === 0 && selection.focusOffset === focusNode.childNodes.length;
49
+ }
50
+
51
+ export { isAllSelected, selectAll };