Skip to content

Resolve node #513

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/cdp/domains/dom.zig
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const std = @import("std");
const parser = @import("netsurf");
const Node = @import("../Node.zig");
const css = @import("../../dom/css.zig");
const dom_node = @import("../../dom/node.zig");

pub fn processMessage(cmd: anytype) !void {
const action = std.meta.stringToEnum(enum {
Expand All @@ -28,6 +29,7 @@ pub fn processMessage(cmd: anytype) !void {
performSearch,
getSearchResults,
discardSearchResults,
resolveNode,
}, cmd.input.action) orelse return error.UnknownMethod;

switch (action) {
Expand All @@ -36,6 +38,7 @@ pub fn processMessage(cmd: anytype) !void {
.performSearch => return performSearch(cmd),
.getSearchResults => return getSearchResults(cmd),
.discardSearchResults => return discardSearchResults(cmd),
.resolveNode => return resolveNode(cmd),
}
}

Expand Down Expand Up @@ -115,6 +118,36 @@ fn getSearchResults(cmd: anytype) !void {
return cmd.sendResult(.{ .nodeIds = node_ids[params.fromIndex..params.toIndex] }, .{});
}

fn resolveNode(cmd: anytype) !void {
const params = (try cmd.params(struct {
nodeId: ?Node.Id = null,
backendNodeId: ?u32 = null,
objectGroup: ?[]const u8 = null,
executionContextId: ?u32 = null,
})) orelse return error.InvalidParams;
if (params.nodeId == null or params.backendNodeId != null or params.executionContextId != null) {
return error.NotYetImplementedParams;
}

const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
const node = bc.node_registry.lookup_by_id.get(params.nodeId.?) orelse return error.UnknownNode;

// node._node is a *parser.Node we need this to be able to find its most derived type e.g. Node -> Element -> HTMLElement
// So we use the Node.Union when retrieve the value from the environment
const jsValue = try bc.session.env.findOrAddValue(try dom_node.Node.toInterface(node._node));
const remoteObject = try bc.session.inspector.getRemoteObject(&bc.session.env, jsValue, params.objectGroup orelse "");
defer remoteObject.deinit();

const arena = cmd.arena;
return cmd.sendResult(.{ .object = .{
.type = try remoteObject.getType(arena),
.subtype = try remoteObject.getSubtype(arena),
.className = try remoteObject.getClassName(arena),
.description = try remoteObject.getDescription(arena),
.objectId = try remoteObject.getObjectId(arena),
} }, .{});
}

const testing = @import("../testing.zig");

test "cdp.dom: getSearchResults unknown search id" {
Expand Down
52 changes: 52 additions & 0 deletions src/cdp/testing.zig
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ const Browser = struct {
self.session.?.* = .{
.page = null,
.arena = arena,
.env = Env{},
.inspector = Inspector{},
};
return self.session.?;
}
Expand All @@ -75,6 +77,8 @@ const Browser = struct {
const Session = struct {
page: ?Page = null,
arena: Allocator,
env: Env,
inspector: Inspector,

pub fn currentPage(self: *Session) ?*Page {
return &(self.page orelse return null);
Expand Down Expand Up @@ -102,6 +106,54 @@ const Session = struct {
}
};

const Env = struct {
pub fn findOrAddValue(self: *Env, value: anytype) !@TypeOf(value) { // ?
_ = self;
return value;
}
};

const Inspector = struct {
pub fn getRemoteObject(self: Inspector, env: *Env, jsValue: anytype, groupName: []const u8) !RemoteObject {
_ = self;
_ = env;
_ = jsValue;
_ = groupName;
return RemoteObject{};
}
};

const RemoteObject = struct {
pub fn deinit(self: RemoteObject) void {
_ = self;
}
pub fn getType(self: RemoteObject, alloc: std.mem.Allocator) ![:0]const u8 {
_ = self;
_ = alloc;
return "TheType";
}
pub fn getSubtype(self: RemoteObject, alloc: std.mem.Allocator) ![:0]const u8 {
_ = self;
_ = alloc;
return "TheSubtype";
}
pub fn getClassName(self: RemoteObject, alloc: std.mem.Allocator) ![:0]const u8 {
_ = self;
_ = alloc;
return "TheClassName";
}
pub fn getDescription(self: RemoteObject, alloc: std.mem.Allocator) ![:0]const u8 {
_ = self;
_ = alloc;
return "TheDescription";
}
pub fn getObjectId(self: RemoteObject, alloc: std.mem.Allocator) ![:0]const u8 {
_ = self;
_ = alloc;
return "TheObjectId";
}
};

const Page = struct {
session: *Session,
rawuri: []const u8,
Expand Down
2 changes: 1 addition & 1 deletion vendor/zig-js-runtime