1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.isFocusable = isFocusable;
|
7 | exports.isClickable = isClickable;
|
8 | exports.getMouseEventOptions = getMouseEventOptions;
|
9 | exports.isLabelWithInternallyDisabledControl = isLabelWithInternallyDisabledControl;
|
10 | exports.getActiveElement = getActiveElement;
|
11 | exports.calculateNewValue = calculateNewValue;
|
12 | exports.setSelectionRangeIfNecessary = setSelectionRangeIfNecessary;
|
13 | exports.eventWrapper = eventWrapper;
|
14 | exports.isValidDateValue = isValidDateValue;
|
15 | exports.getValue = getValue;
|
16 | exports.getSelectionRange = getSelectionRange;
|
17 | exports.isContentEditable = isContentEditable;
|
18 | exports.FOCUSABLE_SELECTOR = void 0;
|
19 |
|
20 | var _dom = require("@testing-library/dom");
|
21 |
|
22 | function isMousePressEvent(event) {
|
23 | return event === 'mousedown' || event === 'mouseup' || event === 'click' || event === 'dblclick';
|
24 | }
|
25 |
|
26 | function invert(map) {
|
27 | const res = {};
|
28 |
|
29 | for (const key of Object.keys(map)) {
|
30 | res[map[key]] = key;
|
31 | }
|
32 |
|
33 | return res;
|
34 | }
|
35 |
|
36 |
|
37 | const BUTTONS_TO_NAMES = {
|
38 | 0: 'none',
|
39 | 1: 'primary',
|
40 | 2: 'secondary',
|
41 | 4: 'auxiliary'
|
42 | };
|
43 | const NAMES_TO_BUTTONS = invert(BUTTONS_TO_NAMES);
|
44 |
|
45 | const BUTTON_TO_NAMES = {
|
46 | 0: 'primary',
|
47 | 1: 'auxiliary',
|
48 | 2: 'secondary'
|
49 | };
|
50 | const NAMES_TO_BUTTON = invert(BUTTON_TO_NAMES);
|
51 |
|
52 | function convertMouseButtons(event, init, property, mapping) {
|
53 | if (!isMousePressEvent(event)) {
|
54 | return 0;
|
55 | }
|
56 |
|
57 | if (init[property] != null) {
|
58 | return init[property];
|
59 | }
|
60 |
|
61 | if (init.buttons != null) {
|
62 |
|
63 |
|
64 | return mapping[BUTTONS_TO_NAMES[init.buttons]] || 0;
|
65 | }
|
66 |
|
67 | if (init.button != null) {
|
68 |
|
69 |
|
70 | return mapping[BUTTON_TO_NAMES[init.button]] || 0;
|
71 | }
|
72 |
|
73 | return property != 'button' && isMousePressEvent(event) ? 1 : 0;
|
74 | }
|
75 |
|
76 | function getMouseEventOptions(event, init, clickCount = 0) {
|
77 | init = init || {};
|
78 | return { ...init,
|
79 |
|
80 | detail: event === 'mousedown' || event === 'mouseup' || event === 'click' ? 1 + clickCount : clickCount,
|
81 | buttons: convertMouseButtons(event, init, 'buttons', NAMES_TO_BUTTONS),
|
82 | button: convertMouseButtons(event, init, 'button', NAMES_TO_BUTTON)
|
83 | };
|
84 | }
|
85 |
|
86 |
|
87 |
|
88 |
|
89 | function isLabelWithInternallyDisabledControl(element) {
|
90 | var _element$control;
|
91 |
|
92 | return element.tagName === 'LABEL' && ((_element$control = element.control) == null ? void 0 : _element$control.disabled) && element.contains(element.control);
|
93 | }
|
94 |
|
95 | function getActiveElement(document) {
|
96 | const activeElement = document.activeElement;
|
97 |
|
98 | if (activeElement == null ? void 0 : activeElement.shadowRoot) {
|
99 | return getActiveElement(activeElement.shadowRoot);
|
100 | } else {
|
101 | return activeElement;
|
102 | }
|
103 | }
|
104 |
|
105 | function supportsMaxLength(element) {
|
106 | if (element.tagName === 'TEXTAREA') return true;
|
107 |
|
108 | if (element.tagName === 'INPUT') {
|
109 | const type = element.getAttribute('type');
|
110 |
|
111 | if (!type) return true;
|
112 |
|
113 | if (type.match(/email|password|search|telephone|text|url/)) return true;
|
114 | }
|
115 |
|
116 | return false;
|
117 | }
|
118 |
|
119 | function getSelectionRange(element) {
|
120 | if (isContentEditable(element)) {
|
121 | const range = element.ownerDocument.getSelection().getRangeAt(0);
|
122 | return {
|
123 | selectionStart: range.startOffset,
|
124 | selectionEnd: range.endOffset
|
125 | };
|
126 | }
|
127 |
|
128 | return {
|
129 | selectionStart: element.selectionStart,
|
130 | selectionEnd: element.selectionEnd
|
131 | };
|
132 | }
|
133 |
|
134 |
|
135 | function isContentEditable(element) {
|
136 | return element.hasAttribute('contenteditable') && (element.getAttribute('contenteditable') == 'true' || element.getAttribute('contenteditable') == '');
|
137 | }
|
138 |
|
139 | function getValue(element) {
|
140 | if (isContentEditable(element)) {
|
141 | return element.textContent;
|
142 | }
|
143 |
|
144 | return element.value;
|
145 | }
|
146 |
|
147 | function calculateNewValue(newEntry, element) {
|
148 | var _element$getAttribute;
|
149 |
|
150 | const {
|
151 | selectionStart,
|
152 | selectionEnd
|
153 | } = getSelectionRange(element);
|
154 | const value = getValue(element);
|
155 |
|
156 |
|
157 | const maxLength = Number((_element$getAttribute = element.getAttribute('maxlength')) != null ? _element$getAttribute : -1);
|
158 | let newValue, newSelectionStart;
|
159 |
|
160 | if (selectionStart === null) {
|
161 |
|
162 |
|
163 | newValue = value + newEntry;
|
164 | } else if (selectionStart === selectionEnd) {
|
165 | if (selectionStart === 0) {
|
166 |
|
167 | newValue = newEntry + value;
|
168 | } else if (selectionStart === value.length) {
|
169 |
|
170 | newValue = value + newEntry;
|
171 | } else {
|
172 |
|
173 | newValue = value.slice(0, selectionStart) + newEntry + value.slice(selectionEnd);
|
174 | }
|
175 |
|
176 | newSelectionStart = selectionStart + newEntry.length;
|
177 | } else {
|
178 |
|
179 | const firstPart = value.slice(0, selectionStart) + newEntry;
|
180 | newValue = firstPart + value.slice(selectionEnd);
|
181 | newSelectionStart = firstPart.length;
|
182 | }
|
183 |
|
184 | if (element.type === 'date' && !isValidDateValue(element, newValue)) {
|
185 | newValue = value;
|
186 | }
|
187 |
|
188 | if (!supportsMaxLength(element) || maxLength < 0) {
|
189 | return {
|
190 | newValue,
|
191 | newSelectionStart
|
192 | };
|
193 | } else {
|
194 | return {
|
195 | newValue: newValue.slice(0, maxLength),
|
196 | newSelectionStart: newSelectionStart > maxLength ? maxLength : newSelectionStart
|
197 | };
|
198 | }
|
199 | }
|
200 |
|
201 | function setSelectionRangeIfNecessary(element, newSelectionStart, newSelectionEnd) {
|
202 | const {
|
203 | selectionStart,
|
204 | selectionEnd
|
205 | } = getSelectionRange(element);
|
206 |
|
207 | if (!isContentEditable(element) && (!element.setSelectionRange || selectionStart === null)) {
|
208 |
|
209 | return;
|
210 | }
|
211 |
|
212 | if (selectionStart !== newSelectionStart || selectionEnd !== newSelectionStart) {
|
213 | if (isContentEditable(element)) {
|
214 | const range = element.ownerDocument.createRange();
|
215 | range.selectNodeContents(element);
|
216 | range.setStart(element.firstChild, newSelectionStart);
|
217 | range.setEnd(element.firstChild, newSelectionEnd);
|
218 | element.ownerDocument.getSelection().removeAllRanges();
|
219 | element.ownerDocument.getSelection().addRange(range);
|
220 | } else {
|
221 | element.setSelectionRange(newSelectionStart, newSelectionEnd);
|
222 | }
|
223 | }
|
224 | }
|
225 |
|
226 | const FOCUSABLE_SELECTOR = ['input:not([disabled])', 'button:not([disabled])', 'select:not([disabled])', 'textarea:not([disabled])', '[contenteditable=""]', '[contenteditable="true"]', 'a[href]', '[tabindex]:not([disabled])'].join(', ');
|
227 | exports.FOCUSABLE_SELECTOR = FOCUSABLE_SELECTOR;
|
228 |
|
229 | function isFocusable(element) {
|
230 | return !isLabelWithInternallyDisabledControl(element) && (element == null ? void 0 : element.matches(FOCUSABLE_SELECTOR));
|
231 | }
|
232 |
|
233 | const CLICKABLE_INPUT_TYPES = ['button', 'color', 'file', 'image', 'reset', 'submit'];
|
234 |
|
235 | function isClickable(element) {
|
236 | return element.tagName === 'BUTTON' || element instanceof element.ownerDocument.defaultView.HTMLInputElement && CLICKABLE_INPUT_TYPES.includes(element.type);
|
237 | }
|
238 |
|
239 | function eventWrapper(cb) {
|
240 | let result;
|
241 | (0, _dom.getConfig)().eventWrapper(() => {
|
242 | result = cb();
|
243 | });
|
244 | return result;
|
245 | }
|
246 |
|
247 | function isValidDateValue(element, value) {
|
248 | if (element.type !== 'date') return false;
|
249 | const clone = element.cloneNode();
|
250 | clone.value = value;
|
251 | return clone.value === value;
|
252 | } |
\ | No newline at end of file |