clear.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.clear = clear;
  6. var _utils = require("./utils");
  7. var _type = require("./type");
  8. function clear(element) {
  9. var _element$selectionSta, _element$selectionEnd;
  10. if (!(0, _utils.isElementType)(element, ['input', 'textarea'])) {
  11. // TODO: support contenteditable
  12. throw new Error('clear currently only supports input and textarea elements.');
  13. }
  14. if ((0, _utils.isDisabled)(element)) {
  15. return;
  16. } // 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. const elementType = element.type;
  19. if (elementType !== 'textarea') {
  20. // setSelectionRange is not supported on certain types of inputs, e.g. "number" or "email"
  21. ;
  22. element.type = 'text';
  23. }
  24. (0, _type.type)(element, '{selectall}{del}', {
  25. delay: 0,
  26. initialSelectionStart: (_element$selectionSta = element.selectionStart) != null ? _element$selectionSta :
  27. /* istanbul ignore next */
  28. undefined,
  29. initialSelectionEnd: (_element$selectionEnd = element.selectionEnd) != null ? _element$selectionEnd :
  30. /* istanbul ignore next */
  31. undefined
  32. });
  33. if (elementType !== 'textarea') {
  34. ;
  35. element.type = elementType;
  36. }
  37. }