@testing-library/user-event 14.0.0-alpha.6 → 14.0.0-beta.1

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 (137) hide show
  1. package/dist/clipboard/copy.d.ts +2 -0
  2. package/dist/clipboard/copy.js +36 -0
  3. package/dist/clipboard/cut.d.ts +2 -0
  4. package/dist/clipboard/cut.js +42 -0
  5. package/dist/clipboard/index.d.ts +3 -0
  6. package/dist/clipboard/index.js +44 -0
  7. package/dist/clipboard/paste.d.ts +2 -0
  8. package/dist/clipboard/paste.js +47 -0
  9. package/dist/convenience/click.d.ts +4 -0
  10. package/dist/convenience/click.js +52 -0
  11. package/dist/convenience/hover.d.ts +3 -0
  12. package/dist/convenience/hover.js +31 -0
  13. package/dist/convenience/index.d.ts +3 -0
  14. package/dist/convenience/index.js +44 -0
  15. package/dist/convenience/tab.d.ts +4 -0
  16. package/dist/convenience/tab.js +12 -0
  17. package/dist/document/index.d.ts +1 -0
  18. package/dist/document/selection.d.ts +17 -7
  19. package/dist/document/selection.js +33 -17
  20. package/dist/document/value.d.ts +1 -1
  21. package/dist/document/value.js +1 -1
  22. package/dist/index.d.ts +2 -7
  23. package/dist/index.js +3 -12
  24. package/dist/keyboard/getEventProps.d.ts +0 -2
  25. package/dist/keyboard/getEventProps.js +1 -7
  26. package/dist/keyboard/index.d.ts +6 -18
  27. package/dist/keyboard/index.js +9 -45
  28. package/dist/keyboard/keyMap.js +24 -48
  29. package/dist/keyboard/keyboardAction.d.ts +10 -0
  30. package/dist/keyboard/keyboardAction.js +156 -0
  31. package/dist/keyboard/{getNextKeyDef.d.ts → parseKeyDef.d.ts} +4 -6
  32. package/dist/keyboard/parseKeyDef.js +61 -0
  33. package/dist/keyboard/plugins/arrow.js +7 -12
  34. package/dist/keyboard/plugins/character.js +42 -54
  35. package/dist/keyboard/plugins/combination.d.ts +5 -0
  36. package/dist/keyboard/plugins/combination.js +19 -0
  37. package/dist/keyboard/plugins/control.js +4 -12
  38. package/dist/keyboard/plugins/functional.js +46 -33
  39. package/dist/keyboard/plugins/index.d.ts +0 -1
  40. package/dist/keyboard/plugins/index.js +4 -11
  41. package/dist/keyboard/types.d.ts +3 -19
  42. package/dist/options.d.ts +102 -0
  43. package/dist/options.js +36 -0
  44. package/dist/pointer/index.d.ts +6 -14
  45. package/dist/pointer/index.js +8 -42
  46. package/dist/pointer/parseKeyDef.d.ts +2 -2
  47. package/dist/pointer/parseKeyDef.js +1 -3
  48. package/dist/pointer/pointerAction.d.ts +3 -2
  49. package/dist/pointer/pointerAction.js +9 -15
  50. package/dist/pointer/pointerMove.d.ts +2 -1
  51. package/dist/pointer/pointerMove.js +28 -17
  52. package/dist/pointer/pointerPress.d.ts +2 -1
  53. package/dist/pointer/pointerPress.js +6 -4
  54. package/dist/pointer/resolveSelectionTarget.js +7 -6
  55. package/dist/pointer/types.d.ts +0 -11
  56. package/dist/setup/api.d.ts +5 -0
  57. package/dist/setup/api.js +111 -0
  58. package/dist/setup/config.d.ts +10 -0
  59. package/dist/setup/config.js +8 -0
  60. package/dist/setup/directApi.d.ts +20 -0
  61. package/dist/setup/directApi.js +95 -0
  62. package/dist/setup/index.d.ts +30 -0
  63. package/dist/setup/index.js +27 -0
  64. package/dist/setup/setup.d.ts +74 -0
  65. package/dist/setup/setup.js +98 -0
  66. package/dist/setup/wrapAsync.d.ts +4 -0
  67. package/dist/setup/wrapAsync.js +15 -0
  68. package/dist/{clear.d.ts → utility/clear.d.ts} +2 -2
  69. package/dist/utility/clear.js +30 -0
  70. package/dist/utility/index.d.ts +4 -0
  71. package/dist/utility/index.js +57 -0
  72. package/dist/utility/selectOptions.d.ts +3 -0
  73. package/dist/{selectOptions.js → utility/selectOptions.js} +31 -41
  74. package/dist/utility/type.d.ts +9 -0
  75. package/dist/utility/type.js +37 -0
  76. package/dist/utility/upload.d.ts +5 -0
  77. package/dist/{upload.js → utility/upload.js} +12 -40
  78. package/dist/utils/dataTransfer/Blob.d.ts +1 -0
  79. package/dist/utils/dataTransfer/Blob.js +21 -0
  80. package/dist/utils/dataTransfer/Clipboard.d.ts +9 -0
  81. package/dist/utils/dataTransfer/Clipboard.js +188 -0
  82. package/dist/utils/dataTransfer/DataTransfer.d.ts +2 -0
  83. package/dist/utils/dataTransfer/DataTransfer.js +158 -0
  84. package/dist/utils/dataTransfer/FileList.d.ts +1 -0
  85. package/dist/utils/dataTransfer/FileList.js +23 -0
  86. package/dist/utils/edit/calculateNewValue.d.ts +12 -5
  87. package/dist/utils/edit/calculateNewValue.js +19 -21
  88. package/dist/utils/edit/editInputElement.d.ts +16 -0
  89. package/dist/utils/edit/{fireInputEvent.js → editInputElement.js} +20 -26
  90. package/dist/utils/edit/isContentEditable.d.ts +4 -0
  91. package/dist/utils/edit/isContentEditable.js +14 -0
  92. package/dist/utils/edit/isEditable.d.ts +1 -0
  93. package/dist/utils/edit/prepareInput.d.ts +7 -0
  94. package/dist/utils/edit/prepareInput.js +77 -0
  95. package/dist/utils/edit/setFiles.d.ts +12 -0
  96. package/dist/utils/edit/setFiles.js +72 -0
  97. package/dist/utils/focus/copySelection.d.ts +1 -0
  98. package/dist/utils/focus/copySelection.js +36 -0
  99. package/dist/utils/focus/focus.js +3 -0
  100. package/dist/utils/focus/selectAll.d.ts +5 -0
  101. package/dist/utils/focus/selectAll.js +51 -0
  102. package/dist/utils/focus/selection.d.ts +42 -0
  103. package/dist/utils/focus/selection.js +190 -0
  104. package/dist/utils/index.d.ts +11 -4
  105. package/dist/utils/index.js +124 -33
  106. package/dist/utils/misc/getDocumentFromNode.d.ts +1 -0
  107. package/dist/utils/misc/getDocumentFromNode.js +14 -0
  108. package/dist/utils/pointer/firePointerEvents.d.ts +12 -1
  109. package/dist/utils/pointer/firePointerEvents.js +62 -5
  110. package/package.json +1 -1
  111. package/dist/clear.js +0 -46
  112. package/dist/click.d.ts +0 -9
  113. package/dist/click.js +0 -62
  114. package/dist/hover.d.ts +0 -4
  115. package/dist/hover.js +0 -43
  116. package/dist/keyboard/getNextKeyDef.js +0 -85
  117. package/dist/keyboard/keyboardImplementation.d.ts +0 -3
  118. package/dist/keyboard/keyboardImplementation.js +0 -169
  119. package/dist/keyboard/specialCharMap.d.ts +0 -22
  120. package/dist/keyboard/specialCharMap.js +0 -30
  121. package/dist/paste.d.ts +0 -7
  122. package/dist/paste.js +0 -67
  123. package/dist/selectOptions.d.ts +0 -4
  124. package/dist/setup.d.ts +0 -92
  125. package/dist/setup.js +0 -203
  126. package/dist/tab.d.ts +0 -5
  127. package/dist/tab.js +0 -12
  128. package/dist/type.d.ts +0 -14
  129. package/dist/type.js +0 -74
  130. package/dist/upload.d.ts +0 -10
  131. package/dist/utils/edit/cursorPosition.d.ts +0 -2
  132. package/dist/utils/edit/cursorPosition.js +0 -35
  133. package/dist/utils/edit/fireInputEvent.d.ts +0 -8
  134. package/dist/utils/edit/selectionRange.d.ts +0 -5
  135. package/dist/utils/edit/selectionRange.js +0 -64
  136. package/dist/utils/pointer/fakeEvent.d.ts +0 -20
  137. package/dist/utils/pointer/fakeEvent.js +0 -91
