@@ -6,9 +6,10 @@ const COMBINATOR_PATTERN = /[\s>+~]/u;
66const NARROWING_PATTERN = / [ . # [ ] | ^ [ a - z ] / iu;
77const ROOT_COMPOUND_PATTERN = / ^ (?: h t m l | b o d y | : r o o t ) (? ! [ \w - ] ) / iu;
88
9- // group-[...] and peer-[...] match the .group/.peer element; data -[...] and
10- // aria-[...] hold attribute values, not selectors.
9+ // group-[...] and peer-[...] match the .group/.peer element, in -[...] matches an
10+ // ancestor, and data-[...] and aria-[...] hold attribute values, not selectors.
1111const GROUP_PREFIX_PATTERN = / (?: ^ | : ) (?: g r o u p | p e e r ) - $ / u;
12+ const ANCESTOR_PREFIX_PATTERN = / (?: ^ | : ) i n - $ / u;
1213const ATTRIBUTE_PREFIX_PATTERN = / (?: ^ | : ) (?: d a t a | a r i a ) - $ / u;
1314const QUOTED_PATTERN = / ( [ " ' ] ) (?: \\ .| (? ! \1) .) * \1/ gu;
1415
@@ -35,23 +36,62 @@ function variantGroups(token: string): { prefix: string; group: string }[] {
3536 return groups ;
3637}
3738
39+ /** Index of the ")" closing the "(" at `open`, or the selector's length. */
40+ function closingParen ( selector : string , open : number ) : number {
41+ let depth = 0 ;
42+ for ( let index = open ; index < selector . length ; index ++ ) {
43+ if ( selector [ index ] === "(" ) depth ++ ;
44+ else if ( selector [ index ] === ")" && -- depth === 0 ) return index ;
45+ }
46+ return selector . length ;
47+ }
48+
49+ /**
50+ * Whether nothing after `from` in the enclosing branch is joined by a combinator,
51+ * i.e. the element ending at `from` is the subject that its wrapper matches.
52+ */
53+ function isWrapperSubject ( selector : string , from : number ) : boolean {
54+ let depth = 0 ;
55+ for ( let index = from ; index < selector . length ; index ++ ) {
56+ const char = selector [ index ] ?? "" ;
57+ if ( char === "(" ) depth ++ ;
58+ else if ( char === ")" ) {
59+ if ( depth === 0 ) return true ;
60+ depth -- ;
61+ } else if ( depth > 0 ) continue ;
62+ else if ( char === "," ) return true ;
63+ else if ( COMBINATOR_PATTERN . test ( char ) ) {
64+ const next = selector . slice ( index ) . trimStart ( ) [ 0 ] ;
65+ if ( / \s / u. test ( char ) && ( next === undefined || next === ")" || next === "," ) ) continue ;
66+ return false ;
67+ }
68+ }
69+ return true ;
70+ }
71+
3872/** The compound selector each `:has(` in `selector` is attached to. */
3973function hasCompounds ( selector : string ) : string [ ] {
4074 const compounds : string [ ] = [ ] ;
4175 let index = selector . indexOf ( ":has(" ) ;
4276 while ( index !== - 1 ) {
4377 let compound = "" ;
4478 let depth = 0 ;
79+ let subjectEnd = closingParen ( selector , index + ":has" . length ) + 1 ;
4580 // Other branches of a selector list are skipped until the wrapper that holds
4681 // them opens. An unbalanced "(" means the :has() sits inside :not()/:is()/
47- // :where(), so the compound outside that wrapper still applies.
82+ // :where(); the compound outside that wrapper applies only when the :has()
83+ // is in the wrapper's subject position.
4884 let skippingBranch = false ;
4985 for ( let position = index - 1 ; position >= 0 ; position -- ) {
5086 const char = selector [ position ] ?? "" ;
5187 if ( char === ")" ) depth ++ ;
5288 else if ( char === "(" ) {
5389 if ( depth > 0 ) depth -- ;
54- else skippingBranch = false ;
90+ else {
91+ if ( ! isWrapperSubject ( selector , subjectEnd ) ) break ;
92+ skippingBranch = false ;
93+ subjectEnd = closingParen ( selector , position ) + 1 ;
94+ }
5595 } else if ( depth === 0 && char === "," ) {
5696 skippingBranch = true ;
5797 continue ;
@@ -93,12 +133,13 @@ function findUnscopedHasVariants(text: string): string[] {
93133 for ( const { prefix, group } of variantGroups ( token ) ) {
94134 if ( ATTRIBUTE_PREFIX_PATTERN . test ( prefix ) ) continue ;
95135 // Tailwind writes spaces as "_", and "&" is the element carrying the class.
136+ // A selector without "&" applies to that element, as `&:is(...)`.
96137 const relative = group . toLowerCase ( ) . replace ( QUOTED_PATTERN , '""' ) . replaceAll ( "_" , " " ) ;
97138 const owner = GROUP_PREFIX_PATTERN . test ( prefix ) ? ".group" : ".self" ;
98- const selector = relative . includes ( "&" )
99- ? relative . replaceAll ( "&" , owner )
100- : owner === ".self"
101- ? relative
139+ const selector = ANCESTOR_PREFIX_PATTERN . test ( prefix )
140+ ? `:is( ${ relative . replaceAll ( "&" , "*" ) } ) .self`
141+ : relative . includes ( "&" )
142+ ? relative . replaceAll ( "&" , owner )
102143 : `${ owner } :is(${ relative } )` ;
103144 const unscoped = hasCompounds ( selector ) . some ( ( compound ) => {
104145 const anchor = withoutNegations ( compound ) ;
0 commit comments