@testing-library/user-event 14.3.0 → 14.4.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 (237) hide show
  1. package/dist/cjs/clipboard/copy.js +40 -0
  2. package/dist/cjs/clipboard/cut.js +40 -0
  3. package/dist/cjs/clipboard/index.js +13 -0
  4. package/dist/cjs/clipboard/paste.js +41 -0
  5. package/dist/cjs/convenience/click.js +40 -0
  6. package/dist/cjs/convenience/hover.js +37 -0
  7. package/dist/cjs/convenience/index.js +16 -0
  8. package/dist/cjs/convenience/tab.js +9 -0
  9. package/dist/cjs/document/index.js +74 -0
  10. package/dist/cjs/document/interceptor.js +30 -0
  11. package/dist/cjs/document/selection.js +107 -0
  12. package/dist/cjs/document/setRangeText.js +21 -0
  13. package/dist/cjs/document/value.js +125 -0
  14. package/dist/cjs/event/behavior/click.js +42 -0
  15. package/dist/cjs/event/behavior/cut.js +26 -0
  16. package/dist/cjs/event/behavior/index.js +15 -0
  17. package/dist/cjs/event/behavior/keydown.js +134 -0
  18. package/dist/cjs/event/behavior/keypress.js +58 -0
  19. package/dist/cjs/event/behavior/keyup.js +24 -0
  20. package/dist/cjs/event/behavior/paste.js +29 -0
  21. package/dist/cjs/event/behavior/registry.js +7 -0
  22. package/dist/cjs/event/createEvent.js +193 -0
  23. package/dist/cjs/event/dispatchEvent.js +37 -0
  24. package/dist/cjs/event/dom-events.d.js +2 -0
  25. package/dist/cjs/event/eventMap.js +64 -0
  26. package/dist/cjs/event/index.js +24 -0
  27. package/dist/cjs/event/types.js +2 -0
  28. package/dist/cjs/event/wrapEvent.js +11 -0
  29. package/dist/cjs/index.js +14 -0
  30. package/dist/cjs/keyboard/index.js +59 -0
  31. package/dist/cjs/keyboard/keyMap.js +158 -0
  32. package/dist/cjs/keyboard/parseKeyDef.js +58 -0
  33. package/dist/cjs/options.js +42 -0
  34. package/dist/cjs/package.json +3 -0
  35. package/dist/cjs/pointer/index.js +88 -0
  36. package/dist/cjs/pointer/keyMap.js +35 -0
  37. package/dist/cjs/pointer/parseKeyDef.js +39 -0
  38. package/dist/cjs/setup/api.js +35 -0
  39. package/dist/cjs/setup/config.js +7 -0
  40. package/dist/cjs/setup/directApi.js +77 -0
  41. package/dist/cjs/setup/index.js +15 -0
  42. package/dist/cjs/setup/setup.js +97 -0
  43. package/dist/cjs/setup/wrapAsync.js +13 -0
  44. package/dist/cjs/system/index.js +31 -0
  45. package/dist/cjs/system/keyboard.js +135 -0
  46. package/dist/cjs/system/pointer/buttons.js +71 -0
  47. package/dist/cjs/system/pointer/device.js +21 -0
  48. package/dist/cjs/system/pointer/index.js +146 -0
  49. package/dist/cjs/system/pointer/mouse.js +186 -0
  50. package/dist/cjs/system/pointer/pointer.js +114 -0
  51. package/dist/cjs/utility/clear.js +42 -0
  52. package/dist/cjs/utility/index.js +16 -0
  53. package/dist/cjs/utility/selectOptions.js +125 -0
  54. package/dist/cjs/utility/type.js +41 -0
  55. package/dist/cjs/utility/upload.js +72 -0
  56. package/dist/cjs/utils/click/isClickableInput.js +22 -0
  57. package/dist/cjs/utils/dataTransfer/Blob.js +18 -0
  58. package/dist/cjs/utils/dataTransfer/Clipboard.js +168 -0
  59. package/dist/cjs/utils/dataTransfer/DataTransfer.js +124 -0
  60. package/dist/cjs/utils/dataTransfer/FileList.js +26 -0
  61. package/dist/cjs/utils/edit/buildTimeValue.js +36 -0
  62. package/dist/cjs/utils/edit/getValue.js +35 -0
  63. package/dist/cjs/utils/edit/input.js +162 -0
  64. package/dist/cjs/utils/edit/isContentEditable.js +20 -0
  65. package/dist/cjs/utils/edit/isEditable.js +35 -0
  66. package/dist/cjs/utils/edit/isValidDateOrTimeValue.js +11 -0
  67. package/dist/cjs/utils/edit/maxLength.js +38 -0
  68. package/dist/cjs/utils/edit/setFiles.js +59 -0
  69. package/dist/cjs/utils/edit/walkRadio.js +28 -0
  70. package/dist/cjs/utils/focus/blur.js +16 -0
  71. package/dist/cjs/utils/focus/copySelection.js +46 -0
  72. package/dist/cjs/utils/focus/cursor.js +143 -0
  73. package/dist/cjs/utils/focus/focus.js +26 -0
  74. package/dist/cjs/utils/focus/getActiveElement.js +24 -0
  75. package/dist/cjs/utils/focus/getTabDestination.js +82 -0
  76. package/dist/cjs/utils/focus/isFocusable.js +11 -0
  77. package/dist/cjs/utils/focus/resolveCaretPosition.js +78 -0
  78. package/dist/cjs/utils/focus/selectAll.js +54 -0
  79. package/dist/cjs/utils/focus/selection.js +287 -0
  80. package/dist/cjs/utils/focus/selector.js +16 -0
  81. package/dist/cjs/utils/index.js +108 -0
  82. package/dist/cjs/utils/keyDef/readNextDescriptor.js +92 -0
  83. package/dist/cjs/utils/misc/cloneEvent.js +9 -0
  84. package/dist/cjs/utils/misc/dom-helpers.d.js +2 -0
  85. package/dist/cjs/utils/misc/eventWrapper.js +15 -0
  86. package/dist/cjs/utils/misc/findClosest.js +16 -0
  87. package/dist/cjs/utils/misc/getDocumentFromNode.js +12 -0
  88. package/dist/cjs/utils/misc/getTreeDiff.js +27 -0
  89. package/dist/cjs/utils/misc/getWindow.js +11 -0
  90. package/dist/cjs/utils/misc/isDescendantOrSelf.js +16 -0
  91. package/dist/cjs/utils/misc/isDisabled.js +34 -0
  92. package/dist/cjs/utils/misc/isElementType.js +22 -0
  93. package/dist/cjs/utils/misc/isVisible.js +21 -0
  94. package/dist/cjs/utils/misc/level.js +20 -0
  95. package/dist/cjs/utils/misc/wait.js +16 -0
  96. package/dist/cjs/utils/pointer/cssPointerEvents.js +111 -0
  97. package/dist/esm/clipboard/copy.js +36 -0
  98. package/dist/esm/clipboard/cut.js +36 -0
  99. package/dist/esm/clipboard/index.js +3 -0
  100. package/dist/esm/clipboard/paste.js +37 -0
  101. package/dist/esm/convenience/click.js +34 -0
  102. package/dist/esm/convenience/hover.js +32 -0
  103. package/dist/esm/convenience/index.js +3 -0
  104. package/dist/esm/convenience/tab.js +5 -0
  105. package/dist/esm/document/index.js +66 -0
  106. package/dist/esm/document/interceptor.js +26 -0
  107. package/dist/esm/document/selection.js +99 -0
  108. package/dist/esm/document/setRangeText.js +17 -0
  109. package/dist/esm/document/value.js +115 -0
  110. package/dist/esm/event/behavior/click.js +40 -0
  111. package/dist/esm/event/behavior/cut.js +24 -0
  112. package/dist/esm/event/behavior/index.js +7 -0
  113. package/dist/esm/event/behavior/keydown.js +132 -0
  114. package/dist/esm/event/behavior/keypress.js +56 -0
  115. package/dist/esm/event/behavior/keyup.js +22 -0
  116. package/dist/esm/event/behavior/paste.js +27 -0
  117. package/dist/esm/event/behavior/registry.js +3 -0
  118. package/dist/esm/event/createEvent.js +189 -0
  119. package/dist/esm/event/dispatchEvent.js +33 -0
  120. package/dist/esm/event/dom-events.d.js +1 -0
  121. package/dist/esm/event/eventMap.js +57 -0
  122. package/dist/esm/event/index.js +19 -0
  123. package/dist/esm/event/types.js +1 -0
  124. package/dist/esm/event/wrapEvent.js +7 -0
  125. package/dist/esm/index.js +2 -0
  126. package/dist/esm/keyboard/index.js +54 -0
  127. package/dist/esm/keyboard/keyMap.js +154 -0
  128. package/dist/esm/keyboard/parseKeyDef.js +54 -0
  129. package/dist/esm/options.js +37 -0
  130. package/dist/esm/package.json +3 -0
  131. package/dist/esm/pointer/index.js +84 -0
  132. package/dist/esm/pointer/keyMap.js +31 -0
  133. package/dist/esm/pointer/parseKeyDef.js +35 -0
  134. package/dist/esm/setup/api.js +12 -0
  135. package/dist/esm/setup/config.js +3 -0
  136. package/dist/esm/setup/directApi.js +58 -0
  137. package/dist/esm/setup/index.js +10 -0
  138. package/dist/esm/setup/setup.js +90 -0
  139. package/dist/esm/setup/wrapAsync.js +9 -0
  140. package/dist/esm/system/index.js +27 -0
  141. package/dist/esm/system/keyboard.js +131 -0
  142. package/dist/esm/system/pointer/buttons.js +63 -0
  143. package/dist/esm/system/pointer/device.js +17 -0
  144. package/dist/esm/system/pointer/index.js +141 -0
  145. package/dist/esm/system/pointer/mouse.js +182 -0
  146. package/dist/esm/system/pointer/pointer.js +110 -0
  147. package/dist/esm/utility/clear.js +38 -0
  148. package/dist/esm/utility/index.js +4 -0
  149. package/dist/esm/utility/selectOptions.js +120 -0
  150. package/dist/esm/utility/type.js +37 -0
  151. package/dist/esm/utility/upload.js +68 -0
  152. package/dist/esm/utils/click/isClickableInput.js +18 -0
  153. package/dist/esm/utils/dataTransfer/Blob.js +14 -0
  154. package/dist/esm/utils/dataTransfer/Clipboard.js +159 -0
  155. package/dist/esm/utils/dataTransfer/DataTransfer.js +119 -0
  156. package/dist/esm/utils/dataTransfer/FileList.js +22 -0
  157. package/dist/esm/utils/edit/buildTimeValue.js +32 -0
  158. package/dist/esm/utils/edit/getValue.js +31 -0
  159. package/dist/esm/utils/edit/input.js +158 -0
  160. package/dist/esm/utils/edit/isContentEditable.js +15 -0
  161. package/dist/esm/utils/edit/isEditable.js +30 -0
  162. package/dist/esm/utils/edit/isValidDateOrTimeValue.js +7 -0
  163. package/dist/esm/utils/edit/maxLength.js +34 -0
  164. package/dist/esm/utils/edit/setFiles.js +55 -0
  165. package/dist/esm/utils/edit/walkRadio.js +24 -0
  166. package/dist/esm/utils/focus/blur.js +12 -0
  167. package/dist/esm/utils/focus/copySelection.js +42 -0
  168. package/dist/esm/utils/focus/cursor.js +139 -0
  169. package/dist/esm/utils/focus/focus.js +22 -0
  170. package/dist/esm/utils/focus/getActiveElement.js +19 -0
  171. package/dist/esm/utils/focus/getTabDestination.js +78 -0
  172. package/dist/esm/utils/focus/isFocusable.js +7 -0
  173. package/dist/esm/utils/focus/resolveCaretPosition.js +74 -0
  174. package/dist/esm/utils/focus/selectAll.js +49 -0
  175. package/dist/esm/utils/focus/selection.js +274 -0
  176. package/dist/esm/utils/focus/selector.js +12 -0
  177. package/dist/esm/utils/index.js +36 -0
  178. package/dist/esm/utils/keyDef/readNextDescriptor.js +88 -0
  179. package/dist/esm/utils/misc/cloneEvent.js +5 -0
  180. package/dist/esm/utils/misc/dom-helpers.d.js +1 -0
  181. package/dist/esm/utils/misc/eventWrapper.js +11 -0
  182. package/dist/esm/utils/misc/findClosest.js +12 -0
  183. package/dist/esm/utils/misc/getDocumentFromNode.js +8 -0
  184. package/dist/esm/utils/misc/getTreeDiff.js +23 -0
  185. package/dist/esm/utils/misc/getWindow.js +7 -0
  186. package/dist/esm/utils/misc/isDescendantOrSelf.js +12 -0
  187. package/dist/esm/utils/misc/isDisabled.js +30 -0
  188. package/dist/esm/utils/misc/isElementType.js +18 -0
  189. package/dist/esm/utils/misc/isVisible.js +17 -0
  190. package/dist/esm/utils/misc/level.js +15 -0
  191. package/dist/esm/utils/misc/wait.js +12 -0
  192. package/dist/esm/utils/pointer/cssPointerEvents.js +106 -0
  193. package/dist/types/event/createEvent.d.ts +2 -8
  194. package/dist/types/event/eventMap.d.ts +204 -181
  195. package/dist/types/event/index.d.ts +1 -2
  196. package/dist/types/event/types.d.ts +6 -0
  197. package/dist/types/index.d.ts +2 -2
  198. package/dist/types/keyboard/index.d.ts +2 -6
  199. package/dist/types/keyboard/keyMap.d.ts +1 -1
  200. package/dist/types/keyboard/parseKeyDef.d.ts +1 -1
  201. package/dist/types/options.d.ts +4 -4
  202. package/dist/types/pointer/index.d.ts +23 -5
  203. package/dist/types/pointer/keyMap.d.ts +1 -1
  204. package/dist/types/pointer/parseKeyDef.d.ts +1 -1
  205. package/dist/types/setup/config.d.ts +3 -7
  206. package/dist/types/setup/directApi.d.ts +21 -16
  207. package/dist/types/setup/index.d.ts +1 -2
  208. package/dist/types/setup/setup.d.ts +7 -1
  209. package/dist/types/system/index.d.ts +10 -0
  210. package/dist/types/system/keyboard.d.ts +53 -0
  211. package/dist/types/system/pointer/buttons.d.ts +23 -0
  212. package/dist/types/system/pointer/device.d.ts +8 -0
  213. package/dist/types/system/pointer/index.d.ts +39 -0
  214. package/dist/types/system/pointer/mouse.d.ts +26 -0
  215. package/dist/types/system/pointer/pointer.d.ts +30 -0
  216. package/dist/types/utils/click/isClickableInput.d.ts +15 -1
  217. package/dist/types/utils/focus/getActiveElement.d.ts +1 -0
  218. package/dist/types/utils/focus/resolveCaretPosition.d.ts +8 -0
  219. package/dist/types/utils/focus/selection.d.ts +28 -1
  220. package/dist/types/utils/index.d.ts +2 -3
  221. package/dist/types/utils/misc/getTreeDiff.d.ts +1 -0
  222. package/package.json +9 -15
  223. package/dist/index.cjs +0 -3104
  224. package/dist/index.mjs +0 -3155
  225. package/dist/types/event/eventTypes.d.ts +0 -2
  226. package/dist/types/keyboard/keyboardAction.d.ts +0 -10
  227. package/dist/types/keyboard/modifiers.d.ts +0 -9
  228. package/dist/types/keyboard/types.d.ts +0 -73
  229. package/dist/types/pointer/firePointerEvents.d.ts +0 -11
  230. package/dist/types/pointer/pointerAction.d.ts +0 -7
  231. package/dist/types/pointer/pointerMove.d.ts +0 -6
  232. package/dist/types/pointer/pointerPress.d.ts +0 -8
  233. package/dist/types/pointer/resolveSelectionTarget.d.ts +0 -5
  234. package/dist/types/pointer/types.d.ts +0 -62
  235. package/dist/types/utils/keyboard/getKeyEventProps.d.ts +0 -5
  236. package/dist/types/utils/keyboard/getUIEventModifiers.d.ts +0 -15
  237. package/dist/types/utils/pointer/mouseButtons.d.ts +0 -12
