Bug 27019

Summary: window.getSelection().focusNode is null with onfocus handler registered
Product: WebKit Reporter: Jessie Berlin <jberlin>
Component: HTML EditingAssignee: Nobody <webkit-unassigned>
Status: NEW ---    
Severity: Normal CC: ahmad.saleem792, ap, bfulgham, jparent, leviw, mrowe, rniwa, webkit
Priority: P2    
Version: 528+ (Nightly build)   
Hardware: All   
OS: All   
Attachments:
Description Flags
Test case for window.getSelection with onfocus, onclick handlers none

Description Jessie Berlin 2009-07-06 20:00:12 PDT
Created attachment 32354 [details]
Test case for window.getSelection with onfocus, onclick handlers

When an onfocus handler is registered on a div containing another div with text, window.getSelection().focusNode reports null in said onfocus handler. If the handler is an onclick handler, it correctly reports the focusNode as a HTML div element.
Comment 1 Ryosuke Niwa 2010-10-26 22:18:27 PDT
This is due to the fact WebKit sets selection after setting the focus.
Comment 2 Ryosuke Niwa 2010-10-26 22:19:37 PDT
Ojan, Alexey:

Do you think we should change the WebKit's behavior so that we set the selection first and then focus?  This will make our behavior consistent with IE.
Comment 3 Alexey Proskuryakov 2010-10-26 22:37:14 PDT
> This will make our behavior consistent with IE.

Have you tested Firefox?

I'm not aware of any reason why such a change would be bad per se. But I don't think that we allow unfocused (inactive? I'm confused about the difference) elements/frames to be selected, because having multiple selections in a window is bad UI.
Comment 4 Ryosuke Niwa 2011-03-03 22:47:11 PST
FocusController / SelectionController is in the realm of the HTML Editing component.
Comment 5 Ahmad Saleem 2022-07-30 10:22:36 PDT
Latest test results across all browsers:

*** Safari 15.6 on macOS ***

> Clicking on 'Red' padding

window.getSelection():
window.getSelection().focusNode: null


> Clicking on 'Blue' padding

window.getSelection():
window.getSelection().focusNode: [object Text]
window.getSelection().focusNode.parentNode: [object HTMLDivElement]

*** Chrome Canary 106 *** [MATCH WITH SAFARI]

> Clicking on 'Red' padding

window.getSelection():
window.getSelection().focusNode: null


> Clicking on 'Blue' padding

window.getSelection():
window.getSelection().focusNode: [object Text]
window.getSelection().focusNode.parentNode: [object HTMLDivElement]

*** Firefox Nightly 105 ***

> Clicking on 'Red' padding

window.getSelection():
window.getSelection().focusNode: [object Text]
window.getSelection().focusNode.parentNode: [object HTMLDivElement]


> Clicking on 'Blue' padding

window.getSelection():
window.getSelection().focusNode: [object Text]
window.getSelection().focusNode.parentNode: [object HTMLDivElement]

___________

I am not sure on web-spec but just wanted to share latest results across all browsers using attached test case. Thanks!