Bug 238505

Summary: Default event target for keyboard events is different in HTML and XHTML documents
Product: WebKit Reporter: Robert Knight <robertknight>
Component: UI EventsAssignee: Nobody <webkit-unassigned>
Status: NEW ---    
Severity: Normal CC: cdumez, megan_gardner, webkit-bug-importer, wenson_hsieh
Priority: P2 Keywords: InRadar
Version: Safari 15   
Hardware: Unspecified   
OS: Unspecified   
Attachments:
Description Flags
HTML test case
none
XHTML test case none

Description Robert Knight 2022-03-29 08:20:34 PDT
Created attachment 456025 [details]
HTML test case

When no element is focused in a document, the element to which keyboard events are dispatched is different in XHTML vs HTML documents.

Steps to reproduce:

1. Open attached test.html
2. Focus the document, press some keys and note the `target` property of events logged in the browser console
3. Repeat steps 1 and 2 with attached test.xhtml

Ideally the keyboard events would be fired at the same element in both HTML and XHTML documents.

In Safari and Chrome the keyboard events are fired at the body  element for HTML documents and the document element for XHTML documents. In Firefox the keyboard events are fired at the document body for both document types. 

For XHTML documents that do not have a body element, Firefox fires events at the document element instead, which seems like reasonable fallback behavior.

We encountered this issue when investigating a bug in our web app where a keyboard shortcut handler, installed on the document body, didn't work when the code was loaded in an XHTML document.
Comment 1 Robert Knight 2022-03-29 08:20:51 PDT
Created attachment 456026 [details]
XHTML test case
Comment 2 Robert Knight 2022-03-29 08:21:31 PDT
Related Chrome issue: https://bugs.chromium.org/p/chromium/issues/detail?id=1311257#c1
Comment 3 Radar WebKit Bug Importer 2022-04-05 08:21:18 PDT
<rdar://problem/91294819>