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.
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.
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.)
<rdar://problem/89902824>
Safari TP 151 is still failing these tests: https://wpt.fyi/results/inert/dynamic-inert-on-focused-element.html?label=master&label=experimental&aligned&view=subtest&q=dynamic-inert-on-focused-element
Pull request: https://github.com/WebKit/WebKit/pull/6069
Pull request: https://github.com/WebKit/WebKit/pull/9835
Committed 260067@main (71bc5343fd7b): <https://commits.webkit.org/260067@main> Reviewed commits have been landed. Closing PR #9835 and removing active labels.