Skip to content

Commit

Permalink
Ensure \ is a valid arbitrary variant token (#8576)
Browse files Browse the repository at this point in the history
* `\` are valid arbitrary variant tokens

We use `\` for escaping `.` or `_` so they should be part of the
arbitrary variant regex.

* update changelog
  • Loading branch information
RobinMalfait authored Jun 10, 2022
1 parent 3f2e570 commit d32728b
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 2 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- Nothing yet!
### Fixed

- Ensure `\` is a valid arbitrary variant token ([#8576](https://github.com/tailwindlabs/tailwindcss/pull/8576))

## [3.1.1] - 2022-06-09

Expand Down
2 changes: 1 addition & 1 deletion src/lib/defaultExtractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function* buildRegExps(context) {
'((?=((',
regex.any(
[
regex.pattern([/([^\s"'`\[\\]+-)?\[[^\s"'`\\]+\]/, separator]),
regex.pattern([/([^\s"'`\[\\]+-)?\[[^\s"'`]+\]/, separator]),
regex.pattern([/[^\s"'`\[\\]+/, separator]),
],
true
Expand Down
88 changes: 88 additions & 0 deletions tests/arbitrary-variants.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,3 +405,91 @@ test('with @apply', () => {
`)
})
})

test('keeps escaped underscores', () => {
let config = {
content: [
{
raw: '<div class="[&_.foo\\_\\_bar]:underline"></div>',
},
],
corePlugins: { preflight: false },
}

let input = `
@tailwind base;
@tailwind components;
@tailwind utilities;
`

return run(input, config).then((result) => {
expect(result.css).toMatchFormattedCss(css`
${defaults}
.\[\&_\.foo\\_\\_bar\]\:underline .foo__bar {
text-decoration-line: underline;
}
`)
})
})

test('keeps escaped underscores with multiple arbitrary variants', () => {
let config = {
content: [
{
raw: '<div class="[&_.foo\\_\\_bar]:[&_.bar\\_\\_baz]:underline"></div>',
},
],
corePlugins: { preflight: false },
}

let input = `
@tailwind base;
@tailwind components;
@tailwind utilities;
`

return run(input, config).then((result) => {
expect(result.css).toMatchFormattedCss(css`
${defaults}
.\[\&_\.foo\\_\\_bar\]\:\[\&_\.bar\\_\\_baz\]\:underline .bar__baz .foo__bar {
text-decoration-line: underline;
}
`)
})
})

test('keeps escaped underscores in arbitrary variants mixed with normal variants', () => {
let config = {
content: [
{
raw: `
<div class="[&_.foo\\_\\_bar]:hover:underline"></div>
<div class="hover:[&_.foo\\_\\_bar]:underline"></div>
`,
},
],
corePlugins: { preflight: false },
}

let input = `
@tailwind base;
@tailwind components;
@tailwind utilities;
`

return run(input, config).then((result) => {
expect(result.css).toMatchFormattedCss(css`
${defaults}
.\[\&_\.foo\\_\\_bar\]\:hover\:underline:hover .foo__bar {
text-decoration-line: underline;
}
.hover\:\[\&_\.foo\\_\\_bar\]\:underline .foo__bar:hover {
text-decoration-line: underline;
}
`)
})
})

0 comments on commit d32728b

Please sign in to comment.