@@ -32,112 +32,88 @@ const defaultKeyMap = [// alphanumeric keys
32
32
  }, {
33
33
  code: 'AltLeft',
34
34
  key: 'Alt',
35
- location: _types.DOM_KEY_LOCATION.LEFT,
36
- keyCode: 18
35
+ location: _types.DOM_KEY_LOCATION.LEFT
37
36
  }, {
38
37
  code: 'AltRight',
39
38
  key: 'Alt',
40
- location: _types.DOM_KEY_LOCATION.RIGHT,
41
- keyCode: 18
39
+ location: _types.DOM_KEY_LOCATION.RIGHT
42
40
  }, {
43
41
  code: 'ShiftLeft',
44
42
  key: 'Shift',
45
- location: _types.DOM_KEY_LOCATION.LEFT,
46
- keyCode: 16
43
+ location: _types.DOM_KEY_LOCATION.LEFT
47
44
  }, {
48
45
  code: 'ShiftRight',
49
46
  key: 'Shift',
50
- location: _types.DOM_KEY_LOCATION.RIGHT,
51
- keyCode: 16
47
+ location: _types.DOM_KEY_LOCATION.RIGHT
52
48
  }, {
53
49
  code: 'ControlLeft',
54
50
  key: 'Control',
55
- location: _types.DOM_KEY_LOCATION.LEFT,
56
- keyCode: 17
51
+ location: _types.DOM_KEY_LOCATION.LEFT
57
52
  }, {
58
53
  code: 'ControlRight',
59
54
  key: 'Control',
60
- location: _types.DOM_KEY_LOCATION.RIGHT,
61
- keyCode: 17
55
+ location: _types.DOM_KEY_LOCATION.RIGHT
62
56
  }, {
63
57
  code: 'MetaLeft',
64
58
  key: 'Meta',
65
- location: _types.DOM_KEY_LOCATION.LEFT,
66
- keyCode: 93
59
+ location: _types.DOM_KEY_LOCATION.LEFT
67
60
  }, {
68
61
  code: 'MetaRight',
69
62
  key: 'Meta',
70
- location: _types.DOM_KEY_LOCATION.RIGHT,
71
- keyCode: 93
63
+ location: _types.DOM_KEY_LOCATION.RIGHT
72
64
  }, {
73
65
  code: 'OSLeft',
74
66
  key: 'OS',
75
- location: _types.DOM_KEY_LOCATION.LEFT,
76
- keyCode: 91
67
+ location: _types.DOM_KEY_LOCATION.LEFT
77
68
  }, {
78
69
  code: 'OSRight',
79
70
  key: 'OS',
80
- location: _types.DOM_KEY_LOCATION.RIGHT,
81
- keyCode: 91
71
+ location: _types.DOM_KEY_LOCATION.RIGHT
82
72
  }, {
83
73
  code: 'Tab',
84
- key: 'Tab',
85
- keyCode: 9
74
+ key: 'Tab'
86
75
  }, {
87
76
  code: 'CapsLock',
88
- key: 'CapsLock',
89
- keyCode: 20
77
+ key: 'CapsLock'
90
78
  }, {
91
79
  code: 'Backspace',
92
- key: 'Backspace',
93
- keyCode: 8
80
+ key: 'Backspace'
94
81
  }, {
95
82
  code: 'Enter',
96
- key: 'Enter',
97
- keyCode: 13
83
+ key: 'Enter'
98
84
  }, // function
