css: implement id and class match selector

This commit is contained in:
Pierre Tachoire
2024-03-18 12:48:03 +01:00
parent 4629e8a9eb
commit d64fffc5b3
4 changed files with 61 additions and 12 deletions

View File

@@ -28,4 +28,8 @@ pub const Node = struct {
pub fn tag(n: Node) ![]const u8 { pub fn tag(n: Node) ![]const u8 {
return try parser.nodeName(n.node); return try parser.nodeName(n.node);
} }
pub fn attr(n: Node, key: []const u8) !?[]const u8 {
return try parser.elementGetAttribute(parser.nodeToElement(n.node), key);
}
}; };

View File

@@ -36,11 +36,10 @@ test "matchFirst" {
html: []const u8, html: []const u8,
exp: usize, exp: usize,
}{ }{
.{ .{ .q = "address", .html = "<body><address>This address...</address></body>", .exp = 1 },
.q = "address", .{ .q = "#foo", .html = "<p id=\"foo\"><p id=\"bar\">", .exp = 1 },
.html = "<body><address>This address...</address></body>", .{ .q = ".t1", .html = "<ul><li class=\"t1\"><li class=\"t2\">", .exp = 1 },
.exp = 1, .{ .q = ".t3", .html = "<ul><li class=\"t1\"><li class=\"t2 t3\">", .exp = 1 },
},
}; };
for (testcases) |tc| { for (testcases) |tc| {
@@ -70,11 +69,10 @@ test "matchAll" {
html: []const u8, html: []const u8,
exp: usize, exp: usize,
}{ }{
.{ .{ .q = "address", .html = "<body><address>This address...</address></body>", .exp = 1 },
.q = "address", .{ .q = "#foo", .html = "<p id=\"foo\"><p id=\"bar\">", .exp = 1 },
.html = "<body><address>This address...</address></body>", .{ .q = ".t1", .html = "<ul><li class=\"t1\"><li class=\"t2\">", .exp = 1 },
.exp = 1, .{ .q = ".t3", .html = "<ul><li class=\"t1\"><li class=\"t2 t3\">", .exp = 1 },
},
}; };
for (testcases) |tc| { for (testcases) |tc| {

View File

@@ -7,6 +7,7 @@ pub const Node = struct {
sibling: ?*const Node = null, sibling: ?*const Node = null,
name: []const u8 = "", name: []const u8 = "",
att: ?[]const u8 = null,
pub fn firstChild(n: *const Node) !?*const Node { pub fn firstChild(n: *const Node) !?*const Node {
return n.child; return n.child;
@@ -23,6 +24,10 @@ pub const Node = struct {
pub fn tag(n: *const Node) ![]const u8 { pub fn tag(n: *const Node) ![]const u8 {
return n.name; return n.name;
} }
pub fn attr(n: *const Node, _: []const u8) !?[]const u8 {
return n.att;
}
}; };
const Matcher = struct { const Matcher = struct {
@@ -60,7 +65,22 @@ test "matchFirst" {
}{ }{
.{ .{
.q = "address", .q = "address",
.n = .{ .name = "body", .child = &.{ .name = "address" } }, .n = .{ .child = &.{ .name = "body", .child = &.{ .name = "address" } } },
.exp = 1,
},
.{
.q = "#foo",
.n = .{ .child = &.{ .name = "p", .att = "foo", .child = &.{ .name = "p" } } },
.exp = 1,
},
.{
.q = ".t1",
.n = .{ .child = &.{ .name = "p", .sibling = &.{ .name = "p", .att = "t1" } } },
.exp = 1,
},
.{
.q = ".t1",
.n = .{ .child = &.{ .name = "p", .sibling = &.{ .name = "p", .att = "foo t1" } } },
.exp = 1, .exp = 1,
}, },
}; };
@@ -89,7 +109,22 @@ test "matchAll" {
}{ }{
.{ .{
.q = "address", .q = "address",
.n = .{ .name = "body", .child = &.{ .name = "address" } }, .n = .{ .child = &.{ .name = "body", .child = &.{ .name = "address" } } },
.exp = 1,
},
.{
.q = "#foo",
.n = .{ .child = &.{ .name = "p", .att = "foo", .child = &.{ .name = "p" } } },
.exp = 1,
},
.{
.q = ".t1",
.n = .{ .child = &.{ .name = "p", .sibling = &.{ .name = "p", .att = "t1" } } },
.exp = 1,
},
.{
.q = ".t1",
.n = .{ .child = &.{ .name = "p", .sibling = &.{ .name = "p", .att = "foo t1" } } },
.exp = 1, .exp = 1,
}, },
}; };

View File

@@ -165,9 +165,21 @@ pub const Selector = union(enum) {
}, },
pseudo_element: PseudoClass, pseudo_element: PseudoClass,
// returns true if s is a whitespace-separated list that includes val.
fn contains(haystack: []const u8, needle: []const u8) bool {
if (haystack.len == 0) return false;
var it = std.mem.splitAny(u8, haystack, " \t\r\n"); // TODO add \f
while (it.next()) |part| {
if (std.mem.eql(u8, part, needle)) return true;
}
return false;
}
pub fn match(s: Selector, n: anytype) !bool { pub fn match(s: Selector, n: anytype) !bool {
return switch (s) { return switch (s) {
.tag => |v| n.isElement() and std.ascii.eqlIgnoreCase(v, try n.tag()), .tag => |v| n.isElement() and std.ascii.eqlIgnoreCase(v, try n.tag()),
.id => |v| return n.isElement() and std.mem.eql(u8, v, try n.attr("id") orelse return false),
.class => |v| return n.isElement() and contains(try n.attr("class") orelse return false, v),
else => false, else => false,
}; };
} }