css: implement :lang match

This commit is contained in:
Pierre Tachoire
2024-03-25 11:43:32 +01:00
parent bd899111d5
commit 2671cda98f
2 changed files with 38 additions and 1 deletions

View File

@@ -215,6 +215,16 @@ test "matchFirst" {
.n = .{ .child = &.{ .name = "p", .child = &.{ .name = "a" }, .sibling = &.{ .name = "strong" } } },
.exp = 0,
},
.{
.q = "p:lang(en)",
.n = .{ .child = &.{ .name = "p", .att = "en-US", .child = &.{ .name = "a" } } },
.exp = 1,
},
.{
.q = "a:lang(en)",
.n = .{ .child = &.{ .name = "p", .child = &.{ .name = "a", .par = &.{ .att = "en-US" } } } },
.exp = 1,
},
};
for (testcases) |tc| {
@@ -379,6 +389,16 @@ test "matchAll" {
.n = .{ .child = &.{ .name = "p", .child = &.{ .name = "a" }, .sibling = &.{ .name = "strong" } } },
.exp = 0,
},
.{
.q = "p:lang(en)",
.n = .{ .child = &.{ .name = "p", .att = "en-US", .child = &.{ .name = "a" } } },
.exp = 1,
},
.{
.q = "a:lang(en)",
.n = .{ .child = &.{ .name = "p", .child = &.{ .name = "a", .par = &.{ .att = "en-US" } } } },
.exp = 1,
},
};
for (testcases) |tc| {

View File

@@ -337,11 +337,28 @@ pub const Selector = union(enum) {
}
},
.pseudo_class_only_child => |v| onlyChildMatch(v, n),
.pseudo_class_lang => return false,
.pseudo_class_lang => |v| langMatch(v, n),
.pseudo_element => return false,
};
}
fn langMatch(lang: []const u8, n: anytype) anyerror!bool {
if (try n.attr("lang")) |own| {
if (std.mem.eql(u8, own, lang)) return true;
// check if the lang attr starts with lang+'-'
if (std.mem.startsWith(u8, own, lang)) {
if (own.len > lang.len and own[lang.len] == '-') return true;
}
}
// if the tag doesn't match, try the parent.
const p = try n.parent();
if (p == null) return false;
return langMatch(lang, p.?);
}
// onlyChildMatch implements :only-child
// If `ofType` is true, it implements :only-of-type instead.
fn onlyChildMatch(of_type: bool, n: anytype) anyerror!bool {