99
85
  {
100
86
  code: 'Escape',
101
- key: 'Escape',
102
- keyCode: 27
87
+ key: 'Escape'
103
88
  }, // arrows
104
89
  {
105
90
  code: 'ArrowUp',
106
- key: 'ArrowUp',
107
- keyCode: 38
91
+ key: 'ArrowUp'
108
92
  }, {
109
93
  code: 'ArrowDown',
110
- key: 'ArrowDown',
111
- keyCode: 40
94
+ key: 'ArrowDown'
112
95
  }, {
113
96
  code: 'ArrowLeft',
114
- key: 'ArrowLeft',
115
- keyCode: 37
97
+ key: 'ArrowLeft'
116
98
  }, {
117
99
  code: 'ArrowRight',
118
- key: 'ArrowRight',
119
- keyCode: 39
100
+ key: 'ArrowRight'
120
101
  }, // control pad
121
102
  {
122
103
  code: 'Home',
123
- key: 'Home',
124
- keyCode: 36
104
+ key: 'Home'
125
105
  }, {
126
106
  code: 'End',
127
- key: 'End',
128
- keyCode: 35
107
+ key: 'End'
129
108
  }, {
130
109
  code: 'Delete',
131
- key: 'Delete',
132
- keyCode: 46
110
+ key: 'Delete'
133
111
  }, {
134
112
  code: 'PageUp',
135
- key: 'PageUp',
136
- keyCode: 33
113
+ key: 'PageUp'
137
114
  }, {
138
115
  code: 'PageDown',
139
- key: 'PageDown',
140
- keyCode: 34
116
+ key: 'PageDown'
141
117
  } // TODO: add mappings
