Skip to content

Commit c024006

Browse files
committed
Exclude non-relevant selectors when generating rules with the important modifier. Fixes tailwindlabs#9677.
1 parent c936989 commit c024006

File tree

4 files changed

+43
-7
lines changed

4 files changed

+43
-7
lines changed

CHANGELOG.md

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

1212
- Escape special characters in resolved content base paths ([#9650](https://github.com/tailwindlabs/tailwindcss/pull/9650))
1313
- Don't reuse container for array returning variant functions ([#9644](https://github.com/tailwindlabs/tailwindcss/pull/9644))
14+
- Exclude non-relevant selectors when generating rules with the important modifier ([#9677](https://github.com/tailwindlabs/tailwindcss/issues/9677))
1415

1516
## [3.2.1] - 2022-10-21
1617

src/lib/generateRules.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import selectorParser from 'postcss-selector-parser'
33
import parseObjectStyles from '../util/parseObjectStyles'
44
import isPlainObject from '../util/isPlainObject'
55
import prefixSelector from '../util/prefixSelector'
6-
import { updateAllClasses, getMatchingTypes } from '../util/pluginUtils'
6+
import { updateAllClasses, filterSelectorsForClass, getMatchingTypes } from '../util/pluginUtils'
77
import log from '../util/log'
88
import * as sharedState from './sharedState'
99
import { formatVariantSelector, finalizeSelector } from '../util/formatVariantSelector'
@@ -116,12 +116,15 @@ function applyImportant(matches, classCandidate) {
116116
for (let [meta, rule] of matches) {
117117
let container = postcss.root({ nodes: [rule.clone()] })
118118
container.walkRules((r) => {
119-
r.selector = updateAllClasses(r.selector, (className) => {
120-
if (className === classCandidate) {
121-
return `!${className}`
119+
r.selector = updateAllClasses(
120+
filterSelectorsForClass(r.selector, classCandidate),
121+
(className) => {
122+
if (className === classCandidate) {
123+
return `!${className}`
124+
}
125+
return className
122126
}
123-
return className
124-
})
127+
)
125128
r.walkDecls((d) => (d.important = true))
126129
})
127130
result.push([{ ...meta, important: true }, container.nodes[0]])

src/util/pluginUtils.js

+17
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,23 @@ export function updateAllClasses(selectors, updateClass) {
3737
return result
3838
}
3939

40+
export function filterSelectorsForClass(selectors, classCandidate) {
41+
let parser = selectorParser((selectors) => {
42+
selectors.each((sel) => {
43+
const containsClass = sel.nodes.some(
44+
(node) => node.type === 'class' && node.value === classCandidate
45+
)
46+
if (!containsClass) {
47+
sel.remove()
48+
}
49+
})
50+
})
51+
52+
let result = parser.processSync(selectors)
53+
54+
return result
55+
}
56+
4057
function resolveArbitraryValue(modifier, validate) {
4158
if (!isArbitraryValue(modifier)) {
4259
return undefined

tests/important-modifier.test.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ test('important modifier', () => {
1313
<div class="lg:!opacity-50"></div>
1414
<div class="xl:focus:disabled:!float-right"></div>
1515
<div class="!custom-parent-5"></div>
16+
<div class="btn !disabled"></div>
1617
`,
1718
},
1819
],
1920
corePlugins: { preflight: false },
2021
plugins: [
21-
function ({ theme, matchUtilities }) {
22+
function ({ theme, matchUtilities, addComponents }) {
2223
matchUtilities(
2324
{
2425
'custom-parent': (value) => {
@@ -31,6 +32,13 @@ test('important modifier', () => {
3132
},
3233
{ values: theme('spacing') }
3334
)
35+
addComponents({
36+
'.btn': {
37+
'&.disabled, &:disabled': {
38+
color: 'gray',
39+
},
40+
},
41+
})
3442
},
3543
],
3644
}
@@ -70,6 +78,13 @@ test('important modifier', () => {
7078
max-width: 1536px !important;
7179
}
7280
}
81+
.btn.disabled,
82+
.btn:disabled {
83+
color: gray;
84+
}
85+
.btn.\!disabled {
86+
color: gray !important;
87+
}
7388
.\!font-bold {
7489
font-weight: 700 !important;
7590
}

0 commit comments

Comments
 (0)