Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/document/selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ export function prepareSelectionInterceptor(
return {realArgs: v}
},
)

prepareInterceptor(
element,
'select',
function interceptorImpl(this: HTMLInputElement | HTMLTextAreaElement) {
this[UISelection] = {
anchorOffset: 0,
focusOffset: getUIValue(element).length,
}

return {realArgs: [] as []}
},
)
}

export function setUISelection(
Expand Down
19 changes: 19 additions & 0 deletions tests/document/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,23 @@ test('clear UI selection if selection is programmatically set', async () => {
element.selectionStart = 2
expect(getUISelection(element)).toHaveProperty('startOffset', 2)
expect(getUISelection(element)).toHaveProperty('endOffset', 2)

setUISelection(element, {anchorOffset: 1, focusOffset: 2})
element.select()
expect(getUISelection(element)).toHaveProperty('startOffset', 0)
expect(getUISelection(element)).toHaveProperty('endOffset', 3)
})

test('select input without selectionRange support', () => {
const {element} = render<HTMLInputElement>(
`<input type="number" value="123"/>`,
)

prepare(element)

setUISelection(element, {focusOffset: 1})
element.select()

expect(getUISelection(element)).toHaveProperty('startOffset', 0)
expect(getUISelection(element)).toHaveProperty('endOffset', 3)
})