You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
:has() never matches in find(): qp($xml)->find('ul:has(li)') returns nothing #121
With the current engine (CSS\DOMTraverser, used by find() and the qp()/htmlqp()/html5qp() factories), :has() never matches, whatever its argument. The legacy engine (QueryPathEventHandler, used by remove() and replaceAll()) gets the same selectors right, so the two engines disagree:
$xml = '<?xml version="1.0"?><root><ul id="a"><li/><li/></ul><ul id="b"><li/><li/><li/></ul></root>';
qp($xml)->find('ul:has(li)'); // (none) expected: a, bqp($xml)->remove('ul:has(li)'); // a, bqp($xml)->find('ul:has(li:nth-child(3))'); // (none) expected: bqp($xml)->remove('ul:has(li:nth-child(3))'); // b
Same result through html5qp(), and for body:has(ul), ul:has(> li), ul:has(li:first-child), ul:has(li:eq(1)). Consequently :not(:has(...)) is always true: find('ul:not(:has(li))') returns every ul.
Cause
PseudoClass::has() (src/CSS/DOMTraverser/PseudoClass.php) builds a DOMTraverser in initialized mode over the node itself:
Initialized mode treats the supplied nodes as the candidates (the "is this node a match?" question that is() and filter() ask after #72), so ul:has(li) asks whether the ulis an li. CSS :has() asks whether the element has a descendant (or, for a relative selector like > li, a related element) matching the argument, which is a non-initialized search rooted at the node. Verified: changing that line to a descendant search rooted at the node, new DOMTraverser($splos, false, $node), makes every find() result above agree with remove().
That one-line change is not the fix on its own, though: isNot() is defined as ! $this->has(), and :not() genuinely needs the initialized "is this node a match?" semantics (li:not(.x) asks whether the li is .x, not whether it contains one). The fix needs isNot() to keep the current body, with has() switched to the descendant search, plus tests for both.
Activity