@testing-library/user-event 13.5.0 → 14.0.0-alpha.12

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 (125) hide show
  1. package/README.md +3 -0
  2. package/dist/clear.d.ts +2 -2
  3. package/dist/clear.js +14 -29
  4. package/dist/click.d.ts +4 -3
  5. package/dist/click.js +30 -145
  6. package/dist/document/index.d.ts +10 -0
  7. package/dist/document/index.js +100 -0
  8. package/dist/document/interceptor.d.ts +15 -0
  9. package/dist/document/interceptor.js +37 -0
  10. package/dist/document/selection.d.ts +27 -0
  11. package/dist/document/selection.js +90 -0
  12. package/dist/document/value.d.ts +18 -0
  13. package/dist/document/value.js +77 -0
  14. package/dist/hover.d.ts +3 -3
  15. package/dist/hover.js +17 -59
  16. package/dist/index.d.ts +3 -22
  17. package/dist/index.js +3 -36
  18. package/dist/keyboard/getEventProps.d.ts +0 -2
  19. package/dist/keyboard/getEventProps.js +1 -7
  20. package/dist/keyboard/getNextKeyDef.d.ts +0 -1
  21. package/dist/keyboard/getNextKeyDef.js +4 -123
  22. package/dist/keyboard/index.d.ts +1 -1
  23. package/dist/keyboard/index.js +4 -8
  24. package/dist/keyboard/keyMap.js +26 -46
  25. package/dist/keyboard/keyboardImplementation.js +15 -17
  26. package/dist/keyboard/plugins/arrow.js +7 -12
  27. package/dist/keyboard/plugins/character.js +29 -57
  28. package/dist/keyboard/plugins/combination.d.ts +5 -0
  29. package/dist/keyboard/plugins/combination.js +17 -0
  30. package/dist/keyboard/plugins/control.js +5 -16
  31. package/dist/keyboard/plugins/functional.js +23 -14
  32. package/dist/keyboard/plugins/index.d.ts +0 -1
  33. package/dist/keyboard/plugins/index.js +4 -13
  34. package/dist/keyboard/shared/index.d.ts +0 -2
  35. package/dist/keyboard/shared/index.js +0 -26
  36. package/dist/keyboard/types.d.ts +3 -2
  37. package/dist/paste.d.ts +3 -2
  38. package/dist/paste.js +8 -15
  39. package/dist/pointer/index.d.ts +18 -0
  40. package/dist/pointer/index.js +90 -0
  41. package/dist/pointer/keyMap.d.ts +2 -0
  42. package/dist/pointer/keyMap.js +29 -0
  43. package/dist/pointer/parseKeyDef.d.ts +6 -0
  44. package/dist/pointer/parseKeyDef.js +36 -0
  45. package/dist/pointer/pointerAction.d.ts +6 -0
  46. package/dist/pointer/pointerAction.js +52 -0
  47. package/dist/pointer/pointerMove.d.ts +5 -0
  48. package/dist/pointer/pointerMove.js +136 -0
  49. package/dist/pointer/pointerPress.d.ts +7 -0
  50. package/dist/pointer/pointerPress.js +330 -0
  51. package/dist/pointer/resolveSelectionTarget.d.ts +5 -0
  52. package/dist/pointer/resolveSelectionTarget.js +75 -0
  53. package/dist/pointer/types.d.ts +72 -0
  54. package/dist/pointer/types.js +1 -0
  55. package/dist/selectOptions.d.ts +4 -0
  56. package/dist/{select-options.js → selectOptions.js} +35 -38
  57. package/dist/setup.d.ts +92 -0
  58. package/dist/setup.js +203 -0
  59. package/dist/tab.d.ts +3 -4
  60. package/dist/tab.js +3 -136
  61. package/dist/type.d.ts +14 -0
  62. package/dist/type.js +74 -0
  63. package/dist/upload.d.ts +4 -4
  64. package/dist/upload.js +3 -9
  65. package/dist/utils/edit/calculateNewValue.d.ts +12 -5
  66. package/dist/utils/edit/calculateNewValue.js +19 -21
  67. package/dist/utils/edit/editInputElement.d.ts +16 -0
  68. package/dist/utils/edit/editInputElement.js +53 -0
  69. package/dist/utils/edit/getValue.d.ts +1 -1
  70. package/dist/utils/edit/getValue.js +3 -1
  71. package/dist/utils/edit/isContentEditable.d.ts +4 -0
  72. package/dist/utils/edit/isContentEditable.js +14 -0
  73. package/dist/utils/edit/isEditable.d.ts +1 -0
  74. package/dist/utils/edit/prepareInput.d.ts +7 -0
  75. package/dist/utils/edit/prepareInput.js +77 -0
  76. package/dist/{blur.d.ts → utils/focus/blur.d.ts} +0 -0
  77. package/dist/utils/focus/blur.js +19 -0
  78. package/dist/{focus.d.ts → utils/focus/focus.d.ts} +0 -0
  79. package/dist/utils/focus/focus.js +22 -0
  80. package/dist/utils/focus/getTabDestination.d.ts +1 -0
  81. package/dist/utils/focus/getTabDestination.js +71 -0
  82. package/dist/utils/focus/isFocusable.js +1 -3
  83. package/dist/utils/focus/selectAll.d.ts +5 -0
  84. package/dist/utils/focus/selectAll.js +51 -0
  85. package/dist/utils/focus/selection.d.ts +42 -0
  86. package/dist/utils/focus/selection.js +190 -0
  87. package/dist/utils/index.d.ts +12 -6
  88. package/dist/utils/index.js +132 -54
  89. package/dist/utils/keyDef/readNextDescriptor.d.ts +19 -0
  90. package/dist/utils/keyDef/readNextDescriptor.js +102 -0
  91. package/dist/utils/misc/findClosest.d.ts +1 -0
  92. package/dist/utils/misc/findClosest.js +20 -0
  93. package/dist/utils/misc/isDescendantOrSelf.d.ts +1 -0
  94. package/dist/utils/misc/isDescendantOrSelf.js +20 -0
  95. package/dist/utils/pointer/firePointerEvents.d.ts +25 -0
  96. package/dist/utils/pointer/firePointerEvents.js +112 -0
  97. package/dist/utils/pointer/mouseButtons.d.ts +12 -0
  98. package/dist/utils/pointer/mouseButtons.js +45 -0
  99. package/package.json +4 -3
  100. package/dist/blur.js +0 -15
  101. package/dist/dom-helpers.d.ts +0 -3
  102. package/dist/focus.js +0 -15
  103. package/dist/keyboard/shared/carryValue.d.ts +0 -2
  104. package/dist/keyboard/shared/carryValue.js +0 -13
  105. package/dist/keyboard/shared/fireInputEvent.d.ts +0 -17
  106. package/dist/keyboard/shared/fireInputEvent.js +0 -110
  107. package/dist/keyboard/specialCharMap.d.ts +0 -22
  108. package/dist/keyboard/specialCharMap.js +0 -30
  109. package/dist/select-options.d.ts +0 -4
  110. package/dist/type/index.d.ts +0 -7
  111. package/dist/type/index.js +0 -33
  112. package/dist/type/typeImplementation.d.ts +0 -10
  113. package/dist/type/typeImplementation.js +0 -64
  114. package/dist/utils/click/getMouseEventOptions.d.ts +0 -31
  115. package/dist/utils/click/getMouseEventOptions.js +0 -61
  116. package/dist/utils/edit/cursorPosition.d.ts +0 -2
  117. package/dist/utils/edit/cursorPosition.js +0 -35
  118. package/dist/utils/edit/hasUnreliableEmptyValue.d.ts +0 -10
  119. package/dist/utils/edit/hasUnreliableEmptyValue.js +0 -21
  120. package/dist/utils/edit/selectionRange.d.ts +0 -16
  121. package/dist/utils/edit/selectionRange.js +0 -104
  122. package/dist/utils/misc/isDocument.d.ts +0 -1
  123. package/dist/utils/misc/isDocument.js +0 -10
  124. package/dist/utils/misc/isLabelWithInternallyDisabledControl.d.ts +0 -1
  125. package/dist/utils/misc/isLabelWithInternallyDisabledControl.js +0 -22