142
118
  ];
143
119
  exports.defaultKeyMap = defaultKeyMap;
@@ -0,0 +1,10 @@
1
+ import { Config } from '../setup';
2
+ import { keyboardKey } from './types';
3
+ export interface KeyboardAction {
4
+ keyDef: keyboardKey;
5
+ releasePrevious: boolean;
6
+ releaseSelf: boolean;
7
+ repeat: number;
8
+ }
9
+ export declare function keyboardAction(config: Config, actions: KeyboardAction[]): Promise<void>;
10
+ export declare function releaseAllKeys(config: Config): Promise<void>;
@@ -0,0 +1,156 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.keyboardAction = keyboardAction;
7
+ exports.releaseAllKeys = releaseAllKeys;
8
+
9
+ var _dom = require("@testing-library/dom");
10
+
11
+ var _utils = require("../utils");
12
+
13
+ var plugins = _interopRequireWildcard(require("./plugins"));
14
+
15
+ var _getEventProps = require("./getEventProps");
16
+
17
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
18
+
19
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
20
+
21
+ async function keyboardAction(config, actions) {
22
+ for (let i = 0; i < actions.length; i++) {
23
+ await keyboardKeyAction(config, actions[i]);
24
+
25
+ if (typeof config.delay === 'number' && i < actions.length - 1) {
26
+ await (0, _utils.wait)(config.delay);
27
+ }
28
+ }
29
+ }
30
+
31
+ async function keyboardKeyAction(config, {
32
+ keyDef,
33
+ releasePrevious,
34
+ releaseSelf,
35
+ repeat
36
+ }) {
37
+ const {
38
+ document,
39
+ keyboardState,
40
+ delay
41
+ } = config;
42
+
43
+ const getCurrentElement = () => getActive(document); // Release the key automatically if it was pressed before.
44
+
45
+
46
+ const pressed = keyboardState.pressed.find(p => p.keyDef === keyDef);
47
+
48
+ if (pressed) {
49
+ await keyup(keyDef, getCurrentElement, config, pressed.unpreventedDefault);
50
+ }
51
+
52
+ if (!releasePrevious) {
53
+ let unpreventedDefault = true;
54
+
55
+ for (let i = 1; i <= repeat; i++) {
56
+ unpreventedDefault = await keydown(keyDef, getCurrentElement, config);
57
+
58
+ if (unpreventedDefault && hasKeyPress(keyDef, config)) {
59
+ await keypress(keyDef, getCurrentElement, config);
60
+ }
61
+
62
+ if (typeof delay === 'number' && i < repeat) {
63
+ await (0, _utils.wait)(delay);
64
+ }
65
+ } // Release the key only on the last iteration on `state.repeatKey`.
66
+
67
+
68
+ if (releaseSelf) {
69
+ await keyup(keyDef, getCurrentElement, config, unpreventedDefault);
70
+ }
71
+ }
72
+ }
73
+
74
+ function getActive(document) {
75
+ var _getActiveElement;
76
+
77
+ return (_getActiveElement = (0, _utils.getActiveElement)(document)) != null ? _getActiveElement :
78
+ /* istanbul ignore next */
79
+ document.body;
80
+ }
81
+
82
+ async function releaseAllKeys(config) {
83
+ const getCurrentElement = () => getActive(config.document);
84
+
85
+ for (const k of config.keyboardState.pressed) {
86
+ await keyup(k.keyDef, getCurrentElement, config, k.unpreventedDefault);
87
+ }
88
+ }
89
+
90
+ async function keydown(keyDef, getCurrentElement, config) {
91
+ const element = getCurrentElement(); // clear carried characters when focus is moved
92
+
93
+ if (element !== config.keyboardState.activeElement) {
94
+ config.keyboardState.carryValue = undefined;
95
+ config.keyboardState.carryChar = '';
96
+ }
97
+
98
+ config.keyboardState.activeElement = element;
99
+ applyPlugins(plugins.preKeydownBehavior, keyDef, element, config);
100
+
101
+ const unpreventedDefault = _dom.fireEvent.keyDown(element, (0, _getEventProps.getKeyEventProps)(keyDef, config.keyboardState));
102
+
103
+ config.keyboardState.pressed.push({
104
+ keyDef,
105
+ unpreventedDefault
106
+ });
107
+
108
+ if (unpreventedDefault) {
109
+ // all default behavior like keypress/submit etc is applied to the currentElement
110
+ applyPlugins(plugins.keydownBehavior, keyDef, getCurrentElement(), config);
111
+ }
112
+
113
+ return unpreventedDefault;
114
+ }
115
+
116
+ async function keypress(keyDef, getCurrentElement, config) {
117
+ const element = getCurrentElement();
118
+
119
+ const unpreventedDefault = _dom.fireEvent.keyPress(element, { ...(0, _getEventProps.getKeyEventProps)(keyDef, config.keyboardState),
120
+ charCode: keyDef.key === 'Enter' ? 13 : String(keyDef.key).charCodeAt(0)
121
+ });
122
+
123
+ if (unpreventedDefault) {
124
+ applyPlugins(plugins.keypressBehavior, keyDef, getCurrentElement(), config);
125
+ }
126
+ }
127
+
128
+ async function keyup(keyDef, getCurrentElement, config, unprevented) {
129
+ const element = getCurrentElement();
130
+ applyPlugins(plugins.preKeyupBehavior, keyDef, element, config);
131
+
132
+ const unpreventedDefault = _dom.fireEvent.keyUp(element, (0, _getEventProps.getKeyEventProps)(keyDef, config.keyboardState));
133
+
134
+ if (unprevented && unpreventedDefault) {
135
+ applyPlugins(plugins.keyupBehavior, keyDef, getCurrentElement(), config);
136
+ }
137
+
138
+ config.keyboardState.pressed = config.keyboardState.pressed.filter(k => k.keyDef !== keyDef);
139
+ applyPlugins(plugins.postKeyupBehavior, keyDef, element, config);
140
+ }
141
+
142
+ function applyPlugins(pluginCollection, keyDef, element, config) {
143
+ const plugin = pluginCollection.find(p => p.matches(keyDef, element, config));
144
+
145
+ if (plugin) {
146
+ plugin.handle(keyDef, element, config);
147
+ }
148
+
149
+ return !!plugin;
150
+ }
151
+
152
+ function hasKeyPress(keyDef, config) {
153
+ var _keyDef$key;
154
+
155
+ return (((_keyDef$key = keyDef.key) == null ? void 0 : _keyDef$key.length) === 1 || keyDef.key === 'Enter') && !config.keyboardState.modifiers.ctrl && !config.keyboardState.modifiers.alt;
156
+ }
@@ -1,6 +1,6 @@
1
- import { keyboardKey, keyboardOptions } from './types';
1
+ import { keyboardKey } from './types';
2
2
  /**
3
- * Get the next key from keyMap
3
+ * Parse key defintions per `keyboardMap`
4
4
  *
5
5
  * Keys can be referenced by `{key}` or `{special}` as well as physical locations per `[code]`.
6
6
  * Everything else will be interpreted as a typed character - e.g. `a`.
@@ -8,12 +8,10 @@ import { keyboardKey, keyboardOptions } from './types';
8
8
  * Keeping the key pressed can be written as `{key>}`.
9
9
  * When keeping the key pressed you can choose how long (how many keydown and keypress) the key is pressed `{key>3}`.
10
10
  * You can then release the key per `{key>3/}` or keep it pressed and continue with the next key.
11
- * Modifiers like `{shift}` imply being kept pressed. This can be turned of per `{shift/}`.
12
11
  */