@@ -0,0 +1,90 @@
1
+ import { prepareDocument } from '../document/index.js';
2
+ import { bindDispatchUIEvent } from '../event/index.js';
3
+ import { defaultOptionsSetup, defaultOptionsDirect } from '../options.js';
4
+ import '../utils/click/isClickableInput.js';
5
+ import { attachClipboardStubToView } from '../utils/dataTransfer/Clipboard.js';
6
+ import '../utils/edit/maxLength.js';
7
+ import '../utils/edit/isEditable.js';
8
+ import '@testing-library/dom';
9
+ import '@testing-library/dom/dist/helpers.js';
10
+ import '../utils/keyDef/readNextDescriptor.js';
11
+ import { getDocumentFromNode } from '../utils/misc/getDocumentFromNode.js';
12
+ import { setLevelRef, ApiLevel } from '../utils/misc/level.js';
13
+ import { wait } from '../utils/misc/wait.js';
14
+ import { System } from '../system/index.js';
15
+ import { Config } from './config.js';
16
+ import * as api from './api.js';
17
+ import { wrapAsync } from './wrapAsync.js';
18
+
19
+ function createConfig(options = {}, defaults = defaultOptionsSetup, node) {
20
+ const document = getDocument(options, node, defaults);
21
+ return {
22
+ ...defaults,
23
+ ...options,
24
+ document,
25
+ system: options.system ?? new System()
26
+ };
27
+ }
28
+ /**
29
+ * Start a "session" with userEvent.
30
+ * All APIs returned by this function share an input device state and a default configuration.
31
+ */ function setupMain(options = {}) {
32
+ const config = createConfig(options);
33
+ prepareDocument(config.document);
34
+ const view = config.document.defaultView ?? /* istanbul ignore next */ globalThis.window;
35
+ attachClipboardStubToView(view);
36
+ return doSetup(config);
37
+ }
38
+ /**
39
+ * Setup in direct call per `userEvent.anyApi()`
40
+ */ function setupDirect({ keyboardState , pointerState , ...options } = {}, node) {
41
+ const config = createConfig({
42
+ ...options,
43
+ system: pointerState ?? keyboardState
44
+ }, defaultOptionsDirect, node);
45
+ prepareDocument(config.document);
46
+ return {
47
+ config,
48
+ api: doSetup(config)
49
+ };
50
+ }
51
+ /**
52
+ * Create a set of callbacks with different default settings but the same state.
53
+ */ function setupSub(options) {
54
+ return doSetup({
55
+ ...this[Config],
56
+ ...options
57
+ });
58
+ }
59
+ function wrapAndBindImpl(instance, impl) {
60
+ function method(...args) {
61
+ setLevelRef(instance[Config], ApiLevel.Call);
62
+ return wrapAsync(()=>impl.apply(instance, args).then(async (ret)=>{
63
+ await wait(instance[Config]);
64
+ return ret;
65
+ }));
66
+ }
67
+ Object.defineProperty(method, 'name', {
68
+ get: ()=>impl.name
69
+ });
70
+ return method;
71
+ }
72
+ function doSetup(config) {
73
+ const instance = {
74
+ [Config]: config,
75
+ dispatchUIEvent: bindDispatchUIEvent(config),
76
+ ...api
77
+ };
78
+ return {
79
+ ...Object.fromEntries(Object.entries(api).map(([name, api])=>[
80
+ name,
81
+ wrapAndBindImpl(instance, api),
82
+ ])),
83
+ setup: setupSub.bind(instance)
84
+ };
85
+ }
86
+ function getDocument(options, node, defaults) {
87
+ return (options.document ?? (node && getDocumentFromNode(node))) ?? defaults.document;
88
+ }
89
+
90
+ export { createConfig, setupDirect, setupMain, setupSub };
@@ -0,0 +1,9 @@
1
+ import { getConfig } from '@testing-library/dom';
2
+
3
+ /**
4
+ * Wrap an internal Promise
5
+ */ function wrapAsync(implementation) {
6
+ return getConfig().asyncWrapper(implementation);
7
+ }
8
+
9
+ export { wrapAsync };
@@ -0,0 +1,27 @@
1
+ import { KeyboardHost } from './keyboard.js';
2
+ import { PointerHost } from './pointer/index.js';
3
+
4
+ /**
5
+ * @internal Do not create/alter this by yourself as this type might be subject to changes.
6
+ */ class System {
7
+ keyboard = new KeyboardHost(this);
8
+ pointer = new PointerHost(this);
9
+ getUIEventModifiers() {
10
+ return {
11
+ altKey: this.keyboard.modifiers.Alt,
12
+ ctrlKey: this.keyboard.modifiers.Control,
13
+ metaKey: this.keyboard.modifiers.Meta,
14
+ shiftKey: this.keyboard.modifiers.Shift,
15
+ modifierAltGraph: this.keyboard.modifiers.AltGraph,
16
+ modifierCapsLock: this.keyboard.modifiers.CapsLock,
17
+ modifierFn: this.keyboard.modifiers.Fn,
18
+ modifierFnLock: this.keyboard.modifiers.FnLock,
19
+ modifierNumLock: this.keyboard.modifiers.NumLock,
20
+ modifierScrollLock: this.keyboard.modifiers.ScrollLock,
21
+ modifierSymbol: this.keyboard.modifiers.Symbol,
22
+ modifierSymbolLock: this.keyboard.modifiers.SymbolLock
23
+ };
24
+ }
25
+ }
26
+
27
+ export { System };
@@ -0,0 +1,131 @@
1
+ import { dispatchUIEvent } from '../event/index.js';
2
+ import '../utils/click/isClickableInput.js';
3
+ import '../utils/dataTransfer/Clipboard.js';
4
+ import '../utils/edit/maxLength.js';
5
+ import '../utils/edit/isEditable.js';
6
+ import '@testing-library/dom';
7
+ import { getActiveElementOrBody } from '../utils/focus/getActiveElement.js';
8
+ import '@testing-library/dom/dist/helpers.js';
9
+ import '../utils/keyDef/readNextDescriptor.js';
10
+ import '../utils/misc/level.js';
11
+ import '../options.js';
12
+
13
+ var DOM_KEY_LOCATION;
14
+ (function(DOM_KEY_LOCATION) {
15
+ DOM_KEY_LOCATION[DOM_KEY_LOCATION["STANDARD"] = 0] = "STANDARD";
16
+ DOM_KEY_LOCATION[DOM_KEY_LOCATION["LEFT"] = 1] = "LEFT";
17
+ DOM_KEY_LOCATION[DOM_KEY_LOCATION["RIGHT"] = 2] = "RIGHT";
18
+ DOM_KEY_LOCATION[DOM_KEY_LOCATION["NUMPAD"] = 3] = "NUMPAD";
19
+ })(DOM_KEY_LOCATION || (DOM_KEY_LOCATION = {}));
20
+ const modifierKeys = [
21
+ 'Alt',
22
+ 'AltGraph',
23
+ 'Control',
24
+ 'Fn',
25
+ 'Meta',
26
+ 'Shift',
27
+ 'Symbol',
28
+ ];
29
+ function isModifierKey(key) {
30
+ return modifierKeys.includes(key);
31
+ }
32
+ const modifierLocks = [
33
+ 'CapsLock',
34
+ 'FnLock',
35
+ 'NumLock',
36
+ 'ScrollLock',
37
+ 'SymbolLock',
38
+ ];
39
+ function isModifierLock(key) {
40
+ return modifierLocks.includes(key);
41
+ }
42
+ class KeyboardHost {
43
+ constructor(system){
44
+ this.system = system;
45
+ }
46
+ modifiers = {
47
+ Alt: false,
48
+ AltGraph: false,
49
+ CapsLock: false,
50
+ Control: false,
51
+ Fn: false,
52
+ FnLock: false,
53
+ Meta: false,
54
+ NumLock: false,
55
+ ScrollLock: false,
56
+ Shift: false,
57
+ Symbol: false,
58
+ SymbolLock: false
59
+ };
60
+ pressed = {};
61
+ carryChar = '';
62
+ lastKeydownTarget = undefined;
63
+ modifierLockStart = {};
64
+ isKeyPressed(keyDef) {
65
+ return !!this.pressed[String(keyDef.code)];
66
+ }
67
+ getPressedKeys() {
68
+ return Object.values(this.pressed).map((p)=>p.keyDef);
69
+ }
70
+ /** Press a key */ async keydown(config, keyDef) {
71
+ const key = String(keyDef.key);
72
+ const code = String(keyDef.code);
73
+ const target = getActiveElementOrBody(config.document);
74
+ this.setKeydownTarget(target);
75
+ this.pressed[code] ??= {
76
+ keyDef,
77
+ unpreventedDefault: false
78
+ };
79
+ if (isModifierKey(key)) {
80
+ this.modifiers[key] = true;
81
+ }
82
+ const unprevented = dispatchUIEvent(config, target, 'keydown', {
83
+ key,
84
+ code
85
+ });
86
+ if (isModifierLock(key) && !this.modifiers[key]) {
87
+ this.modifiers[key] = true;
88
+ this.modifierLockStart[key] = true;
89
+ }
90
+ this.pressed[code].unpreventedDefault ||= unprevented;
91
+ if (unprevented && this.hasKeyPress(key)) {
92
+ dispatchUIEvent(config, getActiveElementOrBody(config.document), 'keypress', {
93
+ key,
94
+ code,
95
+ charCode: keyDef.key === 'Enter' ? 13 : String(keyDef.key).charCodeAt(0)
96
+ });
97
+ }
98
+ }
99
+ /** Release a key */ async keyup(config, keyDef) {
100
+ const key = String(keyDef.key);
101
+ const code = String(keyDef.code);
102
+ const unprevented = this.pressed[code].unpreventedDefault;
103
+ // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
104
+ delete this.pressed[code];
105
+ if (isModifierKey(key) && !Object.values(this.pressed).find((p)=>p.keyDef.key === key)) {
106
+ this.modifiers[key] = false;
107
+ }
108
+ dispatchUIEvent(config, getActiveElementOrBody(config.document), 'keyup', {
109
+ key,
110
+ code
111
+ }, !unprevented);
112
+ if (isModifierLock(key) && this.modifiers[key]) {
113
+ if (this.modifierLockStart[key]) {
114
+ this.modifierLockStart[key] = false;
115
+ } else {
116
+ this.modifiers[key] = false;
117
+ }
118
+ }
119
+ }
120
+ setKeydownTarget(target) {
121
+ if (target !== this.lastKeydownTarget) {
122
+ this.carryChar = '';
123
+ }
124
+ this.lastKeydownTarget = target;
125
+ }
126
+ hasKeyPress(key) {
127
+ return (key.length === 1 || key === 'Enter') && !this.modifiers.Control && !this.modifiers.Alt;
128
+ }
129
+ }
130
+
131
+ export { DOM_KEY_LOCATION, KeyboardHost };
@@ -0,0 +1,63 @@
1
+ class Buttons {
2
+ pressed = {};
3
+ getButtons() {
4
+ let v = 0;
5
+ for (const button of Object.keys(this.pressed)){
6
+ // eslint-disable-next-line no-bitwise
7
+ v |= 2 ** Number(button);
8
+ }
9
+ return v;
10
+ }
11
+ down(keyDef) {
12
+ const button = getMouseButtonId(keyDef.button);
13
+ if (button in this.pressed) {
14
+ this.pressed[button].push(keyDef);
15
+ return undefined;
16
+ }
17
+ this.pressed[button] = [
18
+ keyDef
19
+ ];
20
+ return button;
21
+ }
22
+ up(keyDef) {
23
+ const button = getMouseButtonId(keyDef.button);
24
+ if (button in this.pressed) {
25
+ this.pressed[button] = this.pressed[button].filter((k)=>k.name !== keyDef.name);
26
+ if (this.pressed[button].length === 0) {
27
+ // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
28
+ delete this.pressed[button];
29
+ return button;
30
+ }
31
+ }
32
+ return undefined;
33
+ }
34
+ }
35
+ const MouseButton = {
36
+ primary: 0,
37
+ secondary: 1,
38
+ auxiliary: 2,
39
+ back: 3,
40
+ X1: 3,
41
+ forward: 4,
42
+ X2: 4
43
+ };
44
+ function getMouseButtonId(button = 0) {
45
+ if (button in MouseButton) {
46
+ return MouseButton[button];
47
+ }
48
+ return Number(button);
49
+ }
50
+ // On the `MouseEvent.button` property auxiliary and secondary button are flipped compared to `MouseEvent.buttons`.
51
+ const MouseButtonFlip = {
52
+ 1: 2,
53
+ 2: 1
54
+ };
55
+ function getMouseEventButton(button) {
56
+ button = getMouseButtonId(button);
57
+ if (button in MouseButtonFlip) {
58
+ return MouseButtonFlip[button];
59
+ }
60
+ return button;
61
+ }
62
+
63
+ export { Buttons, MouseButton, MouseButtonFlip, getMouseButtonId, getMouseEventButton };
@@ -0,0 +1,17 @@
1
+ class Device {
2
+ pressedKeys = new Set();
3
+ get countPressed() {
4
+ return this.pressedKeys.size;
5
+ }
6
+ isPressed(keyDef) {
7
+ return this.pressedKeys.has(keyDef.name);
8
+ }
9
+ addPressed(keyDef) {
10
+ return this.pressedKeys.add(keyDef.name);
11
+ }
12
+ removePressed(keyDef) {
13
+ return this.pressedKeys.delete(keyDef.name);
14
+ }
15
+ }
16
+
17
+ export { Device };
@@ -0,0 +1,141 @@
1
+ import { Buttons } from './buttons.js';
2
+ import { Device } from './device.js';
3
+ import { Mouse } from './mouse.js';
4
+ import { Pointer } from './pointer.js';
5
+
6
+ class PointerHost {
7
+ constructor(system){
8
+ this.system = system;
9
+ this.buttons = new Buttons();
10
+ this.mouse = new Mouse();
11
+ }
12
+ devices = new class {
13
+ registry = {};
14
+ get(k) {
15
+ this.registry[k] ??= new Device();
16
+ return this.registry[k];
17
+ }
18
+ }();
19
+ pointers = new class {
20
+ registry = {
21
+ mouse: new Pointer({
22
+ pointerId: 1,
23
+ pointerType: 'mouse',
24
+ isPrimary: true
25
+ })
26
+ };
27
+ nextId = 2;
28
+ new(pointerName, keyDef) {
29
+ const isPrimary = keyDef.pointerType !== 'touch' || !Object.values(this.registry).some((p)=>p.pointerType === 'touch' && !p.isCancelled);
30
+ if (!isPrimary) {
31
+ Object.values(this.registry).forEach((p)=>{
32
+ if (p.pointerType === keyDef.pointerType && !p.isCancelled) {
33
+ p.isMultitouch = true;
34
+ }
35
+ });
36
+ }
37
+ this.registry[pointerName] = new Pointer({
38
+ pointerId: this.nextId++,
39
+ pointerType: keyDef.pointerType,
40
+ isPrimary
41
+ });
42
+ return this.registry[pointerName];
43
+ }
44
+ get(pointerName) {
45
+ if (!this.has(pointerName)) {
46
+ throw new Error(`Trying to access pointer "${pointerName}" which does not exist.`);
47
+ }
48
+ return this.registry[pointerName];
49
+ }
50
+ has(pointerName) {
51
+ return pointerName in this.registry;
52
+ }
53
+ }();
54
+ isKeyPressed(keyDef) {
55
+ return this.devices.get(keyDef.pointerType).isPressed(keyDef);
56
+ }
57
+ async press(config, keyDef, position) {
58
+ const pointerName = this.getPointerName(keyDef);
59
+ const pointer = keyDef.pointerType === 'touch' ? this.pointers.new(pointerName, keyDef).init(config, position) : this.pointers.get(pointerName);
60
+ // TODO: deprecate the following implicit setting of position
61
+ pointer.position = position;
62
+ if (pointer.pointerType !== 'touch') {
63
+ this.mouse.position = position;
64
+ }
65
+ this.devices.get(keyDef.pointerType).addPressed(keyDef);
66
+ this.buttons.down(keyDef);
67
+ pointer.down(config, keyDef);
68
+ if (pointer.pointerType !== 'touch' && !pointer.isPrevented) {
69
+ this.mouse.down(config, keyDef, pointer);
70
+ }
71
+ }
72
+ async move(config, pointerName, position) {
73
+ const pointer = this.pointers.get(pointerName);
74
+ // In (some?) browsers this order of events can be observed.
75
+ // This interweaving of events is probably unnecessary.
76
+ // While the order of mouse (or pointer) events is defined per spec,
77
+ // the order in which they interweave/follow on a user interaction depends on the implementation.
78
+ const pointermove = pointer.move(config, position);
79
+ const mousemove = pointer.pointerType === 'touch' || pointer.isPrevented && pointer.isDown ? undefined : this.mouse.move(config, position);
80
+ pointermove?.leave();
81
+ mousemove?.leave();
82
+ pointermove?.enter();
83
+ mousemove?.enter();
84
+ pointermove?.move();
85
+ mousemove?.move();
86
+ }
87
+ async release(config, keyDef, position) {
88
+ const device = this.devices.get(keyDef.pointerType);
89
+ device.removePressed(keyDef);
90
+ this.buttons.up(keyDef);
91
+ const pointer = this.pointers.get(this.getPointerName(keyDef));
92
+ // TODO: deprecate the following implicit setting of position
93
+ pointer.position = position;
94
+ if (pointer.pointerType !== 'touch') {
95
+ this.mouse.position = position;
96
+ }
97
+ if (device.countPressed === 0) {
98
+ pointer.up(config, keyDef);
99
+ }
100
+ if (pointer.pointerType === 'touch') {
101
+ pointer.release(config);
102
+ }
103
+ if (!pointer.isPrevented) {
104
+ if (pointer.pointerType === 'touch' && !pointer.isMultitouch) {
105
+ const mousemove = this.mouse.move(config, pointer.position);
106
+ mousemove?.leave();
107
+ mousemove?.enter();
108
+ mousemove?.move();
109
+ this.mouse.down(config, keyDef, pointer);
110
+ }
111
+ if (!pointer.isMultitouch) {
112
+ const mousemove1 = this.mouse.move(config, pointer.position);
113
+ mousemove1?.leave();
114
+ mousemove1?.enter();
115
+ mousemove1?.move();
116
+ this.mouse.up(config, keyDef, pointer);
117
+ }
118
+ }
119
+ }
120
+ getPointerName(keyDef) {
121
+ return keyDef.pointerType === 'touch' ? keyDef.name : keyDef.pointerType;
122
+ }
123
+ getPreviousPosition(pointerName) {
124
+ return this.pointers.has(pointerName) ? this.pointers.get(pointerName).position : undefined;
125
+ }
126
+ resetClickCount() {
127
+ this.mouse.resetClickCount();
128
+ }
129
+ getMouseTarget(config) {
130
+ return this.mouse.position.target ?? config.document.body;
131
+ }
132
+ setMousePosition(position) {
133
+ this.mouse.position = position;
134
+ this.pointers.get('mouse').position = position;
135
+ }
136
+ }
137
+ function isDifferentPointerPosition(positionA, positionB) {
138
+ return positionA.target !== positionB.target || positionA.coords?.x !== positionB.coords?.y || positionA.coords?.y !== positionB.coords?.y || positionA.caret?.node !== positionB.caret?.node || positionA.caret?.offset !== positionB.caret?.offset;
139
+ }
140
+
141
+ export { PointerHost, isDifferentPointerPosition };
@@ -0,0 +1,182 @@
1
+ import { dispatchUIEvent } from '../../event/index.js';
2
+ import '../../utils/click/isClickableInput.js';
3
+ import '../../utils/dataTransfer/Clipboard.js';
4
+ import '../../utils/edit/maxLength.js';
5
+ import '../../utils/edit/isEditable.js';
6
+ import { focus } from '../../utils/focus/focus.js';
7
+ import '@testing-library/dom/dist/helpers.js';
8
+ import { isDisabled } from '../../utils/misc/isDisabled.js';
9
+ import '@testing-library/dom';
10
+ import { setSelectionPerMouseDown, modifySelectionPerMouseMove } from '../../utils/focus/selection.js';
11
+ import '../../utils/keyDef/readNextDescriptor.js';
12
+ import { getTreeDiff } from '../../utils/misc/getTreeDiff.js';
13
+ import '../../utils/misc/level.js';
14
+ import '../../options.js';
15
+ import { isDifferentPointerPosition } from './index.js';
16
+ import { Buttons, getMouseEventButton } from './buttons.js';
17
+
18
+ /**
19
+ * This object is the single "virtual" mouse that might be controlled by multiple different pointer devices.
20
+ */ class Mouse {
21
+ position = {};
22
+ buttons = new Buttons();
23
+ buttonDownTarget = {};
24
+ // According to spec the `detail` on click events should be the number
25
+ // of *consecutive* clicks with a specific button.
26
+ // On `mousedown` and `mouseup` it should be this number increased by one.
27
+ // But the browsers don't implement it this way.
28
+ // If another button is pressed,
29
+ // in Webkit: the `mouseup` on the previously pressed button has `detail: 0` and no `click`/`auxclick`.
30
+ // in Gecko: the `mouseup` and click events have the same detail as the `mousedown`.
31
+ // If there is a delay while a button is pressed,
32
+ // the `mouseup` and `click` are normal, but a following `mousedown` starts a new click count.
33
+ // We'll follow the minimal implementation of Webkit.
34
+ clickCount = new class {
35
+ down = {};
36
+ count = {};
37
+ incOnClick(button) {
38
+ const current = this.down[button] === undefined ? undefined : Number(this.down[button]) + 1;
39
+ this.count = this.count[button] === undefined ? {} : {
40
+ [button]: Number(this.count[button]) + 1
41
+ };
42
+ return current;
43
+ }
44
+ getOnDown(button) {
45
+ this.down = {
46
+ [button]: this.count[button] ?? 0
47
+ };
48
+ this.count = {
49
+ [button]: this.count[button] ?? 0
50
+ };
51
+ return Number(this.count[button]) + 1;
52
+ }
53
+ getOnUp(button) {
54
+ return this.down[button] === undefined ? undefined : Number(this.down[button]) + 1;
55
+ }
56
+ reset() {
57
+ this.count = {};
58
+ }
59
+ }();
60
+ move(config, position) {
61
+ const prevPosition = this.position;
62
+ const prevTarget = this.getTarget(config);
63
+ this.position = position;
64
+ if (!isDifferentPointerPosition(prevPosition, position)) {
65
+ return;
66
+ }
67
+ const nextTarget = this.getTarget(config);
68
+ const init = this.getEventInit('mousemove');
69
+ const [leave, enter] = getTreeDiff(prevTarget, nextTarget);
70
+ return {
71
+ leave: ()=>{
72
+ if (prevTarget !== nextTarget) {
73
+ dispatchUIEvent(config, prevTarget, 'mouseout', init);
74
+ leave.forEach((el)=>dispatchUIEvent(config, el, 'mouseleave', init));
75
+ }
76
+ },
77
+ enter: ()=>{
78
+ if (prevTarget !== nextTarget) {
79
+ dispatchUIEvent(config, nextTarget, 'mouseover', init);
80
+ enter.forEach((el)=>dispatchUIEvent(config, el, 'mouseenter', init));
81
+ }
82
+ },
83
+ move: ()=>{
84
+ dispatchUIEvent(config, nextTarget, 'mousemove', init);
85
+ this.modifySelecting(config);
86
+ }
87
+ };
88
+ }
89
+ down(config, keyDef, pointer) {
90
+ const button = this.buttons.down(keyDef);
91
+ if (button === undefined) {
92
+ return;
93
+ }
94
+ const target = this.getTarget(config);
95
+ this.buttonDownTarget[button] = target;
96
+ const disabled = isDisabled(target);
97
+ const init = this.getEventInit('mousedown', keyDef.button);
98
+ if (disabled || dispatchUIEvent(config, target, 'mousedown', init)) {
99
+ this.startSelecting(config, init.detail);
100
+ focus(target);
101
+ }
102
+ if (!disabled && getMouseEventButton(keyDef.button) === 2) {
103
+ dispatchUIEvent(config, target, 'contextmenu', this.getEventInit('contextmenu', keyDef.button, pointer));
104
+ }
105
+ }
106
+ up(config, keyDef, pointer) {
107
+ const button = this.buttons.up(keyDef);
108
+ if (button === undefined) {
109
+ return;
110
+ }
111
+ const target = this.getTarget(config);
112
+ if (!isDisabled(target)) {
113
+ dispatchUIEvent(config, target, 'mouseup', this.getEventInit('mouseup', keyDef.button));
114
+ this.endSelecting();
115
+ const clickTarget = getTreeDiff(this.buttonDownTarget[button], target)[2][0];
116
+ if (clickTarget) {
117
+ const init = this.getEventInit('click', keyDef.button, pointer);
118
+ if (init.detail) {
119
+ dispatchUIEvent(config, clickTarget, init.button === 0 ? 'click' : 'auxclick', init);
120
+ if (init.button === 0 && init.detail === 2) {
121
+ dispatchUIEvent(config, clickTarget, 'dblclick', {
122
+ ...this.getEventInit('dblclick', keyDef.button),
123
+ detail: init.detail
124
+ });
125
+ }
126
+ }
127
+ }
128
+ }
129
+ }
130
+ resetClickCount() {
131
+ this.clickCount.reset();
132
+ }
133
+ getEventInit(type, button, pointer) {
134
+ const init = {
135
+ ...this.position.coords
136
+ };
137
+ if (pointer) {
138
+ init.pointerId = pointer.pointerId;
139
+ init.pointerType = pointer.pointerType;
140
+ init.isPrimary = pointer.isPrimary;
141
+ }
142
+ init.button = getMouseEventButton(button);
143
+ init.buttons = this.buttons.getButtons();
144
+ if (type === 'mousedown') {
145
+ init.detail = this.clickCount.getOnDown(init.button);
146
+ } else if (type === 'mouseup') {
147
+ init.detail = this.clickCount.getOnUp(init.button);
148
+ } else if (type === 'click' || type === 'auxclick') {
149
+ init.detail = this.clickCount.incOnClick(init.button);
150
+ }
151
+ return init;
152
+ }
153
+ getTarget(config) {
154
+ return this.position.target ?? config.document.body;
155
+ }
156
+ startSelecting(config, clickCount) {
157
+ // TODO: support extending range (shift)
158
+ this.selecting = setSelectionPerMouseDown({
159
+ document: config.document,
160
+ target: this.getTarget(config),
161
+ node: this.position.caret?.node,
162
+ offset: this.position.caret?.offset,
163
+ clickCount
164
+ });
165
+ }
166
+ modifySelecting(config) {
167
+ if (!this.selecting) {
168
+ return;
169
+ }
170
+ modifySelectionPerMouseMove(this.selecting, {
171
+ document: config.document,
172
+ target: this.getTarget(config),
173
+ node: this.position.caret?.node,
174
+ offset: this.position.caret?.offset
175
+ });
176
+ }
177
+ endSelecting() {
178
+ this.selecting = undefined;
179
+ }
180
+ }
181
+
182
+ export { Mouse };