@@ -44,7 +44,7 @@ const empty = []
4444 */
4545export function walk ( state , tree ) {
4646 if ( tree ) {
47- one ( state , [ ] , tree , undefined , undefined )
47+ one ( state , [ ] , tree , undefined , undefined , tree )
4848 }
4949}
5050
@@ -76,10 +76,12 @@ function add(nest, field, rule) {
7676 * Nesting.
7777 * @param {Parents } node
7878 * Parent.
79+ * @param {Nodes } tree
80+ * Tree.
7981 * @returns {undefined }
8082 * Nothing.
8183 */
82- function all ( state , nest , node ) {
84+ function all ( state , nest , node , tree ) {
8385 const fromParent = combine ( nest . descendant , nest . directChild )
8486 /** @type {Array<AstRule> | undefined } */
8587 let fromSibling
@@ -118,7 +120,14 @@ function all(state, nest, node) {
118120 // for parents so that we delve into custom nodes too.
119121 if ( 'children' in child ) {
120122 const forSibling = combine ( fromParent , fromSibling )
121- const nest = one ( state , forSibling , node . children [ index ] , index , node )
123+ const nest = one (
124+ state ,
125+ forSibling ,
126+ node . children [ index ] ,
127+ index ,
128+ node ,
129+ tree
130+ )
122131 fromSibling = combine ( nest . generalSibling , nest . adjacentSibling )
123132 }
124133
@@ -268,10 +277,12 @@ function count(counts, node) {
268277 * Index of `node` in `parent`.
269278 * @param {Parents | undefined } parent
270279 * Parent of `node`.
280+ * @param {Nodes } tree
281+ * Tree.
271282 * @returns {Nest }
272283 * Nesting.
273284 */
274- function one ( state , currentRules , node , index , parent ) {
285+ function one ( state , currentRules , node , index , parent , tree ) {
275286 /** @type {Nest } */
276287 let nestResult = {
277288 adjacentSibling : undefined ,
@@ -283,10 +294,23 @@ function one(state, currentRules, node, index, parent) {
283294 const exit = enterState ( state , node )
284295
285296 if ( node . type === 'element' ) {
297+ let rootRules = state . rootQuery . rules
298+
299+ // Remove direct child rules if this is the root.
300+ // This only happens for a `:has()` rule, which can be like
301+ // `a:has(> b)`.
302+ if ( parent && parent !== tree ) {
303+ rootRules = state . rootQuery . rules . filter (
304+ ( d ) =>
305+ d . combinator === undefined ||
306+ ( d . combinator === '>' && parent === tree )
307+ )
308+ }
309+
286310 nestResult = applySelectors (
287311 state ,
288312 // Try the root rules for this element too.
289- combine ( currentRules , state . rootQuery . rules ) ,
313+ combine ( currentRules , rootRules ) ,
290314 node ,
291315 index ,
292316 parent
@@ -296,7 +320,7 @@ function one(state, currentRules, node, index, parent) {
296320 // If this is a parent, and we want to delve into them, and we haven’t found
297321 // our single result yet.
298322 if ( 'children' in node && ! state . shallow && ! ( state . one && state . found ) ) {
299- all ( state , nestResult , node )
323+ all ( state , nestResult , node , tree )
300324 }
301325
302326 exit ( )
0 commit comments