UNPKG

1.19 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.clear = clear;
7
8var _type = require("./type");
9
10function clear(element) {
11 if (element.tagName !== 'INPUT' && element.tagName !== 'TEXTAREA') {
12 // TODO: support contenteditable
13 throw new Error('clear currently only supports input and textarea elements.');
14 }
15
16 if (element.disabled) return; // TODO: track the selection range ourselves so we don't have to do this input "type" trickery
17 // just like cypress does: https://github.com/cypress-io/cypress/blob/8d7f1a0bedc3c45a2ebf1ff50324b34129fdc683/packages/driver/src/dom/selection.ts#L16-L37
18
19 const elementType = element.type; // type is a readonly property on textarea, so check if element is an input before trying to modify it
20
21 if (element.tagName === 'INPUT') {
22 // setSelectionRange is not supported on certain types of inputs, e.g. "number" or "email"
23 element.type = 'text';
24 }
25
26 (0, _type.type)(element, '{selectall}{del}', {
27 delay: 0,
28 initialSelectionStart: element.selectionStart,
29 initialSelectionEnd: element.selectionEnd
30 });
31
32 if (element.tagName === 'INPUT') {
33 element.type = elementType;
34 }
35}
\No newline at end of file