Skip to content

Commit 8494f75

Browse files
Don’t prefix selectors in arbitrary variants (#8773)
* Don’t prefix selectors in arbitrary variants * Update changelog
1 parent 5191ec1 commit 8494f75

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
- Allows fallback values in plugin API helpers ([#8762](https://github.com/tailwindlabs/tailwindcss/pull/8762))
1313
- Fix usage of postcss.config.js in standalone CLI ([#8769](https://github.com/tailwindlabs/tailwindcss/pull/8769))
1414
- Fix usage of special-character prefixes ([#8772](https://github.com/tailwindlabs/tailwindcss/pull/8772))
15+
- Don’t prefix selectors in arbitrary variants ([#8773](https://github.com/tailwindlabs/tailwindcss/pull/8773))
1516

1617
## [3.1.4] - 2022-06-21
1718

src/lib/generateRules.js

+5
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ function applyVariant(variant, matches, context) {
129129
}
130130

131131
let args
132+
let isArbitraryVariant = false
132133

133134
// Find partial arbitrary variants
134135
if (variant.endsWith(']') && !variant.startsWith('[')) {
@@ -144,6 +145,8 @@ function applyVariant(variant, matches, context) {
144145
return []
145146
}
146147

148+
isArbitraryVariant = true
149+
147150
let fn = parseVariant(selector)
148151

149152
let sort = Array.from(context.variantOrder.values()).pop() << 1n
@@ -300,6 +303,7 @@ function applyVariant(variant, matches, context) {
300303
...meta,
301304
sort: variantSort | meta.sort,
302305
collectedFormats: (meta.collectedFormats ?? []).concat(collectedFormats),
306+
isArbitraryVariant,
303307
},
304308
clone.nodes[0],
305309
]
@@ -627,6 +631,7 @@ function* resolveMatches(candidate, context, original = candidate) {
627631
base: candidate
628632
.split(new RegExp(`\\${context?.tailwindConfig?.separator ?? ':'}(?![^[]*\\])`))
629633
.pop(),
634+
isArbitraryVariant: match[0].isArbitraryVariant,
630635

631636
context,
632637
})

src/util/formatVariantSelector.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export function finalizeSelector(
3535
selector,
3636
candidate,
3737
context,
38+
isArbitraryVariant,
3839

3940
// Split by the separator, but ignore the separator inside square brackets:
4041
//
@@ -50,7 +51,8 @@ export function finalizeSelector(
5051
) {
5152
let ast = selectorParser().astSync(selector)
5253

53-
if (context?.tailwindConfig?.prefix) {
54+
// We explicitly DO NOT prefix classes in arbitrary variants
55+
if (context?.tailwindConfig?.prefix && !isArbitraryVariant) {
5456
format = prefixSelector(context.tailwindConfig.prefix, format)
5557
}
5658

tests/arbitrary-variants.test.js

+39
Original file line numberDiff line numberDiff line change
@@ -527,3 +527,42 @@ test('allows attribute variants with quotes', () => {
527527
`)
528528
})
529529
})
530+
531+
test('classes in arbitrary variants should not be prefixed', () => {
532+
let config = {
533+
prefix: 'tw-',
534+
content: [
535+
{
536+
raw: `
537+
<div class="[.foo_&]:tw-text-red-400">should not be red</div>
538+
<div class="foo">
539+
<div class="[.foo_&]:tw-text-red-400">should be red</div>
540+
</div>
541+
<div class="[&_.foo]:tw-text-red-400">
542+
<div>should not be red</div>
543+
<div class="foo">should be red</div>
544+
</div>
545+
`,
546+
},
547+
],
548+
corePlugins: { preflight: false },
549+
}
550+
551+
let input = `
552+
@tailwind utilities;
553+
`
554+
555+
return run(input, config).then((result) => {
556+
expect(result.css).toMatchFormattedCss(css`
557+
.foo .\[\.foo_\&\]\:tw-text-red-400 {
558+
--tw-text-opacity: 1;
559+
color: rgb(248 113 113 / var(--tw-text-opacity));
560+
}
561+
562+
.\[\&_\.foo\]\:tw-text-red-400 .foo {
563+
--tw-text-opacity: 1;
564+
color: rgb(248 113 113 / var(--tw-text-opacity));
565+
}
566+
`)
567+
})
568+
})

0 commit comments

Comments
 (0)