Lightpanda: the headless browser designed for AI and automation
perf: optimize some subsequent sibling selector cases
Karl Seguin87e043ea9454
diff --git a/src/browser/tests/document/query_selector_edge_cases.html b/src/browser/tests/document/query_selector_edge_cases.htmlindex e700cb9ba..0e47c906c 100644--- a/src/browser/tests/document/query_selector_edge_cases.html+++ b/src/browser/tests/document/query_selector_edge_cases.html@@ -201,3 +201,34 @@testing.expectEqual(true, mixedUniversal.length >= 1);}</script>++<script id=subsequentSiblingSharedAncestry>+{+ const make = (html) => {+ const div = document.createElement('div');+ div.innerHTML = html;+ return div;+ };++ // Every candidate sibling shares the parent, so the first one decides+ const spaced = make('<div class=space><i>1</i><i hidden>2</i><i>3</i><i>4</i></div><div class=other><i>5</i><i>6</i></div>');+ assertList(['3', '4'], spaced.querySelectorAll('.space > :not([hidden]) ~ :not([hidden])'));+ assertList(['3', '4'], spaced.querySelectorAll('.space :not([hidden]) ~ :not([hidden])'));+ assertList(['6'], spaced.querySelectorAll('.other > * ~ *'));+ assertList([], spaced.querySelectorAll('.missing > * ~ *'));+ assertList(['3', '4'], spaced.firstChild.querySelectorAll(':scope > :not([hidden]) ~ :not([hidden])'));++ // A sibling hop to the left still needs every candidate tried: the+ // nearest .b fails (its previous sibling isn't .a), an earlier one matches+ const backtrack = make('<i class=a></i><i class=b>1</i><i class=b>2</i><i class=c>3</i>');+ assertList(['3'], backtrack.querySelectorAll('.a + .b ~ .c'));++ // A `~` hop to the left: the nearest candidate searched a superset of the+ // siblings an earlier one would, wherever a `+` sits further left+ const chained = make('<i class=x></i><i class=y></i><i class=b>1</i><i class=b>2</i><i class=c>3</i><i class=y></i><i class=b>4</i><i class=c>5</i>');+ assertList(['2', '4'], chained.querySelectorAll('.y ~ .b ~ .b'));+ assertList(['3', '5'], chained.querySelectorAll('.x + .y ~ .b ~ .c'));+ assertList(['3', '5'], chained.querySelectorAll('div > .x ~ .b ~ .c'));+ assertList([], chained.querySelectorAll('.missing > .x ~ .b ~ .c'));+}+</script>diff --git a/src/browser/webapi/selector/List.zig b/src/browser/webapi/selector/List.zigindex d5e89d01f..35baf8b73 100644--- a/src/browser/webapi/selector/List.zig+++ b/src/browser/webapi/selector/List.zig@@ -304,7 +304,19 @@ fn matchSegments(node: *Node, selector: Selector.Selector, segment_index: usize,.child => matchChild(node, target_compound, root, scope, nth, frame),.next_sibling => matchNextSibling(node, target_compound, scope, nth, frame),.subsequent_sibling => {- // For subsequent_sibling, try all matching siblings with backtracking+ // For subsequent_sibling, try all matching siblings with backtracking.+ const first_decides = blk: {+ // Given #c > .a ~ .b+ // We don't have to visit each of .b's siblings if the parent+ // of the first matching .a sibling isn't `#c`..because every+ // sibling shares the same parent, so if it isn't true for one+ // it isn't true for any of them. Only a `+`, checking each+ // candidate's own neighbour, needs every candidate tried.+ if (segment_index == 0) {+ break :blk true;+ }+ break :blk selector.segments[segment_index - 1].combinator != .next_sibling;+ };var sibling = node.previousSibling();while (sibling) |s| {const sibling_el = s.is(Node.Element) orelse {@@ -321,6 +333,9 @@ fn matchSegments(node: *Node, selector: Selector.Selector, segment_index: usize,if (matchSegments(s, selector, segment_index - 1, root, scope, nth, frame)) {return true;}+ if (first_decides) {+ return false;+ }// This sibling didn't work, try the next one}sibling = s.previousSibling();