Lightpanda: the headless browser designed for AI and automation
form: press enter on a form must implicitely submit it
Pierre Tachoire28fd984cdd78
diff --git a/src/browser/Frame.zig b/src/browser/Frame.zigindex f1f864735..0c7fce5f7 100644--- a/src/browser/Frame.zig+++ b/src/browser/Frame.zig@@ -3783,8 +3783,6 @@ pub fn submitForm(self: *Frame, submitter_: ?*Element, form_: ?*Element.Html.Forconst FormData = @import("webapi/net/FormData.zig");- // The submitter can be an input box (if enter was entered on the box)- // I don't think this is technically correct, but FormData handles it ok// Resolved before the entry list is built: a hidden `_charset_` field// takes the submission encoding's name as its value.const charset: []const u8 = blk: {diff --git a/src/browser/frame/user_input.zig b/src/browser/frame/user_input.zigindex 88e34d377..c5bf625c7 100644--- a/src/browser/frame/user_input.zig+++ b/src/browser/frame/user_input.zig@@ -914,7 +914,7 @@ pub fn typeChar(frame: *Frame, target: *Element, keypress: *KeyboardEvent, text:if (target.is(Element.Html.Input)) |input| {if (is_enter) {- return frame.submitForm(input.asElement(), input.getForm(frame), .{});+ return implicitFormSubmission(frame, input);}return insertInto(frame, input, text);}@@ -930,6 +930,24 @@ pub fn typeChar(frame: *Frame, target: *Element, keypress: *KeyboardEvent, text:}}+/// Enter in a form field. The default button, when there is one, is clicked and+/// its activation submits the form with it as the submitter; otherwise the+/// form submits itself (SubmitEvent.submitter is null).+/// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#implicit-submission+fn implicitFormSubmission(frame: *Frame, input: *Element.Html.Input) !void {+ const form = input.getForm(frame) orelse return;+ if (form.getDefaultButton(frame)) |button| {+ if (button.isDisabled()) {+ return;+ }+ return dispatchKeyboardClick(frame, button);+ }+ if (form.hasMultipleImplicitSubmissionBlockers(frame)) {+ return;+ }+ return frame.submitForm(form.asElement(), form, .{});+}+fn keypressFor(frame: *Frame, keydown: *const KeyboardEvent) !*KeyboardEvent {return KeyboardEvent.initTrusted(comptime .wrap("keypress"), .{.key = keydown.getKey().asString(),diff --git a/src/browser/webapi/element/html/Form.zig b/src/browser/webapi/element/html/Form.zigindex 5dac1f506..349e03338 100644--- a/src/browser/webapi/element/html/Form.zig+++ b/src/browser/webapi/element/html/Form.zig@@ -167,6 +167,39 @@ pub fn requestSubmit(self: *Form, submitter: ?*Element, frame: *Frame) !void {return frame.submitForm(submitter_element, self, .{});}+/// The form's first submit button in tree order.+/// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#default-button+pub fn getDefaultButton(self: *Form, frame: *Frame) ?*Element {+ var it = self.iterator(frame);+ while (it.next()) |element| {+ if (isSubmitButton(element)) {+ return element;+ }+ }+ return null;+}++/// Implicit submission without a default button only submits when the form has+/// at most one field that blocks it.+/// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#implicit-submission+pub fn hasMultipleImplicitSubmissionBlockers(self: *Form, frame: *Frame) bool {+ var count: usize = 0;+ 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;+ }+ },+ else => {},+ }+ }+ return false;+}+/// Returns true if the element is a submit button per the HTML spec:/// - <input type="submit"> or <input type="image">/// - <button type="submit"> (including default, since button's default type is "submit")diff --git a/src/server/cdp/domains/input.zig b/src/server/cdp/domains/input.zigindex 3e0864d21..b78001dab 100644--- a/src/server/cdp/domains/input.zig+++ b/src/server/cdp/domains/input.zig@@ -1783,6 +1783,72 @@ test "cdp.input: dispatchKeyEvent Enter clicks buttons and submits once" {}}+// Enter in a text field clicks the form's default button (its first submit+// button in tree order), which then submits with itself as the submitter.+// Without a default button the form submits itself, unless more than one+// field blocks implicit submission.+test "cdp.input: dispatchKeyEvent Enter in a field submits through the default button" {+ 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=a><input id=a_text name=q><input id=a_go type=submit name=search value=Go><button id=a_go2>2</button></form>' ++ \\ '<form id=b><input id=b_text><button id=b_go disabled>go</button></form>' ++ \\ '<form id=c><fieldset disabled><button id=c_go>go</button></fieldset><input id=c_text></form>' ++ \\ '<form id=d><input id=d_text name=q><input type=checkbox><input type=hidden></form>' ++ \\ '<form id=e><input id=e_text><input type=email></form>' ++ \\ '<input id=f_go type=submit form=f name=out value=1><form id=f><input id=f_text><button id=f_go2>2</button></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 }{+ .{ .id = "a_text", .expect = "click:a_go submit:a_go:q=&search=Go" },+ .{ .id = "b_text", .expect = "" },+ .{ .id = "c_text", .expect = "" },+ .{ .id = "d_text", .expect = "submit:null:q=" },+ .{ .id = "e_text", .expect = "" },+ .{ .id = "f_text", .expect = "click:f_go submit:f_go:out=1" },+ };++ 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();