13
- export declare function getNextKeyDef(text: string, options: keyboardOptions): {
12
+ export declare function parseKeyDef(keyboardMap: keyboardKey[], text: string): {
14
13
  keyDef: keyboardKey;
15
- consumedLength: number;
16
14
  releasePrevious: boolean;
17
15
  releaseSelf: boolean;
18
16
  repeat: number;
19
- };
17
+ }[];
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.parseKeyDef = parseKeyDef;
7
+
8
+ var _utils = require("../utils");
9
+
10
+ /**
11
+ * Parse key defintions per `keyboardMap`
12
+ *
13
+ * Keys can be referenced by `{key}` or `{special}` as well as physical locations per `[code]`.
14
+ * Everything else will be interpreted as a typed character - e.g. `a`.
15
+ * Brackets `{` and `[` can be escaped by doubling - e.g. `foo[[bar` translates to `foo[bar`.
16
+ * Keeping the key pressed can be written as `{key>}`.
17
+ * When keeping the key pressed you can choose how long (how many keydown and keypress) the key is pressed `{key>3}`.
18
+ * You can then release the key per `{key>3/}` or keep it pressed and continue with the next key.
19
+ */
20
+ function parseKeyDef(keyboardMap, text) {
21
+ const defs = [];
22
+
23
+ do {
24
+ var _keyboardMap$find;
25
+
26
+ const {
27
+ type,
28
+ descriptor,
29
+ consumedLength,
30
+ releasePrevious,
31
+ releaseSelf = true,
32
+ repeat
33
+ } = (0, _utils.readNextDescriptor)(text);
34
+ const keyDef = (_keyboardMap$find = keyboardMap.find(def => {
35
+ if (type === '[') {
36
+ var _def$code;
37
+
38
+ return ((_def$code = def.code) == null ? void 0 : _def$code.toLowerCase()) === descriptor.toLowerCase();
39
+ } else if (type === '{') {
40
+ var _def$key;
41
+
42
+ return ((_def$key = def.key) == null ? void 0 : _def$key.toLowerCase()) === descriptor.toLowerCase();
43
+ }
44
+
45
+ return def.key === descriptor;
46
+ })) != null ? _keyboardMap$find : {
47
+ key: 'Unknown',
48
+ code: 'Unknown',
49
+ [type === '[' ? 'code' : 'key']: descriptor
50
+ };
51
+ defs.push({
52
+ keyDef,
53
+ releasePrevious,
54
+ releaseSelf,
55
+ repeat
56
+ });
57
+ text = text.slice(consumedLength);
58
+ } while (text);
59
+
60
+ return defs;
61
+ }
@@ -7,6 +7,8 @@ exports.keydownBehavior = void 0;
7
7
 
