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

Commit 5670e7a

Browse files
committed
fix: validate width, height, size, and min/max sizing values
w-sidebar emitted width: sidebar (same for h-, size-, min-w-, max-w-, min-h-, max-h-), leaking semantic class names into CSS. The sizing utilities now share one resolver: keyword maps, theme spacing, fractions, off-scale numbers (0.25rem steps, so w-4.5 finally works), and arbitrary values; unknown words emit nothing.
1 parent b2ef16c commit 5670e7a

3 files changed

Lines changed: 67 additions & 44 deletions

File tree

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { UtilityRule } from './rules'
2-
import { resolveColorValue } from './rules'
2+
import { resolveColorValue, resolveSizeToken } from './rules'
33

44
// Advanced utilities
55

@@ -27,22 +27,22 @@ export const minMaxSizingRule: UtilityRule = (parsed, config) => {
2727
}
2828

2929
if (parsed.utility === 'min-w' && parsed.value) {
30-
const value = config.theme.spacing[parsed.value] || minMaxMap[parsed.value] || parsed.value
31-
return { 'min-width': value } as Record<string, string>
30+
const value = resolveSizeToken(parsed, config, minMaxMap)
31+
return value !== undefined ? { 'min-width': value } as Record<string, string> : undefined
3232
}
3333
if (parsed.utility === 'max-w' && parsed.value) {
34-
const value = config.theme.spacing[parsed.value] || minMaxMap[parsed.value] || parsed.value
35-
return { 'max-width': value } as Record<string, string>
34+
const value = resolveSizeToken(parsed, config, minMaxMap)
35+
return value !== undefined ? { 'max-width': value } as Record<string, string> : undefined
3636
}
3737
if (parsed.utility === 'min-h' && parsed.value) {
3838
const hMap: Record<string, string> = { ...minMaxMap, screen: '100vh' }
39-
const value = config.theme.spacing[parsed.value] || hMap[parsed.value] || parsed.value
40-
return { 'min-height': value } as Record<string, string>
39+
const value = resolveSizeToken(parsed, config, hMap)
40+
return value !== undefined ? { 'min-height': value } as Record<string, string> : undefined
4141
}
4242
if (parsed.utility === 'max-h' && parsed.value) {
4343
const hMap: Record<string, string> = { ...minMaxMap, screen: '100vh' }
44-
const value = config.theme.spacing[parsed.value] || hMap[parsed.value] || parsed.value
45-
return { 'max-height': value } as Record<string, string>
44+
const value = resolveSizeToken(parsed, config, hMap)
45+
return value !== undefined ? { 'max-height': value } as Record<string, string> : undefined
4646
}
4747
}
4848

‎packages/crosswind/src/rules.ts‎

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,34 @@ export const spacingRule: UtilityRule = (parsed, config) => {
293293
return result
294294
}
295295

