Skip to content

Lightpanda: the headless browser designed for AI and automation

selector: tell custom and unknown elements apart in *-of-type

Jay Hemnanica0df0188bb8
diff --git a/src/browser/SelectorPath.zig b/src/browser/SelectorPath.zig
index c5713cd51..7cf340f41 100644
--- a/src/browser/SelectorPath.zig
+++ b/src/browser/SelectorPath.zig
@@ -260,3 +260,8 @@ test "SelectorPath: shared attribute, first match vs :has() disambiguation" {
"form:has(input[type=\"submit\"][value=\"create account\"]) input[name=\"acct\"]",
);
}
+
+test "SelectorPath: nth-of-type among custom elements" {
+ // <x-head> is a different type, so the 2nd <x-card> is nth-of-type(2).
+ try expectSelector("x-card:last-child", "x-card:nth-of-type(2)");
+}
diff --git a/src/browser/tests/element/pseudo_classes.html b/src/browser/tests/element/pseudo_classes.html
index b3cb1c039..546f1d5c7 100644
--- a/src/browser/tests/element/pseudo_classes.html
+++ b/src/browser/tests/element/pseudo_classes.html
@@ -164,3 +164,31 @@
testing.expectEqual('1', texts('#nth_list li:nth-last-of-type( 3 )'));
}
</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">
+{
+ const texts = (sel) => Array.from(document.querySelectorAll(sel)).map((e) => e.textContent).join();
+ testing.expectEqual('a1,b1,f1,r1', texts('#of_type_custom > :first-of-type'));
+ testing.expectEqual('a1,f1,b2,r1', texts('#of_type_custom > :last-of-type'));
+ testing.expectEqual('a1,f1,r1', texts('#of_type_custom > :only-of-type'));
+ testing.expectEqual('b1', texts('#of_type_custom x-b:nth-of-type(1)'));
+ testing.expectEqual('b2', texts('#of_type_custom x-b:nth-of-type(2)'));
+ testing.expectEqual('b1', texts('#of_type_custom x-b:nth-last-of-type(2)'));
+ testing.expectEqual('r1', texts('#of_type_custom bar:first-of-type'));
+
+ // enough siblings to go through the cached ordinals
+ const many = $('#of_type_custom_many');
+ for (let i = 1; i <= 40; i++) {
+ const el = document.createElement(i % 2 ? 'x-a' : 'x-b');
+ el.textContent = i;
+ many.appendChild(el);
+ }
+ testing.expectEqual('2', texts('#of_type_custom_many > x-b:nth-of-type(1)'));
+ testing.expectEqual('6', texts('#of_type_custom_many > x-b:nth-of-type(3)'));
+ testing.expectEqual('38', texts('#of_type_custom_many > x-b:nth-last-of-type(2)'));
+ testing.expectEqual('1,2', texts('#of_type_custom_many > :first-of-type'));
+ testing.expectEqual('39,40', texts('#of_type_custom_many > :last-of-type'));
+}
+</script>
diff --git a/src/browser/tests/selector_path.html b/src/browser/tests/selector_path.html
index 691608344..a6842bbc5 100644
--- a/src/browser/tests/selector_path.html
+++ b/src/browser/tests/selector_path.html
@@ -25,5 +25,6 @@
<input name="acct">
<input type="submit" value="create account">
</form>
+ <div><x-head>H</x-head><x-card>1</x-card><x-card>2</x-card></div>
</body>
</html>
diff --git a/src/browser/webapi/selector/List.zig b/src/browser/webapi/selector/List.zig
index c805deb46..7d90fb67c 100644
--- a/src/browser/webapi/selector/List.zig
+++ b/src/browser/webapi/selector/List.zig
@@ -795,8 +795,17 @@ fn isLastChild(el: *Node.Element) bool {
return true;
}
+// Custom and unknown elements share a Tag, so those compare by name.
+fn isSameType(a: *Node.Element, b: *Node.Element) bool {
+ const tag = a.getTag();
+ if (b.getTag() != tag) return false;
+ return switch (tag) {
+ .custom, .unknown => std.mem.eql(u8, a.getTagNameLower(), b.getTagNameLower()),
+ else => true,
+ };
+}
+
fn isFirstOfType(el: *Node.Element) bool {
- const tag = el.getTag();
const node = el.asNode();
var sibling = node.previousSibling();
@@ -807,7 +816,7 @@ fn isFirstOfType(el: *Node.Element) bool {
continue;
};
- if (sibling_el.getTag() == tag) {
+ if (isSameType(sibling_el, el)) {
return false;
}
@@ -818,7 +827,6 @@ fn isFirstOfType(el: *Node.Element) bool {
}
fn isLastOfType(el: *Node.Element) bool {
- const tag = el.getTag();
const node = el.asNode();
var sibling = node.nextSibling();
@@ -829,7 +837,7 @@ fn isLastOfType(el: *Node.Element) bool {
continue;
};
- if (sibling_el.getTag() == tag) {
+ if (isSameType(sibling_el, el)) {
return false;
}
@@ -918,7 +926,6 @@ fn getTypeIndex(el: *Node.Element, nth: ?*NthCache) usize {
return o.of_type;
}
- const tag = el.getTag();
const node = el.asNode();
var index: usize = 1;
@@ -933,7 +940,7 @@ fn getTypeIndex(el: *Node.Element, nth: ?*NthCache) usize {
continue;
};
- if (sibling_el.getTag() == tag) {
+ if (isSameType(sibling_el, el)) {
index += 1;
}
@@ -955,7 +962,6 @@ fn getTypeIndexFromEnd(el: *Node.Element, nth: ?*NthCache) usize {
return o.of_type_from_end;
}
- const tag = el.getTag();
const node = el.asNode();
var index: usize = 1;
@@ -970,7 +976,7 @@ fn getTypeIndexFromEnd(el: *Node.Element, nth: ?*NthCache) usize {
continue;
};
- if (sibling_el.getTag() == tag) {
+ if (isSameType(sibling_el, el)) {
index += 1;
}
@@ -1045,7 +1051,19 @@ pub const NthCache = struct {
of_type_from_end: u32,
};
- const TypeCounts = [@typeInfo(Node.Element.Tag).@"enum".fields.len]u32;
+ const TypeCounts = struct {
+ tags: [@typeInfo(Node.Element.Tag).@"enum".fields.len]u32 = @splat(0),
+ // Custom and unknown elements share a Tag, so those count by name.
+ names: std.StringHashMapUnmanaged(u32) = .empty,
+
+ fn get(self: *TypeCounts, allocator: std.mem.Allocator, el: *Node.Element) !*u32 {
+ const tag = el.getTag();
+ return switch (tag) {
+ .custom, .unknown => (try self.names.getOrPutValue(allocator, el.getTagNameLower(), 0)).value_ptr,
+ else => &self.tags[@intFromEnum(tag)],
+ };
+ }
+ };
pub fn deinit(self: *NthCache) void {
self.entries.deinit(self.allocator);
@@ -1064,13 +1082,14 @@ pub const NthCache = struct {
fn index(self: *NthCache, parent: *Node) !void {
var child_count: u32 = 0;
- var type_counts: TypeCounts = @splat(0);
+ var type_counts: TypeCounts = .{};
+ defer type_counts.names.deinit(self.allocator);
var it = parent.childrenIterator();
while (it.next()) |child| {
const el = child.is(Node.Element) orelse continue;
child_count += 1;
- const of_type = &type_counts[@intFromEnum(el.getTag())];
+ const of_type = try type_counts.get(self.allocator, el);
of_type.* += 1;
try self.entries.put(self.allocator, child, .{
.child = child_count,
@@ -1082,17 +1101,14 @@ pub const NthCache = struct {
// The totals are only known once the forward pass is done.
var seen_count: u32 = 0;
- var seen_types: TypeCounts = @splat(0);
it = parent.childrenIterator();
while (it.next()) |child| {
const el = child.is(Node.Element) orelse continue;
seen_count += 1;
- const tag = @intFromEnum(el.getTag());
- seen_types[tag] += 1;
const ordinals = self.entries.getPtr(child).?;
ordinals.child_from_end = child_count - seen_count + 1;
- ordinals.of_type_from_end = type_counts[tag] - seen_types[tag] + 1;
+ ordinals.of_type_from_end = (try type_counts.get(self.allocator, el)).* - ordinals.of_type + 1;
}
}
};