@testing-library/user-event 14.0.0-beta.7 → 14.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/README.md +6 -193
  2. package/dist/index.cjs +995 -1040
  3. package/dist/index.mjs +988 -1028
  4. package/dist/types/event/behavior/click.d.ts +1 -0
  5. package/dist/types/event/behavior/cut.d.ts +1 -0
  6. package/dist/types/event/behavior/index.d.ts +8 -0
  7. package/dist/types/event/behavior/keydown.d.ts +1 -0
  8. package/dist/types/event/behavior/keypress.d.ts +1 -0
  9. package/dist/types/event/behavior/keyup.d.ts +1 -0
  10. package/dist/types/event/behavior/paste.d.ts +1 -0
  11. package/dist/types/event/behavior/registry.d.ts +9 -0
  12. package/dist/types/event/createEvent.d.ts +8 -0
  13. package/dist/types/event/dispatchEvent.d.ts +2 -0
  14. package/dist/types/event/eventMap.d.ts +361 -0
  15. package/dist/types/event/eventTypes.d.ts +2 -0
  16. package/dist/types/event/index.d.ts +6 -0
  17. package/dist/types/event/types.d.ts +13 -0
  18. package/dist/types/event/wrapEvent.d.ts +1 -0
  19. package/dist/types/keyboard/modifiers.d.ts +9 -0
  20. package/dist/types/keyboard/types.d.ts +0 -5
  21. package/dist/types/pointer/firePointerEvents.d.ts +11 -0
  22. package/dist/types/pointer/types.d.ts +2 -1
  23. package/dist/types/setup/index.d.ts +2 -0
  24. package/dist/types/setup/setup.d.ts +1 -0
  25. package/dist/types/utility/clear.d.ts +1 -1
  26. package/dist/types/utils/edit/input.d.ts +2 -0
  27. package/dist/types/utils/edit/isValidDateOrTimeValue.d.ts +3 -0
  28. package/dist/types/utils/focus/focus.d.ts +3 -0
  29. package/dist/types/utils/focus/selection.d.ts +4 -0
  30. package/dist/types/utils/index.d.ts +2 -9
  31. package/dist/types/utils/keyDef/readNextDescriptor.d.ts +3 -1
  32. package/dist/types/utils/keyboard/getKeyEventProps.d.ts +2 -14
  33. package/dist/types/utils/misc/cloneEvent.d.ts +1 -0
  34. package/dist/types/utils/misc/findClosest.d.ts +1 -1
  35. package/package.json +10 -13
  36. package/CHANGELOG.md +0 -5
  37. package/dist/types/keyboard/plugins/arrow.d.ts +0 -6
  38. package/dist/types/keyboard/plugins/character.d.ts +0 -5
  39. package/dist/types/keyboard/plugins/combination.d.ts +0 -5
  40. package/dist/types/keyboard/plugins/control.d.ts +0 -6
  41. package/dist/types/keyboard/plugins/functional.d.ts +0 -8
  42. package/dist/types/keyboard/plugins/index.d.ts +0 -7
  43. package/dist/types/keyboard/plugins/modifiers.d.ts +0 -8
  44. package/dist/types/keyboard/shared/fireChangeForInputTimeIfValid.d.ts +0 -3
  45. package/dist/types/keyboard/shared/index.d.ts +0 -1
  46. package/dist/types/utils/edit/calculateNewValue.d.ts +0 -14
  47. package/dist/types/utils/edit/editInputElement.d.ts +0 -16
  48. package/dist/types/utils/edit/isValidDateValue.d.ts +0 -3
  49. package/dist/types/utils/edit/isValidInputTimeValue.d.ts +0 -3
  50. package/dist/types/utils/edit/prepareInput.d.ts +0 -7
  51. package/dist/types/utils/misc/hasFormSubmit.d.ts +0 -1
  52. package/dist/types/utils/pointer/firePointerEvents.d.ts +0 -25
package/dist/index.cjs CHANGED
@@ -19,24 +19,19 @@ var __spreadValues = (a, b) => {
19
19
  return a;
20
20
  };
21
21
  var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
22
- var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
23
22
  var __export = (target, all) => {
24
23
  for (var name in all)
25
24
  __defProp(target, name, { get: all[name], enumerable: true });
26
25
  };
27
- var __reExport = (target, module2, copyDefault, desc) => {
28
- if (module2 && typeof module2 === "object" || typeof module2 === "function") {
29
- for (let key of __getOwnPropNames(module2))
30
- if (!__hasOwnProp.call(target, key) && (copyDefault || key !== "default"))
31
- __defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable });
26
+ var __copyProps = (to, from, except, desc) => {
27
+ if (from && typeof from === "object" || typeof from === "function") {
28
+ for (let key of __getOwnPropNames(from))
29
+ if (!__hasOwnProp.call(to, key) && key !== except)
30
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
32
31
  }
33
- return target;
32
+ return to;
34
33
  };
35
- var __toCommonJS = /* @__PURE__ */ ((cache) => {
36
- return (module2, temp) => {
37
- return cache && cache.get(module2) || (temp = __reExport(__markAsModule({}), module2, 1), cache && cache.set(module2, temp), temp);
38
- };
39
- })(typeof WeakMap !== "undefined" ? /* @__PURE__ */ new WeakMap() : 0);
34
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
40
35
 
41
36
  // src/index.ts
42
37
  var src_exports = {};
@@ -44,187 +39,7 @@ __export(src_exports, {
44
39
  PointerEventsCheckLevel: () => PointerEventsCheckLevel,
45
40
  default: () => userEvent
46
41
  });
47
-
48
- // src/document/index.ts
49
- var import_dom = require("@testing-library/dom");
50
-
51
- // src/document/interceptor.ts
52
- var Interceptor = Symbol("Interceptor for programmatical calls");
53
- function prepareInterceptor(element, propName, interceptorImpl) {
54
- const prototypeDescriptor = Object.getOwnPropertyDescriptor(element.constructor.prototype, propName);
55
- const objectDescriptor = Object.getOwnPropertyDescriptor(element, propName);
56
- const target = (prototypeDescriptor == null ? void 0 : prototypeDescriptor.set) ? "set" : "value";
57
- if (typeof (prototypeDescriptor == null ? void 0 : prototypeDescriptor[target]) !== "function" || prototypeDescriptor[target][Interceptor]) {
58
- return;
59
- }
60
- function intercept(...args) {
61
- const { applyNative = true, realArgs } = interceptorImpl.call(this, ...args);
62
- const realFunc = (!applyNative && objectDescriptor || prototypeDescriptor)[target];
63
- if (target === "set") {
64
- realFunc.call(this, realArgs);
65
- } else {
66
- realFunc.call(this, ...realArgs);
67
- }
68
- }
69
- ;
70
- intercept[Interceptor] = Interceptor;
71
- Object.defineProperty(element, propName, __spreadProps(__spreadValues({}, objectDescriptor != null ? objectDescriptor : prototypeDescriptor), {
72
- [target]: intercept
73
- }));
74
- }
75
-
76
- // src/document/selection.ts
77
- var UISelection = Symbol("Displayed selection in UI");
78
- function prepareSelectionInterceptor(element) {
79
- prepareInterceptor(element, "setSelectionRange", function interceptorImpl(start, end, direction = "none") {
80
- const isUI = start && typeof start === "object" && start[UISelection];
81
- if (!isUI) {
82
- this[UISelection] = void 0;
83
- }
84
- return {
85
- realArgs: [Number(start), end, direction]
86
- };
87
- });
88
- prepareInterceptor(element, "selectionStart", function interceptorImpl(v) {
89
- this[UISelection] = void 0;
90
- return { realArgs: v };
91
- });
92
- prepareInterceptor(element, "selectionEnd", function interceptorImpl(v) {
93
- this[UISelection] = void 0;
94
- return { realArgs: v };
95
- });
96
- }
97
- function setUISelection(element, {
98
- focusOffset: focusOffsetParam,
99
- anchorOffset: anchorOffsetParam = focusOffsetParam
100
- }, mode = "replace") {
101
- const valueLength = getUIValue(element).length;
102
- const sanitizeOffset = (o) => Math.max(0, Math.min(valueLength, o));
103
- const anchorOffset = mode === "replace" || element[UISelection] === void 0 ? sanitizeOffset(anchorOffsetParam) : element[UISelection].anchorOffset;
104
- const focusOffset = sanitizeOffset(focusOffsetParam);
105
- const startOffset = Math.min(anchorOffset, focusOffset);
106
- const endOffset = Math.max(anchorOffset, focusOffset);
107
- element[UISelection] = {
108
- anchorOffset,
109
- focusOffset
110
- };
111
- if (element.selectionStart === startOffset && element.selectionEnd === endOffset) {
112
- return;
113
- }
114
- const startObj = new Number(startOffset);
115
- startObj[UISelection] = UISelection;
116
- try {
117
- element.setSelectionRange(startObj, endOffset);
118
- } catch {
119
- }
120
- }
121
- function getUISelection(element) {
122
- var _a, _b, _c;
123
- const sel = (_c = element[UISelection]) != null ? _c : {
124
- anchorOffset: (_a = element.selectionStart) != null ? _a : 0,
125
- focusOffset: (_b = element.selectionEnd) != null ? _b : 0
126
- };
127
- return __spreadProps(__spreadValues({}, sel), {
128
- startOffset: Math.min(sel.anchorOffset, sel.focusOffset),
129
- endOffset: Math.max(sel.anchorOffset, sel.focusOffset)
130
- });
131
- }
132
- function clearUISelection(element) {
133
- element[UISelection] = void 0;
134
- }
135
-
136
- // src/document/value.ts
137
- var UIValue = Symbol("Displayed value in UI");
138
- var InitialValue = Symbol("Initial value to compare on blur");
139
- var TrackChanges = Symbol("Track programmatic changes for React workaround");
140
- function valueInterceptor(v) {
141
- const isUI = typeof v === "object" && v[UIValue];
142
- this[UIValue] = isUI ? String(v) : void 0;
143
- if (!isUI) {
144
- trackValue(this, String(v));
145
- this[InitialValue] = String(v);
146
- clearUISelection(this);
147
- }
148
- return {
149
- applyNative: !!isUI,
150
- realArgs: String(v)
151
- };
152
- }
153
- function prepareValueInterceptor(element) {
154
- prepareInterceptor(element, "value", valueInterceptor);
155
- }
156
- function setUIValue(element, value) {
157
- element.value = {
158
- [UIValue]: UIValue,
159
- toString: () => value
160
- };
161
- }
162
- function getUIValue(element) {
163
- return element[UIValue] === void 0 ? element.value : String(element[UIValue]);
164
- }
165
- function setInitialValue(element) {
166
- element[InitialValue] = element.value;
167
- }
168
- function getInitialValue(element) {
169
- return element[InitialValue];
170
- }
171
- function startTrackValue(element) {
172
- element[TrackChanges] = [];
173
- }
174
- function trackValue(element, v) {
175
- var _a;
176
- (_a = element[TrackChanges]) == null ? void 0 : _a.push(v);
177
- }
178
- function endTrackValue(element) {
179
- const tracked = element[TrackChanges];
180
- element[TrackChanges] = void 0;
181
- return tracked;
182
- }
183
-
184
- // src/document/index.ts
185
- var isPrepared = Symbol("Node prepared with document state workarounds");
186
- function prepareDocument(document2) {
187
- if (document2[isPrepared]) {
188
- return;
189
- }
190
- document2.addEventListener("focus", (e) => {
191
- const el = e.target;
192
- prepareElement(el);
193
- }, {
194
- capture: true,
195
- passive: true
196
- });
197
- if (document2.activeElement) {
198
- prepareElement(document2.activeElement);
199
- }
200
- document2.addEventListener("blur", (e) => {
201
- const el = e.target;
202
- const initialValue = getInitialValue(el);
203
- if (typeof initialValue === "string" && el.value !== initialValue) {
204
- import_dom.fireEvent.change(el);
205
- }
206
- }, {
207
- capture: true,
208
- passive: true
209
- });
210
- document2[isPrepared] = isPrepared;
211
- }
212
- function prepareElement(el) {
213
- if ("value" in el) {
214
- setInitialValue(el);
215
- }
216
- if (el[isPrepared]) {
217
- return;
218
- }
219
- if ("value" in el) {
220
- prepareValueInterceptor(el);
221
- prepareSelectionInterceptor(el);
222
- }
223
- el[isPrepared] = isPrepared;
224
- }
225
-
226
- // src/keyboard/keyboardAction.ts
227
- var import_dom10 = require("@testing-library/dom");
42
+ module.exports = __toCommonJS(src_exports);
228
43
 
229
44
  // src/utils/misc/isElementType.ts
