mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-28 22:53:28 +00:00
css: implement :lang match
This commit is contained in:
@@ -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| {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user