Repository navigation
fix: reset document selection when focus moves per HTMLElement.focus() - #1344
Enesuygurs wants to merge 6 commits into
Conversation
snowystinger
left a comment
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)
})
|
Thanks for catching this and for the tests! The listener reacted to every |
A synthetic `focus` event on an element that already has focus no longer resets the document selection. Adds the tests suggested in review.
|
I tested the fiddle in Safari and FF as well I also just looked through the toolbox tests, a bunch of the new ones are failing in Chrome. There are other failures in the toolbox, I'll work on cleaning those up or annotating them later, just focus on the tests in this PR. |
|
I'm not aware of any normative spec text for this. The HTML focusing steps don't say what happens to the document selection, and the Selection API leaves the relationship between the selection and input/textarea selections open, so this is browser behaviour: Chrome and Firefox move the document selection into the text control on focus, Safari doesn't. user-event already models this in |
|
Thanks for the screenshot. The |

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