Skip to content

describeNode #524

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

Closed
wants to merge 5 commits into from
Closed
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
32 changes: 32 additions & 0 deletions src/cdp/domains/dom.zig
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub fn processMessage(cmd: anytype) !void {
getSearchResults,
discardSearchResults,
resolveNode,
describeNode,
}, cmd.input.action) orelse return error.UnknownMethod;

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

Expand Down Expand Up @@ -148,6 +150,36 @@ fn resolveNode(cmd: anytype) !void {
} }, .{});
}

fn describeNode(cmd: anytype) !void {
const params = (try cmd.params(struct {
nodeId: ?Node.Id = null,
backendNodeId: ?Node.Id = null,
objectId: ?[]const u8 = null,
depth: u32 = 1,
pierce: bool = false,
})) orelse return error.InvalidParams;
if (params.backendNodeId != null or params.depth != 1 or params.pierce) {
return error.NotYetImplementedParams;
}

const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;

if (params.nodeId != null) {
const node = bc.node_registry.lookup_by_id.get(params.nodeId.?) orelse return error.NodeNotFound;
return cmd.sendResult(.{ .node = bc.nodeWriter(node, .{}) }, .{});
} else if (params.objectId != null) {
const jsValue = try bc.session.inspector.getValueByObjectId(cmd.arena, params.objectId.?);

const entry = jsValue.externalEntry() orelse return error.ObjectIdIsNotANode;
const sub_type = entry.sub_type orelse return error.ObjectIdIsNotANode;
if (!std.mem.eql(u8, sub_type[0..std.mem.len(sub_type)], "node")) return error.ObjectIdIsNotANode;

const node = try bc.node_registry.register(@ptrCast(entry.ptr));
return cmd.sendResult(.{ .node = bc.nodeWriter(node, .{}) }, .{});
}
return error.MissingParams;
}

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

test "cdp.dom: getSearchResults unknown search id" {
Expand Down
22 changes: 20 additions & 2 deletions src/cdp/testing.zig
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,22 @@ const Session = struct {
}
};

const Value = struct {
pub fn externalEntry(self: Value) ?*ExternalEntry {
_ = self;
return allocator.create(ExternalEntry) catch unreachable;
}
};
const ExternalEntry = struct {
ptr: *anyopaque,
sub_type: [*c]const u8 = null,
};

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

Expand All @@ -122,6 +134,12 @@ const Inspector = struct {
_ = groupName;
return RemoteObject{};
}
pub fn getValueByObjectId(self: Inspector, alloc: std.mem.Allocator, object_id: []const u8) !Value {
_ = self;
_ = alloc;
_ = object_id;
return .{};
}
};

const RemoteObject = struct {
Expand Down
1 change: 1 addition & 0 deletions src/dom/attribute.zig
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub const Attr = struct {
pub const Self = parser.Attribute;
pub const prototype = *Node;
pub const mem_guarantied = true;
pub const sub_type = "node";

pub fn get_namespaceURI(self: *parser.Attribute) !?[]const u8 {
return try parser.nodeGetNamespace(parser.attributeToNode(self));
Expand Down
1 change: 1 addition & 0 deletions src/dom/cdata_section.zig
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ pub const CDATASection = struct {
pub const Self = parser.CDATASection;
pub const prototype = *Text;
pub const mem_guarantied = true;
pub const sub_type = "node";
};
1 change: 1 addition & 0 deletions src/dom/character_data.zig
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub const CharacterData = struct {
pub const Self = parser.CharacterData;
pub const prototype = *Node;
pub const mem_guarantied = true;
pub const sub_type = "node";

// JS funcs
// --------
Expand Down
1 change: 1 addition & 0 deletions src/dom/comment.zig
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub const Comment = struct {
pub const Self = parser.Comment;
pub const prototype = *CharacterData;
pub const mem_guarantied = true;
pub const sub_type = "node";

pub fn constructor(userctx: UserContext, data: ?[]const u8) !*parser.Comment {
return parser.documentCreateComment(
Expand Down
1 change: 1 addition & 0 deletions src/dom/document.zig
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pub const Document = struct {
pub const Self = parser.Document;
pub const prototype = *Node;
pub const mem_guarantied = true;
pub const sub_type = "node";

pub fn constructor(userctx: UserContext) !*parser.DocumentHTML {
const doc = try parser.documentCreateDocument(
Expand Down
1 change: 1 addition & 0 deletions src/dom/document_fragment.zig
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub const DocumentFragment = struct {
pub const Self = parser.DocumentFragment;
pub const prototype = *Node;
pub const mem_guarantied = true;
pub const sub_type = "node";

pub fn constructor(userctx: UserContext) !*parser.DocumentFragment {
return parser.documentCreateDocumentFragment(
Expand Down
1 change: 1 addition & 0 deletions src/dom/document_type.zig
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub const DocumentType = struct {
pub const Self = parser.DocumentType;
pub const prototype = *Node;
pub const mem_guarantied = true;
pub const sub_type = "node";

pub fn get_name(self: *parser.DocumentType) ![]const u8 {
return try parser.documentTypeGetName(self);
Expand Down
1 change: 1 addition & 0 deletions src/dom/element.zig
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub const Element = struct {
pub const Self = parser.Element;
pub const prototype = *Node;
pub const mem_guarantied = true;
pub const sub_type = "node";

pub const DOMRect = struct {
x: f64,
Expand Down
2 changes: 2 additions & 0 deletions src/dom/html_collection.zig
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ pub fn HTMLCollectionByAnchors(

pub const HTMLCollectionIterator = struct {
pub const mem_guarantied = true;
pub const sub_type = "node";

coll: *HTMLCollection,
index: u32 = 0,
Expand Down Expand Up @@ -312,6 +313,7 @@ pub const HTMLCollectionIterator = struct {
// But we wanted a dynamically comparison here, according to the match tagname.
pub const HTMLCollection = struct {
pub const mem_guarantied = true;
pub const sub_type = "node";

matcher: Matcher,
walker: Walker,
Expand Down
1 change: 1 addition & 0 deletions src/dom/node.zig
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ pub const Node = struct {
pub const Self = parser.Node;
pub const prototype = *EventTarget;
pub const mem_guarantied = true;
pub const sub_type = "node";

pub fn toInterface(node: *parser.Node) !Union {
return switch (try parser.nodeType(node)) {
Expand Down
1 change: 1 addition & 0 deletions src/dom/text.zig
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub const Text = struct {
pub const Self = parser.Text;
pub const prototype = *CharacterData;
pub const mem_guarantied = true;
pub const sub_type = "node";

pub fn constructor(userctx: UserContext, data: ?[]const u8) !*parser.Text {
return parser.documentCreateTextNode(
Expand Down
1 change: 0 additions & 1 deletion src/html/document.zig
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ pub const HTMLDocument = struct {
pub const Self = parser.DocumentHTML;
pub const prototype = *Document;
pub const mem_guarantied = true;

pub const sub_type = "node";

// JS funcs
Expand Down
Loading
Loading