package/README.md CHANGED
@@ -257,6 +257,9 @@ Thanks goes to these people ([emoji key][emojis]):
257
257
  <td align="center"><a href="https://johannesfischer.github.io/"><img src="https://avatars.githubusercontent.com/u/28100?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Johannes Fischer</b></sub></a><br /><a href="https://github.com/testing-library/user-event/commits?author=JohannesFischer" title="Code">💻</a></td>
258
258
  <td align="center"><a href="https://github.com/777PolarFox777"><img src="https://avatars.githubusercontent.com/u/19393384?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Andrew D.</b></sub></a><br /><a href="https://github.com/testing-library/user-event/commits?author=777PolarFox777" title="Code">💻</a></td>
259
259
  </tr>
260
+ <tr>
261
+ <td align="center"><a href="https://github.com/patricklizon"><img src="https://avatars.githubusercontent.com/u/12571855?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Patrick Lizoń</b></sub></a><br /><a href="https://github.com/testing-library/user-event/commits?author=patricklizon" title="Code">💻</a></td>
262
+ </tr>
260
263
  </table>
261
264
 
262
265
  <!-- markdownlint-restore -->
package/dist/clear.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- declare function clear(element: Element): void;
2
- export { clear };
1
+ import type { UserEvent } from './setup';
2
+ export declare function clear(this: UserEvent, element: Element): void;
package/dist/clear.js CHANGED
@@ -5,44 +5,29 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.clear = clear;
7
7
 
