Skip to content

Lightpanda: the headless browser designed for AI and automation

selector: allow whitespace before ) in :nth-* pseudo-classes

Jay Hemnani2b182c69320c
diff --git a/src/browser/tests/element/pseudo_classes.html b/src/browser/tests/element/pseudo_classes.html
index 11bac08a2..b3cb1c039 100644
--- a/src/browser/tests/element/pseudo_classes.html
+++ b/src/browser/tests/element/pseudo_classes.html
@@ -151,3 +151,16 @@
testing.expectEqual('rw_readonly,rw_disabled,rw_checkbox,rw_textarea_ro,rw_not_editable,rw_not_editable_child,rw_p', ids(':read-only'));
}
</script>
+
+<ul id="nth_list"><li>1</li><li>2</li><li>3</li></ul>
+<script id="nth_whitespace">
+{
+ const texts = (sel) => Array.from(document.querySelectorAll(sel)).map((li) => li.textContent).join();
+ testing.expectEqual('1,3', texts('#nth_list li:nth-child(odd )'));
+ testing.expectEqual('1,3', texts('#nth_list li:nth-child( 2n+1 )'));
+ testing.expectEqual('2', texts('#nth_list li:nth-child( 2 )'));
+ testing.expectEqual('2', texts('#nth_list li:nth-last-child(even )'));
+ testing.expectEqual('3', texts('#nth_list li:nth-of-type( 3 )'));
+ testing.expectEqual('1', texts('#nth_list li:nth-last-of-type( 3 )'));
+}
+</script>
diff --git a/src/browser/webapi/selector/Parser.zig b/src/browser/webapi/selector/Parser.zig
index fa2cad3ac..8b995ee42 100644
--- a/src/browser/webapi/selector/Parser.zig
+++ b/src/browser/webapi/selector/Parser.zig
@@ -429,6 +429,7 @@ fn pseudoClass(self: *Parser, arena: Allocator) !Selector.PseudoClass {
if (std.mem.eql(u8, name, "nth-child")) {
const pattern = try self.parseNthPattern();
+ _ = self.skipSpaces();
if (self.peek() != ')') return error.InvalidPseudoClass;
self.input = self.input[1..];
return .{ .nth_child = pattern };
@@ -436,6 +437,7 @@ fn pseudoClass(self: *Parser, arena: Allocator) !Selector.PseudoClass {
if (std.mem.eql(u8, name, "nth-last-child")) {
const pattern = try self.parseNthPattern();
+ _ = self.skipSpaces();
if (self.peek() != ')') return error.InvalidPseudoClass;
self.input = self.input[1..];
return .{ .nth_last_child = pattern };
@@ -443,6 +445,7 @@ fn pseudoClass(self: *Parser, arena: Allocator) !Selector.PseudoClass {
if (std.mem.eql(u8, name, "nth-of-type")) {
const pattern = try self.parseNthPattern();
+ _ = self.skipSpaces();
if (self.peek() != ')') return error.InvalidPseudoClass;
self.input = self.input[1..];
return .{ .nth_of_type = pattern };
@@ -450,6 +453,7 @@ fn pseudoClass(self: *Parser, arena: Allocator) !Selector.PseudoClass {
if (std.mem.eql(u8, name, "nth-last-of-type")) {
const pattern = try self.parseNthPattern();
+ _ = self.skipSpaces();
if (self.peek() != ')') return error.InvalidPseudoClass;
self.input = self.input[1..];
return .{ .nth_last_of_type = pattern };