Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure comments in @layer rules don't crash #5854

Merged
merged 2 commits into from
Oct 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix before/after utilities overriding custom content values at larger breakpoints ([#5820](https://github.com/tailwindlabs/tailwindcss/pull/5820))
- Cleanup duplicate properties ([#5830](https://github.com/tailwindlabs/tailwindcss/pull/5830))
- Allow `_` inside `url()` when using arbitrary values ([#5853](https://github.com/tailwindlabs/tailwindcss/pull/5853))
- Prevent crashes when using comments in `@layer` AtRules ([#5854](https://github.com/tailwindlabs/tailwindcss/pull/5854))

## [3.0.0-alpha.1] - 2021-10-01

Expand Down
28 changes: 21 additions & 7 deletions src/lib/generateRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -448,28 +448,42 @@ function generateRules(candidates, context) {
allRules.push(matches)
}

return allRules.flat(1).map(([{ sort, layer, options }, rule]) => {
if (options.respectImportant) {
if (context.tailwindConfig.important === true) {
// Strategy based on `tailwindConfig.important`
let strategy = ((important) => {
if (important === true) {
return (rule) => {
rule.walkDecls((d) => {
if (d.parent.type === 'rule' && !inKeyframes(d.parent)) {
d.important = true
}
})
} else if (typeof context.tailwindConfig.important === 'string') {
}
}

if (typeof important === 'string') {
return (rule) => {
rule.selectors = rule.selectors.map((selector) => {
return `${important} ${selector}`
})
}
}
})(context.tailwindConfig.important)

return allRules.flat(1).map(([{ sort, layer, options }, rule]) => {
if (options.respectImportant) {
if (strategy) {
let container = postcss.root({ nodes: [rule.clone()] })
container.walkRules((r) => {
if (inKeyframes(r)) {
return
}

r.selectors = r.selectors.map((selector) => {
return `${context.tailwindConfig.important} ${selector}`
})
strategy(r)
})
rule = container.nodes[0]
}
}

return [sort | context.layerOrder[layer], rule]
})
}
Expand Down
119 changes: 119 additions & 0 deletions tests/layer-at-rules.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,125 @@ test('custom user-land utilities', () => {
})
})

test('comments can be used inside layers without crashing', () => {
let config = {
content: [
{
raw: html`<div class="important-utility important-component"></div>`,
},
],
corePlugins: { preflight: false },
theme: {},
plugins: [],
}

let input = css`
@tailwind base;
@tailwind components;
@tailwind utilities;

@layer base {
/* Important base */
div {
background-color: #bada55;
}
}

@layer utilities {
/* Important utility */
.important-utility {
text-align: banana;
}
}

@layer components {
/* Important component */
.important-component {
text-align: banana;
}
}
`

return run(input, config).then((result) => {
expect(result.css).toMatchFormattedCss(css`
/* Important base */
div {
background-color: #bada55;
}

/* Important component */
.important-component {
text-align: banana;
}

/* Important utility */
.important-utility {
text-align: banana;
}
`)
})
})

test('comments can be used inside layers (with important) without crashing', () => {
let config = {
important: true,
content: [
{
raw: html`<div class="important-utility important-component"></div>`,
},
],
corePlugins: { preflight: false },
theme: {},
plugins: [],
}

let input = css`
@tailwind base;
@tailwind components;
@tailwind utilities;

@layer base {
/* Important base */
div {
background-color: #bada55;
}
}

@layer utilities {
/* Important utility */
.important-utility {
text-align: banana;
}
}

@layer components {
/* Important component */
.important-component {
text-align: banana;
}
}
`

return run(input, config).then((result) => {
expect(result.css).toMatchFormattedCss(css`
/* Important base */
div {
background-color: #bada55;
}

/* Important component */
.important-component {
text-align: banana;
}

/* Important utility */
.important-utility {
text-align: banana !important;
}
`)
})
})

test('layers are grouped and inserted at the matching @tailwind rule', () => {
let config = {
content: [
Expand Down