@@ -37,7 +37,7 @@ const empty = []
3737 */
3838export function walk ( state , tree ) {
3939 if ( tree ) {
40- one ( state , [ ] , tree , undefined , undefined )
40+ one ( state , [ ] , tree , undefined , undefined , tree )
4141 }
4242}
4343
@@ -49,9 +49,10 @@ export function walk(state, tree) {
4949 * @param {Node } node
5050 * @param {number | undefined } index
5151 * @param {Parent | undefined } parentNode
52+ * @param {Node } tree
5253 * @returns {Nest }
5354 */
54- function one ( state , currentRules , node , index , parentNode ) {
55+ function one ( state , currentRules , node , index , parentNode , tree ) {
5556 /** @type {Nest } */
5657 let nestResult = {
5758 directChild : undefined ,
@@ -60,10 +61,23 @@ function one(state, currentRules, node, index, parentNode) {
6061 generalSibling : undefined
6162 }
6263
64+ let rootRules = state . rootQuery . rules
65+
66+ // Remove direct child rules if this is the root.
67+ // This only happens for a `:has()` rule, which can be like
68+ // `a:has(> b)`.
69+ if ( parentNode && parentNode !== tree ) {
70+ rootRules = state . rootQuery . rules . filter (
71+ ( d ) =>
72+ d . combinator === undefined ||
73+ ( d . combinator === '>' && parentNode === tree )
74+ )
75+ }
76+
6377 nestResult = applySelectors (
6478 state ,
6579 // Try the root rules for this node too.
66- combine ( currentRules , state . rootQuery . rules ) ,
80+ combine ( currentRules , rootRules ) ,
6781 node ,
6882 index ,
6983 parentNode
@@ -72,7 +86,7 @@ function one(state, currentRules, node, index, parentNode) {
7286 // If this is a parent, and we want to delve into them, and we haven’t found
7387 // our single result yet.
7488 if ( parent ( node ) && ! state . shallow && ! ( state . one && state . found ) ) {
75- all ( state , nestResult , node )
89+ all ( state , nestResult , node , tree )
7690 }
7791
7892 return nestResult
@@ -84,9 +98,10 @@ function one(state, currentRules, node, index, parentNode) {
8498 * @param {SelectState } state
8599 * @param {Nest } nest
86100 * @param {Parent } node
101+ * @param {Node } tree
87102 * @returns {undefined }
88103 */
89- function all ( state , nest , node ) {
104+ function all ( state , nest , node , tree ) {
90105 const fromParent = combine ( nest . descendant , nest . directChild )
91106 /** @type {Array<AstRule> | undefined } */
92107 let fromSibling
@@ -121,7 +136,7 @@ function all(state, nest, node) {
121136
122137 // Only apply if this is a parent.
123138 const forSibling = combine ( fromParent , fromSibling )
124- const nest = one ( state , forSibling , node . children [ index ] , index , node )
139+ const nest = one ( state , forSibling , node . children [ index ] , index , node , tree )
125140 fromSibling = combine ( nest . generalSibling , nest . adjacentSibling )
126141
127142 // We found one thing, and one is enough.
@@ -208,7 +223,7 @@ function applySelectors(state, rules, node, index, parent) {
208223 else if ( rule . combinator === '~' ) {
209224 add ( nestResult , 'generalSibling' , rule )
210225 }
211- // Drop top-level nesting (`undefined`), direct child (`>`), adjacent sibling (`+`).
226+ // Drop direct child (`>`), adjacent sibling (`+`).
212227 }
213228
214229 return nestResult
0 commit comments