Skip to content

Commit

Permalink
fix: track calls to HTMLInputElement.select() (#898)
Browse files Browse the repository at this point in the history
  • Loading branch information
ph-fritsche authored Apr 1, 2022
1 parent f2e8f8e commit 6d36828
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
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)
})

0 comments on commit 6d36828

Please sign in to comment.