8
8
  var _utils = require("../../utils");
9
9
 
10
+ var _document = require("../../document");
11
+
10
12
  /**
11
13
  * This file should contain behavior for arrow keys as described here:
12
14
  * https://w3c.github.io/uievents-code/#key-arrowpad-section
@@ -15,19 +17,12 @@ const keydownBehavior = [{
15
17
  // TODO: implement for contentEditable
16
18
  matches: (keyDef, element) => (keyDef.key === 'ArrowLeft' || keyDef.key === 'ArrowRight') && (0, _utils.isElementType)(element, ['input', 'textarea']),
17
19
  handle: (keyDef, element) => {
18
- var _ref;
20
+ const selection = (0, _document.getUISelection)(element); // TODO: implement shift/ctrl
19
21
 
20
- const {
21
- selectionStart,
22
- selectionEnd
23
- } = (0, _utils.getSelectionRange)(element);
24
- const direction = keyDef.key === 'ArrowLeft' ? -1 : 1;
25
- const newPos = (_ref = selectionStart === selectionEnd ? (selectionStart != null ? selectionStart :
26
- /* istanbul ignore next */
27
- 0) + direction : direction < 0 ? selectionStart : selectionEnd) != null ? _ref :
28
- /* istanbul ignore next */
29
- 0;
30
- (0, _utils.setSelectionRange)(element, newPos, newPos);
22
+ (0, _utils.setSelection)({
23
+ focusNode: element,
24
+ focusOffset: selection.startOffset === selection.endOffset ? selection.focusOffset + (keyDef.key === 'ArrowLeft' ? -1 : 1) : keyDef.key === 'ArrowLeft' ? selection.startOffset : selection.endOffset
25
+ });
31
26
  }
32
27
  }];
33
28
  exports.keydownBehavior = keydownBehavior;
@@ -23,11 +23,13 @@ const keypressBehavior = [{
23
23
  readOnly: false
24
24
  });
25
25
  },
