Repository navigation
fix: reset document selection when focus moves per HTMLElement.focus() - #1344
Open
Enesuygurs wants to merge 1 commit into
Open
Enesuygurs wants to merge 1 commit into
Enesuygurs wants to merge 1 commit into
Conversation
snowystinger
left a comment
Contributor
There was a problem hiding this comment.
I think this is introducing a regression
https://jsfiddle.net/0zw6ku1d/9/
Some additional tests for consideration
test('retain own selection of an element focused per `HTMLElement.focus()`', () => {
const {
elements: [div, input],
} = setup<[HTMLDivElement, HTMLInputElement]>(
`<div>Some text</div><input value="hello world"/>`,
{focus: false},
)
input.setSelectionRange(2, 7)
setSelection({
focusNode: div.firstChild as Text,
anchorOffset: 1,
focusOffset: 8,
})
input.focus()
expect(input).toHaveFocus()
expect(input).toHaveProperty('selectionStart', 2)
expect(input).toHaveProperty('selectionEnd', 7)
})
test('do not alter document selection on `focus` event for already focused element', () => {
const {
elements: [div, input],
} = setup<[HTMLDivElement, HTMLInputElement]>(
`<div>Some text</div><input value="hello world"/>`,
)
expect(input).toHaveFocus()
setSelection({
focusNode: div.firstChild as Text,
anchorOffset: 1,
focusOffset: 8,
})
input.dispatchEvent(new FocusEvent('focus'))
expect(input).toHaveFocus()
expect(document.getSelection()?.toString()).toBe('ome tex')
})
test('do not alter own selection on `focus` event for already focused element', () => {
const {element} = setup<HTMLInputElement>(`<input value="hello world"/>`)
expect(element).toHaveFocus()
element.setSelectionRange(1, 3)
element.dispatchEvent(new FocusEvent('focus'))
expect(element).toHaveProperty('selectionStart', 1)
expect(element).toHaveProperty('selectionEnd', 3)
})
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
If an event handler moves focus into an
<input>or<textarea>withHTMLElement.focus(), the document selection is left untouched. In the repro from the issue, the user drags to select text in a<div>, and theclickhandler on that<div>focuses an input. After the click,getSelection().toString()is still"ome tex".Why
user-event already resets the document selection when focus moves into an element that has its own selection (
updateSelectionOnFocus). It only does this infocusElement(), though, which means only when user-event moves the focus itself. When the page moves focus with.focus(), nothing updates the selection. A browser clears the selection in both cases, as the recording in the issue shows: the selected text goes away as soon as the input receives focus.How
prepareDocument()now adds a capturingfocuslistener that callsupdateSelectionOnFocus()for the newly focused element. That makes programmatic focus behave like focus moved by user-event. The listener skipsfocusevents whose target is notdocument.activeElement, so a synthetic event such asfireEvent.focus()doesn't touch the selection. When focus moves throughfocusElement(), the function now runs twice, but it is idempotent.I added a test to
tests/event/selection/index.tsthat follows the repro. It fails onmain(isCollapsedisfalse) and passes with this change. It only checks that the selection is collapsed and empty, not which node holds it, so it doesn't depend on the TODO inupdateSelectionOnFocusabout where Chrome puts the caret. The full jest suite, lint and typecheck pass. I didn't run the toolbox (browser) tests locally.Fixes #1204
Checklist