Skip to content

Commit d48cdc0

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 fac9687 commit d48cdc0

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
@@ -179,7 +179,7 @@ public function find($selector): DOMTraverser
179179
* not be reused.
180180
*
181181
* @param iterable $handler
182-
* A Selector, or an array of SimpleSelector[] groups in the same order.
182+
* The SimpleSelector[] groups, as a Selector yields them.
183183
*
184184
* @return DOMTraverser
185185
* @throws ParseException

‎src/CSS/DOMTraverser/PseudoClass.php‎

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -409,29 +409,28 @@ protected function containsExactly($node, $value): bool
409409
*/
410410
protected function has($node, $selector): bool
411411
{
412-
foreach ($this->relativeSelector($selector) as [$group, $combinator, $nextOnly, $descends]) {
413-
if (! ($combinator & (SimpleSelector::ADJACENT | SimpleSelector::SIBLING))) {
412+
foreach ($this->relativeSelector($selector) as [$group, $siblings, $nextOnly, $descends]) {
413+
if (! $siblings) {
414414
if ($this->findsAny($node, $node, $group, false)) {
415415
return true;
416416
}
417-
continue;
418-
}
419-
420-
// Search the later siblings one at a time, so a match near the node ends the search.
421-
for ($next = $node->nextSibling; $next !== null; $next = $next->nextSibling) {
422-
if (! $next instanceof DOMElement) {
423-
continue;
424-
}
425-
426-
// The sibling can match itself, and its descendants when the group goes on to them (`+ ul li`).
427-
if ($this->findsAny($next, $node, $group, true)
428-
|| ($descends && $this->findsAny($next, $node, $group, false))
429-
) {
430-
return true;
431-
}
432-
433-
if ($nextOnly) {
434-
break;
417+
} else {
418+
// Search the later siblings one at a time, so a match near the node ends the search.
419+
for ($next = $node->nextSibling; $next !== null; $next = $next->nextSibling) {
420+
if (! $next instanceof DOMElement) {
421+
continue;
422+
}
423+
424+
// The sibling can match itself, and its descendants when the group goes on to them (`+ ul li`).
425+
if ($this->findsAny($next, $node, $group, true)
426+
|| ($descends && $this->findsAny($next, $node, $group, false))
427+
) {
428+
return true;
429+
}
430+
431+
if ($nextOnly) {
432+
break;
433+
}
435434
}
436435
}
437436
}
@@ -446,8 +445,8 @@ protected function has($node, $selector): bool
446445
* which becomes :scope. Any other group (`li`) is a descendant of :scope.
447446
*
448447
* @return array
449-
* One [group, anchor combinator, whether only the next sibling can match, whether the group
450-
* reaches below the sibling it starts from] per group.
448+
* One [group, whether it starts at a later sibling (`+`/`~`), whether only the next sibling
449+
* can match, whether it reaches below the sibling it starts from] per group.
451450
* @throws ParseException
452451
*/
453452
protected function relativeSelector($selector): array
@@ -483,7 +482,7 @@ protected function relativeSelector($selector): array
483482
}
484483
}
485484

486-
$groups[] = [$group, $anchor->combinator, $nextOnly, $descends];
485+
$groups[] = [$group, (bool) ($anchor->combinator & $sibling), $nextOnly, $descends];
487486
}
488487

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

0 commit comments

Comments
 (0)