Skip to content

Lightpanda: the headless browser designed for AI and automation

fix: setChecked double-toggled checkables via click activation

Adrià Arrufatf3f731ffb10c
diff --git a/src/browser/actions.zig b/src/browser/actions.zig
index 9ed544c9c..8947b73f6 100644
--- a/src/browser/actions.zig
+++ b/src/browser/actions.zig
@@ -189,23 +189,24 @@ pub fn setChecked(node: *DOMNode, checked: bool, frame: *Frame) !void {
return error.InvalidNodeType;
}
- input.setChecked(checked, frame) catch |err| {
- lp.log.err(.app, "setChecked failed", .{ .err = err });
- return error.ActionFailed;
- };
-
- // Match browser event order: click fires first, then input and change.
- const click_event: *MouseEvent = try .initTrusted(comptime .wrap("click"), .{
- .bubbles = true,
- .cancelable = true,
- .composed = true,
- }, frame);
+ if (input.getChecked() == checked) {
+ return;
+ }
+ if (input._input_type == .radio and !checked) {
+ // A click can never uncheck a radio.
+ return error.InvalidNodeType;
+ }
- frame._event_manager.dispatch(el.asEventTarget(), click_event.asEvent()) catch |err| {
- lp.log.err(.app, "dispatch click event failed", .{ .err = err });
- };
+ // The click's activation behavior (EventManager.ActivationState) toggles
+ // the state and fires input and change; setting the state up front would
+ // make the click undo it, and dispatching input/change here would double
+ // them up.
+ try click(node, frame);
- try dispatchInputAndChangeEvents(el, frame);
+ if (input.getChecked() != checked) {
+ lp.log.err(.app, "setChecked click prevented", .{});
+ return error.ActionFailed;
+ }
}
pub fn fill(node: *DOMNode, text: []const u8, frame: *Frame) !void {
diff --git a/src/script/Runtime.zig b/src/script/Runtime.zig
index df51f2a6b..ddc879148 100644
--- a/src/script/Runtime.zig
+++ b/src/script/Runtime.zig
@@ -1470,11 +1470,14 @@ test "agent script runtime: builtin argument marshalling (positional + options)"
\\if (page.evaluate("window.inputVal") !== "hello") throw new Error("fill two-positional failed");
\\page.selectOption("#sel", "opt2");
\\if (page.evaluate("window.selChanged") !== "opt2") throw new Error("selectOption two-positional failed");
- \\// Bool positional, and the default-true shorthand when omitted. Assert via the
- \\// tool's own report (the synthetic click toggles the DOM state, so .checked is
- \\// not a reliable observation of the `checked` argument).
+ \\// Bool positional, and the default-true shorthand when omitted.
\\if (!page.setChecked("#chk").includes("to checked")) throw new Error("setChecked default-true failed");
+ \\if (page.evaluate("String(document.getElementById('chk').checked)") !== "true") throw new Error("setChecked(true) did not check the box");
\\if (!page.setChecked("#chk", false).includes("to unchecked")) throw new Error("setChecked bool positional failed");
+ \\if (page.evaluate("String(document.getElementById('chk').checked)") !== "false") throw new Error("setChecked(false) did not uncheck the box");
+ \\// Repeat call to the same state is an idempotent no-op, not a toggle.
+ \\page.setChecked("#chk", false);
+ \\if (page.evaluate("String(document.getElementById('chk').checked)") !== "false") throw new Error("setChecked no-op toggled the box");
\\// Selector-first press, and a null selector for a page/focused key press.
\\page.press("#keyTarget", "Enter");
\\if (page.evaluate("window.keyPressed") !== "Enter") throw new Error("selector-first press failed");