Bug 27019 - window.getSelection().focusNode is null with onfocus handler registered
Summary: window.getSelection().focusNode is null with onfocus handler registered
Status: NEW
Alias: None
Product: WebKit
Classification: Unclassified
Component: HTML Editing (show other bugs)
Version: 528+ (Nightly build)
Hardware: All All
: P2 Normal
Assignee: Nobody
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-07-06 20:00 PDT by Jessie Berlin
Modified: 2022-07-30 10:22 PDT (History)
8 users (show)

See Also:


Attachments
Test case for window.getSelection with onfocus, onclick handlers (2.27 KB, text/html)
2009-07-06 20:00 PDT, Jessie Berlin
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
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!