Skip to content

Lightpanda: the headless browser designed for AI and automation

input: type text in a char step, not in the keydown default action

Adrià Arrufat74bf43e7321b
diff --git a/src/browser/actions.zig b/src/browser/actions.zig
index b62016442..c1b295b7b 100644
--- a/src/browser/actions.zig
+++ b/src/browser/actions.zig
@@ -143,7 +143,10 @@ pub fn press(node: ?*DOMNode, key: []const u8, frame: *Frame) !void {
.key = canonical,
}, frame);
- const prevented = frame._event_manager.dispatchCancelable(target, keydown_event.asEvent()) catch |err| {
+ const prevented = (if (target_el) |el|
+ Frame.user_input.pressKey(frame, el, keydown_event, Frame.user_input.textForKey(keydown_event))
+ else
+ frame._event_manager.dispatchCancelable(target, keydown_event.asEvent())) catch |err| {
lp.log.err(.app, "press keydown failed", .{ .err = err });
return error.ActionFailed;
};
diff --git a/src/browser/frame/user_input.zig b/src/browser/frame/user_input.zig
index e150f3c28..507972949 100644
--- a/src/browser/frame/user_input.zig
+++ b/src/browser/frame/user_input.zig
@@ -624,26 +624,106 @@ fn followLink(frame: *Frame, target: *Node, element: *Element, href: []const u8,
}, .{ .anchor = target_frame });
}
-pub fn triggerKeyboard(frame: *Frame, keyboard_event: *KeyboardEvent) !void {
- const event = keyboard_event.asEvent();
- // Dispatch to the effective active element. When nothing is explicitly
- // focused this resolves to <body> (matching `document.activeElement`), so
- // the keydown still fires and its default action — e.g. sequential focus
- // navigation on Tab — can run.
+/// pressKey on the focused element.
+pub fn triggerKeyDown(frame: *Frame, keydown: *KeyboardEvent, text: ?[]const u8) !bool {
+ const element = focusedElement(frame, keydown) orelse return false;
+ return pressKey(frame, element, keydown, text);
+}
+
+pub fn triggerKeyUp(frame: *Frame, keyup: *KeyboardEvent) !void {
+ const element = focusedElement(frame, keyup) orelse return;
+ try frame._event_manager.dispatch(element.asEventTarget(), keyup.asEvent());
+}
+
+// `document.activeElement`, so with nothing focused a key still fires on
+// <body> and Tab's focus navigation can run.
+fn focusedElement(frame: *Frame, keyboard_event: *KeyboardEvent) ?*Element {
const element = frame.window._document.getActiveElement() orelse {
- event.deinit(frame.page);
- return;
+ keyboard_event.asEvent().deinit(frame.page);
+ return null;
};
-
if (comptime lp.IS_DEBUG) {
- log.debug(.frame, "frame keydown", .{
+ log.debug(.frame, "frame key", .{
.url = frame.url,
.node = element,
.key = keyboard_event._key,
.type = frame._type,
});
}
- try frame._event_manager.dispatch(element.asEventTarget(), event);
+ return element;
+}
+
+/// Dispatches a trusted keydown on `target` then, unless cancelled, types
+/// `text` (Chrome's WebKeyboardEvent.text; null when the client sends the
+/// char as its own event, as chromedp does). Returns whether the keydown was
+/// cancelled.
+pub fn pressKey(frame: *Frame, target: *Element, keydown: *KeyboardEvent, text: ?[]const u8) !bool {
+ const event = keydown.asEvent();
+ // dispatch drops the event; keypressFor still needs it.
+ event.acquireRef();
+ defer event.releaseRef(frame.page);
+
+ if (try frame._event_manager.dispatchCancelable(target.asEventTarget(), event)) {
+ return true;
+ }
+ const t = text orelse return false;
+ // logged like a default action's failure, not the key event's
+ typeChar(frame, target, try keypressFor(frame, keydown), t) catch |err| {
+ log.warn(.frame, "frame.keypress", .{ .err = err });
+ };
+ return false;
+}
+
+/// The text a key press produces, following Chrome's WebKeyboardEvent.text: the
+/// key itself when printable, "\r" for Enter, nothing when ctrl/meta turn the
+/// press into a shortcut.
+pub fn textForKey(keyboard_event: *const KeyboardEvent) ?[]const u8 {
+ if (keyboard_event.getCtrlKey() or keyboard_event.getMetaKey()) {
+ return null;
+ }
+ const key = keyboard_event.getKey();
+ if (key == .Enter) {
+ return "\r";
+ }
+ return if (key.isPrintable()) key.asString() else null;
+}
+
+/// The char half of a key press (Chrome's WebInputEvent::kChar): fires
+/// `keypress` on `target` and, unless a listener cancels it, performs the
+/// text edit it stands for.
+pub fn typeChar(frame: *Frame, target: *Element, keypress: *KeyboardEvent, text: []const u8) !void {
+ if (try frame._event_manager.dispatchCancelable(target.asEventTarget(), keypress.asEvent())) {
+ return;
+ }
+ const is_enter = text.len == 1 and (text[0] == '\r' or text[0] == '\n');
+
+ if (target.is(Element.Html.Input)) |input| {
+ if (is_enter) {
+ return frame.submitForm(input.asElement(), input.getForm(frame), .{});
+ }
+ return insertInto(frame, input, text);
+ }
+
+ if (target.is(Element.Html.TextArea)) |textarea| {
+ if (is_enter) {
+ if (try allowEdit(frame, textarea.asElement(), null, "\n", "insertLineBreak")) {
+ try textarea.innerInsert("\n", frame);
+ }
+ return;
+ }
+ return insertInto(frame, textarea, text);
+ }
+}
+
+/// The keypress mirroring `keydown`'s key and modifiers.
+fn keypressFor(frame: *Frame, keydown: *const KeyboardEvent) !*KeyboardEvent {
+ return KeyboardEvent.initTrusted(comptime .wrap("keypress"), .{
+ .key = keydown.getKey().asString(),
+ .ctrlKey = keydown.getCtrlKey(),
+ .shiftKey = keydown.getShiftKey(),
+ .altKey = keydown.getAltKey(),
+ .metaKey = keydown.getMetaKey(),
+ }, frame);
}
pub fn handleKeydown(frame: *Frame, target: *Node, event: *Event) !void {
@@ -659,30 +739,16 @@ pub fn handleKeydown(frame: *Frame, target: *Node, event: *Event) !void {
return moveFocus(frame, keyboard_event.getShiftKey() == false);
}
- if (event.getIsTrusted()) {
- if ((key.isPrintable() or key == .Enter) and keyboard_event.getCtrlKey() == false and keyboard_event.getMetaKey() == false) {
- // Fire a keypress for a printable (or Enter) keydown when ctrl/meta
- // aren't pressed
- if (try dispatchKeypress(frame, target, keyboard_event)) {
- return;
- }
- }
-
- if (key == .Enter) {
- if (target.is(Element)) |element| {
- if (enterActivates(element)) {
- // Enter generates a button-like "click" for some elements
- return dispatchKeyboardClick(frame, element);
- }
+ if (key == .Enter and event.getIsTrusted()) {
+ if (target.is(Element)) |element| {
+ if (enterActivates(element)) {
+ // Enter generates a button-like "click" for some elements
+ return dispatchKeyboardClick(frame, element);
}
}
}
if (target.is(Element.Html.Input)) |input| {
- if (key == .Enter) {
- return frame.submitForm(input.asElement(), input.getForm(frame), .{});
- }
-
// Don't handle text input for radio/checkbox
const input_type = input._input_type;
if (input_type == .radio or input_type == .checkbox) {
@@ -693,21 +759,13 @@ pub fn handleKeydown(frame: *Frame, target: *Node, event: *Event) !void {
}
if (target.is(Element.Html.TextArea)) |textarea| {
- if (key == .Enter) {
- if (try allowEdit(frame, event, textarea.asElement(), null, "\n", "insertLineBreak")) {
- try textarea.innerInsert("\n", frame);
- }
- return;
- }
-
return editKey(frame, keyboard_event, textarea, key);
}
}
-// edit keys are handled by Input and TextArea the same
+// edit keys other than text insertion (typeChar's) are handled by Input and
+// TextArea the same
fn editKey(frame: *Frame, keyboard_event: *KeyboardEvent, ctl: anytype, key: KeyboardEvent.Key) !void {
- const event = keyboard_event.asEvent();
-
if (caretMove(key, ctl)) |move| {
// Word/paragraph motions (ctrl/alt/meta variants) aren't modeled.
if (keyboard_event.getCtrlKey() or keyboard_event.getAltKey() or keyboard_event.getMetaKey()) {
@@ -718,17 +776,22 @@ fn editKey(frame: *Frame, keyboard_event: *KeyboardEvent, ctl: anytype, key: Key
if (key == .Backspace or key == .Delete) {
const forward = key == .Delete;
- if (try allowEdit(frame, event, ctl.asElement(), null, null, deleteInputType(forward))) {
+ if (!keyboard_event.asEvent().getIsTrusted() or try allowEdit(frame, ctl.asElement(), null, null, deleteInputType(forward))) {
try ctl.innerDelete(forward, frame);
}
- return;
}
+}
- if (key.isPrintable()) {
- if (try allowEdit(frame, event, ctl.asElement(), key.asString(), key.asString(), "insertText")) {
- try ctl.innerInsert(key.asString(), frame);
+fn insertInto(frame: *Frame, ctl: anytype, text: []const u8) !void {
+ if (@TypeOf(ctl) == *Element.Html.Input) {
+ const input_type = ctl._input_type;
+ if (input_type == .radio or input_type == .checkbox) {
+ return;
}
}
+ if (try allowEdit(frame, ctl.asElement(), text, text, "insertText")) {
+ try ctl.innerInsert(text, frame);
+ }
}
// Caret movement a key's default action performs on `ctl`, if any. On a
@@ -750,15 +813,9 @@ fn deleteInputType(forward: bool) []const u8 {
return if (forward) "deleteContentForward" else "deleteContentBackward";
}
-// pre-edit events for a key's default action, can cancel the edit (i.e. by
-// returning false)
-fn allowEdit(frame: *Frame, keydown: *Event, target: *Element, before_data: ?[]const u8, text_data: ?[]const u8, input_type: []const u8) !bool {
- if (keydown.getIsTrusted() == false) {
- // only trusted events fire these events, so for a untrusted event, the
- // edit isn't cancelled.
- return true;
- }
-
+// pre-edit events for a trusted key's default action, can cancel the edit
+// (i.e. by returning false)
+fn allowEdit(frame: *Frame, target: *Element, before_data: ?[]const u8, text_data: ?[]const u8, input_type: []const u8) !bool {
{
const before = (try InputEvent.initTrusted(comptime .wrap("beforeinput"), .{
.bubbles = true,
@@ -808,23 +865,6 @@ pub fn handleKeyup(frame: *Frame, target: *Node, event: *Event) !void {
}
}
-// Dispatch keypress mirroring `keydown`'s key and modifiers; returns true when
-// a listener canceled it.
-fn dispatchKeypress(frame: *Frame, target: *Node, keydown: *KeyboardEvent) !bool {
- const event = (try KeyboardEvent.initTrusted(comptime .wrap("keypress"), .{
- .bubbles = true,
- .cancelable = true,
- .composed = true,
- .key = keydown.getKey().asString(),
- .ctrlKey = keydown.getCtrlKey(),
- .shiftKey = keydown.getShiftKey(),
- .altKey = keydown.getAltKey(),
- .metaKey = keydown.getMetaKey(),
- }, frame)).asEvent();
-
- return frame._event_manager.dispatchCancelable(target.asEventTarget(), event);
-}
-
// keydown+enter or keyup+space trigger this syntthetic pointer event (under
// specific conditions, see handleKeydown and handleKeyup).
fn dispatchKeyboardClick(frame: *Frame, element: *Element) !void {
@@ -951,21 +991,16 @@ fn focusOrderBefore(a: *Element, a_tab_index: i32, b: *Element, b_tab_index: i32
return a_tab_index < b_tab_index;
}
-// insertText is a shortcut to insert text into the active element.
+/// Text input without a key press (IME, paste): beforeinput but no keypress.
pub fn insertText(frame: *Frame, v: []const u8) !void {
const html_element = frame.document._active_element orelse return;
if (html_element.is(Element.Html.Input)) |input| {
- const input_type = input._input_type;
- if (input_type == .radio or input_type == .checkbox) {
- return;
- }
-
- return input.innerInsert(v, frame);
+ return insertInto(frame, input, v);
}
if (html_element.is(Element.Html.TextArea)) |textarea| {
- return textarea.innerInsert(v, frame);
+ return insertInto(frame, textarea, v);
}
}
diff --git a/src/browser/webapi/WebDriver.zig b/src/browser/webapi/WebDriver.zig
index bbd850573..e98b0acf9 100644
--- a/src/browser/webapi/WebDriver.zig
+++ b/src/browser/webapi/WebDriver.zig
@@ -426,12 +426,7 @@ fn performKeySource(source: js.Object, frame: *Frame) !void {
// Key actions have no explicit target; they go to the focused element,
// or the document if nothing is focused. Resolved per action since a
// key's default action can move focus.
- const target = if (frame.document._active_element) |el|
- el.asEventTarget()
- else
- frame.document.asNode().asEventTarget();
-
- dispatchKey(target, if (is_down) comptime .wrap("keydown") else comptime .wrap("keyup"), key, frame);
+ dispatchKey(frame.document._active_element, is_down, key, frame);
}
}
@@ -516,7 +511,8 @@ fn setModifier(modifiers: *Modifiers, key: []const u8, pressed: bool) void {
}
}
-fn dispatchKey(target: *EventTarget, typ: lp.String, key: []const u8, frame: *Frame) void {
+fn dispatchKey(focused: ?*Element, is_down: bool, key: []const u8, frame: *Frame) void {
+ const typ: lp.String = if (is_down) comptime .wrap("keydown") else comptime .wrap("keyup");
const modifiers = frame.page.input_modifiers;
const event = KeyboardEvent.initTrusted(typ, .{
.bubbles = true,
@@ -531,7 +527,13 @@ fn dispatchKey(target: *EventTarget, typ: lp.String, key: []const u8, frame: *Fr
log.warn(.app, "webdriver key event", .{ .err = err });
return;
};
- dispatch(target, event.asEvent(), frame, typ.str());
+ const el = focused orelse return dispatch(frame.document.asNode().asEventTarget(), event.asEvent(), frame, typ.str());
+ if (!is_down) {
+ return dispatch(el.asEventTarget(), event.asEvent(), frame, typ.str());
+ }
+ _ = Frame.user_input.pressKey(frame, el, event, Frame.user_input.textForKey(event)) catch |err| {
+ log.warn(.app, "webdriver dispatch", .{ .err = err, .type = typ.str() });
+ };
}
fn readI32(obj: js.Object, key: []const u8, default: i32) i32 {
diff --git a/src/browser/webapi/event/KeyboardEvent.zig b/src/browser/webapi/event/KeyboardEvent.zig
index cd90cb28c..570728c4e 100644
--- a/src/browser/webapi/event/KeyboardEvent.zig
+++ b/src/browser/webapi/event/KeyboardEvent.zig
@@ -289,7 +289,7 @@ const KeyboardEventOptions = struct {
shiftKey: bool = false,
};
-const Options = Event.inheritOptions(
+pub const Options = Event.inheritOptions(
KeyboardEvent,
KeyboardEventOptions,
);
diff --git a/src/server/bidi/input.zig b/src/server/bidi/input.zig
index b63cfef0a..07f9d94bf 100644
--- a/src/server/bidi/input.zig
+++ b/src/server/bidi/input.zig
@@ -625,7 +625,11 @@ fn dispatchKey(frame: *Frame, comptime typ: []const u8, info: *const KeyInfo, mo
.metaKey = modifiers.meta,
.shiftKey = modifiers.shift,
}, frame);
- try user_input.triggerKeyboard(frame, event);
+ if (comptime std.mem.eql(u8, typ, "keydown")) {
+ _ = try user_input.triggerKeyDown(frame, event, user_input.textForKey(event));
+ } else {
+ try user_input.triggerKeyUp(frame, event);
+ }
}
fn setModifier(modifiers: *Modifiers, which: ?Modifier, down: bool) void {
diff --git a/src/server/cdp/CDP.zig b/src/server/cdp/CDP.zig
index 42772309d..24739794f 100644
--- a/src/server/cdp/CDP.zig
+++ b/src/server/cdp/CDP.zig
@@ -459,6 +459,10 @@ pub const BrowserContext = struct {
session_id: ?[]const u8,
attached_sessions: std.ArrayList(AttachedSession) = .empty,
+ // A cancelled text-less keyDown drops the char message that follows it
+ // (chromedp's keyDown/char/keyUp split), as Chrome does.
+ suppress_next_char: bool = false,
+
security_origin: []const u8,
page_life_cycle_events: bool,
secure_context_type: []const u8,
diff --git a/src/server/cdp/domains/input.zig b/src/server/cdp/domains/input.zig
index 7900911a9..2821a0a9e 100644
--- a/src/server/cdp/domains/input.zig
+++ b/src/server/cdp/domains/input.zig
@@ -43,7 +43,7 @@ fn dispatchKeyEvent(cmd: *CDP.Command) !void {
key: []const u8 = "",
code: ?[]const u8 = null,
modifiers: u4 = 0,
- text: ?[]const u8 = null,
+ text: []const u8 = "",
// Many optional parameters are not implemented yet, see documentation url.
const Type = enum {
@@ -56,35 +56,41 @@ fn dispatchKeyEvent(cmd: *CDP.Command) !void {
try cmd.sendResult(null, .{});
- // rawKeyDown is a Chrome-internal event type not used for JS dispatch
- if (params.type == .rawKeyDown) return;
-
const bc = cmd.browser_context orelse return;
const frame = bc.mainFrame() orelse return;
- const key_str = if (params.key.len > 0) params.key else params.text orelse "";
+ // Chrome types text only for an event carrying it: a keyDown with `text`
+ // (Puppeteer, Playwright) or a `char` (chromedp, after a text-less keyDown).
+ const text: ?[]const u8 = if (params.text.len == 0) null else params.text;
const KeyboardEvent = @import("../../../browser/webapi/event/KeyboardEvent.zig");
- const keyboard_event = try KeyboardEvent.initTrusted(switch (params.type) {
- .keyDown => comptime .wrap("keydown"),
- .keyUp => comptime .wrap("keyup"),
- .char => comptime .wrap("keypress"),
- .rawKeyDown => unreachable,
- }, .{
- .key = key_str,
+ const opts: KeyboardEvent.Options = .{
+ .key = if (params.key.len > 0) params.key else params.text,
.code = params.code,
.altKey = params.modifiers & 1 == 1,
.ctrlKey = params.modifiers & 2 == 2,
.metaKey = params.modifiers & 4 == 4,
.shiftKey = params.modifiers & 8 == 8,
- }, frame);
- try Frame.user_input.triggerKeyboard(frame, keyboard_event);
+ };
- if (params.type == .char and !keyboard_event.asEvent().getDefaultPrevented()) {
- if (params.text) |text| {
- if (text.len > 0) {
- try Frame.user_input.insertText(frame, text);
+ switch (params.type) {
+ // rawKeyDown is a Chrome-internal event type not used for JS dispatch
+ .rawKeyDown => {},
+ .keyDown => {
+ const event = try KeyboardEvent.initTrusted(comptime .wrap("keydown"), opts, frame);
+ const prevented = try Frame.user_input.triggerKeyDown(frame, event, text);
+ bc.suppress_next_char = prevented and text == null;
+ },
+ .keyUp => try Frame.user_input.triggerKeyUp(frame, try KeyboardEvent.initTrusted(comptime .wrap("keyup"), opts, frame)),
+ .char => {
+ const t = text orelse return;
+ if (bc.suppress_next_char) {
+ bc.suppress_next_char = false;
+ return;
}
- }
+ const target = frame.window._document.getActiveElement() orelse return;
+ const event = try KeyboardEvent.initTrusted(comptime .wrap("keypress"), opts, frame);
+ try Frame.user_input.typeChar(frame, target, event, t);
+ },
}
}
@@ -791,18 +797,13 @@ test "cdp.input: dispatchKeyEvent caret movement keys move the text entry cursor
}
}
-test "cdp.input: dispatchKeyEvent char text insertion" {
+// chromedp's SendKeys shape: a text-less keyDown, the char with the text, keyUp.
+test "cdp.input: dispatchKeyEvent text-less keyDown then char types once" {
var ctx = try testing.context();
defer ctx.deinit();
- const bc = try ctx.loadBrowserContext(.{});
- const page = try bc.session.createPage();
- const frame = page.frame().?;
- try frame.navigate("http://localhost:9582/src/browser/tests/mcp_actions.html", .{
- .reason = .address_bar,
- .kind = .{ .push = null },
- });
- try testing.waitForPage(bc);
+ const bc = try ctx.loadBrowserContext(.{ .url = "mcp_actions.html" });
+ const frame = bc.mainFrame().?;
var ls: lp.js.Local.Scope = undefined;
frame.js.localScope(&ls);
@@ -813,56 +814,96 @@ test "cdp.input: dispatchKeyEvent char text insertion" {
\\inp.value = '';
\\inp.focus();
\\window.events = [];
- \\inp.addEventListener('keypress', (e) => window.events.push(e.key));
- \\inp.addEventListener('input', (e) => window.events.push(inp.value));
+ \\for (const t of ['keydown', 'keypress', 'input', 'keyup']) {
+ \\ inp.addEventListener(t, (e) => window.events.push(t + ':' + (e.key ?? inp.value)));
+ \\}
, null);
- // Text is inserted and dispatches keypress and input.
- try ctx.processMessage(.{
- .id = 1,
- .method = "Input.dispatchKeyEvent",
- .params = .{ .type = "char", .key = "h", .text = "h" },
- });
+ try ctx.processMessage(.{ .id = 1, .method = "Input.dispatchKeyEvent", .params = .{ .type = "keyDown", .key = "h", .code = "KeyH" } });
try ctx.expectSentResult(null, .{ .id = 1 });
+ try testing.expect((try ls.local.compileAndRun("inp.value === '' && window.events.join(',') === 'keydown:h'", null)).isTrue());
+
+ try ctx.processMessage(.{ .id = 2, .method = "Input.dispatchKeyEvent", .params = .{ .type = "char", .key = "h", .text = "h" } });
+ try ctx.expectSentResult(null, .{ .id = 2 });
+ try ctx.processMessage(.{ .id = 3, .method = "Input.dispatchKeyEvent", .params = .{ .type = "keyUp", .key = "h", .code = "KeyH" } });
+ try ctx.expectSentResult(null, .{ .id = 3 });
try testing.expect((try ls.local.compileAndRun(
- \\inp.value === 'h' && window.events.join(',') === 'h,h'
+ \\inp.value === 'h' && window.events.join(',') === 'keydown:h,keypress:h,input:h,keyup:h'
, null)).isTrue());
- // A char event without a text field (e.g. Enter) must not insert its key name.
- try ctx.processMessage(.{
- .id = 2,
- .method = "Input.dispatchKeyEvent",
- .params = .{ .type = "char", .key = "Enter" },
- });
- try ctx.expectSentResult(null, .{ .id = 2 });
- try testing.expect((try ls.local.compileAndRun("inp.value === 'h'", null)).isTrue());
+ // A char without text has nothing to type.
+ try ctx.processMessage(.{ .id = 4, .method = "Input.dispatchKeyEvent", .params = .{ .type = "char", .key = "Enter" } });
+ try ctx.expectSentResult(null, .{ .id = 4 });
+ try testing.expect((try ls.local.compileAndRun("inp.value === 'h' && window.events.length === 4", null)).isTrue());
- // With preventDefault on keypress, no character is inserted.
+ // A keyDown with text but no key (Puppeteer's shape for some keys) types it.
+ try ctx.processMessage(.{ .id = 5, .method = "Input.dispatchKeyEvent", .params = .{ .type = "keyDown", .text = "z" } });
+ try ctx.expectSentResult(null, .{ .id = 5 });
+ try testing.expect((try ls.local.compileAndRun("inp.value === 'hz'", null)).isTrue());
+
+ // Enter's char is "\r": a line break in a <textarea>, never a literal "\r".
_ = try ls.local.compileAndRun(
- \\inp.addEventListener('keypress', (e) => { if (e.key === 'x') e.preventDefault(); });
+ \\const ta = document.createElement('textarea');
+ \\document.body.appendChild(ta);
+ \\ta.value = 'one';
+ \\ta.focus();
, null);
+ try ctx.processMessage(.{ .id = 6, .method = "Input.dispatchKeyEvent", .params = .{ .type = "keyDown", .key = "Enter", .code = "Enter" } });
+ try ctx.expectSentResult(null, .{ .id = 6 });
+ try ctx.processMessage(.{ .id = 7, .method = "Input.dispatchKeyEvent", .params = .{ .type = "char", .key = "Enter", .text = "\r" } });
+ try ctx.expectSentResult(null, .{ .id = 7 });
+ 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'", null)).isTrue());
+}
- try ctx.processMessage(.{
- .id = 3,
- .method = "Input.dispatchKeyEvent",
- .params = .{ .type = "char", .key = "x", .text = "x" },
- });
- try ctx.expectSentResult(null, .{ .id = 3 });
- try testing.expect((try ls.local.compileAndRun("inp.value === 'h'", null)).isTrue());
+test "cdp.input: dispatchKeyEvent char honors keypress and beforeinput vetoes" {
+ 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();
+
+ // keypress fires before beforeinput, so it sees the digits the latter vetoes.
+ _ = try ls.local.compileAndRun(
+ \\const inp = document.getElementById('inp');
+ \\inp.value = '';
+ \\inp.focus();
+ \\window.keypresses = [];
+ \\inp.addEventListener('keypress', (e) => {
+ \\ window.keypresses.push(e.key);
+ \\ if (e.key === 'x') e.preventDefault();
+ \\});
+ \\inp.addEventListener('beforeinput', (e) => {
+ \\ if (/[0-9]/.test(e.data)) e.preventDefault();
+ \\});
+ , null);
+
+ var id: u32 = 1;
+ for ("a1x2b") |c| {
+ const key: []const u8 = &.{c};
+ try ctx.processMessage(.{ .id = id, .method = "Input.dispatchKeyEvent", .params = .{ .type = "keyDown", .key = key } });
+ try ctx.expectSentResult(null, .{ .id = id });
+ id += 1;
+ try ctx.processMessage(.{ .id = id, .method = "Input.dispatchKeyEvent", .params = .{ .type = "char", .key = key, .text = key } });
+ try ctx.expectSentResult(null, .{ .id = id });
+ id += 1;
+ }
+ try testing.expect((try ls.local.compileAndRun(
+ \\inp.value === 'ab' && window.keypresses.join('') === 'a1x2b'
+ , null)).isTrue());
}
-test "cdp.input: dispatchKeyEvent keyDown text-only fallback" {
+test "cdp.input: dispatchKeyEvent cancelled keyDown suppresses its char" {
var ctx = try testing.context();
defer ctx.deinit();
- const bc = try ctx.loadBrowserContext(.{});
- const page = try bc.session.createPage();
- const frame = page.frame().?;
- try frame.navigate("http://localhost:9582/src/browser/tests/mcp_actions.html", .{
- .reason = .address_bar,
- .kind = .{ .push = null },
- });
- try testing.waitForPage(bc);
+ const bc = try ctx.loadBrowserContext(.{ .url = "mcp_actions.html" });
+ const frame = bc.mainFrame().?;
var ls: lp.js.Local.Scope = undefined;
frame.js.localScope(&ls);
@@ -872,14 +913,24 @@ test "cdp.input: dispatchKeyEvent keyDown text-only fallback" {
\\const inp = document.getElementById('inp');
\\inp.value = '';
\\inp.focus();
+ \\window.keypresses = [];
+ \\inp.addEventListener('keydown', (e) => { if (e.key === 'x') e.preventDefault(); });
+ \\inp.addEventListener('keypress', (e) => window.keypresses.push(e.key));
, null);
- // When keyDown omits `key`, `text` is used as the key and inserted.
- try ctx.processMessage(.{
- .id = 1,
- .method = "Input.dispatchKeyEvent",
- .params = .{ .type = "keyDown", .text = "z" },
- });
+ // The char in the same message (keyDown with text) and in the next one
+ // (chromedp) are both dropped.
+ try ctx.processMessage(.{ .id = 1, .method = "Input.dispatchKeyEvent", .params = .{ .type = "keyDown", .key = "x", .text = "x" } });
try ctx.expectSentResult(null, .{ .id = 1 });
- try testing.expect((try ls.local.compileAndRun("inp.value === 'z'", null)).isTrue());
+ try ctx.processMessage(.{ .id = 2, .method = "Input.dispatchKeyEvent", .params = .{ .type = "keyDown", .key = "x" } });
+ try ctx.expectSentResult(null, .{ .id = 2 });
+ try ctx.processMessage(.{ .id = 3, .method = "Input.dispatchKeyEvent", .params = .{ .type = "char", .key = "x", .text = "x" } });
+ try ctx.expectSentResult(null, .{ .id = 3 });
+ try testing.expect((try ls.local.compileAndRun("inp.value === '' && window.keypresses.length === 0", null)).isTrue());
+
+ try ctx.processMessage(.{ .id = 4, .method = "Input.dispatchKeyEvent", .params = .{ .type = "keyDown", .key = "y" } });
+ try ctx.expectSentResult(null, .{ .id = 4 });
+ try ctx.processMessage(.{ .id = 5, .method = "Input.dispatchKeyEvent", .params = .{ .type = "char", .key = "y", .text = "y" } });
+ try ctx.expectSentResult(null, .{ .id = 5 });
+ try testing.expect((try ls.local.compileAndRun("inp.value === 'y' && window.keypresses.join('') === 'y'", null)).isTrue());
}