Skip to content

Lightpanda: the headless browser designed for AI and automation

form: only a text field submits a form without a default button

Pierre Tachoire2026c226dfb7
diff --git a/src/browser/frame/user_input.zig b/src/browser/frame/user_input.zig
index 5fc659a99..c2958763a 100644
--- a/src/browser/frame/user_input.zig
+++ b/src/browser/frame/user_input.zig
@@ -951,7 +951,7 @@ fn implicitFormSubmission(frame: *Frame, input: *Element.Html.Input) !void {
}
return dispatchKeyboardClick(frame, button);
}
- if (form.hasMultipleImplicitSubmissionBlockers(frame)) {
+ if (!form.canSubmitImplicitly(input, frame)) {
return;
}
return frame.submitForm(form.asElement(), form, .{});
diff --git a/src/browser/webapi/element/html/Form.zig b/src/browser/webapi/element/html/Form.zig
index 349e03338..9655e8d80 100644
--- a/src/browser/webapi/element/html/Form.zig
+++ b/src/browser/webapi/element/html/Form.zig
@@ -180,24 +180,26 @@ pub fn getDefaultButton(self: *Form, frame: *Frame) ?*Element {
}
/// Implicit submission without a default button only submits when the form has
-/// at most one field that blocks it.
+/// at most one field that blocks it. Like Chrome, it also requires `trigger`,
+/// the field that got the Enter key, to be that field: Enter on a checkbox or
+/// a radio does not submit.
/// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#implicit-submission
-pub fn hasMultipleImplicitSubmissionBlockers(self: *Form, frame: *Frame) bool {
- var count: usize = 0;
+pub fn canSubmitImplicitly(self: *Form, trigger: *Input, frame: *Frame) bool {
+ var blocker: ?*Input = null;
var it = self.iterator(frame);
while (it.next()) |element| {
const input = element.is(Input) orelse continue;
switch (input._input_type) {
.text, .search, .url, .tel, .email, .password, .date, .month, .week, .time, .@"datetime-local", .number => {
- count += 1;
- if (count > 1) {
- return true;
+ if (blocker != null) {
+ return false;
}
+ blocker = input;
},
else => {},
}
}
- return false;
+ return blocker == trigger;
}
/// Returns true if the element is a submit button per the HTML spec:
diff --git a/src/server/cdp/domains/input.zig b/src/server/cdp/domains/input.zig
index b78001dab..818ba6d69 100644
--- a/src/server/cdp/domains/input.zig
+++ b/src/server/cdp/domains/input.zig
@@ -1849,6 +1849,76 @@ test "cdp.input: dispatchKeyEvent Enter in a field submits through the default b
}
}
+// Enter on a checkbox or a radio clicks the form's default button like a text
+// field does, but without a default button it never submits the form: only a
+// text field can trigger the submission. Enter on a select never submits.
+// Expectations match Chrome.
+test "cdp.input: dispatchKeyEvent Enter on a checkbox, radio or select" {
+ var ctx = try testing.context();
+ defer ctx.deinit();
+
+ const bc = try ctx.loadBrowserContext(.{ .url = "mcp_actions.html" });
+ const frame = bc.mainFrame().?;
+
+ var ls: lp.js.Local.Scope = undefined;
+ frame.js.localScope(&ls);
+ defer ls.deinit();
+
+ _ = try ls.local.compileAndRun(
+ \\document.body.insertAdjacentHTML('beforeend',
+ \\ '<form id=g><input id=g_text name=q><input id=g_cb type=checkbox name=c checked><input id=g_radio type=radio name=r value=1 checked><select id=g_sel name=s><option>x</option></select></form>' +
+ \\ '<form id=h><input id=h_cb type=checkbox name=c checked><input id=h_radio type=radio name=r value=1 checked><select id=h_sel name=s><option>x</option></select><button id=h_go name=go value=1>go</button></form>' +
+ \\ '<form id=i><input id=i_cb type=checkbox name=c checked><input id=i_radio type=radio name=r value=1 checked><select id=i_sel name=s><option>x</option></select></form>');
+ \\window.events = [];
+ \\document.addEventListener('click', (e) => window.events.push('click:' + e.target.id), true);
+ \\document.addEventListener('submit', (e) => {
+ \\ e.preventDefault();
+ \\ const s = e.submitter;
+ \\ const entries = Array.from(new FormData(e.target, s)).map(([k, v]) => k + '=' + v).join('&');
+ \\ window.events.push('submit:' + (s ? s.id : 'null') + ':' + entries);
+ \\}, true);
+ \\window.arm = (id) => {
+ \\ window.events = [];
+ \\ document.getElementById(id).focus();
+ \\};
+ , null);
+
+ const cases = [_]struct { id: []const u8, expect: []const u8 }{
+ // no default button, one text field
+ .{ .id = "g_text", .expect = "submit:null:q=&c=on&r=1&s=x" },
+ .{ .id = "g_cb", .expect = "" },
+ .{ .id = "g_radio", .expect = "" },
+ .{ .id = "g_sel", .expect = "" },
+ // a default button
+ .{ .id = "h_cb", .expect = "click:h_go submit:h_go:c=on&r=1&s=x&go=1" },
+ .{ .id = "h_radio", .expect = "click:h_go submit:h_go:c=on&r=1&s=x&go=1" },
+ .{ .id = "h_sel", .expect = "" },
+ // no default button, no text field
+ .{ .id = "i_cb", .expect = "" },
+ .{ .id = "i_radio", .expect = "" },
+ .{ .id = "i_sel", .expect = "" },
+ };
+
+ var id: u32 = 1;
+ for (cases) |c| {
+ var buf: [32]u8 = undefined;
+ _ = try ls.local.compileAndRun(try std.mem.print(&buf, "arm('{s}')", .{c.id}), null);
+
+ try ctx.processMessage(.{ .id = id, .method = "Input.dispatchKeyEvent", .params = .{ .type = "keyDown", .key = "Enter", .code = "Enter" } });
+ try ctx.expectSentResult(null, .{ .id = id });
+ id += 1;
+ try ctx.processMessage(.{ .id = id, .method = "Input.dispatchKeyEvent", .params = .{ .type = "char", .key = "Enter", .text = "\r" } });
+ try ctx.expectSentResult(null, .{ .id = id });
+ id += 1;
+ try ctx.processMessage(.{ .id = id, .method = "Input.dispatchKeyEvent", .params = .{ .type = "keyUp", .key = "Enter", .code = "Enter" } });
+ try ctx.expectSentResult(null, .{ .id = id });
+ id += 1;
+
+ const got = try (try ls.local.compileAndRun("window.events.join(' ')", null)).toStringSlice();
+ try testing.expectEqualSlices(u8, c.expect, got);
+ }
+}
+
test "cdp.input: re-navigating an iframe drops the pointer state on its elements" {
var ctx = try testing.context();
defer ctx.deinit();