Skip to content
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
3 changes: 3 additions & 0 deletions .changelog/apply-mixin-arguments-with-spaces.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Fixed

- 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
52 changes: 37 additions & 15 deletions packages/tailwindcss/src/apply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ export function substituteAtApply(ast: AstNode[], designSystem: DesignSystem) {
walk(node.nodes, (child) => {
if (child.kind !== 'at-rule' || child.name !== '@apply') return

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

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

let offset = 0
for (let [idx, part] of parts.entries()) {
if (idx % 2 === 0) {
if (part[0] === '-' && part[1] === '-') {
dashedIdents.push(part)
} else {
normalIdents.push(part)
}

candidateOffsets[part] = offset
for (let [part, offset] of splitApplyParams(child.params)) {
if (isDashedIdent(part)) {
dashedIdents.push(part)
} else {
normalIdents.push(part)
}

offset += part.length
candidateOffsets[part] = offset
}

if (dashedIdents.length) {
Expand Down Expand Up @@ -342,7 +335,7 @@ function* resolveApplyDependencies(
node: Extract<AstNode, { kind: 'at-rule' }>,
designSystem: DesignSystem,
) {
for (let candidate of node.params.split(/\s+/g)) {
for (let [candidate] of splitApplyParams(node.params)) {
for (let node of designSystem.parseCandidate(candidate)) {
switch (node.kind) {
case 'arbitrary':
Expand All @@ -361,3 +354,32 @@ function* resolveApplyDependencies(
}
}
}

// Split the params of an `@apply` into its parts, each with its offset in the
// params.
//
// The parts are split at each whitespace, like the classes in a template. Only
// a CSS mixin keeps the whitespace in its arguments, like `--card(red, 2rem)`:
// https://drafts.csswg.org/css-mixins-1/#apply-rule
function* splitApplyParams(params: string): Generator<[part: string, offset: number]> {
let offset = 0

// Every whitespace character becomes one space, so the offsets stay the same.
for (let part of segment(params.replace(/\s/g, ' '), ' ')) {
if (isDashedIdent(part)) {
yield [part, offset]
} else {
let wordOffset = offset
for (let word of part.split(' ')) {
if (word !== '') yield [word, wordOffset]
wordOffset += word.length + 1
}
}

offset += part.length + 1
}
}

function isDashedIdent(part: string) {
return part[0] === '-' && part[1] === '-'
}
58 changes: 58 additions & 0 deletions packages/tailwindcss/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,8 @@ describe('@apply', () => {
@apply --my-mixin-1();
@apply --my-mixin-1 --my-mixin-2;
@apply --my-mixin-1() --my-mixin-2();
@apply --my-mixin-1(red, 2rem);
@apply --my-mixin-1({ 1px, 2px }, 3px);
@apply --my-mixin-3 {
color: red;
}
Expand All @@ -912,6 +914,8 @@ describe('@apply', () => {
@apply --my-mixin-1();
@apply --my-mixin-1 --my-mixin-2;
@apply --my-mixin-1() --my-mixin-2();
@apply --my-mixin-1(red, 2rem);
@apply --my-mixin-1({ 1px, 2px }, 3px);
@apply --my-mixin-3 {
color: red;
}
Expand All @@ -920,6 +924,27 @@ describe('@apply', () => {
`)
})

it('should not treat CSS mixin arguments as utilities in `@utility`', async () => {
expect(
await run(
['card'],
css`
@utility card {
@apply --card({ red, card }, 2rem);
}

@tailwind utilities;
`,
),
).toMatchInlineSnapshot(`
"
.card {
@apply --card({ red, card }, 2rem);
}
"
`)
})

it('should error when trying to use mixins and utilities together', async () => {
await expect(
compile(css`
Expand All @@ -940,6 +965,39 @@ describe('@apply', () => {
).rejects.toThrowErrorMatchingInlineSnapshot(
`[Error: You cannot use \`@apply\` with both mixins and utilities. Please move \`@apply --my-mixin-1\` into a separate rule.]`,
)

await expect(
compile(css`
.foo {
@apply underline --my-mixin-1(red, 2rem);
}
`),
).rejects.toThrowErrorMatchingInlineSnapshot(
`[Error: You cannot use \`@apply\` with both mixins and utilities. Please move \`@apply --my-mixin-1(red, 2rem)\` into a separate rule.]`,
)
})

it('should split utilities at each space, also inside brackets, parens and strings', async () => {
// Only the arguments of a CSS mixin can contain spaces. A class in a
// template can never contain a space, so `@apply` does not allow it either.
for (let [candidate, unknown] of [
['bg-[rgb(1, 2, 3)]', 'bg-[rgb(1,'],
['[margin:1px 2px]', '[margin:1px'],
["content-['a b']", "content-['a"],
]) {
await expect(
compile(css`
@theme {
--color-red: red;
}
@tailwind utilities;

.foo {
@apply ${candidate};
}
`),
).rejects.toThrow(`Cannot apply unknown utility class \`${unknown}\``)
}
})

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