Skip to content

Commit 1e0fc09

Browse files
Fix !important on multiple selectors #2823 (#2824)
* Add failing test for #2823 * cleanup string literals * use prettier for toMatchCSS diffs * make sure that importants are applied correctly Co-authored-by: Robin Malfait <[email protected]>
1 parent 3ea5e18 commit 1e0fc09

File tree

3 files changed

+68
-21
lines changed

3 files changed

+68
-21
lines changed

__tests__/applyAtRule.test.js

+56-18
Original file line numberDiff line numberDiff line change
@@ -1002,14 +1002,10 @@ describe('using apply with the prefix option', () => {
10021002

10031003
test('a "Did You Mean" suggestion is omitted if a similar class cannot be identified.', () => {
10041004
const input = `
1005-
.foo { @apply anteater; }
1006-
`
1005+
.foo { @apply anteater; }
1006+
`
10071007

1008-
const config = resolveConfig([
1009-
{
1010-
...defaultConfig,
1011-
},
1012-
])
1008+
const config = resolveConfig([{ ...defaultConfig }])
10131009

10141010
expect.assertions(1)
10151011

@@ -1109,7 +1105,7 @@ test('you can apply classes to a rule with multiple selectors', () => {
11091105
@apply float-left opacity-50 hover:opacity-100 md:float-right;
11101106
}
11111107
}
1112-
`
1108+
`
11131109

11141110
const expected = `
11151111
@supports (display: grid) {
@@ -1134,6 +1130,48 @@ test('you can apply classes to a rule with multiple selectors', () => {
11341130
})
11351131
})
11361132

1133+
test('you can apply classes to a rule with multiple selectors with important and a prefix enabled', () => {
1134+
const input = `
1135+
@supports (display: grid) {
1136+
.foo, h1 > .bar * {
1137+
@apply tw-float-left tw-opacity-50 hover:tw-opacity-100 md:tw-float-right;
1138+
}
1139+
}
1140+
`
1141+
1142+
const expected = `
1143+
@supports (display: grid) {
1144+
.foo, h1 > .bar * {
1145+
float: left;
1146+
opacity: 0.5;
1147+
}
1148+
1149+
.foo:hover, h1 > .bar *:hover {
1150+
opacity: 1;
1151+
}
1152+
1153+
@media (min-width: 768px) {
1154+
.foo, h1 > .bar * {
1155+
float: right;
1156+
}
1157+
}
1158+
}
1159+
`
1160+
1161+
const config = resolveConfig([
1162+
{
1163+
...defaultConfig,
1164+
prefix: 'tw-',
1165+
important: true,
1166+
},
1167+
])
1168+
1169+
return run(input, config).then((result) => {
1170+
expect(result.css).toMatchCss(expected)
1171+
expect(result.warnings().length).toBe(0)
1172+
})
1173+
})
1174+
11371175
test('you can apply classes in a nested rule', () => {
11381176
const input = `
11391177
.selector {
@@ -1241,11 +1279,11 @@ test('declarations within a rule that uses @apply can be !important', () => {
12411279
`
12421280

12431281
const expected = `
1244-
.foo {
1245-
text-align: center;
1246-
float: left;
1247-
display: block !important;
1248-
}
1282+
.foo {
1283+
text-align: center;
1284+
float: left;
1285+
display: block !important;
1286+
}
12491287
`
12501288

12511289
expect.assertions(2)
@@ -1266,11 +1304,11 @@ test('declarations within a rule that uses @apply with !important remain not !im
12661304
`
12671305

12681306
const expected = `
1269-
.foo {
1270-
text-align: center !important;
1271-
float: left;
1272-
display: block !important;
1273-
}
1307+
.foo {
1308+
text-align: center !important;
1309+
float: left;
1310+
display: block !important;
1311+
}
12741312
`
12751313

12761314
expect.assertions(2)

jest/customMatchers.js

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import prettier from 'prettier'
22
import diff from 'jest-diff'
33

44
function format(input) {
5-
return input
65
return prettier.format(input, {
76
parser: 'css',
87
printWidth: 100,

src/lib/substituteClassApplyAtRules.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,21 @@ function processApplyAtRules(css, lookupTree, config) {
242242
: (util) => util.rule.nodes.forEach((n) => afterRule.append(n.clone()))
243243
)
244244

245-
const { nodes } = _.tap(postcss.root({ nodes: rulesToInsert }), (root) =>
245+
rulesToInsert.forEach((rule) => {
246+
if (rule.type === 'atrule') {
247+
rule.walkRules((rule) => {
248+
rule.__tailwind = { ...rule.__tailwind, important }
249+
})
250+
} else {
251+
rule.__tailwind = { ...rule.__tailwind, important }
252+
}
253+
})
254+
255+
const { nodes } = _.tap(postcss.root({ nodes: rulesToInsert }), (root) => {
246256
root.walkDecls((d) => {
247257
d.important = important
248258
})
249-
)
259+
})
250260

251261
const mergedRules = mergeAdjacentRules(nearestParentRule, [...nodes, afterRule])
252262

0 commit comments

Comments
 (0)