@@ -44,7 +44,7 @@ const empty = []
44
44
*/
45
45
export function walk ( state , tree ) {
46
46
if ( tree ) {
47
- one ( state , [ ] , tree , undefined , undefined )
47
+ one ( state , [ ] , tree , undefined , undefined , tree )
48
48
}
49
49
}
50
50
@@ -76,10 +76,12 @@ function add(nest, field, rule) {
76
76
* Nesting.
77
77
* @param {Parents } node
78
78
* Parent.
79
+ * @param {Nodes } tree
80
+ * Tree.
79
81
* @returns {undefined }
80
82
* Nothing.
81
83
*/
82
- function all ( state , nest , node ) {
84
+ function all ( state , nest , node , tree ) {
83
85
const fromParent = combine ( nest . descendant , nest . directChild )
84
86
/** @type {Array<AstRule> | undefined } */
85
87
let fromSibling
@@ -118,7 +120,14 @@ function all(state, nest, node) {
118
120
// for parents so that we delve into custom nodes too.
119
121
if ( 'children' in child ) {
120
122
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
+ )
122
131
fromSibling = combine ( nest . generalSibling , nest . adjacentSibling )
123
132
}
124
133
@@ -268,10 +277,12 @@ function count(counts, node) {
268
277
* Index of `node` in `parent`.
269
278
* @param {Parents | undefined } parent
270
279
* Parent of `node`.
280
+ * @param {Nodes } tree
281
+ * Tree.
271
282
* @returns {Nest }
272
283
* Nesting.
273
284
*/
274
- function one ( state , currentRules , node , index , parent ) {
285
+ function one ( state , currentRules , node , index , parent , tree ) {
275
286
/** @type {Nest } */
276
287
let nestResult = {
277
288
adjacentSibling : undefined ,
@@ -283,10 +294,23 @@ function one(state, currentRules, node, index, parent) {
283
294
const exit = enterState ( state , node )
284
295
285
296
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
+
286
310
nestResult = applySelectors (
287
311
state ,
288
312
// Try the root rules for this element too.
289
- combine ( currentRules , state . rootQuery . rules ) ,
313
+ combine ( currentRules , rootRules ) ,
290
314
node ,
291
315
index ,
292
316
parent
@@ -296,7 +320,7 @@ function one(state, currentRules, node, index, parent) {
296
320
// If this is a parent, and we want to delve into them, and we haven’t found
297
321
// our single result yet.
298
322
if ( 'children' in node && ! state . shallow && ! ( state . one && state . found ) ) {
299
- all ( state , nestResult , node )
323
+ all ( state , nestResult , node , tree )
300
324
}
301
325
302
326
exit ( )
0 commit comments