mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-29 23:23:28 +00:00
Add CDP Node Registry
This expands on the existing CDP node work used in DOM.search. It introduces a node registry to track all nodes returned to the client and give lookups to get a node from a Id or a *parser.node. Eventually, the goal is to have the Registry emit the DOM.setChildNodes event whenever necessary, as well as support many of the missing DOM actions. Added tests to existing search handlers. Reworked search a little bit to avoid some unnecessary allocations and to hook it into the registry. The generated Node is currently incomplete. The parentId is missing, the children are missing. Also, we still need to associate the v8 ObjectId to the node. Finally, I moved all action handlers into a nested "domain" folder.
This commit is contained in:
@@ -17,10 +17,12 @@
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
const std = @import("std");
|
||||
const parser = @import("netsurf");
|
||||
|
||||
pub const allocator = std.testing.allocator;
|
||||
pub const expectError = std.testing.expectError;
|
||||
pub const expectString = std.testing.expectEqualStrings;
|
||||
pub const expectEqualSlices = std.testing.expectEqualSlices;
|
||||
|
||||
const App = @import("app.zig").App;
|
||||
|
||||
@@ -190,3 +192,37 @@ pub const Random = struct {
|
||||
return instance.?.random();
|
||||
}
|
||||
};
|
||||
|
||||
pub const Document = struct {
|
||||
doc: *parser.Document,
|
||||
arena: std.heap.ArenaAllocator,
|
||||
|
||||
pub fn init(html: []const u8) !Document {
|
||||
parser.deinit();
|
||||
try parser.init();
|
||||
|
||||
var fbs = std.io.fixedBufferStream(html);
|
||||
const html_doc = try parser.documentHTMLParse(fbs.reader(), "utf-8");
|
||||
|
||||
return .{
|
||||
.arena = std.heap.ArenaAllocator.init(allocator),
|
||||
.doc = parser.documentHTMLToDocument(html_doc),
|
||||
};
|
||||
}
|
||||
|
||||
pub fn deinit(self: *Document) void {
|
||||
parser.deinit();
|
||||
self.arena.deinit();
|
||||
}
|
||||
|
||||
pub fn querySelectorAll(self: *Document, selector: []const u8) ![]const *parser.Node {
|
||||
const css = @import("dom/css.zig");
|
||||
const node_list = try css.querySelectorAll(self.arena.allocator(), parser.documentToNode(self.doc), selector);
|
||||
return node_list.nodes.items;
|
||||
}
|
||||
|
||||
pub fn querySelector(self: *Document, selector: []const u8) !?*parser.Node {
|
||||
const css = @import("dom/css.zig");
|
||||
return css.querySelector(self.arena.allocator(), parser.documentToNode(self.doc), selector);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user