@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.mjs CHANGED
@@ -43,187 +43,6 @@ var __async = (__this, __arguments, generator) => {
43
43
  });
44
44
  };
45
45
 
46
- // src/document/index.ts
47
- import { fireEvent } from "@testing-library/dom";
48
-
49
- // src/document/interceptor.ts
50
- var Interceptor = Symbol("Interceptor for programmatical calls");
51
- function prepareInterceptor(element, propName, interceptorImpl) {
52
- const prototypeDescriptor = Object.getOwnPropertyDescriptor(element.constructor.prototype, propName);
53
- const objectDescriptor = Object.getOwnPropertyDescriptor(element, propName);
54
- const target = (prototypeDescriptor == null ? void 0 : prototypeDescriptor.set) ? "set" : "value";
55
- if (typeof (prototypeDescriptor == null ? void 0 : prototypeDescriptor[target]) !== "function" || prototypeDescriptor[target][Interceptor]) {
56
- return;
57
- }
58
- function intercept(...args) {
59
- const { applyNative = true, realArgs } = interceptorImpl.call(this, ...args);
60
- const realFunc = (!applyNative && objectDescriptor || prototypeDescriptor)[target];
61
- if (target === "set") {
62
- realFunc.call(this, realArgs);
63
- } else {
64
- realFunc.call(this, ...realArgs);
65
- }
66
- }
67
- ;
68
- intercept[Interceptor] = Interceptor;
69
- Object.defineProperty(element, propName, __spreadProps(__spreadValues({}, objectDescriptor != null ? objectDescriptor : prototypeDescriptor), {
70
- [target]: intercept
71
- }));
72
- }
73
-
74
- // src/document/selection.ts
75
- var UISelection = Symbol("Displayed selection in UI");
76
- function prepareSelectionInterceptor(element) {
77
- prepareInterceptor(element, "setSelectionRange", function interceptorImpl(start, end, direction = "none") {
78
- const isUI = start && typeof start === "object" && start[UISelection];
79
- if (!isUI) {
80
- this[UISelection] = void 0;
81
- }
82
- return {
83
- realArgs: [Number(start), end, direction]
84
- };
85
- });
86
- prepareInterceptor(element, "selectionStart", function interceptorImpl(v) {
87
- this[UISelection] = void 0;
88
- return { realArgs: v };
89
- });
90
- prepareInterceptor(element, "selectionEnd", function interceptorImpl(v) {
91
- this[UISelection] = void 0;
92
- return { realArgs: v };
93
- });
94
- }
95
- function setUISelection(element, {
96
- focusOffset: focusOffsetParam,
97
- anchorOffset: anchorOffsetParam = focusOffsetParam
98
- }, mode = "replace") {
99
- const valueLength = getUIValue(element).length;
100
- const sanitizeOffset = (o) => Math.max(0, Math.min(valueLength, o));
101
- const anchorOffset = mode === "replace" || element[UISelection] === void 0 ? sanitizeOffset(anchorOffsetParam) : element[UISelection].anchorOffset;
102
- const focusOffset = sanitizeOffset(focusOffsetParam);
103
- const startOffset = Math.min(anchorOffset, focusOffset);
104
- const endOffset = Math.max(anchorOffset, focusOffset);
105
- element[UISelection] = {
106
- anchorOffset,
107
- focusOffset
108
- };
109
- if (element.selectionStart === startOffset && element.selectionEnd === endOffset) {
110
- return;
111
- }
112
- const startObj = new Number(startOffset);
113
- startObj[UISelection] = UISelection;
114
- try {
115
- element.setSelectionRange(startObj, endOffset);
116
- } catch (e) {
117
- }
118
- }
119
- function getUISelection(element) {
120
- var _a, _b, _c;
121
- const sel = (_c = element[UISelection]) != null ? _c : {
122
- anchorOffset: (_a = element.selectionStart) != null ? _a : 0,
123
- focusOffset: (_b = element.selectionEnd) != null ? _b : 0
124
- };
125
- return __spreadProps(__spreadValues({}, sel), {
126
- startOffset: Math.min(sel.anchorOffset, sel.focusOffset),
127
- endOffset: Math.max(sel.anchorOffset, sel.focusOffset)
128
- });
129
- }
130
- function clearUISelection(element) {
131
- element[UISelection] = void 0;
132
- }
133
-
134
- // src/document/value.ts
135
- var UIValue = Symbol("Displayed value in UI");
136
- var InitialValue = Symbol("Initial value to compare on blur");
137
- var TrackChanges = Symbol("Track programmatic changes for React workaround");
138
- function valueInterceptor(v) {
139
- const isUI = typeof v === "object" && v[UIValue];
140
- this[UIValue] = isUI ? String(v) : void 0;
141
- if (!isUI) {
142
- trackValue(this, String(v));
143
- this[InitialValue] = String(v);
144
- clearUISelection(this);
145
- }
146
- return {
147
- applyNative: !!isUI,
148
- realArgs: String(v)
149
- };
150
- }
151
- function prepareValueInterceptor(element) {
152
- prepareInterceptor(element, "value", valueInterceptor);
153
- }
154
- function setUIValue(element, value) {
155
- element.value = {
156
- [UIValue]: UIValue,
157
- toString: () => value
158
- };
159
- }
160
- function getUIValue(element) {
161
- return element[UIValue] === void 0 ? element.value : String(element[UIValue]);
162
- }
163
- function setInitialValue(element) {
164
- element[InitialValue] = element.value;
165
- }
166
- function getInitialValue(element) {
167
- return element[InitialValue];
168
- }
169
- function startTrackValue(element) {
170
- element[TrackChanges] = [];
171
- }
172
- function trackValue(element, v) {
173
- var _a;
174
- (_a = element[TrackChanges]) == null ? void 0 : _a.push(v);
175
- }
176
- function endTrackValue(element) {
177
- const tracked = element[TrackChanges];
178
- element[TrackChanges] = void 0;
179
- return tracked;
180
- }
181
-
182
- // src/document/index.ts
183
- var isPrepared = Symbol("Node prepared with document state workarounds");
184
- function prepareDocument(document2) {
185
- if (document2[isPrepared]) {
186
- return;
187
- }
188
- document2.addEventListener("focus", (e) => {
189
- const el = e.target;
190
- prepareElement(el);
191
- }, {
192
- capture: true,
193
- passive: true
194
- });
195
- if (document2.activeElement) {
196
- prepareElement(document2.activeElement);
197
- }
198
- document2.addEventListener("blur", (e) => {
199
- const el = e.target;
200
- const initialValue = getInitialValue(el);
201
- if (typeof initialValue === "string" && el.value !== initialValue) {
202
- fireEvent.change(el);
203
- }
204
- }, {
205
- capture: true,
206
- passive: true
207
- });
208
- document2[isPrepared] = isPrepared;
209
- }
210
- function prepareElement(el) {
211
- if ("value" in el) {
212
- setInitialValue(el);
213
- }
214
- if (el[isPrepared]) {
215
- return;
216
- }
217
- if ("value" in el) {
218
- prepareValueInterceptor(el);
219
- prepareSelectionInterceptor(el);
220
- }
221
- el[isPrepared] = isPrepared;
222
- }
223
-
224
- // src/keyboard/keyboardAction.ts
225
- import { fireEvent as fireEvent9 } from "@testing-library/dom";
226
-
227
46
  // src/utils/misc/isElementType.ts
