clear.js 1.2 KB

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