css: add matcher test w/ libdom

This commit is contained in:
Pierre Tachoire
2024-03-18 09:49:12 +01:00
parent b59fd9b1fb
commit 954a693586
4 changed files with 60 additions and 5 deletions

View File

@@ -13,10 +13,14 @@ pub const Node = struct {
return null; return null;
} }
pub fn nextSibling(n: Node) ?Node { pub fn nextSibling(n: Node) !?Node {
const c = try parser.nodeNextSibling(n.node); const c = try parser.nodeNextSibling(n.node);
if (c) |cc| return .{ .node = cc }; if (c) |cc| return .{ .node = cc };
return null; return null;
} }
pub fn tag(n: Node) ![]const u8 {
return try parser.nodeName(n.node);
}
}; };

44
src/css/libdom_test.zig Normal file
View File

@@ -0,0 +1,44 @@
const std = @import("std");
const css = @import("css.zig");
const Node = @import("libdom.zig").Node;
const parser = @import("../netsurf.zig");
const Matcher = struct {
const Nodes = std.ArrayList(Node);
nodes: Nodes,
fn init(alloc: std.mem.Allocator) Matcher {
return .{ .nodes = Nodes.init(alloc) };
}
fn deinit(m: *Matcher) void {
m.nodes.deinit();
}
fn reset(m: *Matcher) void {
m.nodes.clearRetainingCapacity();
}
pub fn match(m: *Matcher, n: Node) !void {
try m.nodes.append(n);
}
};
test "matchFirst" {
const alloc = std.testing.allocator;
const s = try css.parse(alloc, "address", .{});
defer s.deinit(alloc);
var matcher = Matcher.init(alloc);
defer matcher.deinit();
const doc = try parser.documentHTMLParseFromStr("<body><address>This address...</address></body>");
defer parser.documentHTMLClose(doc) catch {};
const node = Node{ .node = parser.documentHTMLToNode(doc) };
_ = try css.matchFirst(s, node, &matcher);
try std.testing.expect(1 == matcher.nodes.items.len);
}

View File

@@ -20,6 +20,7 @@ pub const Node = struct {
return n.name; return n.name;
} }
}; };
const Matcher = struct { const Matcher = struct {
const Nodes = std.ArrayList(*const Node); const Nodes = std.ArrayList(*const Node);

View File

@@ -98,11 +98,17 @@ pub fn main() !void {
} }
test { test {
const AsyncTest = @import("async/test.zig"); const asyncTest = @import("async/test.zig");
std.testing.refAllDecls(AsyncTest); std.testing.refAllDecls(asyncTest);
const DumpTest = @import("browser/dump.zig"); const dumpTest = @import("browser/dump.zig");
std.testing.refAllDecls(DumpTest); std.testing.refAllDecls(dumpTest);
const cssMatchTest = @import("css/match_test.zig");
std.testing.refAllDecls(cssMatchTest);
const cssLibdomTest = @import("css/libdom_test.zig");
std.testing.refAllDecls(cssLibdomTest);
} }
fn testJSRuntime() !void { fn testJSRuntime() !void {