updateSelectionOnFocus.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. 'use strict';
  2. require('../../utils/dataTransfer/Clipboard.js');
  3. var isContentEditable = require('../../utils/edit/isContentEditable.js');
  4. var selection = require('../../utils/focus/selection.js');
  5. // The browser implementation seems to have changed.
  6. // When focus is inside <input type="text"/>,
  7. // Chrome updates Selection to be collapsed at the position of the input element.
  8. // TODO: update implementation to match that of current browsers
  9. /**
  10. * Reset the Document Selection when moving focus into an element
  11. * with own selection implementation.
  12. */ function updateSelectionOnFocus(element) {
  13. const selection$1 = element.ownerDocument.getSelection();
  14. /* istanbul ignore if */ if (!(selection$1 === null || selection$1 === undefined ? undefined : selection$1.focusNode)) {
  15. return;
  16. }
  17. // If the focus moves inside an element with own selection implementation,
  18. // the document selection will be this element.
  19. // But if the focused element is inside a contenteditable,
  20. // 1) a collapsed selection will be retained.
  21. // 2) other selections will be replaced by a cursor
  22. // 2.a) at the start of the first child if it is a text node
  23. // 2.b) at the start of the contenteditable.
  24. if (selection.hasOwnSelection(element)) {
  25. const contenteditable = isContentEditable.getContentEditable(selection$1.focusNode);
  26. if (contenteditable) {
  27. if (!selection$1.isCollapsed) {
  28. var _contenteditable_firstChild;
  29. const focusNode = ((_contenteditable_firstChild = contenteditable.firstChild) === null || _contenteditable_firstChild === undefined ? undefined : _contenteditable_firstChild.nodeType) === 3 ? contenteditable.firstChild : contenteditable;
  30. selection$1.setBaseAndExtent(focusNode, 0, focusNode, 0);
  31. }
  32. } else {
  33. selection$1.setBaseAndExtent(element, 0, element, 0);
  34. }
  35. }
  36. }
  37. exports.updateSelectionOnFocus = updateSelectionOnFocus;