228
47
  function isElementType(element, tag, props) {
229
48
  if (element.namespaceURI && element.namespaceURI !== "http://www.w3.org/1999/xhtml") {
@@ -533,6 +352,29 @@ if (typeof afterAll === "function") {
533
352
  afterAll(() => detachClipboardStubFromView(window));
534
353
  }
535
354
 
355
+ // src/utils/edit/isContentEditable.ts
356
+ function isContentEditable(element) {
357
+ return element.hasAttribute("contenteditable") && (element.getAttribute("contenteditable") == "true" || element.getAttribute("contenteditable") == "");
358
+ }
359
+ function getContentEditable(node) {
360
+ const element = getElement(node);
361
+ return element && (element.closest('[contenteditable=""]') || element.closest('[contenteditable="true"]'));
362
+ }
363
+ function getElement(node) {
364
+ return node.nodeType === 1 ? node : node.parentElement;
365
+ }
366
+
367
+ // src/utils/edit/getValue.ts
368
+ function getValue(element) {
369
+ if (!element) {
370
+ return null;
371
+ }
372
+ if (isContentEditable(element)) {
373
+ return element.textContent;
374
+ }
375
+ return getUIValue(element);
376
+ }
377
+
536
378
  // src/utils/edit/buildTimeValue.ts
537
379
  function buildTimeValue(value) {
538
380
  const onlyDigitsValue = value.replace(/\D/g, "");
@@ -564,62 +406,177 @@ function build(onlyDigitsValue, index) {
564
406
  return `${validHours.toString().padStart(2, "0")}:${validMinutes.toString().padStart(2, "0")}`;
565
407
  }
566
408
 
567
- // src/utils/edit/isValidDateValue.ts
568
- function isValidDateValue(element, value) {
409
+ // src/utils/edit/isValidDateOrTimeValue.ts
410
+ function isValidDateOrTimeValue(element, value) {
569
411
  const clone = element.cloneNode();
570
412
  clone.value = value;
571
413
  return clone.value === value;
572
414
  }
573
415
 
574
- // src/utils/edit/isValidInputTimeValue.ts
575
- function isValidInputTimeValue(element, timeValue) {
576
- const clone = element.cloneNode();
577
- clone.value = timeValue;
578
- return clone.value === timeValue;
416
+ // src/utils/edit/maxLength.ts
417
+ var maxLengthSupportedTypes = /* @__PURE__ */ ((maxLengthSupportedTypes2) => {
418
+ maxLengthSupportedTypes2["email"] = "email";
419
+ maxLengthSupportedTypes2["password"] = "password";
420
+ maxLengthSupportedTypes2["search"] = "search";
421
+ maxLengthSupportedTypes2["telephone"] = "telephone";
422
+ maxLengthSupportedTypes2["text"] = "text";
423
+ maxLengthSupportedTypes2["url"] = "url";
424
+ return maxLengthSupportedTypes2;
425
+ })(maxLengthSupportedTypes || {});
426
+ function getSpaceUntilMaxLength(element) {
427
+ const value = getValue(element);
428
+ if (value === null) {
429
+ return void 0;
430
+ }
431
+ const maxLength = getSanitizedMaxLength(element);
432
+ return maxLength ? maxLength - value.length : void 0;
433
+ }
434
+ function getSanitizedMaxLength(element) {
435
+ var _a;
436
+ if (!supportsMaxLength(element)) {
437
+ return void 0;
438
+ }
439
+ const attr = (_a = element.getAttribute("maxlength")) != null ? _a : "";
440
+ return /^\d+$/.test(attr) && Number(attr) >= 0 ? Number(attr) : void 0;
441
+ }
442
+ function supportsMaxLength(element) {
443
+ return isElementType(element, "textarea") || isElementType(element, "input") && Boolean(maxLengthSupportedTypes[element.type]);
579
444
  }
580
445
 
581
- // src/utils/edit/calculateNewValue.ts
582
- function calculateNewValue(inputData, node, {
583
- startOffset,
584
- endOffset
585
- }, inputType) {
586
- const value = getUIValue(node);
587
- const prologEnd = Math.max(0, startOffset === endOffset && inputType === "deleteContentBackward" ? startOffset - 1 : startOffset);
588
- const prolog = value.substring(0, prologEnd);
589
- const epilogStart = Math.min(value.length, startOffset === endOffset && inputType === "deleteContentForward" ? startOffset + 1 : endOffset);
590
- const epilog = value.substring(epilogStart, value.length);
591
- let newValue = `${prolog}${inputData}${epilog}`;
592
- const newOffset = prologEnd + inputData.length;
593
- if (node.type === "date" && !isValidDateValue(node, newValue)) {
594
- newValue = value;
446
+ // src/utils/edit/input.ts
447
+ function isDateOrTime(element) {
448
+ return isElementType(element, "input") && ["date", "time"].includes(element.type);
449
+ }
450
+ function input(config, element, data, inputType = "insertText") {
451
+ const inputRange = getInputRange(element);
452
+ if (!inputRange) {
453
+ return;
454
+ }
455
+ if (!isDateOrTime(element)) {
456
+ const unprevented = dispatchUIEvent(config, element, "beforeinput", {
457
+ inputType,
458
+ data
459
+ });
460
+ if (!unprevented) {
461
+ return;
462
+ }
595
463
  }
596
- if (node.type === "time" && !isValidInputTimeValue(node, newValue)) {
597
- if (isValidInputTimeValue(node, inputData)) {
598
- newValue = inputData;
464
+ if ("startContainer" in inputRange) {
465
+ editContenteditable(config, element, inputRange, data, inputType);
466
+ } else {
467
+ editInputElement(config, element, inputRange, data, inputType);
468
+ }
469
+ }
470
+ function editContenteditable(config, element, inputRange, data, inputType) {
471
+ let del = false;
472
+ if (!inputRange.collapsed) {
473
+ del = true;
474
+ inputRange.deleteContents();
475
+ } else if (["deleteContentBackward", "deleteContentForward"].includes(inputType)) {
476
+ const nextPosition = getNextCursorPosition(inputRange.startContainer, inputRange.startOffset, inputType === "deleteContentBackward" ? -1 : 1, inputType);
477
+ if (nextPosition) {
478
+ del = true;
479
+ const delRange = inputRange.cloneRange();
480
+ if (delRange.comparePoint(nextPosition.node, nextPosition.offset) < 0) {
481
+ delRange.setStart(nextPosition.node, nextPosition.offset);
482
+ } else {
483
+ delRange.setEnd(nextPosition.node, nextPosition.offset);
484
+ }
485
+ delRange.deleteContents();
486
+ }
487
+ }
488
+ if (data) {
489
+ if (inputRange.endContainer.nodeType === 3) {
490
+ const offset = inputRange.endOffset;
491
+ inputRange.endContainer.insertData(offset, data);
492
+ inputRange.setStart(inputRange.endContainer, offset + data.length);
493
+ inputRange.setEnd(inputRange.endContainer, offset + data.length);
599
494
  } else {
600
- newValue = value;
495
+ const text = element.ownerDocument.createTextNode(data);
496
+ inputRange.insertNode(text);
497
+ inputRange.setStart(text, data.length);
498
+ inputRange.setEnd(text, data.length);
601
499
  }
602
500
  }
603
- return {
604
- oldValue: value,
501
+ if (del || data) {
502
+ dispatchUIEvent(config, element, "input", { inputType });
503
+ }
504
+ }
505
+ function editInputElement(config, element, inputRange, data, inputType) {
506
+ let dataToInsert = data;
507
+ const spaceUntilMaxLength = getSpaceUntilMaxLength(element);
508
+ if (spaceUntilMaxLength !== void 0) {
509
+ if (spaceUntilMaxLength > 0) {
510
+ dataToInsert = data.substring(0, spaceUntilMaxLength);
511
+ } else {
512
+ return;
513
+ }
514
+ }
515
+ const { newValue, newOffset, oldValue } = calculateNewValue(dataToInsert, element, inputRange, inputType);
516
+ if (newValue === oldValue && newOffset === inputRange.startOffset && newOffset === inputRange.endOffset) {
517
+ return;
518
+ }
519
+ if (isElementType(element, "input", { type: "number" }) && !isValidNumberInput(newValue)) {
520
+ return;
521
+ }
522
+ setUIValue(element, newValue);
523
+ setSelection({
524
+ focusNode: element,
525
+ anchorOffset: newOffset,
526
+ focusOffset: newOffset
527
+ });
528
+ if (isDateOrTime(element)) {
529
+ if (isValidDateOrTimeValue(element, newValue)) {
530
+ commitInput(config, element, oldValue, newValue, newOffset, {});
531
+ dispatchUIEvent(config, element, "change");
532
+ }
533
+ } else {
534
+ commitInput(config, element, oldValue, newValue, newOffset, {
535
+ data,
536
+ inputType
537
+ });
538
+ }
539
+ }
540
+ function calculateNewValue(inputData, node, {
541
+ startOffset,
542
+ endOffset
543
+ }, inputType) {
544
+ const value = getUIValue(node);
545
+ const prologEnd = Math.max(0, startOffset === endOffset && inputType === "deleteContentBackward" ? startOffset - 1 : startOffset);
546
+ const prolog = value.substring(0, prologEnd);
547
+ const epilogStart = Math.min(value.length, startOffset === endOffset && inputType === "deleteContentForward" ? startOffset + 1 : endOffset);
548
+ const epilog = value.substring(epilogStart, value.length);
549
+ let newValue = `${prolog}${inputData}${epilog}`;
550
+ let newOffset = prologEnd + inputData.length;
551
+ if (isElementType(node, "input", { type: "time" })) {
552
+ const builtValue = buildTimeValue(newValue);
553
+ if (builtValue !== "" && isValidDateOrTimeValue(node, builtValue)) {
554
+ newValue = builtValue;
555
+ newOffset = builtValue.length;
556
+ }
557
+ }
558
+ return {
559
+ oldValue: value,
605
560
  newValue,
606
561
  newOffset
607
562
  };
608
563
  }
609
-
610
- // src/utils/edit/editInputElement.ts
611
- import { fireEvent as fireEvent2 } from "@testing-library/dom";
612
-
613
- // src/utils/edit/isContentEditable.ts
614
- function isContentEditable(element) {
615
- return element.hasAttribute("contenteditable") && (element.getAttribute("contenteditable") == "true" || element.getAttribute("contenteditable") == "");
616
- }
617
- function getContentEditable(node) {
618
- const element = getElement(node);
619
- return element && (element.closest('[contenteditable=""]') || element.closest('[contenteditable="true"]'));
564
+ function commitInput(config, element, oldValue, newValue, newOffset, inputInit) {
565
+ startTrackValue(element);
566
+ dispatchUIEvent(config, element, "input", inputInit);
567
+ const tracked = endTrackValue(element);
568
+ if ((tracked == null ? void 0 : tracked.length) === 2 && tracked[0] === oldValue && tracked[1] === newValue) {
569
+ setSelection({
570
+ focusNode: element,
571
+ anchorOffset: newOffset,
572
+ focusOffset: newOffset
573
+ });
574
+ }
620
575
  }
621
- function getElement(node) {
622
- return node.nodeType === 1 ? node : node.parentElement;
576
+ function isValidNumberInput(value) {
577
+ var _a, _b;
578
+ const valueParts = value.split("e", 2);
579
+ 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]));
623
580
  }
624
581
 
625
582
  // src/utils/edit/isEditable.ts
@@ -645,161 +602,124 @@ function isEditableInput(element) {
645
602
  return isElementType(element, "input", { readOnly: false }) && Boolean(editableInputTypes[element.type]);
646
603
  }
647
604
 
648
- // src/utils/focus/selection.ts
649
- function setSelectionRange(element, anchorOffset, focusOffset) {
605
+ // src/utils/edit/setFiles.ts
606
+ var fakeFiles = Symbol("files and value properties are mocked");
607
+ function setFiles(el, files) {
650
608
  var _a;
651
- if (hasOwnSelection(element)) {
652
- return setSelection({
653
- focusNode: element,
654
- anchorOffset,
655
- focusOffset
656
- });
657
- }
658
- if (isContentEditable(element) && ((_a = element.firstChild) == null ? void 0 : _a.nodeType) === 3) {
659
- return setSelection({
660
- focusNode: element.firstChild,
661
- anchorOffset,
662
- focusOffset
609
+ (_a = el[fakeFiles]) == null ? void 0 : _a.restore();
610
+ const objectDescriptors = Object.getOwnPropertyDescriptors(el);
611
+ const prototypeDescriptors = Object.getOwnPropertyDescriptors(Object.getPrototypeOf(el));
612
+ function restore() {
613
+ Object.defineProperties(el, {
614
+ files: __spreadValues(__spreadValues({}, prototypeDescriptors.files), objectDescriptors.files),
615
+ value: __spreadValues(__spreadValues({}, prototypeDescriptors.value), objectDescriptors.value),
616
+ type: __spreadValues(__spreadValues({}, prototypeDescriptors.type), objectDescriptors.type)
663
617
  });
664
618
  }
665
- throw new Error("Not implemented. The result of this interaction is unreliable.");
666
- }
667
- function hasOwnSelection(node) {
668
- return isElement(node) && (isElementType(node, "textarea") || isElementType(node, "input") && node.type in editableInputTypes);
669
- }
670
- function isElement(node) {
671
- return node.nodeType === 1;
672
- }
673
- function getTargetTypeAndSelection(node) {
674
- const element = getElement2(node);
675
- if (element && hasOwnSelection(element)) {
676
- return {
677
- type: "input",
678
- selection: getUISelection(element)
679
- };
680
- }
681
- const selection = element == null ? void 0 : element.ownerDocument.getSelection();
682
- const isCE = getContentEditable(node) && (selection == null ? void 0 : selection.anchorNode) && getContentEditable(selection.anchorNode);
683
- return {
684
- type: isCE ? "contenteditable" : "default",
685
- selection
686
- };
619
+ el[fakeFiles] = { restore };
620
+ Object.defineProperties(el, {
621
+ files: __spreadProps(__spreadValues(__spreadValues({}, prototypeDescriptors.files), objectDescriptors.files), {
622
+ get: () => files
623
+ }),
624
+ value: __spreadProps(__spreadValues(__spreadValues({}, prototypeDescriptors.value), objectDescriptors.value), {
625
+ get: () => files.length ? `C:\\fakepath\\${files[0].name}` : "",
626
+ set(v) {
627
+ var _a2;
628
+ if (v === "") {
629
+ restore();
630
+ } else {
631
+ (_a2 = objectDescriptors.value.set) == null ? void 0 : _a2.call(el, v);
632
+ }
633
+ }
634
+ }),
635
+ type: __spreadProps(__spreadValues(__spreadValues({}, prototypeDescriptors.type), objectDescriptors.type), {
636
+ set(v) {
637
+ if (v !== "file") {
638
+ restore();
639
+ el.type = v;
640
+ }
641
+ }
642
+ })
643
+ });
687
644
  }
688
- function getElement2(node) {
689
- return node.nodeType === 1 ? node : node.parentElement;
645
+
646
+ // src/utils/misc/eventWrapper.ts
647
+ import { getConfig } from "@testing-library/dom";
648
+ function eventWrapper(cb) {
649
+ let result;
650
+ getConfig().eventWrapper(() => {
651
+ result = cb();
652
+ });
653
+ return result;
690
654
  }
691
- function updateSelectionOnFocus(element) {
655
+
656
+ // src/utils/misc/isDisabled.ts
657
+ function isDisabled(element) {
692
658
  var _a;
693
- const selection = element.ownerDocument.getSelection();
694
- if (!(selection == null ? void 0 : selection.focusNode)) {
695
- return;
696
- }
697
- if (hasOwnSelection(element)) {
698
- const contenteditable = getContentEditable(selection.focusNode);
699
- if (contenteditable) {
700
- if (!selection.isCollapsed) {
701
- const focusNode = ((_a = contenteditable.firstChild) == null ? void 0 : _a.nodeType) === 3 ? contenteditable.firstChild : contenteditable;
702
- selection.setBaseAndExtent(focusNode, 0, focusNode, 0);
659
+ for (let el = element; el; el = el.parentElement) {
660
+ if (isElementType(el, [
661
+ "button",
662
+ "input",
663
+ "select",
664
+ "textarea",
665
+ "optgroup",
666
+ "option"
667
+ ])) {
668
+ if (el.hasAttribute("disabled")) {
669
+ return true;
670
+ }
671
+ } else if (isElementType(el, "fieldset")) {
672
+ if (el.hasAttribute("disabled") && !((_a = el.querySelector(":scope > legend")) == null ? void 0 : _a.contains(element))) {
673
+ return true;
674
+ }
675
+ } else if (el.tagName.includes("-")) {
676
+ if (el.constructor.formAssociated && el.hasAttribute("disabled")) {
677
+ return true;
703
678
  }
704
- } else {
705
- selection.setBaseAndExtent(element, 0, element, 0);
706
679
  }
707
680
  }
708
- }
709
- function getInputRange(focusNode) {
710
- var _a;
711
- const typeAndSelection = getTargetTypeAndSelection(focusNode);
712
- if (typeAndSelection.type === "input") {
713
- return typeAndSelection.selection;
714
- } else if (typeAndSelection.type === "contenteditable") {
715
- return (_a = typeAndSelection.selection) == null ? void 0 : _a.getRangeAt(0);
716
- }
717
- }
718
- function setSelection({
719
- focusNode,
720
- focusOffset,
721
- anchorNode = focusNode,
722
- anchorOffset = focusOffset
723
- }) {
724
- var _a, _b;
725
- const typeAndSelection = getTargetTypeAndSelection(focusNode);
726
- if (typeAndSelection.type === "input") {
727
- return setUISelection(focusNode, {
728
- anchorOffset,
729
- focusOffset
730
- });
731
- }
732
- (_b = (_a = anchorNode.ownerDocument) == null ? void 0 : _a.getSelection()) == null ? void 0 : _b.setBaseAndExtent(anchorNode, anchorOffset, focusNode, focusOffset);
681
+ return false;
733
682
  }
734
683
 
735
- // src/utils/edit/editInputElement.ts
736
- function editInputElement(element, {
737
- newValue,
738
- newSelection,
739
- eventOverrides
740
- }) {
741
- const oldValue = element.value;
742
- setUIValue(element, newValue);
743
- setSelection({
744
- focusNode: newSelection.node,
745
- anchorOffset: newSelection.offset,
746
- focusOffset: newSelection.offset
747
- });
748
- startTrackValue(element);
749
- fireEvent2.input(element, __spreadValues({}, eventOverrides));
750
- const tracked = endTrackValue(element);
751
- if ((tracked == null ? void 0 : tracked.length) === 2 && tracked[0] === oldValue && tracked[1] === newValue) {
752
- setSelection({
753
- focusNode: newSelection.node,
754
- anchorOffset: newSelection.offset,
755
- focusOffset: newSelection.offset
756
- });
684
+ // src/utils/focus/getActiveElement.ts
685
+ function getActiveElement(document2) {
686
+ const activeElement = document2.activeElement;
687
+ if (activeElement == null ? void 0 : activeElement.shadowRoot) {
688
+ return getActiveElement(activeElement.shadowRoot);
689
+ } else {
690
+ if (isDisabled(activeElement)) {
691
+ return document2.ownerDocument ? document2.ownerDocument.body : document2.body;
692
+ }
693
+ return activeElement;
757
694
  }
758
695
  }
759
696
 
760
- // src/utils/edit/getValue.ts
761
- function getValue(element) {
762
- if (!element) {
763
- return null;
764
- }
765
- if (isContentEditable(element)) {
766
- return element.textContent;
767
- }
768
- return getUIValue(element);
769
- }
697
+ // src/utils/focus/selector.ts
698
+ var FOCUSABLE_SELECTOR = [
699
+ "input:not([type=hidden]):not([disabled])",
700
+ "button:not([disabled])",
701
+ "select:not([disabled])",
702
+ "textarea:not([disabled])",
703
+ '[contenteditable=""]',
704
+ '[contenteditable="true"]',
705
+ "a[href]",
706
+ "[tabindex]:not([disabled])"
707
+ ].join(", ");
770
708
 
771
- // src/utils/edit/maxLength.ts
772
- var maxLengthSupportedTypes = /* @__PURE__ */ ((maxLengthSupportedTypes2) => {
773
- maxLengthSupportedTypes2["email"] = "email";
774
- maxLengthSupportedTypes2["password"] = "password";
775
- maxLengthSupportedTypes2["search"] = "search";
776
- maxLengthSupportedTypes2["telephone"] = "telephone";
777
- maxLengthSupportedTypes2["text"] = "text";
778
- maxLengthSupportedTypes2["url"] = "url";
779
- return maxLengthSupportedTypes2;
780
- })(maxLengthSupportedTypes || {});
781
- function getSpaceUntilMaxLength(element) {
782
- const value = getValue(element);
783
- if (value === null) {
784
- return void 0;
785
- }
786
- const maxLength = getSanitizedMaxLength(element);
787
- return maxLength ? maxLength - value.length : void 0;
788
- }
789
- function getSanitizedMaxLength(element) {
790
- var _a;
791
- if (!supportsMaxLength(element)) {
792
- return void 0;
793
- }
794
- const attr = (_a = element.getAttribute("maxlength")) != null ? _a : "";
795
- return /^\d+$/.test(attr) && Number(attr) >= 0 ? Number(attr) : void 0;
796
- }
797
- function supportsMaxLength(element) {
798
- return isElementType(element, "textarea") || isElementType(element, "input") && Boolean(maxLengthSupportedTypes[element.type]);
709
+ // src/utils/focus/isFocusable.ts
710
+ function isFocusable(element) {
711
+ return element.matches(FOCUSABLE_SELECTOR);
799
712
  }
800
713
 
801
- // src/utils/edit/prepareInput.ts
802
- import { fireEvent as fireEvent3 } from "@testing-library/dom";
714
+ // src/utils/focus/blur.ts
715
+ function blur(element) {
716
+ if (!isFocusable(element))
717
+ return;
718
+ const wasActive = getActiveElement(element.ownerDocument) === element;
719
+ if (!wasActive)
720
+ return;
721
+ eventWrapper(() => element.blur());
722
+ }
803
723
 
804
724
  // src/utils/focus/cursor.ts
805
725
  function getNextCursorPosition(node, offset, direction, inputType) {
@@ -849,7 +769,7 @@ function getNextCursorPosition(node, offset, direction, inputType) {
849
769
  }
850
770
  function getNextCharacterContentNode(node, offset, direction) {
851
771
  const nextOffset = Number(offset) + (direction < 0 ? -1 : 0);
852
- if (offset !== void 0 && isElement2(node) && nextOffset >= 0 && nextOffset < node.children.length) {
772
+ if (offset !== void 0 && isElement(node) && nextOffset >= 0 && nextOffset < node.children.length) {
853
773
  node = node.children[nextOffset];
854
774
  }
855
775
  return walkNodes(node, direction === 1 ? "next" : "previous", isTreatedAsCharacterContent);
@@ -858,7 +778,7 @@ function isTreatedAsCharacterContent(node) {
858
778
  if (isTextNode(node)) {
859
779
  return true;
860
780
  }
861
- if (isElement2(node)) {
781
+ if (isElement(node)) {
862
782
  if (isElementType(node, ["input", "textarea"])) {
863
783
  return node.type !== "hidden";
864
784
  } else if (isElementType(node, "br")) {
@@ -875,7 +795,7 @@ function getOffset(node) {
875
795
  }
876
796
  return i;
877
797
  }
878
- function isElement2(node) {
798
+ function isElement(node) {
879
799
  return node.nodeType === 1;
880
800
  }
881
801
  function isTextNode(node) {
@@ -890,7 +810,7 @@ function walkNodes(node, direction, callback) {
890
810
  if (callback(node)) {
891
811
  return node;
892
812
  }
893
- } else if (node.parentNode && (!isElement2(node.parentNode) || !isContentEditable(node.parentNode) && node.parentNode !== ((_a = node.ownerDocument) == null ? void 0 : _a.body))) {
813
+ } else if (node.parentNode && (!isElement(node.parentNode) || !isContentEditable(node.parentNode) && node.parentNode !== ((_a = node.ownerDocument) == null ? void 0 : _a.body))) {
894
814
  node = node.parentNode;
895
815
  } else {
896
816
  break;
@@ -904,167 +824,116 @@ function getDescendant(node, direction) {
904
824
  return node;
905
825
  }
906
826
 
907
- // src/utils/edit/prepareInput.ts
908
- function prepareInput(data, element, inputType = "insertText") {
909
- const inputRange = getInputRange(element);
910
- if (!inputRange) {
911
- return;
827
+ // src/utils/focus/selection.ts
828
+ function setSelectionRange(element, anchorOffset, focusOffset) {
829
+ var _a;
830
+ if (hasOwnSelection(element)) {
831
+ return setSelection({
832
+ focusNode: element,
833
+ anchorOffset,
834
+ focusOffset
835
+ });
912
836
  }
913
- if ("startContainer" in inputRange) {
914
- return {
915
- commit: () => {
916
- let del = false;
917
- if (!inputRange.collapsed) {
918
- del = true;
919
- inputRange.deleteContents();
920
- } else if (["deleteContentBackward", "deleteContentForward"].includes(inputType)) {
921
- const nextPosition = getNextCursorPosition(inputRange.startContainer, inputRange.startOffset, inputType === "deleteContentBackward" ? -1 : 1, inputType);
922
- if (nextPosition) {
923
- del = true;
924
- const delRange = inputRange.cloneRange();
925
- if (delRange.comparePoint(nextPosition.node, nextPosition.offset) < 0) {
926
- delRange.setStart(nextPosition.node, nextPosition.offset);
927
- } else {
928
- delRange.setEnd(nextPosition.node, nextPosition.offset);
929
- }
930
- delRange.deleteContents();
931
- }
932
- }
933
- if (data) {
934
- if (inputRange.endContainer.nodeType === 3) {
935
- const offset = inputRange.endOffset;
936
- inputRange.endContainer.insertData(offset, data);
937
- inputRange.setStart(inputRange.endContainer, offset + data.length);
938
- inputRange.setEnd(inputRange.endContainer, offset + data.length);
939
- } else {
940
- const text = element.ownerDocument.createTextNode(data);
941
- inputRange.insertNode(text);
942
- inputRange.setStart(text, data.length);
943
- inputRange.setEnd(text, data.length);
944
- }
945
- }
946
- if (del || data) {
947
- fireEvent3.input(element, { inputType });
948
- }
949
- }
950
- };
951
- } else {
837
+ if (isContentEditable(element) && ((_a = element.firstChild) == null ? void 0 : _a.nodeType) === 3) {
838
+ return setSelection({
839
+ focusNode: element.firstChild,
840
+ anchorOffset,
841
+ focusOffset
842
+ });
843
+ }
844
+ throw new Error("Not implemented. The result of this interaction is unreliable.");
845
+ }
846
+ function hasOwnSelection(node) {
847
+ return isElement2(node) && (isElementType(node, "textarea") || isElementType(node, "input") && node.type in editableInputTypes);
848
+ }
849
+ function isElement2(node) {
850
+ return node.nodeType === 1;
851
+ }
852
+ function getTargetTypeAndSelection(node) {
853
+ const element = getElement2(node);
854
+ if (element && hasOwnSelection(element)) {
952
855
  return {
953
- getNewValue: () => calculateNewValue(data, element, inputRange, inputType).newValue,
954
- commit: () => {
955
- const { newValue, newOffset, oldValue } = calculateNewValue(data, element, inputRange, inputType);
956
- if (newValue === oldValue && newOffset === inputRange.startOffset && newOffset === inputRange.endOffset) {
957
- return;
958
- }
959
- editInputElement(element, {
960
- newValue,
961
- newSelection: {
962
- node: element,
963
- offset: newOffset
964
- },
965
- eventOverrides: {
966
- inputType
967
- }
968
- });
969
- }
856
+ type: "input",
857
+ selection: getUISelection(element)
970
858
  };
971
859
  }
860
+ const selection = element == null ? void 0 : element.ownerDocument.getSelection();
861
+ const isCE = getContentEditable(node) && (selection == null ? void 0 : selection.anchorNode) && getContentEditable(selection.anchorNode);
862
+ return {
863
+ type: isCE ? "contenteditable" : "default",
864
+ selection
865
+ };
972
866
  }
973
-
974
- // src/utils/edit/setFiles.ts
975
- var fakeFiles = Symbol("files and value properties are mocked");
976
- function setFiles(el, files) {
867
+ function getElement2(node) {
868
+ return node.nodeType === 1 ? node : node.parentElement;
869
+ }
870
+ function updateSelectionOnFocus(element) {
977
871
  var _a;
978
- (_a = el[fakeFiles]) == null ? void 0 : _a.restore();
979
- const objectDescriptors = Object.getOwnPropertyDescriptors(el);
980
- const prototypeDescriptors = Object.getOwnPropertyDescriptors(Object.getPrototypeOf(el));
981
- function restore() {
982
- Object.defineProperties(el, {
983
- files: __spreadValues(__spreadValues({}, prototypeDescriptors.files), objectDescriptors.files),
984
- value: __spreadValues(__spreadValues({}, prototypeDescriptors.value), objectDescriptors.value),
985
- type: __spreadValues(__spreadValues({}, prototypeDescriptors.type), objectDescriptors.type)
986
- });
872
+ const selection = element.ownerDocument.getSelection();
873
+ if (!(selection == null ? void 0 : selection.focusNode)) {
874
+ return;
987
875
  }
988
- el[fakeFiles] = { restore };
989
- Object.defineProperties(el, {
990
- files: __spreadProps(__spreadValues(__spreadValues({}, prototypeDescriptors.files), objectDescriptors.files), {
991
- get: () => files
992
- }),
993
- value: __spreadProps(__spreadValues(__spreadValues({}, prototypeDescriptors.value), objectDescriptors.value), {
994
- get: () => files.length ? `C:\\fakepath\\${files[0].name}` : "",
995
- set(v) {
996
- var _a2;
997
- if (v === "") {
998
- restore();
999
- } else {
1000
- (_a2 = objectDescriptors.value.set) == null ? void 0 : _a2.call(el, v);
1001
- }
1002
- }
1003
- }),
1004
- type: __spreadProps(__spreadValues(__spreadValues({}, prototypeDescriptors.type), objectDescriptors.type), {
1005
- set(v) {
1006
- if (v !== "file") {
1007
- restore();
1008
- el.type = v;
1009
- }
876
+ if (hasOwnSelection(element)) {
877
+ const contenteditable = getContentEditable(selection.focusNode);
878
+ if (contenteditable) {
879
+ if (!selection.isCollapsed) {
880
+ const focusNode = ((_a = contenteditable.firstChild) == null ? void 0 : _a.nodeType) === 3 ? contenteditable.firstChild : contenteditable;
881
+ selection.setBaseAndExtent(focusNode, 0, focusNode, 0);
1010
882
  }
1011
- })
1012
- });
1013
- }
1014
-
1015
- // src/utils/misc/eventWrapper.ts
1016
- import { getConfig } from "@testing-library/dom";
1017
- function eventWrapper(cb) {
1018
- let result;
1019
- getConfig().eventWrapper(() => {
1020
- result = cb();
1021
- });
1022
- return result;
1023
- }
1024
-
1025
- // src/utils/misc/isDisabled.ts
1026
- function isDisabled(element) {
1027
- return Boolean(element && element.disabled);
1028
- }
1029
-
1030
- // src/utils/focus/getActiveElement.ts
1031
- function getActiveElement(document2) {
1032
- const activeElement = document2.activeElement;
1033
- if (activeElement == null ? void 0 : activeElement.shadowRoot) {
1034
- return getActiveElement(activeElement.shadowRoot);
1035
- } else {
1036
- if (isDisabled(activeElement)) {
1037
- return document2.ownerDocument ? document2.ownerDocument.body : document2.body;
883
+ } else {
884
+ selection.setBaseAndExtent(element, 0, element, 0);
1038
885
  }
1039
- return activeElement;
1040
886
  }
1041
887
  }
1042
-
1043
- // src/utils/focus/selector.ts
1044
- var FOCUSABLE_SELECTOR = [
1045
- "input:not([type=hidden]):not([disabled])",
1046
- "button:not([disabled])",
1047
- "select:not([disabled])",
1048
- "textarea:not([disabled])",
1049
- '[contenteditable=""]',
1050
- '[contenteditable="true"]',
1051
- "a[href]",
1052
- "[tabindex]:not([disabled])"
1053
- ].join(", ");
1054
-
1055
- // src/utils/focus/isFocusable.ts
1056
- function isFocusable(element) {
1057
- return element.matches(FOCUSABLE_SELECTOR);
888
+ function getInputRange(focusNode) {
889
+ var _a;
890
+ const typeAndSelection = getTargetTypeAndSelection(focusNode);
891
+ if (typeAndSelection.type === "input") {
892
+ return typeAndSelection.selection;
893
+ } else if (typeAndSelection.type === "contenteditable") {
894
+ return (_a = typeAndSelection.selection) == null ? void 0 : _a.getRangeAt(0);
895
+ }
896
+ }
897
+ function setSelection({
898
+ focusNode,
899
+ focusOffset,
900
+ anchorNode = focusNode,
901
+ anchorOffset = focusOffset
902
+ }) {
903
+ var _a, _b;
904
+ const typeAndSelection = getTargetTypeAndSelection(focusNode);
905
+ if (typeAndSelection.type === "input") {
906
+ return setUISelection(focusNode, {
907
+ anchorOffset,
908
+ focusOffset
909
+ });
910
+ }
911
+ (_b = (_a = anchorNode.ownerDocument) == null ? void 0 : _a.getSelection()) == null ? void 0 : _b.setBaseAndExtent(anchorNode, anchorOffset, focusNode, focusOffset);
1058
912
  }
1059
-
1060
- // src/utils/focus/blur.ts
1061
- function blur(element) {
1062
- if (!isFocusable(element))
1063
- return;
1064
- const wasActive = getActiveElement(element.ownerDocument) === element;
1065
- if (!wasActive)
1066
- return;
1067
- eventWrapper(() => element.blur());
913
+ function moveSelection(node, direction) {
914
+ if (hasOwnSelection(node)) {
915
+ const selection = getUISelection(node);
916
+ setSelection({
917
+ focusNode: node,
918
+ focusOffset: selection.startOffset === selection.endOffset ? selection.focusOffset + direction : direction < 0 ? selection.startOffset : selection.endOffset
919
+ });
920
+ } else {
921
+ const selection = node.ownerDocument.getSelection();
922
+ if (!selection) {
923
+ return;
924
+ }
925
+ if (selection.isCollapsed) {
926
+ const nextPosition = getNextCursorPosition(selection.focusNode, selection.focusOffset, direction);
927
+ if (nextPosition) {
928
+ setSelection({
929
+ focusNode: nextPosition.node,
930
+ focusOffset: nextPosition.offset
931
+ });
932
+ }
933
+ } else {
934
+ selection[direction < 0 ? "collapseToStart" : "collapseToEnd"]();
935
+ }
936
+ }
1068
937
  }
1069
938
 
1070
939
  // src/utils/focus/copySelection.ts
@@ -1084,15 +953,30 @@ function readSelectedValueFromInput(target) {
1084
953
  return val.substring(sel.startOffset, sel.endOffset);
1085
954
  }
1086
955
 
956
+ // src/utils/misc/findClosest.ts
957
+ function findClosest(element, callback) {
958
+ let el = element;
959
+ do {
960
+ if (callback(el)) {
961
+ return el;
962
+ }
963
+ el = el.parentElement;
964
+ } while (el && el !== element.ownerDocument.body);
965
+ return void 0;
966
+ }
967
+
1087
968
  // src/utils/focus/focus.ts
1088
969
  function focus(element) {
1089
- if (!isFocusable(element))
1090
- return;
1091
- const isAlreadyActive = getActiveElement(element.ownerDocument) === element;
1092
- if (isAlreadyActive)
970
+ const target = findClosest(element, isFocusable);
971
+ const activeElement = getActiveElement(element.ownerDocument);
972
+ if ((target != null ? target : element.ownerDocument.body) === activeElement) {
1093
973
  return;
1094
- eventWrapper(() => element.focus());
1095
- updateSelectionOnFocus(element);
974
+ } else if (target) {
975
+ eventWrapper(() => target.focus());
976
+ } else {
977
+ eventWrapper(() => activeElement == null ? void 0 : activeElement.blur());
978
+ }
979
+ updateSelectionOnFocus(target != null ? target : element.ownerDocument.body);
1096
980
  }
1097
981
 
1098
982
  // src/utils/misc/isVisible.ts
@@ -1189,6 +1073,14 @@ function isAllSelected(target) {
1189
1073
  return (selection == null ? void 0 : selection.anchorNode) === focusNode && selection.focusNode === focusNode && selection.anchorOffset === 0 && selection.focusOffset === focusNode.childNodes.length;
1190
1074
  }
1191
1075
 
1076
+ // src/utils/keyboard/getKeyEventProps.ts
1077
+ function getKeyEventProps(keyDef) {
1078
+ return {
1079
+ key: keyDef.key,
1080
+ code: keyDef.code
1081
+ };
1082
+ }
1083
+
1192
1084
  // src/utils/keyboard/getUIEventModifiers.ts
1193
1085
  function getUIEventModifiers(keyboardState) {
1194
1086
  return {
@@ -1207,21 +1099,13 @@ function getUIEventModifiers(keyboardState) {
1207
1099
  };
1208
1100
  }
1209
1101
 
1210
- // src/utils/keyboard/getKeyEventProps.ts
1211
- function getKeyEventProps(keyDef, state) {
1212
- return __spreadValues({
1213
- key: keyDef.key,
1214
- code: keyDef.code
1215
- }, getUIEventModifiers(state));
1216
- }
1217
-
1218
1102
  // src/utils/keyDef/readNextDescriptor.ts
1219
1103
  var bracketDict = /* @__PURE__ */ ((bracketDict2) => {
1220
1104
  bracketDict2["{"] = "}";
1221
1105
  bracketDict2["["] = "]";
1222
1106
  return bracketDict2;
1223
1107
  })(bracketDict || {});
1224
- function readNextDescriptor(text) {
1108
+ function readNextDescriptor(text, context) {
1225
1109
  let pos = 0;
1226
1110
  const startBracket = text[pos] in bracketDict ? text[pos] : "";
1227
1111
  pos += startBracket.length;
@@ -1229,11 +1113,11 @@ function readNextDescriptor(text) {
1229
1113
  const type3 = isEscapedChar ? "" : startBracket;
1230
1114
  return __spreadValues({
1231
1115
  type: type3
1232
- }, type3 === "" ? readPrintableChar(text, pos) : readTag(text, pos, type3));
1116
+ }, type3 === "" ? readPrintableChar(text, pos, context) : readTag(text, pos, type3, context));
1233
1117
  }
1234
- function readPrintableChar(text, pos) {
1118
+ function readPrintableChar(text, pos, context) {
1235
1119
  const descriptor = text[pos];
1236
- assertDescriptor(descriptor, text, pos);
1120
+ assertDescriptor(descriptor, text, pos, context);
1237
1121
  pos += descriptor.length;
1238
1122
  return {
1239
1123
  consumedLength: pos,
@@ -1243,14 +1127,14 @@ function readPrintableChar(text, pos) {
1243
1127
  repeat: 1
1244
1128
  };
1245
1129
  }
1246
- function readTag(text, pos, startBracket) {
1130
+ function readTag(text, pos, startBracket, context) {
1247
1131
  var _a, _b, _c;
1248
1132
  const releasePreviousModifier = text[pos] === "/" ? "/" : "";
1249
1133
  pos += releasePreviousModifier.length;
1250
1134
  const escapedDescriptor = startBracket === "{" && text[pos] === "\\";
1251
1135
  pos += Number(escapedDescriptor);
1252
1136
  const descriptor = escapedDescriptor ? text[pos] : (_a = text.slice(pos).match(startBracket === "{" ? /^\w+|^[^}>/]/ : /^\w+/)) == null ? void 0 : _a[0];
1253
- assertDescriptor(descriptor, text, pos);
1137
+ assertDescriptor(descriptor, text, pos, context);
1254
1138
  pos += descriptor.length;
1255
1139
  const repeatModifier = (_c = (_b = text.slice(pos).match(/^>\d+/)) == null ? void 0 : _b[0]) != null ? _c : "";
1256
1140
  pos += repeatModifier.length;
@@ -1263,7 +1147,7 @@ function readTag(text, pos, startBracket) {
1263
1147
  !repeatModifier && "repeat modifier",
1264
1148
  !releaseSelfModifier && "release modifier",
1265
1149
  `"${expectedEndBracket}"`
1266
- ].filter(Boolean).join(" or "), text[pos], text));
1150
+ ].filter(Boolean).join(" or "), text[pos], text, context));
1267
1151
  }
1268
1152
  pos += endBracket.length;
1269
1153
  return {
@@ -1274,9 +1158,9 @@ function readTag(text, pos, startBracket) {
1274
1158
  releaseSelf: hasReleaseSelf(releaseSelfModifier, repeatModifier)
1275
1159
  };
1276
1160
  }
1277
- function assertDescriptor(descriptor, text, pos) {
1161
+ function assertDescriptor(descriptor, text, pos, context) {
1278
1162
  if (!descriptor) {
1279
- throw new Error(getErrorMessage("key descriptor", text[pos], text));
1163
+ throw new Error(getErrorMessage("key descriptor", text[pos], text, context));
1280
1164
  }
1281
1165
  }
1282
1166
  function hasReleaseSelf(releaseSelfModifier, repeatModifier) {
@@ -1287,22 +1171,15 @@ function hasReleaseSelf(releaseSelfModifier, repeatModifier) {
1287
1171
  return false;
1288
1172
  }
1289
1173
  }
1290
- function getErrorMessage(expected, found, text) {
1174
+ function getErrorMessage(expected, found, text, context) {
1291
1175
  return `Expected ${expected} but found "${found != null ? found : ""}" in "${text}"
1292
- See https://github.com/testing-library/user-event/blob/main/README.md#keyboardtext-options
1176
+ 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`}
1293
1177
  for more information about how userEvent parses your input.`;
1294
1178
  }
1295
1179
 
1296
- // src/utils/misc/findClosest.ts
1297
- function findClosest(element, callback) {
1298
- let el = element;
1299
- do {
1300
- if (callback(el)) {
1301
- return el;
1302
- }
1303
- el = el.parentElement;
1304
- } while (el && el !== element.ownerDocument.body);
1305
- return void 0;
1180
+ // src/utils/misc/cloneEvent.ts
1181
+ function cloneEvent(event) {
1182
+ return new event.constructor(event.type, event);
1306
1183
  }
1307
1184
 
1308
1185
  // src/utils/misc/getDocumentFromNode.ts
@@ -1342,9 +1219,6 @@ function wait(time) {
1342
1219
  return new Promise((resolve) => setTimeout(() => resolve(), time));
1343
1220
  }
1344
1221
 
1345
- // src/utils/misc/hasFormSubmit.ts
1346
- var hasFormSubmit = (form) => !!(form == null ? void 0 : form.querySelector('input[type="submit"], button:not([type]), button[type="submit"]'));
1347
-
1348
1222
  // src/utils/pointer/cssPointerEvents.ts
1349
1223
  import { getWindowFromNode as getWindowFromNode2 } from "@testing-library/dom/dist/helpers.js";
1350
1224
 
@@ -1471,10 +1345,6 @@ function hasBitFlag(conf, flag) {
1471
1345
  return (conf & flag) > 0;
1472
1346
  }
1473
1347
 
1474
- // src/utils/pointer/firePointerEvents.ts
1475
- import { createEvent, fireEvent as fireEvent4 } from "@testing-library/dom";
1476
- import { eventMap } from "@testing-library/dom/dist/event-map.js";
1477
-
1478
1348
  // src/utils/pointer/mouseButtons.ts
1479
1349
  var MouseButton = {
1480
1350
  primary: 0,
@@ -1506,38 +1376,41 @@ function getMouseButtons(...buttons) {
1506
1376
  return v;
1507
1377
  }
1508
1378
 
1509
- // src/utils/pointer/firePointerEvents.ts
1510
- function firePointerEvent(target, type3, {
1511
- pointerState,
1512
- keyboardState,
1513
- pointerType,
1514
- button,
1515
- coords,
1516
- pointerId,
1517
- isPrimary,
1518
- clickCount
1519
- }) {
1520
- const init = __spreadValues(__spreadValues({}, coords), getUIEventModifiers(keyboardState));
1521
- if (type3 === "click" || type3.startsWith("pointer")) {
1522
- init.pointerId = pointerId;
1523
- init.pointerType = pointerType;
1524
- }
1525
- if (["pointerdown", "pointerup"].includes(type3)) {
1526
- init.isPrimary = isPrimary;
1379
+ // src/event/createEvent.ts
1380
+ import { createEvent as createEventBase } from "@testing-library/dom";
1381
+
1382
+ // src/event/eventMap.ts
1383
+ import { eventMap as baseEventMap } from "@testing-library/dom/dist/event-map.js";
1384
+ var eventMap = __spreadProps(__spreadValues({}, baseEventMap), {
1385
+ beforeInput: {
1386
+ EventType: "InputEvent",
1387
+ defaultInit: { bubbles: true, cancelable: true, composed: true }
1527
1388
  }
1528
- init.button = getMouseButton(button != null ? button : 0);
1529
- init.buttons = getMouseButtons(...pointerState.pressed.filter((p) => p.keyDef.pointerType === pointerType).map((p) => {
1530
- var _a;
1531
- return (_a = p.keyDef.button) != null ? _a : 0;
1532
- }));
1533
- if (["mousedown", "mouseup", "click", "dblclick", "contextmenu"].includes(type3)) {
1534
- init.detail = clickCount;
1389
+ });
1390
+ var eventMapKeys = Object.fromEntries(Object.keys(eventMap).map((k) => [k.toLowerCase(), k]));
1391
+
1392
+ // src/event/eventTypes.ts
1393
+ import { eventMap as eventMap2 } from "@testing-library/dom/dist/event-map.js";
1394
+ var eventKeys = Object.fromEntries(Object.keys(eventMap2).map((k) => [k.toLowerCase(), k]));
1395
+ function getEventClass(type3) {
1396
+ return type3 in eventKeys ? eventMap2[eventKeys[type3]].EventType : "Event";
1397
+ }
1398
+ var mouseEvents = ["MouseEvent", "PointerEvent"];
1399
+ function isMouseEvent(type3) {
1400
+ return mouseEvents.includes(getEventClass(type3));
1401
+ }
1402
+ function isKeyboardEvent(type3) {
1403
+ return getEventClass(type3) === "KeyboardEvent";
1404
+ }
1405
+
1406
+ // src/event/createEvent.ts
1407
+ function createEvent(type3, target, init) {
1408
+ const event = createEventBase(type3, target, init, eventMap[eventMapKeys[type3]]);
1409
+ if (isMouseEvent(type3) && init) {
1410
+ assignPositionInit(event, init);
1411
+ assignPointerInit(event, init);
1535
1412
  }
1536
- const eventKey = Object.keys(eventMap).find((k) => k.toLowerCase() === type3);
1537
- const event = createEvent[eventKey](target, init);
1538
- assignPositionInit(event, init);
1539
- assignPointerInit(event, init);
1540
- return fireEvent4(target, event);
1413
+ return event;
1541
1414
  }
1542
1415
  function assignProps(obj, props) {
1543
1416
  for (const [key, value] of Object.entries(props)) {
@@ -1578,265 +1451,400 @@ function assignPointerInit(obj, { isPrimary, pointerId, pointerType }) {
1578
1451
  });
1579
1452
  }
1580
1453
 
1581
- // src/keyboard/plugins/arrow.ts
1582
- var keydownBehavior = [
1583
- {
1584
- matches: (keyDef) => keyDef.key === "ArrowLeft" || keyDef.key === "ArrowRight",
1585
- handle: (keyDef, element) => {
1586
- if (hasOwnSelection(element)) {
1587
- const selection = getUISelection(element);
1588
- setSelection({
1589
- focusNode: element,
1590
- focusOffset: selection.startOffset === selection.endOffset ? selection.focusOffset + (keyDef.key === "ArrowLeft" ? -1 : 1) : keyDef.key === "ArrowLeft" ? selection.startOffset : selection.endOffset
1454
+ // src/event/behavior/registry.ts
1455
+ var behavior = {};
1456
+
1457
+ // src/event/behavior/click.ts
1458
+ behavior.click = (event, target, config) => {
1459
+ const context = target.closest("button,input,label,textarea");
1460
+ const control = context && isElementType(context, "label") && context.control;
1461
+ if (control) {
1462
+ return () => {
1463
+ if (isFocusable(control)) {
1464
+ focus(control);
1465
+ }
1466
+ dispatchEvent(config, control, cloneEvent(event));
1467
+ };
1468
+ } else if (isElementType(target, "input", { type: "file" })) {
1469
+ return () => {
1470
+ blur(target);
1471
+ target.dispatchEvent(new Event("fileDialog"));
1472
+ focus(target);
1473
+ };
1474
+ }
1475
+ };
1476
+
1477
+ // src/event/behavior/cut.ts
1478
+ behavior.cut = (event, target, config) => {
1479
+ return () => {
1480
+ if (isEditable(target)) {
1481
+ input(config, target, "", "deleteByCut");
1482
+ }
1483
+ };
1484
+ };
1485
+
1486
+ // src/event/behavior/keydown.ts
1487
+ behavior.keydown = (event, target, config) => {
1488
+ var _a, _b;
1489
+ return (_b = (_a = keydownBehavior[event.key]) == null ? void 0 : _a.call(keydownBehavior, event, target, config)) != null ? _b : combinationBehavior(event, target, config);
1490
+ };
1491
+ var keydownBehavior = {
1492
+ ArrowLeft: (event, target) => () => moveSelection(target, -1),
1493
+ ArrowRight: (event, target) => () => moveSelection(target, 1),
1494
+ Backspace: (event, target, config) => {
1495
+ if (isEditable(target)) {
1496
+ return () => {
1497
+ input(config, target, "", "deleteContentBackward");
1498
+ };
1499
+ }
1500
+ },
1501
+ Delete: (event, target, config) => {
1502
+ if (isEditable(target)) {
1503
+ return () => {
1504
+ input(config, target, "", "deleteContentForward");
1505
+ };
1506
+ }
1507
+ },
1508
+ End: (event, target) => {
1509
+ if (isElementType(target, ["input", "textarea"]) || isContentEditable(target)) {
1510
+ return () => {
1511
+ var _a, _b;
1512
+ const newPos = (_b = (_a = getValue(target)) == null ? void 0 : _a.length) != null ? _b : 0;
1513
+ setSelectionRange(target, newPos, newPos);
1514
+ };
1515
+ }
1516
+ },
1517
+ Home: (event, target) => {
1518
+ if (isElementType(target, ["input", "textarea"]) || isContentEditable(target)) {
1519
+ return () => {
1520
+ setSelectionRange(target, 0, 0);
1521
+ };
1522
+ }
1523
+ },
1524
+ PageDown: (event, target) => {
1525
+ if (isElementType(target, ["input"])) {
1526
+ return () => {
1527
+ const newPos = getValue(target).length;
1528
+ setSelectionRange(target, newPos, newPos);
1529
+ };
1530
+ }
1531
+ },
1532
+ PageUp: (event, target) => {
1533
+ if (isElementType(target, ["input"])) {
1534
+ return () => {
1535
+ setSelectionRange(target, 0, 0);
1536
+ };
1537
+ }
1538
+ },
1539
+ Tab: (event, target, { keyboardState }) => {
1540
+ return () => {
1541
+ const dest = getTabDestination(target, keyboardState.modifiers.Shift);
1542
+ focus(dest);
1543
+ if (hasOwnSelection(dest)) {
1544
+ setUISelection(dest, {
1545
+ anchorOffset: 0,
1546
+ focusOffset: dest.value.length
1591
1547
  });
1548
+ }
1549
+ };
1550
+ }
1551
+ };
1552
+ var combinationBehavior = (event, target, config) => {
1553
+ if (event.code === "KeyA" && config.keyboardState.modifiers.Control) {
1554
+ return () => selectAll(target);
1555
+ }
1556
+ };
1557
+
1558
+ // src/event/behavior/keypress.ts
1559
+ behavior.keypress = (event, target, config) => {
1560
+ if (event.key === "Enter") {
1561
+ if (isElementType(target, "button") || isElementType(target, "input") && ClickInputOnEnter.includes(target.type) || isElementType(target, "a") && Boolean(target.href)) {
1562
+ return () => {
1563
+ dispatchUIEvent(config, target, "click");
1564
+ };
1565
+ } else if (isElementType(target, "input")) {
1566
+ const form = target.form;
1567
+ const submit = form == null ? void 0 : form.querySelector('input[type="submit"], button:not([type]), button[type="submit"]');
1568
+ if (submit) {
1569
+ return () => dispatchUIEvent(config, submit, "click");
1570
+ } else if (form && SubmitSingleInputOnEnter.includes(target.type) && form.querySelectorAll("input").length === 1) {
1571
+ return () => dispatchUIEvent(config, form, "submit");
1592
1572
  } else {
1593
- const selection = element.ownerDocument.getSelection();
1594
- if (!selection) {
1595
- return;
1596
- }
1597
- if (selection.isCollapsed) {
1598
- const nextPosition = getNextCursorPosition(selection.focusNode, selection.focusOffset, keyDef.key === "ArrowLeft" ? -1 : 1);
1599
- if (nextPosition) {
1600
- setSelection({
1601
- focusNode: nextPosition.node,
1602
- focusOffset: nextPosition.offset
1603
- });
1604
- }
1605
- } else {
1606
- selection[keyDef.key === "ArrowLeft" ? "collapseToStart" : "collapseToEnd"]();
1607
- }
1573
+ return;
1608
1574
  }
1609
1575
  }
1610
1576
  }
1577
+ if (isEditable(target)) {
1578
+ const inputType = event.key === "Enter" ? isContentEditable(target) && !config.keyboardState.modifiers.Shift ? "insertParagraph" : "insertLineBreak" : "insertText";
1579
+ const inputData = event.key === "Enter" ? "\n" : event.key;
1580
+ return () => input(config, target, inputData, inputType);
1581
+ }
1582
+ };
1583
+ var ClickInputOnEnter = [
1584
+ "button",
1585
+ "color",
1586
+ "file",
1587
+ "image",
1588
+ "reset",
1589
+ "submit"
1590
+ ];
1591
+ var SubmitSingleInputOnEnter = [
1592
+ "email",
1593
+ "month",
1594
+ "password",
1595
+ "search",
1596
+ "tel",
1597
+ "text",
1598
+ "url",
1599
+ "week"
1611
1600
  ];
1612
1601
 
1613
- // src/keyboard/plugins/control.ts
1614
- var keydownBehavior2 = [
1615
- {
1616
- matches: (keyDef, element) => (keyDef.key === "Home" || keyDef.key === "End") && (isElementType(element, ["input", "textarea"]) || isContentEditable(element)),
1617
- handle: (keyDef, element) => {
1618
- var _a, _b;
1619
- if (keyDef.key === "Home") {
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);
1624
- }
1602
+ // src/event/behavior/keyup.ts
1603
+ behavior.keyup = (event, target, config) => {
1604
+ var _a;
1605
+ return (_a = keyupBehavior[event.key]) == null ? void 0 : _a.call(keyupBehavior, event, target, config);
1606
+ };
1607
+ var keyupBehavior = {
1608
+ " ": (event, target, config) => {
1609
+ if (isClickableInput(target)) {
1610
+ return () => dispatchUIEvent(config, target, "click");
1625
1611
  }
1626
- },
1627
- {
1628
- matches: (keyDef, element) => (keyDef.key === "PageUp" || keyDef.key === "PageDown") && isElementType(element, ["input"]),
1629
- handle: (keyDef, element) => {
1630
- var _a, _b;
1631
- if (keyDef.key === "PageUp") {
1632
- setSelectionRange(element, 0, 0);
1633
- } else {
1634
- const newPos = (_b = (_a = getValue(element)) == null ? void 0 : _a.length) != null ? _b : 0;
1635
- setSelectionRange(element, newPos, newPos);
1612
+ }
1613
+ };
1614
+
1615
+ // src/event/behavior/paste.ts
1616
+ behavior.paste = (event, target, config) => {
1617
+ if (isEditable(target)) {
1618
+ return () => {
1619
+ var _a;
1620
+ const insertData = (_a = event.clipboardData) == null ? void 0 : _a.getData("text");
1621
+ if (insertData) {
1622
+ input(config, target, insertData, "insertFromPaste");
1623
+ }
1624
+ };
1625
+ }
1626
+ };
1627
+
1628
+ // src/event/wrapEvent.ts
1629
+ import { getConfig as getConfig2 } from "@testing-library/dom";
1630
+ function wrapEvent(cb, _element) {
1631
+ return getConfig2().eventWrapper(cb);
1632
+ }
1633
+
1634
+ // src/event/dispatchEvent.ts
1635
+ function dispatchEvent(config, target, event, preventDefault = false) {
1636
+ var _a, _b;
1637
+ const type3 = event.type;
1638
+ const behaviorImplementation = preventDefault ? () => {
1639
+ } : (_b = (_a = behavior)[type3]) == null ? void 0 : _b.call(_a, event, target, config);
1640
+ if (behaviorImplementation) {
1641
+ event.preventDefault();
1642
+ let defaultPrevented = false;
1643
+ Object.defineProperty(event, "defaultPrevented", {
1644
+ get: () => defaultPrevented
1645
+ });
1646
+ Object.defineProperty(event, "preventDefault", {
1647
+ value: () => {
1648
+ defaultPrevented = event.cancelable;
1636
1649
  }
1650
+ });
1651
+ wrapEvent(() => target.dispatchEvent(event), target);
1652
+ if (!defaultPrevented) {
1653
+ behaviorImplementation();
1637
1654
  }
1638
- },
1639
- {
1640
- matches: (keyDef, element) => keyDef.key === "Delete" && isEditable(element),
1641
- handle: (keDef, element) => {
1642
- var _a;
1643
- (_a = prepareInput("", element, "deleteContentForward")) == null ? void 0 : _a.commit();
1655
+ return !defaultPrevented;
1656
+ }
1657
+ return wrapEvent(() => target.dispatchEvent(event), target);
1658
+ }
1659
+
1660
+ // src/event/index.ts
1661
+ function dispatchUIEvent(config, target, type3, init, preventDefault = false) {
1662
+ if (isMouseEvent(type3) || isKeyboardEvent(type3)) {
1663
+ init = __spreadValues(__spreadValues({}, init), getUIEventModifiers(config.keyboardState));
1664
+ }
1665
+ const event = createEvent(type3, target, init);
1666
+ return dispatchEvent(config, target, event, preventDefault);
1667
+ }
1668
+ function bindDispatchUIEvent(config) {
1669
+ return dispatchUIEvent.bind(void 0, config);
1670
+ }
1671
+
1672
+ // src/document/interceptor.ts
1673
+ var Interceptor = Symbol("Interceptor for programmatical calls");
1674
+ function prepareInterceptor(element, propName, interceptorImpl) {
1675
+ const prototypeDescriptor = Object.getOwnPropertyDescriptor(element.constructor.prototype, propName);
1676
+ const objectDescriptor = Object.getOwnPropertyDescriptor(element, propName);
1677
+ const target = (prototypeDescriptor == null ? void 0 : prototypeDescriptor.set) ? "set" : "value";
1678
+ if (typeof (prototypeDescriptor == null ? void 0 : prototypeDescriptor[target]) !== "function" || prototypeDescriptor[target][Interceptor]) {
1679
+ return;
1680
+ }
1681
+ function intercept(...args) {
1682
+ const { applyNative = true, realArgs } = interceptorImpl.call(this, ...args);
1683
+ const realFunc = (!applyNative && objectDescriptor || prototypeDescriptor)[target];
1684
+ if (target === "set") {
1685
+ realFunc.call(this, realArgs);
1686
+ } else {
1687
+ realFunc.call(this, ...realArgs);
1644
1688
  }
1645
1689
  }
1646
- ];
1690
+ ;
1691
+ intercept[Interceptor] = Interceptor;
1692
+ Object.defineProperty(element, propName, __spreadProps(__spreadValues({}, objectDescriptor != null ? objectDescriptor : prototypeDescriptor), {
1693
+ [target]: intercept
1694
+ }));
1695
+ }
1647
1696
 
1648
- // src/keyboard/plugins/character.ts
1649
- import { fireEvent as fireEvent6 } from "@testing-library/dom";
1697
+ // src/document/selection.ts
1698
+ var UISelection = Symbol("Displayed selection in UI");
1699
+ function prepareSelectionInterceptor(element) {
1700
+ prepareInterceptor(element, "setSelectionRange", function interceptorImpl(start, end, direction = "none") {
1701
+ const isUI = start && typeof start === "object" && start[UISelection];
1702
+ if (!isUI) {
1703
+ this[UISelection] = void 0;
1704
+ }
1705
+ return {
1706
+ realArgs: [Number(start), end, direction]
1707
+ };
1708
+ });
1709
+ prepareInterceptor(element, "selectionStart", function interceptorImpl(v) {
1710
+ this[UISelection] = void 0;
1711
+ return { realArgs: v };
1712
+ });
1713
+ prepareInterceptor(element, "selectionEnd", function interceptorImpl(v) {
1714
+ this[UISelection] = void 0;
1715
+ return { realArgs: v };
1716
+ });
1717
+ }
1718
+ function setUISelection(element, {
1719
+ focusOffset: focusOffsetParam,
1720
+ anchorOffset: anchorOffsetParam = focusOffsetParam
1721
+ }, mode = "replace") {
1722
+ const valueLength = getUIValue(element).length;
1723
+ const sanitizeOffset = (o) => Math.max(0, Math.min(valueLength, o));
1724
+ const anchorOffset = mode === "replace" || element[UISelection] === void 0 ? sanitizeOffset(anchorOffsetParam) : element[UISelection].anchorOffset;
1725
+ const focusOffset = sanitizeOffset(focusOffsetParam);
1726
+ const startOffset = Math.min(anchorOffset, focusOffset);
1727
+ const endOffset = Math.max(anchorOffset, focusOffset);
1728
+ element[UISelection] = {
1729
+ anchorOffset,
1730
+ focusOffset
1731
+ };
1732
+ if (element.selectionStart === startOffset && element.selectionEnd === endOffset) {
1733
+ return;
1734
+ }
1735
+ const startObj = new Number(startOffset);
1736
+ startObj[UISelection] = UISelection;
1737
+ try {
1738
+ element.setSelectionRange(startObj, endOffset);
1739
+ } catch (e) {
1740
+ }
1741
+ }
1742
+ function getUISelection(element) {
1743
+ var _a, _b, _c;
1744
+ const sel = (_c = element[UISelection]) != null ? _c : {
1745
+ anchorOffset: (_a = element.selectionStart) != null ? _a : 0,
1746
+ focusOffset: (_b = element.selectionEnd) != null ? _b : 0
1747
+ };
1748
+ return __spreadProps(__spreadValues({}, sel), {
1749
+ startOffset: Math.min(sel.anchorOffset, sel.focusOffset),
1750
+ endOffset: Math.max(sel.anchorOffset, sel.focusOffset)
1751
+ });
1752
+ }
1753
+ function clearUISelection(element) {
1754
+ element[UISelection] = void 0;
1755
+ }
1650
1756
 
1651
- // src/keyboard/shared/fireChangeForInputTimeIfValid.ts
1652
- import { fireEvent as fireEvent5 } from "@testing-library/dom";
1653
- function fireChangeForInputTimeIfValid(el, prevValue, timeNewEntry) {
1654
- if (isValidInputTimeValue(el, timeNewEntry) && prevValue !== timeNewEntry) {
1655
- fireEvent5.change(el, { target: { value: timeNewEntry } });
1757
+ // src/document/value.ts
1758
+ var UIValue = Symbol("Displayed value in UI");
1759
+ var InitialValue = Symbol("Initial value to compare on blur");
1760
+ var TrackChanges = Symbol("Track programmatic changes for React workaround");
1761
+ function valueInterceptor(v) {
1762
+ const isUI = typeof v === "object" && v[UIValue];
1763
+ this[UIValue] = isUI ? String(v) : void 0;
1764
+ if (!isUI) {
1765
+ trackValue(this, String(v));
1766
+ this[InitialValue] = String(v);
1767
+ clearUISelection(this);
1656
1768
  }
1769
+ return {
1770
+ applyNative: !!isUI,
1771
+ realArgs: String(v)
1772
+ };
1773
+ }
1774
+ function prepareValueInterceptor(element) {
1775
+ prepareInterceptor(element, "value", valueInterceptor);
1776
+ }
1777
+ function setUIValue(element, value) {
1778
+ element.value = {
1779
+ [UIValue]: UIValue,
1780
+ toString: () => value
1781
+ };
1782
+ }
1783
+ function getUIValue(element) {
1784
+ return element[UIValue] === void 0 ? element.value : String(element[UIValue]);
1785
+ }
1786
+ function setInitialValue(element) {
1787
+ element[InitialValue] = element.value;
1788
+ }
1789
+ function getInitialValue(element) {
1790
+ return element[InitialValue];
1791
+ }
1792
+ function startTrackValue(element) {
1793
+ element[TrackChanges] = [];
1794
+ }
1795
+ function trackValue(element, v) {
1796
+ var _a;
1797
+ (_a = element[TrackChanges]) == null ? void 0 : _a.push(v);
1798
+ }
1799
+ function endTrackValue(element) {
1800
+ const tracked = element[TrackChanges];
1801
+ element[TrackChanges] = void 0;
1802
+ return tracked;
1657
1803
  }
1658
1804
 
1659
- // src/keyboard/plugins/character.ts
1660
- var keypressBehavior = [
1661
- {
1662
- matches: (keyDef, element) => {
1663
- var _a;
1664
- return ((_a = keyDef.key) == null ? void 0 : _a.length) === 1 && isElementType(element, "input", { type: "time", readOnly: false });
1665
- },
1666
- handle: (keyDef, element, { keyboardState }) => {
1667
- var _a;
1668
- let newEntry = keyDef.key;
1669
- const textToBeTyped = ((_a = keyboardState.carryValue) != null ? _a : "") + newEntry;
1670
- const timeNewEntry = buildTimeValue(textToBeTyped);
1671
- if (isValidInputTimeValue(element, timeNewEntry)) {
1672
- newEntry = timeNewEntry;
1673
- }
1674
- const { newValue, newOffset } = calculateNewValue(newEntry, element, getInputRange(element));
1675
- const prevValue = getValue(element);
1676
- if (prevValue !== newValue) {
1677
- editInputElement(element, {
1678
- newValue,
1679
- newSelection: {
1680
- node: element,
1681
- offset: newOffset
1682
- },
1683
- eventOverrides: {
1684
- data: keyDef.key,
1685
- inputType: "insertText"
1686
- }
1687
- });
1688
- }
1689
- fireChangeForInputTimeIfValid(element, prevValue, timeNewEntry);
1690
- keyboardState.carryValue = textToBeTyped;
1691
- }
1692
- },
1693
- {
1694
- matches: (keyDef, element) => {
1695
- var _a;
1696
- return ((_a = keyDef.key) == null ? void 0 : _a.length) === 1 && isElementType(element, "input", { type: "date", readOnly: false });
1697
- },
1698
- handle: (keyDef, element, { keyboardState }) => {
1699
- var _a;
1700
- let newEntry = keyDef.key;
1701
- const textToBeTyped = ((_a = keyboardState.carryValue) != null ? _a : "") + newEntry;
1702
- const isValidToBeTyped = isValidDateValue(element, textToBeTyped);
1703
- if (isValidToBeTyped) {
1704
- newEntry = textToBeTyped;
1705
- }
1706
- const { newValue, newOffset } = calculateNewValue(newEntry, element, getInputRange(element));
1707
- const prevValue = getValue(element);
1708
- if (prevValue !== newValue) {
1709
- editInputElement(element, {
1710
- newValue,
1711
- newSelection: {
1712
- node: element,
1713
- offset: newOffset
1714
- },
1715
- eventOverrides: {
1716
- data: keyDef.key,
1717
- inputType: "insertText"
1718
- }
1719
- });
1720
- }
1721
- if (isValidToBeTyped) {
1722
- fireEvent6.change(element, {
1723
- target: { value: textToBeTyped }
1724
- });
1725
- }
1726
- keyboardState.carryValue = textToBeTyped;
1727
- }
1728
- },
1729
- {
1730
- matches: (keyDef, element) => {
1731
- var _a;
1732
- return ((_a = keyDef.key) == null ? void 0 : _a.length) === 1 && isElementType(element, "input", { type: "number", readOnly: false });
1733
- },
1734
- handle: (keyDef, element) => {
1735
- var _a, _b;
1736
- if (!/[\d.\-e]/.test(keyDef.key)) {
1737
- return;
1738
- }
1739
- const { getNewValue, commit } = prepareInput(keyDef.key, element);
1740
- const newValue = getNewValue();
1741
- const valueParts = newValue.split("e", 2);
1742
- 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])) {
1743
- return;
1744
- }
1745
- commit();
1746
- }
1747
- },
1748
- {
1749
- matches: (keyDef, element) => {
1750
- var _a;
1751
- return ((_a = keyDef.key) == null ? void 0 : _a.length) === 1 && (isEditableInput(element) || isElementType(element, "textarea", { readOnly: false }) || isContentEditable(element)) && getSpaceUntilMaxLength(element) !== 0;
1752
- },
1753
- handle: (keyDef, element) => {
1754
- var _a;
1755
- (_a = prepareInput(keyDef.key, element)) == null ? void 0 : _a.commit();
1756
- }
1757
- },
1758
- {
1759
- matches: (keyDef, element) => keyDef.key === "Enter" && (isElementType(element, "textarea", { readOnly: false }) || isContentEditable(element)) && getSpaceUntilMaxLength(element) !== 0,
1760
- handle: (keyDef, element, { keyboardState }) => {
1761
- var _a;
1762
- (_a = prepareInput("\n", element, isContentEditable(element) && !keyboardState.modifiers.Shift ? "insertParagraph" : "insertLineBreak")) == null ? void 0 : _a.commit();
1763
- }
1805
+ // src/document/index.ts
1806
+ var isPrepared = Symbol("Node prepared with document state workarounds");
1807
+ function prepareDocument(document2) {
1808
+ if (document2[isPrepared]) {
1809
+ return;
1764
1810
  }
1765
- ];
1766
-
1767
- // src/keyboard/plugins/functional.ts
1768
- import { fireEvent as fireEvent7 } from "@testing-library/dom";
1769
- var keydownBehavior3 = [
1770
- {
1771
- matches: (keyDef, element) => keyDef.key === "Backspace" && isEditable(element),
1772
- handle: (keyDef, element) => {
1773
- var _a;
1774
- (_a = prepareInput("", element, "deleteContentBackward")) == null ? void 0 : _a.commit();
1775
- }
1776
- },
1777
- {
1778
- matches: (keyDef) => keyDef.key === "Tab",
1779
- handle: (keyDef, element, { keyboardState }) => {
1780
- const dest = getTabDestination(element, keyboardState.modifiers.Shift);
1781
- if (dest === element.ownerDocument.body) {
1782
- blur(element);
1783
- } else {
1784
- focus(dest);
1785
- if (isElementType(dest, ["input", "textarea"])) {
1786
- setUISelection(dest, {
1787
- anchorOffset: 0,
1788
- focusOffset: dest.value.length
1789
- });
1790
- }
1791
- }
1792
- }
1811
+ document2.addEventListener("focus", (e) => {
1812
+ const el = e.target;
1813
+ prepareElement(el);
1814
+ }, {
1815
+ capture: true,
1816
+ passive: true
1817
+ });
1818
+ if (document2.activeElement) {
1819
+ prepareElement(document2.activeElement);
1793
1820
  }
1794
- ];
1795
- var keypressBehavior2 = [
1796
- {
1797
- matches: (keyDef, element) => keyDef.key === "Enter" && isElementType(element, "input") && ["checkbox", "radio"].includes(element.type),
1798
- handle: (keyDef, element) => {
1799
- const form = element.form;
1800
- if (hasFormSubmit(form)) {
1801
- fireEvent7.submit(form);
1802
- }
1803
- }
1804
- },
1805
- {
1806
- matches: (keyDef, element) => keyDef.key === "Enter" && (isClickableInput(element) || isElementType(element, "a") && Boolean(element.href)),
1807
- handle: (keyDef, element, { keyboardState }) => {
1808
- fireEvent7.click(element, getUIEventModifiers(keyboardState));
1809
- }
1810
- },
1811
- {
1812
- matches: (keyDef, element) => keyDef.key === "Enter" && isElementType(element, "input"),
1813
- handle: (keyDef, element) => {
1814
- const form = element.form;
1815
- if (form && (form.querySelectorAll("input").length === 1 || hasFormSubmit(form))) {
1816
- fireEvent7.submit(form);
1817
- }
1821
+ document2.addEventListener("blur", (e) => {
1822
+ const el = e.target;
1823
+ const initialValue = getInitialValue(el);
1824
+ if (typeof initialValue === "string" && el.value !== initialValue) {
1825
+ dispatchUIEvent({}, el, "change");
1818
1826
  }
1827
+ }, {
1828
+ capture: true,
1829
+ passive: true
1830
+ });
1831
+ document2[isPrepared] = isPrepared;
1832
+ }
1833
+ function prepareElement(el) {
1834
+ if ("value" in el) {
1835
+ setInitialValue(el);
1819
1836
  }
1820
- ];
1821
- var keyupBehavior = [
1822
- {
1823
- matches: (keyDef, element) => keyDef.key === " " && isClickableInput(element),
1824
- handle: (keyDef, element, { keyboardState }) => {
1825
- fireEvent7.click(element, getUIEventModifiers(keyboardState));
1826
- }
1837
+ if (el[isPrepared]) {
1838
+ return;
1827
1839
  }
1828
- ];
1829
-
1830
- // src/keyboard/plugins/combination.ts
1831
- var keydownBehavior4 = [
1832
- {
1833
- matches: (keyDef, element, { keyboardState }) => keyDef.code === "KeyA" && keyboardState.modifiers.Control,
1834
- handle: (keyDef, element) => selectAll(element)
1840
+ if ("value" in el) {
1841
+ prepareValueInterceptor(el);
1842
+ prepareSelectionInterceptor(el);
1835
1843
  }
1836
- ];
1844
+ el[isPrepared] = isPrepared;
1845
+ }
1837
1846
 
1838
- // src/keyboard/plugins/modifiers.ts
1839
- import { fireEvent as fireEvent8 } from "@testing-library/dom";
1847
+ // src/keyboard/modifiers.ts
1840
1848
  var modifierKeys = [
1841
1849
  "Alt",
1842
1850
  "AltGraph",
@@ -1846,6 +1854,9 @@ var modifierKeys = [
1846
1854
  "Shift",
1847
1855
  "Symbol"
1848
1856
  ];
1857
+ function isModifierKey(key) {
1858
+ return modifierKeys.includes(key);
1859
+ }
1849
1860
  var modifierLocks = [
1850
1861
  "CapsLock",
1851
1862
  "FnLock",
@@ -1853,78 +1864,40 @@ var modifierLocks = [
1853
1864
  "ScrollLock",
1854
1865
  "SymbolLock"
1855
1866
  ];
1856
- var preKeydownBehavior = [
1857
- {
1858
- matches: (keyDef) => modifierKeys.includes(keyDef.key),
1859
- handle: (keyDef, element, { keyboardMap, keyboardState }) => {
1860
- var _a;
1861
- keyboardState.modifiers[keyDef.key] = true;
1862
- if (keyDef.key === "AltGraph") {
1863
- const ctrlKeyDef = (_a = keyboardMap.find((k) => k.key === "Control")) != null ? _a : { key: "Control", code: "Control" };
1864
- fireEvent8.keyDown(element, getKeyEventProps(ctrlKeyDef, keyboardState));
1865
- }
1867
+ function isModifierLock(key) {
1868
+ return modifierLocks.includes(key);
1869
+ }
1870
+ function preKeydownBehavior(config, { key }, element) {
1871
+ var _a;
1872
+ if (isModifierKey(key)) {
1873
+ config.keyboardState.modifiers[key] = true;
1874
+ if (key === "AltGraph") {
1875
+ const ctrlKeyDef = (_a = config.keyboardMap.find((k) => k.key === "Control")) != null ? _a : { key: "Control", code: "Control" };
1876
+ dispatchUIEvent(config, element, "keydown", getKeyEventProps(ctrlKeyDef));
1866
1877
  }
1867
- },
1868
- {
1869
- matches: (keyDef) => modifierLocks.includes(keyDef.key),
1870
- handle: (keyDef, element, { keyboardState }) => {
1871
- const key = keyDef.key;
1872
- keyboardState.modifierPhase[key] = keyboardState.modifiers[key];
1873
- if (!keyboardState.modifierPhase[key]) {
1874
- keyboardState.modifiers[key] = true;
1875
- }
1878
+ } else if (isModifierLock(key)) {
1879
+ config.keyboardState.modifierPhase[key] = config.keyboardState.modifiers[key];
1880
+ if (!config.keyboardState.modifierPhase[key]) {
1881
+ config.keyboardState.modifiers[key] = true;
1876
1882
  }
1877
1883
  }
1878
- ];
1879
- var preKeyupBehavior = [
1880
- {
1881
- matches: (keyDef) => modifierKeys.includes(keyDef.key),
1882
- handle: (keyDef, element, { keyboardState }) => {
1883
- keyboardState.modifiers[keyDef.key] = false;
1884
- }
1885
- },
1886
- {
1887
- matches: (keyDef) => modifierLocks.includes(keyDef.key),
1888
- handle: (keyDef, element, { keyboardState }) => {
1889
- const key = keyDef.key;
1890
- if (keyboardState.modifierPhase[key]) {
1891
- keyboardState.modifiers[key] = false;
1892
- }
1884
+ }
1885
+ function preKeyupBehavior(config, { key }) {
1886
+ if (isModifierKey(key)) {
1887
+ config.keyboardState.modifiers[key] = false;
1888
+ } else if (isModifierLock(key)) {
1889
+ if (config.keyboardState.modifierPhase[key]) {
1890
+ config.keyboardState.modifiers[key] = false;
1893
1891
  }
1894
1892
  }
1895
- ];
1896
- var postKeyupBehavior = [
1897
- {
1898
- matches: (keyDef) => keyDef.key === "AltGraph",
1899
- handle: (keyDef, element, { keyboardMap, keyboardState }) => {
1900
- var _a;
1901
- const ctrlKeyDef = (_a = keyboardMap.find((k) => k.key === "Control")) != null ? _a : { key: "Control", code: "Control" };
1902
- fireEvent8.keyUp(element, getKeyEventProps(ctrlKeyDef, keyboardState));
1903
- }
1893
+ }
1894
+ function postKeyupBehavior(config, { key }, element) {
1895
+ var _a;
1896
+ if (key === "AltGraph") {
1897
+ const ctrlKeyDef = (_a = config.keyboardMap.find((k) => k.key === "Control")) != null ? _a : { key: "Control", code: "Control" };
1898
+ dispatchUIEvent(config, element, "keyup", getKeyEventProps(ctrlKeyDef));
1904
1899
  }
1905
- ];
1906
-
1907
- // src/keyboard/plugins/index.ts
1908
- var preKeydownBehavior2 = [
1909
- ...preKeydownBehavior
1910
- ];
1911
- var keydownBehavior5 = [
1912
- ...keydownBehavior,
1913
- ...keydownBehavior2,
1914
- ...keydownBehavior3,
1915
- ...keydownBehavior4
1916
- ];
1917
- var keypressBehavior3 = [
1918
- ...keypressBehavior2,
1919
- ...keypressBehavior
1920
- ];
1921
- var preKeyupBehavior2 = [
1922
- ...preKeyupBehavior
1923
- ];
1924
- var keyupBehavior2 = [...keyupBehavior];
1925
- var postKeyupBehavior2 = [
1926
- ...postKeyupBehavior
1927
- ];
1900
+ }
1928
1901
 
1929
1902
  // src/keyboard/keyboardAction.ts
1930
1903
  function keyboardAction(config, actions) {
@@ -1982,45 +1955,29 @@ function keydown(keyDef, getCurrentElement, config) {
1982
1955
  config.keyboardState.carryChar = "";
1983
1956
  }
1984
1957
  config.keyboardState.activeElement = element;
1985
- applyPlugins(preKeydownBehavior2, keyDef, element, config);
1986
- const unpreventedDefault = fireEvent9.keyDown(element, getKeyEventProps(keyDef, config.keyboardState));
1958
+ preKeydownBehavior(config, keyDef, element);
1959
+ const unpreventedDefault = dispatchUIEvent(config, element, "keydown", getKeyEventProps(keyDef));
1987
1960
  config.keyboardState.pressed.push({ keyDef, unpreventedDefault });
1988
- if (unpreventedDefault) {
1989
- applyPlugins(keydownBehavior5, keyDef, getCurrentElement(), config);
1990
- }
1991
1961
  return unpreventedDefault;
1992
1962
  });
1993
1963
  }
1994
1964
  function keypress(keyDef, getCurrentElement, config) {
1995
1965
  return __async(this, null, function* () {
1996
1966
  const element = getCurrentElement();
1997
- const unpreventedDefault = fireEvent9.keyPress(element, __spreadProps(__spreadValues({}, getKeyEventProps(keyDef, config.keyboardState)), {
1967
+ dispatchUIEvent(config, element, "keypress", __spreadProps(__spreadValues({}, getKeyEventProps(keyDef)), {
1998
1968
  charCode: keyDef.key === "Enter" ? 13 : String(keyDef.key).charCodeAt(0)
1999
1969
  }));
2000
- if (unpreventedDefault) {
2001
- applyPlugins(keypressBehavior3, keyDef, getCurrentElement(), config);
2002
- }
2003
1970
  });
2004
1971
  }
2005
1972
  function keyup(keyDef, getCurrentElement, config, unprevented) {
2006
1973
  return __async(this, null, function* () {
2007
1974
  const element = getCurrentElement();
2008
- applyPlugins(preKeyupBehavior2, keyDef, element, config);
2009
- const unpreventedDefault = fireEvent9.keyUp(element, getKeyEventProps(keyDef, config.keyboardState));
2010
- if (unprevented && unpreventedDefault) {
2011
- applyPlugins(keyupBehavior2, keyDef, getCurrentElement(), config);
2012
- }
1975
+ preKeyupBehavior(config, keyDef);
1976
+ dispatchUIEvent(config, element, "keyup", getKeyEventProps(keyDef), !unprevented);
2013
1977
  config.keyboardState.pressed = config.keyboardState.pressed.filter((k) => k.keyDef !== keyDef);
2014
- applyPlugins(postKeyupBehavior2, keyDef, element, config);
1978
+ postKeyupBehavior(config, keyDef, element);
2015
1979
  });
2016
1980
  }
2017
- function applyPlugins(pluginCollection, keyDef, element, config) {
2018
- const plugin = pluginCollection.find((p) => p.matches(keyDef, element, config));
2019
- if (plugin) {
2020
- plugin.handle(keyDef, element, config);
2021
- }
2022
- return !!plugin;
2023
- }
2024
1981
  function hasKeyPress(keyDef, config) {
2025
1982
  var _a;
2026
1983
  return (((_a = keyDef.key) == null ? void 0 : _a.length) === 1 || keyDef.key === "Enter") && !config.keyboardState.modifiers.Control && !config.keyboardState.modifiers.Alt;
@@ -2038,7 +1995,7 @@ function parseKeyDef(keyboardMap, text) {
2038
1995
  releasePrevious,
2039
1996
  releaseSelf = true,
2040
1997
  repeat
2041
- } = readNextDescriptor(text);
1998
+ } = readNextDescriptor(text, "keyboard");
2042
1999
  const keyDef = (_a = keyboardMap.find((def) => {
2043
2000
  var _a2, _b;
2044
2001
  if (type3 === "[") {
@@ -2061,8 +2018,7 @@ function parseKeyDef(keyboardMap, text) {
2061
2018
  // src/keyboard/index.ts
2062
2019
  function keyboard(text) {
2063
2020
  return __async(this, null, function* () {
2064
- const { keyboardMap } = this[Config];
2065
- const actions = parseKeyDef(keyboardMap, text);
2021
+ const actions = parseKeyDef(this[Config].keyboardMap, text);
2066
2022
  return keyboardAction(this[Config], actions);
2067
2023
  });
2068
2024
  }
@@ -2098,7 +2054,7 @@ function parseKeyDef2(pointerMap, keys) {
2098
2054
  consumedLength,
2099
2055
  releasePrevious,
2100
2056
  releaseSelf = true
2101
- } = readNextDescriptor(keys);
2057
+ } = readNextDescriptor(keys, "pointer");
2102
2058
  const keyDef = pointerMap.find((p) => p.name === descriptor);
2103
2059
  if (keyDef) {
2104
2060
  defs.push({ keyDef, releasePrevious, releaseSelf });
@@ -2108,6 +2064,34 @@ function parseKeyDef2(pointerMap, keys) {
2108
2064
  return defs;
2109
2065
  }
2110
2066
 
2067
+ // src/pointer/firePointerEvents.ts
2068
+ function firePointerEvent(config, target, type3, {
2069
+ pointerType,
2070
+ button,
2071
+ coords,
2072
+ pointerId,
2073
+ isPrimary,
2074
+ clickCount
2075
+ }) {
2076
+ const init = __spreadValues({}, coords);
2077
+ if (type3 === "click" || type3.startsWith("pointer")) {
2078
+ init.pointerId = pointerId;
2079
+ init.pointerType = pointerType;
2080
+ }
2081
+ if (["pointerdown", "pointerup"].includes(type3)) {
2082
+ init.isPrimary = isPrimary;
2083
+ }
2084
+ init.button = getMouseButton(button != null ? button : 0);
2085
+ init.buttons = getMouseButtons(...config.pointerState.pressed.filter((p) => p.keyDef.pointerType === pointerType).map((p) => {
2086
+ var _a;
2087
+ return (_a = p.keyDef.button) != null ? _a : 0;
2088
+ }));
2089
+ if (["mousedown", "mouseup", "click", "dblclick", "contextmenu"].includes(type3)) {
2090
+ init.detail = clickCount;
2091
+ }
2092
+ return dispatchUIEvent(config, target, type3, init);
2093
+ }
2094
+
2111
2095
  // src/pointer/resolveSelectionTarget.ts
2112
2096
  function resolveSelectionTarget({
2113
2097
  target,
@@ -2155,7 +2139,7 @@ function findNodeAtTextOffset(node, offset, isRoot = true) {
2155
2139
  // src/pointer/pointerMove.ts
2156
2140
  function pointerMove(_0, _1) {
2157
2141
  return __async(this, arguments, function* (config, { pointerName = "mouse", target, coords, node, offset }) {
2158
- const { pointerState, keyboardState } = config;
2142
+ const { pointerState } = config;
2159
2143
  if (!(pointerName in pointerState.position)) {
2160
2144
  throw new Error(`Trying to move pointer "${pointerName}" which does not exist.`);
2161
2145
  }
@@ -2230,9 +2214,7 @@ function pointerMove(_0, _1) {
2230
2214
  }
2231
2215
  }
2232
2216
  function fire(eventTarget, type3, eventCoords) {
2233
- return firePointerEvent(eventTarget, type3, {
2234
- pointerState,
2235
- keyboardState,
2217
+ return firePointerEvent(config, eventTarget, type3, {
2236
2218
  coords: eventCoords,
2237
2219
  pointerId,
2238
2220
  pointerType
@@ -2267,7 +2249,7 @@ function down(config, pointerName, { keyDef, node, offset, target, coords }, tar
2267
2249
  var _a, _b, _c;
2268
2250
  setLevelRef(config, 2 /* Trigger */);
2269
2251
  assertPointerEvents(config, target);
2270
- const { pointerState, keyboardState } = config;
2252
+ const { pointerState } = config;
2271
2253
  const { name, pointerType, button } = keyDef;
2272
2254
  const pointerId = pointerType === "mouse" ? 1 : getNextPointerId(pointerState);
2273
2255
  pointerState.position[pointerName] = __spreadProps(__spreadValues({}, pointerState.position[pointerName]), {
@@ -2329,9 +2311,7 @@ function down(config, pointerName, { keyDef, node, offset, target, coords }, tar
2329
2311
  }
2330
2312
  return pressObj;
2331
2313
  function fire(type3) {
2332
- return firePointerEvent(target, type3, {
2333
- pointerState,
2334
- keyboardState,
2314
+ return firePointerEvent(config, target, type3, {
2335
2315
  button,
2336
2316
  clickCount,
2337
2317
  coords,
@@ -2348,10 +2328,9 @@ function up(config, pointerName, {
2348
2328
  node,
2349
2329
  offset
2350
2330
  }, pressed, targetIsDisabled) {
2351
- var _a;
2352
2331
  setLevelRef(config, 2 /* Trigger */);
2353
2332
  assertPointerEvents(config, target);
2354
- const { pointerState, keyboardState } = config;
2333
+ const { pointerState } = config;
2355
2334
  pointerState.pressed = pointerState.pressed.filter((p) => p !== pressed);
2356
2335
  const { isMultiTouch, isPrimary, pointerId, clickCount } = pressed;
2357
2336
  let { unpreventedDefault } = pressed;
@@ -2392,21 +2371,15 @@ function up(config, pointerName, {
2392
2371
  unpreventedDefault = fire("mouseup") && unpreventedDefault;
2393
2372
  const canClick = pointerType !== "mouse" || button === "primary";
2394
2373
  if (canClick && target === pressed.downTarget) {
2395
- const unpreventedClick = fire("click");
2374
+ fire("click");
2396
2375
  if (clickCount === 2) {
2397
2376
  fire("dblclick");
2398
2377
  }
2399
- const control = (_a = target.closest("label")) == null ? void 0 : _a.control;
2400
- if (unpreventedClick && control && isFocusable(control)) {
2401
- focus(control);
2402
- }
2403
2378
  }
2404
2379
  }
2405
2380
  }
2406
2381
  function fire(type3) {
2407
- return firePointerEvent(target, type3, {
2408
- pointerState,
2409
- keyboardState,
2382
+ return firePointerEvent(config, target, type3, {
2410
2383
  button,
2411
2384
  clickCount,
2412
2385
  coords,
@@ -2424,8 +2397,7 @@ function mousedownDefaultBehavior({
2424
2397
  node,
2425
2398
  offset
2426
2399
  }) {
2427
- var _a;
2428
- focus((_a = findClosest(target, isFocusable)) != null ? _a : target.ownerDocument.body);
2400
+ focus(target);
2429
2401
  if (!targetIsDisabled) {
2430
2402
  const hasValue = isElementType(target, ["input", "textarea"]);
2431
2403
  const text = String(hasValue ? getUIValue(target) : target.textContent);
@@ -2505,11 +2477,11 @@ function getPrevTarget(pointerName, state) {
2505
2477
  }
2506
2478
 
2507
2479
  // src/pointer/index.ts
2508
- function pointer(input) {
2480
+ function pointer(input2) {
2509
2481
  return __async(this, null, function* () {
2510
2482
  const { pointerMap } = this[Config];
2511
2483
  const actions = [];
2512
- (Array.isArray(input) ? input : [input]).forEach((actionInput) => {
2484
+ (Array.isArray(input2) ? input2 : [input2]).forEach((actionInput) => {
2513
2485
  if (typeof actionInput === "string") {
2514
2486
  actions.push(...parseKeyDef2(pointerMap, actionInput));
2515
2487
  } else if ("keys" in actionInput) {
@@ -2613,7 +2585,6 @@ function tab() {
2613
2585
  }
2614
2586
 
2615
2587
  // src/clipboard/copy.ts
2616
- import { fireEvent as fireEvent10 } from "@testing-library/dom";
2617
2588
  function copy() {
2618
2589
  return __async(this, null, function* () {
2619
2590
  var _a;
@@ -2623,10 +2594,9 @@ function copy() {
2623
2594
  if (clipboardData.items.length === 0) {
2624
2595
  return;
2625
2596
  }
2626
- fireEvent10.copy(target, {
2597
+ if (this.dispatchUIEvent(target, "copy", {
2627
2598
  clipboardData
2628
- });
2629
- if (this[Config].writeToClipboard) {
2599
+ }) && this[Config].writeToClipboard) {
2630
2600
  yield writeDataTransferToClipboard(doc, clipboardData);
2631
2601
  }
2632
2602
  return clipboardData;
@@ -2634,53 +2604,37 @@ function copy() {
2634
2604
  }
2635
2605
 
2636
2606
  // src/clipboard/cut.ts
2637
- import { fireEvent as fireEvent11 } from "@testing-library/dom";
2638
2607
  function cut() {
2639
2608
  return __async(this, null, function* () {
2640
- var _a, _b;
2609
+ var _a;
2641
2610
  const doc = this[Config].document;
2642
2611
  const target = (_a = doc.activeElement) != null ? _a : doc.body;
2643
2612
  const clipboardData = copySelection(target);
2644
2613
  if (clipboardData.items.length === 0) {
2645
2614
  return;
2646
2615
  }
2647
- fireEvent11.cut(target, {
2616
+ if (this.dispatchUIEvent(target, "cut", {
2648
2617
  clipboardData
2649
- });
2650
- if (isEditable(target)) {
2651
- (_b = prepareInput("", target, "deleteByCut")) == null ? void 0 : _b.commit();
2652
- }
2653
- if (this[Config].writeToClipboard) {
2654
- yield writeDataTransferToClipboard(doc, clipboardData);
2618
+ }) && this[Config].writeToClipboard) {
2619
+ yield writeDataTransferToClipboard(target.ownerDocument, clipboardData);
2655
2620
  }
2656
2621
  return clipboardData;
2657
2622
  });
2658
2623
  }
2659
2624
 
2660
2625
  // src/clipboard/paste.ts
2661
- import { fireEvent as fireEvent12 } from "@testing-library/dom";
2662
2626
  function paste(clipboardData) {
2663
2627
  return __async(this, null, function* () {
2664
2628
  var _a, _b;
2665
2629
  const doc = this[Config].document;
2666
2630
  const target = (_a = doc.activeElement) != null ? _a : doc.body;
2667
- const data = (_b = typeof clipboardData === "string" ? getClipboardDataFromString(clipboardData) : clipboardData) != null ? _b : yield readDataTransferFromClipboard(doc).catch(() => {
2631
+ const dataTransfer = (_b = typeof clipboardData === "string" ? getClipboardDataFromString(clipboardData) : clipboardData) != null ? _b : yield readDataTransferFromClipboard(doc).catch(() => {
2668
2632
  throw new Error("`userEvent.paste()` without `clipboardData` requires the `ClipboardAPI` to be available.");
2669
2633
  });
2670
- return pasteImpl(target, data);
2671
- });
2672
- }
2673
- function pasteImpl(target, clipboardData) {
2674
- var _a;
2675
- fireEvent12.paste(target, {
2676
- clipboardData
2634
+ this.dispatchUIEvent(target, "paste", {
2635
+ clipboardData: dataTransfer
2636
+ });
2677
2637
  });
2678
- if (isEditable(target)) {
2679
- const data = clipboardData.getData("text").substr(0, getSpaceUntilMaxLength(target));
2680
- if (data) {
2681
- (_a = prepareInput(data, target, "insertFromPaste")) == null ? void 0 : _a.commit();
2682
- }
2683
- }
2684
2638
  }
2685
2639
  function getClipboardDataFromString(text) {
2686
2640
  const dt = createDataTransfer();
@@ -2691,7 +2645,6 @@ function getClipboardDataFromString(text) {
2691
2645
  // src/utility/clear.ts
2692
2646
  function clear(element) {
2693
2647
  return __async(this, null, function* () {
2694
- var _a;
2695
2648
  if (!isEditable(element) || isDisabled(element)) {
2696
2649
  throw new Error("clear()` is only supported on editable elements.");
2697
2650
  }
@@ -2703,12 +2656,12 @@ function clear(element) {
2703
2656
  if (!isAllSelected(element)) {
2704
2657
  throw new Error("The element content to be cleared could not be selected.");
2705
2658
  }
2706
- (_a = prepareInput("", element, "deleteContentBackward")) == null ? void 0 : _a.commit();
2659
+ input(this[Config], element, "", "deleteContentBackward");
2707
2660
  });
2708
2661
  }
2709
2662
 
2710
2663
  // src/utility/selectOptions.ts
2711
- import { createEvent as createEvent2, getConfig as getConfig2, fireEvent as fireEvent13 } from "@testing-library/dom";
2664
+ import { getConfig as getConfig3 } from "@testing-library/dom";
2712
2665
  function selectOptions(select, values) {
2713
2666
  return __async(this, null, function* () {
2714
2667
  return selectOptionsBase.call(this, true, select, values);
@@ -2722,7 +2675,7 @@ function deselectOptions(select, values) {
2722
2675
  function selectOptionsBase(newValue, select, values) {
2723
2676
  return __async(this, null, function* () {
2724
2677
  if (!newValue && !select.multiple) {
2725
- throw getConfig2().getElementError(`Unable to deselect an option in a non-multiple select. Use selectOptions to change the selection instead.`, select);
2678
+ throw getConfig3().getElementError(`Unable to deselect an option in a non-multiple select. Use selectOptions to change the selection instead.`, select);
2726
2679
  }
2727
2680
  const valArray = Array.isArray(values) ? values : [values];
2728
2681
  const allOptions = Array.from(select.querySelectorAll('option, [role="option"]'));
@@ -2734,34 +2687,43 @@ function selectOptionsBase(newValue, select, values) {
2734
2687
  if (matchingOption) {
2735
2688
  return matchingOption;
2736
2689
  } else {
2737
- throw getConfig2().getElementError(`Value "${String(val)}" not found in options`, select);
2690
+ throw getConfig3().getElementError(`Value "${String(val)}" not found in options`, select);
2738
2691
  }
2739
2692
  }
2740
2693
  }).filter((option) => !isDisabled(option));
2741
2694
  if (isDisabled(select) || !selectedOptions.length)
2742
2695
  return;
2696
+ const selectOption = (option) => {
2697
+ option.selected = newValue;
2698
+ this.dispatchUIEvent(select, "input", {
2699
+ bubbles: true,
2700
+ cancelable: false,
2701
+ composed: true
2702
+ });
2703
+ this.dispatchUIEvent(select, "change");
2704
+ };
2743
2705
  if (isElementType(select, "select")) {
2744
2706
  if (select.multiple) {
2745
2707
  for (const option of selectedOptions) {
2746
2708
  const withPointerEvents = this[Config].pointerEventsCheck === 0 ? true : hasPointerEvents(option);
2747
2709
  if (withPointerEvents) {
2748
- fireEvent13.pointerOver(option);
2749
- fireEvent13.pointerEnter(select);
2750
- fireEvent13.mouseOver(option);
2751
- fireEvent13.mouseEnter(select);
2752
- fireEvent13.pointerMove(option);
2753
- fireEvent13.mouseMove(option);
2754
- fireEvent13.pointerDown(option);
2755
- fireEvent13.mouseDown(option);
2710
+ this.dispatchUIEvent(option, "pointerover");
2711
+ this.dispatchUIEvent(select, "pointerenter");
2712
+ this.dispatchUIEvent(option, "mouseover");
2713
+ this.dispatchUIEvent(select, "mouseenter");
2714
+ this.dispatchUIEvent(option, "pointermove");
2715
+ this.dispatchUIEvent(option, "mousemove");
2716
+ this.dispatchUIEvent(option, "pointerdown");
2717
+ this.dispatchUIEvent(option, "mousedown");
2756
2718
  }
2757
2719
  focus(select);
2758
2720
  if (withPointerEvents) {
2759
- fireEvent13.pointerUp(option);
2760
- fireEvent13.mouseUp(option);
2721
+ this.dispatchUIEvent(option, "pointerup");
2722
+ this.dispatchUIEvent(option, "mouseup");
2761
2723
  }
2762
2724
  selectOption(option);
2763
2725
  if (withPointerEvents) {
2764
- fireEvent13.click(option);
2726
+ this.dispatchUIEvent(option, "click");
2765
2727
  }
2766
2728
  }
2767
2729
  } else if (selectedOptions.length === 1) {
@@ -2773,16 +2735,16 @@ function selectOptionsBase(newValue, select, values) {
2773
2735
  }
2774
2736
  selectOption(selectedOptions[0]);
2775
2737
  if (withPointerEvents) {
2776
- fireEvent13.pointerOver(select);
2777
- fireEvent13.pointerEnter(select);
2778
- fireEvent13.mouseOver(select);
2779
- fireEvent13.mouseEnter(select);
2780
- fireEvent13.pointerUp(select);
2781
- fireEvent13.mouseUp(select);
2782
- fireEvent13.click(select);
2738
+ this.dispatchUIEvent(select, "pointerover");
2739
+ this.dispatchUIEvent(select, "pointerenter");
2740
+ this.dispatchUIEvent(select, "mouseover");
2741
+ this.dispatchUIEvent(select, "mouseenter");
2742
+ this.dispatchUIEvent(select, "pointerup");
2743
+ this.dispatchUIEvent(select, "mouseup");
2744
+ this.dispatchUIEvent(select, "click");
2783
2745
  }
2784
2746
  } else {
2785
- throw getConfig2().getElementError(`Cannot select multiple options on a non-multiple select`, select);
2747
+ throw getConfig3().getElementError(`Cannot select multiple options on a non-multiple select`, select);
2786
2748
  }
2787
2749
  } else if (select.getAttribute("role") === "listbox") {
2788
2750
  for (const option of selectedOptions) {
@@ -2790,16 +2752,7 @@ function selectOptionsBase(newValue, select, values) {
2790
2752
  yield this.unhover(option);
2791
2753
  }
2792
2754
  } else {
2793
- throw getConfig2().getElementError(`Cannot select options on elements that are neither select nor listbox elements`, select);
2794
- }
2795
- function selectOption(option) {
2796
- option.selected = newValue;
2797
- fireEvent13(select, createEvent2("input", select, {
2798
- bubbles: true,
2799
- cancelable: false,
2800
- composed: true
2801
- }));
2802
- fireEvent13.change(select);
2755
+ throw getConfig3().getElementError(`Cannot select options on elements that are neither select nor listbox elements`, select);
2803
2756
  }
2804
2757
  });
2805
2758
  }
@@ -2828,29 +2781,30 @@ function type(_0, _1) {
2828
2781
  }
2829
2782
 
2830
2783
  // src/utility/upload.ts
2831
- import { fireEvent as fireEvent14 } from "@testing-library/dom";
2832
2784
  function upload(element, fileOrFiles) {
2833
2785
  return __async(this, null, function* () {
2834
- var _a;
2835
- const input = isElementType(element, "label") ? element.control : element;
2836
- if (!input || !isElementType(input, "input", { type: "file" })) {
2837
- throw new TypeError(`The ${input === element ? "given" : "associated"} ${input == null ? void 0 : input.tagName} element does not accept file uploads`);
2786
+ const input2 = isElementType(element, "label") ? element.control : element;
2787
+ if (!input2 || !isElementType(input2, "input", { type: "file" })) {
2788
+ throw new TypeError(`The ${input2 === element ? "given" : "associated"} ${input2 == null ? void 0 : input2.tagName} element does not accept file uploads`);
2838
2789
  }
2839
2790
  if (isDisabled(element))
2840
2791
  return;
2792
+ const files = (Array.isArray(fileOrFiles) ? fileOrFiles : [fileOrFiles]).filter((file) => !this[Config].applyAccept || isAcceptableFile(file, input2.accept)).slice(0, input2.multiple ? void 0 : 1);
2793
+ const fileDialog = () => {
2794
+ var _a;
2795
+ if (files.length === ((_a = input2.files) == null ? void 0 : _a.length) && files.every((f, i) => {
2796
+ var _a2;
2797
+ return f === ((_a2 = input2.files) == null ? void 0 : _a2.item(i));
2798
+ })) {
2799
+ return;
2800
+ }
2801
+ setFiles(input2, createFileList(files));
2802
+ this.dispatchUIEvent(input2, "input");
2803
+ this.dispatchUIEvent(input2, "change");
2804
+ };
2805
+ input2.addEventListener("fileDialog", fileDialog);
2841
2806
  yield this.click(element);
2842
- const files = (Array.isArray(fileOrFiles) ? fileOrFiles : [fileOrFiles]).filter((file) => !this[Config].applyAccept || isAcceptableFile(file, input.accept)).slice(0, input.multiple ? void 0 : 1);
2843
- blur(element);
2844
- focus(element);
2845
- if (files.length === ((_a = input.files) == null ? void 0 : _a.length) && files.every((f, i) => {
2846
- var _a2;
2847
- return f === ((_a2 = input.files) == null ? void 0 : _a2.item(i));
2848
- })) {
2849
- return;
2850
- }
2851
- setFiles(input, createFileList(files));
2852
- fireEvent14.input(input);
2853
- fireEvent14.change(input);
2807
+ input2.removeEventListener("fileDialog", fileDialog);
2854
2808
  });
2855
2809
  }
2856
2810
  function isAcceptableFile(file, accept) {
@@ -2875,24 +2829,29 @@ function wrapAsync(implementation) {
2875
2829
  }
2876
2830
 
2877
2831
  // src/setup/setup.ts
2832
+ function createConfig(options = {}, defaults = defaultOptionsSetup, node) {
2833
+ const document2 = getDocument(options, node);
2834
+ const {
2835
+ keyboardState = createKeyboardState(),
2836
+ pointerState = createPointerState(document2)
2837
+ } = options;
2838
+ return __spreadProps(__spreadValues(__spreadValues({}, defaults), options), {
2839
+ document: document2,
2840
+ keyboardState,
2841
+ pointerState
2842
+ });
2843
+ }
2878
2844
  function setupMain(options = {}) {
2879
2845
  var _a;
2880
- const doc = getDocument(options);
2881
- prepareDocument(doc);
2882
- const view = (_a = doc.defaultView) != null ? _a : window;
2846
+ const config = createConfig(options);
2847
+ prepareDocument(config.document);
2848
+ const view = (_a = config.document.defaultView) != null ? _a : window;
2883
2849
  attachClipboardStubToView(view);
2884
- return doSetup(__spreadProps(__spreadValues(__spreadValues({}, defaultOptionsSetup), options), {
2885
- keyboardState: createKeyboardState(),
2886
- pointerState: createPointerState(doc)
2887
- }));
2850
+ return doSetup(config);
2888
2851
  }
2889
2852
  function setupDirect(options = {}, node) {
2890
- const doc = getDocument(options, node);
2891
- prepareDocument(doc);
2892
- const config = __spreadValues(__spreadValues({
2893
- keyboardState: createKeyboardState(),
2894
- pointerState: createPointerState(doc)
2895
- }, defaultOptionsDirect), options);
2853
+ const config = createConfig(options, defaultOptionsDirect, node);
2854
+ prepareDocument(config.document);
2896
2855
  return {
2897
2856
  config,
2898
2857
  api: doSetup(config)
@@ -2911,7 +2870,8 @@ function wrapAndBindImpl(instance, impl) {
2911
2870
  }
2912
2871
  function doSetup(config) {
2913
2872
  const instance = __spreadValues({
2914
- [Config]: config
2873
+ [Config]: config,
2874
+ dispatchUIEvent: bindDispatchUIEvent(config)
2915
2875
  }, api_exports);
2916
2876
  return __spreadProps(__spreadValues({}, Object.fromEntries(Object.entries(api_exports).map(([name, api]) => [
2917
2877
  name,
@@ -2973,9 +2933,9 @@ function keyboard2(_0) {
2973
2933
  });
2974
2934
  }
2975
2935
  function pointer2(_0) {
2976
- return __async(this, arguments, function* (input, options = {}) {
2936
+ return __async(this, arguments, function* (input2, options = {}) {
2977
2937
  const { config, api } = setupDirect(options);
2978
- return api.pointer(input).then(() => config.pointerState);
2938
+ return api.pointer(input2).then(() => config.pointerState);
2979
2939
  });
2980
2940
  }
2981
2941
  function paste2(clipboardData, options) {