diff --git a/src/browser/css/libdom.zig b/src/browser/css/libdom.zig index 44307c63..ea28fb5c 100644 --- a/src/browser/css/libdom.zig +++ b/src/browser/css/libdom.zig @@ -19,6 +19,8 @@ const std = @import("std"); const parser = @import("../netsurf.zig"); +const css = @import("css.zig"); +const Allocator = std.mem.Allocator; // Node implementation with Netsurf Libdom C lib. pub const Node = struct { @@ -79,6 +81,14 @@ pub const Node = struct { return t == .text; } + pub fn text(n: Node) !?[]const u8 { + const data = try parser.nodeTextContent(n.node); + if (data == null) return null; + if (data.?.len == 0) return null; + + return std.mem.trim(u8, data.?, &std.ascii.whitespace); + } + pub fn isEmptyText(n: Node) !bool { const data = try parser.nodeTextContent(n.node); if (data == null) return true; @@ -100,3 +110,318 @@ pub const Node = struct { return a.node == b.node; } }; + +const MatcherTest = struct { + const Nodes = std.ArrayListUnmanaged(Node); + + nodes: Nodes, + allocator: Allocator, + + fn init(allocator: Allocator) MatcherTest { + return .{ + .nodes = .empty, + .allocator = allocator, + }; + } + + fn deinit(m: *MatcherTest) void { + m.nodes.deinit(m.allocator); + } + + fn reset(m: *MatcherTest) void { + m.nodes.clearRetainingCapacity(); + } + + pub fn match(m: *MatcherTest, n: Node) !void { + try m.nodes.append(m.allocator, n); + } +}; + +test "Browser.CSS.Libdom: matchFirst" { + const alloc = std.testing.allocator; + + try parser.init(); + defer parser.deinit(); + + var matcher = MatcherTest.init(alloc); + defer matcher.deinit(); + + const testcases = [_]struct { + q: []const u8, + html: []const u8, + exp: usize, + }{ + .{ .q = "address", .html = "
This address...
", .exp = 1 }, + .{ .q = "*", .html = "text", .exp = 1 }, + .{ .q = "*", .html = "", .exp = 1 }, + .{ .q = "#foo", .html = "

", .exp = 1 }, + .{ .q = "li#t1", .html = "