* Cross browser routine for getting selected range/cursor position. * Note: this also works with edge cases like contenteditable-enabled elements, and hidden inputs. * * @param {HTMLInputElement|EventTarget} element * @returns {{start: number, end: number, length: number}}
(element)
| 691 | * @returns {{start: number, end: number, length: number}} |
| 692 | */ |
| 693 | static getElementSelection(element) { |
| 694 | const position = {}; |
| 695 | |
| 696 | let isSelectionStartUndefined; |
| 697 | try { |
| 698 | isSelectionStartUndefined = this.isUndefined(element.selectionStart); |
| 699 | } catch (error) { |
| 700 | isSelectionStartUndefined = false; |
| 701 | } |
| 702 | |
| 703 | try { |
| 704 | if (isSelectionStartUndefined) { |
| 705 | const selection = window.getSelection(); |
| 706 | const selectionInfo = selection.getRangeAt(0); |
| 707 | position.start = selectionInfo.startOffset; |
| 708 | position.end = selectionInfo.endOffset; |
| 709 | position.length = position.end - position.start; |
| 710 | } else { |
| 711 | position.start = element.selectionStart; |
| 712 | position.end = element.selectionEnd; |
| 713 | position.length = position.end - position.start; |
| 714 | } |
| 715 | } catch (error) { |
| 716 | // Manages the cases where : |
| 717 | // - the 'contenteditable' elements that have no selections |
| 718 | // - the <input> element is of type 'hidden' |
| 719 | position.start = 0; |
| 720 | position.end = 0; |
| 721 | position.length = 0; |
| 722 | } |
| 723 | |
| 724 | return position; |
| 725 | } |
| 726 | |
| 727 | /** |
| 728 | * Cross browser routine for setting selected range/cursor position |
no test coverage detected