Lightpanda: the headless browser designed for AI and automation
Don't swallow ExecutionTerminated
Karl Seguin56cb34f184f5
diff --git a/src/browser/tests/frames/cross_realm_focus.html b/src/browser/tests/frames/cross_realm_focus.htmlindex bd379dd3c..992b03dbe 100644--- a/src/browser/tests/frames/cross_realm_focus.html+++ b/src/browser/tests/frames/cross_realm_focus.html@@ -10,6 +10,7 @@ activeElement, and — the focus chain running through navigable containers —the parent's activeElement becomes the <iframe> itself. Open in Firefox orChrome to cross-check.-->+<input id="pout"><iframe id="idf" src="support/cross_realm_focus.html"></iframe><script id="focus_targets_own_document">@@ -49,6 +50,49 @@ Chrome to cross-check.});</script>+<script id="focus_chain_crosses_frames">+ testing.onload(() => {+ const idf = document.getElementById('idf');+ const iwin = idf.contentWindow;+ const idoc = idf.contentDocument;+ const inp = idoc.getElementById('fin');+ const pout = document.getElementById('pout');++ for (const t of ['focus', 'focusin', 'blur', 'focusout']) {+ for (const el of [pout, idf]) {+ el.addEventListener(t, (e) => {+ iwin.events.push(`parent ${e.type}:${e.target.id}:${e.relatedTarget}`);+ });+ }+ }++ pout.focus();+ iwin.events.length = 0;++ // Into the iframe: the parent's element blurs first, then the inner one+ // takes focus. relatedTarget doesn't cross documents, and the <iframe>+ // itself gets no focus events (as in Chrome).+ inp.focus();+ testing.expectEqual(+ 'parent blur:pout:null,parent focusout:pout:null,focus:fin:true,focusin:fin:true',+ iwin.events.join(','));+ testing.expectEqual(idf, document.activeElement);+ testing.expectEqual(inp, idoc.activeElement);+ iwin.events.length = 0;++ // Back out: the element inside the iframe blurs, and the iframe document+ // no longer has a focused element.+ pout.focus();+ testing.expectEqual(+ 'blur:fin:true,focusout:fin:true,parent focus:pout:null,parent focusin:pout:null',+ iwin.events.join(','));+ testing.expectEqual(pout, document.activeElement);+ testing.expectEqual(idoc.body, idoc.activeElement);+ testing.expectEqual(null, idoc.querySelector('input:focus'));+ pout.blur();+ });+</script>+<script id="frameless_document_is_not_focusable">testing.onload(() => {const doc = new DOMParser().parseFromString('<input id=x>', 'text/html');diff --git a/src/browser/webapi/Element.zig b/src/browser/webapi/Element.zigindex 9a6500c30..10a4d456c 100644--- a/src/browser/webapi/Element.zig+++ b/src/browser/webapi/Element.zig@@ -1298,34 +1298,43 @@ pub fn focus(self: *Element, frame: *Frame) !void {}const owner = self.ownerFrame(frame) orelse return;- const doc = owner.document;- const old_active = doc._active_element;- if (old_active == self) {- return focusContainer(owner);+ const already_active = owner.document._active_element == self;+ if (already_active == false and self.isFocusable(owner) == false) {+ return;}- if (self.isFocusable(owner) == false) {- return;+ // The focus chain runs through navigable containers: each <iframe> holding+ // the focused document is the focused area of its parent document, so the+ // parent's activeElement is that <iframe>. Like Chrome, ancestors blur+ // their previous element first, and the <iframe> itself gets no focus events.+ // https://html.spec.whatwg.org/multipage/interaction.html#focus-chain+ var child = owner;+ while (child.iframe) |container| {+ const parent = child.parent orelse break;+ const parent_doc = parent.document;+ const old = parent_doc._active_element;+ if (old == container.asElement()) {+ break;+ }+ parent_doc.setActiveElement(container.asElement(), parent);+ if (old) |o| {+ _ = try blurFocusedArea(o, null, parent);+ }+ child = parent;}- const FocusEvent = @import("event/FocusEvent.zig");+ if (already_active) {+ return;+ }+ const doc = owner.document;+ const old_active = doc._active_element;const new_target = self.asEventTarget();doc.setActiveElement(self, owner);- if (old_active) |old| {- const old_target = old.asEventTarget();-- // Dispatch blur on old element (no bubble, composed)- const blur_event = try FocusEvent.initTrusted(comptime .wrap("blur"), .{ .composed = true, .relatedTarget = new_target }, owner);- try owner._event_manager.dispatch(old_target, blur_event.asEvent());+ const old_related: ?*EventTarget = if (old_active) |old| try blurFocusedArea(old, new_target, owner) else null;- // Dispatch focusout on old element (bubbles, composed)- const focusout_event = try FocusEvent.initTrusted(comptime .wrap("focusout"), .{ .bubbles = true, .composed = true, .relatedTarget = new_target }, owner);- try owner._event_manager.dispatch(old_target, focusout_event.asEvent());- }-- const old_related: ?*EventTarget = if (old_active) |old| old.asEventTarget() else null;+ const FocusEvent = @import("event/FocusEvent.zig");// Dispatch focus on new element (no bubble, composed)const focus_event = try FocusEvent.initTrusted(comptime .wrap("focus"), .{ .composed = true, .relatedTarget = old_related }, owner);@@ -1334,21 +1343,41 @@ pub fn focus(self: *Element, frame: *Frame) !void {// Dispatch focusin on new element (bubbles, composed)const focusin_event = try FocusEvent.initTrusted(comptime .wrap("focusin"), .{ .bubbles = true, .composed = true, .relatedTarget = old_related }, owner);try owner._event_manager.dispatch(new_target, focusin_event.asEvent());-- focusContainer(owner);}-// The focus chain runs through navigable containers: the <iframe> holding a-// document with a focused element is itself the focused area of its parent-// document, and so on up. So the parent's activeElement is that <iframe>, and-// keyboard input routed from the top-level document reaches the element.-// https://html.spec.whatwg.org/multipage/interaction.html#focus-chain-fn focusContainer(owner: *Frame) void {- const container = owner.iframe orelse return;- const parent = owner.parent orelse return;- container.asElement().focus(parent) catch |err| {- log.debug(.js, "focus container", .{ .err = err });- };+// `old` just lost focus in `frame`'s document. When it's an <iframe> holding+// the focus chain, the element focused inside it is what blurs (and its+// document's activeElement is cleared); the <iframe> itself gets no events.+// Returns the relatedTarget for the element taking focus: the blurred element,+// unless it was in another document.+fn blurFocusedArea(old: *Element, related: ?*EventTarget, frame: *Frame) !?*EventTarget {+ var el = old;+ var el_frame = frame;+ var el_related = related;+ while (el.is(Html.IFrame)) |iframe| {+ const window = iframe._window orelse break;+ const child = window._frame;+ const inner = child.document._active_element orelse return null;+ child.document.setActiveElement(null, child);+ el = inner;+ el_frame = child;+ // relatedTarget doesn't cross documents+ el_related = null;+ }+ try dispatchBlur(el, el_related, el_frame);+ return if (el_frame == frame) el.asEventTarget() else null;+}++// Dispatches blur (no bubble) then focusout (bubbles) on `old`, which just lost focus.+fn dispatchBlur(old: *Element, related: ?*EventTarget, frame: *Frame) !void {+ const FocusEvent = @import("event/FocusEvent.zig");+ const old_target = old.asEventTarget();++ const blur_event = try FocusEvent.initTrusted(comptime .wrap("blur"), .{ .composed = true, .relatedTarget = related }, frame);+ try frame._event_manager.dispatch(old_target, blur_event.asEvent());++ const focusout_event = try FocusEvent.initTrusted(comptime .wrap("focusout"), .{ .bubbles = true, .composed = true, .relatedTarget = related }, frame);+ try frame._event_manager.dispatch(old_target, focusout_event.asEvent());}pub fn blur(self: *Element, frame: *Frame) !void {@@ -1358,17 +1387,7 @@ pub fn blur(self: *Element, frame: *Frame) !void {if (doc._active_element != self) return;doc.setActiveElement(null, owner);-- const FocusEvent = @import("event/FocusEvent.zig");- const old_target = self.asEventTarget();-- // Dispatch blur (no bubble, composed)- const blur_event = try FocusEvent.initTrusted(comptime .wrap("blur"), .{ .composed = true }, owner);- try owner._event_manager.dispatch(old_target, blur_event.asEvent());-- // Dispatch focusout (bubbles, composed)- const focusout_event = try FocusEvent.initTrusted(comptime .wrap("focusout"), .{ .bubbles = true, .composed = true }, owner);- try owner._event_manager.dispatch(old_target, focusout_event.asEvent());+ _ = try blurFocusedArea(self, null, owner);}pub fn getChildren(self: *Element, frame: *Frame) !collections.NodeLive(.child_elements) {