Lightpanda: the headless browser designed for AI and automation
Add early stop in matchesNthChildOf
Karl Seguin6df3ec542e9c
diff --git a/src/browser/tests/element/pseudo_classes.html b/src/browser/tests/element/pseudo_classes.htmlindex 8f8228e71..c17cd3326 100644--- a/src/browser/tests/element/pseudo_classes.html+++ b/src/browser/tests/element/pseudo_classes.html@@ -176,10 +176,14 @@testing.expectEqual('4', texts('#nth_of_list li:nth-child(2 of.y)'));testing.expectEqual('3', texts('#nth_of_list li:nth-child(2 of:not(.y))'));testing.expectEqual('4', texts('#nth_of_list li:nth-last-child(2 of .y)'));+ testing.expectEqual('4', texts('#nth_of_list li:nth-child(2 OF .y)'));testing.expectEqual('3', texts('#nth_of_list li:nth-child(2 of :not(.y))'));testing.expectEqual('2,4', texts('#nth_of_list li:nth-child(-n+2 of .y, :last-child)'));testing.expectEqual('1,2,3,4,5', texts('#nth_of_list li:nth-child(n of li)'));testing.expectEqual('', texts('#nth_of_list li:nth-child(1 of .z)'));+ testing.expectEqual('', texts('#nth_of_list li:nth-child(0 of .y)'));+ testing.expectEqual('2,5', texts('#nth_of_list li:nth-child(-2n+3 of .y)'));+ testing.expectEqual('5', texts('#nth_of_list li:nth-last-child(-n+1 of .y)'));testing.expectEqual('3', texts('#nth_of_list > :nth-child(1 of .y) + li'));testing.expectEqual(true, $('#nth_of_list').children[3].matches(':nth-child(2 of .y)'));testing.expectEqual(false, $('#nth_of_list').children[2].matches(':nth-child(2 of .y)'));@@ -188,6 +192,8 @@testing.expectError("SyntaxError", () => document.querySelector(':nth-child(2 of .y,)'));testing.expectError("SyntaxError", () => document.querySelector(':nth-child(2of .y)'));testing.expectError("SyntaxError", () => document.querySelector(':nth-of-type(2 of .y)'));+ testing.expectError("SyntaxError", () => document.querySelector(':nth-child(2 of ::before)'));+ testing.expectError("SyntaxError", () => document.querySelector(':nth-child(2 of .y, ::before)'));}</script>diff --git a/src/browser/webapi/selector/List.zig b/src/browser/webapi/selector/List.zigindex bb5986ca4..e0090f1c6 100644--- a/src/browser/webapi/selector/List.zig+++ b/src/browser/webapi/selector/List.zig@@ -854,6 +854,13 @@ fn matchesNthLastChild(el: *Node.Element, pattern: Selector.NthPattern, nth: ?*N}fn matchesNthChildOf(el: *Node.Element, nth_of: Selector.NthOf, from_end: bool, scope: *Node, nth: ?*NthCache, frame: *Frame) bool {+ // With a <= 0 (":nth-child(2 of .y)", "-n+3 of .y") only indexes up to b+ // can match, so stop counting once past it.+ const max_index: usize = if (nth_of.pattern.a > 0) std.math.maxInt(usize) else blk: {+ if (nth_of.pattern.b < 1) return false;+ break :blk @intCast(nth_of.pattern.b);+ };+const node = el.asNode();if (!matchesAny(node, nth_of.selectors, scope, nth, frame)) {return false;@@ -864,6 +871,7 @@ fn matchesNthChildOf(el: *Node.Element, nth_of: Selector.NthOf, from_end: bool,while (sibling) |s| {if (s.is(Node.Element) != null and matchesAny(s, nth_of.selectors, scope, nth, frame)) {index += 1;+ if (index > max_index) return false;}sibling = if (from_end) s.nextSibling() else s.previousSibling();}diff --git a/src/browser/webapi/selector/Parser.zig b/src/browser/webapi/selector/Parser.zigindex 25fdd2b57..78a838624 100644--- a/src/browser/webapi/selector/Parser.zig+++ b/src/browser/webapi/selector/Parser.zig@@ -789,7 +789,7 @@ fn parseNthOf(self: *Parser, arena: Allocator) !?[]const Selector.Selector {return null;}const input = self.input;- if (input.len < 3 or !std.mem.eql(u8, input[0..2], "of")) {+ if (input.len < 3 or !std.ascii.eqlIgnoreCase(input[0..2], "of")) {return null;}// "of" followed by a name character is a longer identifier, not the keyword@@ -804,7 +804,9 @@ fn parseNthOf(self: *Parser, arena: Allocator) !?[]const Selector.Selector {while (true) {if (self.peek() == ')' or self.peek() == 0) return error.InvalidPseudoClass;- try selectors.append(arena, try parse(arena, self.consumeUntilCommaOrParen()));+ const selector = try parse(arena, self.consumeUntilCommaOrParen());+ if (selector.hasPseudoElement()) return error.InvalidPseudoClass;+ try selectors.append(arena, selector);_ = self.skipSpaces();if (self.peek() != ',') break;