mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-28 22:53:28 +00:00
css: add matcher test w/ libdom
This commit is contained in:
@@ -13,10 +13,14 @@ pub const Node = struct {
|
||||
return null;
|
||||
}
|
||||
|
||||
pub fn nextSibling(n: Node) ?Node {
|
||||
pub fn nextSibling(n: Node) !?Node {
|
||||
const c = try parser.nodeNextSibling(n.node);
|
||||
if (c) |cc| return .{ .node = cc };
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
pub fn tag(n: Node) ![]const u8 {
|
||||
return try parser.nodeName(n.node);
|
||||
}
|
||||
};
|
||||
|
||||
44
src/css/libdom_test.zig
Normal file
44
src/css/libdom_test.zig
Normal 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);
|
||||
}
|
||||
@@ -20,6 +20,7 @@ pub const Node = struct {
|
||||
return n.name;
|
||||
}
|
||||
};
|
||||
|
||||
const Matcher = struct {
|
||||
const Nodes = std.ArrayList(*const Node);
|
||||
|
||||
|
||||
@@ -98,11 +98,17 @@ pub fn main() !void {
|
||||
}
|
||||
|
||||
test {
|
||||
const AsyncTest = @import("async/test.zig");
|
||||
std.testing.refAllDecls(AsyncTest);
|
||||
const asyncTest = @import("async/test.zig");
|
||||
std.testing.refAllDecls(asyncTest);
|
||||
|
||||
const DumpTest = @import("browser/dump.zig");
|
||||
std.testing.refAllDecls(DumpTest);
|
||||
const dumpTest = @import("browser/dump.zig");
|
||||
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 {
|
||||
|
||||
Reference in New Issue
Block a user