Closed Bug 2026022 Opened 3 months ago Closed 1 month ago

Make `EditorEventListener` listen `focus` event on window object in the capture phase

Categories

(Core :: DOM: Editor, defect)

defect

Tracking

()

RESOLVED FIXED
153 Branch
Tracking Status
firefox-esr115 --- wontfix
firefox-esr140 --- wontfix
firefox149 --- wontfix
firefox150 --- wontfix
firefox151 --- wontfix
firefox152 --- wontfix
firefox153 --- fixed

People

(Reporter: masayuki, Assigned: masayuki)

References

(Blocks 1 open bug)

Details

(Keywords: webcompat:platform-bug)

User Story

user-impact-score:450

Attachments

(1 file)

Currently, EditorEventListener listen all events which it needs in the system event group as handling everything as default action of the events. However, focus event is defined as fired after the focus is changed. I.e., the default handler should've already run before the focus event is dispatched.

Ideally, we need a internal path to run the default action before dispatching eFocus event or an internal event which is dispatched before eFocus. However, it requires a big change but this is a web-compat issue on the major web site. Therefore, we should make EditorEventListener listen focus event on the Window object in the capture phase for now.

Why would we need a new even before focus? Just have the listener on window's parent target in default group using capturing listener.
(Now, I don't remember the order of focusin and focus events)

User Story: (updated)

Hmm, simply changing the event target which has focus/blur event listener to the window does break the initialization order. E.g., text control value is not selected automatically. I need to investigate where does what.

(In reply to Olli Pettay [:smaug][bugs@pettay.fi] from comment #1)

Why would we need a new even before focus? Just have the listener on window's parent target in default group using capturing listener.
(Now, I don't remember the order of focusin and focus events)

IIRC, the order of event listeners is the order of the addEventListener calls. I.e., if a focus event listener is added to the window before HTMLEditor is created, our focus event listener runs after the content's focus event listener or won't run due to a call of stopImmediatePropagation(). To avoid that, we need to add a trick to run our focus event listener first. Do you have an idea? Do we have an event target which runs before window's ones? Looks like yes, but I'm not sure whether it's safe.

Flags: needinfo?(smaug)

Currently, editors, HTMLInputElement and HTMLTextAreaElement
initializes its selection as a default action of focus event. I.e.,
after the propagation ends. However, UI Events defines the event [1] as:

The focus MUST be given to the element before the dispatch of this
event type.

So, we need to initialize Selection before eFocus event is
dispatched.

For TextEditor, we can use EventTarget::PreHandleEvent(). Then, for
making the editor's focus handling simpler, we can directly call
EditorBase::OnFocus() from the PreHandleEvent method.

However, HTMLEditor needs to observe all focus move in the document
because it works with focused editing host, i.e., it's shared by
all editing hosts in the document. Therefore, for now, this patch makes
EditorEventListener listen to focus event at the Window. Although,
this cannot fix focus event listeners which are added before
EditorEventListener starts listening to focus. Therefore, a new
test does not pass with this patch.

  1. https://w3c.github.io/uievents/#event-type-focus

(In reply to Masayuki Nakano [:masayuki] (he/him)(JST, +0900) from comment #2)

IIRC, the order of event listeners is the order of the addEventListener calls. I.e., if a focus event listener is added to the window before HTMLEditor is created, our focus event listener runs after the content's focus event listener or won't run due to a call of stopImmediatePropagation(). To avoid that, we need to add a trick to run our focus event listener first. Do you have an idea? Do we have an event target which runs before window's ones? Looks like yes, but I'm not sure whether it's safe.

I didn't say that we should add listener to window, but window's parent in event path.
https://searchfox.org/firefox-main/rev/187dc6baf299450446944dc90ba2239913004ba8/dom/base/nsPIDOMWindow.h#856

Flags: needinfo?(smaug)

(In reply to Olli Pettay [:smaug][bugs@pettay.fi] from comment #4)

(In reply to Masayuki Nakano [:masayuki] (he/him)(JST, +0900) from comment #2)

IIRC, the order of event listeners is the order of the addEventListener calls. I.e., if a focus event listener is added to the window before HTMLEditor is created, our focus event listener runs after the content's focus event listener or won't run due to a call of stopImmediatePropagation(). To avoid that, we need to add a trick to run our focus event listener first. Do you have an idea? Do we have an event target which runs before window's ones? Looks like yes, but I'm not sure whether it's safe.

I didn't say that we should add listener to window, but window's parent in event path.
https://searchfox.org/firefox-main/rev/187dc6baf299450446944dc90ba2239913004ba8/dom/base/nsPIDOMWindow.h#856

Thanks! It makes my patch work better!

Hmm, some tests might depend on a buggy behavior of HTMLEditor. Currently, focus event is handled by HTMLEditor and TextEditor in this order if a text control element in an editing host gets focus. Then, HTMLEditor should do nothing but handles it and change Selection temporarily. However, we should not do that and my change required to fix it because of the focus event handling order is changed.

Attachment #9566851 - Attachment description: WIP: Bug 2026022 - Make `TextEditor` and `HTMLEditor` initialize `Selection` before `focus` event is dispatched r=smaug! → Bug 2026022 - Make `TextEditor` and `HTMLEditor` initialize `Selection` before `focus` event is dispatched r=smaug!
Pushed by masayuki@d-toybox.com: https://github.com/mozilla-firefox/firefox/commit/52def4ebef0f https://hg.mozilla.org/integration/autoland/rev/ea48d7fa55a9 Make `TextEditor` and `HTMLEditor` initialize `Selection` before `focus` event is dispatched r=smaug
Status: ASSIGNED → RESOLVED
Closed: 1 month ago
Resolution: --- → FIXED
Target Milestone: --- → 153 Branch

Since nightly and release are affected, beta will likely be affected too.
For more information, please visit BugBot documentation.

The patch landed in nightly and beta is affected.
:masayuki, is this bug important enough to require an uplift?

For more information, please visit BugBot documentation.

Flags: needinfo?(masayuki)

Created web-platform-tests PR https://github.com/web-platform-tests/wpt/pull/60215 for changes under testing/web-platform/tests

Although we could uplift the patch and the follow-ups in bug 2031894 to beta from the marketing point of view. However, I'd mark this as WONTFIX in beta 152 for now to conform to the theory. Feel free to ask me again if we should fix the TikTok issue ASAP.

Flags: needinfo?(masayuki)

Upstream PR merged by moz-wptsync-bot

This change is causing problems in Thunderbird where the cursor will not move. It does not happen every where. The case that causes it is where we have a dialog with a form in it that has autofocus on the first input. You can change focus and type in other inputs but the visible cursor will never move out of that input. You can move it within the input but not to a new input. clicking in other inputs moves focus and typing works.

You can move the visual cursor by putting focus into the new input, switch to a different application, and then switch back, now the cursor is stuck in this new input until repeating the same process.

Regressions: 2043372
QA Whiteboard: [qa-triage-done-c154/b153]
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: