Skip to content

Commit 3061bb5

Browse files
committed
Keep spaces only in the arguments of a CSS mixin in @apply
The previous commits split all `@apply` params with `segment`. That also kept the spaces inside the brackets, parens and strings of utilities, so `@apply bg-[rgb(1, 2, 3)]`, `@apply [margin:1px 2px]` and `@apply content-['a b']` started to compile. No class in a template can contain a space, so `@apply` should not allow it either. `splitApplyParams` now keeps a part whole only when it is a dashed ident (a CSS mixin, like `--card(red, 2rem)`). Every other part is split at each whitespace, as before. All three readers of the params (the dependency scan, the circular dependency check and the substitution) use it, with the offsets of each part for the source maps.
1 parent e811a49 commit 3061bb5

2 files changed

Lines changed: 70 additions & 15 deletions

File tree

‎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 = segment(child.params.replace(/\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 = segment(child.params.replace(/\s/g, ' '), ' ')
165163
let candidateOffsets: Record<string, number> = {}
166164
let normalIdents: string[] = []
167165
let dashedIdents: string[] = []
168166

169-
let offset = 0
170-
for (let part of parts) {
171-
if (part !== '') {
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 + 1
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 segment(node.params.replace(/\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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -965,6 +965,39 @@ describe('@apply', () => {
965965
).rejects.toThrowErrorMatchingInlineSnapshot(
966966
`[Error: You cannot use \`@apply\` with both mixins and utilities. Please move \`@apply --my-mixin-1\` into a separate rule.]`,
967967
)
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+
}
9681001
})
9691002

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

0 commit comments

Comments
 (0)