Truly independent web browser
LibWeb: Only fire iframe focus events for explicit focus
Callum Law642de23dcffb
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cppindex 7932135a222..129214de22e 100644--- a/Libraries/LibWeb/DOM/Document.cpp+++ b/Libraries/LibWeb/DOM/Document.cpp@@ -4245,6 +4245,7 @@ void Document::set_focused_area(GC::Ptr<Node> node, InvalidateFocusPseudoClasses}m_focused_area = node;+ m_focused_container_has_explicit_focus = false;publish_focused_text_control_nodes(old_focused_area);publish_focused_text_control_nodes(node);set_needs_highlight_style_update(CSS::PseudoElement::Selection);diff --git a/Libraries/LibWeb/DOM/Document.h b/Libraries/LibWeb/DOM/Document.hindex a24f9662fdf..1d5219a8692 100644--- a/Libraries/LibWeb/DOM/Document.h+++ b/Libraries/LibWeb/DOM/Document.h@@ -800,6 +800,9 @@ public:};void set_focused_area(GC::Ptr<Node>, InvalidateFocusPseudoClasses = InvalidateFocusPseudoClasses::Yes);+ bool focused_container_has_explicit_focus() const { return m_focused_container_has_explicit_focus; }+ void set_focused_container_has_explicit_focus(bool value) { m_focused_container_has_explicit_focus = value; }+// https://html.spec.whatwg.org/multipage/interaction.html#sequential-focus-navigation-starting-pointGC::Ptr<Node> sequential_focus_navigation_starting_point() { return m_sequential_focus_navigation_starting_point; }void move_sequential_focus_navigation_starting_point_to(GC::Ptr<Node> node) { m_sequential_focus_navigation_starting_point = node; }@@ -2010,6 +2013,7 @@ private:// https://html.spec.whatwg.org/multipage/interaction.html#focused-area-of-the-documentGC::Ptr<Node> m_focused_area;+ bool m_focused_container_has_explicit_focus { false };// https://html.spec.whatwg.org/multipage/interaction.html#sequential-focus-navigation-starting-pointGC::Ptr<Node> m_sequential_focus_navigation_starting_point;diff --git a/Libraries/LibWeb/HTML/Focus.cpp b/Libraries/LibWeb/HTML/Focus.cppindex 3ddc234c62f..be4d6b0d834 100644--- a/Libraries/LibWeb/HTML/Focus.cpp+++ b/Libraries/LibWeb/HTML/Focus.cpp@@ -128,8 +128,15 @@ static bool a_handler_may_read_style_during_blur(DOM::Element& element)return listens(element.document()) || listens(relevant_window(element));}+static bool container_has_explicit_focus(GC::Ptr<NavigableContainer> container)+{+ return container+ && container->document().focused_area() == container+ && container->document().focused_container_has_explicit_focus();+}+// https://html.spec.whatwg.org/multipage/interaction.html#focus-update-steps-void run_focus_update_steps(Vector<GC::Root<DOM::Node>> old_chain, Vector<GC::Root<DOM::Node>> new_chain, GC::Ptr<DOM::Node> new_focus_target)+void run_focus_update_steps(Vector<GC::Root<DOM::Node>> old_chain, Vector<GC::Root<DOM::Node>> new_chain, GC::Ptr<DOM::Node> new_focus_target, GC::Ptr<NavigableContainer> explicitly_focused_container){// The focus update steps, given an old chain, a new chain, and a new focus target respectively, are as follows:@@ -217,6 +224,25 @@ void run_focus_update_steps(Vector<GC::Root<DOM::Node>> old_chain, Vector<GC::Roreturn nullptr;};+ // AD-HOC: Chromium and WebKit fire element focus/blur events on a navigable container only when it was itself+ // focused. The specification does not distinguish this from focus in the container's contents:+ // https://github.com/whatwg/html/issues/11873+ auto dispatch_explicit_container_focus_events = [&]() {+ if (!container_has_explicit_focus(explicitly_focused_container))+ return;++ GC::Ptr<DOM::EventTarget> related_focus_target;+ if (!new_chain.is_empty() && new_chain.last().ptr() == explicitly_focused_container.ptr() && !old_chain.is_empty() && is<DOM::Element>(*old_chain.last()))+ related_focus_target = old_chain.last();++ fire_a_focus_event(explicitly_focused_container, related_focus_target, HTML::EventNames::focus, false);++ if (!container_has_explicit_focus(explicitly_focused_container))+ return;++ fire_a_focus_event(explicitly_focused_container, related_focus_target, HTML::EventNames::focusin, true);+ };+// 2. For each entry entry in old chain, in order, run these substeps:for (auto& entry : old_chain) {// 1. If entry is an input element@@ -242,7 +268,10 @@ void run_focus_update_steps(Vector<GC::Root<DOM::Node>> old_chain, Vector<GC::Ro// If entry is a Document object, let blur event target be that Document object's relevant global object.// Otherwise, let blur event target be null.GC::Ptr<DOM::EventTarget> blur_event_target;- if (is<DOM::Element>(*entry))+ if (is<NavigableContainer>(*entry) && !entry->document().focused_container_has_explicit_focus()) {+ // AD-HOC: Suppress events for containers designated only by descendant focus.+ // https://github.com/whatwg/html/issues/11873+ } else if (is<DOM::Element>(*entry))blur_event_target = entry;else if (is<DOM::Document>(*entry))blur_event_target = &relevant_window(*entry);@@ -307,6 +336,11 @@ void run_focus_update_steps(Vector<GC::Root<DOM::Node>> old_chain, Vector<GC::Roif (new_focus_target_is_document_viewport)designate_document_viewport_as_focused_area(as<DOM::Document>(*new_focus_target));+ // NB: The container may have been removed with the common chain tail, e.g. after contentWindow.focus(). Record+ // explicit focus before content Window handlers run, and dispatch its events after them.+ if (explicitly_focused_container && explicitly_focused_container->document().focused_area() == explicitly_focused_container)+ explicitly_focused_container->document().set_focused_container_has_explicit_focus(true);+// 4. For each entry entry in new chain, in reverse order, run these substeps:for (auto& entry : new_chain.in_reverse()) {// 1. If entry is a focusable area, and the focused area of the document is not entry:@@ -326,6 +360,9 @@ void run_focus_update_steps(Vector<GC::Root<DOM::Node>> old_chain, Vector<GC::Ro}}+ if (entry.ptr() == explicitly_focused_container.ptr() && entry->document().focused_area().ptr() == entry.ptr())+ entry->document().set_focused_container_has_explicit_focus(true);+// 2. If entry is an element, let focus event target be entry.// If entry is a Document object, let focus event target be that Document object's relevant global object.// Otherwise, let focus event target be null.@@ -334,6 +371,8 @@ void run_focus_update_steps(Vector<GC::Root<DOM::Node>> old_chain, Vector<GC::Ro// The viewport does not fire a focus event. The Document object is only its surrogate.} else if (is<DOM::Document>(*entry)) {focus_event_target = &relevant_window(*entry);+ } else if (is<NavigableContainer>(*entry)) {+ // Container element events are dispatched after this loop.} else if (is<DOM::Element>(*entry)) {focus_event_target = *entry;}@@ -364,19 +403,24 @@ void run_focus_update_steps(Vector<GC::Root<DOM::Node>> old_chain, Vector<GC::Ro// listener that focus arrived here from there, when it went the other way. The spec's steps// don't account for a handler moving focus at all:// https://github.com/whatwg/html/issues/12329- if (currently_focused_area() != focused_before_dispatch)+ if (currently_focused_area() != focused_before_dispatch) {+ dispatch_explicit_container_focus_events();return;+ }// AD-HOC: dispatch focusinfire_a_focus_event(focus_event_target, related_focus_target, HTML::EventNames::focusin, true);// AD-HOC: A handler moved focus; the nested update owns the designation. See above.- if (currently_focused_area() != focused_before_dispatch)+ if (currently_focused_area() != focused_before_dispatch) {+ dispatch_explicit_container_focus_events();return;+ }}}finish_focus_transition(new_focus_target);+ dispatch_explicit_container_focus_events();}// https://html.spec.whatwg.org/multipage/interaction.html#focus-chain@@ -696,10 +740,12 @@ void run_focusing_steps(GC::Ptr<DOM::Node> new_focus_target, GC::Ptr<DOM::Node>return;}+ GC::Ptr<NavigableContainer> explicitly_focused_container = as_if<NavigableContainer>(new_focus_target.ptr());+// 3. If new focus target is a navigable container with non-null content navigable, then set new focus target to the content navigable's active document.GC::Ptr<RemoteNavigable> remote_content_navigable;- if (auto* navigable_container = as_if<NavigableContainer>(*new_focus_target)) {- if (auto content_navigable = navigable_container->content_navigable()) {+ if (explicitly_focused_container) {+ if (auto content_navigable = explicitly_focused_container->content_navigable()) {// NB: The active document of a navigable another process hosts is there, and that process runs these steps// for it. The container stands for it in the rest of these steps, as the focused area of its node// document.@@ -714,6 +760,14 @@ void run_focusing_steps(GC::Ptr<DOM::Node> new_focus_target, GC::Ptr<DOM::Node>if (is_inert_for_focus(*new_focus_target))return;+ // AD-HOC: Match Chromium and WebKit: refocusing the container preserves descendant focus.+ // https://github.com/whatwg/html/issues/11873+ if (container_has_explicit_focus(explicitly_focused_container)) {+ if (focus_visible.has_value())+ explicitly_focused_container->document().request_focus_visibility_for_focused_element(*focus_visible);+ return;+ }+if (remote_content_navigable)new_focus_target->document().page().client().request_focusing_steps_for_remote_navigable(*remote_content_navigable, focus_trigger);@@ -725,7 +779,7 @@ void run_focusing_steps(GC::Ptr<DOM::Node> new_focus_target, GC::Ptr<DOM::Node>// check never matches. Focusing the area that keeps focus then fires the focus events again, and a page// whose focus handler focuses its own target again recurses until the stack runs out. Compare against the// area that would be focused with system focus.- if (new_focus_target.ptr() == currently_or_otherwise_focused_area(*root).ptr()) {+ if (new_focus_target.ptr() == currently_or_otherwise_focused_area(*root).ptr() && !explicitly_focused_container) {// NB: focus()'s focusVisible option still applies to the element that already has focus, e.g. the delegate a// shadow host resolved to.if (focus_visible.has_value())@@ -752,7 +806,7 @@ void run_focusing_steps(GC::Ptr<DOM::Node> new_focus_target, GC::Ptr<DOM::Node>navigable->page().set_focused_navigable(*navigable);// 8. Run the focus update steps with old chain, new chain, and new focus target respectively.- run_focus_update_steps(move(old_chain), move(new_chain), new_focus_target);+ run_focus_update_steps(move(old_chain), move(new_chain), new_focus_target, explicitly_focused_container);// INTEROP: Keyboard input follows the deepest focused navigable. Focus event handlers can move focus reentrantly,// so derive the input target from the final focused area instead of the target which began this update.diff --git a/Libraries/LibWeb/HTML/Focus.h b/Libraries/LibWeb/HTML/Focus.hindex b32645659e3..6205d9d6422 100644--- a/Libraries/LibWeb/HTML/Focus.h+++ b/Libraries/LibWeb/HTML/Focus.h@@ -22,7 +22,7 @@ enum class ScrollIntoView : u8 {};WEB_API Vector<GC::Root<DOM::Node>> focus_chain(GC::Ptr<DOM::Node> subject);-WEB_API void run_focus_update_steps(Vector<GC::Root<DOM::Node>> old_chain, Vector<GC::Root<DOM::Node>> new_chain, GC::Ptr<DOM::Node> new_focus_target);+WEB_API void run_focus_update_steps(Vector<GC::Root<DOM::Node>> old_chain, Vector<GC::Root<DOM::Node>> new_chain, GC::Ptr<DOM::Node> new_focus_target, GC::Ptr<NavigableContainer> explicitly_focused_container = nullptr);WEB_API void run_focusing_steps(GC::Ptr<DOM::Node> new_focus_target, GC::Ptr<DOM::Node> fallback_target = nullptr, FocusTrigger focus_trigger = FocusTrigger::Other, ScrollIntoView = ScrollIntoView::Yes, Optional<bool> focus_visible = {});WEB_API void run_unfocusing_steps(GC::Ptr<DOM::Node> old_focus_target);GC::Ptr<DOM::Node> focus_delegate(DOM::Node& focus_target, FocusTrigger = FocusTrigger::Other);diff --git a/Tests/LibWeb/Text/expected/HTML/iframe-descendant-focus-events.txt b/Tests/LibWeb/Text/expected/HTML/iframe-descendant-focus-events.txtnew file mode 100644index 00000000000..1775b3bbaaf--- /dev/null+++ b/Tests/LibWeb/Text/expected/HTML/iframe-descendant-focus-events.txt@@ -0,0 +1,19 @@+Nested input -> top body: iframe events=none+ #top-body active in its document: true+Focus outer window: iframe events=none+Outer window -> explicit outer iframe: iframe events=outer.focus, outer.focusin+Explicit outer iframe -> top body: iframe events=outer.blur, outer.focusout+Focus inner iframe: iframe events=inner.focus, inner.focusin+Explicit inner iframe -> middle body: iframe events=inner.blur, inner.focusout+iframe element focus handler -> nested input: iframe events=outer.focus, outer.focusin+ #input active in its document: true+Refocus after iframe element handler: iframe events=none+ #input active in its document: true+Leave iframe after iframe element handler: iframe events=outer.blur, outer.focusout+content window focus handler -> nested input: iframe events=outer.focus, outer.focusin+ #input active in its document: true+Refocus after content window handler: iframe events=none+ #input active in its document: true+Leave iframe after content window handler: iframe events=outer.blur, outer.focusout+Content window focus handler -> top button: iframe events=outer.blur, outer.focusout+ #button active in its document: truediff --git a/Tests/LibWeb/Text/input/HTML/iframe-descendant-focus-events.html b/Tests/LibWeb/Text/input/HTML/iframe-descendant-focus-events.htmlnew file mode 100644index 00000000000..a8ed2d39297--- /dev/null+++ b/Tests/LibWeb/Text/input/HTML/iframe-descendant-focus-events.html@@ -0,0 +1,55 @@+<!doctype html>+<script src="../include.js"></script>+<body id="top-body" tabindex="-1">+ <iframe id="outer" srcdoc="<body tabindex=-1><iframe id=inner srcdoc='<input id=input>'></iframe>"></iframe>+ <button id="button"></button>+ <script>+ promiseTest(async () => {+ await new Promise(resolve => window.addEventListener("load", resolve, { once: true }));++ const outerFrame = document.getElementById("outer");+ const middleDocument = outerFrame.contentDocument;+ const innerFrame = middleDocument.querySelector("iframe");+ const innerInput = innerFrame.contentDocument.querySelector("input");+ const events = [];+ for (const frame of [outerFrame, innerFrame]) {+ for (const type of ["focus", "focusin", "blur", "focusout"])+ frame.addEventListener(type, () => events.push(`${frame.id}.${type}`));+ }++ function focusAndReport(target, label, expectedActiveElement) {+ target.focus();+ println(`${label}: iframe events=${events.join(", ") || "none"}`);+ if (expectedActiveElement)+ println(+ ` #${expectedActiveElement.id} active in its document: ${expectedActiveElement.ownerDocument.activeElement === expectedActiveElement}`+ );+ events.length = 0;+ }++ innerInput.focus();+ focusAndReport(document.body, "Nested input -> top body", document.body);++ focusAndReport(outerFrame.contentWindow, "Focus outer window");+ focusAndReport(outerFrame, "Outer window -> explicit outer iframe");+ focusAndReport(document.body, "Explicit outer iframe -> top body");++ focusAndReport(innerFrame, "Focus inner iframe");+ focusAndReport(middleDocument.body, "Explicit inner iframe -> middle body");++ for (const [name, target] of [+ ["iframe element", outerFrame],+ ["content window", outerFrame.contentWindow],+ ]) {+ target.addEventListener("focus", () => innerInput.focus(), { once: true });+ focusAndReport(outerFrame, `${name} focus handler -> nested input`, innerInput);+ focusAndReport(outerFrame, `Refocus after ${name} handler`, innerInput);+ focusAndReport(document.body, `Leave iframe after ${name} handler`);+ }++ const topButton = document.getElementById("button");+ outerFrame.contentWindow.addEventListener("focus", () => topButton.focus(), { once: true });+ focusAndReport(outerFrame, "Content window focus handler -> top button", topButton);+ });+ </script>+</body>