230
45
  function isElementType(element, tag, props) {
@@ -521,6 +336,29 @@ if (typeof afterAll === "function") {
521
336
  afterAll(() => detachClipboardStubFromView(window));
522
337
  }
523
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
+
524
362
  // src/utils/edit/buildTimeValue.ts
525
363
  function buildTimeValue(value) {
526
364
  const onlyDigitsValue = value.replace(/\D/g, "");
@@ -552,40 +390,153 @@ function build(onlyDigitsValue, index) {
552
390
  return `${validHours.toString().padStart(2, "0")}:${validMinutes.toString().padStart(2, "0")}`;
553
391
  }
554
392
 
555
- // src/utils/edit/isValidDateValue.ts
556
- function isValidDateValue(element, value) {
393
+ // src/utils/edit/isValidDateOrTimeValue.ts
394
+ function isValidDateOrTimeValue(element, value) {
557
395
  const clone = element.cloneNode();
558
396
  clone.value = value;
559
397
  return clone.value === value;
560
398
  }
561
399
 
562
- // src/utils/edit/isValidInputTimeValue.ts
563
- function isValidInputTimeValue(element, timeValue) {
564
- const clone = element.cloneNode();
565
- clone.value = timeValue;
566
- return clone.value === timeValue;
400
+ // src/utils/edit/maxLength.ts
401
+ var maxLengthSupportedTypes = /* @__PURE__ */ ((maxLengthSupportedTypes2) => {
402
+ maxLengthSupportedTypes2["email"] = "email";
403
+ maxLengthSupportedTypes2["password"] = "password";
404
+ maxLengthSupportedTypes2["search"] = "search";
405
+ maxLengthSupportedTypes2["telephone"] = "telephone";
406
+ maxLengthSupportedTypes2["text"] = "text";
407
+ maxLengthSupportedTypes2["url"] = "url";
408
+ return maxLengthSupportedTypes2;
409
+ })(maxLengthSupportedTypes || {});
410
+ function getSpaceUntilMaxLength(element) {
411
+ const value = getValue(element);
412
+ if (value === null) {
413
+ return void 0;
414
+ }
415
+ const maxLength = getSanitizedMaxLength(element);
416
+ return maxLength ? maxLength - value.length : void 0;
417
+ }
418
+ function getSanitizedMaxLength(element) {
419
+ var _a;
420
+ if (!supportsMaxLength(element)) {
421
+ return void 0;
422
+ }
423
+ const attr = (_a = element.getAttribute("maxlength")) != null ? _a : "";
424
+ return /^\d+$/.test(attr) && Number(attr) >= 0 ? Number(attr) : void 0;
425
+ }
426
+ function supportsMaxLength(element) {
427
+ return isElementType(element, "textarea") || isElementType(element, "input") && Boolean(maxLengthSupportedTypes[element.type]);
567
428
  }
568
429
 
569
- // src/utils/edit/calculateNewValue.ts
570
- function calculateNewValue(inputData, node, {
571
- startOffset,
572
- endOffset
573
- }, inputType) {
574
- const value = getUIValue(node);
575
- const prologEnd = Math.max(0, startOffset === endOffset && inputType === "deleteContentBackward" ? startOffset - 1 : startOffset);
576
- const prolog = value.substring(0, prologEnd);
577
- const epilogStart = Math.min(value.length, startOffset === endOffset && inputType === "deleteContentForward" ? startOffset + 1 : endOffset);
578
- const epilog = value.substring(epilogStart, value.length);
579
- let newValue = `${prolog}${inputData}${epilog}`;
580
- const newOffset = prologEnd + inputData.length;
581
- if (node.type === "date" && !isValidDateValue(node, newValue)) {
582
- newValue = value;
430
+ // src/utils/edit/input.ts
431
+ function isDateOrTime(element) {
432
+ return isElementType(element, "input") && ["date", "time"].includes(element.type);
433
+ }
434
+ function input(config, element, data, inputType = "insertText") {
435
+ const inputRange = getInputRange(element);
436
+ if (!inputRange) {
437
+ return;
438
+ }
439
+ if (!isDateOrTime(element)) {
440
+ const unprevented = dispatchUIEvent(config, element, "beforeinput", {
441
+ inputType,
442
+ data
443
+ });
444
+ if (!unprevented) {
445
+ return;
446
+ }
447
+ }
448
+ if ("startContainer" in inputRange) {
449
+ editContenteditable(config, element, inputRange, data, inputType);
450
+ } else {
451
+ editInputElement(config, element, inputRange, data, inputType);
452
+ }
453
+ }
454
+ function editContenteditable(config, element, inputRange, data, inputType) {
455
+ let del = false;
456
+ if (!inputRange.collapsed) {
457
+ del = true;
458
+ inputRange.deleteContents();
459
+ } else if (["deleteContentBackward", "deleteContentForward"].includes(inputType)) {
460
+ const nextPosition = getNextCursorPosition(inputRange.startContainer, inputRange.startOffset, inputType === "deleteContentBackward" ? -1 : 1, inputType);
461
+ if (nextPosition) {
462
+ del = true;
463
+ const delRange = inputRange.cloneRange();
464
+ if (delRange.comparePoint(nextPosition.node, nextPosition.offset) < 0) {
465
+ delRange.setStart(nextPosition.node, nextPosition.offset);
466
+ } else {
467
+ delRange.setEnd(nextPosition.node, nextPosition.offset);
468
+ }
469
+ delRange.deleteContents();
470
+ }
583
471
  }
584
- if (node.type === "time" && !isValidInputTimeValue(node, newValue)) {
585
- if (isValidInputTimeValue(node, inputData)) {
586
- newValue = inputData;
472
+ if (data) {
473
+ if (inputRange.endContainer.nodeType === 3) {
474
+ const offset = inputRange.endOffset;
475
+ inputRange.endContainer.insertData(offset, data);
476
+ inputRange.setStart(inputRange.endContainer, offset + data.length);
477
+ inputRange.setEnd(inputRange.endContainer, offset + data.length);
587
478
  } else {
588
- newValue = value;
479
+ const text = element.ownerDocument.createTextNode(data);
480
+ inputRange.insertNode(text);
481
+ inputRange.setStart(text, data.length);
482
+ inputRange.setEnd(text, data.length);
483
+ }
484
+ }
485
+ if (del || data) {
486
+ dispatchUIEvent(config, element, "input", { inputType });
487
+ }
488
+ }
489
+ function editInputElement(config, element, inputRange, data, inputType) {
490
+ let dataToInsert = data;
491
+ const spaceUntilMaxLength = getSpaceUntilMaxLength(element);
492
+ if (spaceUntilMaxLength !== void 0) {
493
+ if (spaceUntilMaxLength > 0) {
494
+ dataToInsert = data.substring(0, spaceUntilMaxLength);
495
+ } else {
496
+ return;
497
+ }
498
+ }
499
+ const { newValue, newOffset, oldValue } = calculateNewValue(dataToInsert, element, inputRange, inputType);
500
+ if (newValue === oldValue && newOffset === inputRange.startOffset && newOffset === inputRange.endOffset) {
501
+ return;
502
+ }
503
+ if (isElementType(element, "input", { type: "number" }) && !isValidNumberInput(newValue)) {
504
+ return;
505
+ }
506
+ setUIValue(element, newValue);
507
+ setSelection({
508
+ focusNode: element,
509
+ anchorOffset: newOffset,
510
+ focusOffset: newOffset
511
+ });
512
+ if (isDateOrTime(element)) {
513
+ if (isValidDateOrTimeValue(element, newValue)) {
514
+ commitInput(config, element, oldValue, newValue, newOffset, {});
515
+ dispatchUIEvent(config, element, "change");
516
+ }
517
+ } else {
518
+ commitInput(config, element, oldValue, newValue, newOffset, {
519
+ data,
520
+ inputType
521
+ });
522
+ }
523
+ }
524
+ function calculateNewValue(inputData, node, {
525
+ startOffset,
526
+ endOffset
527
+ }, inputType) {
528
+ const value = getUIValue(node);
529
+ const prologEnd = Math.max(0, startOffset === endOffset && inputType === "deleteContentBackward" ? startOffset - 1 : startOffset);
530
+ const prolog = value.substring(0, prologEnd);
531
+ const epilogStart = Math.min(value.length, startOffset === endOffset && inputType === "deleteContentForward" ? startOffset + 1 : endOffset);
532
+ const epilog = value.substring(epilogStart, value.length);
533
+ let newValue = `${prolog}${inputData}${epilog}`;
534
+ let newOffset = prologEnd + inputData.length;
535
+ if (isElementType(node, "input", { type: "time" })) {
536
+ const builtValue = buildTimeValue(newValue);
537
+ if (builtValue !== "" && isValidDateOrTimeValue(node, builtValue)) {
538
+ newValue = builtValue;
539
+ newOffset = builtValue.length;
589
540
  }
590
541
  }
591
542
  return {
@@ -594,20 +545,22 @@ function calculateNewValue(inputData, node, {
594
545
  newOffset
595
546
  };
596
547
  }
597
-
598
- // src/utils/edit/editInputElement.ts
599
- var import_dom2 = require("@testing-library/dom");
600
-
601
- // src/utils/edit/isContentEditable.ts
602
- function isContentEditable(element) {
603
- return element.hasAttribute("contenteditable") && (element.getAttribute("contenteditable") == "true" || element.getAttribute("contenteditable") == "");
604
- }
605
- function getContentEditable(node) {
606
- const element = getElement(node);
607
- return element && (element.closest('[contenteditable=""]') || element.closest('[contenteditable="true"]'));
548
+ function commitInput(config, element, oldValue, newValue, newOffset, inputInit) {
549
+ startTrackValue(element);
550
+ dispatchUIEvent(config, element, "input", inputInit);
551
+ const tracked = endTrackValue(element);
552
+ if ((tracked == null ? void 0 : tracked.length) === 2 && tracked[0] === oldValue && tracked[1] === newValue) {
553
+ setSelection({
554
+ focusNode: element,
555
+ anchorOffset: newOffset,
556
+ focusOffset: newOffset
557
+ });
558
+ }
608
559
  }
609
- function getElement(node) {
610
- return node.nodeType === 1 ? node : node.parentElement;
560
+ function isValidNumberInput(value) {
561
+ var _a, _b;
562
+ const valueParts = value.split("e", 2);
563
+ 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]));
611
564
  }
612
565
 
613
566
  // src/utils/edit/isEditable.ts
@@ -633,161 +586,124 @@ function isEditableInput(element) {
633
586
  return isElementType(element, "input", { readOnly: false }) && Boolean(editableInputTypes[element.type]);
634
587
  }
635
588
 
636
- // src/utils/focus/selection.ts
637
- function setSelectionRange(element, anchorOffset, focusOffset) {
589
+ // src/utils/edit/setFiles.ts
590
+ var fakeFiles = Symbol("files and value properties are mocked");
591
+ function setFiles(el, files) {
638
592
  var _a;
639
- if (hasOwnSelection(element)) {
640
- return setSelection({
641
- focusNode: element,
642
- anchorOffset,
643
- focusOffset
644
- });
645
- }
646
- if (isContentEditable(element) && ((_a = element.firstChild) == null ? void 0 : _a.nodeType) === 3) {
647
- return setSelection({
648
- focusNode: element.firstChild,
649
- anchorOffset,
650
- focusOffset
593
+ (_a = el[fakeFiles]) == null ? void 0 : _a.restore();
594
+ const objectDescriptors = Object.getOwnPropertyDescriptors(el);
595
+ const prototypeDescriptors = Object.getOwnPropertyDescriptors(Object.getPrototypeOf(el));
596
+ function restore() {
597
+ Object.defineProperties(el, {
598
+ files: __spreadValues(__spreadValues({}, prototypeDescriptors.files), objectDescriptors.files),
599
+ value: __spreadValues(__spreadValues({}, prototypeDescriptors.value), objectDescriptors.value),
600
+ type: __spreadValues(__spreadValues({}, prototypeDescriptors.type), objectDescriptors.type)
651
601
  });
652
602
  }
653
- throw new Error("Not implemented. The result of this interaction is unreliable.");
654
- }
655
- function hasOwnSelection(node) {
656
- return isElement(node) && (isElementType(node, "textarea") || isElementType(node, "input") && node.type in editableInputTypes);
657
- }
658
- function isElement(node) {
659
- return node.nodeType === 1;
660
- }
661
- function getTargetTypeAndSelection(node) {
662
- const element = getElement2(node);
663
- if (element && hasOwnSelection(element)) {
664
- return {
665
- type: "input",
666
- selection: getUISelection(element)
667
- };
668
- }
669
- const selection = element == null ? void 0 : element.ownerDocument.getSelection();
670
- const isCE = getContentEditable(node) && (selection == null ? void 0 : selection.anchorNode) && getContentEditable(selection.anchorNode);
671
- return {
672
- type: isCE ? "contenteditable" : "default",
673
- selection
674
- };
603
+ el[fakeFiles] = { restore };
604
+ Object.defineProperties(el, {
605
+ files: __spreadProps(__spreadValues(__spreadValues({}, prototypeDescriptors.files), objectDescriptors.files), {
606
+ get: () => files
607
+ }),
608
+ value: __spreadProps(__spreadValues(__spreadValues({}, prototypeDescriptors.value), objectDescriptors.value), {
609
+ get: () => files.length ? `C:\\fakepath\\${files[0].name}` : "",
610
+ set(v) {
611
+ var _a2;
612
+ if (v === "") {
613
+ restore();
614
+ } else {
615
+ (_a2 = objectDescriptors.value.set) == null ? void 0 : _a2.call(el, v);
616
+ }
617
+ }
618
+ }),
619
+ type: __spreadProps(__spreadValues(__spreadValues({}, prototypeDescriptors.type), objectDescriptors.type), {
620
+ set(v) {
621
+ if (v !== "file") {
622
+ restore();
623
+ el.type = v;
624
+ }
625
+ }
626
+ })
627
+ });
675
628
  }
676
- function getElement2(node) {
677
- return node.nodeType === 1 ? node : node.parentElement;
629
+
630
+ // src/utils/misc/eventWrapper.ts
631
+ var import_dom = require("@testing-library/dom");
632
+ function eventWrapper(cb) {
633
+ let result;
634
+ (0, import_dom.getConfig)().eventWrapper(() => {
635
+ result = cb();
636
+ });
637
+ return result;
678
638
  }
679
- function updateSelectionOnFocus(element) {
639
+
640
+ // src/utils/misc/isDisabled.ts
641
+ function isDisabled(element) {
680
642
  var _a;
681
- const selection = element.ownerDocument.getSelection();
682
- if (!(selection == null ? void 0 : selection.focusNode)) {
683
- return;
684
- }
685
- if (hasOwnSelection(element)) {
686
- const contenteditable = getContentEditable(selection.focusNode);
687
- if (contenteditable) {
688
- if (!selection.isCollapsed) {
689
- const focusNode = ((_a = contenteditable.firstChild) == null ? void 0 : _a.nodeType) === 3 ? contenteditable.firstChild : contenteditable;
690
- selection.setBaseAndExtent(focusNode, 0, focusNode, 0);
643
+ for (let el = element; el; el = el.parentElement) {
644
+ if (isElementType(el, [
645
+ "button",
646
+ "input",
647
+ "select",
648
+ "textarea",
649
+ "optgroup",
650
+ "option"
651
+ ])) {
652
+ if (el.hasAttribute("disabled")) {
653
+ return true;
654
+ }
655
+ } else if (isElementType(el, "fieldset")) {
656
+ if (el.hasAttribute("disabled") && !((_a = el.querySelector(":scope > legend")) == null ? void 0 : _a.contains(element))) {
657
+ return true;
658
+ }
659
+ } else if (el.tagName.includes("-")) {
660
+ if (el.constructor.formAssociated && el.hasAttribute("disabled")) {
661
+ return true;
691
662
  }
692
- } else {
693
- selection.setBaseAndExtent(element, 0, element, 0);
694
663
  }
695
664
  }
696
- }
697
- function getInputRange(focusNode) {
698
- var _a;
699
- const typeAndSelection = getTargetTypeAndSelection(focusNode);
700
- if (typeAndSelection.type === "input") {
701
- return typeAndSelection.selection;
702
- } else if (typeAndSelection.type === "contenteditable") {
703
- return (_a = typeAndSelection.selection) == null ? void 0 : _a.getRangeAt(0);
704
- }
705
- }
706
- function setSelection({
707
- focusNode,
708
- focusOffset,
709
- anchorNode = focusNode,
710
- anchorOffset = focusOffset
711
- }) {
712
- var _a, _b;
713
- const typeAndSelection = getTargetTypeAndSelection(focusNode);
714
- if (typeAndSelection.type === "input") {
715
- return setUISelection(focusNode, {
716
- anchorOffset,
717
- focusOffset
718
- });
719
- }
720
- (_b = (_a = anchorNode.ownerDocument) == null ? void 0 : _a.getSelection()) == null ? void 0 : _b.setBaseAndExtent(anchorNode, anchorOffset, focusNode, focusOffset);
665
+ return false;
721
666
  }
722
667
 
723
- // src/utils/edit/editInputElement.ts
724
- function editInputElement(element, {
725
- newValue,
726
- newSelection,
727
- eventOverrides
728
- }) {
729
- const oldValue = element.value;
730
- setUIValue(element, newValue);
731
- setSelection({
732
- focusNode: newSelection.node,
733
- anchorOffset: newSelection.offset,
734
- focusOffset: newSelection.offset
735
- });
736
- startTrackValue(element);
737
- import_dom2.fireEvent.input(element, __spreadValues({}, eventOverrides));
738
- const tracked = endTrackValue(element);
739
- if ((tracked == null ? void 0 : tracked.length) === 2 && tracked[0] === oldValue && tracked[1] === newValue) {
740
- setSelection({
741
- focusNode: newSelection.node,
742
- anchorOffset: newSelection.offset,
743
- focusOffset: newSelection.offset
744
- });
668
+ // src/utils/focus/getActiveElement.ts
669
+ function getActiveElement(document2) {
670
+ const activeElement = document2.activeElement;
671
+ if (activeElement == null ? void 0 : activeElement.shadowRoot) {
672
+ return getActiveElement(activeElement.shadowRoot);
673
+ } else {
674
+ if (isDisabled(activeElement)) {
675
+ return document2.ownerDocument ? document2.ownerDocument.body : document2.body;
676
+ }
677
+ return activeElement;
745
678
  }
746
679
  }
747
680
 
748
- // src/utils/edit/getValue.ts
749
- function getValue(element) {
750
- if (!element) {
751
- return null;
752
- }
753
- if (isContentEditable(element)) {
754
- return element.textContent;
755
- }
756
- return getUIValue(element);
757
- }
681
+ // src/utils/focus/selector.ts
682
+ var FOCUSABLE_SELECTOR = [
683
+ "input:not([type=hidden]):not([disabled])",
684
+ "button:not([disabled])",
685
+ "select:not([disabled])",
686
+ "textarea:not([disabled])",
687
+ '[contenteditable=""]',
688
+ '[contenteditable="true"]',
689
+ "a[href]",
690
+ "[tabindex]:not([disabled])"
691
+ ].join(", ");
758
692
 
759
- // src/utils/edit/maxLength.ts
760
- var maxLengthSupportedTypes = /* @__PURE__ */ ((maxLengthSupportedTypes2) => {
761
- maxLengthSupportedTypes2["email"] = "email";
762
- maxLengthSupportedTypes2["password"] = "password";
763
- maxLengthSupportedTypes2["search"] = "search";
764
- maxLengthSupportedTypes2["telephone"] = "telephone";
765
- maxLengthSupportedTypes2["text"] = "text";
766
- maxLengthSupportedTypes2["url"] = "url";
767
- return maxLengthSupportedTypes2;
768
- })(maxLengthSupportedTypes || {});
769
- function getSpaceUntilMaxLength(element) {
770
- const value = getValue(element);
771
- if (value === null) {
772
- return void 0;
773
- }
774
- const maxLength = getSanitizedMaxLength(element);
775
- return maxLength ? maxLength - value.length : void 0;
776
- }
777
- function getSanitizedMaxLength(element) {
778
- var _a;
779
- if (!supportsMaxLength(element)) {
780
- return void 0;
781
- }
782
- const attr = (_a = element.getAttribute("maxlength")) != null ? _a : "";
783
- return /^\d+$/.test(attr) && Number(attr) >= 0 ? Number(attr) : void 0;
784
- }
785
- function supportsMaxLength(element) {
786
- return isElementType(element, "textarea") || isElementType(element, "input") && Boolean(maxLengthSupportedTypes[element.type]);
693
+ // src/utils/focus/isFocusable.ts
694
+ function isFocusable(element) {
695
+ return element.matches(FOCUSABLE_SELECTOR);
787
696
  }
788
697
 
789
- // src/utils/edit/prepareInput.ts
790
- var import_dom3 = require("@testing-library/dom");
698
+ // src/utils/focus/blur.ts
699
+ function blur(element) {
700
+ if (!isFocusable(element))
701
+ return;
702
+ const wasActive = getActiveElement(element.ownerDocument) === element;
703
+ if (!wasActive)
704
+ return;
705
+ eventWrapper(() => element.blur());
706
+ }
791
707
 
792
708
  // src/utils/focus/cursor.ts
793
709
  function getNextCursorPosition(node, offset, direction, inputType) {
@@ -837,7 +753,7 @@ function getNextCursorPosition(node, offset, direction, inputType) {
837
753
  }
838
754
  function getNextCharacterContentNode(node, offset, direction) {
839
755
  const nextOffset = Number(offset) + (direction < 0 ? -1 : 0);
840
- if (offset !== void 0 && isElement2(node) && nextOffset >= 0 && nextOffset < node.children.length) {
756
+ if (offset !== void 0 && isElement(node) && nextOffset >= 0 && nextOffset < node.children.length) {
841
757
  node = node.children[nextOffset];
842
758
  }
843
759
  return walkNodes(node, direction === 1 ? "next" : "previous", isTreatedAsCharacterContent);
@@ -846,7 +762,7 @@ function isTreatedAsCharacterContent(node) {
846
762
  if (isTextNode(node)) {
847
763
  return true;
848
764
  }
849
- if (isElement2(node)) {
765
+ if (isElement(node)) {
850
766
  if (isElementType(node, ["input", "textarea"])) {
851
767
  return node.type !== "hidden";
852
768
  } else if (isElementType(node, "br")) {
@@ -863,7 +779,7 @@ function getOffset(node) {
863
779
  }
864
780
  return i;
865
781
  }
866
- function isElement2(node) {
782
+ function isElement(node) {
867
783
  return node.nodeType === 1;
868
784
  }
869
785
  function isTextNode(node) {
@@ -878,7 +794,7 @@ function walkNodes(node, direction, callback) {
878
794
  if (callback(node)) {
879
795
  return node;
880
796
  }
881
- } else if (node.parentNode && (!isElement2(node.parentNode) || !isContentEditable(node.parentNode) && node.parentNode !== ((_a = node.ownerDocument) == null ? void 0 : _a.body))) {
797
+ } else if (node.parentNode && (!isElement(node.parentNode) || !isContentEditable(node.parentNode) && node.parentNode !== ((_a = node.ownerDocument) == null ? void 0 : _a.body))) {
882
798
  node = node.parentNode;
883
799
  } else {
884
800
  break;
@@ -892,167 +808,116 @@ function getDescendant(node, direction) {
892
808
  return node;
893
809
  }
894
810
 
895
- // src/utils/edit/prepareInput.ts
896
- function prepareInput(data, element, inputType = "insertText") {
897
- const inputRange = getInputRange(element);
898
- if (!inputRange) {
899
- return;
811
+ // src/utils/focus/selection.ts
812
+ function setSelectionRange(element, anchorOffset, focusOffset) {
813
+ var _a;
814
+ if (hasOwnSelection(element)) {
815
+ return setSelection({
816
+ focusNode: element,
817
+ anchorOffset,
818
+ focusOffset
819
+ });
900
820
  }
901
- if ("startContainer" in inputRange) {
902
- return {
903
- commit: () => {
904
- let del = false;
905
- if (!inputRange.collapsed) {
906
- del = true;
907
- inputRange.deleteContents();
908
- } else if (["deleteContentBackward", "deleteContentForward"].includes(inputType)) {
909
- const nextPosition = getNextCursorPosition(inputRange.startContainer, inputRange.startOffset, inputType === "deleteContentBackward" ? -1 : 1, inputType);
910
- if (nextPosition) {
911
- del = true;
912
- const delRange = inputRange.cloneRange();
913
- if (delRange.comparePoint(nextPosition.node, nextPosition.offset) < 0) {
914
- delRange.setStart(nextPosition.node, nextPosition.offset);
915
- } else {
916
- delRange.setEnd(nextPosition.node, nextPosition.offset);
917
- }
918
- delRange.deleteContents();
919
- }
920
- }
921
- if (data) {
922
- if (inputRange.endContainer.nodeType === 3) {
923
- const offset = inputRange.endOffset;
924
- inputRange.endContainer.insertData(offset, data);
925
- inputRange.setStart(inputRange.endContainer, offset + data.length);
926
- inputRange.setEnd(inputRange.endContainer, offset + data.length);
927
- } else {
928
- const text = element.ownerDocument.createTextNode(data);
929
- inputRange.insertNode(text);
930
- inputRange.setStart(text, data.length);
931
- inputRange.setEnd(text, data.length);
932
- }
933
- }
934
- if (del || data) {
935
- import_dom3.fireEvent.input(element, { inputType });
936
- }
937
- }
938
- };
939
- } else {
821
+ if (isContentEditable(element) && ((_a = element.firstChild) == null ? void 0 : _a.nodeType) === 3) {
822
+ return setSelection({
823
+ focusNode: element.firstChild,
824
+ anchorOffset,
825
+ focusOffset
826
+ });
827
+ }
828
+ throw new Error("Not implemented. The result of this interaction is unreliable.");
829
+ }
830
+ function hasOwnSelection(node) {
831
+ return isElement2(node) && (isElementType(node, "textarea") || isElementType(node, "input") && node.type in editableInputTypes);
832
+ }
833
+ function isElement2(node) {
834
+ return node.nodeType === 1;
835
+ }
836
+ function getTargetTypeAndSelection(node) {
837
+ const element = getElement2(node);
838
+ if (element && hasOwnSelection(element)) {
940
839
  return {
941
- getNewValue: () => calculateNewValue(data, element, inputRange, inputType).newValue,
942
- commit: () => {
943
- const { newValue, newOffset, oldValue } = calculateNewValue(data, element, inputRange, inputType);
944
- if (newValue === oldValue && newOffset === inputRange.startOffset && newOffset === inputRange.endOffset) {
945
- return;
946
- }
947
- editInputElement(element, {
948
- newValue,
949
- newSelection: {
950
- node: element,
951
- offset: newOffset
952
- },
953
- eventOverrides: {
954
- inputType
955
- }
956
- });
957
- }
840
+ type: "input",
841
+ selection: getUISelection(element)
958
842
  };
959
843
  }
844
+ const selection = element == null ? void 0 : element.ownerDocument.getSelection();
845
+ const isCE = getContentEditable(node) && (selection == null ? void 0 : selection.anchorNode) && getContentEditable(selection.anchorNode);
846
+ return {
847
+ type: isCE ? "contenteditable" : "default",
848
+ selection
849
+ };
960
850
  }
961
-
962
- // src/utils/edit/setFiles.ts
963
- var fakeFiles = Symbol("files and value properties are mocked");
964
- function setFiles(el, files) {
851
+ function getElement2(node) {
852
+ return node.nodeType === 1 ? node : node.parentElement;
853
+ }
854
+ function updateSelectionOnFocus(element) {
965
855
  var _a;
966
- (_a = el[fakeFiles]) == null ? void 0 : _a.restore();
967
- const objectDescriptors = Object.getOwnPropertyDescriptors(el);
968
- const prototypeDescriptors = Object.getOwnPropertyDescriptors(Object.getPrototypeOf(el));
969
- function restore() {
970
- Object.defineProperties(el, {
971
- files: __spreadValues(__spreadValues({}, prototypeDescriptors.files), objectDescriptors.files),
972
- value: __spreadValues(__spreadValues({}, prototypeDescriptors.value), objectDescriptors.value),
973
- type: __spreadValues(__spreadValues({}, prototypeDescriptors.type), objectDescriptors.type)
974
- });
856
+ const selection = element.ownerDocument.getSelection();
857
+ if (!(selection == null ? void 0 : selection.focusNode)) {
858
+ return;
975
859
  }
976
- el[fakeFiles] = { restore };
977
- Object.defineProperties(el, {
978
- files: __spreadProps(__spreadValues(__spreadValues({}, prototypeDescriptors.files), objectDescriptors.files), {
979
- get: () => files
980
- }),
981
- value: __spreadProps(__spreadValues(__spreadValues({}, prototypeDescriptors.value), objectDescriptors.value), {
982
- get: () => files.length ? `C:\\fakepath\\${files[0].name}` : "",
983
- set(v) {
984
- var _a2;
985
- if (v === "") {
986
- restore();
987
- } else {
988
- (_a2 = objectDescriptors.value.set) == null ? void 0 : _a2.call(el, v);
989
- }
990
- }
991
- }),
992
- type: __spreadProps(__spreadValues(__spreadValues({}, prototypeDescriptors.type), objectDescriptors.type), {
993
- set(v) {
994
- if (v !== "file") {
995
- restore();
996
- el.type = v;
997
- }
860
+ if (hasOwnSelection(element)) {
861
+ const contenteditable = getContentEditable(selection.focusNode);
862
+ if (contenteditable) {
863
+ if (!selection.isCollapsed) {
864
+ const focusNode = ((_a = contenteditable.firstChild) == null ? void 0 : _a.nodeType) === 3 ? contenteditable.firstChild : contenteditable;
865
+ selection.setBaseAndExtent(focusNode, 0, focusNode, 0);
998
866
  }
999
- })
1000
- });
1001
- }
1002
-
1003
- // src/utils/misc/eventWrapper.ts
1004
- var import_dom4 = require("@testing-library/dom");
1005
- function eventWrapper(cb) {
1006
- let result;
1007
- (0, import_dom4.getConfig)().eventWrapper(() => {
1008
- result = cb();
1009
- });
1010
- return result;
1011
- }
1012
-
1013
- // src/utils/misc/isDisabled.ts
1014
- function isDisabled(element) {
1015
- return Boolean(element && element.disabled);
1016
- }
1017
-
1018
- // src/utils/focus/getActiveElement.ts
1019
- function getActiveElement(document2) {
1020
- const activeElement = document2.activeElement;
1021
- if (activeElement == null ? void 0 : activeElement.shadowRoot) {
1022
- return getActiveElement(activeElement.shadowRoot);
1023
- } else {
1024
- if (isDisabled(activeElement)) {
1025
- return document2.ownerDocument ? document2.ownerDocument.body : document2.body;
867
+ } else {
868
+ selection.setBaseAndExtent(element, 0, element, 0);
1026
869
  }
1027
- return activeElement;
1028
870
  }
1029
871
  }
1030
-
1031
- // src/utils/focus/selector.ts
1032
- var FOCUSABLE_SELECTOR = [
1033
- "input:not([type=hidden]):not([disabled])",
1034
- "button:not([disabled])",
1035
- "select:not([disabled])",
1036
- "textarea:not([disabled])",
1037
- '[contenteditable=""]',
1038
- '[contenteditable="true"]',
1039
- "a[href]",
1040
- "[tabindex]:not([disabled])"
1041
- ].join(", ");
1042
-
1043
- // src/utils/focus/isFocusable.ts
1044
- function isFocusable(element) {
1045
- return element.matches(FOCUSABLE_SELECTOR);
872
+ function getInputRange(focusNode) {
873
+ var _a;
874
+ const typeAndSelection = getTargetTypeAndSelection(focusNode);
875
+ if (typeAndSelection.type === "input") {
876
+ return typeAndSelection.selection;
877
+ } else if (typeAndSelection.type === "contenteditable") {
878
+ return (_a = typeAndSelection.selection) == null ? void 0 : _a.getRangeAt(0);
879
+ }
880
+ }
881
+ function setSelection({
882
+ focusNode,
883
+ focusOffset,
884
+ anchorNode = focusNode,
885
+ anchorOffset = focusOffset
886
+ }) {
887
+ var _a, _b;
888
+ const typeAndSelection = getTargetTypeAndSelection(focusNode);
889
+ if (typeAndSelection.type === "input") {
890
+ return setUISelection(focusNode, {
891
+ anchorOffset,
892
+ focusOffset
893
+ });
894
+ }
895
+ (_b = (_a = anchorNode.ownerDocument) == null ? void 0 : _a.getSelection()) == null ? void 0 : _b.setBaseAndExtent(anchorNode, anchorOffset, focusNode, focusOffset);
1046
896
  }
1047
-
1048
- // src/utils/focus/blur.ts
1049
- function blur(element) {
1050
- if (!isFocusable(element))
1051
- return;
1052
- const wasActive = getActiveElement(element.ownerDocument) === element;
1053
- if (!wasActive)
1054
- return;
1055
- eventWrapper(() => element.blur());
897
+ function moveSelection(node, direction) {
898
+ if (hasOwnSelection(node)) {
899
+ const selection = getUISelection(node);
900
+ setSelection({
901
+ focusNode: node,
902
+ focusOffset: selection.startOffset === selection.endOffset ? selection.focusOffset + direction : direction < 0 ? selection.startOffset : selection.endOffset
903
+ });
904
+ } else {
905
+ const selection = node.ownerDocument.getSelection();
906
+ if (!selection) {
907
+ return;
908
+ }
909
+ if (selection.isCollapsed) {
910
+ const nextPosition = getNextCursorPosition(selection.focusNode, selection.focusOffset, direction);
911
+ if (nextPosition) {
912
+ setSelection({
913
+ focusNode: nextPosition.node,
914
+ focusOffset: nextPosition.offset
915
+ });
916
+ }
917
+ } else {
918
+ selection[direction < 0 ? "collapseToStart" : "collapseToEnd"]();
919
+ }
920
+ }
1056
921
  }
1057
922
 
1058
923
  // src/utils/focus/copySelection.ts
@@ -1072,15 +937,30 @@ function readSelectedValueFromInput(target) {
1072
937
  return val.substring(sel.startOffset, sel.endOffset);
1073
938
  }
1074
939
 
940
+ // src/utils/misc/findClosest.ts
941
+ function findClosest(element, callback) {
942
+ let el = element;
943
+ do {
944
+ if (callback(el)) {
945
+ return el;
946
+ }
947
+ el = el.parentElement;
948
+ } while (el && el !== element.ownerDocument.body);
949
+ return void 0;
950
+ }
951
+
1075
952
  // src/utils/focus/focus.ts
1076
953
  function focus(element) {
1077
- if (!isFocusable(element))
954
+ const target = findClosest(element, isFocusable);
955
+ const activeElement = getActiveElement(element.ownerDocument);
956
+ if ((target != null ? target : element.ownerDocument.body) === activeElement) {
1078
957
  return;
1079
- const isAlreadyActive = getActiveElement(element.ownerDocument) === element;
1080
- if (isAlreadyActive)
1081
- return;
1082
- eventWrapper(() => element.focus());
1083
- updateSelectionOnFocus(element);
958
+ } else if (target) {
959
+ eventWrapper(() => target.focus());
960
+ } else {
961
+ eventWrapper(() => activeElement == null ? void 0 : activeElement.blur());
962
+ }
963
+ updateSelectionOnFocus(target != null ? target : element.ownerDocument.body);
1084
964
  }
1085
965
 
1086
966
  // src/utils/misc/isVisible.ts
@@ -1177,6 +1057,14 @@ function isAllSelected(target) {
1177
1057
  return (selection == null ? void 0 : selection.anchorNode) === focusNode && selection.focusNode === focusNode && selection.anchorOffset === 0 && selection.focusOffset === focusNode.childNodes.length;
1178
1058
  }
1179
1059
 
1060
+ // src/utils/keyboard/getKeyEventProps.ts
1061
+ function getKeyEventProps(keyDef) {
1062
+ return {
1063
+ key: keyDef.key,
1064
+ code: keyDef.code
1065
+ };
1066
+ }
1067
+
1180
1068
  // src/utils/keyboard/getUIEventModifiers.ts
1181
1069
  function getUIEventModifiers(keyboardState) {
1182
1070
  return {
@@ -1195,21 +1083,13 @@ function getUIEventModifiers(keyboardState) {
1195
1083
  };
1196
1084
  }
1197
1085
 
1198
- // src/utils/keyboard/getKeyEventProps.ts
1199
- function getKeyEventProps(keyDef, state) {
1200
- return __spreadValues({
1201
- key: keyDef.key,
1202
- code: keyDef.code
1203
- }, getUIEventModifiers(state));
1204
- }
1205
-
1206
1086
  // src/utils/keyDef/readNextDescriptor.ts
1207
1087
  var bracketDict = /* @__PURE__ */ ((bracketDict2) => {
1208
1088
  bracketDict2["{"] = "}";
1209
1089
  bracketDict2["["] = "]";
1210
1090
  return bracketDict2;
1211
1091
  })(bracketDict || {});
1212
- function readNextDescriptor(text) {
1092
+ function readNextDescriptor(text, context) {
1213
1093
  let pos = 0;
1214
1094
  const startBracket = text[pos] in bracketDict ? text[pos] : "";
1215
1095
  pos += startBracket.length;
@@ -1217,11 +1097,11 @@ function readNextDescriptor(text) {
1217
1097
  const type3 = isEscapedChar ? "" : startBracket;
1218
1098
  return __spreadValues({
1219
1099
  type: type3
1220
- }, type3 === "" ? readPrintableChar(text, pos) : readTag(text, pos, type3));
1100
+ }, type3 === "" ? readPrintableChar(text, pos, context) : readTag(text, pos, type3, context));
1221
1101
  }
1222
- function readPrintableChar(text, pos) {
1102
+ function readPrintableChar(text, pos, context) {
1223
1103
  const descriptor = text[pos];
1224
- assertDescriptor(descriptor, text, pos);
1104
+ assertDescriptor(descriptor, text, pos, context);
1225
1105
  pos += descriptor.length;
1226
1106
  return {
1227
1107
  consumedLength: pos,
@@ -1231,14 +1111,14 @@ function readPrintableChar(text, pos) {
1231
1111
  repeat: 1
1232
1112
  };
1233
1113
  }
1234
- function readTag(text, pos, startBracket) {
1114
+ function readTag(text, pos, startBracket, context) {
1235
1115
  var _a, _b, _c;
1236
1116
  const releasePreviousModifier = text[pos] === "/" ? "/" : "";
1237
1117
  pos += releasePreviousModifier.length;
1238
1118
  const escapedDescriptor = startBracket === "{" && text[pos] === "\\";
1239
1119
  pos += Number(escapedDescriptor);
1240
1120
  const descriptor = escapedDescriptor ? text[pos] : (_a = text.slice(pos).match(startBracket === "{" ? /^\w+|^[^}>/]/ : /^\w+/)) == null ? void 0 : _a[0];
1241
- assertDescriptor(descriptor, text, pos);
1121
+ assertDescriptor(descriptor, text, pos, context);
1242
1122
  pos += descriptor.length;
1243
1123
  const repeatModifier = (_c = (_b = text.slice(pos).match(/^>\d+/)) == null ? void 0 : _b[0]) != null ? _c : "";
1244
1124
  pos += repeatModifier.length;
@@ -1251,7 +1131,7 @@ function readTag(text, pos, startBracket) {
1251
1131
  !repeatModifier && "repeat modifier",
1252
1132
  !releaseSelfModifier && "release modifier",
1253
1133
  `"${expectedEndBracket}"`
1254
- ].filter(Boolean).join(" or "), text[pos], text));
1134
+ ].filter(Boolean).join(" or "), text[pos], text, context));
1255
1135
  }
1256
1136
  pos += endBracket.length;
1257
1137
  return {
@@ -1262,9 +1142,9 @@ function readTag(text, pos, startBracket) {
1262
1142
  releaseSelf: hasReleaseSelf(releaseSelfModifier, repeatModifier)
1263
1143
  };
1264
1144
  }
1265
- function assertDescriptor(descriptor, text, pos) {
1145
+ function assertDescriptor(descriptor, text, pos, context) {
1266
1146
  if (!descriptor) {
1267
- throw new Error(getErrorMessage("key descriptor", text[pos], text));
1147
+ throw new Error(getErrorMessage("key descriptor", text[pos], text, context));
1268
1148
  }
1269
1149
  }
1270
1150
  function hasReleaseSelf(releaseSelfModifier, repeatModifier) {
@@ -1275,22 +1155,15 @@ function hasReleaseSelf(releaseSelfModifier, repeatModifier) {
1275
1155
  return false;
1276
1156
  }
1277
1157
  }
1278
- function getErrorMessage(expected, found, text) {
1158
+ function getErrorMessage(expected, found, text, context) {
1279
1159
  return `Expected ${expected} but found "${found != null ? found : ""}" in "${text}"
1280
- See https://github.com/testing-library/user-event/blob/main/README.md#keyboardtext-options
1160
+ 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`}
1281
1161
  for more information about how userEvent parses your input.`;
1282
1162
  }
1283
1163
 
1284
- // src/utils/misc/findClosest.ts
1285
- function findClosest(element, callback) {
1286
- let el = element;
1287
- do {
1288
- if (callback(el)) {
1289
- return el;
1290
- }
1291
- el = el.parentElement;
1292
- } while (el && el !== element.ownerDocument.body);
1293
- return void 0;
1164
+ // src/utils/misc/cloneEvent.ts
1165
+ function cloneEvent(event) {
1166
+ return new event.constructor(event.type, event);
1294
1167
  }
1295
1168
 
1296
1169
  // src/utils/misc/getDocumentFromNode.ts
@@ -1330,9 +1203,6 @@ function wait(time) {
1330
1203
  return new Promise((resolve) => setTimeout(() => resolve(), time));
1331
1204
  }
1332
1205
 
1333
- // src/utils/misc/hasFormSubmit.ts
1334
- var hasFormSubmit = (form) => !!(form == null ? void 0 : form.querySelector('input[type="submit"], button:not([type]), button[type="submit"]'));
1335
-
1336
1206
  // src/utils/pointer/cssPointerEvents.ts
1337
1207
  var import_helpers2 = require("@testing-library/dom/dist/helpers.js");
1338
1208
 
@@ -1459,10 +1329,6 @@ function hasBitFlag(conf, flag) {
1459
1329
  return (conf & flag) > 0;
1460
1330
  }
1461
1331
 
1462
- // src/utils/pointer/firePointerEvents.ts
1463
- var import_dom5 = require("@testing-library/dom");
1464
- var import_event_map = require("@testing-library/dom/dist/event-map.js");
1465
-
1466
1332
  // src/utils/pointer/mouseButtons.ts
1467
1333
  var MouseButton = {
1468
1334
  primary: 0,
@@ -1494,38 +1360,41 @@ function getMouseButtons(...buttons) {
1494
1360
  return v;
1495
1361
  }
1496
1362
 
1497
- // src/utils/pointer/firePointerEvents.ts
1498
- function firePointerEvent(target, type3, {
1499
- pointerState,
1500
- keyboardState,
1501
- pointerType,
1502
- button,
1503
- coords,
1504
- pointerId,
1505
- isPrimary,
1506
- clickCount
1507
- }) {
1508
- const init = __spreadValues(__spreadValues({}, coords), getUIEventModifiers(keyboardState));
1509
- if (type3 === "click" || type3.startsWith("pointer")) {
1510
- init.pointerId = pointerId;
1511
- init.pointerType = pointerType;
1512
- }
1513
- if (["pointerdown", "pointerup"].includes(type3)) {
1514
- init.isPrimary = isPrimary;
1363
+ // src/event/createEvent.ts
1364
+ var import_dom2 = require("@testing-library/dom");
1365
+
1366
+ // src/event/eventMap.ts
1367
+ var import_event_map = require("@testing-library/dom/dist/event-map.js");
1368
+ var eventMap = __spreadProps(__spreadValues({}, import_event_map.eventMap), {
1369
+ beforeInput: {
1370
+ EventType: "InputEvent",
1371
+ defaultInit: { bubbles: true, cancelable: true, composed: true }
1515
1372
  }
1516
- init.button = getMouseButton(button != null ? button : 0);
1517
- init.buttons = getMouseButtons(...pointerState.pressed.filter((p) => p.keyDef.pointerType === pointerType).map((p) => {
1518
- var _a;
1519
- return (_a = p.keyDef.button) != null ? _a : 0;
1520
- }));
1521
- if (["mousedown", "mouseup", "click", "dblclick", "contextmenu"].includes(type3)) {
1522
- init.detail = clickCount;
1373
+ });
1374
+ var eventMapKeys = Object.fromEntries(Object.keys(eventMap).map((k) => [k.toLowerCase(), k]));
1375
+
1376
+ // src/event/eventTypes.ts
1377
+ var import_event_map2 = require("@testing-library/dom/dist/event-map.js");
1378
+ var eventKeys = Object.fromEntries(Object.keys(import_event_map2.eventMap).map((k) => [k.toLowerCase(), k]));
1379
+ function getEventClass(type3) {
1380
+ return type3 in eventKeys ? import_event_map2.eventMap[eventKeys[type3]].EventType : "Event";
1381
+ }
1382
+ var mouseEvents = ["MouseEvent", "PointerEvent"];
1383
+ function isMouseEvent(type3) {
1384
+ return mouseEvents.includes(getEventClass(type3));
1385
+ }
1386
+ function isKeyboardEvent(type3) {
1387
+ return getEventClass(type3) === "KeyboardEvent";
1388
+ }
1389
+
1390
+ // src/event/createEvent.ts
1391
+ function createEvent(type3, target, init) {
1392
+ const event = (0, import_dom2.createEvent)(type3, target, init, eventMap[eventMapKeys[type3]]);
1393
+ if (isMouseEvent(type3) && init) {
1394
+ assignPositionInit(event, init);
1395
+ assignPointerInit(event, init);
1523
1396
  }
1524
- const eventKey = Object.keys(import_event_map.eventMap).find((k) => k.toLowerCase() === type3);
1525
- const event = import_dom5.createEvent[eventKey](target, init);
1526
- assignPositionInit(event, init);
1527
- assignPointerInit(event, init);
1528
- return (0, import_dom5.fireEvent)(target, event);
1397
+ return event;
1529
1398
  }
1530
1399
  function assignProps(obj, props) {
1531
1400
  for (const [key, value] of Object.entries(props)) {
@@ -1566,265 +1435,400 @@ function assignPointerInit(obj, { isPrimary, pointerId, pointerType }) {
1566
1435
  });
1567
1436
  }
1568
1437
 
1569
- // src/keyboard/plugins/arrow.ts
1570
- var keydownBehavior = [
1571
- {
1572
- matches: (keyDef) => keyDef.key === "ArrowLeft" || keyDef.key === "ArrowRight",
1573
- handle: (keyDef, element) => {
1574
- if (hasOwnSelection(element)) {
1575
- const selection = getUISelection(element);
1576
- setSelection({
1577
- focusNode: element,
1578
- focusOffset: selection.startOffset === selection.endOffset ? selection.focusOffset + (keyDef.key === "ArrowLeft" ? -1 : 1) : keyDef.key === "ArrowLeft" ? selection.startOffset : selection.endOffset
1438
+ // src/event/behavior/registry.ts
1439
+ var behavior = {};
1440
+
1441
+ // src/event/behavior/click.ts
1442
+ behavior.click = (event, target, config) => {
1443
+ const context = target.closest("button,input,label,textarea");
1444
+ const control = context && isElementType(context, "label") && context.control;
1445
+ if (control) {
1446
+ return () => {
1447
+ if (isFocusable(control)) {
1448
+ focus(control);
1449
+ }
1450
+ dispatchEvent(config, control, cloneEvent(event));
1451
+ };
1452
+ } else if (isElementType(target, "input", { type: "file" })) {
1453
+ return () => {
1454
+ blur(target);
1455
+ target.dispatchEvent(new Event("fileDialog"));
1456
+ focus(target);
1457
+ };
1458
+ }
1459
+ };
1460
+
1461
+ // src/event/behavior/cut.ts
1462
+ behavior.cut = (event, target, config) => {
1463
+ return () => {
1464
+ if (isEditable(target)) {
1465
+ input(config, target, "", "deleteByCut");
1466
+ }
1467
+ };
1468
+ };
1469
+
1470
+ // src/event/behavior/keydown.ts
1471
+ behavior.keydown = (event, target, config) => {
1472
+ var _a, _b;
1473
+ return (_b = (_a = keydownBehavior[event.key]) == null ? void 0 : _a.call(keydownBehavior, event, target, config)) != null ? _b : combinationBehavior(event, target, config);
1474
+ };
1475
+ var keydownBehavior = {
1476
+ ArrowLeft: (event, target) => () => moveSelection(target, -1),
1477
+ ArrowRight: (event, target) => () => moveSelection(target, 1),
1478
+ Backspace: (event, target, config) => {
1479
+ if (isEditable(target)) {
1480
+ return () => {
1481
+ input(config, target, "", "deleteContentBackward");
1482
+ };
1483
+ }
1484
+ },
1485
+ Delete: (event, target, config) => {
1486
+ if (isEditable(target)) {
1487
+ return () => {
1488
+ input(config, target, "", "deleteContentForward");
1489
+ };
1490
+ }
1491
+ },
1492
+ End: (event, target) => {
1493
+ if (isElementType(target, ["input", "textarea"]) || isContentEditable(target)) {
1494
+ return () => {
1495
+ var _a, _b;
1496
+ const newPos = (_b = (_a = getValue(target)) == null ? void 0 : _a.length) != null ? _b : 0;
1497
+ setSelectionRange(target, newPos, newPos);
1498
+ };
1499
+ }
1500
+ },
1501
+ Home: (event, target) => {
1502
+ if (isElementType(target, ["input", "textarea"]) || isContentEditable(target)) {
1503
+ return () => {
1504
+ setSelectionRange(target, 0, 0);
1505
+ };
1506
+ }
1507
+ },
1508
+ PageDown: (event, target) => {
1509
+ if (isElementType(target, ["input"])) {
1510
+ return () => {
1511
+ const newPos = getValue(target).length;
1512
+ setSelectionRange(target, newPos, newPos);
1513
+ };
1514
+ }
1515
+ },
1516
+ PageUp: (event, target) => {
1517
+ if (isElementType(target, ["input"])) {
1518
+ return () => {
1519
+ setSelectionRange(target, 0, 0);
1520
+ };
1521
+ }
1522
+ },
1523
+ Tab: (event, target, { keyboardState }) => {
1524
+ return () => {
1525
+ const dest = getTabDestination(target, keyboardState.modifiers.Shift);
1526
+ focus(dest);
1527
+ if (hasOwnSelection(dest)) {
1528
+ setUISelection(dest, {
1529
+ anchorOffset: 0,
1530
+ focusOffset: dest.value.length
1579
1531
  });
1532
+ }
1533
+ };
1534
+ }
1535
+ };
1536
+ var combinationBehavior = (event, target, config) => {
1537
+ if (event.code === "KeyA" && config.keyboardState.modifiers.Control) {
1538
+ return () => selectAll(target);
1539
+ }
1540
+ };
1541
+
1542
+ // src/event/behavior/keypress.ts
1543
+ behavior.keypress = (event, target, config) => {
1544
+ if (event.key === "Enter") {
1545
+ if (isElementType(target, "button") || isElementType(target, "input") && ClickInputOnEnter.includes(target.type) || isElementType(target, "a") && Boolean(target.href)) {
1546
+ return () => {
1547
+ dispatchUIEvent(config, target, "click");
1548
+ };
1549
+ } else if (isElementType(target, "input")) {
1550
+ const form = target.form;
1551
+ const submit = form == null ? void 0 : form.querySelector('input[type="submit"], button:not([type]), button[type="submit"]');
1552
+ if (submit) {
1553
+ return () => dispatchUIEvent(config, submit, "click");
1554
+ } else if (form && SubmitSingleInputOnEnter.includes(target.type) && form.querySelectorAll("input").length === 1) {
1555
+ return () => dispatchUIEvent(config, form, "submit");
1580
1556
  } else {
1581
- const selection = element.ownerDocument.getSelection();
1582
- if (!selection) {
1583
- return;
1584
- }
1585
- if (selection.isCollapsed) {
1586
- const nextPosition = getNextCursorPosition(selection.focusNode, selection.focusOffset, keyDef.key === "ArrowLeft" ? -1 : 1);
1587
- if (nextPosition) {
1588
- setSelection({
1589
- focusNode: nextPosition.node,
1590
- focusOffset: nextPosition.offset
1591
- });
1592
- }
1593
- } else {
1594
- selection[keyDef.key === "ArrowLeft" ? "collapseToStart" : "collapseToEnd"]();
1595
- }
1557
+ return;
1596
1558
  }
1597
1559
  }
1598
1560
  }
1561
+ if (isEditable(target)) {
1562
+ const inputType = event.key === "Enter" ? isContentEditable(target) && !config.keyboardState.modifiers.Shift ? "insertParagraph" : "insertLineBreak" : "insertText";
1563
+ const inputData = event.key === "Enter" ? "\n" : event.key;
1564
+ return () => input(config, target, inputData, inputType);
1565
+ }
1566
+ };
1567
+ var ClickInputOnEnter = [
1568
+ "button",
1569
+ "color",
1570
+ "file",
1571
+ "image",
1572
+ "reset",
1573
+ "submit"
1574
+ ];
1575
+ var SubmitSingleInputOnEnter = [
1576
+ "email",
1577
+ "month",
1578
+ "password",
1579
+ "search",
1580
+ "tel",
1581
+ "text",
1582
+ "url",
1583
+ "week"
1599
1584
  ];
1600
1585
 
1601
- // src/keyboard/plugins/control.ts
1602
- var keydownBehavior2 = [
1603
- {
1604
- matches: (keyDef, element) => (keyDef.key === "Home" || keyDef.key === "End") && (isElementType(element, ["input", "textarea"]) || isContentEditable(element)),
1605
- handle: (keyDef, element) => {
1606
- var _a, _b;
1607
- if (keyDef.key === "Home") {
1608
- setSelectionRange(element, 0, 0);
1609
- } else {
1610
- const newPos = (_b = (_a = getValue(element)) == null ? void 0 : _a.length) != null ? _b : 0;
1611
- setSelectionRange(element, newPos, newPos);
1612
- }
1586
+ // src/event/behavior/keyup.ts
1587
+ behavior.keyup = (event, target, config) => {
1588
+ var _a;
1589
+ return (_a = keyupBehavior[event.key]) == null ? void 0 : _a.call(keyupBehavior, event, target, config);
1590
+ };
1591
+ var keyupBehavior = {
1592
+ " ": (event, target, config) => {
1593
+ if (isClickableInput(target)) {
1594
+ return () => dispatchUIEvent(config, target, "click");
1613
1595
  }
1614
- },
1615
- {
1616
- matches: (keyDef, element) => (keyDef.key === "PageUp" || keyDef.key === "PageDown") && isElementType(element, ["input"]),
1617
- handle: (keyDef, element) => {
1618
- var _a, _b;
1619
- if (keyDef.key === "PageUp") {
1620
- setSelectionRange(element, 0, 0);
1621
- } else {
1622
- const newPos = (_b = (_a = getValue(element)) == null ? void 0 : _a.length) != null ? _b : 0;
1623
- setSelectionRange(element, newPos, newPos);
1596
+ }
1597
+ };
1598
+
1599
+ // src/event/behavior/paste.ts
1600
+ behavior.paste = (event, target, config) => {
1601
+ if (isEditable(target)) {
1602
+ return () => {
1603
+ var _a;
1604
+ const insertData = (_a = event.clipboardData) == null ? void 0 : _a.getData("text");
1605
+ if (insertData) {
1606
+ input(config, target, insertData, "insertFromPaste");
1607
+ }
1608
+ };
1609
+ }
1610
+ };
1611
+
1612
+ // src/event/wrapEvent.ts
1613
+ var import_dom3 = require("@testing-library/dom");
1614
+ function wrapEvent(cb, _element) {
1615
+ return (0, import_dom3.getConfig)().eventWrapper(cb);
1616
+ }
1617
+
1618
+ // src/event/dispatchEvent.ts
1619
+ function dispatchEvent(config, target, event, preventDefault = false) {
1620
+ var _a, _b;
1621
+ const type3 = event.type;
1622
+ const behaviorImplementation = preventDefault ? () => {
1623
+ } : (_b = (_a = behavior)[type3]) == null ? void 0 : _b.call(_a, event, target, config);
1624
+ if (behaviorImplementation) {
1625
+ event.preventDefault();
1626
+ let defaultPrevented = false;
1627
+ Object.defineProperty(event, "defaultPrevented", {
1628
+ get: () => defaultPrevented
1629
+ });
1630
+ Object.defineProperty(event, "preventDefault", {
1631
+ value: () => {
1632
+ defaultPrevented = event.cancelable;
1624
1633
  }
1634
+ });
1635
+ wrapEvent(() => target.dispatchEvent(event), target);
1636
+ if (!defaultPrevented) {
1637
+ behaviorImplementation();
1625
1638
  }
1626
- },
1627
- {
1628
- matches: (keyDef, element) => keyDef.key === "Delete" && isEditable(element),
1629
- handle: (keDef, element) => {
1630
- var _a;
1631
- (_a = prepareInput("", element, "deleteContentForward")) == null ? void 0 : _a.commit();
1639
+ return !defaultPrevented;
1640
+ }
1641
+ return wrapEvent(() => target.dispatchEvent(event), target);
1642
+ }
1643
+
1644
+ // src/event/index.ts
1645
+ function dispatchUIEvent(config, target, type3, init, preventDefault = false) {
1646
+ if (isMouseEvent(type3) || isKeyboardEvent(type3)) {
1647
+ init = __spreadValues(__spreadValues({}, init), getUIEventModifiers(config.keyboardState));
1648
+ }
1649
+ const event = createEvent(type3, target, init);
1650
+ return dispatchEvent(config, target, event, preventDefault);
1651
+ }
1652
+ function bindDispatchUIEvent(config) {
1653
+ return dispatchUIEvent.bind(void 0, config);
1654
+ }
1655
+
1656
+ // src/document/interceptor.ts
1657
+ var Interceptor = Symbol("Interceptor for programmatical calls");
1658
+ function prepareInterceptor(element, propName, interceptorImpl) {
1659
+ const prototypeDescriptor = Object.getOwnPropertyDescriptor(element.constructor.prototype, propName);
1660
+ const objectDescriptor = Object.getOwnPropertyDescriptor(element, propName);
1661
+ const target = (prototypeDescriptor == null ? void 0 : prototypeDescriptor.set) ? "set" : "value";
1662
+ if (typeof (prototypeDescriptor == null ? void 0 : prototypeDescriptor[target]) !== "function" || prototypeDescriptor[target][Interceptor]) {
1663
+ return;
1664
+ }
1665
+ function intercept(...args) {
1666
+ const { applyNative = true, realArgs } = interceptorImpl.call(this, ...args);
1667
+ const realFunc = (!applyNative && objectDescriptor || prototypeDescriptor)[target];
1668
+ if (target === "set") {
1669
+ realFunc.call(this, realArgs);
1670
+ } else {
1671
+ realFunc.call(this, ...realArgs);
1632
1672
  }
1633
1673
  }
1634
- ];
1674
+ ;
1675
+ intercept[Interceptor] = Interceptor;
1676
+ Object.defineProperty(element, propName, __spreadProps(__spreadValues({}, objectDescriptor != null ? objectDescriptor : prototypeDescriptor), {
1677
+ [target]: intercept
1678
+ }));
1679
+ }
1635
1680
 
1636
- // src/keyboard/plugins/character.ts
1637
- var import_dom7 = require("@testing-library/dom");
1681
+ // src/document/selection.ts
1682
+ var UISelection = Symbol("Displayed selection in UI");
1683
+ function prepareSelectionInterceptor(element) {
1684
+ prepareInterceptor(element, "setSelectionRange", function interceptorImpl(start, end, direction = "none") {
1685
+ const isUI = start && typeof start === "object" && start[UISelection];
1686
+ if (!isUI) {
1687
+ this[UISelection] = void 0;
1688
+ }
1689
+ return {
1690
+ realArgs: [Number(start), end, direction]
1691
+ };
1692
+ });
1693
+ prepareInterceptor(element, "selectionStart", function interceptorImpl(v) {
1694
+ this[UISelection] = void 0;
1695
+ return { realArgs: v };
1696
+ });
1697
+ prepareInterceptor(element, "selectionEnd", function interceptorImpl(v) {
1698
+ this[UISelection] = void 0;
1699
+ return { realArgs: v };
1700
+ });
1701
+ }
1702
+ function setUISelection(element, {
1703
+ focusOffset: focusOffsetParam,
1704
+ anchorOffset: anchorOffsetParam = focusOffsetParam
1705
+ }, mode = "replace") {
1706
+ const valueLength = getUIValue(element).length;
1707
+ const sanitizeOffset = (o) => Math.max(0, Math.min(valueLength, o));
1708
+ const anchorOffset = mode === "replace" || element[UISelection] === void 0 ? sanitizeOffset(anchorOffsetParam) : element[UISelection].anchorOffset;
1709
+ const focusOffset = sanitizeOffset(focusOffsetParam);
1710
+ const startOffset = Math.min(anchorOffset, focusOffset);
1711
+ const endOffset = Math.max(anchorOffset, focusOffset);
1712
+ element[UISelection] = {
1713
+ anchorOffset,
1714
+ focusOffset
1715
+ };
1716
+ if (element.selectionStart === startOffset && element.selectionEnd === endOffset) {
1717
+ return;
1718
+ }
1719
+ const startObj = new Number(startOffset);
1720
+ startObj[UISelection] = UISelection;
1721
+ try {
1722
+ element.setSelectionRange(startObj, endOffset);
1723
+ } catch {
1724
+ }
1725
+ }
1726
+ function getUISelection(element) {
1727
+ var _a, _b, _c;
1728
+ const sel = (_c = element[UISelection]) != null ? _c : {
1729
+ anchorOffset: (_a = element.selectionStart) != null ? _a : 0,
1730
+ focusOffset: (_b = element.selectionEnd) != null ? _b : 0
1731
+ };
1732
+ return __spreadProps(__spreadValues({}, sel), {
1733
+ startOffset: Math.min(sel.anchorOffset, sel.focusOffset),
1734
+ endOffset: Math.max(sel.anchorOffset, sel.focusOffset)
1735
+ });
1736
+ }
1737
+ function clearUISelection(element) {
1738
+ element[UISelection] = void 0;
1739
+ }
1638
1740
 
1639
- // src/keyboard/shared/fireChangeForInputTimeIfValid.ts
1640
- var import_dom6 = require("@testing-library/dom");
1641
- function fireChangeForInputTimeIfValid(el, prevValue, timeNewEntry) {
1642
- if (isValidInputTimeValue(el, timeNewEntry) && prevValue !== timeNewEntry) {
1643
- import_dom6.fireEvent.change(el, { target: { value: timeNewEntry } });
1741
+ // src/document/value.ts
1742
+ var UIValue = Symbol("Displayed value in UI");
1743
+ var InitialValue = Symbol("Initial value to compare on blur");
1744
+ var TrackChanges = Symbol("Track programmatic changes for React workaround");
1745
+ function valueInterceptor(v) {
1746
+ const isUI = typeof v === "object" && v[UIValue];
1747
+ this[UIValue] = isUI ? String(v) : void 0;
1748
+ if (!isUI) {
1749
+ trackValue(this, String(v));
1750
+ this[InitialValue] = String(v);
1751
+ clearUISelection(this);
1644
1752
  }
1753
+ return {
1754
+ applyNative: !!isUI,
1755
+ realArgs: String(v)
1756
+ };
1757
+ }
1758
+ function prepareValueInterceptor(element) {
1759
+ prepareInterceptor(element, "value", valueInterceptor);
1760
+ }
1761
+ function setUIValue(element, value) {
1762
+ element.value = {
1763
+ [UIValue]: UIValue,
1764
+ toString: () => value
1765
+ };
1766
+ }
1767
+ function getUIValue(element) {
1768
+ return element[UIValue] === void 0 ? element.value : String(element[UIValue]);
1769
+ }
1770
+ function setInitialValue(element) {
1771
+ element[InitialValue] = element.value;
1772
+ }
1773
+ function getInitialValue(element) {
1774
+ return element[InitialValue];
1775
+ }
1776
+ function startTrackValue(element) {
1777
+ element[TrackChanges] = [];
1778
+ }
1779
+ function trackValue(element, v) {
1780
+ var _a;
1781
+ (_a = element[TrackChanges]) == null ? void 0 : _a.push(v);
1782
+ }
1783
+ function endTrackValue(element) {
1784
+ const tracked = element[TrackChanges];
1785
+ element[TrackChanges] = void 0;
1786
+ return tracked;
1645
1787
  }
1646
1788
 
1647
- // src/keyboard/plugins/character.ts
1648
- var keypressBehavior = [
1649
- {
1650
- matches: (keyDef, element) => {
1651
- var _a;
1652
- return ((_a = keyDef.key) == null ? void 0 : _a.length) === 1 && isElementType(element, "input", { type: "time", readOnly: false });
1653
- },
1654
- handle: (keyDef, element, { keyboardState }) => {
1655
- var _a;
1656
- let newEntry = keyDef.key;
1657
- const textToBeTyped = ((_a = keyboardState.carryValue) != null ? _a : "") + newEntry;
1658
- const timeNewEntry = buildTimeValue(textToBeTyped);
1659
- if (isValidInputTimeValue(element, timeNewEntry)) {
1660
- newEntry = timeNewEntry;
1661
- }
1662
- const { newValue, newOffset } = calculateNewValue(newEntry, element, getInputRange(element));
1663
- const prevValue = getValue(element);
1664
- if (prevValue !== newValue) {
1665
- editInputElement(element, {
1666
- newValue,
1667
- newSelection: {
1668
- node: element,
1669
- offset: newOffset
1670
- },
1671
- eventOverrides: {
1672
- data: keyDef.key,
1673
- inputType: "insertText"
1674
- }
1675
- });
1676
- }
1677
- fireChangeForInputTimeIfValid(element, prevValue, timeNewEntry);
1678
- keyboardState.carryValue = textToBeTyped;
1679
- }
1680
- },
1681
- {
1682
- matches: (keyDef, element) => {
1683
- var _a;
1684
- return ((_a = keyDef.key) == null ? void 0 : _a.length) === 1 && isElementType(element, "input", { type: "date", readOnly: false });
1685
- },
1686
- handle: (keyDef, element, { keyboardState }) => {
1687
- var _a;
1688
- let newEntry = keyDef.key;
1689
- const textToBeTyped = ((_a = keyboardState.carryValue) != null ? _a : "") + newEntry;
1690
- const isValidToBeTyped = isValidDateValue(element, textToBeTyped);
1691
- if (isValidToBeTyped) {
1692
- newEntry = textToBeTyped;
1693
- }
1694
- const { newValue, newOffset } = calculateNewValue(newEntry, element, getInputRange(element));
1695
- const prevValue = getValue(element);
1696
- if (prevValue !== newValue) {
1697
- editInputElement(element, {
1698
- newValue,
1699
- newSelection: {
1700
- node: element,
1701
- offset: newOffset
1702
- },
1703
- eventOverrides: {
1704
- data: keyDef.key,
1705
- inputType: "insertText"
1706
- }
1707
- });
1708
- }
1709
- if (isValidToBeTyped) {
1710
- import_dom7.fireEvent.change(element, {
1711
- target: { value: textToBeTyped }
1712
- });
1713
- }
1714
- keyboardState.carryValue = textToBeTyped;
1715
- }
1716
- },
1717
- {
1718
- matches: (keyDef, element) => {
1719
- var _a;
1720
- return ((_a = keyDef.key) == null ? void 0 : _a.length) === 1 && isElementType(element, "input", { type: "number", readOnly: false });
1721
- },
1722
- handle: (keyDef, element) => {
1723
- var _a, _b;
1724
- if (!/[\d.\-e]/.test(keyDef.key)) {
1725
- return;
1726
- }
1727
- const { getNewValue, commit } = prepareInput(keyDef.key, element);
1728
- const newValue = getNewValue();
1729
- const valueParts = newValue.split("e", 2);
1730
- if (Number((_a = newValue.match(/-/g)) == null ? void 0 : _a.length) > 2 || Number((_b = newValue.match(/\./g)) == null ? void 0 : _b.length) > 1 || valueParts[1] && !/^-?\d*$/.test(valueParts[1])) {
1731
- return;
1732
- }
1733
- commit();
1734
- }
1735
- },
1736
- {
1737
- matches: (keyDef, element) => {
1738
- var _a;
1739
- return ((_a = keyDef.key) == null ? void 0 : _a.length) === 1 && (isEditableInput(element) || isElementType(element, "textarea", { readOnly: false }) || isContentEditable(element)) && getSpaceUntilMaxLength(element) !== 0;
1740
- },
1741
- handle: (keyDef, element) => {
1742
- var _a;
1743
- (_a = prepareInput(keyDef.key, element)) == null ? void 0 : _a.commit();
1744
- }
1745
- },
1746
- {
1747
- matches: (keyDef, element) => keyDef.key === "Enter" && (isElementType(element, "textarea", { readOnly: false }) || isContentEditable(element)) && getSpaceUntilMaxLength(element) !== 0,
1748
- handle: (keyDef, element, { keyboardState }) => {
1749
- var _a;
1750
- (_a = prepareInput("\n", element, isContentEditable(element) && !keyboardState.modifiers.Shift ? "insertParagraph" : "insertLineBreak")) == null ? void 0 : _a.commit();
1751
- }
1789
+ // src/document/index.ts
1790
+ var isPrepared = Symbol("Node prepared with document state workarounds");
1791
+ function prepareDocument(document2) {
1792
+ if (document2[isPrepared]) {
1793
+ return;
1752
1794
  }
1753
- ];
1754
-
1755
- // src/keyboard/plugins/functional.ts
1756
- var import_dom8 = require("@testing-library/dom");
1757
- var keydownBehavior3 = [
1758
- {
1759
- matches: (keyDef, element) => keyDef.key === "Backspace" && isEditable(element),
1760
- handle: (keyDef, element) => {
1761
- var _a;
1762
- (_a = prepareInput("", element, "deleteContentBackward")) == null ? void 0 : _a.commit();
1763
- }
1764
- },
1765
- {
1766
- matches: (keyDef) => keyDef.key === "Tab",
1767
- handle: (keyDef, element, { keyboardState }) => {
1768
- const dest = getTabDestination(element, keyboardState.modifiers.Shift);
1769
- if (dest === element.ownerDocument.body) {
1770
- blur(element);
1771
- } else {
1772
- focus(dest);
1773
- if (isElementType(dest, ["input", "textarea"])) {
1774
- setUISelection(dest, {
1775
- anchorOffset: 0,
1776
- focusOffset: dest.value.length
1777
- });
1778
- }
1779
- }
1780
- }
1795
+ document2.addEventListener("focus", (e) => {
1796
+ const el = e.target;
1797
+ prepareElement(el);
1798
+ }, {
1799
+ capture: true,
1800
+ passive: true
1801
+ });
1802
+ if (document2.activeElement) {
1803
+ prepareElement(document2.activeElement);
1781
1804
  }
1782
- ];
1783
- var keypressBehavior2 = [
1784
- {
1785
- matches: (keyDef, element) => keyDef.key === "Enter" && isElementType(element, "input") && ["checkbox", "radio"].includes(element.type),
1786
- handle: (keyDef, element) => {
1787
- const form = element.form;
1788
- if (hasFormSubmit(form)) {
1789
- import_dom8.fireEvent.submit(form);
1790
- }
1791
- }
1792
- },
1793
- {
1794
- matches: (keyDef, element) => keyDef.key === "Enter" && (isClickableInput(element) || isElementType(element, "a") && Boolean(element.href)),
1795
- handle: (keyDef, element, { keyboardState }) => {
1796
- import_dom8.fireEvent.click(element, getUIEventModifiers(keyboardState));
1797
- }
1798
- },
1799
- {
1800
- matches: (keyDef, element) => keyDef.key === "Enter" && isElementType(element, "input"),
1801
- handle: (keyDef, element) => {
1802
- const form = element.form;
1803
- if (form && (form.querySelectorAll("input").length === 1 || hasFormSubmit(form))) {
1804
- import_dom8.fireEvent.submit(form);
1805
- }
1805
+ document2.addEventListener("blur", (e) => {
1806
+ const el = e.target;
1807
+ const initialValue = getInitialValue(el);
1808
+ if (typeof initialValue === "string" && el.value !== initialValue) {
1809
+ dispatchUIEvent({}, el, "change");
1806
1810
  }
1811
+ }, {
1812
+ capture: true,
1813
+ passive: true
1814
+ });
1815
+ document2[isPrepared] = isPrepared;
1816
+ }
1817
+ function prepareElement(el) {
1818
+ if ("value" in el) {
1819
+ setInitialValue(el);
1807
1820
  }
1808
- ];
1809
- var keyupBehavior = [
1810
- {
1811
- matches: (keyDef, element) => keyDef.key === " " && isClickableInput(element),
1812
- handle: (keyDef, element, { keyboardState }) => {
1813
- import_dom8.fireEvent.click(element, getUIEventModifiers(keyboardState));
1814
- }
1821
+ if (el[isPrepared]) {
1822
+ return;
1815
1823
  }
1816
- ];
1817
-
1818
- // src/keyboard/plugins/combination.ts
1819
- var keydownBehavior4 = [
1820
- {
1821
- matches: (keyDef, element, { keyboardState }) => keyDef.code === "KeyA" && keyboardState.modifiers.Control,
1822
- handle: (keyDef, element) => selectAll(element)
1824
+ if ("value" in el) {
1825
+ prepareValueInterceptor(el);
1826
+ prepareSelectionInterceptor(el);
1823
1827
  }
1824
- ];
1828
+ el[isPrepared] = isPrepared;
1829
+ }
1825
1830
 
1826
- // src/keyboard/plugins/modifiers.ts
1827
- var import_dom9 = require("@testing-library/dom");
1831
+ // src/keyboard/modifiers.ts
1828
1832
  var modifierKeys = [
1829
1833
  "Alt",
1830
1834
  "AltGraph",
@@ -1834,6 +1838,9 @@ var modifierKeys = [
1834
1838
  "Shift",
1835
1839
  "Symbol"
1836
1840
  ];
1841
+ function isModifierKey(key) {
1842
+ return modifierKeys.includes(key);
1843
+ }
1837
1844
  var modifierLocks = [
1838
1845
  "CapsLock",
1839
1846
  "FnLock",
@@ -1841,78 +1848,40 @@ var modifierLocks = [
1841
1848
  "ScrollLock",
1842
1849
  "SymbolLock"
1843
1850
  ];
1844
- var preKeydownBehavior = [
1845
- {
1846
- matches: (keyDef) => modifierKeys.includes(keyDef.key),
1847
- handle: (keyDef, element, { keyboardMap, keyboardState }) => {
1848
- var _a;
1849
- keyboardState.modifiers[keyDef.key] = true;
1850
- if (keyDef.key === "AltGraph") {
1851
- const ctrlKeyDef = (_a = keyboardMap.find((k) => k.key === "Control")) != null ? _a : { key: "Control", code: "Control" };
1852
- import_dom9.fireEvent.keyDown(element, getKeyEventProps(ctrlKeyDef, keyboardState));
1853
- }
1851
+ function isModifierLock(key) {
1852
+ return modifierLocks.includes(key);
1853
+ }
1854
+ function preKeydownBehavior(config, { key }, element) {
1855
+ var _a;
1856
+ if (isModifierKey(key)) {
1857
+ config.keyboardState.modifiers[key] = true;
1858
+ if (key === "AltGraph") {
1859
+ const ctrlKeyDef = (_a = config.keyboardMap.find((k) => k.key === "Control")) != null ? _a : { key: "Control", code: "Control" };
1860
+ dispatchUIEvent(config, element, "keydown", getKeyEventProps(ctrlKeyDef));
1854
1861
  }
1855
- },
1856
- {
1857
- matches: (keyDef) => modifierLocks.includes(keyDef.key),
1858
- handle: (keyDef, element, { keyboardState }) => {
1859
- const key = keyDef.key;
1860
- keyboardState.modifierPhase[key] = keyboardState.modifiers[key];
1861
- if (!keyboardState.modifierPhase[key]) {
1862
- keyboardState.modifiers[key] = true;
1863
- }
1862
+ } else if (isModifierLock(key)) {
1863
+ config.keyboardState.modifierPhase[key] = config.keyboardState.modifiers[key];
1864
+ if (!config.keyboardState.modifierPhase[key]) {
1865
+ config.keyboardState.modifiers[key] = true;
1864
1866
  }
1865
1867
  }
1866
- ];
1867
- var preKeyupBehavior = [
1868
- {
1869
- matches: (keyDef) => modifierKeys.includes(keyDef.key),
1870
- handle: (keyDef, element, { keyboardState }) => {
1871
- keyboardState.modifiers[keyDef.key] = false;
1872
- }
1873
- },
1874
- {
1875
- matches: (keyDef) => modifierLocks.includes(keyDef.key),
1876
- handle: (keyDef, element, { keyboardState }) => {
1877
- const key = keyDef.key;
1878
- if (keyboardState.modifierPhase[key]) {
1879
- keyboardState.modifiers[key] = false;
1880
- }
1868
+ }
1869
+ function preKeyupBehavior(config, { key }) {
1870
+ if (isModifierKey(key)) {
1871
+ config.keyboardState.modifiers[key] = false;
1872
+ } else if (isModifierLock(key)) {
1873
+ if (config.keyboardState.modifierPhase[key]) {
1874
+ config.keyboardState.modifiers[key] = false;
1881
1875
  }
1882
1876
  }
1883
- ];
1884
- var postKeyupBehavior = [
1885
- {
1886
- matches: (keyDef) => keyDef.key === "AltGraph",
1887
- handle: (keyDef, element, { keyboardMap, keyboardState }) => {
1888
- var _a;
1889
- const ctrlKeyDef = (_a = keyboardMap.find((k) => k.key === "Control")) != null ? _a : { key: "Control", code: "Control" };
1890
- import_dom9.fireEvent.keyUp(element, getKeyEventProps(ctrlKeyDef, keyboardState));
1891
- }
1877
+ }
1878
+ function postKeyupBehavior(config, { key }, element) {
1879
+ var _a;
1880
+ if (key === "AltGraph") {
1881
+ const ctrlKeyDef = (_a = config.keyboardMap.find((k) => k.key === "Control")) != null ? _a : { key: "Control", code: "Control" };
1882
+ dispatchUIEvent(config, element, "keyup", getKeyEventProps(ctrlKeyDef));
1892
1883
  }
1893
- ];
1894
-
1895
- // src/keyboard/plugins/index.ts
1896
- var preKeydownBehavior2 = [
1897
- ...preKeydownBehavior
1898
- ];
1899
- var keydownBehavior5 = [
1900
- ...keydownBehavior,
1901
- ...keydownBehavior2,
1902
- ...keydownBehavior3,
1903
- ...keydownBehavior4
1904
- ];
1905
- var keypressBehavior3 = [
1906
- ...keypressBehavior2,
1907
- ...keypressBehavior
1908
- ];
1909
- var preKeyupBehavior2 = [
1910
- ...preKeyupBehavior
1911
- ];
1912
- var keyupBehavior2 = [...keyupBehavior];
1913
- var postKeyupBehavior2 = [
1914
- ...postKeyupBehavior
1915
- ];
1884
+ }
1916
1885
 
1917
1886
  // src/keyboard/keyboardAction.ts
1918
1887
  async function keyboardAction(config, actions) {
@@ -1963,39 +1932,23 @@ async function keydown(keyDef, getCurrentElement, config) {
1963
1932
  config.keyboardState.carryChar = "";
1964
1933
  }
1965
1934
  config.keyboardState.activeElement = element;
1966
- applyPlugins(preKeydownBehavior2, keyDef, element, config);
1967
- const unpreventedDefault = import_dom10.fireEvent.keyDown(element, getKeyEventProps(keyDef, config.keyboardState));
1935
+ preKeydownBehavior(config, keyDef, element);
1936
+ const unpreventedDefault = dispatchUIEvent(config, element, "keydown", getKeyEventProps(keyDef));
1968
1937
  config.keyboardState.pressed.push({ keyDef, unpreventedDefault });
1969
- if (unpreventedDefault) {
1970
- applyPlugins(keydownBehavior5, keyDef, getCurrentElement(), config);
1971
- }
1972
1938
  return unpreventedDefault;
1973
1939
  }
1974
1940
  async function keypress(keyDef, getCurrentElement, config) {
1975
1941
  const element = getCurrentElement();
1976
- const unpreventedDefault = import_dom10.fireEvent.keyPress(element, __spreadProps(__spreadValues({}, getKeyEventProps(keyDef, config.keyboardState)), {
1942
+ dispatchUIEvent(config, element, "keypress", __spreadProps(__spreadValues({}, getKeyEventProps(keyDef)), {
1977
1943
  charCode: keyDef.key === "Enter" ? 13 : String(keyDef.key).charCodeAt(0)
1978
1944
  }));
1979
- if (unpreventedDefault) {
1980
- applyPlugins(keypressBehavior3, keyDef, getCurrentElement(), config);
1981
- }
1982
1945
  }
1983
1946
  async function keyup(keyDef, getCurrentElement, config, unprevented) {
1984
1947
  const element = getCurrentElement();
1985
- applyPlugins(preKeyupBehavior2, keyDef, element, config);
1986
- const unpreventedDefault = import_dom10.fireEvent.keyUp(element, getKeyEventProps(keyDef, config.keyboardState));
1987
- if (unprevented && unpreventedDefault) {
1988
- applyPlugins(keyupBehavior2, keyDef, getCurrentElement(), config);
1989
- }
1948
+ preKeyupBehavior(config, keyDef);
1949
+ dispatchUIEvent(config, element, "keyup", getKeyEventProps(keyDef), !unprevented);
1990
1950
  config.keyboardState.pressed = config.keyboardState.pressed.filter((k) => k.keyDef !== keyDef);
1991
- applyPlugins(postKeyupBehavior2, keyDef, element, config);
1992
- }
1993
- function applyPlugins(pluginCollection, keyDef, element, config) {
1994
- const plugin = pluginCollection.find((p) => p.matches(keyDef, element, config));
1995
- if (plugin) {
1996
- plugin.handle(keyDef, element, config);
1997
- }
1998
- return !!plugin;
1951
+ postKeyupBehavior(config, keyDef, element);
1999
1952
  }
2000
1953
  function hasKeyPress(keyDef, config) {
2001
1954
  var _a;
@@ -2014,7 +1967,7 @@ function parseKeyDef(keyboardMap, text) {
2014
1967
  releasePrevious,
2015
1968
  releaseSelf = true,
2016
1969
  repeat
2017
- } = readNextDescriptor(text);
1970
+ } = readNextDescriptor(text, "keyboard");
2018
1971
  const keyDef = (_a = keyboardMap.find((def) => {
2019
1972
  var _a2, _b;
2020
1973
  if (type3 === "[") {
@@ -2036,8 +1989,7 @@ function parseKeyDef(keyboardMap, text) {
2036
1989
 
2037
1990
  // src/keyboard/index.ts
2038
1991
  async function keyboard(text) {
2039
- const { keyboardMap } = this[Config];
2040
- const actions = parseKeyDef(keyboardMap, text);
1992
+ const actions = parseKeyDef(this[Config].keyboardMap, text);
2041
1993
  return keyboardAction(this[Config], actions);
2042
1994
  }
2043
1995
  function createKeyboardState() {
@@ -2072,7 +2024,7 @@ function parseKeyDef2(pointerMap, keys) {
2072
2024
  consumedLength,
2073
2025
  releasePrevious,
2074
2026
  releaseSelf = true
2075
- } = readNextDescriptor(keys);
2027
+ } = readNextDescriptor(keys, "pointer");
2076
2028
  const keyDef = pointerMap.find((p) => p.name === descriptor);
2077
2029
  if (keyDef) {
2078
2030
  defs.push({ keyDef, releasePrevious, releaseSelf });
@@ -2082,6 +2034,34 @@ function parseKeyDef2(pointerMap, keys) {
2082
2034
  return defs;
2083
2035
  }
2084
2036
 
2037
+ // src/pointer/firePointerEvents.ts
2038
+ function firePointerEvent(config, target, type3, {
2039
+ pointerType,
2040
+ button,
2041
+ coords,
2042
+ pointerId,
2043
+ isPrimary,
2044
+ clickCount
2045
+ }) {
2046
+ const init = __spreadValues({}, coords);
2047
+ if (type3 === "click" || type3.startsWith("pointer")) {
2048
+ init.pointerId = pointerId;
2049
+ init.pointerType = pointerType;
2050
+ }
2051
+ if (["pointerdown", "pointerup"].includes(type3)) {
2052
+ init.isPrimary = isPrimary;
2053
+ }
2054
+ init.button = getMouseButton(button != null ? button : 0);
2055
+ init.buttons = getMouseButtons(...config.pointerState.pressed.filter((p) => p.keyDef.pointerType === pointerType).map((p) => {
2056
+ var _a;
2057
+ return (_a = p.keyDef.button) != null ? _a : 0;
2058
+ }));
2059
+ if (["mousedown", "mouseup", "click", "dblclick", "contextmenu"].includes(type3)) {
2060
+ init.detail = clickCount;
2061
+ }
2062
+ return dispatchUIEvent(config, target, type3, init);
2063
+ }
2064
+
2085
2065
  // src/pointer/resolveSelectionTarget.ts
2086
2066
  function resolveSelectionTarget({
2087
2067
  target,
@@ -2128,7 +2108,7 @@ function findNodeAtTextOffset(node, offset, isRoot = true) {
2128
2108
 
2129
2109
  // src/pointer/pointerMove.ts
2130
2110
  async function pointerMove(config, { pointerName = "mouse", target, coords, node, offset }) {
2131
- const { pointerState, keyboardState } = config;
2111
+ const { pointerState } = config;
2132
2112
  if (!(pointerName in pointerState.position)) {
2133
2113
  throw new Error(`Trying to move pointer "${pointerName}" which does not exist.`);
2134
2114
  }
@@ -2203,9 +2183,7 @@ async function pointerMove(config, { pointerName = "mouse", target, coords, node
2203
2183
  }
2204
2184
  }
2205
2185
  function fire(eventTarget, type3, eventCoords) {
2206
- return firePointerEvent(eventTarget, type3, {
2207
- pointerState,
2208
- keyboardState,
2186
+ return firePointerEvent(config, eventTarget, type3, {
2209
2187
  coords: eventCoords,
2210
2188
  pointerId,
2211
2189
  pointerType
@@ -2237,7 +2215,7 @@ function down(config, pointerName, { keyDef, node, offset, target, coords }, tar
2237
2215
  var _a, _b, _c;
2238
2216
  setLevelRef(config, 2 /* Trigger */);
2239
2217
  assertPointerEvents(config, target);
2240
- const { pointerState, keyboardState } = config;
2218
+ const { pointerState } = config;
2241
2219
  const { name, pointerType, button } = keyDef;
2242
2220
  const pointerId = pointerType === "mouse" ? 1 : getNextPointerId(pointerState);
2243
2221
  pointerState.position[pointerName] = __spreadProps(__spreadValues({}, pointerState.position[pointerName]), {
@@ -2299,9 +2277,7 @@ function down(config, pointerName, { keyDef, node, offset, target, coords }, tar
2299
2277
  }
2300
2278
  return pressObj;
2301
2279
  function fire(type3) {
2302
- return firePointerEvent(target, type3, {
2303
- pointerState,
2304
- keyboardState,
2280
+ return firePointerEvent(config, target, type3, {
2305
2281
  button,
2306
2282
  clickCount,
2307
2283
  coords,
@@ -2318,10 +2294,9 @@ function up(config, pointerName, {
2318
2294
  node,
2319
2295
  offset
2320
2296
  }, pressed, targetIsDisabled) {
2321
- var _a;
2322
2297
  setLevelRef(config, 2 /* Trigger */);
2323
2298
  assertPointerEvents(config, target);
2324
- const { pointerState, keyboardState } = config;
2299
+ const { pointerState } = config;
2325
2300
  pointerState.pressed = pointerState.pressed.filter((p) => p !== pressed);
2326
2301
  const { isMultiTouch, isPrimary, pointerId, clickCount } = pressed;
2327
2302
  let { unpreventedDefault } = pressed;
@@ -2362,21 +2337,15 @@ function up(config, pointerName, {
2362
2337
  unpreventedDefault = fire("mouseup") && unpreventedDefault;
2363
2338
  const canClick = pointerType !== "mouse" || button === "primary";
2364
2339
  if (canClick && target === pressed.downTarget) {
2365
- const unpreventedClick = fire("click");
2340
+ fire("click");
2366
2341
  if (clickCount === 2) {
2367
2342
  fire("dblclick");
2368
2343
  }
2369
- const control = (_a = target.closest("label")) == null ? void 0 : _a.control;
2370
- if (unpreventedClick && control && isFocusable(control)) {
2371
- focus(control);
2372
- }
2373
2344
  }
2374
2345
  }
2375
2346
  }
2376
2347
  function fire(type3) {
2377
- return firePointerEvent(target, type3, {
2378
- pointerState,
2379
- keyboardState,
2348
+ return firePointerEvent(config, target, type3, {
2380
2349
  button,
2381
2350
  clickCount,
2382
2351
  coords,
@@ -2394,8 +2363,7 @@ function mousedownDefaultBehavior({
2394
2363
  node,
2395
2364
  offset
2396
2365
  }) {
2397
- var _a;
2398
- focus((_a = findClosest(target, isFocusable)) != null ? _a : target.ownerDocument.body);
2366
+ focus(target);
2399
2367
  if (!targetIsDisabled) {
2400
2368
  const hasValue = isElementType(target, ["input", "textarea"]);
2401
2369
  const text = String(hasValue ? getUIValue(target) : target.textContent);
@@ -2473,10 +2441,10 @@ function getPrevTarget(pointerName, state) {
2473
2441
  }
2474
2442
 
2475
2443
  // src/pointer/index.ts
2476
- async function pointer(input) {
2444
+ async function pointer(input2) {
2477
2445
  const { pointerMap } = this[Config];
2478
2446
  const actions = [];
2479
- (Array.isArray(input) ? input : [input]).forEach((actionInput) => {
2447
+ (Array.isArray(input2) ? input2 : [input2]).forEach((actionInput) => {
2480
2448
  if (typeof actionInput === "string") {
2481
2449
  actions.push(...parseKeyDef2(pointerMap, actionInput));
2482
2450
  } else if ("keys" in actionInput) {
@@ -2567,7 +2535,6 @@ async function tab({
2567
2535
  }
2568
2536
 
2569
2537
  // src/clipboard/copy.ts
2570
- var import_dom11 = require("@testing-library/dom");
2571
2538
  async function copy() {
2572
2539
  var _a;
2573
2540
  const doc = this[Config].document;
@@ -2576,59 +2543,42 @@ async function copy() {
2576
2543
  if (clipboardData.items.length === 0) {
2577
2544
  return;
2578
2545
  }
2579
- import_dom11.fireEvent.copy(target, {
2546
+ if (this.dispatchUIEvent(target, "copy", {
2580
2547
  clipboardData
2581
- });
2582
- if (this[Config].writeToClipboard) {
2548
+ }) && this[Config].writeToClipboard) {
2583
2549
  await writeDataTransferToClipboard(doc, clipboardData);
2584
2550
  }
2585
2551
  return clipboardData;
2586
2552
  }
2587
2553
 
2588
2554
  // src/clipboard/cut.ts
2589
- var import_dom12 = require("@testing-library/dom");
2590
2555
  async function cut() {
2591
- var _a, _b;
2556
+ var _a;
2592
2557
  const doc = this[Config].document;
2593
2558
  const target = (_a = doc.activeElement) != null ? _a : doc.body;
2594
2559
  const clipboardData = copySelection(target);
2595
2560
  if (clipboardData.items.length === 0) {
2596
2561
  return;
2597
2562
  }
2598
- import_dom12.fireEvent.cut(target, {
2563
+ if (this.dispatchUIEvent(target, "cut", {
2599
2564
  clipboardData
2600
- });
2601
- if (isEditable(target)) {
2602
- (_b = prepareInput("", target, "deleteByCut")) == null ? void 0 : _b.commit();
2603
- }
2604
- if (this[Config].writeToClipboard) {
2605
- await writeDataTransferToClipboard(doc, clipboardData);
2565
+ }) && this[Config].writeToClipboard) {
2566
+ await writeDataTransferToClipboard(target.ownerDocument, clipboardData);
2606
2567
  }
2607
2568
  return clipboardData;
2608
2569
  }
2609
2570
 
2610
2571
  // src/clipboard/paste.ts
2611
- var import_dom13 = require("@testing-library/dom");
2612
2572
  async function paste(clipboardData) {
2613
2573
  var _a, _b;
2614
2574
  const doc = this[Config].document;
2615
2575
  const target = (_a = doc.activeElement) != null ? _a : doc.body;
2616
- const data = (_b = typeof clipboardData === "string" ? getClipboardDataFromString(clipboardData) : clipboardData) != null ? _b : await readDataTransferFromClipboard(doc).catch(() => {
2576
+ const dataTransfer = (_b = typeof clipboardData === "string" ? getClipboardDataFromString(clipboardData) : clipboardData) != null ? _b : await readDataTransferFromClipboard(doc).catch(() => {
2617
2577
  throw new Error("`userEvent.paste()` without `clipboardData` requires the `ClipboardAPI` to be available.");
2618
2578
  });
2619
- return pasteImpl(target, data);
2620
- }
2621
- function pasteImpl(target, clipboardData) {
2622
- var _a;
2623
- import_dom13.fireEvent.paste(target, {
2624
- clipboardData
2579
+ this.dispatchUIEvent(target, "paste", {
2580
+ clipboardData: dataTransfer
2625
2581
  });
2626
- if (isEditable(target)) {
2627
- const data = clipboardData.getData("text").substr(0, getSpaceUntilMaxLength(target));
2628
- if (data) {
2629
- (_a = prepareInput(data, target, "insertFromPaste")) == null ? void 0 : _a.commit();
2630
- }
2631
- }
2632
2582
  }
2633
2583
  function getClipboardDataFromString(text) {
2634
2584
  const dt = createDataTransfer();
@@ -2638,7 +2588,6 @@ function getClipboardDataFromString(text) {
2638
2588
 
2639
2589
  // src/utility/clear.ts
2640
2590
  async function clear(element) {
2641
- var _a;
2642
2591
  if (!isEditable(element) || isDisabled(element)) {
2643
2592
  throw new Error("clear()` is only supported on editable elements.");
2644
2593
  }
@@ -2650,11 +2599,11 @@ async function clear(element) {
2650
2599
  if (!isAllSelected(element)) {
2651
2600
  throw new Error("The element content to be cleared could not be selected.");
2652
2601
  }
2653
- (_a = prepareInput("", element, "deleteContentBackward")) == null ? void 0 : _a.commit();
2602
+ input(this[Config], element, "", "deleteContentBackward");
2654
2603
  }
2655
2604
 
2656
2605
  // src/utility/selectOptions.ts
2657
- var import_dom14 = require("@testing-library/dom");
2606
+ var import_dom4 = require("@testing-library/dom");
2658
2607
  async function selectOptions(select, values) {
2659
2608
  return selectOptionsBase.call(this, true, select, values);
2660
2609
  }
@@ -2663,7 +2612,7 @@ async function deselectOptions(select, values) {
2663
2612
  }
2664
2613
  async function selectOptionsBase(newValue, select, values) {
2665
2614
  if (!newValue && !select.multiple) {
2666
- throw (0, import_dom14.getConfig)().getElementError(`Unable to deselect an option in a non-multiple select. Use selectOptions to change the selection instead.`, select);
2615
+ throw (0, import_dom4.getConfig)().getElementError(`Unable to deselect an option in a non-multiple select. Use selectOptions to change the selection instead.`, select);
2667
2616
  }
2668
2617
  const valArray = Array.isArray(values) ? values : [values];
2669
2618
  const allOptions = Array.from(select.querySelectorAll('option, [role="option"]'));
@@ -2675,34 +2624,43 @@ async function selectOptionsBase(newValue, select, values) {
2675
2624
  if (matchingOption) {
2676
2625
  return matchingOption;
2677
2626
  } else {
2678
- throw (0, import_dom14.getConfig)().getElementError(`Value "${String(val)}" not found in options`, select);
2627
+ throw (0, import_dom4.getConfig)().getElementError(`Value "${String(val)}" not found in options`, select);
2679
2628
  }
2680
2629
  }
2681
2630
  }).filter((option) => !isDisabled(option));
2682
2631
  if (isDisabled(select) || !selectedOptions.length)
2683
2632
  return;
2633
+ const selectOption = (option) => {
2634
+ option.selected = newValue;
2635
+ this.dispatchUIEvent(select, "input", {
2636
+ bubbles: true,
2637
+ cancelable: false,
2638
+ composed: true
2639
+ });
2640
+ this.dispatchUIEvent(select, "change");
2641
+ };
2684
2642
  if (isElementType(select, "select")) {
2685
2643
  if (select.multiple) {
2686
2644
  for (const option of selectedOptions) {
2687
2645
  const withPointerEvents = this[Config].pointerEventsCheck === 0 ? true : hasPointerEvents(option);
2688
2646
  if (withPointerEvents) {
2689
- import_dom14.fireEvent.pointerOver(option);
2690
- import_dom14.fireEvent.pointerEnter(select);
2691
- import_dom14.fireEvent.mouseOver(option);
2692
- import_dom14.fireEvent.mouseEnter(select);
2693
- import_dom14.fireEvent.pointerMove(option);
2694
- import_dom14.fireEvent.mouseMove(option);
2695
- import_dom14.fireEvent.pointerDown(option);
2696
- import_dom14.fireEvent.mouseDown(option);
2647
+ this.dispatchUIEvent(option, "pointerover");
2648
+ this.dispatchUIEvent(select, "pointerenter");
2649
+ this.dispatchUIEvent(option, "mouseover");
2650
+ this.dispatchUIEvent(select, "mouseenter");
2651
+ this.dispatchUIEvent(option, "pointermove");
2652
+ this.dispatchUIEvent(option, "mousemove");
2653
+ this.dispatchUIEvent(option, "pointerdown");
2654
+ this.dispatchUIEvent(option, "mousedown");
2697
2655
  }
2698
2656
  focus(select);
2699
2657
  if (withPointerEvents) {
2700
- import_dom14.fireEvent.pointerUp(option);
2701
- import_dom14.fireEvent.mouseUp(option);
2658
+ this.dispatchUIEvent(option, "pointerup");
2659
+ this.dispatchUIEvent(option, "mouseup");
2702
2660
  }
2703
2661
  selectOption(option);
2704
2662
  if (withPointerEvents) {
2705
- import_dom14.fireEvent.click(option);
2663
+ this.dispatchUIEvent(option, "click");
2706
2664
  }
2707
2665
  }
2708
2666
  } else if (selectedOptions.length === 1) {
@@ -2714,16 +2672,16 @@ async function selectOptionsBase(newValue, select, values) {
2714
2672
  }
2715
2673
  selectOption(selectedOptions[0]);
2716
2674
  if (withPointerEvents) {
2717
- import_dom14.fireEvent.pointerOver(select);
2718
- import_dom14.fireEvent.pointerEnter(select);
2719
- import_dom14.fireEvent.mouseOver(select);
2720
- import_dom14.fireEvent.mouseEnter(select);
2721
- import_dom14.fireEvent.pointerUp(select);
2722
- import_dom14.fireEvent.mouseUp(select);
2723
- import_dom14.fireEvent.click(select);
2675
+ this.dispatchUIEvent(select, "pointerover");
2676
+ this.dispatchUIEvent(select, "pointerenter");
2677
+ this.dispatchUIEvent(select, "mouseover");
2678
+ this.dispatchUIEvent(select, "mouseenter");
2679
+ this.dispatchUIEvent(select, "pointerup");
2680
+ this.dispatchUIEvent(select, "mouseup");
2681
+ this.dispatchUIEvent(select, "click");
2724
2682
  }
2725
2683
  } else {
2726
- throw (0, import_dom14.getConfig)().getElementError(`Cannot select multiple options on a non-multiple select`, select);
2684
+ throw (0, import_dom4.getConfig)().getElementError(`Cannot select multiple options on a non-multiple select`, select);
2727
2685
  }
2728
2686
  } else if (select.getAttribute("role") === "listbox") {
2729
2687
  for (const option of selectedOptions) {
@@ -2731,16 +2689,7 @@ async function selectOptionsBase(newValue, select, values) {
2731
2689
  await this.unhover(option);
2732
2690
  }
2733
2691
  } else {
2734
- throw (0, import_dom14.getConfig)().getElementError(`Cannot select options on elements that are neither select nor listbox elements`, select);
2735
- }
2736
- function selectOption(option) {
2737
- option.selected = newValue;
2738
- (0, import_dom14.fireEvent)(select, (0, import_dom14.createEvent)("input", select, {
2739
- bubbles: true,
2740
- cancelable: false,
2741
- composed: true
2742
- }));
2743
- import_dom14.fireEvent.change(select);
2692
+ throw (0, import_dom4.getConfig)().getElementError(`Cannot select options on elements that are neither select nor listbox elements`, select);
2744
2693
  }
2745
2694
  }
2746
2695
 
@@ -2766,28 +2715,29 @@ async function type(element, text, {
2766
2715
  }
2767
2716
 
2768
2717
  // src/utility/upload.ts
2769
- var import_dom15 = require("@testing-library/dom");
2770
2718
  async function upload(element, fileOrFiles) {
2771
- var _a;
2772
- const input = isElementType(element, "label") ? element.control : element;
2773
- if (!input || !isElementType(input, "input", { type: "file" })) {
2774
- throw new TypeError(`The ${input === element ? "given" : "associated"} ${input == null ? void 0 : input.tagName} element does not accept file uploads`);
2719
+ const input2 = isElementType(element, "label") ? element.control : element;
2720
+ if (!input2 || !isElementType(input2, "input", { type: "file" })) {
2721
+ throw new TypeError(`The ${input2 === element ? "given" : "associated"} ${input2 == null ? void 0 : input2.tagName} element does not accept file uploads`);
2775
2722
  }
2776
2723
  if (isDisabled(element))
2777
2724
  return;
2725
+ const files = (Array.isArray(fileOrFiles) ? fileOrFiles : [fileOrFiles]).filter((file) => !this[Config].applyAccept || isAcceptableFile(file, input2.accept)).slice(0, input2.multiple ? void 0 : 1);
2726
+ const fileDialog = () => {
2727
+ var _a;
2728
+ if (files.length === ((_a = input2.files) == null ? void 0 : _a.length) && files.every((f, i) => {
2729
+ var _a2;
2730
+ return f === ((_a2 = input2.files) == null ? void 0 : _a2.item(i));
2731
+ })) {
2732
+ return;
2733
+ }
2734
+ setFiles(input2, createFileList(files));
2735
+ this.dispatchUIEvent(input2, "input");
2736
+ this.dispatchUIEvent(input2, "change");
2737
+ };
2738
+ input2.addEventListener("fileDialog", fileDialog);
2778
2739
  await this.click(element);
2779
- const files = (Array.isArray(fileOrFiles) ? fileOrFiles : [fileOrFiles]).filter((file) => !this[Config].applyAccept || isAcceptableFile(file, input.accept)).slice(0, input.multiple ? void 0 : 1);
2780
- blur(element);
2781
- focus(element);
2782
- if (files.length === ((_a = input.files) == null ? void 0 : _a.length) && files.every((f, i) => {
2783
- var _a2;
2784
- return f === ((_a2 = input.files) == null ? void 0 : _a2.item(i));
2785
- })) {
2786
- return;
2787
- }
2788
- setFiles(input, createFileList(files));
2789
- import_dom15.fireEvent.input(input);
2790
- import_dom15.fireEvent.change(input);
2740
+ input2.removeEventListener("fileDialog", fileDialog);
2791
2741
  }
2792
2742
  function isAcceptableFile(file, accept) {
2793
2743
  if (!accept) {
@@ -2805,30 +2755,35 @@ function isAcceptableFile(file, accept) {
2805
2755
  }
2806
2756
 
2807
2757
  // src/setup/wrapAsync.ts
2808
- var import_dom16 = require("@testing-library/dom");
2758
+ var import_dom5 = require("@testing-library/dom");
2809
2759
  function wrapAsync(implementation) {
2810
- return (0, import_dom16.getConfig)().asyncWrapper(implementation);
2760
+ return (0, import_dom5.getConfig)().asyncWrapper(implementation);
2811
2761
  }
2812
2762
 
2813
2763
  // src/setup/setup.ts
2764
+ function createConfig(options = {}, defaults = defaultOptionsSetup, node) {
2765
+ const document2 = getDocument(options, node);
2766
+ const {
2767
+ keyboardState = createKeyboardState(),
2768
+ pointerState = createPointerState(document2)
2769
+ } = options;
2770
+ return __spreadProps(__spreadValues(__spreadValues({}, defaults), options), {
2771
+ document: document2,
2772
+ keyboardState,
2773
+ pointerState
2774
+ });
2775
+ }
2814
2776
  function setupMain(options = {}) {
2815
2777
  var _a;
2816
- const doc = getDocument(options);
2817
- prepareDocument(doc);
2818
- const view = (_a = doc.defaultView) != null ? _a : window;
2778
+ const config = createConfig(options);
2779
+ prepareDocument(config.document);
2780
+ const view = (_a = config.document.defaultView) != null ? _a : window;
2819
2781
  attachClipboardStubToView(view);
2820
- return doSetup(__spreadProps(__spreadValues(__spreadValues({}, defaultOptionsSetup), options), {
2821
- keyboardState: createKeyboardState(),
2822
- pointerState: createPointerState(doc)
2823
- }));
2782
+ return doSetup(config);
2824
2783
  }
2825
2784
  function setupDirect(options = {}, node) {
2826
- const doc = getDocument(options, node);
2827
- prepareDocument(doc);
2828
- const config = __spreadValues(__spreadValues({
2829
- keyboardState: createKeyboardState(),
2830
- pointerState: createPointerState(doc)
2831
- }, defaultOptionsDirect), options);
2785
+ const config = createConfig(options, defaultOptionsDirect, node);
2786
+ prepareDocument(config.document);
2832
2787
  return {
2833
2788
  config,
2834
2789
  api: doSetup(config)
@@ -2847,7 +2802,8 @@ function wrapAndBindImpl(instance, impl) {
2847
2802
  }
2848
2803
  function doSetup(config) {
2849
2804
  const instance = __spreadValues({
2850
- [Config]: config
2805
+ [Config]: config,
2806
+ dispatchUIEvent: bindDispatchUIEvent(config)
2851
2807
  }, api_exports);
2852
2808
  return __spreadProps(__spreadValues({}, Object.fromEntries(Object.entries(api_exports).map(([name, api]) => [
2853
2809
  name,
@@ -2906,9 +2862,9 @@ async function keyboard2(text, options = {}) {
2906
2862
  const { config, api } = setupDirect(options);
2907
2863
  return api.keyboard(text).then(() => config.keyboardState);
2908
2864
  }
2909
- async function pointer2(input, options = {}) {
2865
+ async function pointer2(input2, options = {}) {
2910
2866
  const { config, api } = setupDirect(options);
2911
- return api.pointer(input).then(() => config.pointerState);
2867
+ return api.pointer(input2).then(() => config.pointerState);
2912
2868
  }
2913
2869
  function paste2(clipboardData, options) {
2914
2870
  return setupDirect(options).api.paste(clipboardData);
@@ -2938,4 +2894,3 @@ function tab2(options = {}) {
2938
2894
  var userEvent = __spreadProps(__spreadValues({}, directApi_exports), {
2939
2895
  setup: setupMain
2940
2896
  });
2941
- module.exports = __toCommonJS(src_exports);