Skip to content

Commit 1bea2b0

Browse files
lazergRobinMalfait
andauthored
Allow spaces in the arguments of a CSS mixin in @apply (#20725)
## Summary `@apply` with a CSS mixin threw "You cannot use `@apply` with both mixins and utilities" as soon as the mixin arguments had a space, e.g. `@apply --card(red, 2rem)`. The params were split on every whitespace, so `--card(red,` was seen as a mixin and `2rem)` as a utility. The params are now split with `segment`, so whitespace inside parens, brackets, braces and strings is kept in the same part. The dependency scan and the circular dependency check use the same split, so a mixin argument like `card` in `@utility card { @apply --card({ red, card }, 2rem); }` is not read as a utility. A list of valid utilities splits the same way as before. Fixes #20724 ## Test plan - Added `@apply --my-mixin-1(red, 2rem)` and `@apply --my-mixin-1({ 1px, 2px }, 3px)` to the existing "should be usable with CSS mixins" test in `index.test.ts`. It fails without this change and passes with it. - Added a test with `@apply --card({ red, card }, 2rem)` inside `@utility card`. It fails with a circular dependency error without this change. - `pnpm vitest run` in `packages/tailwindcss`: all tests pass. - `pnpm changelog check` passes. --------- Co-authored-by: Robin Malfait <robin.malfait@shopify.com>
1 parent 603cc8d commit 1bea2b0

3 files changed

Lines changed: 98 additions & 15 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
### Fixed
2+
3+
- Allow `@apply` with a CSS mixin that has spaces in its arguments (e.g. `@apply --card(red, 2rem)`) instead of throwing an error about mixins and utilities

‎packages/tailwindcss/src/apply.ts‎

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,7 @@ export function substituteAtApply(ast: AstNode[], designSystem: DesignSystem) {
102102
walk(node.nodes, (child) => {
103103
if (child.kind !== 'at-rule' || child.name !== '@apply') return
104104

105-
let candidates = child.params.split(/\s+/g)
106-
for (let candidate of candidates) {
105+
for (let [candidate] of splitApplyParams(child.params)) {
107106
for (let candidateAstNode of designSystem.parseCandidate(candidate)) {
108107
switch (candidateAstNode.kind) {
109108
case 'arbitrary':
@@ -161,24 +160,18 @@ export function substituteAtApply(ast: AstNode[], designSystem: DesignSystem) {
161160
walk(parent.nodes, (child) => {
162161
if (child.kind !== 'at-rule' || child.name !== '@apply') return
163162

164-
let parts = child.params.split(/(\s+)/g)
165163
let candidateOffsets: Record<string, number> = {}
166164
let normalIdents: string[] = []
167165
let dashedIdents: string[] = []
168166

169-
let offset = 0
170-
for (let [idx, part] of parts.entries()) {
171-
if (idx % 2 === 0) {
172-
if (part[0] === '-' && part[1] === '-') {
173-
dashedIdents.push(part)
174-
} else {
175-
normalIdents.push(part)
176-
}
177-
178-
candidateOffsets[part] = offset
167+
for (let [part, offset] of splitApplyParams(child.params)) {
168+
if (isDashedIdent(part)) {
169+
dashedIdents.push(part)
170+
} else {
171+
normalIdents.push(part)
179172
}
180173

181-
offset += part.length
174+
candidateOffsets[part] = offset
182175
}
183176

184177
if (dashedIdents.length) {
@@ -342,7 +335,7 @@ function* resolveApplyDependencies(
342335
node: Extract<AstNode, { kind: 'at-rule' }>,
343336
designSystem: DesignSystem,
344337
) {
345-
for (let candidate of node.params.split(/\s+/g)) {
338+
for (let [candidate] of splitApplyParams(node.params)) {
346339
for (let node of designSystem.parseCandidate(candidate)) {
347340
switch (node.kind) {
348341
case 'arbitrary':
@@ -361,3 +354,32 @@ function* resolveApplyDependencies(
361354
}
362355
}
363356
}
357+
358+
// Split the params of an `@apply` into its parts, each with its offset in the
359+
// params.
360+
//
361+
// The parts are split at each whitespace, like the classes in a template. Only
362+
// a CSS mixin keeps the whitespace in its arguments, like `--card(red, 2rem)`:
363+
// https://drafts.csswg.org/css-mixins-1/#apply-rule
364+
function* splitApplyParams(params: string): Generator<[part: string, offset: number]> {
365+
let offset = 0
366+
367+
// Every whitespace character becomes one space, so the offsets stay the same.
368+
for (let part of segment(params.replace(/\s/g, ' '), ' ')) {
369+
if (isDashedIdent(part)) {
370+
yield [part, offset]
371+
} else {
372+
let wordOffset = offset
373+
for (let word of part.split(' ')) {
374+
if (word !== '') yield [word, wordOffset]
375+
wordOffset += word.length + 1
376+
}
377+
}
378+
379+
offset += part.length + 1
380+
}
381+
}
382+
383+
function isDashedIdent(part: string) {
384+
return part[0] === '-' && part[1] === '-'
385+
}

‎packages/tailwindcss/src/index.test.ts‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,8 @@ describe('@apply', () => {
898898
@apply --my-mixin-1();
899899
@apply --my-mixin-1 --my-mixin-2;
900900
@apply --my-mixin-1() --my-mixin-2();
901+
@apply --my-mixin-1(red, 2rem);
902+
@apply --my-mixin-1({ 1px, 2px }, 3px);
901903
@apply --my-mixin-3 {
902904
color: red;
903905
}
@@ -912,6 +914,8 @@ describe('@apply', () => {
912914
@apply --my-mixin-1();
913915
@apply --my-mixin-1 --my-mixin-2;
914916
@apply --my-mixin-1() --my-mixin-2();
917+
@apply --my-mixin-1(red, 2rem);
918+
@apply --my-mixin-1({ 1px, 2px }, 3px);
915919
@apply --my-mixin-3 {
916920
color: red;
917921
}
@@ -920,6 +924,27 @@ describe('@apply', () => {
920924
`)
921925
})
922926

927+
it('should not treat CSS mixin arguments as utilities in `@utility`', async () => {
928+
expect(
929+
await run(
930+
['card'],
931+
css`
932+
@utility card {
933+
@apply --card({ red, card }, 2rem);
934+
}
935+
936+
@tailwind utilities;
937+
`,
938+
),
939+
).toMatchInlineSnapshot(`
940+
"
941+
.card {
942+
@apply --card({ red, card }, 2rem);
943+
}
944+
"
945+
`)
946+
})
947+
923948
it('should error when trying to use mixins and utilities together', async () => {
924949
await expect(
925950
compile(css`
@@ -940,6 +965,39 @@ describe('@apply', () => {
940965
).rejects.toThrowErrorMatchingInlineSnapshot(
941966
`[Error: You cannot use \`@apply\` with both mixins and utilities. Please move \`@apply --my-mixin-1\` into a separate rule.]`,
942967
)
968+
969+
await expect(
970+
compile(css`
971+
.foo {
972+
@apply underline --my-mixin-1(red, 2rem);
973+
}
974+
`),
975+
).rejects.toThrowErrorMatchingInlineSnapshot(
976+
`[Error: You cannot use \`@apply\` with both mixins and utilities. Please move \`@apply --my-mixin-1(red, 2rem)\` into a separate rule.]`,
977+
)
978+
})
979+
980+
it('should split utilities at each space, also inside brackets, parens and strings', async () => {
981+
// Only the arguments of a CSS mixin can contain spaces. A class in a
982+
// template can never contain a space, so `@apply` does not allow it either.
983+
for (let [candidate, unknown] of [
984+
['bg-[rgb(1, 2, 3)]', 'bg-[rgb(1,'],
985+
['[margin:1px 2px]', '[margin:1px'],
986+
["content-['a b']", "content-['a"],
987+
]) {
988+
await expect(
989+
compile(css`
990+
@theme {
991+
--color-red: red;
992+
}
993+
@tailwind utilities;
994+
995+
.foo {
996+
@apply ${candidate};
997+
}
998+
`),
999+
).rejects.toThrow(`Cannot apply unknown utility class \`${unknown}\``)
1000+
}
9431001
})
9441002

9451003
it('should error when used with a body', async () => {

0 commit comments

Comments
 (0)