Bug 237273 - Implement focus fixup rule
Summary: Implement focus fixup rule
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: DOM (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Ryosuke Niwa
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2022-02-28 06:20 PST by Tim Nguyen (:ntim)
Modified: 2023-02-09 09:46 PST (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Tim Nguyen (:ntim) 2022-02-28 06:20:53 PST
https://html.spec.whatwg.org/multipage/interaction.html#focus-fixup-rule

Focus fixup rule: When the designated focused area of the document is removed from that Document in some way (e.g. it stops being a focusable area, it is removed from the DOM, it becomes inert, etc.), designate the Document's viewport to be the new focused area of the document.
Comment 1 Tim Nguyen (:ntim) 2022-02-28 06:38:28 PST
Adding this seems to work:

```
        if (auto* activeElement = document.activeElement()) {
            if (!activeElement->isFocusable())
                document.setFocusedElement(nullptr);
        }
```

I put it in the RenderingUpdateStep::FlushAutofocusCandidates block, but that's probably not the right place (not sure which one is?).

One thing that's broken is interaction with shadow DOM: shadow-dom/focus/focus-pseudo-on-shadow-host-1.html fails. This is precisely because activeElement->isFocusable() doesn't take in account contents of the shadow DOM, and wrongly clears focus in this case. Though I expect that's not too hard to fix.
Comment 2 Tim Nguyen (:ntim) 2022-02-28 06:40:04 PST
imported/w3c/web-platform-tests/inert/dynamic-inert-on-focused-element.tentative.html tests this functionality.

I'm actually a bit surprised there aren't more tests (e.g. moving out of a focusable area, disabled attribute, etc.)
Comment 3 Radar WebKit Bug Importer 2022-03-07 06:21:13 PST
<rdar://problem/89902824>
Comment 5 Tim Nguyen (:ntim) 2022-11-02 18:51:55 PDT
Pull request: https://github.com/WebKit/WebKit/pull/6069
Comment 6 Ryosuke Niwa 2023-02-08 13:35:55 PST
Pull request: https://github.com/WebKit/WebKit/pull/9835
Comment 7 EWS 2023-02-09 09:46:51 PST
Committed 260067@main (71bc5343fd7b): <https://commits.webkit.org/260067@main>

Reviewed commits have been landed. Closing PR #9835 and removing active labels.