MCPcopy Create free account
hub / github.com/testing-library/user-event / getSelectionRange

Function getSelectionRange

src/utils/edit/selectionRange.ts:34–72  ·  view source on GitHub ↗
(
  element: Element,
)

Source from the content-addressed store, hash-verified

32}
33
34export 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
74export function setSelectionRange(
75 element: Element,

Callers 8

isCursorAtEndFunction · 0.90
isCursorAtStartFunction · 0.90
calculateNewValueFunction · 0.90
typeImplementationFunction · 0.90
selectionRange.tsFile · 0.90
arrow.tsFile · 0.90
setSelectionRangeFunction · 0.85

Calls 2

isElementTypeFunction · 0.90
hasSelectionSupportFunction · 0.85

Tested by

no test coverage detected