css: check if node is an html element

This commit is contained in:
Pierre Tachoire
2024-03-18 11:36:06 +01:00
parent 7839f466ea
commit 4629e8a9eb
3 changed files with 10 additions and 1 deletions

View File

@@ -20,6 +20,11 @@ pub const Node = struct {
return null;
}
pub fn isElement(n: Node) bool {
const t = parser.nodeType(n.node) catch return false;
return t == .element;
}
pub fn tag(n: Node) ![]const u8 {
return try parser.nodeName(n.node);
}

View File

@@ -16,6 +16,10 @@ pub const Node = struct {
return n.sibling;
}
pub fn isElement(_: *const Node) bool {
return true;
}
pub fn tag(n: *const Node) ![]const u8 {
return n.name;
}

View File

@@ -167,7 +167,7 @@ pub const Selector = union(enum) {
pub fn match(s: Selector, n: anytype) !bool {
return switch (s) {
.tag => |v| std.ascii.eqlIgnoreCase(v, try n.tag()),
.tag => |v| n.isElement() and std.ascii.eqlIgnoreCase(v, try n.tag()),
else => false,
};
}