css: add isDocument, isText and isComment

This commit is contained in:
Pierre Tachoire
2024-03-25 17:38:21 +01:00
parent 8a91840783
commit 2c7650cdb1
3 changed files with 29 additions and 2 deletions

View File

@@ -46,6 +46,21 @@ pub const Node = struct {
return t == .element;
}
pub fn isDocument(n: Node) bool {
const t = parser.nodeType(n.node) catch return false;
return t == .document;
}
pub fn isComment(n: Node) bool {
const t = parser.nodeType(n.node) catch return false;
return t == .comment;
}
pub fn isText(n: Node) bool {
const t = parser.nodeType(n.node) catch return false;
return t == .text;
}
pub fn tag(n: Node) ![]const u8 {
return try parser.nodeName(n.node);
}

View File

@@ -36,6 +36,18 @@ pub const Node = struct {
return true;
}
pub fn isDocument(_: *const Node) bool {
return false;
}
pub fn isComment(_: *const Node) bool {
return false;
}
pub fn isText(_: *const Node) bool {
return false;
}
pub fn tag(n: *const Node) ![]const u8 {
return n.name;
}

View File

@@ -274,7 +274,7 @@ pub const Selector = union(enum) {
if (!try v.second.match(n)) return false;
var c = try n.prevSibling();
while (c != null) {
if (!c.?.isElement()) { // TODO must check text node or comment node instead.
if (c.?.isText() or c.?.isComment()) {
c = try c.?.prevSibling();
continue;
}
@@ -395,7 +395,7 @@ pub const Selector = union(enum) {
if (!n.isElement()) return false;
const p = try n.parent();
return p == null;
return (p != null and p.?.isDocument());
},
.link => {
const ntag = try n.tag();