Skip to content

Lightpanda: the headless browser designed for AI and automation

input: let text typed into a readonly control reach beforeinput

Adrià Arrufatfad5162eefa1
diff --git a/src/browser/frame/user_input.zig b/src/browser/frame/user_input.zig
index 5348cae4b..b2a2196da 100644
--- a/src/browser/frame/user_input.zig
+++ b/src/browser/frame/user_input.zig
@@ -996,7 +996,15 @@ pub const EditResult = enum { done, refused, cancelled };
/// control, cancellable through beforeinput.
pub fn applyEdit(frame: *Frame, ctl: anytype, edit: Edit, opts: struct { beforeinput: bool = true }) !EditResult {
const el = ctl.asElement();
- if (!ctl.acceptsTextEntry() or !acceptsEdit(el)) {
+ 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) {
@@ -1012,6 +1020,9 @@ pub fn applyEdit(frame: *Frame, ctl: anytype, edit: Edit, opts: struct { beforei
return .cancelled;
}
}
+ if (!editable) {
+ return .refused;
+ }
switch (edit) {
.insert, .replace => |text| try ctl.innerInsert(text, .text, frame),
.line_break => try ctl.innerInsert("\n", .line_break, frame),
diff --git a/src/server/cdp/domains/input.zig b/src/server/cdp/domains/input.zig
index d2ae6327f..14631ecce 100644
--- a/src/server/cdp/domains/input.zig
+++ b/src/server/cdp/domains/input.zig
@@ -1642,12 +1642,16 @@ test "cdp.input: dispatchKeyEvent text-less keyDown then char types once" {
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; the keys themselves still fire.
- _ = try ls.local.compileAndRun("ta.readOnly = true; window.taInput = null;", null);
+ // A readonly control keeps its value. Typed text still reaches
+ // beforeinput, as in Chrome; Backspace doesn't.
+ _ = try ls.local.compileAndRun(
+ \\ta.readOnly = true; window.taInput = null; window.taBefore = [];
+ \\ta.addEventListener('beforeinput', (e) => window.taBefore.push(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", null)).isTrue());
+ try testing.expect((try ls.local.compileAndRun("ta.value === 'one\\n' && window.taInput === null && window.taBefore.join() === 'insertText,insertText'", null)).isTrue());
}
test "cdp.input: dispatchKeyEvent char honors keypress and beforeinput vetoes" {