From 2c7650cdb15ef39e8b84134034852a410fecc074 Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Mon, 25 Mar 2024 17:38:21 +0100 Subject: [PATCH] css: add isDocument, isText and isComment --- src/css/libdom.zig | 15 +++++++++++++++ src/css/match_test.zig | 12 ++++++++++++ src/css/selector.zig | 4 ++-- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/css/libdom.zig b/src/css/libdom.zig index 04c99a66..93acb790 100644 --- a/src/css/libdom.zig +++ b/src/css/libdom.zig @@ -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); } diff --git a/src/css/match_test.zig b/src/css/match_test.zig index 47fbdb78..796ee7d7 100644 --- a/src/css/match_test.zig +++ b/src/css/match_test.zig @@ -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; } diff --git a/src/css/selector.zig b/src/css/selector.zig index 696203d5..e678fb1d 100644 --- a/src/css/selector.zig +++ b/src/css/selector.zig @@ -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();