镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Commit 323b9f1

Browse files
committed
fix: reject unknown words in gradient color stops
from-foo, via-foo, and to-foo leaked the raw word into the --cw-gradient-* variables, silently corrupting the gradient stop list. Stops now resolve theme colors (including DEFAULT shades and opacity modifiers), CSS color expressions, and arbitrary values; unknown words emit nothing.
1 parent 268d6d0 commit 323b9f1

2 files changed

Lines changed: 33 additions & 3 deletions

File tree

‎packages/crosswind/src/rules-advanced.ts‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -466,14 +466,24 @@ export const gradientStopsRule: UtilityRule = (parsed, config) => {
466466
}
467467
}
468468

469-
// Arbitrary color like `#FF3E54` — apply alpha via the hex path.
470-
if (lookup.startsWith('#')) return applyAlpha(lookup)
469+
// Arbitrary values and CSS color expressions pass through the alpha
470+
// path; unknown bare words are rejected — they previously leaked into
471+
// the gradient variables (`from-foo` -> --cw-gradient-from: foo).
472+
if (
473+
parsed.arbitrary
474+
|| lookup.startsWith('#')
475+
|| /^(?:var|rgb|rgba|hsl|hsla|oklch|oklab|lab|lch|color|color-mix)\(/.test(lookup)
476+
) {
477+
return applyAlpha(lookup)
478+
}
471479

472-
return applyAlpha(lookup)
480+
return undefined
473481
}
474482

475483
if (parsed.utility === 'from' && parsed.value) {
476484
const color = getColor(parsed.value)
485+
if (color === undefined)
486+
return undefined
477487
return {
478488
'--cw-gradient-from': color,
479489
'--cw-gradient-to': 'rgb(255 255 255 / 0)',
@@ -483,6 +493,8 @@ export const gradientStopsRule: UtilityRule = (parsed, config) => {
483493

484494
if (parsed.utility === 'via' && parsed.value) {
485495
const color = getColor(parsed.value)
496+
if (color === undefined)
497+
return undefined
486498
return {
487499
'--cw-gradient-to': 'rgb(255 255 255 / 0)',
488500
'--cw-gradient-stops': `var(--cw-gradient-from), ${color}, var(--cw-gradient-to)`,
@@ -491,6 +503,8 @@ export const gradientStopsRule: UtilityRule = (parsed, config) => {
491503

492504
if (parsed.utility === 'to' && parsed.value) {
493505
const color = getColor(parsed.value)
506+
if (color === undefined)
507+
return undefined
494508
return {
495509
'--cw-gradient-to': color,
496510
'--cw-gradient-stops': 'var(--cw-gradient-from), var(--cw-gradient-to)',

‎packages/crosswind/test/value-validation.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,3 +285,19 @@ describe('between-element utilities value validation', () => {
285285
expect(css('border-opacity-75')).toContain('--cw-border-opacity: 0.75;')
286286
})
287287
})
288+
289+
describe('gradient stop value validation', () => {
290+
it('rejects unknown words', () => {
291+
expect(css('from-foo')).toBe('')
292+
expect(css('via-foo')).toBe('')
293+
expect(css('to-foo')).toBe('')
294+
})
295+
296+
it('keeps theme colors, opacity modifiers, and arbitrary values', () => {
297+
expect(css('from-blue-500')).toContain('--cw-gradient-from: oklch(62.3% 0.214 259.815);')
298+
expect(css('from-blue-500/50')).toContain('/ 0.5)')
299+
expect(css('via-transparent')).toContain('transparent')
300+
expect(css('to-[#ff3e54]')).toContain('--cw-gradient-to: #ff3e54;')
301+
expect(css('from-[var(--brand)]')).toContain('--cw-gradient-from: var(--brand);')
302+
})
303+
})

0 commit comments

Comments
 (0)