( element: Element, )
| 32 | } |
| 33 | |
| 34 | export function getSelectionRange( |
| 35 | element: Element, |
| 36 | ): { |
| 37 | selectionStart: number | null |
| 38 | selectionEnd: number | null |
| 39 | } { |
| 40 | if (hasSelectionSupport(element)) { |
| 41 | return { |
| 42 | selectionStart: element.selectionStart, |
| 43 | selectionEnd: element.selectionEnd, |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | if (isElementType(element, 'input')) { |
| 48 | return ( |
| 49 | (element as InputWithInternalSelection)[InputSelection] ?? { |
| 50 | selectionStart: null, |
| 51 | selectionEnd: null, |
| 52 | } |
| 53 | ) |
| 54 | } |
| 55 | |
| 56 | const selection = element.ownerDocument.getSelection() |
| 57 | |
| 58 | // there should be no editing if the focusNode is outside of element |
| 59 | // TODO: properly handle selection ranges |
| 60 | if (selection?.rangeCount && element.contains(selection.focusNode)) { |
| 61 | const range = selection.getRangeAt(0) |
| 62 | return { |
| 63 | selectionStart: range.startOffset, |
| 64 | selectionEnd: range.endOffset, |
| 65 | } |
| 66 | } else { |
| 67 | return { |
| 68 | selectionStart: null, |
| 69 | selectionEnd: null, |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | export function setSelectionRange( |
| 75 | element: Element, |
no test coverage detected