26
- handle: (keyDef, element, options, state) => {
27
- var _state$carryValue;
26
+ handle: (keyDef, element, {
27
+ keyboardState
28
+ }) => {
29
+ var _keyboardState$carryV;
28
30
 
29
31
  let newEntry = keyDef.key;
30
- const textToBeTyped = ((_state$carryValue = state.carryValue) != null ? _state$carryValue : '') + newEntry;
32
+ const textToBeTyped = ((_keyboardState$carryV = keyboardState.carryValue) != null ? _keyboardState$carryV : '') + newEntry;
31
33
  const timeNewEntry = (0, _utils.buildTimeValue)(textToBeTyped);
32
34
 
33
35
  if ((0, _utils.isValidInputTimeValue)(element, timeNewEntry)) {
@@ -36,15 +38,18 @@ const keypressBehavior = [{
36
38
 
37
39
  const {
38
40
  newValue,
39
- newSelectionStart
40
- } = (0, _utils.calculateNewValue)(newEntry, element);
41
+ newOffset
42
+ } = (0, _utils.calculateNewValue)(newEntry, element, (0, _utils.getInputRange)(element));
41
43
  const prevValue = (0, _utils.getValue)(element); // this check was provided by fireInputEventIfNeeded
42
44
  // TODO: verify if it is even needed by this handler
43
45
 
44
46
  if (prevValue !== newValue) {
45
- (0, _utils.fireInputEvent)(element, {
47
+ (0, _utils.editInputElement)(element, {
46
48
  newValue,
47
- newSelectionStart,
49
+ newSelection: {
50
+ node: element,
51
+ offset: newOffset
52
+ },
48
53
  eventOverrides: {
49
54
  data: keyDef.key,
50
55
  inputType: 'insertText'
@@ -53,7 +58,7 @@ const keypressBehavior = [{
53
58
  }
54
59
 
55
60
  (0, _shared.fireChangeForInputTimeIfValid)(element, prevValue, timeNewEntry);
56
- state.carryValue = textToBeTyped;
61
+ keyboardState.carryValue = textToBeTyped;
57
62
  }
58
63
  }, {
59
64
  matches: (keyDef, element) => {
@@ -64,11 +69,13 @@ const keypressBehavior = [{
64
69
  readOnly: false
65
70
  });
66
71
  },
67
- handle: (keyDef, element, options, state) => {
68
- var _state$carryValue2;
72
+ handle: (keyDef, element, {
73
+ keyboardState
74
+ }) => {
75
+ var _keyboardState$carryV2;
69
76
 
70
77
  let newEntry = keyDef.key;
71
- const textToBeTyped = ((_state$carryValue2 = state.carryValue) != null ? _state$carryValue2 : '') + newEntry;
78
+ const textToBeTyped = ((_keyboardState$carryV2 = keyboardState.carryValue) != null ? _keyboardState$carryV2 : '') + newEntry;
72
79
  const isValidToBeTyped = (0, _utils.isValidDateValue)(element, textToBeTyped);
73
80
 
74
81
  if (isValidToBeTyped) {
@@ -77,15 +84,18 @@ const keypressBehavior = [{
77
84
 
78
85
  const {
79
86
  newValue,
80
- newSelectionStart
81
- } = (0, _utils.calculateNewValue)(newEntry, element);
87
+ newOffset
88
+ } = (0, _utils.calculateNewValue)(newEntry, element, (0, _utils.getInputRange)(element));
82
89
  const prevValue = (0, _utils.getValue)(element); // this check was provided by fireInputEventIfNeeded
83
90
  // TODO: verify if it is even needed by this handler
84
91
 
85
92
  if (prevValue !== newValue) {
86
- (0, _utils.fireInputEvent)(element, {
93
+ (0, _utils.editInputElement)(element, {
87
94
  newValue,
88
- newSelectionStart,
95
+ newSelection: {
96
+ node: element,
97
+ offset: newOffset
98
+ },
89
99
  eventOverrides: {
90
100
  data: keyDef.key,
91
101
  inputType: 'insertText'
@@ -101,7 +111,7 @@ const keypressBehavior = [{
101
111
  });
102
112
  }
103
113
 
104
- state.carryValue = textToBeTyped;
114
+ keyboardState.carryValue = textToBeTyped;
105
115
  }
106
116
  }, {
107
117
  matches: (keyDef, element) => {
@@ -120,9 +130,10 @@ const keypressBehavior = [{
120
130
  }
121
131
 
122
132
  const {
123
- newValue,
124
- newSelectionStart
125
- } = (0, _utils.calculateNewValue)(keyDef.key, element); // the browser allows some invalid input but not others
133
+ getNewValue,
134
+ commit
135
+ } = (0, _utils.prepareInput)(keyDef.key, element);
136
+ const newValue = getNewValue(); // the browser allows some invalid input but not others
126
137
  // it allows up to two '-' at any place before any 'e' or one directly following 'e'
127
138
  // it allows one '.' at any place before e
128
139
 
@@ -132,54 +143,31 @@ const keypressBehavior = [{
132
143
  return;
133
144
  }
134
145
 
135
- (0, _utils.fireInputEvent)(element, {
136
- newValue,
137
- newSelectionStart,
138
- eventOverrides: {
139
- data: keyDef.key,
140
- inputType: 'insertText'
141
- }
142
- });
146
+ commit();
143
147
  }
144
148
  }, {
145
149
  matches: (keyDef, element) => {
146
150
  var _keyDef$key4;
147
151
 
148
- return ((_keyDef$key4 = keyDef.key) == null ? void 0 : _keyDef$key4.length) === 1 && ((0, _utils.isElementType)(element, ['input', 'textarea'], {
152
+ return ((_keyDef$key4 = keyDef.key) == null ? void 0 : _keyDef$key4.length) === 1 && ((0, _utils.isEditableInput)(element) || (0, _utils.isElementType)(element, 'textarea', {
149
153
  readOnly: false
150
- }) && !(0, _utils.isClickableInput)(element) || (0, _utils.isContentEditable)(element)) && (0, _utils.getSpaceUntilMaxLength)(element) !== 0;
154
+ }) || (0, _utils.isContentEditable)(element)) && (0, _utils.getSpaceUntilMaxLength)(element) !== 0;
151
155
  },
152
156
  handle: (keyDef, element) => {
153
- const {
154
- newValue,
155
- newSelectionStart
156
- } = (0, _utils.calculateNewValue)(keyDef.key, element);
157
- (0, _utils.fireInputEvent)(element, {
158
- newValue,
159
- newSelectionStart,
160
- eventOverrides: {
161
- data: keyDef.key,
162
- inputType: 'insertText'
163
- }
164
- });
157
+ var _prepareInput;
158
+
159
+ (_prepareInput = (0, _utils.prepareInput)(keyDef.key, element)) == null ? void 0 : _prepareInput.commit();
165
160
  }
166
161
  }, {
167
162
  matches: (keyDef, element) => keyDef.key === 'Enter' && ((0, _utils.isElementType)(element, 'textarea', {
168
163
  readOnly: false
169
164
  }) || (0, _utils.isContentEditable)(element)) && (0, _utils.getSpaceUntilMaxLength)(element) !== 0,
170
- handle: (keyDef, element, options, state) => {
171
- const {
172
- newValue,
173
- newSelectionStart
174
- } = (0, _utils.calculateNewValue)('\n', element);
175
- const inputType = (0, _utils.isContentEditable)(element) && !state.modifiers.shift ? 'insertParagraph' : 'insertLineBreak';
176
- (0, _utils.fireInputEvent)(element, {
177
- newValue,
178
- newSelectionStart,
179
- eventOverrides: {
180
- inputType
181
- }
182
- });
165
+ handle: (keyDef, element, {
166
+ keyboardState
167
+ }) => {
168
+ var _prepareInput2;
169
+
170
+ (_prepareInput2 = (0, _utils.prepareInput)('\n', element, (0, _utils.isContentEditable)(element) && !keyboardState.modifiers.shift ? 'insertParagraph' : 'insertLineBreak')) == null ? void 0 : _prepareInput2.commit();
183
171
  }
184
172
  }];
185
173
  exports.keypressBehavior = keypressBehavior;
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Default behavior for key combinations
3
+ */
4
+ import { behaviorPlugin } from '../types';
5
+ export declare const keydownBehavior: behaviorPlugin[];
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.keydownBehavior = void 0;
7
+
8
+ var _utils = require("../../utils");
9
+
10
+ /**
11
+ * Default behavior for key combinations
12
+ */
13
+ const keydownBehavior = [{
14
+ matches: (keyDef, element, {
15
+ keyboardState
16
+ }) => keyDef.code === 'KeyA' && keyboardState.modifiers.ctrl,
17
+ handle: (keyDef, element) => (0, _utils.selectAll)(element)
18
+ }];
19
+ exports.keydownBehavior = keydownBehavior;
@@ -42,19 +42,11 @@ const keydownBehavior = [{
42
42
  }
43
43
  }
44
44
  }, {
45
- matches: (keyDef, element) => keyDef.key === 'Delete' && (0, _utils.isEditable)(element) && !(0, _utils.isCursorAtEnd)(element),
45
+ matches: (keyDef, element) => keyDef.key === 'Delete' && (0, _utils.isEditable)(element),
46
46
  handle: (keDef, element) => {
47
- const {
48
- newValue,
49
- newSelectionStart
50
- } = (0, _utils.calculateNewValue)('', element, undefined, undefined, 'forward');
51
- (0, _utils.fireInputEvent)(element, {
52
- newValue,
53
- newSelectionStart,
54
- eventOverrides: {
55
- inputType: 'deleteContentForward'
56
- }
57
- });
47
+ var _prepareInput;
48
+
49
+ (_prepareInput = (0, _utils.prepareInput)('', element, 'deleteContentForward')) == null ? void 0 : _prepareInput.commit();
58
50
  }
59
51
  }];
60
52
  exports.keydownBehavior = keydownBehavior;