Skip to content

Commit 8f8a1fd

Browse files
committed
cdp: implement DOM.getDocument
1 parent 26be25c commit 8f8a1fd

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

src/cdp/dom.zig

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,13 @@ const result = cdp.result;
2525
const IncomingMessage = @import("msg.zig").IncomingMessage;
2626
const Input = @import("msg.zig").Input;
2727

28+
const parser = @import("netsurf");
29+
2830
const log = std.log.scoped(.cdp);
2931

3032
const Methods = enum {
3133
enable,
34+
getDocument,
3235
};
3336

3437
pub fn dom(
@@ -42,6 +45,7 @@ pub fn dom(
4245

4346
return switch (method) {
4447
.enable => enable(alloc, msg, ctx),
48+
.getDocument => getDocument(alloc, msg, ctx),
4549
};
4650
}
4751

@@ -57,3 +61,79 @@ fn enable(
5761

5862
return result(alloc, input.id, null, null, input.sessionId);
5963
}
64+
65+
const NodeId = u32;
66+
67+
const Node = struct {
68+
nodeId: NodeId,
69+
parentId: ?NodeId = null,
70+
backendNodeId: NodeId,
71+
nodeType: u32,
72+
nodeName: []const u8 = "",
73+
localName: []const u8 = "",
74+
nodeValue: []const u8 = "",
75+
childNodeCount: u32,
76+
children: ?[]const Node = null,
77+
documentURL: ?[]const u8 = null,
78+
baseURL: ?[]const u8 = null,
79+
xmlVersion: []const u8 = "",
80+
compatibilityMode: []const u8 = "NoQuirksMode",
81+
isScrollable: bool = false,
82+
83+
fn init(n: *parser.Node) !Node {
84+
const children = try parser.nodeGetChildNodes(n);
85+
const ln = try parser.nodeListLength(children);
86+
87+
return .{
88+
.nodeId = 1,
89+
.backendNodeId = 1,
90+
.nodeType = @intFromEnum(try parser.nodeType(n)),
91+
.nodeName = try parser.nodeName(n),
92+
.localName = try parser.nodeLocalName(n),
93+
.nodeValue = try parser.nodeValue(n) orelse "",
94+
.childNodeCount = ln,
95+
};
96+
}
97+
};
98+
99+
// https://chromedevtools.github.io/devtools-protocol/tot/DOM/#method-getDocument
100+
fn getDocument(
101+
alloc: std.mem.Allocator,
102+
msg: *IncomingMessage,
103+
ctx: *Ctx,
104+
) ![]const u8 {
105+
// input
106+
const Params = struct {
107+
depth: ?u32 = null,
108+
pierce: ?bool = null,
109+
};
110+
const input = try Input(Params).get(alloc, msg);
111+
defer input.deinit();
112+
std.debug.assert(input.sessionId != null);
113+
log.debug("Req > id {d}, method {s}", .{ input.id, "DOM.getDocument" });
114+
115+
if (ctx.browser.session.page == null) {
116+
return error.NoPage;
117+
}
118+
119+
// retrieve the root node
120+
const page = ctx.browser.session.page.?;
121+
122+
if (page.doc == null) {
123+
return error.NoDocument;
124+
}
125+
126+
const root = try parser.documentGetDocumentElement(page.doc.?) orelse {
127+
return error.NoRoot;
128+
};
129+
130+
// output
131+
const Resp = struct {
132+
root: Node,
133+
};
134+
const resp: Resp = .{
135+
.root = try Node.init(parser.elementToNode(root)),
136+
};
137+
138+
return result(alloc, input.id, Resp, resp, input.sessionId);
139+
}

0 commit comments

Comments
 (0)