8
- var _utils = require("./utils");
8
+ var _document = require("./document");
9
9
 
10
- var _type = require("./type");
10
+ var _utils = require("./utils");
11
11
 
12
12
  function clear(element) {
13
- var _element$selectionSta, _element$selectionEnd;
13
+ var _prepareInput;
14
14
 
15
- if (!(0, _utils.isElementType)(element, ['input', 'textarea'])) {
16
- // TODO: support contenteditable
17
- throw new Error('clear currently only supports input and textarea elements.');
15
+ if (!(0, _utils.isEditable)(element) || (0, _utils.isDisabled)(element)) {
16
+ throw new Error('clear()` is only supported on editable elements.');
18
17
  }
19
18
 
20
- if ((0, _utils.isDisabled)(element)) {
21
- return;
22
- } // TODO: track the selection range ourselves so we don't have to do this input "type" trickery
23
- // just like cypress does: https://github.com/cypress-io/cypress/blob/8d7f1a0bedc3c45a2ebf1ff50324b34129fdc683/packages/driver/src/dom/selection.ts#L16-L37
19
+ (0, _document.prepareDocument)(element.ownerDocument);
20
+ (0, _utils.focus)(element);
24
21
 
22
+ if (element.ownerDocument.activeElement !== element) {
23
+ throw new Error('The element to be cleared could not be focused.');
24
+ }
25
25
 
26
- const elementType = element.type;
26
+ (0, _utils.selectAll)(element);
27
27
 
28
- if (elementType !== 'textarea') {
29
- // setSelectionRange is not supported on certain types of inputs, e.g. "number" or "email"
30
- ;
31
- element.type = 'text';
28
+ if (!(0, _utils.isAllSelected)(element)) {
29
+ throw new Error('The element content to be cleared could not be selected.');
32
30
  }
33
31
 
34
- (0, _type.type)(element, '{selectall}{del}', {
35
- delay: 0,
36
- initialSelectionStart: (_element$selectionSta = element.selectionStart) != null ? _element$selectionSta :
37
- /* istanbul ignore next */
38
- undefined,
39
- initialSelectionEnd: (_element$selectionEnd = element.selectionEnd) != null ? _element$selectionEnd :
40
- /* istanbul ignore next */
41
- undefined
42
- });
43
-
44
- if (elementType !== 'textarea') {
45
- ;
46
- element.type = elementType;
47
- }
32
+ (_prepareInput = (0, _utils.prepareInput)('', element, 'deleteContentBackward')) == null ? void 0 : _prepareInput.commit();
48
33
  }
package/dist/click.d.ts CHANGED
@@ -1,8 +1,9 @@
1
1
  import { PointerOptions } from './utils';
2
+ import type { UserEvent } from './setup';
2
3
  export declare interface clickOptions {
3
4
  skipHover?: boolean;
4
5
  clickCount?: number;
5
6
  }
6
- declare function click(element: Element, init?: MouseEventInit, { skipHover, clickCount, skipPointerEventsCheck, }?: clickOptions & PointerOptions): void;
7
- declare function dblClick(element: Element, init?: MouseEventInit, { skipPointerEventsCheck }?: clickOptions & PointerOptions): void;
8
- export { click, dblClick };
7
+ export declare function click(this: UserEvent, element: Element, { skipHover, skipPointerEventsCheck, }?: clickOptions & PointerOptions): void;
8
+ export declare function dblClick(this: UserEvent, element: Element, { skipPointerEventsCheck }?: clickOptions & PointerOptions): void;
9
+ export declare function tripleClick(this: UserEvent, element: Element, { skipPointerEventsCheck }?: clickOptions & PointerOptions): void;
package/dist/click.js CHANGED
@@ -5,172 +5,57 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.click = click;
7
7
  exports.dblClick = dblClick;
8
-
9
- var _dom = require("@testing-library/dom");
8
+ exports.tripleClick = tripleClick;
10
9
 
11
10
  var _utils = require("./utils");
12
11
 
13
- var _hover = require("./hover");
14
-
15
- var _blur = require("./blur");
16
-
17
- var _focus = require("./focus");
18
-
19
- function getPreviouslyFocusedElement(element) {
20
- const focusedElement = element.ownerDocument.activeElement;
21
- const wasAnotherElementFocused = focusedElement && focusedElement !== element.ownerDocument.body && focusedElement !== element;
22
- return wasAnotherElementFocused ? focusedElement : null;
23
- }
24
-
25
- function clickLabel(label, init, {
26
- clickCount
27
- }) {
28
- if ((0, _utils.isLabelWithInternallyDisabledControl)(label)) return;
29
-
30
- _dom.fireEvent.pointerDown(label, init);
31
-
32
- _dom.fireEvent.mouseDown(label, (0, _utils.getMouseEventOptions)('mousedown', init, clickCount));
33
-
34
- _dom.fireEvent.pointerUp(label, init);
35
-
36
- _dom.fireEvent.mouseUp(label, (0, _utils.getMouseEventOptions)('mouseup', init, clickCount));
37
-
38
- fireClick(label, (0, _utils.getMouseEventOptions)('click', init, clickCount)); // clicking the label will trigger a click of the label.control
39
- // however, it will not focus the label.control so we have to do it
40
- // ourselves.
41
-
42
- if (label.control) (0, _focus.focus)(label.control);
43
- }
44
-
45
- function clickBooleanElement(element, init, {
46
- clickCount
47
- }) {
48
- _dom.fireEvent.pointerDown(element, init);
49
-
50
- if (!element.disabled) {
51
- _dom.fireEvent.mouseDown(element, (0, _utils.getMouseEventOptions)('mousedown', init, clickCount));
52
- }
53
-
54
- (0, _focus.focus)(element);
55
-
56
- _dom.fireEvent.pointerUp(element, init);
57
-
58
- if (!element.disabled) {
59
- _dom.fireEvent.mouseUp(element, (0, _utils.getMouseEventOptions)('mouseup', init, clickCount));
60
-
61
- fireClick(element, (0, _utils.getMouseEventOptions)('click', init, clickCount));
62
- }
63
- }
64
-
65
- function clickElement(element, init, {
66
- clickCount
67
- }) {
68
- const previousElement = getPreviouslyFocusedElement(element);
69
-
70
- _dom.fireEvent.pointerDown(element, init);
71
-
72
- if (!(0, _utils.isDisabled)(element)) {
73
- const continueDefaultHandling = _dom.fireEvent.mouseDown(element, (0, _utils.getMouseEventOptions)('mousedown', init, clickCount));
74
-
75
- if (continueDefaultHandling) {
76
- const closestFocusable = findClosest(element, _utils.isFocusable);
77
-
78
- if (previousElement && !closestFocusable) {
79
- (0, _blur.blur)(previousElement);
80
- } else if (closestFocusable) {
81
- (0, _focus.focus)(closestFocusable);
82
- }
83
- }
84
- }
85
-
86
- _dom.fireEvent.pointerUp(element, init);
87
-
88
- if (!(0, _utils.isDisabled)(element)) {
89
- _dom.fireEvent.mouseUp(element, (0, _utils.getMouseEventOptions)('mouseup', init, clickCount));
90
-
91
- fireClick(element, (0, _utils.getMouseEventOptions)('click', init, clickCount));
92
- const parentLabel = element.closest('label');
93
- if (parentLabel != null && parentLabel.control) (0, _focus.focus)(parentLabel.control);
94
- }
95
- }
96
-
97
- function findClosest(element, callback) {
98
- let el = element;
99
-
100
- do {
101
- if (callback(el)) {
102
- return el;
103
- }
104
-
105
- el = el.parentElement;
106
- } while (el && el !== element.ownerDocument.body);
107
-
108
- return undefined;
109
- }
110
-
111
- function click(element, init, {
12
+ function click(element, {
112
13
  skipHover = false,
113
- clickCount = 0,
114
14
  skipPointerEventsCheck = false
115
15
  } = {}) {
116
16
  if (!skipPointerEventsCheck && !(0, _utils.hasPointerEvents)(element)) {
117
17
  throw new Error('unable to click element as it has or inherits pointer-events set to "none".');
118
- } // We just checked for `pointerEvents`. We can always skip this one in `hover`.
18
+ } // istanbul ignore else
119
19
 
120
20
 
121
- if (!skipHover) (0, _hover.hover)(element, init, {
122
- skipPointerEventsCheck: true
123
- });
124
-
125
- if ((0, _utils.isElementType)(element, 'label')) {
126
- clickLabel(element, init, {
127
- clickCount
128
- });
129
- } else if ((0, _utils.isElementType)(element, 'input')) {
130
- if (element.type === 'checkbox' || element.type === 'radio') {
131
- clickBooleanElement(element, init, {
132
- clickCount
133
- });
134
- } else {
135
- clickElement(element, init, {
136
- clickCount
137
- });
138
- }
139
- } else {
140
- clickElement(element, init, {
141
- clickCount
21
+ if (!skipHover) // We just checked for `pointerEvents`. We can always skip this one in `hover`.
22
+ this.hover(element, {
23
+ skipPointerEventsCheck: true
142
24
  });
143
- }
25
+ this.pointer({
26
+ keys: '[MouseLeft]',
27
+ target: element
28
+ });
144
29
  }
145
30
 
146
- function fireClick(element, mouseEventOptions) {
147
- if (mouseEventOptions.button === 2) {
148
- _dom.fireEvent.contextMenu(element, mouseEventOptions);
149
- } else {
150
- _dom.fireEvent.click(element, mouseEventOptions);
31
+ function dblClick(element, {
32
+ skipPointerEventsCheck = false
33
+ } = {}) {
34
+ if (!skipPointerEventsCheck && !(0, _utils.hasPointerEvents)(element)) {
35
+ throw new Error('unable to double-click element as it has or inherits pointer-events set to "none".');
151
36
  }
37
+
38
+ this.hover(element, {
39
+ skipPointerEventsCheck: true
40
+ });
41
+ this.pointer({
42
+ keys: '[MouseLeft][MouseLeft]',
43
+ target: element
44
+ });
152
45
  }
153
46
 
154
- function dblClick(element, init, {
47
+ function tripleClick(element, {
155
48
  skipPointerEventsCheck = false
156
49
  } = {}) {
157
50
  if (!skipPointerEventsCheck && !(0, _utils.hasPointerEvents)(element)) {
158
- throw new Error('unable to double-click element as it has or inherits pointer-events set to "none".');
51
+ throw new Error('unable to triple-click element as it has or inherits pointer-events set to "none".');
159
52
  }
160
53
 
161
- (0, _hover.hover)(element, init, {
162
- skipPointerEventsCheck
163
- });
164
- click(element, init, {
165
- skipHover: true,
166
- clickCount: 0,
167
- skipPointerEventsCheck
54
+ this.hover(element, {
55
+ skipPointerEventsCheck: true
168
56
  });
169
- click(element, init, {
170
- skipHover: true,
171
- clickCount: 1,
172
- skipPointerEventsCheck
57
+ this.pointer({
58
+ keys: '[MouseLeft][MouseLeft][MouseLeft]',
59
+ target: element
173
60
  });
174
-
175
- _dom.fireEvent.dblClick(element, (0, _utils.getMouseEventOptions)('dblclick', init, 2));
176
61
  }
@@ -0,0 +1,10 @@
1
+ declare const isPrepared: unique symbol;
2
+ declare global {
3
+ interface Node {
4
+ [isPrepared]?: typeof isPrepared;
5
+ }
6
+ }
7
+ export declare function prepareDocument(document: Document): void;
8
+ export { getUIValue, setUIValue, startTrackValue, endTrackValue } from './value';
9
+ export { getUISelection, setUISelection } from './selection';
10
+ export type { UISelectionRange } from './selection';
@@ -0,0 +1,100 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ Object.defineProperty(exports, "endTrackValue", {
7
+ enumerable: true,
8
+ get: function () {
9
+ return _value.endTrackValue;
10
+ }
11
+ });
12
+ Object.defineProperty(exports, "getUISelection", {
13
+ enumerable: true,
14
+ get: function () {
15
+ return _selection.getUISelection;
16
+ }
17
+ });
18
+ Object.defineProperty(exports, "getUIValue", {
19
+ enumerable: true,
20
+ get: function () {
21
+ return _value.getUIValue;
22
+ }
23
+ });
24
+ exports.prepareDocument = prepareDocument;
25
+ Object.defineProperty(exports, "setUISelection", {
26
+ enumerable: true,
27
+ get: function () {
28
+ return _selection.setUISelection;
29
+ }
30
+ });
31
+ Object.defineProperty(exports, "setUIValue", {
32
+ enumerable: true,
33
+ get: function () {
34
+ return _value.setUIValue;
35
+ }
36
+ });
37
+ Object.defineProperty(exports, "startTrackValue", {
38
+ enumerable: true,
39
+ get: function () {
40
+ return _value.startTrackValue;
41
+ }
42
+ });
43
+
44
+ var _dom = require("@testing-library/dom");
45
+
46
+ var _selection = require("./selection");
47
+
48
+ var _value = require("./value");
49
+
50
+ const isPrepared = Symbol('Node prepared with document state workarounds');
51
+
52
+ function prepareDocument(document) {
53
+ if (document[isPrepared]) {
54
+ return;
55
+ }
56
+
57
+ document.addEventListener('focus', e => {
58
+ const el = e.target;
59
+ prepareElement(el);
60
+ }, {
61
+ capture: true,
62
+ passive: true
63
+ }); // Our test environment defaults to `document.body` as `activeElement`.
64
+ // In other environments this might be `null` when preparing.
65
+ // istanbul ignore else
66
+
67
+ if (document.activeElement) {
68
+ prepareElement(document.activeElement);
69
+ }
70
+
71
+ document.addEventListener('blur', e => {
72
+ const el = e.target;
73
+ const initialValue = (0, _value.getInitialValue)(el);
74
+
75
+ if (typeof initialValue === 'string' && el.value !== initialValue) {
76
+ _dom.fireEvent.change(el);
77
+ }
78
+ }, {
79
+ capture: true,
80
+ passive: true
81
+ });
82
+ document[isPrepared] = isPrepared;
83
+ }
84
+
85
+ function prepareElement(el) {
86
+ if ('value' in el) {
87
+ (0, _value.setInitialValue)(el);
88
+ }
89
+
90
+ if (el[isPrepared]) {
91
+ return;
92
+ }
93
+
94
+ if ('value' in el) {
95
+ (0, _value.prepareValueInterceptor)(el);
96
+ (0, _selection.prepareSelectionInterceptor)(el);
97
+ }
98
+
99
+ el[isPrepared] = isPrepared;
100
+ }
@@ -0,0 +1,15 @@
1
+ declare type anyFunc = (...a: any[]) => any;
2
+ declare type Params<Prop> = Prop extends anyFunc ? Parameters<Prop> : [Prop];
3
+ declare type ImplReturn<Prop> = Prop extends anyFunc ? Parameters<Prop> : Prop;
4
+ export declare function prepareInterceptor<ElementType extends Node, PropName extends keyof ElementType>(element: ElementType, propName: PropName, interceptorImpl: (this: ElementType, ...args: Params<ElementType[PropName]>) => {
5
+ /**
6
+ * React tracks the changes on element properties.
7
+ * This workaround tries to alter the DOM element without React noticing,
8
+ * so that it later picks up the change.
9
+ *
10
+ * @see https://github.com/facebook/react/blob/148f8e497c7d37a3c7ab99f01dec2692427272b1/packages/react-dom/src/client/inputValueTracking.js#L51-L104
11
+ */
12
+ applyNative?: boolean;
13
+ realArgs?: ImplReturn<ElementType[PropName]>;
14
+ }): void;
15
+ export {};
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.prepareInterceptor = prepareInterceptor;
7
+ const Interceptor = Symbol('Interceptor for programmatical calls');
8
+
9
+ function prepareInterceptor(element, propName, interceptorImpl) {
10
+ const prototypeDescriptor = Object.getOwnPropertyDescriptor(element.constructor.prototype, propName);
11
+ const objectDescriptor = Object.getOwnPropertyDescriptor(element, propName);
12
+ const target = prototypeDescriptor != null && prototypeDescriptor.set ? 'set' : 'value';
13
+
14
+ if (typeof (prototypeDescriptor == null ? void 0 : prototypeDescriptor[target]) !== 'function' || prototypeDescriptor[target][Interceptor]) {
15
+ return;
16
+ }
17
+
18
+ function intercept(...args) {
19
+ const {
20
+ applyNative = true,
21
+ realArgs
22
+ } = interceptorImpl.call(this, ...args);
23
+ const realFunc = (!applyNative && objectDescriptor || prototypeDescriptor)[target];
24
+
25
+ if (target === 'set') {
26
+ realFunc.call(this, realArgs);
27
+ } else {
28
+ realFunc.call(this, ...realArgs);
29
+ }
30
+ }
31
+
32
+ ;
33
+ intercept[Interceptor] = Interceptor;
34
+ Object.defineProperty(element, propName, { ...(objectDescriptor != null ? objectDescriptor : prototypeDescriptor),
35
+ [target]: intercept
36
+ });
37
+ }
@@ -0,0 +1,27 @@
1
+ declare const UISelection: unique symbol;
2
+ export interface UISelectionRange {
3
+ startOffset: number;
4
+ endOffset: number;
5
+ }
6
+ export interface UISelection {
7
+ anchorOffset: number;
8
+ focusOffset: number;
9
+ }
10
+ declare global {
11
+ interface Element {
12
+ [UISelection]?: UISelection;
13
+ }
14
+ }
15
+ export declare function prepareSelectionInterceptor(element: HTMLInputElement | HTMLTextAreaElement): void;
16
+ export declare function setUISelection(element: HTMLInputElement | HTMLTextAreaElement, { focusOffset: focusOffsetParam, anchorOffset: anchorOffsetParam, }: {
17
+ anchorOffset?: number;
18
+ focusOffset: number;
19
+ }, mode?: 'replace' | 'modify'): void;
20
+ export declare function getUISelection(element: HTMLInputElement | HTMLTextAreaElement): {
21
+ startOffset: number;
22
+ endOffset: number;
23
+ anchorOffset: number;
24
+ focusOffset: number;
25
+ };
26
+ export declare function clearUISelection(element: HTMLInputElement | HTMLTextAreaElement): void;
27
+ export {};
@@ -0,0 +1,90 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.clearUISelection = clearUISelection;
7
+ exports.getUISelection = getUISelection;
8
+ exports.prepareSelectionInterceptor = prepareSelectionInterceptor;
9
+ exports.setUISelection = setUISelection;
10
+
11
+ var _ = require(".");
12
+
13
+ var _interceptor = require("./interceptor");
14
+
15
+ const UISelection = Symbol('Displayed selection in UI');
16
+
17
+ function prepareSelectionInterceptor(element) {
18
+ (0, _interceptor.prepareInterceptor)(element, 'setSelectionRange', function interceptorImpl(start, end, direction = 'none') {
19
+ const isUI = start && typeof start === 'object' && start[UISelection];
20
+
21
+ if (!isUI) {
22
+ this[UISelection] = undefined;
23
+ }
24
+
25
+ return {
26
+ realArgs: [Number(start), end, direction]
27
+ };
28
+ });
29
+ (0, _interceptor.prepareInterceptor)(element, 'selectionStart', function interceptorImpl(v) {
30
+ this[UISelection] = undefined;
31
+ return {
32
+ realArgs: v
33
+ };
34
+ });
35
+ (0, _interceptor.prepareInterceptor)(element, 'selectionEnd', function interceptorImpl(v) {
36
+ this[UISelection] = undefined;
37
+ return {
38
+ realArgs: v
39
+ };
40
+ });
41
+ }
42
+
43
+ function setUISelection(element, {
44
+ focusOffset: focusOffsetParam,
45
+ anchorOffset: anchorOffsetParam = focusOffsetParam
46
+ }, mode = 'replace') {
47
+ const valueLength = (0, _.getUIValue)(element).length;
48
+
49
+ const sanitizeOffset = o => Math.max(0, Math.min(valueLength, o));
50
+
51
+ const anchorOffset = mode === 'replace' || element[UISelection] === undefined ? sanitizeOffset(anchorOffsetParam) : element[UISelection].anchorOffset;
52
+ const focusOffset = sanitizeOffset(focusOffsetParam);
53
+ const startOffset = Math.min(anchorOffset, focusOffset);
54
+ const endOffset = Math.max(anchorOffset, focusOffset);
55
+ element[UISelection] = {
56
+ anchorOffset,
57
+ focusOffset
58
+ };
59
+
60
+ if (element.selectionStart === startOffset && element.selectionEnd === endOffset) {
61
+ return;
62
+ } // eslint-disable-next-line no-new-wrappers
63
+
64
+
65
+ const startObj = new Number(startOffset);
66
+ startObj[UISelection] = UISelection;
67
+
68
+ try {
69
+ element.setSelectionRange(startObj, endOffset);
70
+ } catch {// DOMException for invalid state is expected when calling this
71
+ // on an element without support for setSelectionRange
72
+ }
73
+ }
74
+
75
+ function getUISelection(element) {
76
+ var _element$UISelection, _element$selectionSta, _element$selectionEnd;
77
+
78
+ const sel = (_element$UISelection = element[UISelection]) != null ? _element$UISelection : {
79
+ anchorOffset: (_element$selectionSta = element.selectionStart) != null ? _element$selectionSta : 0,
80
+ focusOffset: (_element$selectionEnd = element.selectionEnd) != null ? _element$selectionEnd : 0
81
+ };
82
+ return { ...sel,
83
+ startOffset: Math.min(sel.anchorOffset, sel.focusOffset),
84
+ endOffset: Math.max(sel.anchorOffset, sel.focusOffset)
85
+ };
86
+ }
87
+
88
+ function clearUISelection(element) {
89
+ element[UISelection] = undefined;
90
+ }
@@ -0,0 +1,18 @@
1
+ declare const UIValue: unique symbol;
2
+ declare const InitialValue: unique symbol;
3
+ declare const TrackChanges: unique symbol;
4
+ declare global {
5
+ interface Element {
6
+ [UIValue]?: string;
7
+ [InitialValue]?: string;
8
+ [TrackChanges]?: string[];
9
+ }
10
+ }
11
+ export declare function prepareValueInterceptor(element: HTMLInputElement): void;
12
+ export declare function setUIValue(element: HTMLInputElement | HTMLTextAreaElement, value: string): void;
13
+ export declare function getUIValue(element: HTMLInputElement | HTMLTextAreaElement): string;
14
+ export declare function setInitialValue(element: HTMLInputElement | HTMLTextAreaElement): void;
15
+ export declare function getInitialValue(element: HTMLInputElement | HTMLTextAreaElement): string | undefined;
16
+ export declare function startTrackValue(element: HTMLInputElement | HTMLTextAreaElement): void;
17
+ export declare function endTrackValue(element: HTMLInputElement | HTMLTextAreaElement): string[] | undefined;
18
+ export {};
@@ -0,0 +1,77 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.endTrackValue = endTrackValue;
7
+ exports.getInitialValue = getInitialValue;
8
+ exports.getUIValue = getUIValue;
9
+ exports.prepareValueInterceptor = prepareValueInterceptor;
10
+ exports.setInitialValue = setInitialValue;
11
+ exports.setUIValue = setUIValue;
12
+ exports.startTrackValue = startTrackValue;
13
+
14
+ var _interceptor = require("./interceptor");
15
+
16
+ var _selection = require("./selection");
17
+
18
+ const UIValue = Symbol('Displayed value in UI');
19
+ const InitialValue = Symbol('Initial value to compare on blur');
20
+ const TrackChanges = Symbol('Track programmatic changes for React workaround');
21
+
22
+ function valueInterceptor(v) {
23
+ const isUI = typeof v === 'object' && v[UIValue];
24
+ this[UIValue] = isUI ? String(v) : undefined;
25
+
26
+ if (!isUI) {
27
+ trackValue(this, String(v));
28
+ this[InitialValue] = String(v); // Programmatically setting the value property
29
+ // moves the cursor to the end of the input.
30
+
31
+ (0, _selection.clearUISelection)(this);
32
+ }
33
+
34
+ return {
35
+ applyNative: !!isUI,
36
+ realArgs: String(v)
37
+ };
38
+ }
39
+
40
+ function prepareValueInterceptor(element) {
41
+ (0, _interceptor.prepareInterceptor)(element, 'value', valueInterceptor);
42
+ }
43
+
44
+ function setUIValue(element, value) {
45
+ element.value = {
46
+ [UIValue]: UIValue,
47
+ toString: () => value
48
+ };
49
+ }
50
+
51
+ function getUIValue(element) {
52
+ return element[UIValue] === undefined ? element.value : String(element[UIValue]);
53
+ }
54
+
55
+ function setInitialValue(element) {
56
+ element[InitialValue] = element.value;
57
+ }
58
+
59
+ function getInitialValue(element) {
60
+ return element[InitialValue];
61
+ }
62
+
63
+ function startTrackValue(element) {
64
+ element[TrackChanges] = [];
65
+ }
66
+
67
+ function trackValue(element, v) {
68
+ var _element$TrackChanges;
69
+
70
+ (_element$TrackChanges = element[TrackChanges]) == null ? void 0 : _element$TrackChanges.push(v);
71
+ }
72
+
73
+ function endTrackValue(element) {
74
+ const tracked = element[TrackChanges];
75
+ element[TrackChanges] = undefined;
76
+ return tracked;
77
+ }