Skip to content

Commit 4f0ea88

Browse files
Merge pull request #513 from lightpanda-io/resolveNode
Resolve node
2 parents 8b29653 + bc1a83d commit 4f0ea88

File tree

3 files changed

+86
-1
lines changed

3 files changed

+86
-1
lines changed

src/cdp/domains/dom.zig

+33
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const std = @import("std");
2020
const parser = @import("netsurf");
2121
const Node = @import("../Node.zig");
2222
const css = @import("../../dom/css.zig");
23+
const dom_node = @import("../../dom/node.zig");
2324

2425
pub fn processMessage(cmd: anytype) !void {
2526
const action = std.meta.stringToEnum(enum {
@@ -28,6 +29,7 @@ pub fn processMessage(cmd: anytype) !void {
2829
performSearch,
2930
getSearchResults,
3031
discardSearchResults,
32+
resolveNode,
3133
}, cmd.input.action) orelse return error.UnknownMethod;
3234

3335
switch (action) {
@@ -36,6 +38,7 @@ pub fn processMessage(cmd: anytype) !void {
3638
.performSearch => return performSearch(cmd),
3739
.getSearchResults => return getSearchResults(cmd),
3840
.discardSearchResults => return discardSearchResults(cmd),
41+
.resolveNode => return resolveNode(cmd),
3942
}
4043
}
4144

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

121+
fn resolveNode(cmd: anytype) !void {
122+
const params = (try cmd.params(struct {
123+
nodeId: ?Node.Id = null,
124+
backendNodeId: ?u32 = null,
125+
objectGroup: ?[]const u8 = null,
126+
executionContextId: ?u32 = null,
127+
})) orelse return error.InvalidParams;
128+
if (params.nodeId == null or params.backendNodeId != null or params.executionContextId != null) {
129+
return error.NotYetImplementedParams;
130+
}
131+
132+
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
133+
const node = bc.node_registry.lookup_by_id.get(params.nodeId.?) orelse return error.UnknownNode;
134+
135+
// node._node is a *parser.Node we need this to be able to find its most derived type e.g. Node -> Element -> HTMLElement
136+
// So we use the Node.Union when retrieve the value from the environment
137+
const jsValue = try bc.session.env.findOrAddValue(try dom_node.Node.toInterface(node._node));
138+
const remoteObject = try bc.session.inspector.getRemoteObject(&bc.session.env, jsValue, params.objectGroup orelse "");
139+
defer remoteObject.deinit();
140+
141+
const arena = cmd.arena;
142+
return cmd.sendResult(.{ .object = .{
143+
.type = try remoteObject.getType(arena),
144+
.subtype = try remoteObject.getSubtype(arena),
145+
.className = try remoteObject.getClassName(arena),
146+
.description = try remoteObject.getDescription(arena),
147+
.objectId = try remoteObject.getObjectId(arena),
148+
} }, .{});
149+
}
150+
118151
const testing = @import("../testing.zig");
119152

120153
test "cdp.dom: getSearchResults unknown search id" {

src/cdp/testing.zig

+52
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ const Browser = struct {
6060
self.session.?.* = .{
6161
.page = null,
6262
.arena = arena,
63+
.env = Env{},
64+
.inspector = Inspector{},
6365
};
6466
return self.session.?;
6567
}
@@ -75,6 +77,8 @@ const Browser = struct {
7577
const Session = struct {
7678
page: ?Page = null,
7779
arena: Allocator,
80+
env: Env,
81+
inspector: Inspector,
7882

7983
pub fn currentPage(self: *Session) ?*Page {
8084
return &(self.page orelse return null);
@@ -102,6 +106,54 @@ const Session = struct {
102106
}
103107
};
104108

109+
const Env = struct {
110+
pub fn findOrAddValue(self: *Env, value: anytype) !@TypeOf(value) { // ?
111+
_ = self;
112+
return value;
113+
}
114+
};
115+
116+
const Inspector = struct {
117+
pub fn getRemoteObject(self: Inspector, env: *Env, jsValue: anytype, groupName: []const u8) !RemoteObject {
118+
_ = self;
119+
_ = env;
120+
_ = jsValue;
121+
_ = groupName;
122+
return RemoteObject{};
123+
}
124+
};
125+
126+
const RemoteObject = struct {
127+
pub fn deinit(self: RemoteObject) void {
128+
_ = self;
129+
}
130+
pub fn getType(self: RemoteObject, alloc: std.mem.Allocator) ![:0]const u8 {
131+
_ = self;
132+
_ = alloc;
133+
return "TheType";
134+
}
135+
pub fn getSubtype(self: RemoteObject, alloc: std.mem.Allocator) ![:0]const u8 {
136+
_ = self;
137+
_ = alloc;
138+
return "TheSubtype";
139+
}
140+
pub fn getClassName(self: RemoteObject, alloc: std.mem.Allocator) ![:0]const u8 {
141+
_ = self;
142+
_ = alloc;
143+
return "TheClassName";
144+
}
145+
pub fn getDescription(self: RemoteObject, alloc: std.mem.Allocator) ![:0]const u8 {
146+
_ = self;
147+
_ = alloc;
148+
return "TheDescription";
149+
}
150+
pub fn getObjectId(self: RemoteObject, alloc: std.mem.Allocator) ![:0]const u8 {
151+
_ = self;
152+
_ = alloc;
153+
return "TheObjectId";
154+
}
155+
};
156+
105157
const Page = struct {
106158
session: *Session,
107159
rawuri: []const u8,

0 commit comments

Comments
 (0)