Skip to content
Open
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/reject-bare-opacity-modifiers-above-100.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Fixed

- Don't generate CSS for bare opacity modifiers above `100` (e.g. `bg-red-500/150` or `shadow-lg/150`)
38 changes: 38 additions & 0 deletions packages/tailwindcss/src/compat/plugin-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4188,6 +4188,44 @@ describe('matchUtilities()', () => {
}
"
`)

// Bare opacity modifiers above `100` are invalid, because `color-mix(…)`
// doesn't accept percentages above `100%`
expect(
await run(
[
'scrollbar-black/100',
'scrollbar-black/101',
'scrollbar-current/150',
'scrollbar-[var(--my-color)]/150',
],
css`
@plugin "my-plugin";

@tailwind utilities;
`,
{
async loadModule(_id, base) {
return {
path: '',
base,
module: ({ matchUtilities }: PluginAPI) => {
matchUtilities(
{ scrollbar: (value) => ({ 'scrollbar-color': value }) },
{ type: ['color', 'any'], values: { black: 'black' } },
)
},
}
},
},
),
).toMatchInlineSnapshot(`
"
.scrollbar-black\\/100 {
scrollbar-color: black;
}
"
`)
})

test('functional utilities with explicit modifiers', async () => {
Expand Down
7 changes: 6 additions & 1 deletion packages/tailwindcss/src/compat/plugin-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,12 @@ export function buildPluginApi({
modifier = candidate.modifier.value
} else if (modifiers && Object.hasOwn(modifiers, candidate.modifier.value)) {
modifier = modifiers[candidate.modifier.value]
} else if (isColor && !Number.isNaN(Number(candidate.modifier.value))) {
} else if (
isColor &&
!Number.isNaN(Number(candidate.modifier.value)) &&
// `color-mix(…)` doesn't accept percentages above `100%`
Number(candidate.modifier.value) <= 100
) {
modifier = `${candidate.modifier.value}%`
} else {
modifier = null
Expand Down
52 changes: 52 additions & 0 deletions packages/tailwindcss/src/utilities.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13202,6 +13202,19 @@ for (let prefix of prefixes) {
`${prefix}-[line-width:var(--my-width)]/foo`,
]),
).toEqual('')

// Invalid bare opacity modifiers, e.g. above `100`
expect(
await run(
[`${prefix}-red-500/100.25`, `${prefix}-red-500/101`],
css`
@theme {
--color-red-500: #ef4444;
}
@tailwind utilities;
`,
),
).toEqual('')
})
}

Expand Down Expand Up @@ -14308,6 +14321,19 @@ test('bg', async () => {
]),
).toEqual('')

// Invalid bare opacity modifiers, e.g. above `100`
expect(
await run(
['bg-red-500/2.3', 'bg-red-500/101', 'bg-current/150', 'bg-[#0088cc]/101'],
css`
@theme {
--color-red-500: #ef4444;
}
@tailwind utilities;
`,
),
).toEqual('')

expect(
await run(
['bg-current/half', 'bg-current/custom', '[color:red]/half'],
Expand Down Expand Up @@ -24450,6 +24476,9 @@ test('filter', async () => {
'drop-shadow-red-500/foo',
'-drop-shadow-red-500/foo',
'-drop-shadow-red-500/50',
'drop-shadow-red-500/101',
'drop-shadow/101',
'drop-shadow-[0_0_red]/101',

'-saturate-0',
'saturate--5',
Expand Down Expand Up @@ -24492,6 +24521,7 @@ test('filter', async () => {
],
css`
@theme {
--color-red-500: #ef4444;
--drop-shadow: 0 1px 1px rgb(0 0 0 / 0.05);
}
@tailwind utilities;
Expand Down Expand Up @@ -26390,6 +26420,7 @@ test('opacity', async () => {
'opacity-2.5',
'opacity-3.25',
'opacity-4.75',
'opacity-150',
'opacity-[var(--value)]',
]),
).toMatchInlineSnapshot(`
Expand All @@ -26410,6 +26441,10 @@ test('opacity', async () => {
opacity: .15;
}

.opacity-150 {
opacity: 1.5;
}

.opacity-\\[var\\(--value\\)\\] {
opacity: var(--value);
}
Expand Down Expand Up @@ -26989,6 +27024,7 @@ test('text', async () => {
'-text-[#0088cc]/50',
'-text-[#0088cc]/[0.5]',
'-text-[#0088cc]/[50%]',
'text-red-500/500',

// font-size / line-height / letter-spacing / font-weight
'-text-sm',
Expand All @@ -26999,6 +27035,7 @@ test('text', async () => {
],
css`
@theme inline reference {
--color-red-500: #ef4444;
--text-sm: 0.875rem;
}
@tailwind utilities;
Expand Down Expand Up @@ -27377,9 +27414,14 @@ test('text-shadow', async () => {
'-text-shadow-[var(--value)]',
'text-shadow/foo',
'text-shadow-sm/foo',
'text-shadow-red-500/101',
'text-shadow/101',
'text-shadow-sm/101',
'text-shadow-[10px_10px]/101',
],
css`
@theme {
--color-red-500: #ef4444;
--text-shadow: 0 1px 1px rgb(0 0 0 / 0.05);
--text-shadow-sm: 0 1px 2px rgb(0 0 0 / 0.06);
}
Expand Down Expand Up @@ -27872,9 +27914,14 @@ test('shadow', async () => {
'shadow-sm/foo',
'shadow-[10px_10px]/foo',
'shadow-[#0088cc]/foo',
'shadow-red-500/101',
'shadow/101',
'shadow-sm/101',
'shadow-[10px_10px]/101',
],
css`
@theme {
--color-red-500: #ef4444;
--shadow: 0 1px 3px 0 rgb(0 0 0 / 0.1);
--shadow-sm: 0 1px 3px 0 rgb(0 0 0 / 0.1), 0 1px 2px -1px rgb(0 0 0 / 0.1);
}
Expand Down Expand Up @@ -28375,9 +28422,14 @@ test('inset-shadow', async () => {
'inset-shadow/foo',
'inset-shadow-sm/foo',
'inset-shadow-[10px_10px]/foo',
'inset-shadow-red-500/101',
'inset-shadow/101',
'inset-shadow-sm/101',
'inset-shadow-[10px_10px]/101',
],
css`
@theme {
--color-red-500: #ef4444;
--inset-shadow: inset 0 1px 3px 0 rgb(0 0 0 / 0.1);
--inset-shadow-sm: inset 0 1px 1px rgb(0 0 0 / 0.05);
}
Expand Down
37 changes: 20 additions & 17 deletions packages/tailwindcss/src/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,23 @@ export function asColor(
return withAlpha(value, alpha)
}

if (!isValidOpacityValue(modifier.value)) {
return null
}

// The modifier is a bare value like `50`, so convert that to `50%`.
return withAlpha(value, `${modifier.value}%`)
alpha = bareAlpha(modifier.value)
if (alpha === null) return null

return withAlpha(value, alpha)
}

/**
* Convert a bare opacity modifier like `50` to a percentage like `50%`.
*
* Values above `100` are invalid, because `color-mix(…)` doesn't accept
* percentages above `100%`. The declaration would be dropped by the browser.
*/
function bareAlpha(value: string): string | null {
if (!isValidOpacityValue(value)) return null
if (Number(value) > 100) return null
return `${value}%`
}

/**
Expand Down Expand Up @@ -4629,9 +4640,7 @@ export function createUtilities(theme: Theme) {
if (candidate.modifier.kind === 'arbitrary') {
alpha = candidate.modifier.value
} else {
if (isValidOpacityValue(candidate.modifier.value)) {
alpha = `${candidate.modifier.value}%`
}
alpha = bareAlpha(candidate.modifier.value) ?? undefined
}
}

Expand Down Expand Up @@ -5439,9 +5448,7 @@ export function createUtilities(theme: Theme) {
if (candidate.modifier.kind === 'arbitrary') {
alpha = candidate.modifier.value
} else {
if (isValidOpacityValue(candidate.modifier.value)) {
alpha = `${candidate.modifier.value}%`
}
alpha = bareAlpha(candidate.modifier.value) ?? undefined
}
}

Expand Down Expand Up @@ -5590,9 +5597,7 @@ export function createUtilities(theme: Theme) {
if (candidate.modifier.kind === 'arbitrary') {
alpha = candidate.modifier.value
} else {
if (isValidOpacityValue(candidate.modifier.value)) {
alpha = `${candidate.modifier.value}%`
}
alpha = bareAlpha(candidate.modifier.value) ?? undefined
}
}

Expand Down Expand Up @@ -5721,9 +5726,7 @@ export function createUtilities(theme: Theme) {
if (candidate.modifier.kind === 'arbitrary') {
alpha = candidate.modifier.value
} else {
if (isValidOpacityValue(candidate.modifier.value)) {
alpha = `${candidate.modifier.value}%`
}
alpha = bareAlpha(candidate.modifier.value) ?? undefined
}
}

Expand Down
Loading