Lightpanda: the headless browser designed for AI and automation
cdp: window.devicePixelRatio from viewport scale and Input char text insertion
Adrià Arrufatfb95e8b39e98
diff --git a/src/browser/Viewport.zig b/src/browser/Viewport.zigindex 06c220975..7537d7900 100644--- a/src/browser/Viewport.zig+++ b/src/browser/Viewport.zig@@ -20,7 +20,7 @@ const Viewport = @This();width: u32,height: u32,-scale: f32 = 1.0, // for screenshot raster+scale: f32 = 1.0, // for screenshot raster and window.devicePixelRatio// window.screen dimensions; null means the same as the viewport.screen_width: ?u32 = null,screen_height: ?u32 = null,diff --git a/src/browser/webapi/Window.zig b/src/browser/webapi/Window.zigindex f4f8a7838..c8ef3d7dd 100644--- a/src/browser/webapi/Window.zig+++ b/src/browser/webapi/Window.zig@@ -222,6 +222,10 @@ fn setInnerHeight(self: *Window, value: js.Value) void {self.replaceGlobalProperty(value, "innerHeight");}+fn setDevicePixelRatio(self: *Window, value: js.Value) void {+ self.replaceGlobalProperty(value, "devicePixelRatio");+}+fn setScrollX(self: *Window, value: js.Value) void {self.replaceGlobalProperty(value, "scrollX");}@@ -923,6 +927,10 @@ pub fn getInnerHeight(_: *const Window, frame: *Frame) u32 {return frame.page.getViewport().height;}+fn getDevicePixelRatio(_: *const Window, frame: *Frame) f32 {+ return frame.page.getViewport().scale;+}+pub fn scrollTo(self: *Window, opts: Element.ScrollToOpts, y: ?i32, frame: *Frame) !void {const o = opts.offsets(y);const new_x: u32 = if (o.left) |left| @intCast(@max(0, left)) else self._scroll_pos.x;@@ -1227,7 +1235,7 @@ pub const JsApi = struct {// the attribute rather than throwing.pub const innerWidth = bridge.accessor(Window.getInnerWidth, Window.setInnerWidth, .{});pub const innerHeight = bridge.accessor(Window.getInnerHeight, Window.setInnerHeight, .{});- pub const devicePixelRatio = bridge.property(1, .{ .template = false, .readonly = false });+ pub const devicePixelRatio = bridge.accessor(Window.getDevicePixelRatio, Window.setDevicePixelRatio, .{});pub const opener = bridge.accessor(Window.getOpener, Window.setOpener, .{});pub const closed = bridge.accessor(Window.getClosed, null, .{});diff --git a/src/server/cdp/domains/emulation.zig b/src/server/cdp/domains/emulation.zigindex a12510a60..757a27e30 100644--- a/src/server/cdp/domains/emulation.zig+++ b/src/server/cdp/domains/emulation.zig@@ -310,10 +310,10 @@ test "cdp.Emulation: setDeviceMetricsOverride screenWidth/screenHeight reach wintry ctx.processMessage(.{.id = 1,.method = "Emulation.setDeviceMetricsOverride",- .params = .{ .width = 1280, .height = 720, .deviceScaleFactor = 1, .mobile = false, .screenWidth = 2560, .screenHeight = 1440 },+ .params = .{ .width = 1280, .height = 720, .deviceScaleFactor = 2, .mobile = false, .screenWidth = 2560, .screenHeight = 1440 },});try ctx.expectSentResult(null, .{ .id = 1 });- var v = try ls.local.exec("screen.width === 2560 && screen.height === 1440 && innerWidth === 1280 && innerHeight === 720", null);+ var v = try ls.local.exec("screen.width === 2560 && screen.height === 1440 && innerWidth === 1280 && innerHeight === 720 && devicePixelRatio === 2", null);try testing.expect(v.toBool());// 0 keeps the current value, as for width/height.@@ -322,11 +322,11 @@ test "cdp.Emulation: setDeviceMetricsOverride screenWidth/screenHeight reach win.method = "Emulation.setDeviceMetricsOverride",.params = .{ .width = 1024, .height = 0, .deviceScaleFactor = 0, .mobile = false, .screenWidth = 0, .screenHeight = 0 },});- v = try ls.local.exec("screen.width === 2560 && screen.height === 1440 && innerWidth === 1024 && innerHeight === 720", null);+ v = try ls.local.exec("screen.width === 2560 && screen.height === 1440 && innerWidth === 1024 && innerHeight === 720 && devicePixelRatio === 2", null);try testing.expect(v.toBool());try ctx.processMessage(.{ .id = 3, .method = "Emulation.clearDeviceMetricsOverride" });- v = try ls.local.exec("screen.width === innerWidth && screen.height === innerHeight", null);+ v = try ls.local.exec("screen.width === innerWidth && screen.height === innerHeight && devicePixelRatio === 1", null);try testing.expect(v.toBool());}diff --git a/src/server/cdp/domains/input.zig b/src/server/cdp/domains/input.zigindex 0b0299571..c10a801c9 100644--- a/src/server/cdp/domains/input.zig+++ b/src/server/cdp/domains/input.zig@@ -43,6 +43,7 @@ fn dispatchKeyEvent(cmd: *CDP.Command) !void {key: []const u8 = "",code: ?[]const u8 = null,modifiers: u4 = 0,+ text: ?[]const u8 = null,// Many optional parameters are not implemented yet, see documentation url.const Type = enum {@@ -61,6 +62,7 @@ fn dispatchKeyEvent(cmd: *CDP.Command) !void {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 "";const KeyboardEvent = @import("../../../browser/webapi/event/KeyboardEvent.zig");const keyboard_event = try KeyboardEvent.initTrusted(switch (params.type) {.keyDown => comptime .wrap("keydown"),@@ -68,7 +70,7 @@ fn dispatchKeyEvent(cmd: *CDP.Command) !void {.char => comptime .wrap("keypress"),.rawKeyDown => unreachable,}, .{- .key = params.key,+ .key = key_str,.code = params.code,.altKey = params.modifiers & 1 == 1,.ctrlKey = params.modifiers & 2 == 2,@@ -76,7 +78,14 @@ fn dispatchKeyEvent(cmd: *CDP.Command) !void {.shiftKey = params.modifiers & 8 == 8,}, frame);try Frame.user_input.triggerKeyboard(frame, keyboard_event);- // result already sent++ if (params.type == .char and !keyboard_event.asEvent().getDefaultPrevented()) {+ if (params.text) |text| {+ if (text.len > 0) {+ try Frame.user_input.insertText(frame, text);+ }+ }+ }}// https://chromedevtools.github.io/devtools-protocol/tot/Input/#method-dispatchMouseEvent@@ -781,3 +790,79 @@ test "cdp.input: dispatchKeyEvent caret movement keys move the text entry cursortry testing.expectEqualSlices(u8, step.expect, got);}}++test "cdp.input: dispatchKeyEvent char fires keypress and inserts text" {+ 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);++ var ls: lp.js.Local.Scope = undefined;+ frame.js.localScope(&ls);+ defer ls.deinit();++ _ = try ls.local.compileAndRun(+ \\const inp = document.getElementById('inp');+ \\inp.value = '';+ \\inp.focus();+ \\window.events = [];+ \\inp.addEventListener('keypress', (e) => window.events.push('keypress:' + e.key));+ \\inp.addEventListener('input', (e) => window.events.push('input:' + inp.value));+ , null);++ try ctx.processMessage(.{+ .id = 1,+ .method = "Input.dispatchKeyEvent",+ .params = .{ .type = "char", .key = "h", .text = "h" },+ });+ try ctx.expectSentResult(null, .{ .id = 1 });++ try testing.expect((try ls.local.compileAndRun(+ \\inp.value === 'h' && window.events.join(',') === 'keypress:h,input:h'+ , null)).isTrue());++ // With preventDefault on keypress, no character is inserted.+ _ = try ls.local.compileAndRun(+ \\inp.addEventListener('keypress', (e) => { if (e.key === 'x') e.preventDefault(); });+ , null);++ try ctx.processMessage(.{+ .id = 2,+ .method = "Input.dispatchKeyEvent",+ .params = .{ .type = "char", .key = "x", .text = "x" },+ });+ try ctx.expectSentResult(null, .{ .id = 2 });++ try testing.expect((try ls.local.compileAndRun(+ \\inp.value === 'h' && window.events.join(',') === 'keypress:h,input:h,keypress:x'+ , null)).isTrue());++ // A `char` event without a `text` field must not insert its `key`.+ try ctx.processMessage(.{+ .id = 3,+ .method = "Input.dispatchKeyEvent",+ .params = .{ .type = "char", .key = "Enter" },+ });+ try ctx.expectSentResult(null, .{ .id = 3 });+ try testing.expect((try ls.local.compileAndRun(+ \\inp.value === 'h' && window.events.join(',') === 'keypress:h,input:h,keypress:x,keypress:Enter'+ , null)).isTrue());++ // A `keyDown` sending only `text` falls back to using it as the key and inserts it.+ try ctx.processMessage(.{+ .id = 4,+ .method = "Input.dispatchKeyEvent",+ .params = .{ .type = "keyDown", .text = "z" },+ });+ try ctx.expectSentResult(null, .{ .id = 4 });+ try testing.expect((try ls.local.compileAndRun(+ \\inp.value === 'hz' && window.events.join(',') === 'keypress:h,input:h,keypress:x,keypress:Enter,keypress:z,input:hz'+ , null)).isTrue());+}