Skip to content

Commit f7a9d37

Browse files
authored
Only add ! to selector class matching template candidate (tailwindlabs#7664)
* Only add `!` to selector class matching template candidate Fixes tailwindlabs#7226. Before this PR, if you had a class like: ```css .one .two { background: black } ``` ...and then used `!one` in your template, the generated CSS would be this: ```css .\!one .\!two { background: black !important } ``` This would cause the styles to not be applied unless you also added `!` to the beginning of other classes in the template that are part of this selector. This PR makes sure that other classes in the selector aren't mistakenly prefixed with `!`, so that you can add `!` to only one of the classes in your template and get the expected result. * Update CHANGELOG
1 parent bd16763 commit f7a9d37

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515
- Split box shadows on top-level commas only ([#7479](https://github.com/tailwindlabs/tailwindcss/pull/7479))
1616
- Use local user CSS cache for `@apply` ([#7524](https://github.com/tailwindlabs/tailwindcss/pull/7524))
1717
- Invalidate context when main CSS changes ([#7626](https://github.com/tailwindlabs/tailwindcss/pull/7626))
18+
- Only add `!` to selector class matching template candidate when using important modifier with mutli-class selectors ([#7664](https://github.com/tailwindlabs/tailwindcss/pull/7664))
1819

1920
### Changed
2021

src/lib/generateRules.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ function applyPrefix(matches, context) {
8888
return matches
8989
}
9090

91-
function applyImportant(matches) {
91+
function applyImportant(matches, classCandidate) {
9292
if (matches.length === 0) {
9393
return matches
9494
}
@@ -98,7 +98,10 @@ function applyImportant(matches) {
9898
let container = postcss.root({ nodes: [rule.clone()] })
9999
container.walkRules((r) => {
100100
r.selector = updateAllClasses(r.selector, (className) => {
101-
return `!${className}`
101+
if (className === classCandidate) {
102+
return `!${className}`
103+
}
104+
return className
102105
})
103106
r.walkDecls((d) => (d.important = true))
104107
})
@@ -514,7 +517,7 @@ function* resolveMatches(candidate, context) {
514517
matches = applyPrefix(matches, context)
515518

516519
if (important) {
517-
matches = applyImportant(matches, context)
520+
matches = applyImportant(matches, classCandidate)
518521
}
519522

520523
for (let variant of variants) {

tests/important-modifier.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,22 @@ test('important modifier', () => {
1717
},
1818
],
1919
corePlugins: { preflight: false },
20+
plugins: [
21+
function ({ theme, matchUtilities }) {
22+
matchUtilities(
23+
{
24+
'custom-parent': (value) => {
25+
return {
26+
'.custom-child': {
27+
margin: value,
28+
},
29+
}
30+
},
31+
},
32+
{ values: theme('spacing') }
33+
)
34+
},
35+
],
2036
}
2137

2238
let input = css`
@@ -57,6 +73,9 @@ test('important modifier', () => {
5773
.\!font-bold {
5874
font-weight: 700 !important;
5975
}
76+
.\!custom-parent-5 .custom-child {
77+
margin: 1.25rem !important;
78+
}
6079
.hover\:\!text-center:hover {
6180
text-align: center !important;
6281
}

0 commit comments

Comments
 (0)