Lightpanda: the headless browser designed for AI and automation
input: describe each edit once and leave select-all to fill
Adrià Arrufat0313fd154814
diff --git a/src/browser/actions.zig b/src/browser/actions.zigindex 23d96cf1f..9514fbe97 100644--- a/src/browser/actions.zig+++ b/src/browser/actions.zig@@ -193,21 +193,22 @@ pub fn fill(node: *DOMNode, text: []const u8, frame: *Frame) !void {/// its picker would. `change` fires right away: nothing commits it on blur.fn fillControl(ctl: anytype, text: []const u8, frame: *Frame) !void {const el = ctl.asElement();+ if (!ctl.acceptsTextEntry() or !Frame.user_input.acceptsEdit(el)) {+ return error.InvalidNodeType;+ }+if (ctl.tracksSelection()) {- const result = Frame.user_input.applyEdit(frame, ctl, .{ .replace = text }, .{}) catch |err| {+ try ctl.select(frame);+ const edited = Frame.user_input.applyEdit(frame, ctl, .{ .insert = text }, .{}) catch |err| {lp.log.debug(.app, "fill insert failed", .{ .err = err });return error.ActionFailed;};- return switch (result) {- .done => dispatchTrusted(el, "change", frame),- .refused => error.InvalidNodeType,- .cancelled => error.ActionFailed,- };+ if (!edited) {+ return error.ActionFailed;+ }+ return dispatchTrusted(el, "change", frame);}- if (!ctl.acceptsTextEntry() or !Frame.user_input.acceptsEdit(el)) {- return error.InvalidNodeType;- }ctl.setUserValue(text, frame) catch |err| {lp.log.debug(.app, "fill setValue failed", .{ .err = err });return error.ActionFailed;diff --git a/src/browser/frame/user_input.zig b/src/browser/frame/user_input.zigindex b2a2196da..18675da1c 100644--- a/src/browser/frame/user_input.zig+++ b/src/browser/frame/user_input.zig@@ -984,51 +984,39 @@ fn editKey(frame: *Frame, keyboard_event: *KeyboardEvent, ctl: anytype, key: Keypub const Edit = union(enum) {insert: []const u8,- /// Selects the whole value first, as typing over a select-all does.- replace: []const u8,line_break,delete: enum { backward, forward },};-pub const EditResult = enum { done, refused, cancelled };--/// A text edit as the user makes it: refused on a readonly or disabled-/// control, cancellable through beforeinput.-pub fn applyEdit(frame: *Frame, ctl: anytype, edit: Edit, opts: struct { beforeinput: bool = true }) !EditResult {+/// A text edit as the user makes it: cancellable through beforeinput, and+/// refused on a readonly or disabled control. Returns whether it happened.+pub fn applyEdit(frame: *Frame, ctl: anytype, edit: Edit, opts: struct { beforeinput: bool = true }) !bool {const el = ctl.asElement();- if (!ctl.acceptsTextEntry()) {- return .refused;- }- // Chrome lets text typed into any text control reach beforeinput and- // textInput, and only then finds it can't edit a readonly one. Its- // editing commands (delete, line break) are disabled there and fire- // nothing.const editable = acceptsEdit(el);- if (!editable and edit != .insert) {- return .refused;- }- if (edit == .replace) {- try ctl.select(frame);+ // Chrome fires beforeinput and textInput for text typed into a readonly+ // control and only then refuses it; its editing commands fire nothing.+ if (!ctl.acceptsTextEntry() or (!editable and edit != .insert)) {+ return false;}- if (opts.beforeinput) {- const allowed = switch (edit) {- .insert, .replace => |text| try allowEdit(frame, el, text, text, "insertText"),- .line_break => try allowEdit(frame, el, null, "\n", "insertLineBreak"),- .delete => |dir| try allowEdit(frame, el, null, null, if (dir == .forward) "deleteContentForward" else "deleteContentBackward"),- };- if (!allowed) {- return .cancelled;- }++ const data: ?[]const u8, const text: ?[]const u8, const input_type: []const u8 = switch (edit) {+ .insert => |t| .{ t, t, "insertText" },+ .line_break => .{ null, "\n", "insertLineBreak" },+ .delete => |dir| .{ null, null, if (dir == .forward) "deleteContentForward" else "deleteContentBackward" },+ };+ if (opts.beforeinput and !try allowEdit(frame, el, data, text, input_type)) {+ return false;}if (!editable) {- return .refused;+ return false;}- switch (edit) {- .insert, .replace => |text| try ctl.innerInsert(text, .text, frame),- .line_break => try ctl.innerInsert("\n", .line_break, frame),- .delete => |dir| try ctl.innerDelete(dir == .forward, frame),++ if (text) |t| {+ try ctl.innerInsert(t, data, input_type, frame);+ } else {+ try ctl.innerDelete(edit.delete == .forward, input_type, frame);}- return .done;+ return true;}pub fn acceptsEdit(el: *Element) bool {diff --git a/src/browser/webapi/element/text_entry.zig b/src/browser/webapi/element/text_entry.zigindex 85e9b54f3..267dd16e4 100644--- a/src/browser/webapi/element/text_entry.zig+++ b/src/browser/webapi/element/text_entry.zig@@ -69,19 +69,7 @@ pub fn TextEntry(comptime T: type) type {try frame._event_manager.dispatch(self.asElement().asEventTarget(), event);}- pub const Insertion = enum {- text,- line_break,-- fn inputType(self: Insertion) []const u8 {- return switch (self) {- .text => "insertText",- .line_break => "insertLineBreak",- };- }- };-- pub fn innerInsert(self: *T, str: []const u8, kind: Insertion, frame: *Frame) !void {+ pub fn innerInsert(self: *T, str: []const u8, data: ?[]const u8, input_type: []const u8, frame: *Frame) !void {const current_value = self.getValue();const value_len: u32 = @intCast(current_value.len);const start: u32, const end: u32 = switch (howSelected(self)) {@@ -94,10 +82,12 @@ pub fn TextEntry(comptime T: type) type {},};- const scratch = try frame.getArena(.small, "TextEntry.innerInsert");- defer scratch.release();- const new_value = try std.mem.concat(scratch.allocator(), u8, &.{ current_value[0..start], str, current_value[end..] });- try self.setUserValue(new_value, frame);+ {+ const scratch = try frame.getArena(.small, "TextEntry.innerInsert");+ defer scratch.release();+ const new_value = try std.mem.concat(scratch.allocator(), u8, &.{ current_value[0..start], str, current_value[end..] });+ try self.setUserValue(new_value, frame);+ }if (tracksSelection(self)) {// the sanitized value can be shorter than what was inserted@@ -107,12 +97,12 @@ pub fn TextEntry(comptime T: type) type {self._selection_direction = .none;try dispatchSelectionChangeEvent(self, frame);}- try dispatchInputEvent(self, if (kind == .text) str else null, kind.inputType(), frame);+ try dispatchInputEvent(self, data, input_type, frame);}// forward == delete// !forward == backspace- pub fn innerDelete(self: *T, forward: bool, frame: *Frame) !void {+ pub fn innerDelete(self: *T, forward: bool, input_type: []const u8, frame: *Frame) !void {const current_value = self.getValue();const value_len: u32 = @intCast(current_value.len);@@ -148,18 +138,20 @@ pub fn TextEntry(comptime T: type) type {},}- const scratch = try frame.getArena(.small, "TextEntry.innerDelete");- defer scratch.release();- const new_value = try std.mem.concat(scratch.allocator(), u8, &.{- current_value[0..start],- current_value[@min(end, value_len)..],- });- try self.setUserValue(new_value, frame);+ {+ const scratch = try frame.getArena(.small, "TextEntry.innerDelete");+ defer scratch.release();+ const new_value = try std.mem.concat(scratch.allocator(), u8, &.{+ current_value[0..start],+ current_value[@min(end, value_len)..],+ });+ try self.setUserValue(new_value, frame);+ }self._selection_start = start;self._selection_end = start;self._selection_direction = .none;try dispatchSelectionChangeEvent(self, frame);- try dispatchInputEvent(self, null, if (forward) "deleteContentForward" else "deleteContentBackward", frame);+ try dispatchInputEvent(self, null, input_type, frame);}// Collapses the selection to the end of the value. Unlikediff --git a/src/server/cdp/domains/input.zig b/src/server/cdp/domains/input.zigindex 14631ecce..ec48da09d 100644--- a/src/server/cdp/domains/input.zig+++ b/src/server/cdp/domains/input.zig@@ -1641,17 +1641,37 @@ test "cdp.input: dispatchKeyEvent text-less keyDown then char types once" {try ctx.processMessage(.{ .id = 8, .method = "Input.dispatchKeyEvent", .params = .{ .type = "keyUp", .key = "Enter", .code = "Enter" } });try ctx.expectSentResult(null, .{ .id = 8 });try testing.expect((try ls.local.compileAndRun("ta.value === 'one\\n' && window.taInput === 'insertLineBreak:null'", null)).isTrue());+}- // A readonly control keeps its value. Typed text still reaches- // beforeinput, as in Chrome; Backspace doesn't.+test "cdp.input: a readonly textarea fires beforeinput for typed text only" {+ 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();++ // As in Chrome, typed text reaches beforeinput before the readonly+ // control refuses it; Backspace and Enter fire no edit event._ = try ls.local.compileAndRun(- \\ta.readOnly = true; window.taInput = null; window.taBefore = [];- \\ta.addEventListener('beforeinput', (e) => window.taBefore.push(e.inputType));+ \\const ta = document.createElement('textarea');+ \\document.body.appendChild(ta);+ \\ta.value = 'ro';+ \\ta.readOnly = true;+ \\ta.focus();+ \\window.edits = [];+ \\for (const t of ['beforeinput', 'input']) ta.addEventListener(t, (e) => window.edits.push(t + ':' + e.inputType));, null);- try ctx.processMessage(.{ .id = 9, .method = "Input.dispatchKeyEvent", .params = .{ .type = "keyDown", .key = "x", .text = "x" } });- try ctx.processMessage(.{ .id = 10, .method = "Input.dispatchKeyEvent", .params = .{ .type = "keyDown", .key = "Backspace", .code = "Backspace" } });- try ctx.processMessage(.{ .id = 11, .method = "Input.insertText", .params = .{ .text = "y" } });- try testing.expect((try ls.local.compileAndRun("ta.value === 'one\\n' && window.taInput === null && window.taBefore.join() === 'insertText,insertText'", null)).isTrue());+ try ctx.processMessage(.{ .id = 1, .method = "Input.dispatchKeyEvent", .params = .{ .type = "keyDown", .key = "x", .text = "x" } });+ try ctx.processMessage(.{ .id = 2, .method = "Input.dispatchKeyEvent", .params = .{ .type = "keyDown", .key = "Backspace", .code = "Backspace" } });+ try ctx.processMessage(.{ .id = 3, .method = "Input.dispatchKeyEvent", .params = .{ .type = "keyDown", .key = "Enter", .code = "Enter", .text = "\r" } });+ try ctx.processMessage(.{ .id = 4, .method = "Input.insertText", .params = .{ .text = "y" } });+ try testing.expect((try ls.local.compileAndRun(+ \\ta.value === 'ro' && window.edits.join() === 'beforeinput:insertText,beforeinput:insertText'+ , null)).isTrue());}test "cdp.input: dispatchKeyEvent char honors keypress and beforeinput vetoes" {