Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: track calls to HTMLInputElement.select() #898

Merged
merged 1 commit into from Apr 1, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/document/selection.ts
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
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)
})