mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-29 15:13:28 +00:00
css: implement id and class match selector
This commit is contained in:
@@ -28,4 +28,8 @@ pub const Node = struct {
|
|||||||
pub fn tag(n: Node) ![]const u8 {
|
pub fn tag(n: Node) ![]const u8 {
|
||||||
return try parser.nodeName(n.node);
|
return try parser.nodeName(n.node);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn attr(n: Node, key: []const u8) !?[]const u8 {
|
||||||
|
return try parser.elementGetAttribute(parser.nodeToElement(n.node), key);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -36,11 +36,10 @@ test "matchFirst" {
|
|||||||
html: []const u8,
|
html: []const u8,
|
||||||
exp: usize,
|
exp: usize,
|
||||||
}{
|
}{
|
||||||
.{
|
.{ .q = "address", .html = "<body><address>This address...</address></body>", .exp = 1 },
|
||||||
.q = "address",
|
.{ .q = "#foo", .html = "<p id=\"foo\"><p id=\"bar\">", .exp = 1 },
|
||||||
.html = "<body><address>This address...</address></body>",
|
.{ .q = ".t1", .html = "<ul><li class=\"t1\"><li class=\"t2\">", .exp = 1 },
|
||||||
.exp = 1,
|
.{ .q = ".t3", .html = "<ul><li class=\"t1\"><li class=\"t2 t3\">", .exp = 1 },
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
for (testcases) |tc| {
|
for (testcases) |tc| {
|
||||||
@@ -70,11 +69,10 @@ test "matchAll" {
|
|||||||
html: []const u8,
|
html: []const u8,
|
||||||
exp: usize,
|
exp: usize,
|
||||||
}{
|
}{
|
||||||
.{
|
.{ .q = "address", .html = "<body><address>This address...</address></body>", .exp = 1 },
|
||||||
.q = "address",
|
.{ .q = "#foo", .html = "<p id=\"foo\"><p id=\"bar\">", .exp = 1 },
|
||||||
.html = "<body><address>This address...</address></body>",
|
.{ .q = ".t1", .html = "<ul><li class=\"t1\"><li class=\"t2\">", .exp = 1 },
|
||||||
.exp = 1,
|
.{ .q = ".t3", .html = "<ul><li class=\"t1\"><li class=\"t2 t3\">", .exp = 1 },
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
for (testcases) |tc| {
|
for (testcases) |tc| {
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ pub const Node = struct {
|
|||||||
sibling: ?*const Node = null,
|
sibling: ?*const Node = null,
|
||||||
|
|
||||||
name: []const u8 = "",
|
name: []const u8 = "",
|
||||||
|
att: ?[]const u8 = null,
|
||||||
|
|
||||||
pub fn firstChild(n: *const Node) !?*const Node {
|
pub fn firstChild(n: *const Node) !?*const Node {
|
||||||
return n.child;
|
return n.child;
|
||||||
@@ -23,6 +24,10 @@ pub const Node = struct {
|
|||||||
pub fn tag(n: *const Node) ![]const u8 {
|
pub fn tag(n: *const Node) ![]const u8 {
|
||||||
return n.name;
|
return n.name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn attr(n: *const Node, _: []const u8) !?[]const u8 {
|
||||||
|
return n.att;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const Matcher = struct {
|
const Matcher = struct {
|
||||||
@@ -60,7 +65,22 @@ test "matchFirst" {
|
|||||||
}{
|
}{
|
||||||
.{
|
.{
|
||||||
.q = "address",
|
.q = "address",
|
||||||
.n = .{ .name = "body", .child = &.{ .name = "address" } },
|
.n = .{ .child = &.{ .name = "body", .child = &.{ .name = "address" } } },
|
||||||
|
.exp = 1,
|
||||||
|
},
|
||||||
|
.{
|
||||||
|
.q = "#foo",
|
||||||
|
.n = .{ .child = &.{ .name = "p", .att = "foo", .child = &.{ .name = "p" } } },
|
||||||
|
.exp = 1,
|
||||||
|
},
|
||||||
|
.{
|
||||||
|
.q = ".t1",
|
||||||
|
.n = .{ .child = &.{ .name = "p", .sibling = &.{ .name = "p", .att = "t1" } } },
|
||||||
|
.exp = 1,
|
||||||
|
},
|
||||||
|
.{
|
||||||
|
.q = ".t1",
|
||||||
|
.n = .{ .child = &.{ .name = "p", .sibling = &.{ .name = "p", .att = "foo t1" } } },
|
||||||
.exp = 1,
|
.exp = 1,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
@@ -89,7 +109,22 @@ test "matchAll" {
|
|||||||
}{
|
}{
|
||||||
.{
|
.{
|
||||||
.q = "address",
|
.q = "address",
|
||||||
.n = .{ .name = "body", .child = &.{ .name = "address" } },
|
.n = .{ .child = &.{ .name = "body", .child = &.{ .name = "address" } } },
|
||||||
|
.exp = 1,
|
||||||
|
},
|
||||||
|
.{
|
||||||
|
.q = "#foo",
|
||||||
|
.n = .{ .child = &.{ .name = "p", .att = "foo", .child = &.{ .name = "p" } } },
|
||||||
|
.exp = 1,
|
||||||
|
},
|
||||||
|
.{
|
||||||
|
.q = ".t1",
|
||||||
|
.n = .{ .child = &.{ .name = "p", .sibling = &.{ .name = "p", .att = "t1" } } },
|
||||||
|
.exp = 1,
|
||||||
|
},
|
||||||
|
.{
|
||||||
|
.q = ".t1",
|
||||||
|
.n = .{ .child = &.{ .name = "p", .sibling = &.{ .name = "p", .att = "foo t1" } } },
|
||||||
.exp = 1,
|
.exp = 1,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -165,9 +165,21 @@ pub const Selector = union(enum) {
|
|||||||
},
|
},
|
||||||
pseudo_element: PseudoClass,
|
pseudo_element: PseudoClass,
|
||||||
|
|
||||||
|
// returns true if s is a whitespace-separated list that includes val.
|
||||||
|
fn contains(haystack: []const u8, needle: []const u8) bool {
|
||||||
|
if (haystack.len == 0) return false;
|
||||||
|
var it = std.mem.splitAny(u8, haystack, " \t\r\n"); // TODO add \f
|
||||||
|
while (it.next()) |part| {
|
||||||
|
if (std.mem.eql(u8, part, needle)) return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
pub fn match(s: Selector, n: anytype) !bool {
|
pub fn match(s: Selector, n: anytype) !bool {
|
||||||
return switch (s) {
|
return switch (s) {
|
||||||
.tag => |v| n.isElement() and std.ascii.eqlIgnoreCase(v, try n.tag()),
|
.tag => |v| n.isElement() and std.ascii.eqlIgnoreCase(v, try n.tag()),
|
||||||
|
.id => |v| return n.isElement() and std.mem.eql(u8, v, try n.attr("id") orelse return false),
|
||||||
|
.class => |v| return n.isElement() and contains(try n.attr("class") orelse return false, v),
|
||||||
else => false,
|
else => false,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user