Skip to content

Lightpanda: the headless browser designed for AI and automation

Merge remote-tracking branch 'origin/main' into fix/click-dispatch-full-mouse-sequence

Adrià Arrufat0e04a2fe5db2
diff --cc src/browser/frame/user_input.zig
index 2fbc25d61,3cc7920e2..a0f791db2
--- a/src/browser/frame/user_input.zig
+++ b/src/browser/frame/user_input.zig
@@@ -326,10 -323,15 +326,10 @@@ fn deltaToScroll(d: f64) i32
fn hasClickActivationBehavior(node: *Node) bool {
const element = node.is(Element) orelse return false;
- const html_element = element.is(Element.Html) orelse {
- if (element.is(Element.Svg.Graphics.A) != null) {
- return svgAnchorHref(element) != null;
- }
- return false;
- };
+ const html_element = element.is(Element.Html) orelse return isSvgLink(element);
return switch (html_element._type) {
- .anchor => element.getAttributeSafe(comptime .wrap("href")) != null,
+ .anchor => element.getAttributeInterned("href") != null,
.input, .button, .select, .textarea, .label => true,
.generic => html_element.subtype(Element.Html.Generic)._tag == .summary,
else => false,
@@@ -338,26 -340,9 +338,26 @@@
// SVG 2 <a> links via `href`; xlink:href is the deprecated SVG 1.1 spelling.
fn svgAnchorHref(element: *Element) ?[]const u8 {
- return element.getAttributeSafe(comptime .wrap("href")) orelse element.getAttributeSafe(comptime .wrap("xlink:href"));
+ return element.getAttributeInterned("href") orelse element.getAttributeSafe(comptime .wrap("xlink:href"));
}
+fn isSvgLink(element: *Element) bool {
+ return element.is(Element.Svg.Graphics.A) != null and svgAnchorHref(element) != null;
+}
+
+/// Focusable without a tabindex attribute.
+fn isNativelyFocusable(el: *Element) bool {
+ if (el.is(Element.Html) == null) {
+ return isSvgLink(el);
+ }
+ return switch (el.getTag()) {
+ .button, .select, .textarea, .iframe => true,
+ .input => el.as(Element.Html.Input)._input_type != .hidden,
+ .anchor, .area => el.getAttributeSafe(comptime .wrap("href")) != null,
+ else => false,
+ };
+}
+
// Clicks on editable content are for editing: they don't activate the
// element or any enclosing link.
// "contenteditable" is 15 bytes — past the comptime SSO limit — so the
diff --cc src/browser/webapi/Element.zig
index 392d1b817,35354321c..70f8a5f87
--- a/src/browser/webapi/Element.zig
+++ b/src/browser/webapi/Element.zig
@@@ -1126,24 -1211,24 +1211,24 @@@ pub fn focus(self: *Element, frame: *Fr
return;
}
+ const doc = self.asNode().ownerDocument(frame) orelse frame.document;
+ const old_active = doc._active_element;
+ if (old_active == self) {
+ return;
+ }
+
// Per HTML spec §6.4.4, an element must be "being rendered" (not
// display:none on self or any ancestor) to be focusable.
- if (!self.checkVisibilityCached(null, frame, .materialize)) {
+ if (!self.isVisible(frame)) {
return;
}
const FocusEvent = @import("event/FocusEvent.zig");
const new_target = self.asEventTarget();
- doc._active_element = self;
- const doc = self.asNode().ownerDocument(frame) orelse frame.document;
- const old_active = doc._active_element;
+ doc.setActiveElement(self, frame);
if (old_active) |old| {
- if (old == self) {
- return;
- }
-
const old_target = old.asEventTarget();
// Dispatch blur on old element (no bubble, composed)
diff --cc src/script/Runtime.zig
index 1fe92f243,5c6c6f811..31aa95c97
--- a/src/script/Runtime.zig
+++ b/src/script/Runtime.zig
@@@ -1441,68 -1441,6 +1441,68 @@@ test "agent script runtime: tool error
);
}
+// Complements the fixture-driven MCP test in tools.zig with nodes created at
+// runtime.
+test "agent script runtime: mousedown focus follows mouse-focusability rules" {
+ defer testing.test_session.closeAllPages();
+
- var registry = CDPNode.Registry.init(testing.allocator);
++ var registry = NodeRegistry.init(testing.allocator);
+ defer registry.deinit();
+
+ const runtime = try Runtime.init(testing.allocator, testing.test_app, testing.test_session, &registry);
+ defer runtime.deinit();
+
+ try runTestScript(runtime,
+ \\const page = new Page();
+ \\await page.goto("http://localhost:9582/src/browser/tests/mcp_actions.html");
+ \\const active = () => page.evaluate("document.activeElement === document.body ? 'body' : document.activeElement.id");
+ \\const expectActive = (id, what) => { const got = active(); if (got !== id) throw new Error(what + " (active: " + got + ")"); };
+ \\page.evaluate(`
+ \\ const add = (tag, id, attrs = {}, parent = document.body, ns = null) => {
+ \\ const e = ns ? document.createElementNS(ns, tag) : document.createElement(tag);
+ \\ e.id = id;
+ \\ for (const [k, v] of Object.entries(attrs)) e.setAttribute(k, v);
+ \\ parent.appendChild(e);
+ \\ return e;
+ \\ };
+ \\ 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('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';
+ \\ add('rect', 'dynSvgRect', { tabindex: '0', width: '100', height: '40' }, add('svg', 'dynSvg', {}, document.body, SVG), SVG);
+ \\`);
+ \\page.click("#dynChild");
+ \\expectActive("dynFocus", "child click did not focus tabindex ancestor");
+ \\page.click("#dynNeg");
+ \\expectActive("dynNeg", "tabindex=-1 was not mouse-focusable");
+ \\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");
+ \\// 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");
+ \\// Verified against Chrome: a click inside a contenteditable focuses the
+ \\// host, and a nested host resolves to the outer one.
+ \\page.click("#dynHostSpan");
+ \\expectActive("dynHost", "span inside contenteditable did not focus host");
+ \\page.click("#dynInnerSpan");
+ \\expectActive("dynOuter", "nested contenteditable did not focus the outer host");
+ \\// An explicit tabindex is focusable on a non-HTML element too.
+ \\page.click("#inp");
+ \\page.click("#dynSvgRect");
+ \\expectActive("dynSvgRect", "svg [tabindex] click did not focus the svg element");
+ );
+}
+
test "agent script runtime: builtin argument marshalling (positional + options)" {
defer testing.test_session.closeAllPages();