Skip to content

Commit 20456ef

Browse files
Fix @apply of user utilities when negative and non-negative versions both exist (#9027)
* Fix application of rules with multiple matches of differing selectors `-foo-1` and `foo-1` are both matches for the class `-foo-1` but `@apply` only wants the first one. It would remove the second one and cause an error because it’s an entirely separate match that had it’s only rule removed. * Update changelog
1 parent 2bfd3e7 commit 20456ef

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
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
- Don’t prefix classes within reused arbitrary variants ([#8992](https://github.com/tailwindlabs/tailwindcss/pull/8992))
1313
- Fix usage of alpha values inside single-named colors that are functions ([#9008](https://github.com/tailwindlabs/tailwindcss/pull/9008))
14+
- Fix `@apply` of user utilities when negative and non-negative versions both exist ([#9027](https://github.com/tailwindlabs/tailwindcss/pull/9027))
1415

1516
### Changed
1617

src/lib/expandApplyAtRules.js

+6
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,12 @@ function processApply(root, context, localCache) {
499499
})
500500
}
501501

502+
// It could be that the node we were inserted was removed because the class didn't match
503+
// If that was the *only* rule in the parent, then we have nothing add so we skip it
504+
if (!root.nodes[0]) {
505+
continue
506+
}
507+
502508
// Insert it
503509
siblings.push([
504510
// Ensure that when we are sorting, that we take the layer order into account

tests/apply.test.js

+36
Original file line numberDiff line numberDiff line change
@@ -1548,3 +1548,39 @@ it('apply + user CSS + selector variants (like group) + important selector (2)',
15481548
}
15491549
`)
15501550
})
1551+
1552+
it('can apply user utilities that start with a dash', async () => {
1553+
let config = {
1554+
content: [{ raw: html`<div class="foo-1 -foo-1 new-class"></div>` }],
1555+
plugins: [],
1556+
}
1557+
1558+
let input = css`
1559+
@tailwind utilities;
1560+
@layer utilities {
1561+
.foo-1 {
1562+
margin: 10px;
1563+
}
1564+
.-foo-1 {
1565+
margin: -15px;
1566+
}
1567+
.new-class {
1568+
@apply -foo-1;
1569+
}
1570+
}
1571+
`
1572+
1573+
let result = await run(input, config)
1574+
1575+
expect(result.css).toMatchFormattedCss(css`
1576+
.foo-1 {
1577+
margin: 10px;
1578+
}
1579+
.-foo-1 {
1580+
margin: -15px;
1581+
}
1582+
.new-class {
1583+
margin: -15px;
1584+
}
1585+
`)
1586+
})

0 commit comments

Comments
 (0)