merge main, resolve conflicts with getInteractiveElements

This commit is contained in:
egrs
2026-03-10 09:25:12 +01:00
4 changed files with 688 additions and 0 deletions

View File

@@ -19,17 +19,20 @@
const std = @import("std");
const lp = @import("lightpanda");
const markdown = lp.markdown;
const interactive = lp.interactive;
const structured_data = lp.structured_data;
const Node = @import("../Node.zig");
pub fn processMessage(cmd: anytype) !void {
const action = std.meta.stringToEnum(enum {
getMarkdown,
getInteractiveElements,
getStructuredData,
}, cmd.input.action) orelse return error.UnknownMethod;
switch (action) {
.getMarkdown => return getMarkdown(cmd),
.getInteractiveElements => return getInteractiveElements(cmd),
.getStructuredData => return getStructuredData(cmd),
}
}
@@ -57,6 +60,35 @@ fn getMarkdown(cmd: anytype) !void {
}, .{});
}
fn getInteractiveElements(cmd: anytype) !void {
const Params = struct {
nodeId: ?Node.Id = null,
};
const params = (try cmd.params(Params)) orelse Params{};
const bc = cmd.browser_context orelse return error.NoBrowserContext;
const page = bc.session.currentPage() orelse return error.PageNotLoaded;
const root = if (params.nodeId) |nodeId|
(bc.node_registry.lookup_by_id.get(nodeId) orelse return error.InvalidNodeId).dom
else
page.document.asNode();
const elements = try interactive.collectInteractiveElements(root, cmd.arena, page);
// Register nodes so nodeIds are valid for subsequent CDP calls.
var node_ids: std.ArrayList(Node.Id) = try .initCapacity(cmd.arena, elements.len);
for (elements) |el| {
const registered = try bc.node_registry.register(el.node);
node_ids.appendAssumeCapacity(registered.id);
}
return cmd.sendResult(.{
.elements = elements,
.nodeIds = node_ids.items,
}, .{});
}
fn getStructuredData(cmd: anytype) !void {
const bc = cmd.browser_context orelse return error.NoBrowserContext;
const page = bc.session.currentPage() orelse return error.PageNotLoaded;
@@ -89,6 +121,23 @@ test "cdp.lp: getMarkdown" {
try testing.expect(result.get("markdown") != null);
}
test "cdp.lp: getInteractiveElements" {
var ctx = testing.context();
defer ctx.deinit();
const bc = try ctx.loadBrowserContext(.{});
_ = try bc.session.createPage();
try ctx.processMessage(.{
.id = 1,
.method = "LP.getInteractiveElements",
});
const result = ctx.client.?.sent.items[0].object.get("result").?.object;
try testing.expect(result.get("elements") != null);
try testing.expect(result.get("nodeIds") != null);
}
test "cdp.lp: getStructuredData" {
var ctx = testing.context();
defer ctx.deinit();

View File

@@ -42,6 +42,7 @@ pub fn processMessage(cmd: anytype) !void {
stopLoading,
close,
captureScreenshot,
getLayoutMetrics,
}, cmd.input.action) orelse return error.UnknownMethod;
switch (action) {
@@ -54,6 +55,7 @@ pub fn processMessage(cmd: anytype) !void {
.stopLoading => return cmd.sendResult(null, .{}),
.close => return close(cmd),
.captureScreenshot => return captureScreenshot(cmd),
.getLayoutMetrics => return getLayoutMetrics(cmd),
}
}
@@ -569,6 +571,58 @@ fn captureScreenshot(cmd: anytype) !void {
}, .{});
}
fn getLayoutMetrics(cmd: anytype) !void {
const width = 1920;
const height = 1080;
return cmd.sendResult(.{
.layoutViewport = .{
.pageX = 0,
.pageY = 0,
.clientWidth = width,
.clientHeight = height,
},
.visualViewport = .{
.offsetX = 0,
.offsetY = 0,
.pageX = 0,
.pageY = 0,
.clientWidth = width,
.clientHeight = height,
.scale = 1,
.zoom = 1,
},
.contentSize = .{
.x = 0,
.y = 0,
.width = width,
.height = height,
},
.cssLayoutViewport = .{
.pageX = 0,
.pageY = 0,
.clientWidth = width,
.clientHeight = height,
},
.cssVisualViewport = .{
.offsetX = 0,
.offsetY = 0,
.pageX = 0,
.pageY = 0,
.clientWidth = width,
.clientHeight = height,
.scale = 1,
.zoom = 1,
},
.cssContentSize = .{
.x = 0,
.y = 0,
.width = width,
.height = height,
},
}, .{});
}
const testing = @import("../testing.zig");
test "cdp.page: getFrameTree" {
var ctx = testing.context();
@@ -618,3 +672,61 @@ test "cdp.page: captureScreenshot" {
}, .{ .id = 11 });
}
}
test "cdp.page: getLayoutMetrics" {
var ctx = testing.context();
defer ctx.deinit();
_ = try ctx.loadBrowserContext(.{ .id = "BID-9", .url = "hi.html", .target_id = "FID-000000000X".* });
const width = 1920;
const height = 1080;
try ctx.processMessage(.{ .id = 12, .method = "Page.getLayoutMetrics" });
try ctx.expectSentResult(.{
.layoutViewport = .{
.pageX = 0,
.pageY = 0,
.clientWidth = width,
.clientHeight = height,
},
.visualViewport = .{
.offsetX = 0,
.offsetY = 0,
.pageX = 0,
.pageY = 0,
.clientWidth = width,
.clientHeight = height,
.scale = 1,
.zoom = 1,
},
.contentSize = .{
.x = 0,
.y = 0,
.width = width,
.height = height,
},
.cssLayoutViewport = .{
.pageX = 0,
.pageY = 0,
.clientWidth = width,
.clientHeight = height,
},
.cssVisualViewport = .{
.offsetX = 0,
.offsetY = 0,
.pageX = 0,
.pageY = 0,
.clientWidth = width,
.clientHeight = height,
.scale = 1,
.zoom = 1,
},
.cssContentSize = .{
.x = 0,
.y = 0,
.width = width,
.height = height,
},
}, .{ .id = 12 });
}