Truly independent web browser
LibWeb: Defer focus events until system focus returns
Callum Law6a4d05e29225
diff --git a/Libraries/LibWeb/HTML/Focus.cpp b/Libraries/LibWeb/HTML/Focus.cppindex 92533b40c92..4ca7ccc7ce0 100644--- a/Libraries/LibWeb/HTML/Focus.cpp+++ b/Libraries/LibWeb/HTML/Focus.cpp@@ -258,7 +258,7 @@ void run_focus_update_steps(Vector<GC::Root<DOM::Node>> old_chain, Vector<GC::Ro// focused. The specification does not distinguish this from focus in the container's contents:// https://github.com/whatwg/html/issues/11873auto dispatch_explicit_container_focus_events = [&]() {- if (!container_has_explicit_focus(explicitly_focused_container))+ if (!container_has_explicit_focus(explicitly_focused_container) || !explicitly_focused_container->document().page().client().has_focus())return;GC::Ptr<DOM::EventTarget> related_focus_target;@@ -267,7 +267,7 @@ void run_focus_update_steps(Vector<GC::Root<DOM::Node>> old_chain, Vector<GC::Rofire_a_focus_event(explicitly_focused_container, related_focus_target, HTML::EventNames::focus, false);- if (!container_has_explicit_focus(explicitly_focused_container))+ if (!container_has_explicit_focus(explicitly_focused_container) || !explicitly_focused_container->document().page().client().has_focus())return;fire_a_focus_event(explicitly_focused_container, related_focus_target, HTML::EventNames::focusin, true);@@ -423,7 +423,10 @@ void run_focus_update_steps(Vector<GC::Root<DOM::Node>> old_chain, Vector<GC::Ro// focus target as the related target.// FIXME: NOTE: In some cases, e.g. if entry is an area element's shape, a scrollable region, or a viewport, no event// is fired.- if (focus_event_target) {+ // AD-HOC: Script can change the designated focus chain without system focus. Match other browsers by+ // deferring focus events until system focus returns:+ // https://w3c.github.io/uievents/#events-focusevent-doc-focus+ if (focus_event_target && entry->document().page().client().has_focus()) {auto focused_before_dispatch = currently_focused_area();fire_a_focus_event(focus_event_target, related_focus_target, HTML::EventNames::focus, false);diff --git a/Tests/LibWeb/Text/expected/HTML/system-focus-reentrant-handlers.txt b/Tests/LibWeb/Text/expected/HTML/system-focus-reentrant-handlers.txtindex 3662a3f784d..130345eea9d 100644--- a/Tests/LibWeb/Text/expected/HTML/system-focus-reentrant-handlers.txt+++ b/Tests/LibWeb/Text/expected/HTML/system-focus-reentrant-handlers.txt@@ -1,2 +1,5 @@Window blur handler restores system focus: window.blur, window.focus, original.focus, original.focusin; original input still active=true; document.hasFocus()=trueWindow focus handler redirects focus without duplicate events: window.focus, redirected.focus, redirected.focusin; redirected input active=true+Input blur handler focuses iframe without system focus: window.blur; iframe remains active=true; document.hasFocus()=false+System focus returns to iframe: iframe window.focus+Iframe focus handler loses system focus without focusin: iframe window.focus, frame.focus, iframe window.blur; document.hasFocus()=falsediff --git a/Tests/LibWeb/Text/input/HTML/system-focus-reentrant-handlers.html b/Tests/LibWeb/Text/input/HTML/system-focus-reentrant-handlers.htmlindex 7a186dc2018..e180d42615f 100644--- a/Tests/LibWeb/Text/input/HTML/system-focus-reentrant-handlers.html+++ b/Tests/LibWeb/Text/input/HTML/system-focus-reentrant-handlers.html@@ -2,14 +2,19 @@<script src="../include.js"></script><input id="original" /><input id="redirected" />+<iframe id="frame"></iframe><script>test(() => {const originalInput = document.getElementById("original");const redirectedInput = document.getElementById("redirected");+ const frame = document.getElementById("frame");const events = [];for (const type of ["focus", "focusin"])document.addEventListener(type, event => events.push(`${event.target.id}.${type}`), true);- for (const type of ["blur", "focus"]) window.addEventListener(type, () => events.push(`window.${type}`));+ for (const type of ["blur", "focus"]) {+ window.addEventListener(type, () => events.push(`window.${type}`));+ frame.contentWindow.addEventListener(type, () => events.push(`iframe window.${type}`));+ }originalInput.focus();events.length = 0;@@ -26,5 +31,24 @@println(`Window focus handler redirects focus without duplicate events: ${events.join(", ")}; redirected input active=${document.activeElement === redirectedInput}`);++ originalInput.focus();+ events.length = 0;+ originalInput.addEventListener("blur", () => frame.focus(), { once: true });+ internals.setPageFocus(false);+ println(+ `Input blur handler focuses iframe without system focus: ${events.splice(0).join(", ")}; iframe remains active=${document.activeElement === frame}; document.hasFocus()=${document.hasFocus()}`+ );+ internals.setPageFocus(true);+ println(`System focus returns to iframe: ${events.splice(0).join(", ")}`);++ originalInput.focus();+ events.length = 0;+ frame.addEventListener("focus", () => internals.setPageFocus(false), { once: true });+ frame.focus();+ println(+ `Iframe focus handler loses system focus without focusin: ${events.join(", ")}; document.hasFocus()=${document.hasFocus()}`+ );+ internals.setPageFocus(true);});</script>diff --git a/Tests/LibWebView/TestFocusWithoutSystemFocus.cpp b/Tests/LibWebView/TestFocusWithoutSystemFocus.cppindex fd0f8eec292..295056f8f3f 100644--- a/Tests/LibWebView/TestFocusWithoutSystemFocus.cpp+++ b/Tests/LibWebView/TestFocusWithoutSystemFocus.cpp@@ -99,6 +99,9 @@ TEST_CASE(focus_already_focused_area_without_system_focus)Core::EventLoop::current().spin_until([&]() { return title == "frame ready"sv; });auto focus_and_report = [&](StringView focus) {+ // Reset the page title so identical reports still notify the view.+ view->run_javascript("document.title = 'pending'"_string);+ Core::EventLoop::current().spin_until([&]() { return title == "pending"sv; });title.clear();view->run_javascript(MUST(String::formatted("focus_and_report(() => {})", focus)));Core::EventLoop::current().spin_until([&]() { return title.has_value(); });@@ -124,16 +127,16 @@ TEST_CASE(focus_already_focused_area_without_system_focus)// Without system focus, focusing the target that keeps focus fires nothing.VERIFY(focus_and_report("first.focus()"sv) == "none false"sv);- // Moving focus to another target fires its focus event once, and its handler's focus() does nothing.- VERIFY(focus_and_report("second.focus()"sv) == "second:1 false"sv);+ // Moving focus to another target updates the focused area without firing focus events.+ VERIFY(focus_and_report("second.focus()"sv) == "none false"sv);VERIFY(focus_and_report("second.focus()"sv) == "none false"sv);- // The same holds in a same-process iframe, whose window gets focus along with the element.- VERIFY(focus_and_report("frame.contentDocument.getElementById('inner').focus()"sv) == "inner:1 window:1 false"sv);+ // The same holds in a same-process iframe, without firing events on its window or element.+ VERIFY(focus_and_report("frame.contentDocument.getElementById('inner').focus()"sv) == "none false"sv);VERIFY(focus_and_report("frame.contentDocument.getElementById('inner').focus()"sv) == "none false"sv);- // And for the iframe's viewport: Moving focus to it fires its window's focus once, and focusing it again fires nothing.- VERIFY(focus_and_report("frame.contentWindow.focus()"sv) == "window:1 false"sv);+ // Moving focus to the iframe's viewport also fires no events without system focus.+ VERIFY(focus_and_report("frame.contentWindow.focus()"sv) == "none false"sv);VERIFY(focus_and_report("frame.contentWindow.focus()"sv) == "none false"sv);// A shadow host that delegates focus keeps the delegate that has focus.