@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
package/dist/index.cjs DELETED
@@ -1,3104 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/index.ts
21
- var src_exports = {};
22
- __export(src_exports, {
23
- PointerEventsCheckLevel: () => PointerEventsCheckLevel,
24
- default: () => userEvent
25
- });
26
- module.exports = __toCommonJS(src_exports);
27
-
28
- // src/utils/misc/isElementType.ts
29
- function isElementType(element, tag, props) {
30
- if (element.namespaceURI && element.namespaceURI !== "http://www.w3.org/1999/xhtml") {
31
- return false;
32
- }
33
- tag = Array.isArray(tag) ? tag : [tag];
34
- if (!tag.includes(element.tagName.toLowerCase())) {
35
- return false;
36
- }
37
- if (props) {
38
- return Object.entries(props).every(([k, v]) => element[k] === v);
39
- }
40
- return true;
41
- }
42
-
43
- // src/utils/click/isClickableInput.ts
44
- var CLICKABLE_INPUT_TYPES = [
45
- "button",
46
- "color",
47
- "file",
48
- "image",
49
- "reset",
50
- "submit",
51
- "checkbox",
52
- "radio"
53
- ];
54
- function isClickableInput(element) {
55
- return isElementType(element, "button") || isElementType(element, "input") && CLICKABLE_INPUT_TYPES.includes(element.type);
56
- }
57
-
58
- // src/utils/dataTransfer/Blob.ts
59
- function readBlobText(blob, FileReader) {
60
- return new Promise((res, rej) => {
61
- const fr = new FileReader();
62
- fr.onerror = rej;
63
- fr.onabort = rej;
64
- fr.onload = () => {
65
- res(String(fr.result));
66
- };
67
- fr.readAsText(blob);
68
- });
69
- }
70
-
71
- // src/utils/dataTransfer/FileList.ts
72
- function createFileList(window, files) {
73
- const list = {
74
- ...files,
75
- length: files.length,
76
- item: (index) => list[index],
77
- [Symbol.iterator]: function* nextFile() {
78
- for (let i = 0; i < list.length; i++) {
79
- yield list[i];
80
- }
81
- }
82
- };
83
- list.constructor = window.FileList;
84
- Object.setPrototypeOf(list, window.FileList.prototype);
85
- Object.freeze(list);
86
- return list;
87
- }
88
-
89
- // src/utils/dataTransfer/DataTransfer.ts
90
- var DataTransferItemStub = class {
91
- constructor(dataOrFile, type3) {
92
- this.file = null;
93
- this.data = void 0;
94
- if (typeof dataOrFile === "string") {
95
- this.kind = "string";
96
- this.type = String(type3);
97
- this.data = dataOrFile;
98
- } else {
99
- this.kind = "file";
100
- this.type = dataOrFile.type;
101
- this.file = dataOrFile;
102
- }
103
- }
104
- getAsFile() {
105
- return this.file;
106
- }
107
- getAsString(callback) {
108
- if (typeof this.data === "string") {
109
- callback(this.data);
110
- }
111
- }
112
- webkitGetAsEntry() {
113
- throw new Error("not implemented");
114
- }
115
- };
116
- var DataTransferItemListStub = class extends Array {
117
- add(...args) {
118
- const item = new DataTransferItemStub(args[0], args[1]);
119
- this.push(item);
120
- return item;
121
- }
122
- clear() {
123
- this.splice(0, this.length);
124
- }
125
- remove(index) {
126
- this.splice(index, 1);
127
- }
128
- };
129
- function getTypeMatcher(type3, exact) {
130
- const [group, sub] = type3.split("/");
131
- const isGroup = !sub || sub === "*";
132
- return (item) => {
133
- return exact ? item.type === (isGroup ? group : type3) : isGroup ? item.type.startsWith(`${group}/`) : item.type === group;
134
- };
135
- }
136
- function createDataTransferStub(window) {
137
- return new class DataTransferStub {
138
- constructor() {
139
- this.dropEffect = "none";
140
- this.effectAllowed = "uninitialized";
141
- this.items = new DataTransferItemListStub();
142
- this.files = createFileList(window, []);
143
- }
144
- getData(format) {
145
- var _a;
146
- const match = (_a = this.items.find(getTypeMatcher(format, true))) != null ? _a : this.items.find(getTypeMatcher(format, false));
147
- let text = "";
148
- match == null ? void 0 : match.getAsString((t) => {
149
- text = t;
150
- });
151
- return text;
152
- }
153
- setData(format, data) {
154
- const matchIndex = this.items.findIndex(getTypeMatcher(format, true));
155
- const item = new DataTransferItemStub(data, format);
156
- if (matchIndex >= 0) {
157
- this.items.splice(matchIndex, 1, item);
158
- } else {
159
- this.items.push(item);
160
- }
161
- }
162
- clearData(format) {
163
- if (format) {
164
- const matchIndex = this.items.findIndex(getTypeMatcher(format, true));
165
- if (matchIndex >= 0) {
166
- this.items.remove(matchIndex);
167
- }
168
- } else {
169
- this.items.clear();
170
- }
171
- }
172
- get types() {
173
- const t = [];
174
- if (this.files.length) {
175
- t.push("Files");
176
- }
177
- this.items.forEach((i) => t.push(i.type));
178
- Object.freeze(t);
179
- return t;
180
- }
181
- setDragImage() {
182
- }
183
- }();
184
- }
185
- function createDataTransfer(window, files = []) {
186
- const dt = typeof window.DataTransfer === "undefined" ? createDataTransferStub(window) : new window.DataTransfer();
187
- Object.defineProperty(dt, "files", { get: () => createFileList(window, files) });
188
- return dt;
189
- }
190
- function getBlobFromDataTransferItem(window, item) {
191
- if (item.kind === "file") {
192
- return item.getAsFile();
193
- }
194
- let data = "";
195
- item.getAsString((s) => {
196
- data = s;
197
- });
198
- return new window.Blob([data], { type: item.type });
199
- }
200
-
201
- // src/utils/misc/getWindow.ts
202
- var import_helpers = require("@testing-library/dom/dist/helpers.js");
203
- function getWindow(node) {
204
- return (0, import_helpers.getWindowFromNode)(node);
205
- }
206
-
207
- // src/utils/dataTransfer/Clipboard.ts
208
- function createClipboardItem(window, ...blobs) {
209
- const dataMap = Object.fromEntries(blobs.map((b) => [
210
- typeof b === "string" ? "text/plain" : b.type,
211
- Promise.resolve(b)
212
- ]));
213
- if (typeof window.ClipboardItem !== "undefined") {
214
- return new window.ClipboardItem(dataMap);
215
- }
216
- return new class ClipboardItem {
217
- constructor(d) {
218
- this.data = d;
219
- }
220
- get types() {
221
- return Array.from(Object.keys(this.data));
222
- }
223
- async getType(type3) {
224
- const value = await this.data[type3];
225
- if (!value) {
226
- throw new Error(`${type3} is not one of the available MIME types on this item.`);
227
- }
228
- return value instanceof window.Blob ? value : new window.Blob([value], { type: type3 });
229
- }
230
- }(dataMap);
231
- }
232
- var ClipboardStubControl = Symbol("Manage ClipboardSub");
233
- function createClipboardStub(window, control) {
234
- return Object.assign(new class Clipboard extends window.EventTarget {
235
- constructor() {
236
- super(...arguments);
237
- this.items = [];
238
- }
239
- async read() {
240
- return Array.from(this.items);
241
- }
242
- async readText() {
243
- let text = "";
244
- for (const item of this.items) {
245
- const type3 = item.types.includes("text/plain") ? "text/plain" : item.types.find((t) => t.startsWith("text/"));
246
- if (type3) {
247
- text += await item.getType(type3).then((b) => readBlobText(b, window.FileReader));
248
- }
249
- }
250
- return text;
251
- }
252
- async write(data) {
253
- this.items = data;
254
- }
255
- async writeText(text) {
256
- this.items = [createClipboardItem(window, text)];
257
- }
258
- }(), {
259
- [ClipboardStubControl]: control
260
- });
261
- }
262
- function isClipboardStub(clipboard) {
263
- return !!(clipboard == null ? void 0 : clipboard[ClipboardStubControl]);
264
- }
265
- function attachClipboardStubToView(window) {
266
- if (isClipboardStub(window.navigator.clipboard)) {
267
- return window.navigator.clipboard[ClipboardStubControl];
268
- }
269
- const realClipboard = Object.getOwnPropertyDescriptor(window.navigator, "clipboard");
270
- let stub;
271
- const control = {
272
- resetClipboardStub: () => {
273
- stub = createClipboardStub(window, control);
274
- },
275
- detachClipboardStub: () => {
276
- if (realClipboard) {
277
- Object.defineProperty(window.navigator, "clipboard", realClipboard);
278
- } else {
279
- Object.defineProperty(window.navigator, "clipboard", {
280
- value: void 0,
281
- configurable: true
282
- });
283
- }
284
- }
285
- };
286
- stub = createClipboardStub(window, control);
287
- Object.defineProperty(window.navigator, "clipboard", {
288
- get: () => stub,
289
- configurable: true
290
- });
291
- return stub[ClipboardStubControl];
292
- }
293
- function resetClipboardStubOnView(window) {
294
- if (isClipboardStub(window.navigator.clipboard)) {
295
- window.navigator.clipboard[ClipboardStubControl].resetClipboardStub();
296
- }
297
- }
298
- function detachClipboardStubFromView(window) {
299
- if (isClipboardStub(window.navigator.clipboard)) {
300
- window.navigator.clipboard[ClipboardStubControl].detachClipboardStub();
301
- }
302
- }
303
- async function readDataTransferFromClipboard(document) {
304
- const window = document.defaultView;
305
- const clipboard = window == null ? void 0 : window.navigator.clipboard;
306
- const items = clipboard && await clipboard.read();
307
- if (!items) {
308
- throw new Error("The Clipboard API is unavailable.");
309
- }
310
- const dt = createDataTransfer(window);
311
- for (const item of items) {
312
- for (const type3 of item.types) {
313
- dt.setData(type3, await item.getType(type3).then((b) => readBlobText(b, window.FileReader)));
314
- }
315
- }
316
- return dt;
317
- }
318
- async function writeDataTransferToClipboard(document, clipboardData) {
319
- const window = getWindow(document);
320
- const clipboard = window.navigator.clipboard;
321
- const items = [];
322
- for (let i = 0; i < clipboardData.items.length; i++) {
323
- const dtItem = clipboardData.items[i];
324
- const blob = getBlobFromDataTransferItem(window, dtItem);
325
- items.push(createClipboardItem(window, blob));
326
- }
327
- const written = clipboard && await clipboard.write(items).then(() => true, () => false);
328
- if (!written) {
329
- throw new Error("The Clipboard API is unavailable.");
330
- }
331
- }
332
- if (typeof globalThis.afterEach === "function") {
333
- globalThis.afterEach(() => resetClipboardStubOnView(globalThis.window));
334
- }
335
- if (typeof globalThis.afterAll === "function") {
336
- globalThis.afterAll(() => detachClipboardStubFromView(globalThis.window));
337
- }
338
-
339
- // src/utils/edit/isContentEditable.ts
340
- function isContentEditable(element) {
341
- return element.hasAttribute("contenteditable") && (element.getAttribute("contenteditable") == "true" || element.getAttribute("contenteditable") == "");
342
- }
343
- function getContentEditable(node) {
344
- const element = getElement(node);
345
- return element && (element.closest('[contenteditable=""]') || element.closest('[contenteditable="true"]'));
346
- }
347
- function getElement(node) {
348
- return node.nodeType === 1 ? node : node.parentElement;
349
- }
350
-
351
- // src/utils/edit/getValue.ts
352
- function getValue(element) {
353
- if (!element) {
354
- return null;
355
- }
356
- if (isContentEditable(element)) {
357
- return element.textContent;
358
- }
359
- return getUIValue(element);
360
- }
361
-
362
- // src/utils/edit/buildTimeValue.ts
363
- var parseInt = globalThis.parseInt;
364
- function buildTimeValue(value) {
365
- const onlyDigitsValue = value.replace(/\D/g, "");
366
- if (onlyDigitsValue.length < 2) {
367
- return value;
368
- }
369
- const firstDigit = parseInt(onlyDigitsValue[0], 10);
370
- const secondDigit = parseInt(onlyDigitsValue[1], 10);
371
- if (firstDigit >= 3 || firstDigit === 2 && secondDigit >= 4) {
372
- let index;
373
- if (firstDigit >= 3) {
374
- index = 1;
375
- } else {
376
- index = 2;
377
- }
378
- return build(onlyDigitsValue, index);
379
- }
380
- if (value.length === 2) {
381
- return value;
382
- }
383
- return build(onlyDigitsValue, 2);
384
- }
385
- function build(onlyDigitsValue, index) {
386
- const hours = onlyDigitsValue.slice(0, index);
387
- const validHours = Math.min(parseInt(hours, 10), 23);
388
- const minuteCharacters = onlyDigitsValue.slice(index);
389
- const parsedMinutes = parseInt(minuteCharacters, 10);
390
- const validMinutes = Math.min(parsedMinutes, 59);
391
- return `${validHours.toString().padStart(2, "0")}:${validMinutes.toString().padStart(2, "0")}`;
392
- }
393
-
394
- // src/utils/edit/isValidDateOrTimeValue.ts
395
- function isValidDateOrTimeValue(element, value) {
396
- const clone = element.cloneNode();
397
- clone.value = value;
398
- return clone.value === value;
399
- }
400
-
401
- // src/utils/edit/maxLength.ts
402
- var maxLengthSupportedTypes = /* @__PURE__ */ ((maxLengthSupportedTypes2) => {
403
- maxLengthSupportedTypes2["email"] = "email";
404
- maxLengthSupportedTypes2["password"] = "password";
405
- maxLengthSupportedTypes2["search"] = "search";
406
- maxLengthSupportedTypes2["telephone"] = "telephone";
407
- maxLengthSupportedTypes2["text"] = "text";
408
- maxLengthSupportedTypes2["url"] = "url";
409
- return maxLengthSupportedTypes2;
410
- })(maxLengthSupportedTypes || {});
411
- function getSpaceUntilMaxLength(element) {
412
- const value = getValue(element);
413
- if (value === null) {
414
- return void 0;
415
- }
416
- const maxLength = getSanitizedMaxLength(element);
417
- return maxLength ? maxLength - value.length : void 0;
418
- }
419
- function getSanitizedMaxLength(element) {
420
- var _a;
421
- if (!supportsMaxLength(element)) {
422
- return void 0;
423
- }
424
- const attr = (_a = element.getAttribute("maxlength")) != null ? _a : "";
425
- return /^\d+$/.test(attr) && Number(attr) >= 0 ? Number(attr) : void 0;
426
- }
427
- function supportsMaxLength(element) {
428
- return isElementType(element, "textarea") || isElementType(element, "input") && Boolean(maxLengthSupportedTypes[element.type]);
429
- }
430
-
431
- // src/utils/edit/input.ts
432
- function isDateOrTime(element) {
433
- return isElementType(element, "input") && ["date", "time"].includes(element.type);
434
- }
435
- function input(config, element, data, inputType = "insertText") {
436
- const inputRange = getInputRange(element);
437
- if (!inputRange) {
438
- return;
439
- }
440
- if (!isDateOrTime(element)) {
441
- const unprevented = dispatchUIEvent(config, element, "beforeinput", {
442
- inputType,
443
- data
444
- });
445
- if (!unprevented) {
446
- return;
447
- }
448
- }
449
- if ("startContainer" in inputRange) {
450
- editContenteditable(config, element, inputRange, data, inputType);
451
- } else {
452
- editInputElement(config, element, inputRange, data, inputType);
453
- }
454
- }
455
- function editContenteditable(config, element, inputRange, data, inputType) {
456
- let del = false;
457
- if (!inputRange.collapsed) {
458
- del = true;
459
- inputRange.deleteContents();
460
- } else if (["deleteContentBackward", "deleteContentForward"].includes(inputType)) {
461
- const nextPosition = getNextCursorPosition(inputRange.startContainer, inputRange.startOffset, inputType === "deleteContentBackward" ? -1 : 1, inputType);
462
- if (nextPosition) {
463
- del = true;
464
- const delRange = inputRange.cloneRange();
465
- if (delRange.comparePoint(nextPosition.node, nextPosition.offset) < 0) {
466
- delRange.setStart(nextPosition.node, nextPosition.offset);
467
- } else {
468
- delRange.setEnd(nextPosition.node, nextPosition.offset);
469
- }
470
- delRange.deleteContents();
471
- }
472
- }
473
- if (data) {
474
- if (inputRange.endContainer.nodeType === 3) {
475
- const offset = inputRange.endOffset;
476
- inputRange.endContainer.insertData(offset, data);
477
- inputRange.setStart(inputRange.endContainer, offset + data.length);
478
- inputRange.setEnd(inputRange.endContainer, offset + data.length);
479
- } else {
480
- const text = element.ownerDocument.createTextNode(data);
481
- inputRange.insertNode(text);
482
- inputRange.setStart(text, data.length);
483
- inputRange.setEnd(text, data.length);
484
- }
485
- }
486
- if (del || data) {
487
- dispatchUIEvent(config, element, "input", { inputType });
488
- }
489
- }
490
- function editInputElement(config, element, inputRange, data, inputType) {
491
- let dataToInsert = data;
492
- const spaceUntilMaxLength = getSpaceUntilMaxLength(element);
493
- if (spaceUntilMaxLength !== void 0 && data.length > 0) {
494
- if (spaceUntilMaxLength > 0) {
495
- dataToInsert = data.substring(0, spaceUntilMaxLength);
496
- } else {
497
- return;
498
- }
499
- }
500
- const { newValue, newOffset, oldValue } = calculateNewValue(dataToInsert, element, inputRange, inputType);
501
- if (newValue === oldValue && newOffset === inputRange.startOffset && newOffset === inputRange.endOffset) {
502
- return;
503
- }
504
- if (isElementType(element, "input", { type: "number" }) && !isValidNumberInput(newValue)) {
505
- return;
506
- }
507
- setUIValue(element, newValue);
508
- setSelection({
509
- focusNode: element,
510
- anchorOffset: newOffset,
511
- focusOffset: newOffset
512
- });
513
- if (isDateOrTime(element)) {
514
- if (isValidDateOrTimeValue(element, newValue)) {
515
- commitInput(config, element, newOffset, {});
516
- dispatchUIEvent(config, element, "change");
517
- clearInitialValue(element);
518
- }
519
- } else {
520
- commitInput(config, element, newOffset, {
521
- data,
522
- inputType
523
- });
524
- }
525
- }
526
- function calculateNewValue(inputData, node, {
527
- startOffset,
528
- endOffset
529
- }, inputType) {
530
- const value = getUIValue(node);
531
- const prologEnd = Math.max(0, startOffset === endOffset && inputType === "deleteContentBackward" ? startOffset - 1 : startOffset);
532
- const prolog = value.substring(0, prologEnd);
533
- const epilogStart = Math.min(value.length, startOffset === endOffset && inputType === "deleteContentForward" ? startOffset + 1 : endOffset);
534
- const epilog = value.substring(epilogStart, value.length);
535
- let newValue = `${prolog}${inputData}${epilog}`;
536
- let newOffset = prologEnd + inputData.length;
537
- if (isElementType(node, "input", { type: "time" })) {
538
- const builtValue = buildTimeValue(newValue);
539
- if (builtValue !== "" && isValidDateOrTimeValue(node, builtValue)) {
540
- newValue = builtValue;
541
- newOffset = builtValue.length;
542
- }
543
- }
544
- return {
545
- oldValue: value,
546
- newValue,
547
- newOffset
548
- };
549
- }
550
- function commitInput(config, element, newOffset, inputInit) {
551
- dispatchUIEvent(config, element, "input", inputInit);
552
- commitValueAfterInput(element, newOffset);
553
- }
554
- function isValidNumberInput(value) {
555
- var _a, _b;
556
- const valueParts = value.split("e", 2);
557
- return !(/[^\d.\-e]/.test(value) || Number((_a = value.match(/-/g)) == null ? void 0 : _a.length) > 2 || Number((_b = value.match(/\./g)) == null ? void 0 : _b.length) > 1 || valueParts[1] && !/^-?\d*$/.test(valueParts[1]));
558
- }
559
-
560
- // src/utils/edit/isEditable.ts
561
- function isEditable(element) {
562
- return isEditableInput(element) || isElementType(element, "textarea", { readOnly: false }) || isContentEditable(element);
563
- }
564
- var editableInputTypes = /* @__PURE__ */ ((editableInputTypes2) => {
565
- editableInputTypes2["text"] = "text";
566
- editableInputTypes2["date"] = "date";
567
- editableInputTypes2["datetime-local"] = "datetime-local";
568
- editableInputTypes2["email"] = "email";
569
- editableInputTypes2["month"] = "month";
570
- editableInputTypes2["number"] = "number";
571
- editableInputTypes2["password"] = "password";
572
- editableInputTypes2["search"] = "search";
573
- editableInputTypes2["tel"] = "tel";
574
- editableInputTypes2["time"] = "time";
575
- editableInputTypes2["url"] = "url";
576
- editableInputTypes2["week"] = "week";
577
- return editableInputTypes2;
578
- })(editableInputTypes || {});
579
- function isEditableInput(element) {
580
- return isElementType(element, "input", { readOnly: false }) && Boolean(editableInputTypes[element.type]);
581
- }
582
-
583
- // src/utils/edit/setFiles.ts
584
- var fakeFiles = Symbol("files and value properties are mocked");
585
- function setFiles(el, files) {
586
- var _a;
587
- (_a = el[fakeFiles]) == null ? void 0 : _a.restore();
588
- const objectDescriptors = Object.getOwnPropertyDescriptors(el);
589
- const prototypeDescriptors = Object.getOwnPropertyDescriptors(Object.getPrototypeOf(el));
590
- function restore() {
591
- Object.defineProperties(el, {
592
- files: {
593
- ...prototypeDescriptors.files,
594
- ...objectDescriptors.files
595
- },
596
- value: {
597
- ...prototypeDescriptors.value,
598
- ...objectDescriptors.value
599
- },
600
- type: {
601
- ...prototypeDescriptors.type,
602
- ...objectDescriptors.type
603
- }
604
- });
605
- }
606
- el[fakeFiles] = { restore };
607
- Object.defineProperties(el, {
608
- files: {
609
- ...prototypeDescriptors.files,
610
- ...objectDescriptors.files,
611
- get: () => files
612
- },
613
- value: {
614
- ...prototypeDescriptors.value,
615
- ...objectDescriptors.value,
616
- get: () => files.length ? `C:\\fakepath\\${files[0].name}` : "",
617
- set(v) {
618
- var _a2;
619
- if (v === "") {
620
- restore();
621
- } else {
622
- (_a2 = objectDescriptors.value.set) == null ? void 0 : _a2.call(el, v);
623
- }
624
- }
625
- },
626
- type: {
627
- ...prototypeDescriptors.type,
628
- ...objectDescriptors.type,
629
- set(v) {
630
- if (v !== "file") {
631
- restore();
632
- el.type = v;
633
- }
634
- }
635
- }
636
- });
637
- }
638
-
639
- // src/utils/misc/eventWrapper.ts
640
- var import_dom = require("@testing-library/dom");
641
- function eventWrapper(cb) {
642
- let result;
643
- (0, import_dom.getConfig)().eventWrapper(() => {
644
- result = cb();
645
- });
646
- return result;
647
- }
648
-
649
- // src/utils/misc/findClosest.ts
650
- function findClosest(element, callback) {
651
- let el = element;
652
- do {
653
- if (callback(el)) {
654
- return el;
655
- }
656
- el = el.parentElement;
657
- } while (el && el !== element.ownerDocument.body);
658
- return void 0;
659
- }
660
-
661
- // src/utils/misc/isDisabled.ts
662
- function isDisabled(element) {
663
- var _a;
664
- for (let el = element; el; el = el.parentElement) {
665
- if (isElementType(el, [
666
- "button",
667
- "input",
668
- "select",
669
- "textarea",
670
- "optgroup",
671
- "option"
672
- ])) {
673
- if (el.hasAttribute("disabled")) {
674
- return true;
675
- }
676
- } else if (isElementType(el, "fieldset")) {
677
- if (el.hasAttribute("disabled") && !((_a = el.querySelector(":scope > legend")) == null ? void 0 : _a.contains(element))) {
678
- return true;
679
- }
680
- } else if (el.tagName.includes("-")) {
681
- if (el.constructor.formAssociated && el.hasAttribute("disabled")) {
682
- return true;
683
- }
684
- }
685
- }
686
- return false;
687
- }
688
-
689
- // src/utils/focus/getActiveElement.ts
690
- function getActiveElement(document) {
691
- const activeElement = document.activeElement;
692
- if (activeElement == null ? void 0 : activeElement.shadowRoot) {
693
- return getActiveElement(activeElement.shadowRoot);
694
- } else {
695
- if (isDisabled(activeElement)) {
696
- return document.ownerDocument ? document.ownerDocument.body : document.body;
697
- }
698
- return activeElement;
699
- }
700
- }
701
-
702
- // src/utils/focus/selector.ts
703
- var FOCUSABLE_SELECTOR = [
704
- "input:not([type=hidden]):not([disabled])",
705
- "button:not([disabled])",
706
- "select:not([disabled])",
707
- "textarea:not([disabled])",
708
- '[contenteditable=""]',
709
- '[contenteditable="true"]',
710
- "a[href]",
711
- "[tabindex]:not([disabled])"
712
- ].join(", ");
713
-
714
- // src/utils/focus/isFocusable.ts
715
- function isFocusable(element) {
716
- return element.matches(FOCUSABLE_SELECTOR);
717
- }
718
-
719
- // src/utils/focus/cursor.ts
720
- function getNextCursorPosition(node, offset, direction, inputType) {
721
- if (isTextNode(node) && offset + direction >= 0 && offset + direction <= node.nodeValue.length) {
722
- return { node, offset: offset + direction };
723
- }
724
- const nextNode = getNextCharacterContentNode(node, offset, direction);
725
- if (nextNode) {
726
- if (isTextNode(nextNode)) {
727
- return {
728
- node: nextNode,
729
- offset: direction > 0 ? Math.min(1, nextNode.nodeValue.length) : Math.max(nextNode.nodeValue.length - 1, 0)
730
- };
731
- } else if (isElementType(nextNode, "br")) {
732
- const nextPlusOne = getNextCharacterContentNode(nextNode, void 0, direction);
733
- if (!nextPlusOne) {
734
- if (direction < 0 && inputType === "deleteContentBackward") {
735
- return {
736
- node: nextNode.parentNode,
737
- offset: getOffset(nextNode)
738
- };
739
- }
740
- return void 0;
741
- } else if (isTextNode(nextPlusOne)) {
742
- return {
743
- node: nextPlusOne,
744
- offset: direction > 0 ? 0 : nextPlusOne.nodeValue.length
745
- };
746
- } else if (direction < 0 && isElementType(nextPlusOne, "br")) {
747
- return {
748
- node: nextNode.parentNode,
749
- offset: getOffset(nextNode)
750
- };
751
- } else {
752
- return {
753
- node: nextPlusOne.parentNode,
754
- offset: getOffset(nextPlusOne) + (direction > 0 ? 0 : 1)
755
- };
756
- }
757
- } else {
758
- return {
759
- node: nextNode.parentNode,
760
- offset: getOffset(nextNode) + (direction > 0 ? 1 : 0)
761
- };
762
- }
763
- }
764
- }
765
- function getNextCharacterContentNode(node, offset, direction) {
766
- const nextOffset = Number(offset) + (direction < 0 ? -1 : 0);
767
- if (offset !== void 0 && isElement(node) && nextOffset >= 0 && nextOffset < node.children.length) {
768
- node = node.children[nextOffset];
769
- }
770
- return walkNodes(node, direction === 1 ? "next" : "previous", isTreatedAsCharacterContent);
771
- }
772
- function isTreatedAsCharacterContent(node) {
773
- if (isTextNode(node)) {
774
- return true;
775
- }
776
- if (isElement(node)) {
777
- if (isElementType(node, ["input", "textarea"])) {
778
- return node.type !== "hidden";
779
- } else if (isElementType(node, "br")) {
780
- return true;
781
- }
782
- }
783
- return false;
784
- }
785
- function getOffset(node) {
786
- let i = 0;
787
- while (node.previousSibling) {
788
- i++;
789
- node = node.previousSibling;
790
- }
791
- return i;
792
- }
793
- function isElement(node) {
794
- return node.nodeType === 1;
795
- }
796
- function isTextNode(node) {
797
- return node.nodeType === 3;
798
- }
799
- function walkNodes(node, direction, callback) {
800
- var _a;
801
- for (; ; ) {
802
- const sibling = node[`${direction}Sibling`];
803
- if (sibling) {
804
- node = getDescendant(sibling, direction === "next" ? "first" : "last");
805
- if (callback(node)) {
806
- return node;
807
- }
808
- } else if (node.parentNode && (!isElement(node.parentNode) || !isContentEditable(node.parentNode) && node.parentNode !== ((_a = node.ownerDocument) == null ? void 0 : _a.body))) {
809
- node = node.parentNode;
810
- } else {
811
- break;
812
- }
813
- }
814
- }
815
- function getDescendant(node, direction) {
816
- while (node.hasChildNodes()) {
817
- node = node[`${direction}Child`];
818
- }
819
- return node;
820
- }
821
-
822
- // src/utils/focus/selection.ts
823
- function setSelectionRange(element, anchorOffset, focusOffset) {
824
- var _a;
825
- if (hasOwnSelection(element)) {
826
- return setSelection({
827
- focusNode: element,
828
- anchorOffset,
829
- focusOffset
830
- });
831
- }
832
- if (isContentEditable(element) && ((_a = element.firstChild) == null ? void 0 : _a.nodeType) === 3) {
833
- return setSelection({
834
- focusNode: element.firstChild,
835
- anchorOffset,
836
- focusOffset
837
- });
838
- }
839
- throw new Error("Not implemented. The result of this interaction is unreliable.");
840
- }
841
- function hasOwnSelection(node) {
842
- return isElement2(node) && (isElementType(node, "textarea") || isElementType(node, "input") && node.type in editableInputTypes);
843
- }
844
- function isElement2(node) {
845
- return node.nodeType === 1;
846
- }
847
- function getTargetTypeAndSelection(node) {
848
- const element = getElement2(node);
849
- if (element && hasOwnSelection(element)) {
850
- return {
851
- type: "input",
852
- selection: getUISelection(element)
853
- };
854
- }
855
- const selection = element == null ? void 0 : element.ownerDocument.getSelection();
856
- const isCE = getContentEditable(node) && (selection == null ? void 0 : selection.anchorNode) && getContentEditable(selection.anchorNode);
857
- return {
858
- type: isCE ? "contenteditable" : "default",
859
- selection
860
- };
861
- }
862
- function getElement2(node) {
863
- return node.nodeType === 1 ? node : node.parentElement;
864
- }
865
- function updateSelectionOnFocus(element) {
866
- var _a;
867
- const selection = element.ownerDocument.getSelection();
868
- if (!(selection == null ? void 0 : selection.focusNode)) {
869
- return;
870
- }
871
- if (hasOwnSelection(element)) {
872
- const contenteditable = getContentEditable(selection.focusNode);
873
- if (contenteditable) {
874
- if (!selection.isCollapsed) {
875
- const focusNode = ((_a = contenteditable.firstChild) == null ? void 0 : _a.nodeType) === 3 ? contenteditable.firstChild : contenteditable;
876
- selection.setBaseAndExtent(focusNode, 0, focusNode, 0);
877
- }
878
- } else {
879
- selection.setBaseAndExtent(element, 0, element, 0);
880
- }
881
- }
882
- }
883
- function getInputRange(focusNode) {
884
- var _a;
885
- const typeAndSelection = getTargetTypeAndSelection(focusNode);
886
- if (typeAndSelection.type === "input") {
887
- return typeAndSelection.selection;
888
- } else if (typeAndSelection.type === "contenteditable") {
889
- return (_a = typeAndSelection.selection) == null ? void 0 : _a.getRangeAt(0);
890
- }
891
- }
892
- function setSelection({
893
- focusNode,
894
- focusOffset,
895
- anchorNode = focusNode,
896
- anchorOffset = focusOffset
897
- }) {
898
- var _a, _b;
899
- const typeAndSelection = getTargetTypeAndSelection(focusNode);
900
- if (typeAndSelection.type === "input") {
901
- return setUISelection(focusNode, {
902
- anchorOffset,
903
- focusOffset
904
- });
905
- }
906
- (_b = (_a = anchorNode.ownerDocument) == null ? void 0 : _a.getSelection()) == null ? void 0 : _b.setBaseAndExtent(anchorNode, anchorOffset, focusNode, focusOffset);
907
- }
908
- function moveSelection(node, direction) {
909
- if (hasOwnSelection(node)) {
910
- const selection = getUISelection(node);
911
- setSelection({
912
- focusNode: node,
913
- focusOffset: selection.startOffset === selection.endOffset ? selection.focusOffset + direction : direction < 0 ? selection.startOffset : selection.endOffset
914
- });
915
- } else {
916
- const selection = node.ownerDocument.getSelection();
917
- if (!(selection == null ? void 0 : selection.focusNode)) {
918
- return;
919
- }
920
- if (selection.isCollapsed) {
921
- const nextPosition = getNextCursorPosition(selection.focusNode, selection.focusOffset, direction);
922
- if (nextPosition) {
923
- setSelection({
924
- focusNode: nextPosition.node,
925
- focusOffset: nextPosition.offset
926
- });
927
- }
928
- } else {
929
- selection[direction < 0 ? "collapseToStart" : "collapseToEnd"]();
930
- }
931
- }
932
- }
933
-
934
- // src/utils/focus/focus.ts
935
- function focus(element) {
936
- const target = findClosest(element, isFocusable);
937
- const activeElement = getActiveElement(element.ownerDocument);
938
- if ((target != null ? target : element.ownerDocument.body) === activeElement) {
939
- return;
940
- } else if (target) {
941
- eventWrapper(() => target.focus());
942
- } else {
943
- eventWrapper(() => activeElement == null ? void 0 : activeElement.blur());
944
- }
945
- updateSelectionOnFocus(target != null ? target : element.ownerDocument.body);
946
- }
947
-
948
- // src/utils/edit/walkRadio.ts
949
- function walkRadio(config, el, direction) {
950
- const window = getWindow(el);
951
- 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])`));
952
- for (let i = group.findIndex((e) => e === el) + direction; ; i += direction) {
953
- if (!group[i]) {
954
- i = direction > 0 ? 0 : group.length - 1;
955
- }
956
- if (group[i] === el) {
957
- return;
958
- }
959
- if (isDisabled(group[i])) {
960
- continue;
961
- }
962
- focus(group[i]);
963
- dispatchUIEvent(config, group[i], "click");
964
- }
965
- }
966
-
967
- // src/utils/focus/blur.ts
968
- function blur(element) {
969
- if (!isFocusable(element))
970
- return;
971
- const wasActive = getActiveElement(element.ownerDocument) === element;
972
- if (!wasActive)
973
- return;
974
- eventWrapper(() => element.blur());
975
- }
976
-
977
- // src/utils/focus/copySelection.ts
978
- function copySelection(target) {
979
- const data = hasOwnSelection(target) ? { "text/plain": readSelectedValueFromInput(target) } : { "text/plain": String(target.ownerDocument.getSelection()) };
980
- const dt = createDataTransfer(getWindow(target));
981
- for (const type3 in data) {
982
- if (data[type3]) {
983
- dt.setData(type3, data[type3]);
984
- }
985
- }
986
- return dt;
987
- }
988
- function readSelectedValueFromInput(target) {
989
- const sel = getUISelection(target);
990
- const val = getUIValue(target);
991
- return val.substring(sel.startOffset, sel.endOffset);
992
- }
993
-
994
- // src/utils/misc/isVisible.ts
995
- function isVisible(element) {
996
- const window = getWindow(element);
997
- for (let el = element; el == null ? void 0 : el.ownerDocument; el = el.parentElement) {
998
- const { display, visibility } = window.getComputedStyle(el);
999
- if (display === "none") {
1000
- return false;
1001
- }
1002
- if (visibility === "hidden") {
1003
- return false;
1004
- }
1005
- }
1006
- return true;
1007
- }
1008
-
1009
- // src/utils/focus/getTabDestination.ts
1010
- function getTabDestination(activeElement, shift) {
1011
- const document = activeElement.ownerDocument;
1012
- const focusableElements = document.querySelectorAll(FOCUSABLE_SELECTOR);
1013
- const enabledElements = Array.from(focusableElements).filter((el) => el === activeElement || !(Number(el.getAttribute("tabindex")) < 0 || isDisabled(el)));
1014
- if (Number(activeElement.getAttribute("tabindex")) >= 0) {
1015
- enabledElements.sort((a, b) => {
1016
- const i = Number(a.getAttribute("tabindex"));
1017
- const j = Number(b.getAttribute("tabindex"));
1018
- if (i === j) {
1019
- return 0;
1020
- } else if (i === 0) {
1021
- return 1;
1022
- } else if (j === 0) {
1023
- return -1;
1024
- }
1025
- return i - j;
1026
- });
1027
- }
1028
- const checkedRadio = {};
1029
- let prunedElements = [document.body];
1030
- const activeRadioGroup = isElementType(activeElement, "input", {
1031
- type: "radio"
1032
- }) ? activeElement.name : void 0;
1033
- enabledElements.forEach((currentElement) => {
1034
- const el = currentElement;
1035
- if (isElementType(el, "input", { type: "radio" }) && el.name) {
1036
- if (el === activeElement) {
1037
- prunedElements.push(el);
1038
- return;
1039
- } else if (el.name === activeRadioGroup) {
1040
- return;
1041
- }
1042
- if (el.checked) {
1043
- prunedElements = prunedElements.filter((e) => !isElementType(e, "input", { type: "radio", name: el.name }));
1044
- prunedElements.push(el);
1045
- checkedRadio[el.name] = el;
1046
- return;
1047
- }
1048
- if (typeof checkedRadio[el.name] !== "undefined") {
1049
- return;
1050
- }
1051
- }
1052
- prunedElements.push(el);
1053
- });
1054
- for (let index = prunedElements.findIndex((el) => el === activeElement); ; ) {
1055
- index += shift ? -1 : 1;
1056
- if (index === prunedElements.length) {
1057
- index = 0;
1058
- } else if (index === -1) {
1059
- index = prunedElements.length - 1;
1060
- }
1061
- if (prunedElements[index] === activeElement || prunedElements[index] === document.body || isVisible(prunedElements[index])) {
1062
- return prunedElements[index];
1063
- }
1064
- }
1065
- }
1066
-
1067
- // src/utils/focus/selectAll.ts
1068
- function selectAll(target) {
1069
- var _a;
1070
- if (isElementType(target, "textarea") || isElementType(target, "input") && target.type in editableInputTypes) {
1071
- return setSelection({
1072
- focusNode: target,
1073
- anchorOffset: 0,
1074
- focusOffset: getUIValue(target).length
1075
- });
1076
- }
1077
- const focusNode = (_a = getContentEditable(target)) != null ? _a : target.ownerDocument.body;
1078
- setSelection({
1079
- focusNode,
1080
- anchorOffset: 0,
1081
- focusOffset: focusNode.childNodes.length
1082
- });
1083
- }
1084
- function isAllSelected(target) {
1085
- var _a;
1086
- if (isElementType(target, "textarea") || isElementType(target, "input") && target.type in editableInputTypes) {
1087
- return getUISelection(target).startOffset === 0 && getUISelection(target).endOffset === getUIValue(target).length;
1088
- }
1089
- const focusNode = (_a = getContentEditable(target)) != null ? _a : target.ownerDocument.body;
1090
- const selection = target.ownerDocument.getSelection();
1091
- return (selection == null ? void 0 : selection.anchorNode) === focusNode && selection.focusNode === focusNode && selection.anchorOffset === 0 && selection.focusOffset === focusNode.childNodes.length;
1092
- }
1093
-
1094
- // src/utils/keyboard/getKeyEventProps.ts
1095
- function getKeyEventProps(keyDef) {
1096
- return {
1097
- key: keyDef.key,
1098
- code: keyDef.code
1099
- };
1100
- }
1101
-
1102
- // src/utils/keyboard/getUIEventModifiers.ts
1103
- function getUIEventModifiers(keyboardState) {
1104
- return {
1105
- altKey: keyboardState.modifiers.Alt,
1106
- ctrlKey: keyboardState.modifiers.Control,
1107
- metaKey: keyboardState.modifiers.Meta,
1108
- shiftKey: keyboardState.modifiers.Shift,
1109
- modifierAltGraph: keyboardState.modifiers.AltGraph,
1110
- modifierCapsLock: keyboardState.modifiers.CapsLock,
1111
- modifierFn: keyboardState.modifiers.Fn,
1112
- modifierFnLock: keyboardState.modifiers.FnLock,
1113
- modifierNumLock: keyboardState.modifiers.NumLock,
1114
- modifierScrollLock: keyboardState.modifiers.ScrollLock,
1115
- modifierSymbol: keyboardState.modifiers.Symbol,
1116
- modifierSymbolLock: keyboardState.modifiers.SymbolLock
1117
- };
1118
- }
1119
-
1120
- // src/utils/keyDef/readNextDescriptor.ts
1121
- var bracketDict = /* @__PURE__ */ ((bracketDict2) => {
1122
- bracketDict2["{"] = "}";
1123
- bracketDict2["["] = "]";
1124
- return bracketDict2;
1125
- })(bracketDict || {});
1126
- function readNextDescriptor(text, context) {
1127
- let pos = 0;
1128
- const startBracket = text[pos] in bracketDict ? text[pos] : "";
1129
- pos += startBracket.length;
1130
- const isEscapedChar = new RegExp(`^\\${startBracket}{2}`).test(text);
1131
- const type3 = isEscapedChar ? "" : startBracket;
1132
- return {
1133
- type: type3,
1134
- ...type3 === "" ? readPrintableChar(text, pos, context) : readTag(text, pos, type3, context)
1135
- };
1136
- }
1137
- function readPrintableChar(text, pos, context) {
1138
- const descriptor = text[pos];
1139
- assertDescriptor(descriptor, text, pos, context);
1140
- pos += descriptor.length;
1141
- return {
1142
- consumedLength: pos,
1143
- descriptor,
1144
- releasePrevious: false,
1145
- releaseSelf: true,
1146
- repeat: 1
1147
- };
1148
- }
1149
- function readTag(text, pos, startBracket, context) {
1150
- var _a, _b, _c;
1151
- const releasePreviousModifier = text[pos] === "/" ? "/" : "";
1152
- pos += releasePreviousModifier.length;
1153
- const escapedDescriptor = startBracket === "{" && text[pos] === "\\";
1154
- pos += Number(escapedDescriptor);
1155
- const descriptor = escapedDescriptor ? text[pos] : (_a = text.slice(pos).match(startBracket === "{" ? /^\w+|^[^}>/]/ : /^\w+/)) == null ? void 0 : _a[0];
1156
- assertDescriptor(descriptor, text, pos, context);
1157
- pos += descriptor.length;
1158
- const repeatModifier = (_c = (_b = text.slice(pos).match(/^>\d+/)) == null ? void 0 : _b[0]) != null ? _c : "";
1159
- pos += repeatModifier.length;
1160
- const releaseSelfModifier = text[pos] === "/" || !repeatModifier && text[pos] === ">" ? text[pos] : "";
1161
- pos += releaseSelfModifier.length;
1162
- const expectedEndBracket = bracketDict[startBracket];
1163
- const endBracket = text[pos] === expectedEndBracket ? expectedEndBracket : "";
1164
- if (!endBracket) {
1165
- throw new Error(getErrorMessage([
1166
- !repeatModifier && "repeat modifier",
1167
- !releaseSelfModifier && "release modifier",
1168
- `"${expectedEndBracket}"`
1169
- ].filter(Boolean).join(" or "), text[pos], text, context));
1170
- }
1171
- pos += endBracket.length;
1172
- return {
1173
- consumedLength: pos,
1174
- descriptor,
1175
- releasePrevious: !!releasePreviousModifier,
1176
- repeat: repeatModifier ? Math.max(Number(repeatModifier.substr(1)), 1) : 1,
1177
- releaseSelf: hasReleaseSelf(releaseSelfModifier, repeatModifier)
1178
- };
1179
- }
1180
- function assertDescriptor(descriptor, text, pos, context) {
1181
- if (!descriptor) {
1182
- throw new Error(getErrorMessage("key descriptor", text[pos], text, context));
1183
- }
1184
- }
1185
- function hasReleaseSelf(releaseSelfModifier, repeatModifier) {
1186
- if (releaseSelfModifier) {
1187
- return releaseSelfModifier === "/";
1188
- }
1189
- if (repeatModifier) {
1190
- return false;
1191
- }
1192
- }
1193
- function getErrorMessage(expected, found, text, context) {
1194
- return `Expected ${expected} but found "${found != null ? found : ""}" in "${text}"
1195
- See ${context === "pointer" ? `https://testing-library.com/docs/user-event/pointer#pressing-a-button-or-touching-the-screen` : `https://testing-library.com/docs/user-event/keyboard`}
1196
- for more information about how userEvent parses your input.`;
1197
- }
1198
-
1199
- // src/utils/misc/cloneEvent.ts
1200
- function cloneEvent(event) {
1201
- return new event.constructor(event.type, event);
1202
- }
1203
-
1204
- // src/utils/misc/getDocumentFromNode.ts
1205
- function getDocumentFromNode(el) {
1206
- return isDocument(el) ? el : el.ownerDocument;
1207
- }
1208
- function isDocument(node) {
1209
- return node.nodeType === 9;
1210
- }
1211
-
1212
- // src/utils/misc/isDescendantOrSelf.ts
1213
- function isDescendantOrSelf(potentialDescendant, potentialAncestor) {
1214
- let el = potentialDescendant;
1215
- do {
1216
- if (el === potentialAncestor) {
1217
- return true;
1218
- }
1219
- el = el.parentElement;
1220
- } while (el);
1221
- return false;
1222
- }
1223
-
1224
- // src/utils/misc/level.ts
1225
- var Level = Symbol("Api level refs");
1226
- function setLevelRef(config, level) {
1227
- var _a;
1228
- (_a = config[Level]) != null ? _a : config[Level] = {};
1229
- config[Level][level] = {};
1230
- }
1231
- function getLevelRef(config, level) {
1232
- var _a;
1233
- return (_a = config[Level]) == null ? void 0 : _a[level];
1234
- }
1235
-
1236
- // src/utils/misc/wait.ts
1237
- function wait(config) {
1238
- const delay = config.delay;
1239
- if (typeof delay !== "number") {
1240
- return;
1241
- }
1242
- return Promise.all([
1243
- new Promise((resolve) => globalThis.setTimeout(() => resolve(), delay)),
1244
- config.advanceTimers(delay)
1245
- ]);
1246
- }
1247
-
1248
- // src/keyboard/keyMap.ts
1249
- var defaultKeyMap = [
1250
- ..."0123456789".split("").map((c) => ({ code: `Digit${c}`, key: c })),
1251
- ...")!@#$%^&*(".split("").map((c, i) => ({ code: `Digit${i}`, key: c, shiftKey: true })),
1252
- ..."abcdefghijklmnopqrstuvwxyz".split("").map((c) => ({ code: `Key${c.toUpperCase()}`, key: c })),
1253
- ..."ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("").map((c) => ({ code: `Key${c}`, key: c, shiftKey: true })),
1254
- { code: "Space", key: " " },
1255
- { code: "AltLeft", key: "Alt", location: 1 /* LEFT */ },
1256
- { code: "AltRight", key: "Alt", location: 2 /* RIGHT */ },
1257
- {
1258
- code: "ShiftLeft",
1259
- key: "Shift",
1260
- location: 1 /* LEFT */
1261
- },
1262
- {
1263
- code: "ShiftRight",
1264
- key: "Shift",
1265
- location: 2 /* RIGHT */
1266
- },
1267
- {
1268
- code: "ControlLeft",
1269
- key: "Control",
1270
- location: 1 /* LEFT */
1271
- },
1272
- {
1273
- code: "ControlRight",
1274
- key: "Control",
1275
- location: 2 /* RIGHT */
1276
- },
1277
- { code: "MetaLeft", key: "Meta", location: 1 /* LEFT */ },
1278
- {
1279
- code: "MetaRight",
1280
- key: "Meta",
1281
- location: 2 /* RIGHT */
1282
- },
1283
- { code: "OSLeft", key: "OS", location: 1 /* LEFT */ },
1284
- { code: "OSRight", key: "OS", location: 2 /* RIGHT */ },
1285
- { code: "Tab", key: "Tab" },
1286
- { code: "CapsLock", key: "CapsLock" },
1287
- { code: "Backspace", key: "Backspace" },
1288
- { code: "Enter", key: "Enter" },
1289
- { code: "Escape", key: "Escape" },
1290
- { code: "ArrowUp", key: "ArrowUp" },
1291
- { code: "ArrowDown", key: "ArrowDown" },
1292
- { code: "ArrowLeft", key: "ArrowLeft" },
1293
- { code: "ArrowRight", key: "ArrowRight" },
1294
- { code: "Home", key: "Home" },
1295
- { code: "End", key: "End" },
1296
- { code: "Delete", key: "Delete" },
1297
- { code: "PageUp", key: "PageUp" },
1298
- { code: "PageDown", key: "PageDown" },
1299
- { code: "Fn", key: "Fn" },
1300
- { code: "Symbol", key: "Symbol" },
1301
- { code: "AltRight", key: "AltGraph" }
1302
- ];
1303
-
1304
- // src/pointer/keyMap.ts
1305
- var defaultKeyMap2 = [
1306
- { name: "MouseLeft", pointerType: "mouse", button: "primary" },
1307
- { name: "MouseRight", pointerType: "mouse", button: "secondary" },
1308
- { name: "MouseMiddle", pointerType: "mouse", button: "auxiliary" },
1309
- { name: "TouchA", pointerType: "touch" },
1310
- { name: "TouchB", pointerType: "touch" },
1311
- { name: "TouchC", pointerType: "touch" }
1312
- ];
1313
-
1314
- // src/options.ts
1315
- var PointerEventsCheckLevel = /* @__PURE__ */ ((PointerEventsCheckLevel2) => {
1316
- PointerEventsCheckLevel2[PointerEventsCheckLevel2["EachTrigger"] = 4] = "EachTrigger";
1317
- PointerEventsCheckLevel2[PointerEventsCheckLevel2["EachApiCall"] = 2] = "EachApiCall";
1318
- PointerEventsCheckLevel2[PointerEventsCheckLevel2["EachTarget"] = 1] = "EachTarget";
1319
- PointerEventsCheckLevel2[PointerEventsCheckLevel2["Never"] = 0] = "Never";
1320
- return PointerEventsCheckLevel2;
1321
- })(PointerEventsCheckLevel || {});
1322
- var defaultOptionsDirect = {
1323
- applyAccept: true,
1324
- autoModify: true,
1325
- delay: 0,
1326
- document: globalThis.document,
1327
- keyboardMap: defaultKeyMap,
1328
- pointerMap: defaultKeyMap2,
1329
- pointerEventsCheck: 2 /* EachApiCall */,
1330
- skipAutoClose: false,
1331
- skipClick: false,
1332
- skipHover: false,
1333
- writeToClipboard: false,
1334
- advanceTimers: () => Promise.resolve()
1335
- };
1336
- var defaultOptionsSetup = {
1337
- ...defaultOptionsDirect,
1338
- writeToClipboard: true
1339
- };
1340
-
1341
- // src/utils/pointer/cssPointerEvents.ts
1342
- function hasPointerEvents(config, element) {
1343
- var _a;
1344
- return ((_a = checkPointerEvents(config, element)) == null ? void 0 : _a.pointerEvents) !== "none";
1345
- }
1346
- function closestPointerEventsDeclaration(element) {
1347
- const window = getWindow(element);
1348
- for (let el = element, tree = []; el == null ? void 0 : el.ownerDocument; el = el.parentElement) {
1349
- tree.push(el);
1350
- const pointerEvents = window.getComputedStyle(el).pointerEvents;
1351
- if (pointerEvents && !["inherit", "unset"].includes(pointerEvents)) {
1352
- return { pointerEvents, tree };
1353
- }
1354
- }
1355
- return void 0;
1356
- }
1357
- var PointerEventsCheck = Symbol("Last check for pointer-events");
1358
- function checkPointerEvents(config, element) {
1359
- const lastCheck = element[PointerEventsCheck];
1360
- const needsCheck = config.pointerEventsCheck !== 0 /* Never */ && (!lastCheck || hasBitFlag(config.pointerEventsCheck, 2 /* EachApiCall */) && lastCheck[1 /* Call */] !== getLevelRef(config, 1 /* Call */) || hasBitFlag(config.pointerEventsCheck, 4 /* EachTrigger */) && lastCheck[2 /* Trigger */] !== getLevelRef(config, 2 /* Trigger */));
1361
- if (!needsCheck) {
1362
- return lastCheck == null ? void 0 : lastCheck.result;
1363
- }
1364
- const declaration = closestPointerEventsDeclaration(element);
1365
- element[PointerEventsCheck] = {
1366
- [1 /* Call */]: getLevelRef(config, 1 /* Call */),
1367
- [2 /* Trigger */]: getLevelRef(config, 2 /* Trigger */),
1368
- result: declaration
1369
- };
1370
- return declaration;
1371
- }
1372
- function assertPointerEvents(config, element) {
1373
- const declaration = checkPointerEvents(config, element);
1374
- if ((declaration == null ? void 0 : declaration.pointerEvents) === "none") {
1375
- throw new Error([
1376
- `Unable to perform pointer interaction as the element ${declaration.tree.length > 1 ? "inherits" : "has"} \`pointer-events: none\`:`,
1377
- "",
1378
- printTree(declaration.tree)
1379
- ].join("\n"));
1380
- }
1381
- }
1382
- function printTree(tree) {
1383
- return tree.reverse().map((el, i) => [
1384
- "".padEnd(i),
1385
- el.tagName,
1386
- el.id && `#${el.id}`,
1387
- el.hasAttribute("data-testid") && `(testId=${el.getAttribute("data-testid")})`,
1388
- getLabelDescr(el),
1389
- tree.length > 1 && i === 0 && " <-- This element declared `pointer-events: none`",
1390
- tree.length > 1 && i === tree.length - 1 && " <-- Asserted pointer events here"
1391
- ].filter(Boolean).join("")).join("\n");
1392
- }
1393
- function getLabelDescr(element) {
1394
- var _a, _b, _c, _d;
1395
- let label;
1396
- if (element.hasAttribute("aria-label")) {
1397
- label = element.getAttribute("aria-label");
1398
- } else if (element.hasAttribute("aria-labelledby")) {
1399
- label = (_b = (_a = element.ownerDocument.getElementById(element.getAttribute("aria-labelledby"))) == null ? void 0 : _a.textContent) == null ? void 0 : _b.trim();
1400
- } else if (isElementType(element, [
1401
- "button",
1402
- "input",
1403
- "meter",
1404
- "output",
1405
- "progress",
1406
- "select",
1407
- "textarea"
1408
- ]) && ((_c = element.labels) == null ? void 0 : _c.length)) {
1409
- label = Array.from(element.labels).map((el) => {
1410
- var _a2;
1411
- return (_a2 = el.textContent) == null ? void 0 : _a2.trim();
1412
- }).join("|");
1413
- } else if (isElementType(element, "button")) {
1414
- label = (_d = element.textContent) == null ? void 0 : _d.trim();
1415
- }
1416
- label = label == null ? void 0 : label.replace(/\n/g, " ");
1417
- if (Number(label == null ? void 0 : label.length) > 30) {
1418
- label = `${label == null ? void 0 : label.substring(0, 29)}\u2026`;
1419
- }
1420
- return label ? `(label=${label})` : "";
1421
- }
1422
- function hasBitFlag(conf, flag) {
1423
- return (conf & flag) > 0;
1424
- }
1425
-
1426
- // src/utils/pointer/mouseButtons.ts
1427
- var MouseButton = {
1428
- primary: 0,
1429
- secondary: 1,
1430
- auxiliary: 2,
1431
- back: 3,
1432
- X1: 3,
1433
- forward: 4,
1434
- X2: 4
1435
- };
1436
- var MouseButtonFlip = {
1437
- auxiliary: 1,
1438
- secondary: 2,
1439
- 1: 2,
1440
- 2: 1
1441
- };
1442
- function getMouseButton(button) {
1443
- if (button in MouseButtonFlip) {
1444
- return MouseButtonFlip[button];
1445
- }
1446
- return typeof button === "number" ? button : MouseButton[button];
1447
- }
1448
- function getMouseButtons(...buttons) {
1449
- let v = 0;
1450
- for (const t of buttons) {
1451
- const pos = typeof t === "number" ? t : MouseButton[t];
1452
- v |= 2 ** pos;
1453
- }
1454
- return v;
1455
- }
1456
-
1457
- // src/event/createEvent.ts
1458
- var import_dom2 = require("@testing-library/dom");
1459
-
1460
- // src/event/eventMap.ts
1461
- var import_event_map = require("@testing-library/dom/dist/event-map.js");
1462
- var eventMap = {
1463
- ...import_event_map.eventMap,
1464
- beforeInput: {
1465
- EventType: "InputEvent",
1466
- defaultInit: { bubbles: true, cancelable: true, composed: true }
1467
- }
1468
- };
1469
- var eventMapKeys = Object.fromEntries(Object.keys(eventMap).map((k) => [k.toLowerCase(), k]));
1470
-
1471
- // src/event/eventTypes.ts
1472
- var import_event_map2 = require("@testing-library/dom/dist/event-map.js");
1473
- var eventKeys = Object.fromEntries(Object.keys(import_event_map2.eventMap).map((k) => [k.toLowerCase(), k]));
1474
- function getEventClass(type3) {
1475
- return type3 in eventKeys ? import_event_map2.eventMap[eventKeys[type3]].EventType : "Event";
1476
- }
1477
- var mouseEvents = ["MouseEvent", "PointerEvent"];
1478
- function isMouseEvent(type3) {
1479
- return mouseEvents.includes(getEventClass(type3));
1480
- }
1481
- function isKeyboardEvent(type3) {
1482
- return getEventClass(type3) === "KeyboardEvent";
1483
- }
1484
-
1485
- // src/event/createEvent.ts
1486
- function createEvent(type3, target, init) {
1487
- const event = (0, import_dom2.createEvent)(type3, target, init, eventMap[eventMapKeys[type3]]);
1488
- if (isMouseEvent(type3) && init) {
1489
- assignPositionInit(event, init);
1490
- assignPointerInit(event, init);
1491
- }
1492
- return event;
1493
- }
1494
- function assignProps(obj, props) {
1495
- for (const [key, value] of Object.entries(props)) {
1496
- Object.defineProperty(obj, key, { get: () => value });
1497
- }
1498
- }
1499
- function assignPositionInit(obj, {
1500
- x,
1501
- y,
1502
- clientX,
1503
- clientY,
1504
- offsetX,
1505
- offsetY,
1506
- pageX,
1507
- pageY,
1508
- screenX,
1509
- screenY
1510
- }) {
1511
- var _a, _b, _c, _d;
1512
- assignProps(obj, {
1513
- x: (_a = x != null ? x : clientX) != null ? _a : 0,
1514
- y: (_b = y != null ? y : clientY) != null ? _b : 0,
1515
- clientX: (_c = x != null ? x : clientX) != null ? _c : 0,
1516
- clientY: (_d = y != null ? y : clientY) != null ? _d : 0,
1517
- offsetX: offsetX != null ? offsetX : 0,
1518
- offsetY: offsetY != null ? offsetY : 0,
1519
- pageX: pageX != null ? pageX : 0,
1520
- pageY: pageY != null ? pageY : 0,
1521
- screenX: screenX != null ? screenX : 0,
1522
- screenY: screenY != null ? screenY : 0
1523
- });
1524
- }
1525
- function assignPointerInit(obj, { isPrimary, pointerId, pointerType }) {
1526
- assignProps(obj, {
1527
- isPrimary,
1528
- pointerId,
1529
- pointerType
1530
- });
1531
- }
1532
-
1533
- // src/event/behavior/registry.ts
1534
- var behavior = {};
1535
-
1536
- // src/event/behavior/click.ts
1537
- behavior.click = (event, target, config) => {
1538
- const context = target.closest("button,input,label,select,textarea");
1539
- const control = context && isElementType(context, "label") && context.control;
1540
- if (control) {
1541
- return () => {
1542
- if (isFocusable(control)) {
1543
- focus(control);
1544
- }
1545
- dispatchEvent(config, control, cloneEvent(event));
1546
- };
1547
- } else if (isElementType(target, "input", { type: "file" })) {
1548
- return () => {
1549
- blur(target);
1550
- target.dispatchEvent(new (getWindow(target)).Event("fileDialog"));
1551
- focus(target);
1552
- };
1553
- }
1554
- };
1555
-
1556
- // src/event/behavior/cut.ts
1557
- behavior.cut = (event, target, config) => {
1558
- return () => {
1559
- if (isEditable(target)) {
1560
- input(config, target, "", "deleteByCut");
1561
- }
1562
- };
1563
- };
1564
-
1565
- // src/event/behavior/keydown.ts
1566
- behavior.keydown = (event, target, config) => {
1567
- var _a, _b;
1568
- return (_b = (_a = keydownBehavior[event.key]) == null ? void 0 : _a.call(keydownBehavior, event, target, config)) != null ? _b : combinationBehavior(event, target, config);
1569
- };
1570
- var keydownBehavior = {
1571
- ArrowDown: (event, target, config) => {
1572
- if (isElementType(target, "input", { type: "radio" })) {
1573
- return () => walkRadio(config, target, -1);
1574
- }
1575
- },
1576
- ArrowLeft: (event, target, config) => {
1577
- if (isElementType(target, "input", { type: "radio" })) {
1578
- return () => walkRadio(config, target, -1);
1579
- }
1580
- return () => moveSelection(target, -1);
1581
- },
1582
- ArrowRight: (event, target, config) => {
1583
- if (isElementType(target, "input", { type: "radio" })) {
1584
- return () => walkRadio(config, target, 1);
1585
- }
1586
- return () => moveSelection(target, 1);
1587
- },
1588
- ArrowUp: (event, target, config) => {
1589
- if (isElementType(target, "input", { type: "radio" })) {
1590
- return () => walkRadio(config, target, 1);
1591
- }
1592
- },
1593
- Backspace: (event, target, config) => {
1594
- if (isEditable(target)) {
1595
- return () => {
1596
- input(config, target, "", "deleteContentBackward");
1597
- };
1598
- }
1599
- },
1600
- Delete: (event, target, config) => {
1601
- if (isEditable(target)) {
1602
- return () => {
1603
- input(config, target, "", "deleteContentForward");
1604
- };
1605
- }
1606
- },
1607
- End: (event, target) => {
1608
- if (isElementType(target, ["input", "textarea"]) || isContentEditable(target)) {
1609
- return () => {
1610
- var _a, _b;
1611
- const newPos = (_b = (_a = getValue(target)) == null ? void 0 : _a.length) != null ? _b : 0;
1612
- setSelectionRange(target, newPos, newPos);
1613
- };
1614
- }
1615
- },
1616
- Home: (event, target) => {
1617
- if (isElementType(target, ["input", "textarea"]) || isContentEditable(target)) {
1618
- return () => {
1619
- setSelectionRange(target, 0, 0);
1620
- };
1621
- }
1622
- },
1623
- PageDown: (event, target) => {
1624
- if (isElementType(target, ["input"])) {
1625
- return () => {
1626
- const newPos = getValue(target).length;
1627
- setSelectionRange(target, newPos, newPos);
1628
- };
1629
- }
1630
- },
1631
- PageUp: (event, target) => {
1632
- if (isElementType(target, ["input"])) {
1633
- return () => {
1634
- setSelectionRange(target, 0, 0);
1635
- };
1636
- }
1637
- },
1638
- Tab: (event, target, { keyboardState }) => {
1639
- return () => {
1640
- const dest = getTabDestination(target, keyboardState.modifiers.Shift);
1641
- focus(dest);
1642
- if (hasOwnSelection(dest)) {
1643
- setUISelection(dest, {
1644
- anchorOffset: 0,
1645
- focusOffset: dest.value.length
1646
- });
1647
- }
1648
- };
1649
- }
1650
- };
1651
- var combinationBehavior = (event, target, config) => {
1652
- if (event.code === "KeyA" && config.keyboardState.modifiers.Control) {
1653
- return () => selectAll(target);
1654
- }
1655
- };
1656
-
1657
- // src/event/behavior/keypress.ts
1658
- behavior.keypress = (event, target, config) => {
1659
- if (event.key === "Enter") {
1660
- if (isElementType(target, "button") || isElementType(target, "input") && ClickInputOnEnter.includes(target.type) || isElementType(target, "a") && Boolean(target.href)) {
1661
- return () => {
1662
- dispatchUIEvent(config, target, "click");
1663
- };
1664
- } else if (isElementType(target, "input")) {
1665
- const form = target.form;
1666
- const submit = form == null ? void 0 : form.querySelector('input[type="submit"], button:not([type]), button[type="submit"]');
1667
- if (submit) {
1668
- return () => dispatchUIEvent(config, submit, "click");
1669
- } else if (form && SubmitSingleInputOnEnter.includes(target.type) && form.querySelectorAll("input").length === 1) {
1670
- return () => dispatchUIEvent(config, form, "submit");
1671
- } else {
1672
- return;
1673
- }
1674
- }
1675
- }
1676
- if (isEditable(target)) {
1677
- const inputType = event.key === "Enter" ? isContentEditable(target) && !config.keyboardState.modifiers.Shift ? "insertParagraph" : "insertLineBreak" : "insertText";
1678
- const inputData = event.key === "Enter" ? "\n" : event.key;
1679
- return () => input(config, target, inputData, inputType);
1680
- }
1681
- };
1682
- var ClickInputOnEnter = [
1683
- "button",
1684
- "color",
1685
- "file",
1686
- "image",
1687
- "reset",
1688
- "submit"
1689
- ];
1690
- var SubmitSingleInputOnEnter = [
1691
- "email",
1692
- "month",
1693
- "password",
1694
- "search",
1695
- "tel",
1696
- "text",
1697
- "url",
1698
- "week"
1699
- ];
1700
-
1701
- // src/event/behavior/keyup.ts
1702
- behavior.keyup = (event, target, config) => {
1703
- var _a;
1704
- return (_a = keyupBehavior[event.key]) == null ? void 0 : _a.call(keyupBehavior, event, target, config);
1705
- };
1706
- var keyupBehavior = {
1707
- " ": (event, target, config) => {
1708
- if (isClickableInput(target)) {
1709
- return () => dispatchUIEvent(config, target, "click");
1710
- }
1711
- }
1712
- };
1713
-
1714
- // src/event/behavior/paste.ts
1715
- behavior.paste = (event, target, config) => {
1716
- if (isEditable(target)) {
1717
- return () => {
1718
- var _a;
1719
- const insertData = (_a = event.clipboardData) == null ? void 0 : _a.getData("text");
1720
- if (insertData) {
1721
- input(config, target, insertData, "insertFromPaste");
1722
- }
1723
- };
1724
- }
1725
- };
1726
-
1727
- // src/event/wrapEvent.ts
1728
- var import_dom3 = require("@testing-library/dom");
1729
- function wrapEvent(cb, _element) {
1730
- return (0, import_dom3.getConfig)().eventWrapper(cb);
1731
- }
1732
-
1733
- // src/event/dispatchEvent.ts
1734
- function dispatchEvent(config, target, event, preventDefault = false) {
1735
- var _a, _b;
1736
- const type3 = event.type;
1737
- const behaviorImplementation = preventDefault ? () => {
1738
- } : (_b = (_a = behavior)[type3]) == null ? void 0 : _b.call(_a, event, target, config);
1739
- if (behaviorImplementation) {
1740
- event.preventDefault();
1741
- let defaultPrevented = false;
1742
- Object.defineProperty(event, "defaultPrevented", {
1743
- get: () => defaultPrevented
1744
- });
1745
- Object.defineProperty(event, "preventDefault", {
1746
- value: () => {
1747
- defaultPrevented = event.cancelable;
1748
- }
1749
- });
1750
- wrapEvent(() => target.dispatchEvent(event), target);
1751
- if (!defaultPrevented) {
1752
- behaviorImplementation();
1753
- }
1754
- return !defaultPrevented;
1755
- }
1756
- return wrapEvent(() => target.dispatchEvent(event), target);
1757
- }
1758
-
1759
- // src/event/index.ts
1760
- function dispatchUIEvent(config, target, type3, init, preventDefault = false) {
1761
- if (isMouseEvent(type3) || isKeyboardEvent(type3)) {
1762
- init = {
1763
- ...init,
1764
- ...getUIEventModifiers(config.keyboardState)
1765
- };
1766
- }
1767
- const event = createEvent(type3, target, init);
1768
- return dispatchEvent(config, target, event, preventDefault);
1769
- }
1770
- function bindDispatchUIEvent(config) {
1771
- return dispatchUIEvent.bind(void 0, config);
1772
- }
1773
-
1774
- // src/document/interceptor.ts
1775
- var Interceptor = Symbol("Interceptor for programmatical calls");
1776
- function prepareInterceptor(element, propName, interceptorImpl) {
1777
- const prototypeDescriptor = Object.getOwnPropertyDescriptor(element.constructor.prototype, propName);
1778
- const objectDescriptor = Object.getOwnPropertyDescriptor(element, propName);
1779
- const target = (prototypeDescriptor == null ? void 0 : prototypeDescriptor.set) ? "set" : "value";
1780
- if (typeof (prototypeDescriptor == null ? void 0 : prototypeDescriptor[target]) !== "function" || prototypeDescriptor[target][Interceptor]) {
1781
- throw new Error(`Element ${element.tagName} does not implement "${String(propName)}".`);
1782
- }
1783
- function intercept(...args) {
1784
- const {
1785
- applyNative = false,
1786
- realArgs,
1787
- then
1788
- } = interceptorImpl.call(this, ...args);
1789
- const realFunc = (!applyNative && objectDescriptor || prototypeDescriptor)[target];
1790
- if (target === "set") {
1791
- realFunc.call(this, realArgs);
1792
- } else {
1793
- realFunc.call(this, ...realArgs);
1794
- }
1795
- then == null ? void 0 : then();
1796
- }
1797
- ;
1798
- intercept[Interceptor] = Interceptor;
1799
- Object.defineProperty(element, propName, {
1800
- ...objectDescriptor != null ? objectDescriptor : prototypeDescriptor,
1801
- [target]: intercept
1802
- });
1803
- }
1804
-
1805
- // src/document/selection.ts
1806
- var UISelection = Symbol("Displayed selection in UI");
1807
- function prepareSelectionInterceptor(element) {
1808
- prepareInterceptor(element, "setSelectionRange", function interceptorImpl(start, ...others) {
1809
- const isUI = start && typeof start === "object" && start[UISelection];
1810
- if (!isUI) {
1811
- this[UISelection] = void 0;
1812
- }
1813
- return {
1814
- applyNative: !!isUI,
1815
- realArgs: [Number(start), ...others]
1816
- };
1817
- });
1818
- prepareInterceptor(element, "selectionStart", function interceptorImpl(v) {
1819
- this[UISelection] = void 0;
1820
- return { realArgs: v };
1821
- });
1822
- prepareInterceptor(element, "selectionEnd", function interceptorImpl(v) {
1823
- this[UISelection] = void 0;
1824
- return { realArgs: v };
1825
- });
1826
- prepareInterceptor(element, "select", function interceptorImpl() {
1827
- this[UISelection] = {
1828
- anchorOffset: 0,
1829
- focusOffset: getUIValue(element).length
1830
- };
1831
- return { realArgs: [] };
1832
- });
1833
- }
1834
- function setUISelection(element, {
1835
- focusOffset: focusOffsetParam,
1836
- anchorOffset: anchorOffsetParam = focusOffsetParam
1837
- }, mode = "replace") {
1838
- const valueLength = getUIValue(element).length;
1839
- const sanitizeOffset = (o) => Math.max(0, Math.min(valueLength, o));
1840
- const anchorOffset = mode === "replace" || element[UISelection] === void 0 ? sanitizeOffset(anchorOffsetParam) : element[UISelection].anchorOffset;
1841
- const focusOffset = sanitizeOffset(focusOffsetParam);
1842
- const startOffset = Math.min(anchorOffset, focusOffset);
1843
- const endOffset = Math.max(anchorOffset, focusOffset);
1844
- element[UISelection] = {
1845
- anchorOffset,
1846
- focusOffset
1847
- };
1848
- if (element.selectionStart === startOffset && element.selectionEnd === endOffset) {
1849
- return;
1850
- }
1851
- const startObj = new Number(startOffset);
1852
- startObj[UISelection] = UISelection;
1853
- try {
1854
- element.setSelectionRange(startObj, endOffset);
1855
- } catch {
1856
- }
1857
- }
1858
- function getUISelection(element) {
1859
- var _a, _b, _c;
1860
- const sel = (_c = element[UISelection]) != null ? _c : {
1861
- anchorOffset: (_a = element.selectionStart) != null ? _a : 0,
1862
- focusOffset: (_b = element.selectionEnd) != null ? _b : 0
1863
- };
1864
- return {
1865
- ...sel,
1866
- startOffset: Math.min(sel.anchorOffset, sel.focusOffset),
1867
- endOffset: Math.max(sel.anchorOffset, sel.focusOffset)
1868
- };
1869
- }
1870
- function hasUISelection(element) {
1871
- return !!element[UISelection];
1872
- }
1873
- function setUISelectionClean(element) {
1874
- element[UISelection] = void 0;
1875
- }
1876
-
1877
- // src/document/value.ts
1878
- var UIValue = Symbol("Displayed value in UI");
1879
- var InitialValue = Symbol("Initial value to compare on blur");
1880
- var TrackChanges = Symbol("Track programmatic changes for React workaround");
1881
- function valueInterceptor(v) {
1882
- const isUI = typeof v === "object" && v[UIValue];
1883
- if (isUI) {
1884
- this[UIValue] = String(v);
1885
- startTrackValue(this);
1886
- }
1887
- return {
1888
- applyNative: !!isUI,
1889
- realArgs: sanitizeValue(this, v),
1890
- then: isUI ? void 0 : () => trackOrSetValue(this, String(v))
1891
- };
1892
- }
1893
- function sanitizeValue(element, v) {
1894
- if (isElementType(element, "input", { type: "number" }) && String(v) !== "" && !Number.isNaN(Number(v))) {
1895
- return String(Number(v));
1896
- }
1897
- return String(v);
1898
- }
1899
- function prepareValueInterceptor(element) {
1900
- prepareInterceptor(element, "value", valueInterceptor);
1901
- }
1902
- function setUIValue(element, value) {
1903
- if (element[InitialValue] === void 0) {
1904
- element[InitialValue] = element.value;
1905
- }
1906
- element.value = {
1907
- [UIValue]: UIValue,
1908
- toString: () => value
1909
- };
1910
- }
1911
- function getUIValue(element) {
1912
- return element[UIValue] === void 0 ? element.value : String(element[UIValue]);
1913
- }
1914
- function setUIValueClean(element) {
1915
- element[UIValue] = void 0;
1916
- }
1917
- function clearInitialValue(element) {
1918
- element[InitialValue] = void 0;
1919
- }
1920
- function getInitialValue(element) {
1921
- return element[InitialValue];
1922
- }
1923
- function isReact17Element(element) {
1924
- return Object.getOwnPropertyNames(element).some((k) => k.startsWith("__react")) && getWindow(element).REACT_VERSION === 17;
1925
- }
1926
- function startTrackValue(element) {
1927
- if (!isReact17Element(element)) {
1928
- return;
1929
- }
1930
- element[TrackChanges] = {
1931
- previousValue: String(element.value),
1932
- tracked: []
1933
- };
1934
- }
1935
- function trackOrSetValue(element, v) {
1936
- var _a, _b;
1937
- (_b = (_a = element[TrackChanges]) == null ? void 0 : _a.tracked) == null ? void 0 : _b.push(v);
1938
- if (!element[TrackChanges]) {
1939
- setUIValueClean(element);
1940
- setUISelection(element, { focusOffset: v.length });
1941
- }
1942
- }
1943
- function commitValueAfterInput(element, cursorOffset) {
1944
- var _a;
1945
- const changes = element[TrackChanges];
1946
- element[TrackChanges] = void 0;
1947
- if (!((_a = changes == null ? void 0 : changes.tracked) == null ? void 0 : _a.length)) {
1948
- return;
1949
- }
1950
- const isJustReactStateUpdate = changes.tracked.length === 2 && changes.tracked[0] === changes.previousValue && changes.tracked[1] === element.value;
1951
- if (!isJustReactStateUpdate) {
1952
- setUIValueClean(element);
1953
- }
1954
- if (hasUISelection(element)) {
1955
- setUISelection(element, {
1956
- focusOffset: isJustReactStateUpdate ? cursorOffset : element.value.length
1957
- });
1958
- }
1959
- }
1960
-
1961
- // src/document/setRangeText.ts
1962
- function prepareRangeTextInterceptor(element) {
1963
- prepareInterceptor(element, "setRangeText", function interceptorImpl(...realArgs) {
1964
- return {
1965
- realArgs,
1966
- then: () => {
1967
- setUIValueClean(element);
1968
- setUISelectionClean(element);
1969
- }
1970
- };
1971
- });
1972
- }
1973
-
1974
- // src/document/index.ts
1975
- var isPrepared = Symbol("Node prepared with document state workarounds");
1976
- function prepareDocument(document) {
1977
- if (document[isPrepared]) {
1978
- return;
1979
- }
1980
- document.addEventListener("focus", (e) => {
1981
- const el = e.target;
1982
- prepareElement(el);
1983
- }, {
1984
- capture: true,
1985
- passive: true
1986
- });
1987
- if (document.activeElement) {
1988
- prepareElement(document.activeElement);
1989
- }
1990
- document.addEventListener("blur", (e) => {
1991
- const el = e.target;
1992
- const initialValue = getInitialValue(el);
1993
- if (initialValue !== void 0) {
1994
- if (el.value !== initialValue) {
1995
- dispatchUIEvent({}, el, "change");
1996
- }
1997
- clearInitialValue(el);
1998
- }
1999
- }, {
2000
- capture: true,
2001
- passive: true
2002
- });
2003
- document[isPrepared] = isPrepared;
2004
- }
2005
- function prepareElement(el) {
2006
- if (el[isPrepared]) {
2007
- return;
2008
- }
2009
- if (isElementType(el, ["input", "textarea"])) {
2010
- prepareValueInterceptor(el);
2011
- prepareSelectionInterceptor(el);
2012
- prepareRangeTextInterceptor(el);
2013
- }
2014
- el[isPrepared] = isPrepared;
2015
- }
2016
-
2017
- // src/keyboard/modifiers.ts
2018
- var modifierKeys = [
2019
- "Alt",
2020
- "AltGraph",
2021
- "Control",
2022
- "Fn",
2023
- "Meta",
2024
- "Shift",
2025
- "Symbol"
2026
- ];
2027
- function isModifierKey(key) {
2028
- return modifierKeys.includes(key);
2029
- }
2030
- var modifierLocks = [
2031
- "CapsLock",
2032
- "FnLock",
2033
- "NumLock",
2034
- "ScrollLock",
2035
- "SymbolLock"
2036
- ];
2037
- function isModifierLock(key) {
2038
- return modifierLocks.includes(key);
2039
- }
2040
- function preKeydownBehavior(config, { key }, element) {
2041
- var _a;
2042
- if (isModifierKey(key)) {
2043
- config.keyboardState.modifiers[key] = true;
2044
- if (key === "AltGraph") {
2045
- const ctrlKeyDef = (_a = config.keyboardMap.find((k) => k.key === "Control")) != null ? _a : { key: "Control", code: "Control" };
2046
- dispatchUIEvent(config, element, "keydown", getKeyEventProps(ctrlKeyDef));
2047
- }
2048
- } else if (isModifierLock(key)) {
2049
- config.keyboardState.modifierPhase[key] = config.keyboardState.modifiers[key];
2050
- if (!config.keyboardState.modifierPhase[key]) {
2051
- config.keyboardState.modifiers[key] = true;
2052
- }
2053
- }
2054
- }
2055
- function preKeyupBehavior(config, { key }) {
2056
- if (isModifierKey(key)) {
2057
- config.keyboardState.modifiers[key] = false;
2058
- } else if (isModifierLock(key)) {
2059
- if (config.keyboardState.modifierPhase[key]) {
2060
- config.keyboardState.modifiers[key] = false;
2061
- }
2062
- }
2063
- }
2064
- function postKeyupBehavior(config, { key }, element) {
2065
- var _a;
2066
- if (key === "AltGraph") {
2067
- const ctrlKeyDef = (_a = config.keyboardMap.find((k) => k.key === "Control")) != null ? _a : { key: "Control", code: "Control" };
2068
- dispatchUIEvent(config, element, "keyup", getKeyEventProps(ctrlKeyDef));
2069
- }
2070
- }
2071
-
2072
- // src/keyboard/keyboardAction.ts
2073
- async function keyboardAction(config, actions) {
2074
- for (let i = 0; i < actions.length; i++) {
2075
- await keyboardKeyAction(config, actions[i]);
2076
- if (i < actions.length - 1) {
2077
- await wait(config);
2078
- }
2079
- }
2080
- }
2081
- async function keyboardKeyAction(config, { keyDef, releasePrevious, releaseSelf, repeat }) {
2082
- const { document, keyboardState } = config;
2083
- const getCurrentElement = () => getActive(document);
2084
- const pressed = keyboardState.pressed.find((p) => p.keyDef === keyDef);
2085
- if (pressed) {
2086
- await keyup(keyDef, getCurrentElement, config, pressed.unpreventedDefault);
2087
- }
2088
- if (!releasePrevious) {
2089
- let unpreventedDefault = true;
2090
- for (let i = 1; i <= repeat; i++) {
2091
- unpreventedDefault = await keydown(keyDef, getCurrentElement, config);
2092
- if (unpreventedDefault && hasKeyPress(keyDef, config)) {
2093
- await keypress(keyDef, getCurrentElement, config);
2094
- }
2095
- if (i < repeat) {
2096
- await wait(config);
2097
- }
2098
- }
2099
- if (releaseSelf) {
2100
- await keyup(keyDef, getCurrentElement, config, unpreventedDefault);
2101
- }
2102
- }
2103
- }
2104
- function getActive(document) {
2105
- var _a;
2106
- return (_a = getActiveElement(document)) != null ? _a : document.body;
2107
- }
2108
- async function releaseAllKeys(config) {
2109
- const getCurrentElement = () => getActive(config.document);
2110
- for (const k of config.keyboardState.pressed) {
2111
- await keyup(k.keyDef, getCurrentElement, config, k.unpreventedDefault);
2112
- }
2113
- }
2114
- async function keydown(keyDef, getCurrentElement, config) {
2115
- const element = getCurrentElement();
2116
- if (element !== config.keyboardState.activeElement) {
2117
- config.keyboardState.carryValue = void 0;
2118
- config.keyboardState.carryChar = "";
2119
- }
2120
- config.keyboardState.activeElement = element;
2121
- preKeydownBehavior(config, keyDef, element);
2122
- const unpreventedDefault = dispatchUIEvent(config, element, "keydown", getKeyEventProps(keyDef));
2123
- config.keyboardState.pressed.push({ keyDef, unpreventedDefault });
2124
- return unpreventedDefault;
2125
- }
2126
- async function keypress(keyDef, getCurrentElement, config) {
2127
- const element = getCurrentElement();
2128
- dispatchUIEvent(config, element, "keypress", {
2129
- ...getKeyEventProps(keyDef),
2130
- charCode: keyDef.key === "Enter" ? 13 : String(keyDef.key).charCodeAt(0)
2131
- });
2132
- }
2133
- async function keyup(keyDef, getCurrentElement, config, unprevented) {
2134
- const element = getCurrentElement();
2135
- preKeyupBehavior(config, keyDef);
2136
- dispatchUIEvent(config, element, "keyup", getKeyEventProps(keyDef), !unprevented);
2137
- config.keyboardState.pressed = config.keyboardState.pressed.filter((k) => k.keyDef !== keyDef);
2138
- postKeyupBehavior(config, keyDef, element);
2139
- }
2140
- function hasKeyPress(keyDef, config) {
2141
- var _a;
2142
- return (((_a = keyDef.key) == null ? void 0 : _a.length) === 1 || keyDef.key === "Enter") && !config.keyboardState.modifiers.Control && !config.keyboardState.modifiers.Alt;
2143
- }
2144
-
2145
- // src/keyboard/parseKeyDef.ts
2146
- function parseKeyDef(keyboardMap, text) {
2147
- var _a;
2148
- const defs = [];
2149
- do {
2150
- const {
2151
- type: type3,
2152
- descriptor,
2153
- consumedLength,
2154
- releasePrevious,
2155
- releaseSelf = true,
2156
- repeat
2157
- } = readNextDescriptor(text, "keyboard");
2158
- const keyDef = (_a = keyboardMap.find((def) => {
2159
- var _a2, _b;
2160
- if (type3 === "[") {
2161
- return ((_a2 = def.code) == null ? void 0 : _a2.toLowerCase()) === descriptor.toLowerCase();
2162
- } else if (type3 === "{") {
2163
- return ((_b = def.key) == null ? void 0 : _b.toLowerCase()) === descriptor.toLowerCase();
2164
- }
2165
- return def.key === descriptor;
2166
- })) != null ? _a : {
2167
- key: "Unknown",
2168
- code: "Unknown",
2169
- [type3 === "[" ? "code" : "key"]: descriptor
2170
- };
2171
- defs.push({ keyDef, releasePrevious, releaseSelf, repeat });
2172
- text = text.slice(consumedLength);
2173
- } while (text);
2174
- return defs;
2175
- }
2176
-
2177
- // src/keyboard/index.ts
2178
- async function keyboard(text) {
2179
- const actions = parseKeyDef(this[Config].keyboardMap, text);
2180
- return keyboardAction(this[Config], actions);
2181
- }
2182
- function createKeyboardState() {
2183
- return {
2184
- activeElement: null,
2185
- pressed: [],
2186
- carryChar: "",
2187
- modifiers: {
2188
- Alt: false,
2189
- AltGraph: false,
2190
- Control: false,
2191
- CapsLock: false,
2192
- Fn: false,
2193
- FnLock: false,
2194
- Meta: false,
2195
- NumLock: false,
2196
- ScrollLock: false,
2197
- Shift: false,
2198
- Symbol: false,
2199
- SymbolLock: false
2200
- },
2201
- modifierPhase: {}
2202
- };
2203
- }
2204
-
2205
- // src/pointer/parseKeyDef.ts
2206
- function parseKeyDef2(pointerMap, keys) {
2207
- const defs = [];
2208
- do {
2209
- const {
2210
- descriptor,
2211
- consumedLength,
2212
- releasePrevious,
2213
- releaseSelf = true
2214
- } = readNextDescriptor(keys, "pointer");
2215
- const keyDef = pointerMap.find((p) => p.name === descriptor);
2216
- if (keyDef) {
2217
- defs.push({ keyDef, releasePrevious, releaseSelf });
2218
- }
2219
- keys = keys.slice(consumedLength);
2220
- } while (keys);
2221
- return defs;
2222
- }
2223
-
2224
- // src/pointer/firePointerEvents.ts
2225
- function firePointerEvent(config, target, type3, {
2226
- pointerType,
2227
- button,
2228
- coords,
2229
- pointerId,
2230
- isPrimary,
2231
- clickCount
2232
- }) {
2233
- const init = {
2234
- ...coords
2235
- };
2236
- if (type3 === "click" || type3.startsWith("pointer")) {
2237
- init.pointerId = pointerId;
2238
- init.pointerType = pointerType;
2239
- }
2240
- if (["pointerdown", "pointerup"].includes(type3)) {
2241
- init.isPrimary = isPrimary;
2242
- }
2243
- init.button = getMouseButton(button != null ? button : 0);
2244
- init.buttons = getMouseButtons(...config.pointerState.pressed.filter((p) => p.keyDef.pointerType === pointerType).map((p) => {
2245
- var _a;
2246
- return (_a = p.keyDef.button) != null ? _a : 0;
2247
- }));
2248
- if (["mousedown", "mouseup", "click", "dblclick", "contextmenu"].includes(type3)) {
2249
- init.detail = clickCount;
2250
- }
2251
- return dispatchUIEvent(config, target, type3, init);
2252
- }
2253
-
2254
- // src/pointer/resolveSelectionTarget.ts
2255
- function resolveSelectionTarget({
2256
- target,
2257
- node,
2258
- offset
2259
- }) {
2260
- if (isElementType(target, ["input", "textarea"])) {
2261
- return {
2262
- node: target,
2263
- offset: offset != null ? offset : getUIValue(target).length
2264
- };
2265
- } else if (node) {
2266
- return {
2267
- node,
2268
- offset: offset != null ? offset : node.nodeType === 3 ? node.nodeValue.length : node.childNodes.length
2269
- };
2270
- }
2271
- return findNodeAtTextOffset(target, offset);
2272
- }
2273
- function findNodeAtTextOffset(node, offset, isRoot = true) {
2274
- let i = offset === void 0 ? node.childNodes.length - 1 : 0;
2275
- const step = offset === void 0 ? -1 : 1;
2276
- while (offset === void 0 ? i >= (isRoot ? Math.max(node.childNodes.length - 1, 0) : 0) : i <= node.childNodes.length) {
2277
- const c = node.childNodes.item(i);
2278
- const text = String(c.textContent);
2279
- if (text.length) {
2280
- if (offset !== void 0 && text.length < offset) {
2281
- offset -= text.length;
2282
- } else if (c.nodeType === 1) {
2283
- return findNodeAtTextOffset(c, offset, false);
2284
- } else {
2285
- if (c.nodeType === 3) {
2286
- return {
2287
- node: c,
2288
- offset: offset != null ? offset : c.nodeValue.length
2289
- };
2290
- }
2291
- }
2292
- }
2293
- i += step;
2294
- }
2295
- return { node, offset: node.childNodes.length };
2296
- }
2297
-
2298
- // src/pointer/pointerMove.ts
2299
- async function pointerMove(config, { pointerName = "mouse", target, coords, node, offset }) {
2300
- const { pointerState } = config;
2301
- if (!(pointerName in pointerState.position)) {
2302
- throw new Error(`Trying to move pointer "${pointerName}" which does not exist.`);
2303
- }
2304
- const {
2305
- pointerId,
2306
- pointerType,
2307
- target: prevTarget,
2308
- coords: prevCoords,
2309
- selectionRange
2310
- } = pointerState.position[pointerName];
2311
- if (prevTarget && prevTarget !== target) {
2312
- setLevelRef(config, 2 /* Trigger */);
2313
- if (hasPointerEvents(config, prevTarget)) {
2314
- fireMove(prevTarget, prevCoords);
2315
- if (!isDescendantOrSelf(target, prevTarget)) {
2316
- fireLeave(prevTarget, prevCoords);
2317
- }
2318
- }
2319
- }
2320
- setLevelRef(config, 2 /* Trigger */);
2321
- assertPointerEvents(config, target);
2322
- pointerState.position[pointerName] = {
2323
- ...pointerState.position[pointerName],
2324
- target,
2325
- coords
2326
- };
2327
- if (prevTarget !== target) {
2328
- if (!prevTarget || !isDescendantOrSelf(prevTarget, target)) {
2329
- fireEnter(target, coords);
2330
- }
2331
- }
2332
- fireMove(target, coords);
2333
- if (selectionRange) {
2334
- const selectionFocus = resolveSelectionTarget({ target, node, offset });
2335
- if ("node" in selectionRange) {
2336
- if (selectionFocus.node === selectionRange.node) {
2337
- const anchorOffset = selectionFocus.offset < selectionRange.start ? selectionRange.end : selectionRange.start;
2338
- const focusOffset = selectionFocus.offset > selectionRange.end || selectionFocus.offset < selectionRange.start ? selectionFocus.offset : selectionRange.end;
2339
- setUISelection(selectionRange.node, { anchorOffset, focusOffset });
2340
- }
2341
- } else {
2342
- const range = selectionRange.cloneRange();
2343
- const cmp = range.comparePoint(selectionFocus.node, selectionFocus.offset);
2344
- if (cmp < 0) {
2345
- range.setStart(selectionFocus.node, selectionFocus.offset);
2346
- } else if (cmp > 0) {
2347
- range.setEnd(selectionFocus.node, selectionFocus.offset);
2348
- }
2349
- const selection = target.ownerDocument.getSelection();
2350
- selection.removeAllRanges();
2351
- selection.addRange(range.cloneRange());
2352
- }
2353
- }
2354
- function fireMove(eventTarget, eventCoords) {
2355
- fire(eventTarget, "pointermove", eventCoords);
2356
- if (pointerType === "mouse" && !isDisabled(eventTarget)) {
2357
- fire(eventTarget, "mousemove", eventCoords);
2358
- }
2359
- }
2360
- function fireLeave(eventTarget, eventCoords) {
2361
- fire(eventTarget, "pointerout", eventCoords);
2362
- fire(eventTarget, "pointerleave", eventCoords);
2363
- if (pointerType === "mouse" && !isDisabled(eventTarget)) {
2364
- fire(eventTarget, "mouseout", eventCoords);
2365
- fire(eventTarget, "mouseleave", eventCoords);
2366
- }
2367
- }
2368
- function fireEnter(eventTarget, eventCoords) {
2369
- fire(eventTarget, "pointerover", eventCoords);
2370
- fire(eventTarget, "pointerenter", eventCoords);
2371
- if (pointerType === "mouse" && !isDisabled(eventTarget)) {
2372
- fire(eventTarget, "mouseover", eventCoords);
2373
- fire(eventTarget, "mouseenter", eventCoords);
2374
- }
2375
- }
2376
- function fire(eventTarget, type3, eventCoords) {
2377
- return firePointerEvent(config, eventTarget, type3, {
2378
- coords: eventCoords,
2379
- pointerId,
2380
- pointerType
2381
- });
2382
- }
2383
- }
2384
-
2385
- // src/pointer/pointerPress.ts
2386
- async function pointerPress(config, action) {
2387
- const { keyDef, target, releasePrevious, releaseSelf } = action;
2388
- const previous = config.pointerState.pressed.find((p) => p.keyDef === keyDef);
2389
- const pointerName = keyDef.pointerType === "touch" ? keyDef.name : keyDef.pointerType;
2390
- const targetIsDisabled = isDisabled(target);
2391
- if (previous) {
2392
- up(config, pointerName, action, previous, targetIsDisabled);
2393
- }
2394
- if (!releasePrevious) {
2395
- const press = down(config, pointerName, action, targetIsDisabled);
2396
- if (releaseSelf) {
2397
- up(config, pointerName, action, press, targetIsDisabled);
2398
- }
2399
- }
2400
- }
2401
- function getNextPointerId(state) {
2402
- state.pointerId = state.pointerId + 1;
2403
- return state.pointerId;
2404
- }
2405
- function down(config, pointerName, { keyDef, node, offset, target, coords }, targetIsDisabled) {
2406
- var _a, _b, _c;
2407
- setLevelRef(config, 2 /* Trigger */);
2408
- assertPointerEvents(config, target);
2409
- const { pointerState } = config;
2410
- const { name, pointerType, button } = keyDef;
2411
- const pointerId = pointerType === "mouse" ? 1 : getNextPointerId(pointerState);
2412
- pointerState.position[pointerName] = {
2413
- ...pointerState.position[pointerName],
2414
- pointerId,
2415
- pointerType,
2416
- target,
2417
- coords
2418
- };
2419
- let isMultiTouch = false;
2420
- let isPrimary = true;
2421
- if (pointerType !== "mouse") {
2422
- for (const obj of pointerState.pressed) {
2423
- if (obj.keyDef.pointerType === pointerType) {
2424
- obj.isMultiTouch = true;
2425
- isMultiTouch = true;
2426
- isPrimary = false;
2427
- }
2428
- }
2429
- }
2430
- if (((_a = pointerState.activeClickCount) == null ? void 0 : _a[0]) !== name) {
2431
- delete pointerState.activeClickCount;
2432
- }
2433
- const clickCount = Number((_c = (_b = pointerState.activeClickCount) == null ? void 0 : _b[1]) != null ? _c : 0) + 1;
2434
- pointerState.activeClickCount = [name, clickCount];
2435
- const pressObj = {
2436
- keyDef,
2437
- downTarget: target,
2438
- pointerId,
2439
- unpreventedDefault: true,
2440
- isMultiTouch,
2441
- isPrimary,
2442
- clickCount
2443
- };
2444
- pointerState.pressed.push(pressObj);
2445
- if (pointerType !== "mouse") {
2446
- fire("pointerover");
2447
- fire("pointerenter");
2448
- }
2449
- if (pointerType !== "mouse" || !pointerState.pressed.some((p) => p.keyDef !== keyDef && p.keyDef.pointerType === pointerType)) {
2450
- fire("pointerdown");
2451
- }
2452
- if (pointerType === "mouse") {
2453
- if (!targetIsDisabled) {
2454
- pressObj.unpreventedDefault = fire("mousedown");
2455
- }
2456
- if (pressObj.unpreventedDefault) {
2457
- mousedownDefaultBehavior({
2458
- target,
2459
- targetIsDisabled,
2460
- clickCount,
2461
- position: pointerState.position[pointerName],
2462
- node,
2463
- offset
2464
- });
2465
- }
2466
- if (button === "secondary") {
2467
- fire("contextmenu");
2468
- }
2469
- }
2470
- return pressObj;
2471
- function fire(type3) {
2472
- return firePointerEvent(config, target, type3, {
2473
- button,
2474
- clickCount,
2475
- coords,
2476
- isPrimary,
2477
- pointerId,
2478
- pointerType
2479
- });
2480
- }
2481
- }
2482
- function up(config, pointerName, {
2483
- keyDef: { pointerType, button },
2484
- target,
2485
- coords,
2486
- node,
2487
- offset
2488
- }, pressed, targetIsDisabled) {
2489
- setLevelRef(config, 2 /* Trigger */);
2490
- assertPointerEvents(config, target);
2491
- const { pointerState } = config;
2492
- pointerState.pressed = pointerState.pressed.filter((p) => p !== pressed);
2493
- const { isMultiTouch, isPrimary, pointerId, clickCount } = pressed;
2494
- let { unpreventedDefault } = pressed;
2495
- pointerState.position[pointerName] = {
2496
- ...pointerState.position[pointerName],
2497
- target,
2498
- coords
2499
- };
2500
- if (pointerType !== "mouse" || !pointerState.pressed.filter((p) => p.keyDef.pointerType === pointerType).length) {
2501
- fire("pointerup");
2502
- }
2503
- if (pointerType !== "mouse") {
2504
- fire("pointerout");
2505
- fire("pointerleave");
2506
- }
2507
- if (pointerType !== "mouse" && !isMultiTouch) {
2508
- if (!targetIsDisabled) {
2509
- if (clickCount === 1) {
2510
- fire("mouseover");
2511
- fire("mouseenter");
2512
- }
2513
- fire("mousemove");
2514
- unpreventedDefault = fire("mousedown") && unpreventedDefault;
2515
- }
2516
- if (unpreventedDefault) {
2517
- mousedownDefaultBehavior({
2518
- target,
2519
- targetIsDisabled,
2520
- clickCount,
2521
- position: pointerState.position[pointerName],
2522
- node,
2523
- offset
2524
- });
2525
- }
2526
- }
2527
- delete pointerState.position[pointerName].selectionRange;
2528
- if (!targetIsDisabled) {
2529
- if (pointerType === "mouse" || !isMultiTouch) {
2530
- unpreventedDefault = fire("mouseup") && unpreventedDefault;
2531
- const canClick = pointerType !== "mouse" || button === "primary";
2532
- if (canClick && target === pressed.downTarget) {
2533
- fire("click");
2534
- if (clickCount === 2) {
2535
- fire("dblclick");
2536
- }
2537
- }
2538
- }
2539
- }
2540
- function fire(type3) {
2541
- return firePointerEvent(config, target, type3, {
2542
- button,
2543
- clickCount,
2544
- coords,
2545
- isPrimary,
2546
- pointerId,
2547
- pointerType
2548
- });
2549
- }
2550
- }
2551
- function mousedownDefaultBehavior({
2552
- position,
2553
- target,
2554
- targetIsDisabled,
2555
- clickCount,
2556
- node,
2557
- offset
2558
- }) {
2559
- if (!targetIsDisabled) {
2560
- const hasValue = isElementType(target, ["input", "textarea"]);
2561
- const text = String(hasValue ? getUIValue(target) : target.textContent);
2562
- const [start, end] = node ? [offset, offset] : getTextRange(text, offset, clickCount);
2563
- if (hasValue) {
2564
- setUISelection(target, {
2565
- anchorOffset: start != null ? start : text.length,
2566
- focusOffset: end != null ? end : text.length
2567
- });
2568
- position.selectionRange = {
2569
- node: target,
2570
- start: start != null ? start : 0,
2571
- end: end != null ? end : text.length
2572
- };
2573
- } else {
2574
- const { node: startNode, offset: startOffset } = resolveSelectionTarget({
2575
- target,
2576
- node,
2577
- offset: start
2578
- });
2579
- const { node: endNode, offset: endOffset } = resolveSelectionTarget({
2580
- target,
2581
- node,
2582
- offset: end
2583
- });
2584
- const range = target.ownerDocument.createRange();
2585
- range.setStart(startNode, startOffset);
2586
- range.setEnd(endNode, endOffset);
2587
- position.selectionRange = range;
2588
- const selection = target.ownerDocument.getSelection();
2589
- selection.removeAllRanges();
2590
- selection.addRange(range.cloneRange());
2591
- }
2592
- }
2593
- focus(target);
2594
- }
2595
- function getTextRange(text, pos, clickCount) {
2596
- if (clickCount % 3 === 1 || text.length === 0) {
2597
- return [pos, pos];
2598
- }
2599
- const textPos = pos != null ? pos : text.length;
2600
- if (clickCount % 3 === 2) {
2601
- return [
2602
- textPos - text.substr(0, pos).match(/(\w+|\s+|\W)?$/)[0].length,
2603
- pos === void 0 ? pos : pos + text.substr(pos).match(/^(\w+|\s+|\W)?/)[0].length
2604
- ];
2605
- }
2606
- return [
2607
- textPos - text.substr(0, pos).match(/[^\r\n]*$/)[0].length,
2608
- pos === void 0 ? pos : pos + text.substr(pos).match(/^[^\r\n]*/)[0].length
2609
- ];
2610
- }
2611
-
2612
- // src/pointer/pointerAction.ts
2613
- async function pointerAction(config, actions) {
2614
- var _a, _b;
2615
- for (let i = 0; i < actions.length; i++) {
2616
- const action = actions[i];
2617
- const pointerName = "pointerName" in action && action.pointerName ? action.pointerName : "keyDef" in action ? action.keyDef.pointerType === "touch" ? action.keyDef.name : action.keyDef.pointerType : "mouse";
2618
- const target = (_a = action.target) != null ? _a : getPrevTarget(pointerName, config.pointerState);
2619
- const coords = (_b = action.coords) != null ? _b : pointerName in config.pointerState.position ? config.pointerState.position[pointerName].coords : void 0;
2620
- await ("keyDef" in action ? pointerPress(config, { ...action, target, coords }) : pointerMove(config, { ...action, target, coords }));
2621
- if (i < actions.length - 1) {
2622
- await wait(config);
2623
- }
2624
- }
2625
- delete config.pointerState.activeClickCount;
2626
- }
2627
- function getPrevTarget(pointerName, state) {
2628
- if (!(pointerName in state.position) || !state.position[pointerName].target) {
2629
- throw new Error("This pointer has no previous position. Provide a target property!");
2630
- }
2631
- return state.position[pointerName].target;
2632
- }
2633
-
2634
- // src/pointer/index.ts
2635
- async function pointer(input2) {
2636
- const { pointerMap } = this[Config];
2637
- const actions = [];
2638
- (Array.isArray(input2) ? input2 : [input2]).forEach((actionInput) => {
2639
- if (typeof actionInput === "string") {
2640
- actions.push(...parseKeyDef2(pointerMap, actionInput));
2641
- } else if ("keys" in actionInput) {
2642
- actions.push(...parseKeyDef2(pointerMap, actionInput.keys).map((i) => ({
2643
- ...actionInput,
2644
- ...i
2645
- })));
2646
- } else {
2647
- actions.push(actionInput);
2648
- }
2649
- });
2650
- return pointerAction(this[Config], actions).then(() => void 0);
2651
- }
2652
- function createPointerState(document) {
2653
- return {
2654
- pointerId: 1,
2655
- position: {
2656
- mouse: {
2657
- pointerType: "mouse",
2658
- pointerId: 1,
2659
- target: document.body,
2660
- coords: {
2661
- clientX: 0,
2662
- clientY: 0,
2663
- offsetX: 0,
2664
- offsetY: 0,
2665
- pageX: 0,
2666
- pageY: 0,
2667
- x: 0,
2668
- y: 0
2669
- }
2670
- }
2671
- },
2672
- pressed: []
2673
- };
2674
- }
2675
-
2676
- // src/setup/config.ts
2677
- var Config = Symbol("Config");
2678
-
2679
- // src/setup/api.ts
2680
- var api_exports = {};
2681
- __export(api_exports, {
2682
- clear: () => clear,
2683
- click: () => click,
2684
- copy: () => copy,
2685
- cut: () => cut,
2686
- dblClick: () => dblClick,
2687
- deselectOptions: () => deselectOptions,
2688
- hover: () => hover,
2689
- keyboard: () => keyboard,
2690
- paste: () => paste,
2691
- pointer: () => pointer,
2692
- selectOptions: () => selectOptions,
2693
- tab: () => tab,
2694
- tripleClick: () => tripleClick,
2695
- type: () => type,
2696
- unhover: () => unhover,
2697
- upload: () => upload
2698
- });
2699
-
2700
- // src/convenience/click.ts
2701
- async function click(element) {
2702
- const pointerIn = [];
2703
- if (!this[Config].skipHover) {
2704
- pointerIn.push({ target: element });
2705
- }
2706
- pointerIn.push({ keys: "[MouseLeft]", target: element });
2707
- return this.pointer(pointerIn);
2708
- }
2709
- async function dblClick(element) {
2710
- return this.pointer([{ target: element }, "[MouseLeft][MouseLeft]"]);
2711
- }
2712
- async function tripleClick(element) {
2713
- return this.pointer([{ target: element }, "[MouseLeft][MouseLeft][MouseLeft]"]);
2714
- }
2715
-
2716
- // src/convenience/hover.ts
2717
- async function hover(element) {
2718
- return this.pointer({ target: element });
2719
- }
2720
- async function unhover(element) {
2721
- assertPointerEvents(this[Config], this[Config].pointerState.position.mouse.target);
2722
- return this.pointer({ target: element.ownerDocument.body });
2723
- }
2724
-
2725
- // src/convenience/tab.ts
2726
- async function tab({
2727
- shift
2728
- } = {}) {
2729
- return this.keyboard(shift === true ? "{Shift>}{Tab}{/Shift}" : shift === false ? "[/ShiftLeft][/ShiftRight]{Tab}" : "{Tab}");
2730
- }
2731
-
2732
- // src/clipboard/copy.ts
2733
- async function copy() {
2734
- var _a;
2735
- const doc = this[Config].document;
2736
- const target = (_a = doc.activeElement) != null ? _a : doc.body;
2737
- const clipboardData = copySelection(target);
2738
- if (clipboardData.items.length === 0) {
2739
- return;
2740
- }
2741
- if (this.dispatchUIEvent(target, "copy", {
2742
- clipboardData
2743
- }) && this[Config].writeToClipboard) {
2744
- await writeDataTransferToClipboard(doc, clipboardData);
2745
- }
2746
- return clipboardData;
2747
- }
2748
-
2749
- // src/clipboard/cut.ts
2750
- async function cut() {
2751
- var _a;
2752
- const doc = this[Config].document;
2753
- const target = (_a = doc.activeElement) != null ? _a : doc.body;
2754
- const clipboardData = copySelection(target);
2755
- if (clipboardData.items.length === 0) {
2756
- return;
2757
- }
2758
- if (this.dispatchUIEvent(target, "cut", {
2759
- clipboardData
2760
- }) && this[Config].writeToClipboard) {
2761
- await writeDataTransferToClipboard(target.ownerDocument, clipboardData);
2762
- }
2763
- return clipboardData;
2764
- }
2765
-
2766
- // src/clipboard/paste.ts
2767
- async function paste(clipboardData) {
2768
- var _a, _b;
2769
- const doc = this[Config].document;
2770
- const target = (_a = doc.activeElement) != null ? _a : doc.body;
2771
- const dataTransfer = (_b = typeof clipboardData === "string" ? getClipboardDataFromString(doc, clipboardData) : clipboardData) != null ? _b : await readDataTransferFromClipboard(doc).catch(() => {
2772
- throw new Error("`userEvent.paste()` without `clipboardData` requires the `ClipboardAPI` to be available.");
2773
- });
2774
- this.dispatchUIEvent(target, "paste", {
2775
- clipboardData: dataTransfer
2776
- });
2777
- }
2778
- function getClipboardDataFromString(doc, text) {
2779
- const dt = createDataTransfer(getWindow(doc));
2780
- dt.setData("text", text);
2781
- return dt;
2782
- }
2783
-
2784
- // src/utility/clear.ts
2785
- async function clear(element) {
2786
- if (!isEditable(element) || isDisabled(element)) {
2787
- throw new Error("clear()` is only supported on editable elements.");
2788
- }
2789
- focus(element);
2790
- if (element.ownerDocument.activeElement !== element) {
2791
- throw new Error("The element to be cleared could not be focused.");
2792
- }
2793
- selectAll(element);
2794
- if (!isAllSelected(element)) {
2795
- throw new Error("The element content to be cleared could not be selected.");
2796
- }
2797
- input(this[Config], element, "", "deleteContentBackward");
2798
- }
2799
-
2800
- // src/utility/selectOptions.ts
2801
- var import_dom4 = require("@testing-library/dom");
2802
- async function selectOptions(select, values) {
2803
- return selectOptionsBase.call(this, true, select, values);
2804
- }
2805
- async function deselectOptions(select, values) {
2806
- return selectOptionsBase.call(this, false, select, values);
2807
- }
2808
- async function selectOptionsBase(newValue, select, values) {
2809
- if (!newValue && !select.multiple) {
2810
- throw (0, import_dom4.getConfig)().getElementError(`Unable to deselect an option in a non-multiple select. Use selectOptions to change the selection instead.`, select);
2811
- }
2812
- const valArray = Array.isArray(values) ? values : [values];
2813
- const allOptions = Array.from(select.querySelectorAll('option, [role="option"]'));
2814
- const selectedOptions = valArray.map((val) => {
2815
- if (typeof val !== "string" && allOptions.includes(val)) {
2816
- return val;
2817
- } else {
2818
- const matchingOption = allOptions.find((o) => o.value === val || o.innerHTML === val);
2819
- if (matchingOption) {
2820
- return matchingOption;
2821
- } else {
2822
- throw (0, import_dom4.getConfig)().getElementError(`Value "${String(val)}" not found in options`, select);
2823
- }
2824
- }
2825
- }).filter((option) => !isDisabled(option));
2826
- if (isDisabled(select) || !selectedOptions.length)
2827
- return;
2828
- const selectOption = (option) => {
2829
- option.selected = newValue;
2830
- this.dispatchUIEvent(select, "input", {
2831
- bubbles: true,
2832
- cancelable: false,
2833
- composed: true
2834
- });
2835
- this.dispatchUIEvent(select, "change");
2836
- };
2837
- if (isElementType(select, "select")) {
2838
- if (select.multiple) {
2839
- for (const option of selectedOptions) {
2840
- const withPointerEvents = this[Config].pointerEventsCheck === 0 ? true : hasPointerEvents(this[Config], option);
2841
- if (withPointerEvents) {
2842
- this.dispatchUIEvent(option, "pointerover");
2843
- this.dispatchUIEvent(select, "pointerenter");
2844
- this.dispatchUIEvent(option, "mouseover");
2845
- this.dispatchUIEvent(select, "mouseenter");
2846
- this.dispatchUIEvent(option, "pointermove");
2847
- this.dispatchUIEvent(option, "mousemove");
2848
- this.dispatchUIEvent(option, "pointerdown");
2849
- this.dispatchUIEvent(option, "mousedown");
2850
- }
2851
- focus(select);
2852
- if (withPointerEvents) {
2853
- this.dispatchUIEvent(option, "pointerup");
2854
- this.dispatchUIEvent(option, "mouseup");
2855
- }
2856
- selectOption(option);
2857
- if (withPointerEvents) {
2858
- this.dispatchUIEvent(option, "click");
2859
- }
2860
- await wait(this[Config]);
2861
- }
2862
- } else if (selectedOptions.length === 1) {
2863
- const withPointerEvents = this[Config].pointerEventsCheck === 0 ? true : hasPointerEvents(this[Config], select);
2864
- if (withPointerEvents) {
2865
- await this.click(select);
2866
- } else {
2867
- focus(select);
2868
- }
2869
- selectOption(selectedOptions[0]);
2870
- if (withPointerEvents) {
2871
- this.dispatchUIEvent(select, "pointerover");
2872
- this.dispatchUIEvent(select, "pointerenter");
2873
- this.dispatchUIEvent(select, "mouseover");
2874
- this.dispatchUIEvent(select, "mouseenter");
2875
- this.dispatchUIEvent(select, "pointerup");
2876
- this.dispatchUIEvent(select, "mouseup");
2877
- this.dispatchUIEvent(select, "click");
2878
- }
2879
- await wait(this[Config]);
2880
- } else {
2881
- throw (0, import_dom4.getConfig)().getElementError(`Cannot select multiple options on a non-multiple select`, select);
2882
- }
2883
- } else if (select.getAttribute("role") === "listbox") {
2884
- for (const option of selectedOptions) {
2885
- await this.click(option);
2886
- await this.unhover(option);
2887
- }
2888
- } else {
2889
- throw (0, import_dom4.getConfig)().getElementError(`Cannot select options on elements that are neither select nor listbox elements`, select);
2890
- }
2891
- }
2892
-
2893
- // src/utility/type.ts
2894
- async function type(element, text, {
2895
- skipClick = this[Config].skipClick,
2896
- skipAutoClose = this[Config].skipAutoClose,
2897
- initialSelectionStart,
2898
- initialSelectionEnd
2899
- } = {}) {
2900
- if (element.disabled)
2901
- return;
2902
- if (!skipClick) {
2903
- await this.click(element);
2904
- }
2905
- if (initialSelectionStart !== void 0) {
2906
- setSelectionRange(element, initialSelectionStart, initialSelectionEnd != null ? initialSelectionEnd : initialSelectionStart);
2907
- }
2908
- await this.keyboard(text);
2909
- if (!skipAutoClose) {
2910
- await releaseAllKeys(this[Config]);
2911
- }
2912
- }
2913
-
2914
- // src/utility/upload.ts
2915
- async function upload(element, fileOrFiles) {
2916
- const input2 = isElementType(element, "label") ? element.control : element;
2917
- if (!input2 || !isElementType(input2, "input", { type: "file" })) {
2918
- throw new TypeError(`The ${input2 === element ? "given" : "associated"} ${input2 == null ? void 0 : input2.tagName} element does not accept file uploads`);
2919
- }
2920
- if (isDisabled(element))
2921
- return;
2922
- const files = (Array.isArray(fileOrFiles) ? fileOrFiles : [fileOrFiles]).filter((file) => !this[Config].applyAccept || isAcceptableFile(file, input2.accept)).slice(0, input2.multiple ? void 0 : 1);
2923
- const fileDialog = () => {
2924
- var _a;
2925
- if (files.length === ((_a = input2.files) == null ? void 0 : _a.length) && files.every((f, i) => {
2926
- var _a2;
2927
- return f === ((_a2 = input2.files) == null ? void 0 : _a2.item(i));
2928
- })) {
2929
- return;
2930
- }
2931
- setFiles(input2, createFileList(getWindow(element), files));
2932
- this.dispatchUIEvent(input2, "input");
2933
- this.dispatchUIEvent(input2, "change");
2934
- };
2935
- input2.addEventListener("fileDialog", fileDialog);
2936
- await this.click(element);
2937
- input2.removeEventListener("fileDialog", fileDialog);
2938
- }
2939
- function isAcceptableFile(file, accept) {
2940
- if (!accept) {
2941
- return true;
2942
- }
2943
- const wildcards = ["audio/*", "image/*", "video/*"];
2944
- return accept.split(",").some((acceptToken) => {
2945
- if (acceptToken.startsWith(".")) {
2946
- return file.name.endsWith(acceptToken);
2947
- } else if (wildcards.includes(acceptToken)) {
2948
- return file.type.startsWith(acceptToken.substr(0, acceptToken.length - 1));
2949
- }
2950
- return file.type === acceptToken;
2951
- });
2952
- }
2953
-
2954
- // src/setup/wrapAsync.ts
2955
- var import_dom5 = require("@testing-library/dom");
2956
- function wrapAsync(implementation) {
2957
- return (0, import_dom5.getConfig)().asyncWrapper(implementation);
2958
- }
2959
-
2960
- // src/setup/setup.ts
2961
- function createConfig(options = {}, defaults = defaultOptionsSetup, node) {
2962
- const document = getDocument(options, node, defaults);
2963
- const {
2964
- keyboardState = createKeyboardState(),
2965
- pointerState = createPointerState(document)
2966
- } = options;
2967
- return {
2968
- ...defaults,
2969
- ...options,
2970
- document,
2971
- keyboardState,
2972
- pointerState
2973
- };
2974
- }
2975
- function setupMain(options = {}) {
2976
- var _a;
2977
- const config = createConfig(options);
2978
- prepareDocument(config.document);
2979
- const view = (_a = config.document.defaultView) != null ? _a : globalThis.window;
2980
- attachClipboardStubToView(view);
2981
- return doSetup(config);
2982
- }
2983
- function setupDirect(options = {}, node) {
2984
- const config = createConfig(options, defaultOptionsDirect, node);
2985
- prepareDocument(config.document);
2986
- return {
2987
- config,
2988
- api: doSetup(config)
2989
- };
2990
- }
2991
- function setupSub(options) {
2992
- return doSetup({
2993
- ...this[Config],
2994
- ...options
2995
- });
2996
- }
2997
- function wrapAndBindImpl(instance, impl) {
2998
- function method(...args) {
2999
- setLevelRef(instance[Config], 1 /* Call */);
3000
- return wrapAsync(() => impl.apply(instance, args).then(async (ret) => {
3001
- await wait(instance[Config]);
3002
- return ret;
3003
- }));
3004
- }
3005
- Object.defineProperty(method, "name", { get: () => impl.name });
3006
- return method;
3007
- }
3008
- function doSetup(config) {
3009
- const instance = {
3010
- [Config]: config,
3011
- dispatchUIEvent: bindDispatchUIEvent(config),
3012
- ...api_exports
3013
- };
3014
- return {
3015
- ...Object.fromEntries(Object.entries(api_exports).map(([name, api]) => [
3016
- name,
3017
- wrapAndBindImpl(instance, api)
3018
- ])),
3019
- setup: setupSub.bind(instance)
3020
- };
3021
- }
3022
- function getDocument(options, node, defaults) {
3023
- var _a, _b;
3024
- return (_b = (_a = options.document) != null ? _a : node && getDocumentFromNode(node)) != null ? _b : defaults.document;
3025
- }
3026
-
3027
- // src/setup/directApi.ts
3028
- var directApi_exports = {};
3029
- __export(directApi_exports, {
3030
- clear: () => clear2,
3031
- click: () => click2,
3032
- copy: () => copy2,
3033
- cut: () => cut2,
3034
- dblClick: () => dblClick2,
3035
- deselectOptions: () => deselectOptions2,
3036
- hover: () => hover2,
3037
- keyboard: () => keyboard2,
3038
- paste: () => paste2,
3039
- pointer: () => pointer2,
3040
- selectOptions: () => selectOptions2,
3041
- tab: () => tab2,
3042
- tripleClick: () => tripleClick2,
3043
- type: () => type2,
3044
- unhover: () => unhover2,
3045
- upload: () => upload2
3046
- });
3047
- function clear2(element) {
3048
- return setupDirect().api.clear(element);
3049
- }
3050
- function click2(element, options = {}) {
3051
- return setupDirect(options, element).api.click(element);
3052
- }
3053
- function copy2(options = {}) {
3054
- return setupDirect(options).api.copy();
3055
- }
3056
- function cut2(options = {}) {
3057
- return setupDirect(options).api.cut();
3058
- }
3059
- function dblClick2(element, options = {}) {
3060
- return setupDirect(options).api.dblClick(element);
3061
- }
3062
- function deselectOptions2(select, values, options = {}) {
3063
- return setupDirect(options).api.deselectOptions(select, values);
3064
- }
3065
- function hover2(element, options = {}) {
3066
- return setupDirect(options).api.hover(element);
3067
- }
3068
- async function keyboard2(text, options = {}) {
3069
- const { config, api } = setupDirect(options);
3070
- return api.keyboard(text).then(() => config.keyboardState);
3071
- }
3072
- async function pointer2(input2, options = {}) {
3073
- const { config, api } = setupDirect(options);
3074
- return api.pointer(input2).then(() => config.pointerState);
3075
- }
3076
- function paste2(clipboardData, options) {
3077
- return setupDirect(options).api.paste(clipboardData);
3078
- }
3079
- function selectOptions2(select, values, options = {}) {
3080
- return setupDirect(options).api.selectOptions(select, values);
3081
- }
3082
- function tripleClick2(element, options = {}) {
3083
- return setupDirect(options).api.tripleClick(element);
3084
- }
3085
- function type2(element, text, options = {}) {
3086
- return setupDirect(options, element).api.type(element, text, options);
3087
- }
3088
- function unhover2(element, options = {}) {
3089
- const { config, api } = setupDirect(options);
3090
- config.pointerState.position.mouse.target = element;
3091
- return api.unhover(element);
3092
- }
3093
- function upload2(element, fileOrFiles, options = {}) {
3094
- return setupDirect(options).api.upload(element, fileOrFiles);
3095
- }
3096
- function tab2(options = {}) {
3097
- return setupDirect().api.tab(options);
3098
- }
3099
-
3100
- // src/setup/index.ts
3101
- var userEvent = {
3102
- ...directApi_exports,
3103
- setup: setupMain
3104
- };