Lightpanda: the headless browser designed for AI and automation
selector: support :nth-child(An+B of S) and :nth-last-child(An+B of S)
jayhemnani9910bfac5c58f20f
diff --git a/src/browser/StyleManager.zig b/src/browser/StyleManager.zigindex e6cb09fe1..54b7769ce 100644--- a/src/browser/StyleManager.zig+++ b/src/browser/StyleManager.zig@@ -1425,6 +1425,17 @@ fn countCompoundSpecificity(compound: Selector.Compound, ids: *u32, classes: *u3classes.* += (max_nested >> 10) & 0x3FF;elements.* += max_nested & 0x3FF;},+ // :nth-child(An+B of S) is a pseudo-class plus its most specific argument+ .nth_child_of, .nth_last_child_of => |nth_of| {+ var max_nested: u32 = 0;+ for (nth_of.selectors) |nested_sel| {+ const spec = computeSpecificity(nested_sel);+ if (spec > max_nested) max_nested = spec;+ }+ ids.* += (max_nested >> 20) & 0x3FF;+ classes.* += 1 + ((max_nested >> 10) & 0x3FF);+ elements.* += max_nested & 0x3FF;+ },// All other pseudo-classes count as class-level specificityelse => classes.* += 1,}diff --git a/src/browser/tests/element/pseudo_classes.html b/src/browser/tests/element/pseudo_classes.htmlindex 90134cd95..8f8228e71 100644--- a/src/browser/tests/element/pseudo_classes.html+++ b/src/browser/tests/element/pseudo_classes.html@@ -165,6 +165,32 @@}</script>+<ul id="nth_of_list"><li>1</li><li class="y">2</li><li>3</li><li class="y">4</li><li class="y">5</li></ul>+<script id="nth_child_of">+{+ const texts = (sel) => Array.from(document.querySelectorAll(sel)).map((li) => li.textContent).join();+ testing.expectEqual('4', texts('#nth_of_list li:nth-child(2 of .y)'));+ testing.expectEqual('2,5', texts('#nth_of_list li:nth-child(odd of .y)'));+ testing.expectEqual('2,5', texts('#nth_of_list li:nth-child(2n+1 of .y)'));+ testing.expectEqual('4', texts('#nth_of_list li:nth-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('4', texts('#nth_of_list li:nth-last-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('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)'));++ testing.expectError("SyntaxError", () => document.querySelector(':nth-child(2 of)'));+ 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)'));+}+</script>+<div id="of_type_custom"><x-a>a1</x-a><x-b>b1</x-b><foo>f1</foo><x-b>b2</x-b><bar>r1</bar></div><div id="of_type_custom_many"></div><script id="of_type_custom">diff --git a/src/browser/webapi/selector/List.zig b/src/browser/webapi/selector/List.zigindex 2e14a8df7..bb5986ca4 100644--- a/src/browser/webapi/selector/List.zig+++ b/src/browser/webapi/selector/List.zig@@ -661,6 +661,8 @@ fn matchesPseudoClass(el: *Node.Element, pseudo: Selector.PseudoClass, scope: *N.only_of_type => return isFirstOfType(el) and isLastOfType(el),.nth_child => |pattern| return matchesNthChild(el, pattern, nth),.nth_last_child => |pattern| return matchesNthLastChild(el, pattern, nth),+ .nth_child_of => |nth_of| return matchesNthChildOf(el, nth_of, false, scope, nth, frame),+ .nth_last_child_of => |nth_of| return matchesNthChildOf(el, nth_of, true, scope, nth, frame),.nth_of_type => |pattern| return matchesNthOfType(el, pattern, nth),.nth_last_of_type => |pattern| return matchesNthLastOfType(el, pattern, nth),@@ -851,6 +853,32 @@ fn matchesNthLastChild(el: *Node.Element, pattern: Selector.NthPattern, nth: ?*Nreturn matchesNthPattern(getChildIndexFromEnd(el, nth), pattern);}+fn matchesNthChildOf(el: *Node.Element, nth_of: Selector.NthOf, from_end: bool, scope: *Node, nth: ?*NthCache, frame: *Frame) bool {+ const node = el.asNode();+ if (!matchesAny(node, nth_of.selectors, scope, nth, frame)) {+ return false;+ }++ var index: usize = 1;+ var sibling = if (from_end) node.nextSibling() else node.previousSibling();+ while (sibling) |s| {+ if (s.is(Node.Element) != null and matchesAny(s, nth_of.selectors, scope, nth, frame)) {+ index += 1;+ }+ sibling = if (from_end) s.nextSibling() else s.previousSibling();+ }+ return matchesNthPattern(index, nth_of.pattern);+}++fn matchesAny(node: *Node, selectors: []const Selector.Selector, scope: *Node, nth: ?*NthCache, frame: *Frame) bool {+ for (selectors) |selector| {+ if (matches(node, selector, scope, nth, frame)) {+ return true;+ }+ }+ return false;+}+fn matchesNthOfType(el: *Node.Element, pattern: Selector.NthPattern, nth: ?*NthCache) bool {return matchesNthPattern(getTypeIndex(el, nth), pattern);}diff --git a/src/browser/webapi/selector/Parser.zig b/src/browser/webapi/selector/Parser.zigindex e385d628b..25fdd2b57 100644--- a/src/browser/webapi/selector/Parser.zig+++ b/src/browser/webapi/selector/Parser.zig@@ -530,17 +530,21 @@ fn pseudoClass(self: *Parser, arena: Allocator) !Selector.PseudoClass {if (std.mem.eql(u8, name, "nth-child")) {const pattern = try self.parseNthPattern();+ const of = try self.parseNthOf(arena);_ = self.skipSpaces();if (self.peek() != ')') return error.InvalidPseudoClass;self.input = self.input[1..];+ if (of) |selectors| return .{ .nth_child_of = .{ .pattern = pattern, .selectors = selectors } };return .{ .nth_child = pattern };}if (std.mem.eql(u8, name, "nth-last-child")) {const pattern = try self.parseNthPattern();+ const of = try self.parseNthOf(arena);_ = self.skipSpaces();if (self.peek() != ')') return error.InvalidPseudoClass;self.input = self.input[1..];+ if (of) |selectors| return .{ .nth_last_child_of = .{ .pattern = pattern, .selectors = selectors } };return .{ .nth_last_child = pattern };}@@ -777,6 +781,39 @@ fn pseudoClass(self: *Parser, arena: Allocator) !Selector.PseudoClass {return error.UnknownPseudoClass;}+// Parses the optional "of S" part of :nth-child() and :nth-last-child()+fn parseNthOf(self: *Parser, arena: Allocator) !?[]const Selector.Selector {+ // "of" needs whitespace before it; parseNthPattern may already have skipped it+ const spaced = std.ascii.isWhitespace((self.input.ptr - 1)[0]);+ if (!self.skipSpacesConsumed() and !spaced) {+ return null;+ }+ const input = self.input;+ if (input.len < 3 or !std.mem.eql(u8, input[0..2], "of")) {+ return null;+ }+ // "of" followed by a name character is a longer identifier, not the keyword+ const c = input[2];+ if (std.ascii.isAlphanumeric(c) or c == '-' or c == '_' or c == '\\' or c >= 0x80) {+ return null;+ }+ self.input = input[2..];+ _ = self.skipSpaces();++ var selectors: std.ArrayList(Selector.Selector) = .empty;+ while (true) {+ if (self.peek() == ')' or self.peek() == 0) return error.InvalidPseudoClass;++ try selectors.append(arena, try parse(arena, self.consumeUntilCommaOrParen()));++ _ = self.skipSpaces();+ if (self.peek() != ',') break;+ self.input = self.input[1..];+ _ = self.skipSpaces();+ }+ return selectors.items;+}+fn parseNthPattern(self: *Parser) !Selector.NthPattern {_ = self.skipSpaces();diff --git a/src/browser/webapi/selector/Selector.zig b/src/browser/webapi/selector/Selector.zigindex 477b86b50..7bfd10a48 100644--- a/src/browser/webapi/selector/Selector.zig+++ b/src/browser/webapi/selector/Selector.zig@@ -340,6 +340,8 @@ pub const PseudoClass = union(enum) {only_of_type,nth_child: NthPattern,nth_last_child: NthPattern,+ nth_child_of: NthOf, // :nth-child(An+B of S)+ nth_last_child_of: NthOf, // :nth-last-child(An+B of S)nth_of_type: NthPattern,nth_last_of_type: NthPattern,@@ -365,6 +367,12 @@ pub const NthPattern = struct {// 5: a=0, b=5};+// :nth-child(An+B of S) counts only the siblings matching S+pub const NthOf = struct {+ pattern: NthPattern,+ selectors: []const Selector,+};+// Combinator represents the relationship between two compound selectorspub const Combinator = enum {descendant, // ' ' - any descendant