mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-30 07:31:47 +00:00
dom: reimplement nodelist in pure zig
This commit is contained in:
@@ -3,30 +3,73 @@ const std = @import("std");
|
||||
const parser = @import("../netsurf.zig");
|
||||
|
||||
const jsruntime = @import("jsruntime");
|
||||
const Case = jsruntime.test_utils.Case;
|
||||
const checkCases = jsruntime.test_utils.checkCases;
|
||||
|
||||
const NodeUnion = @import("node.zig").Union;
|
||||
const Node = @import("node.zig").Node;
|
||||
|
||||
const DOMException = @import("exceptions.zig").DOMException;
|
||||
|
||||
// Nodelist is implemented in pure Zig b/c libdom's NodeList doesn't allow to
|
||||
// append nodes.
|
||||
// WEB IDL https://dom.spec.whatwg.org/#nodelist
|
||||
pub const NodeList = struct {
|
||||
pub const Self = parser.NodeList;
|
||||
pub const mem_guarantied = true;
|
||||
|
||||
pub const Exception = DOMException;
|
||||
|
||||
pub fn get_length(self: *parser.NodeList) !u32 {
|
||||
return try parser.nodeListLength(self);
|
||||
const NodesArrayList = std.ArrayList(*parser.Node);
|
||||
|
||||
nodes: NodesArrayList,
|
||||
|
||||
pub fn init(alloc: std.mem.Allocator) !*NodeList {
|
||||
const list = try alloc.create(NodeList);
|
||||
list.* = NodeList{
|
||||
.nodes = NodesArrayList.init(alloc),
|
||||
};
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
pub fn _item(self: *parser.NodeList, index: u32) !?NodeUnion {
|
||||
const n = try parser.nodeListItem(self, index);
|
||||
if (n == null) return null;
|
||||
return try Node.toInterface(n.?);
|
||||
pub fn deinit(self: *NodeList, alloc: std.mem.Allocator) void {
|
||||
// TODO unref all nodes
|
||||
self.nodes.deinit();
|
||||
alloc.destroy(self);
|
||||
}
|
||||
|
||||
pub fn append(self: *NodeList, node: *parser.Node) !void {
|
||||
try self.nodes.append(node);
|
||||
}
|
||||
|
||||
pub fn get_length(self: *NodeList) u32 {
|
||||
return @intCast(self.nodes.items.len);
|
||||
}
|
||||
|
||||
pub fn _item(self: *NodeList, index: u32) !?NodeUnion {
|
||||
if (index >= self.nodes.items.len) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const n = self.nodes.items[index];
|
||||
return try Node.toInterface(n);
|
||||
}
|
||||
|
||||
// TODO _symbol_iterator
|
||||
|
||||
// TODO implement postAttach
|
||||
};
|
||||
|
||||
// Tests
|
||||
// -----
|
||||
|
||||
pub fn testExecFn(
|
||||
_: std.mem.Allocator,
|
||||
js_env: *jsruntime.Env,
|
||||
comptime _: []jsruntime.API,
|
||||
) !void {
|
||||
var childnodes = [_]Case{
|
||||
.{ .src = "let list = document.getElementById('content').childNodes", .ex = "undefined" },
|
||||
.{ .src = "list.length", .ex = "9" },
|
||||
};
|
||||
try checkCases(js_env, &childnodes);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user