Skip to content

Commit 8bfec5c

Browse files
Return whether a :has() group starts at a sibling, not its combinator
has() only ever asked whether the anchor combinator was + or ~, so relativeSelector() now answers that, and the descendant and sibling searches become the two arms of one if/else. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent e0bc054 commit 8bfec5c

2 files changed

Lines changed: 23 additions & 24 deletions

File tree

‎src/CSS/DOMTraverser.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public function find($selector): DOMTraverser
173173
* not be reused.
174174
*
175175
* @param iterable $handler
176-
* A Selector, or an array of SimpleSelector[] groups in the same order.
176+
* The SimpleSelector[] groups, as a Selector yields them.
177177
*
178178
* @return DOMTraverser
179179
* @throws ParseException

‎src/CSS/DOMTraverser/PseudoClass.php‎

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -363,29 +363,28 @@ protected function containsExactly($node, $value): bool
363363
*/
364364
protected function has($node, $selector): bool
365365
{
366-
foreach ($this->relativeSelector($selector) as [$group, $combinator, $nextOnly, $descends]) {
367-
if (! ($combinator & (SimpleSelector::ADJACENT | SimpleSelector::SIBLING))) {
366+
foreach ($this->relativeSelector($selector) as [$group, $siblings, $nextOnly, $descends]) {
367+
if (! $siblings) {
368368
if ($this->findsAny($node, $node, $group, false)) {
369369
return true;
370370
}
371-
continue;
372-
}
373-
374-
// Search the later siblings one at a time, so a match near the node ends the search.
375-
for ($next = $node->nextSibling; $next !== null; $next = $next->nextSibling) {
376-
if (! $next instanceof DOMElement) {
377-
continue;
378-
}
379-
380-
// The sibling can match itself, and its descendants when the group goes on to them (`+ ul li`).
381-
if ($this->findsAny($next, $node, $group, true)
382-
|| ($descends && $this->findsAny($next, $node, $group, false))
383-
) {
384-
return true;
385-
}
386-
387-
if ($nextOnly) {
388-
break;
371+
} else {
372+
// Search the later siblings one at a time, so a match near the node ends the search.
373+
for ($next = $node->nextSibling; $next !== null; $next = $next->nextSibling) {
374+
if (! $next instanceof DOMElement) {
375+
continue;
376+
}
377+
378+
// The sibling can match itself, and its descendants when the group goes on to them (`+ ul li`).
379+
if ($this->findsAny($next, $node, $group, true)
380+
|| ($descends && $this->findsAny($next, $node, $group, false))
381+
) {
382+
return true;
383+
}
384+
385+
if ($nextOnly) {
386+
break;
387+
}
389388
}
390389
}
391390
}
@@ -400,8 +399,8 @@ protected function has($node, $selector): bool
400399
* which becomes :scope. Any other group (`li`) is a descendant of :scope.
401400
*
402401
* @return array
403-
* One [group, anchor combinator, whether only the next sibling can match, whether the group
404-
* reaches below the sibling it starts from] per group.
402+
* One [group, whether it starts at a later sibling (`+`/`~`), whether only the next sibling
403+
* can match, whether it reaches below the sibling it starts from] per group.
405404
* @throws ParseException
406405
*/
407406
protected function relativeSelector($selector): array
@@ -437,7 +436,7 @@ protected function relativeSelector($selector): array
437436
}
438437
}
439438

440-
$groups[] = [$group, $anchor->combinator, $nextOnly, $descends];
439+
$groups[] = [$group, (bool) ($anchor->combinator & $sibling), $nextOnly, $descends];
441440
}
442441

443442
return $this->relativeSelectors[$selector] = $groups;

0 commit comments

Comments
 (0)