Lightpanda: the headless browser designed for AI and automation
input: drop redundant focus() in click activation
Adrià Arrufat2a7cf5c3739a
diff --git a/src/browser/frame/user_input.zig b/src/browser/frame/user_input.zigindex 1d17412bd..c1ed0e1df 100644--- a/src/browser/frame/user_input.zig+++ b/src/browser/frame/user_input.zig@@ -699,7 +699,6 @@ pub fn handleClick(frame: *Frame, target: *Node, event_target: *Node) !void {},.input => {const input = html_element.subtype(Element.Html.Input);- try element.focus(frame);// Per HTML §4.10.18.6.4 "Image Button state (type=image)", clicking an// image button submits its form. The form-data set already gets the// submitter's coordinate fields appended via FormData.collectForm@@ -710,12 +709,11 @@ pub fn handleClick(frame: *Frame, target: *Node, event_target: *Node) !void {},.button => {const button = html_element.subtype(Element.Html.Button);- try element.focus(frame);if (std.mem.eql(u8, button.getType(), "submit")) {return frame.submitForm(element, button.getForm(frame), .{});}},- .select, .textarea => try element.focus(frame),+ .select, .textarea => {},.label => {const label = html_element.subtype(Element.Html.Label);// Per HTML §4.10.4 "The label element", a label's activation@@ -728,6 +726,7 @@ pub fn handleClick(frame: *Frame, target: *Node, event_target: *Node) !void {// label (into an infinite loop)return;}+ try control.focus(frame);const control_html = control.is(Element.Html) orelse return;try control_html.click(frame);},@@ -778,14 +777,12 @@ fn followLink(frame: *Frame, target: *Node, element: *Element, href: []const u8,break :blk switch (frame.resolveTargetFrame(target_name)) {.frame => |f| f,.blank => {- try element.focus(frame);_ = try (target.ownerFrame(frame) orelse return).openBlankTarget(element, href);return;},};};- try element.focus(frame);try frame.scheduleNavigation(href, .{.reason = .script,.kind = .{ .push = null },diff --git a/src/browser/tests/document/focus.html b/src/browser/tests/document/focus.htmlindex 474b5de82..5f077ff3c 100644--- a/src/browser/tests/document/focus.html+++ b/src/browser/tests/document/focus.html@@ -255,37 +255,25 @@}</script>-<script id="click_focuses_element">+<script id="click_does_not_focus_element">{const input1 = $('#input1');- const input2 = $('#input2');if (document.activeElement) {document.activeElement.blur();}let focusCount = 0;- let blurCount = 0;-input1.addEventListener('focus', () => focusCount++);- input1.addEventListener('blur', () => blurCount++);- input2.addEventListener('focus', () => focusCount++);- // Click input1 — should focus it and fire focus event+ // el.click() does not move focus per HTML spec / Chrome parityinput1.click();- testing.expectEqual(input1, document.activeElement);- testing.expectEqual(1, focusCount);- testing.expectEqual(0, blurCount);-- // Click input2 — should move focus, fire blur on input1 and focus on input2- input2.click();- testing.expectEqual(input2, document.activeElement);- testing.expectEqual(2, focusCount);- testing.expectEqual(1, blurCount);+ testing.expectEqual('BODY', document.activeElement.tagName);+ testing.expectEqual(0, focusCount);}</script>-<script id="click_focuses_button">+<script id="click_does_not_focus_button">{const btn = $('#btn1');@@ -294,7 +282,7 @@}btn.click();- testing.expectEqual(btn, document.activeElement);+ testing.expectEqual('BODY', document.activeElement.tagName);}</script>diff --git a/src/script/Runtime.zig b/src/script/Runtime.zigindex 8a9140e41..9794776a3 100644--- a/src/script/Runtime.zig+++ b/src/script/Runtime.zig@@ -1469,8 +1469,10 @@ test "agent script runtime: mousedown focus follows mouse-focusability rules" {\\ add('span', 'dynChild', {}, add('div', 'dynFocus', { tabindex: '0' })).textContent = 'x';\\ add('div', 'dynNeg', { tabindex: '-1' }).textContent = 'neg';\\ add('div', 'dynBad', { tabindex: 'abc' }).textContent = 'bad';- \\ add('button', 'dynBadBtn', { tabindex: 'abc' }).addEventListener('mouseup', () => { window.badBtnFocusAtMouseup = document.activeElement.id; });- \\ add('div', 'toolbarBtn').addEventListener('mousedown', (e) => e.preventDefault());+ \\ add('button', 'dynBadBtn', { tabindex: 'abc' });+ \\ add('button', 'toolbarBtn').addEventListener('mousedown', (e) => e.preventDefault());+ \\ add('input', 'dynLabInp');+ \\ add('label', 'dynLabel', { for: 'dynLabInp' }).textContent = 'lab';\\ add('span', 'dynHostSpan', {}, add('div', 'dynHost', { contenteditable: 'true' })).textContent = 'hs';\\ add('span', 'dynInnerSpan', {}, add('div', 'dynInner', { contenteditable: 'true' }, add('div', 'dynOuter', { contenteditable: 'true' }))).textContent = 'is';\\ const SVG = 'http://www.w3.org/2000/svg';@@ -1483,14 +1485,15 @@ test "agent script runtime: mousedown focus follows mouse-focusability rules" {\\page.click("#dynBad");\\expectActive("body", "unparsable tabindex was mouse-focusable");\\page.click("#dynBadBtn");- \\// Sampled at mouseup: click activation focuses a button regardless of- \\// what mousedown decided, which would mask the native focusability.- \\if (page.evaluate("window.badBtnFocusAtMouseup") !== "dynBadBtn") throw new Error("unparsable tabindex on a native button lost native mousedown focusability");+ \\expectActive("dynBadBtn", "unparsable tabindex on a native button lost native mousedown focusability");\\// Toolbar idiom: preventDefault() on mousedown preserves existing focus.\\page.click("#inp");\\expectActive("inp", "setup failed");\\page.click("#toolbarBtn");\\expectActive("inp", "preventDefault on mousedown did not protect focus");+ \\// Label click focuses its labeled control.+ \\page.click("#dynLabel");+ \\expectActive("dynLabInp", "clicking label did not focus its control");\\// Verified against Chrome.\\page.click("#dynHostSpan");\\expectActive("dynHost", "span inside contenteditable did not focus host");