mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-29 15:13:28 +00:00
Merge pull request #977 from lightpanda-io/selector_by_ref
Select is relatively large (64 bytes), pass it by ref
This commit is contained in:
@@ -45,7 +45,7 @@ pub fn parse(alloc: std.mem.Allocator, s: []const u8, opts: parser.ParseOptions)
|
|||||||
|
|
||||||
// matchFirst call m.match with the first node that matches the selector s, from the
|
// matchFirst call m.match with the first node that matches the selector s, from the
|
||||||
// descendants of n and returns true. If none matches, it returns false.
|
// descendants of n and returns true. If none matches, it returns false.
|
||||||
pub fn matchFirst(s: Selector, node: anytype, m: anytype) !bool {
|
pub fn matchFirst(s: *const Selector, node: anytype, m: anytype) !bool {
|
||||||
var c = try node.firstChild();
|
var c = try node.firstChild();
|
||||||
while (true) {
|
while (true) {
|
||||||
if (c == null) break;
|
if (c == null) break;
|
||||||
@@ -63,7 +63,7 @@ pub fn matchFirst(s: Selector, node: anytype, m: anytype) !bool {
|
|||||||
|
|
||||||
// matchAll call m.match with the all the nodes that matches the selector s, from the
|
// matchAll call m.match with the all the nodes that matches the selector s, from the
|
||||||
// descendants of n.
|
// descendants of n.
|
||||||
pub fn matchAll(s: Selector, node: anytype, m: anytype) !void {
|
pub fn matchAll(s: *const Selector, node: anytype, m: anytype) !void {
|
||||||
var c = try node.firstChild();
|
var c = try node.firstChild();
|
||||||
while (true) {
|
while (true) {
|
||||||
if (c == null) break;
|
if (c == null) break;
|
||||||
|
|||||||
@@ -306,8 +306,8 @@ pub const Selector = union(enum) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// match returns true if the node matches the selector query.
|
// match returns true if the node matches the selector query.
|
||||||
pub fn match(s: Selector, n: anytype) !bool {
|
pub fn match(s: *const 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),
|
.id => |v| return n.isElement() and std.mem.eql(u8, v, try n.attr("id") orelse return false),
|
||||||
.class => |v| return n.isElement() and word(try n.attr("class") orelse return false, v, false),
|
.class => |v| return n.isElement() and word(try n.attr("class") orelse return false, v, false),
|
||||||
@@ -794,8 +794,8 @@ pub const Selector = union(enum) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(sel: Selector, alloc: std.mem.Allocator) void {
|
pub fn deinit(sel: *const Selector, alloc: std.mem.Allocator) void {
|
||||||
switch (sel) {
|
switch (sel.*) {
|
||||||
.group => |v| {
|
.group => |v| {
|
||||||
for (v) |vv| vv.deinit(alloc);
|
for (v) |vv| vv.deinit(alloc);
|
||||||
alloc.free(v);
|
alloc.free(v);
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ pub fn querySelector(alloc: std.mem.Allocator, n: *parser.Node, selector: []cons
|
|||||||
|
|
||||||
var m = MatchFirst{};
|
var m = MatchFirst{};
|
||||||
|
|
||||||
_ = try css.matchFirst(ps, Node{ .node = n }, &m);
|
_ = try css.matchFirst(&ps, Node{ .node = n }, &m);
|
||||||
return m.n;
|
return m.n;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -75,6 +75,6 @@ pub fn querySelectorAll(alloc: std.mem.Allocator, n: *parser.Node, selector: []c
|
|||||||
var m = MatchAll.init(alloc);
|
var m = MatchAll.init(alloc);
|
||||||
defer m.deinit();
|
defer m.deinit();
|
||||||
|
|
||||||
try css.matchAll(ps, Node{ .node = n }, &m);
|
try css.matchAll(&ps, Node{ .node = n }, &m);
|
||||||
return m.toOwnedList();
|
return m.toOwnedList();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user