Skip to content

Lightpanda: the headless browser designed for AI and automation

webapi: spec-conformant click activation target selection

Francis Bouvier6587778c12f5
diff --git a/src/browser/EventManager.zig b/src/browser/EventManager.zig
index 5087ae527..a03d36d47 100644
--- a/src/browser/EventManager.zig
+++ b/src/browser/EventManager.zig
@@ -233,9 +233,16 @@ fn dispatchNode(self: *EventManager, target: *Node, event: *Event, comptime opts
if (event._prevent_default) {
// can't return in a defer (╯°□°)╯︵ ┻━┻
} else if (event._type_string.eql(comptime .wrap("click"))) {
- Frame.user_input.handleClick(frame, target) catch |err| {
- log.warn(.event, "frame.click", .{ .err = err });
- };
+ // Per spec, only a MouseEvent "click" is an activation event, and
+ // the activation target is the nearest inclusive ancestor with
+ // activation behavior (ancestors only for bubbling events).
+ if (event.is(@import("webapi/event/MouseEvent.zig")) != null) {
+ if (Frame.user_input.findClickActivationTarget(target, event._bubbles)) |activation_target| {
+ Frame.user_input.handleClick(frame, activation_target) catch |err| {
+ log.warn(.event, "frame.click", .{ .err = err });
+ };
+ }
+ }
} else if (event._type_string.eql(comptime .wrap("keydown"))) {
Frame.user_input.handleKeydown(frame, target, event) catch |err| {
log.warn(.event, "frame.keydown", .{ .err = err });
@@ -656,12 +663,18 @@ const ActivationState = struct {
const Input = Element.Html.Input;
- fn create(event: *const Event, target: *Node, frame: *Frame) !?ActivationState {
+ fn create(event: *Event, target: *Node, frame: *Frame) !?ActivationState {
if (event._type_string.eql(comptime .wrap("click")) == false) {
return null;
}
- const input = target.is(Element.Html.Input) orelse return null;
+ // Per spec, only a MouseEvent "click" is an activation event.
+ if (event.is(@import("webapi/event/MouseEvent.zig")) == null) {
+ return null;
+ }
+
+ const activation_node = Frame.user_input.findClickActivationTarget(target, event._bubbles) orelse return null;
+ const input = activation_node.is(Element.Html.Input) orelse return null;
if (input._input_type != .checkbox and input._input_type != .radio) {
return null;
}
diff --git a/src/browser/Frame.zig b/src/browser/Frame.zig
index 4e7e5b34f..3a3924f26 100644
--- a/src/browser/Frame.zig
+++ b/src/browser/Frame.zig
@@ -3151,6 +3151,14 @@ const SubmitFormOpts = struct {
pub fn submitForm(self: *Frame, submitter_: ?*Element, form_: ?*Element.Html.Form, submit_opts: SubmitFormOpts) !void {
const form = form_ orelse return;
+ // Interactive submission (submit button, requestSubmit) of a
+ // disconnected form does nothing; the programmatic form.submit() is
+ // exempt from the connectedness requirement (HTML "cannot navigate"
+ // only applies its connected clause to non-form elements).
+ if (submit_opts.fire_event and !form.asElement().asNode().isConnected()) {
+ return;
+ }
+
// see the `_constructing_entry_list` field documentation
if (form._constructing_entry_list) {
return;
diff --git a/src/browser/frame/user_input.zig b/src/browser/frame/user_input.zig
index a9c761a8f..32f0c4456 100644
--- a/src/browser/frame/user_input.zig
+++ b/src/browser/frame/user_input.zig
@@ -78,6 +78,7 @@ pub fn triggerMousePress(frame: *Frame, x: f64, y: f64, button: i32) !void {
});
}
try dispatchMouseEventOn(frame, target, "mousedown", x, y, button, 0);
+ try focusEditingHostForMouseDown(frame, target);
}
pub fn triggerMouseMove(frame: *Frame, x: f64, y: f64) !void {
@@ -200,6 +201,76 @@ fn deltaToScroll(d: f64) i32 {
}
// callback when the "click" event reaches the frame.
+// Whether the element has a click activation behavior that handleClick
+// implements.
+fn hasClickActivationBehavior(node: *Node) bool {
+ const element = node.is(Element) orelse return false;
+ const html_element = element.is(Element.Html) orelse return false;
+ return switch (html_element._type) {
+ .anchor => element.getAttributeSafe(comptime .wrap("href")) != null,
+ .input, .button, .select, .textarea, .label => true,
+ .generic => |generic| generic._tag == .summary,
+ 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
+// String wrap runs at runtime, mirroring Html.getIsContentEditable.
+fn isEditingHost(node: *Node) bool {
+ const element = node.is(Element) orelse return false;
+ const value = element.getAttributeSafe(.wrap("contenteditable")) orelse return false;
+ return !std.ascii.eqlIgnoreCase(value, "false");
+}
+
+// A mousedown on editable content focuses its editing host: the outermost
+// element of the contiguous editable chain containing the target.
+pub fn focusEditingHostForMouseDown(frame: *Frame, target: *Element) !void {
+ var node: ?*Node = target.asNode();
+ var editable: ?*Node = null;
+ while (node) |n| : (node = n._parent) {
+ if (isEditingHost(n)) {
+ editable = n;
+ break;
+ }
+ }
+ var host = editable orelse return;
+ while (host._parent) |p| {
+ if (!isEditingHost(p)) {
+ break;
+ }
+ host = p;
+ }
+ const host_element = host.is(Element) orelse return;
+ try host_element.focus(frame);
+}
+
+// Per the DOM dispatch algorithm, a click's activation target is the event
+// target itself when it has activation behavior, otherwise — for bubbling
+// events only — the nearest ancestor that has one.
+pub fn findClickActivationTarget(target: *Node, bubbles: bool) ?*Node {
+ if (isEditingHost(target)) {
+ return null;
+ }
+ if (hasClickActivationBehavior(target)) {
+ return target;
+ }
+ if (!bubbles) {
+ return null;
+ }
+ var node = target._parent;
+ while (node) |n| : (node = n._parent) {
+ if (isEditingHost(n)) {
+ return null;
+ }
+ if (hasClickActivationBehavior(n)) {
+ return n;
+ }
+ }
+ return null;
+}
+
pub fn handleClick(frame: *Frame, target: *Node) !void {
// TODO: Also support <area> elements when implement
const element = target.is(Element) orelse return;
diff --git a/src/browser/webapi/WebDriver.zig b/src/browser/webapi/WebDriver.zig
index 643cd6d98..e494c4b21 100644
--- a/src/browser/webapi/WebDriver.zig
+++ b/src/browser/webapi/WebDriver.zig
@@ -173,6 +173,9 @@ fn performPointerSource(source: js.Object, frame: *Frame) !void {
const button = readI32(action, "button", 0);
dispatchPointer(el, "pointerdown", button, 1, frame);
dispatchMouse(el, "mousedown", button, 1, frame);
+ Frame.user_input.focusEditingHostForMouseDown(frame, el) catch |err| {
+ log.warn(.app, "webdriver editable focus", .{ .err = err });
+ };
} else if (action_type.eql(comptime .wrap("pointerUp"))) {
const el = target orelse continue;
const button = readI32(action, "button", 0);