Skip to content

Commit 457d2b1

Browse files
Don’t crash when important and parent selectors are equal in @apply (#12112)
* Don’t crash when important and parent selectors are equal in `@apply` * Update changelog
1 parent d322286 commit 457d2b1

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2121
- Batch reading content files to prevent `too many open files` error ([#12079](https://github.com/tailwindlabs/tailwindcss/pull/12079))
2222
- Skip over classes inside `:not(…)` when nested in an at-rule ([#12105](https://github.com/tailwindlabs/tailwindcss/pull/12105))
2323
- Update types to work with `Node16` module resolution ([#12097](https://github.com/tailwindlabs/tailwindcss/pull/12097))
24+
- Don’t crash when important and parent selectors are equal in `@apply` ([#12112](https://github.com/tailwindlabs/tailwindcss/pull/12112))
2425

2526
### Added
2627

src/lib/expandApplyAtRules.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,13 @@ function processApply(root, context, localCache) {
553553
? parent.selector.slice(importantSelector.length)
554554
: parent.selector
555555

556+
// If the selector becomes empty after replacing the important selector
557+
// This means that it's the same as the parent selector and we don't want to replace it
558+
// Otherwise we'll crash
559+
if (parentSelector === '') {
560+
parentSelector = parent.selector
561+
}
562+
556563
rule.selector = replaceSelector(parentSelector, rule.selector, applyCandidate)
557564

558565
// And then re-add it if it was removed

tests/apply.test.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2081,3 +2081,55 @@ test('::ng-deep, ::deep, ::v-deep pseudo elements are left alone', () => {
20812081
`)
20822082
})
20832083
})
2084+
2085+
test('should not break replacing important selector when the same as the parent selector (pseudo)', async () => {
2086+
let config = {
2087+
important: ':root',
2088+
content: [],
2089+
}
2090+
2091+
let input = css`
2092+
@tailwind components;
2093+
@layer components {
2094+
:root {
2095+
@apply flex;
2096+
}
2097+
}
2098+
`
2099+
2100+
let result = await run(input, config)
2101+
2102+
expect(result.css).toMatchFormattedCss(css`
2103+
:root {
2104+
display: flex;
2105+
}
2106+
`)
2107+
})
2108+
2109+
test('should not break replacing important selector when the same as the parent selector (class)', async () => {
2110+
let config = {
2111+
important: '.foo',
2112+
content: [
2113+
{
2114+
raw: html` <div class="foo"></div> `,
2115+
},
2116+
],
2117+
}
2118+
2119+
let input = css`
2120+
@tailwind components;
2121+
@layer components {
2122+
.foo {
2123+
@apply flex;
2124+
}
2125+
}
2126+
`
2127+
2128+
let result = await run(input, config)
2129+
2130+
expect(result.css).toMatchFormattedCss(css`
2131+
.foo {
2132+
display: flex;
2133+
}
2134+
`)
2135+
})

0 commit comments

Comments
 (0)