Open Bug 1699567 Opened 5 years ago Updated 5 years ago

document.hasFocus() returns true during focusout event when the document is losing focus

Categories

(Core :: DOM: UI Events & Focus Handling, defect, P3)

defect

Tracking

()

People

(Reporter: xidorn, Unassigned)

References

Details

Attachments

(1 file)

Attached file testcase —

Steps to reproduce:

  1. open the attached testcase
  2. click on a to focus the contenteditable
  3. click somewhere else in the Firefox UI (to switch the focus from the document)

Expected result:

focusout (sync): false
focusout (async): false

should shows up.

Actual result:

focusout (sync): true
focusout (async): false

should shows up.

While the UI Events spec says focusout is dispatched when the element is about to lose focus, the HTML spec says when the browser context does not has system focus, document.hasFocus() should return false.

It feels to me that if the loss of focus of the element is triggered by loss of focus of the document, the document already loses the focus, and document.hasFocus() should probably return false at that point already. Not sure my interpretation is correct.

Anyway, this behavior is different from Chrome, and could cause issues on sites. (Safari doesn't seem to trigger focusout when the document is losing focus, fwiw.)

https://w3c.github.io/uievents/#focusout

This event type MUST be dispatched before the element loses focus.

So, Gecko's behavior confirms the spec, but indeed, Chrome returns false from the focusout listener...

It seems that this is related issue on Chrome side:
https://bugs.chromium.org/p/chromium/issues/detail?id=645988

Smaug, WDYH?

Oops, I forgot to request ni for the previous comment.

Flags: needinfo?(bugs)

focusin/out events have been a great mess. They used to be implemented correctly in IE, but were then incorrectly implemented in webkit/chrome.
Given that Gecko also has now the odd behavior (to be more compatible with Chrome), I guess returning false during focusout is ok.

Flags: needinfo?(bugs)

Thank you for the explanation, I think that this can be P3 unless this breaks some web apps.

Severity: -- → S3
Priority: -- → P3
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: