Skip to content

Commit d32728b

Browse files
authored
Ensure \ is a valid arbitrary variant token (#8576)
* `\` are valid arbitrary variant tokens We use `\` for escaping `.` or `_` so they should be part of the arbitrary variant regex. * update changelog
1 parent 3f2e570 commit d32728b

File tree

3 files changed

+92
-2
lines changed

3 files changed

+92
-2
lines changed

CHANGELOG.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10-
- Nothing yet!
10+
### Fixed
11+
12+
- Ensure `\` is a valid arbitrary variant token ([#8576](https://github.com/tailwindlabs/tailwindcss/pull/8576))
1113

1214
## [3.1.1] - 2022-06-09
1315

src/lib/defaultExtractor.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ function* buildRegExps(context) {
6969
'((?=((',
7070
regex.any(
7171
[
72-
regex.pattern([/([^\s"'`\[\\]+-)?\[[^\s"'`\\]+\]/, separator]),
72+
regex.pattern([/([^\s"'`\[\\]+-)?\[[^\s"'`]+\]/, separator]),
7373
regex.pattern([/[^\s"'`\[\\]+/, separator]),
7474
],
7575
true

tests/arbitrary-variants.test.js

+88
Original file line numberDiff line numberDiff line change
@@ -405,3 +405,91 @@ test('with @apply', () => {
405405
`)
406406
})
407407
})
408+
409+
test('keeps escaped underscores', () => {
410+
let config = {
411+
content: [
412+
{
413+
raw: '<div class="[&_.foo\\_\\_bar]:underline"></div>',
414+
},
415+
],
416+
corePlugins: { preflight: false },
417+
}
418+
419+
let input = `
420+
@tailwind base;
421+
@tailwind components;
422+
@tailwind utilities;
423+
`
424+
425+
return run(input, config).then((result) => {
426+
expect(result.css).toMatchFormattedCss(css`
427+
${defaults}
428+
429+
.\[\&_\.foo\\_\\_bar\]\:underline .foo__bar {
430+
text-decoration-line: underline;
431+
}
432+
`)
433+
})
434+
})
435+
436+
test('keeps escaped underscores with multiple arbitrary variants', () => {
437+
let config = {
438+
content: [
439+
{
440+
raw: '<div class="[&_.foo\\_\\_bar]:[&_.bar\\_\\_baz]:underline"></div>',
441+
},
442+
],
443+
corePlugins: { preflight: false },
444+
}
445+
446+
let input = `
447+
@tailwind base;
448+
@tailwind components;
449+
@tailwind utilities;
450+
`
451+
452+
return run(input, config).then((result) => {
453+
expect(result.css).toMatchFormattedCss(css`
454+
${defaults}
455+
456+
.\[\&_\.foo\\_\\_bar\]\:\[\&_\.bar\\_\\_baz\]\:underline .bar__baz .foo__bar {
457+
text-decoration-line: underline;
458+
}
459+
`)
460+
})
461+
})
462+
463+
test('keeps escaped underscores in arbitrary variants mixed with normal variants', () => {
464+
let config = {
465+
content: [
466+
{
467+
raw: `
468+
<div class="[&_.foo\\_\\_bar]:hover:underline"></div>
469+
<div class="hover:[&_.foo\\_\\_bar]:underline"></div>
470+
`,
471+
},
472+
],
473+
corePlugins: { preflight: false },
474+
}
475+
476+
let input = `
477+
@tailwind base;
478+
@tailwind components;
479+
@tailwind utilities;
480+
`
481+
482+
return run(input, config).then((result) => {
483+
expect(result.css).toMatchFormattedCss(css`
484+
${defaults}
485+
486+
.\[\&_\.foo\\_\\_bar\]\:hover\:underline:hover .foo__bar {
487+
text-decoration-line: underline;
488+
}
489+
490+
.hover\:\[\&_\.foo\\_\\_bar\]\:underline .foo__bar:hover {
491+
text-decoration-line: underline;
492+
}
493+
`)
494+
})
495+
})

0 commit comments

Comments
 (0)