296+
// Resolve a size/spacing token shared by the sizing utilities: fractions,
297+
// keyword map, theme spacing, off-scale numbers (0.25rem steps), and
298+
// arbitrary values. Unknown words return undefined — they previously
299+
// leaked verbatim (`w-sidebar` emitted `width: sidebar;`).
300+
export function resolveSizeToken(
301+
parsed: { value?: string, arbitrary: boolean },
302+
config: { theme: { spacing: Record<string, string> } },
303+
sizeMap: Record<string, string>,
304+
): string | undefined {
305+
const token = parsed.value!
306+
if (token.includes('/') && !parsed.arbitrary) {
307+
const [num, denom] = token.split('/').map(Number)
308+
if (Number.isNaN(num) || Number.isNaN(denom) || denom === 0)
309+
return undefined
310+
return `${(num / denom) * 100}%`
311+
}
312+
const spacing = config.theme.spacing[token]
313+
if (spacing !== undefined)
314+
return spacing
315+
if (sizeMap[token])
316+
return sizeMap[token]
317+
if (parsed.arbitrary)
318+
return token
319+
if (/^\d+(?:\.\d+)?$/.test(token))
320+
return `${Number.parseFloat(token) * 0.25}rem`
321+
return undefined
322+
}
323+
296324
// Width and height utilities
297325
export const sizingRule: UtilityRule = (parsed, config) => {
298326
if (parsed.utility === 'w' && parsed.value) {
@@ -310,18 +338,8 @@ export const sizingRule: UtilityRule = (parsed, config) => {
310338
max: 'max-content',
311339
fit: 'fit-content',
312340
}
313-
// Handle fractions: 1/2 -> 50%
314-
if (parsed.value.includes('/')) {
315-
const [num, denom] = parsed.value.split('/').map(Number)
316-
// Validate: skip invalid fractions (NaN or division by zero)
317-
if (Number.isNaN(num) || Number.isNaN(denom) || denom === 0) {
318-
return undefined
319-
}
320-
return { width: `${(num / denom) * 100}%` } as Record<string, string>
321-
}
322-
// Check spacing config first, then sizeMap, then raw value
323-
const value = config.theme.spacing[parsed.value] || sizeMap[parsed.value] || parsed.value
324-
return { width: value } as Record<string, string>
341+
const value = resolveSizeToken(parsed, config, sizeMap)
342+
return value !== undefined ? { width: value } as Record<string, string> : undefined
325343
}
326344

327345
if (parsed.utility === 'h' && parsed.value) {
@@ -337,18 +355,8 @@ export const sizingRule: UtilityRule = (parsed, config) => {
337355
max: 'max-content',
338356
fit: 'fit-content',
339357
}
340-
// Handle fractions: 3/4 -> 75%
341-
if (parsed.value.includes('/')) {
342-
const [num, denom] = parsed.value.split('/').map(Number)
343-
// Validate: skip invalid fractions (NaN or division by zero)
344-
if (Number.isNaN(num) || Number.isNaN(denom) || denom === 0) {
345-
return undefined
346-
}
347-
return { height: `${(num / denom) * 100}%` } as Record<string, string>
348-
}
349-
// Check spacing config first, then sizeMap, then raw value
350-
const value = config.theme.spacing[parsed.value] || sizeMap[parsed.value] || parsed.value
351-
return { height: value } as Record<string, string>
358+
const value = resolveSizeToken(parsed, config, sizeMap)
359+
return value !== undefined ? { height: value } as Record<string, string> : undefined
352360
}
353361

354362
// Size utility (width + height shorthand)
@@ -360,17 +368,8 @@ export const sizingRule: UtilityRule = (parsed, config) => {
360368
max: 'max-content',
361369
fit: 'fit-content',
362370
}
363-
// Handle fractions: 1/2 -> 50%
364-
if (parsed.value.includes('/')) {
365-
const [num, denom] = parsed.value.split('/').map(Number)
366-
if (Number.isNaN(num) || Number.isNaN(denom) || denom === 0) {
367-
return undefined
368-
}
369-
const percent = `${(num / denom) * 100}%`
370-
return { width: percent, height: percent } as Record<string, string>
371-
}
372-
const value = config.theme.spacing[parsed.value] || sizeMap[parsed.value] || parsed.value
373-
return { width: value, height: value } as Record<string, string>
371+
const value = resolveSizeToken(parsed, config, sizeMap)
372+
return value !== undefined ? { width: value, height: value } as Record<string, string> : undefined
374373
}
375374

376375
return undefined

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,27 @@ describe('typography value validation', () => {
157157
expect(out).toContain('overflow: visible;')
158158
})
159159
})
160+
161+
describe('sizing value validation', () => {
162+
it('rejects unknown words', () => {
163+
expect(css('w-foo')).toBe('')
164+
expect(css('w-sidebar')).toBe('')
165+
expect(css('h-foo')).toBe('')
166+
expect(css('size-foo')).toBe('')
167+
expect(css('min-w-foo')).toBe('')
168+
expect(css('max-h-foo')).toBe('')
169+
})
170+
171+
it('keeps keywords, scale, fractions, off-scale numbers, and arbitrary', () => {
172+
expect(css('w-full')).toContain('width: 100%;')
173+
expect(css('w-dvw')).toContain('width: 100dvw;')
174+
expect(css('w-4')).toContain('width: 1rem;')
175+
expect(css('w-1/2')).toContain('width: 50%;')
176+
expect(css('h-screen')).toContain('height: 100vh;')
177+
expect(css('size-8')).toContain('width: 2rem;')
178+
expect(css('max-w-2xl')).toContain('max-width: 42rem;')
179+
expect(css('min-h-screen')).toContain('min-height: 100vh;')
180+
expect(css('w-[calc(100%-2rem)]')).toContain('width: calc(100%-2rem);')
181+
expect(css('max-w-[70ch]')).toContain('max-width: 70ch;')
182+
})
183+
})

0 commit comments

Comments
 (0)