Skip to content

Commit b67f52c

Browse files
authored
Increase strictness when using applyVariant on illegale candidates (#9599)
* ensure that cases like `@-[200px]` and `group[:hover]` aren't allowed * update changelog
1 parent 1ca51b6 commit b67f52c

File tree

3 files changed

+64
-3
lines changed

3 files changed

+64
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
5858
- Don't require `content` key in custom plugin configs ([#9502](https://github.com/tailwindlabs/tailwindcss/pull/9502), [#9545](https://github.com/tailwindlabs/tailwindcss/pull/9545))
5959
- Fix content path detection on Windows ([#9569](https://github.com/tailwindlabs/tailwindcss/pull/9569))
6060
- Ensure `--content` is used in the CLI when passed ([#9587](https://github.com/tailwindlabs/tailwindcss/pull/9587))
61+
- Increase strictness when using `applyVariant` on illegale candidates ([#9599](https://github.com/tailwindlabs/tailwindcss/pull/9599))
6162

6263
## [3.1.8] - 2022-08-05
6364

src/lib/generateRules.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,23 @@ function applyVariant(variant, matches, context) {
162162

163163
// Retrieve "arbitrary value"
164164
if (variant.endsWith(']') && !variant.startsWith('[')) {
165-
let match = /-?\[(.*)\]/g.exec(variant)
165+
// We either have:
166+
// @[200px]
167+
// group-[:hover]
168+
//
169+
// But we don't want:
170+
// @-[200px] (`-` is incorrect)
171+
// group[:hover] (`-` is missing)
172+
let match = /(.)(-?)\[(.*)\]/g.exec(variant)
166173
if (match) {
167-
variant = variant.replace(match[0], '')
168-
args.value = match[1]
174+
let [, char, seperator, value] = match
175+
// @-[200px] case
176+
if (char === '@' && seperator === '-') return []
177+
// group[:hover] case
178+
if (char !== '@' && seperator === '') return []
179+
180+
variant = variant.replace(`${seperator}[${value}]`, '')
181+
args.value = value
169182
}
170183
}
171184

tests/generate-rules.test.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { css } from './util/run'
2+
3+
import { generateRules } from '../src/lib/generateRules'
4+
import resolveConfig from '../src/public/resolve-config'
5+
import { createContext } from '../src/lib/setupContextUtils'
6+
7+
it('should not generate rules that are incorrect', () => {
8+
let config = {
9+
plugins: [
10+
({ matchVariant }) => {
11+
matchVariant('@', (value) => `@container (min-width: ${value})`)
12+
},
13+
],
14+
}
15+
let context = createContext(resolveConfig(config))
16+
let rules = generateRules(
17+
new Set([
18+
// Invalid, missing `-`
19+
'group[:hover]:underline',
20+
21+
// Invalid, `-` should not be there
22+
'@-[200px]:underline',
23+
24+
// Valid
25+
'group-[:hover]:underline',
26+
'@[200px]:underline',
27+
]),
28+
context
29+
)
30+
31+
// Ensure we only have 2 valid rules
32+
expect(rules).toHaveLength(2)
33+
34+
// Ensure we have the correct values
35+
expect(rules[0][1].toString()).toMatchFormattedCss(css`
36+
.group:hover .group-\[\:hover\]\:underline {
37+
text-decoration-line: underline;
38+
}
39+
`)
40+
expect(rules[1][1].toString()).toMatchFormattedCss(css`
41+
@container (min-width: 200px) {
42+
.\@\[200px\]\:underline {
43+
text-decoration-line: underline;
44+
}
45+
}
46+
`)
47+
})

0 commit comments

Comments
 (0)