Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the focus fixup rule more explicit #8392

Merged
merged 2 commits into from
Jan 27, 2023
Merged

Make the focus fixup rule more explicit #8392

merged 2 commits into from
Jan 27, 2023

Conversation

domenic
Copy link
Member

@domenic domenic commented Oct 16, 2022

  • Split it into two variants: one which runs synchronously on HTML element removal, and one which runs during "update the rendering". Closes Define timing of focus fixup rule precisely when triggered by style changes. #8225.

  • After this split, only the "update the rendering" variant causes the normal focus algorithms to run, and thus blur and change events to fire.

  • Delete the confusing "somehow unfocused without another element being explicitly focused" sentence.

Fixes #3847. Fixes #6729.

(See WHATWG Working Mode: Changes for more details.)


/infrastructure.html ( diff )
/interaction.html ( diff )
/interactive-elements.html ( diff )
/webappapis.html ( diff )

@domenic domenic added topic: events topic: focus agenda+ To be discussed at a triage meeting labels Oct 16, 2022
@emilio
Copy link
Contributor

emilio commented Oct 16, 2022

Are you sure we don't want blur etc events to fire? In my implementation in Gecko we'd still fire the blur event. Also, Blink at least fires the blur event on node removal (unclear what the timing there is tho, see https://bugzilla.mozilla.org/show_bug.cgi?id=559561).

Cc @EdgarChen

@domenic
Copy link
Member Author

domenic commented Oct 17, 2022

Yeah, I think you are right, at least for the animation-frame case. I will update the patch for that case at least.

For the node removal case, I am less sure. We have several developers complaining about Chrome's behavior there, and I guess no other browsers do it currently. Also, there is no great timing at which to fire the events; synchronously firing has the same problems as mutation events, and anything else seems pretty weird. So my preference (without yet consulting with other Chrome team members) would be to avoid firing the events for removal.

@devongovett
Copy link

I would like to strongly advocate for blur events to be fired on element removal. Without this, it is nearly impossible to build a working focus trap. If the focused element is removed, focus is moved to the body with no event, so no way for JS to handle this and move focus elsewhere (e.g. an adjacent element).

My team recently commented on the Firefox bug (13 years old!) linked above advocating for this. We also opened a WebKit issue asking for the same thing, but it was closed with no explanation. Currently, Chrome is the only browser that implements this as we would expect.

At the moment, we may have to do a very hacky workaround involving a MutationObserver on the whole document. This is unfortunately quite performance intensive, as it is impossible to observe when a particular element is removed - you have to observe the whole tree and get notified of many unrelated changes. Also, this is not even entirely working because these events fire asynchronously, which can cause other downstream issues. Firing a blur event in the browser just before the focused element is removed seems like to only way to truly fix this.

@emilio
Copy link
Contributor

emilio commented Oct 17, 2022

@devongovett the problem with firing blur event on removal is timing.

We generally don't want to run sync stuff when DOM is mutated, because it can change stuff like insertion validity checks etc. So whatever we do on removal, if we decide to fire the event, needs to be very precise on when it runs, and probably won't be perfect (since the script running could move the DOM node around etc).

I wonder, why do you need a mutation observer for the whole document? Can't you just capture focus / blur and use a mutation observer for the current activeElement? Seems that should be a bit less terrible, performance-wise?

@devongovett
Copy link

The problem then is if an ancestor element of the active element is removed, we wouldn't get an event. That leads to listening on the whole document.

If firing a blur on the active element is not possible, would an alternative be to fire focus on the body? We just need some event to know that focus moved, it doesn't necessarily need to be on the removed element in our case.

@past past removed the agenda+ To be discussed at a triage meeting label Oct 27, 2022
@smfr
Copy link

smfr commented Nov 2, 2022

What was the reasoning to run the focus fixup step after rAF callbacks, but before intersection observer and resize observer callbacks?

nt1m added a commit to nt1m/WebKit that referenced this pull request Nov 3, 2022
https://bugs.webkit.org/show_bug.cgi?id=237273
rdar://89902824

Reviewed by NOBODY (OOPS!).

It's currently specced at whatwg/html#8392 as a rendering step after requestAnimationFrame callbacks.

* LayoutTests/imported/w3c/web-platform-tests/inert/dynamic-inert-on-focused-element-expected.txt:
* Source/WebCore/page/Page.cpp:
(WebCore::Page::updateRendering):
(WebCore::operator<<):
* Source/WebCore/page/Page.h:
@emilio
Copy link
Contributor

emilio commented Nov 3, 2022

@smfr @domenic I was thinking this should probably run after ResizeObserver, that's when the page is done doing changes to the page for this frame, and it avoids a potentially extra layout update.

@domenic
Copy link
Member Author

domenic commented Nov 4, 2022

Thanks! I've switched it to run after ResizeObserver.

Still unsure what to do about blur/focus/change events on sync removal. I guess our choices are:

  1. No events. (Simplest, but hurts developer use cases like @devongovett's)
  2. Sync events. (Like MutationEvents. Ick.)
  3. Queue a task to run them.
  4. Delay them until update-the-rendering timing. (Would probably involve somewhat-complicated tracking infrastructure or a custom queue, especially if we want to consolidate to handle multiple focus changes.)

Maybe (3) is the way to go?

@domenic domenic added the do not merge yet Pull request must not be merged per rationale in comment label Nov 4, 2022
@mfreed7
Copy link
Contributor

mfreed7 commented Nov 4, 2022

Thanks! I've switched it to run after ResizeObserver.

Still unsure what to do about blur/focus/change events on sync removal. I guess our choices are:

  1. No events. (Simplest, but hurts developer use cases like @devongovett's)
  2. Sync events. (Like MutationEvents. Ick.)
  3. Queue a task to run them.
  4. Delay them until update-the-rendering timing. (Would probably involve somewhat-complicated tracking infrastructure or a custom queue, especially if we want to consolidate to handle multiple focus changes.)

Maybe (3) is the way to go?

I tend to agree that we should fire events on removal. Otherwise, as @devongovett points out, developers have no great way to tell what happened. It would seem that (3) is the best - still fire the events, but do it when convenient, and not synchronously.

I support this change (to (3)), but I'm a tad worried about compat.

@emilio
Copy link
Contributor

emilio commented Nov 5, 2022

The problem with (3) is that if you fire it on the element actually blurred, it won't bubble up because it's already removed... Maybe that's ok as long as we also fire focus somewhere else (to the <body> or so?).

@smaug----
Copy link

There is also
(5) run at custom element reaction time
and
(6) run as a microtask

And since the issue is quite similar to DOM mutations, and microtasks were designed for MutationObserver, (6) feels quite reasonable to me.
blur would fire just on the removed element (and propagate through the removed ancestors), but focus should fire then somewhere else.

@domenic
Copy link
Member Author

domenic commented Nov 8, 2022

I thought about this more and I think any async version (so, including (3), (4), and (6)) are a no-go. Consider the following code:

console.assert(document.activeElement === element1);
element1.remove(); // (1)
element2.focus();  // (2)

If (1) causes blur on the removed element plus focus on document.body on some delay, then we've messed up, because document.body is not actually focused. In fact, due to the event fired at 2, the developer will actually see this confusing sequence:

  • element2 fires focus
  • (async boundary)
  • element1 fires blur
  • document.body fires focus

Given that some focus/blur events will always be sync, introducing async ones will inevitably introduce a problem like the above. So that argues for (1), (2), or (5).

Between (1) and (5), I am still unsure. Using custom element reaction timing is clever, but this would be the first time we start using it beyond custom elements. It's also pretty weird that developers can opt in to this kind of sync-ish removal event on an arbitrary element, just by making it focused. "One weird trick to make your mutation observers sync!"

I'm currently thinking that we should address @devongovett's use case via a different approach, instead of trying to make focus/blur events on removal work well. For example, he says he wants to detect that focus has changed: I think the bubbling focusin event can do that? Or, we could work on adding some variant of MutationObserver which will let you know if an ancestor is removed. (I think this is whatwg/dom#533; see especially whatwg/dom#533 (comment) by @rniwa.)

@devongovett
Copy link

devongovett commented Nov 8, 2022

Firing focus/focusin on document.body (perhaps with relatedTarget set to null) would work for my case. This makes sense IMO since document.activeElement does become document.body, it's just not observable at the moment.

There are some related issues for focusin btw. What's implemented in all browsers is not what is currently specified. Not sure if relevant here. w3c/uievents#276 w3c/uievents#88

@domenic
Copy link
Member Author

domenic commented Nov 8, 2022

I guess I didn't think through my suggestion of focusin; it has the same timing problems as focus. Observing mutations more generally seems to be a better path.

@devongovett
Copy link

If you only fire focus on the body, without a blur, synchronously just after the element has been removed, what is the timing issue? Seems like this wouldn't have the performance impact of mutation events, which must bubble. If it's fired after removal, then additional mutations by the event listener shouldn't affect it. And if it's synchronous, the above example would work as expected.

@domenic
Copy link
Member Author

domenic commented Nov 8, 2022

The issue is the same as with any synchronous mutation events: security, etc. See some discussion e.g. in whatwg/dom#533 (comment) or https://lists.w3.org/Archives/Public/public-webapps/2011JulSep/0779.html

@smaug----
Copy link

synchronously just after the element has been removed

there is no issue with that. Firing event after the DOM mutation is fine. (It is DOMNodeRemoved which is the most problematic mutation event, it happens before the mutation). Firing event after the mutation is basically custom element reaction time.

@devongovett
Copy link

Yeah I figured firing it after mutation just before yielding back to the JS caller would solve that issue.

I think an additional feature to make observing element removal easier in general would also be nice, but would be great to solve this case with focus events specifically if we can. Lots of developers probably won't think to add MutationObserver in addition to focus handlers, so it would be good if there were always a focus event whenever document.activeElement changes.

@mfreed7
Copy link
Contributor

mfreed7 commented Nov 9, 2022

If (1) causes blur on the removed element plus focus on document.body on some delay, then we've messed up, because document.body is not actually focused. In fact, due to the event fired at 2, the developer will actually see this confusing sequence:

  • element2 fires focus
  • (async boundary)
  • element1 fires blur
  • document.body fires focus

The problem here really just seems to be the new focus event on the body, right? That was only presented as an alternative, if the blur couldn't be fired on element1. If we drop that from your scenario, the sequence is:

  • element2 fires focus
  • (async boundary)
  • element1 fires blur

...and since element1 has been disconnected from the document anyway, the point is just to make sure there's any event when element1 gets blurred. Based on the presented use case, having an asynchronous blur would still work. The listener can still do focus fixup based on what it finds for document.activeElement. Right?

@domenic
Copy link
Member Author

domenic commented Nov 10, 2022

That kind of sequence still makes me quite uncomfortable. If the last event a page sees is blur, then I would expect no element to be focused. Whereas in that example, element2 is focused.

@domenic domenic added the agenda+ To be discussed at a triage meeting label Nov 10, 2022
@past past removed the agenda+ To be discussed at a triage meeting label Nov 11, 2022
* Split it into two variants: one which runs synchronously on HTML element removal, and one which runs during "update the rendering". Closes #8225.

* After this split, only the "update the rendering" variant causes the normal focus algorithms to run, and thus blur and change events to fire.

* Delete the confusing "somehow unfocused without another element being explicitly focused" sentence.

Fixes #3847. Fixes #6729.
@domenic
Copy link
Member Author

domenic commented Jan 18, 2023

In the triage meeting at #8446, we had agreement to move forward with what's currently in this PR.

Separately, we're interested in pursuing whatwg/dom#533 to make node removal observable, for the use cases mentioned here.

As such, I think this PR is ready for review. @emilio, @nt1m, do you know the latest status on web platform tests? Especially for the cases around event timing.

@domenic domenic requested a review from annevk January 18, 2023 07:12
@domenic domenic removed the do not merge yet Pull request must not be merged per rationale in comment label Jan 18, 2023
@emilio
Copy link
Contributor

emilio commented Jan 18, 2023

I have tests already reviewed in https://phabricator.services.mozilla.com/D156219

@devongovett
Copy link

Glad that node removal observation will be worked on but I still think it's pretty weird that there is a case where document.activeElement changes but there are no focus or blur events fired. That seems pretty counterintuitive.

@domenic
Copy link
Member Author

domenic commented Jan 19, 2023

I have tests already reviewed in https://phabricator.services.mozilla.com/D156219

However, they say "Don't check precise timing while we wait for more concrete spec edits." Gentle ping to update them with more precise timing checks :)

@emilio
Copy link
Contributor

emilio commented Jan 19, 2023

However, they say "Don't check precise timing while we wait for more concrete spec edits." Gentle ping to update them with more precise timing checks :)

Done, that also caught a bug in Gecko of course ;)

Copy link
Member

@annevk annevk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This generally looks good to me editorially. Left some minor inline nits.

@@ -77074,8 +77061,9 @@ partial interface <span id="NavigatorUserActivation-partial">Navigator</span> {
named <code data-x="event-blur">blur</code> at <var>blur event target</var>, with
<var>related blur target</var> as the related target.</p>

<p class="note">In some cases, e.g. if <var>entry</var> is an <code>area</code>
element's shape, a scrollable region, or a <span>viewport</span>, no event is fired.</p>
<p class="note" id="note-sometimes-no-blur-event">In some cases, e.g. if <var>entry</var> is
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<p class="note" id="note-sometimes-no-blur-event">In some cases, e.g. if <var>entry</var> is
<p class="note" id="note-sometimes-no-blur-event">In some cases, e.g., if <var>entry</var> is

<p>For each <span>fully active</span> <code>Document</code> in <var>docs</var>, if the <span
data-x="focused area of the document">focused area</span> of that <code>Document</code> is no
longer a <span>focusable area</span>, then run the <span>focusing steps</span> for that
<code>Document</code>'s <span>viewport</span>.</p>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is "is no longer" important or can it be "is not a"?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it can be a bit confusing for the reader to see "focused area ... is not a focusable area", so "no longer" makes it a bit clearer what's going on.

But, it can also be confusing for the reader to think "no longer" might involve some sort of extra state-tracking, compared to "is not".

On balance, the latter seems worse, so I'll change it as you suggest.

@domenic domenic merged commit e233336 into main Jan 27, 2023
@domenic domenic deleted the focus-fixup-rule branch January 27, 2023 01:18
webkit-commit-queue pushed a commit to rniwa/WebKit that referenced this pull request Feb 9, 2023
https://bugs.webkit.org/show_bug.cgi?id=237273

Reviewed by Tim Nguyen.

Implement step 15 of update the rendering between 14. servicing resize observers and 16. updating intersection observations:
whatwg/html#8392

* LayoutTests/imported/w3c/web-platform-tests/css/css-contain/container-queries/layout-dependent-focus-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/css/selectors/focus-display-none-001-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/css/selectors/focus-within-display-none-001-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/inert/dynamic-inert-on-focused-element-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/shadow-dom/focus/focus-shadowhost-display-none-expected.txt:
* Source/WebCore/page/Page.cpp:
(WebCore::Page::updateRendering):
(WebCore::operator<<):
* Source/WebCore/page/Page.h:

Canonical link: https://commits.webkit.org/260067@main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
8 participants