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

Commit 7d54f25

Browse files
committed
fix: drop rules with unknown variants and support arbitrary variants
An unrecognized variant was silently ignored, so a typo like hoveer:flex emitted an always-on .hoveer\:flex rule — styles applied unconditionally instead of not at all. Rules now drop when any variant has no handler. Arbitrary variants gain real support instead of falling into that hole: [&>li]:flex emits .…>li, [&_p]: uses descendant combinators (underscore = space), [&:hover]: appends the pseudo, and [@media(...)]: wraps the rule in that media query. The perf test's whitespace-joined class strings are now split like the scanner does.
1 parent 362c833 commit 7d54f25

3 files changed

Lines changed: 85 additions & 1 deletion

File tree

‎packages/crosswind/src/generator.ts‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,6 +1453,7 @@ export class CSSGenerator {
14531453
private screenBreakpoints: Map<string, string>
14541454
private noMatchCache: Set<string> = new Set()
14551455
private usedKeyframes: Set<string> = new Set()
1456+
private variantHandledCache: Map<string, boolean> = new Map()
14561457
// While expanding a shortcut, rules emit under the shortcut's own class
14571458
// name (with variants applied to it: `.btn:hover`) instead of the
14581459
// sub-utility's selector, which the markup never carries.
@@ -2051,7 +2052,38 @@ export class CSSGenerator {
20512052
/**
20522053
* Add a CSS rule with variants applied
20532054
*/
2055+
/**
2056+
* A variant the generator has no handler for must drop the whole rule.
2057+
* Previously an unknown variant was silently ignored, so a typo like
2058+
* `hoveer:flex` emitted `.hoveer\:flex { display: flex; }` — an
2059+
* always-on rule instead of nothing.
2060+
*/
2061+
private isVariantHandled(variant: string): boolean {
2062+
const cached = this.variantHandledCache.get(variant)
2063+
if (cached !== undefined)
2064+
return cached
2065+
const known
2066+
= VARIANT_SELECTORS[variant] !== undefined
2067+
|| PREFIX_VARIANTS[variant] !== undefined
2068+
|| NOT_VARIANT_SELECTORS[variant] !== undefined
2069+
|| this.screenBreakpoints.has(variant)
2070+
|| (variant.startsWith('max-') && this.screenBreakpoints.has(variant.slice(4)))
2071+
|| (variant.startsWith('@') && this.screenBreakpoints.has(variant.slice(1)))
2072+
|| variant.startsWith('group-') || variant.startsWith('group/')
2073+
|| variant.startsWith('peer-') || variant.startsWith('peer/')
2074+
|| variant.startsWith('has-') || variant.startsWith('aria-') || variant.startsWith('data-')
2075+
|| variant.startsWith('supports-')
2076+
|| (variant.startsWith('[') && variant.endsWith(']'))
2077+
|| ['print', 'motion-safe', 'motion-reduce', 'contrast-more', 'contrast-less', 'landscape', 'portrait', 'forced-colors'].includes(variant)
2078+
this.variantHandledCache.set(variant, known)
2079+
return known
2080+
}
2081+
20542082
private addRule(parsed: ParsedClass, properties: Record<string, string>, childSelector?: string, pseudoElement?: string): void {
2083+
for (let i = 0; i < parsed.variants.length; i++) {
2084+
if (!this.isVariantHandled(parsed.variants[i]))
2085+
return
2086+
}
20552087
// Use cached selector if available. Shortcut expansion changes the base
20562088
// selector for the same parsed class, so it needs its own cache slot.
20572089
const cacheKey = this.shortcutSelectorRaw === null
@@ -2118,6 +2150,17 @@ export class CSSGenerator {
21182150
for (let i = 0; i < variantsLen; i++) {
21192151
const variant = variants[i]
21202152

2153+
// Arbitrary selector variants: [&>li]:, [&_p]:, [&:hover]: — the &
2154+
// stands for the selector built so far. Underscores become spaces
2155+
// (descendant combinators). [@media...] is handled by getMediaQuery.
2156+
if (variant.charCodeAt(0) === 91 && variant.charCodeAt(variant.length - 1) === 93) {
2157+
const inner = variant.slice(1, -1).replace(/_/g, ' ')
2158+
if (inner.includes('&')) {
2159+
selector = inner.replace(/&/g, selector)
2160+
}
2161+
continue
2162+
}
2163+
21212164
// Try suffix selector lookup first (most common case)
21222165
const suffixSelector = VARIANT_SELECTORS[variant]
21232166
if (suffixSelector !== undefined) {
@@ -2288,6 +2331,15 @@ export class CSSGenerator {
22882331
const variant = variants[i]
22892332
const firstChar = variant.charCodeAt(0)
22902333

2334+
// Arbitrary media variants: [@media(min-width:900px)]: — the inner
2335+
// @media condition joins the collected media conditions.
2336+
if (firstChar === 91 && variant.startsWith('[@media') && variant.endsWith(']')) {
2337+
const condition = variant.slice(1, -1).replace(/_/g, ' ').slice('@media'.length).trim()
2338+
if (condition)
2339+
mediaConditions.push(condition)
2340+
continue
2341+
}
2342+
22912343
// Container queries (@sm, @md, @lg, etc.) - '@' = 64
22922344
if (firstChar === 64) {
22932345
const breakpointKey = variant.slice(1)

‎packages/crosswind/test/generator.test.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -945,3 +945,30 @@ describe('max-* variants and media type ordering', () => {
945945
expect(gen.toCSS(false)).toContain('@media print and (min-width: 768px)')
946946
})
947947
})
948+
949+
describe('unknown and arbitrary variants', () => {
950+
it('drops rules with unknown variants instead of applying unconditionally', () => {
951+
for (const cls of ['foo:flex', 'hoveer:underline', 'xxl:p-4']) {
952+
const gen = new CSSGenerator(defaultConfig)
953+
gen.generate(cls)
954+
expect(gen.toCSS(false).trim()).toBe('')
955+
}
956+
})
957+
958+
it('supports arbitrary selector variants with &', () => {
959+
const gen = new CSSGenerator(defaultConfig)
960+
gen.generateBatch(['[&>li]:flex', '[&_p]:underline', '[&:hover]:italic'])
961+
const out = gen.toCSS(false)
962+
expect(out).toContain('.\\[\\&\\>li\\]\\:flex>li {')
963+
expect(out).toContain('.\\[\\&_p\\]\\:underline p {')
964+
expect(out).toContain('.\\[\\&\\:hover\\]\\:italic:hover {')
965+
})
966+
967+
it('supports arbitrary media variants', () => {
968+
const gen = new CSSGenerator(defaultConfig)
969+
gen.generate('[@media(min-width:900px)]:flex')
970+
const out = gen.toCSS(false)
971+
expect(out).toContain('@media (min-width:900px)')
972+
expect(out).toContain('display: flex;')
973+
})
974+
})

‎packages/crosswind/test/performance.test.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,14 +479,19 @@ describe('Performance Tests', () => {
479479
}
480480

481481
const start = performance.now()
482+
// Split like the scanner does — generate() receives single class
483+
// names, never whitespace-joined strings.
482484
for (const util of utilities) {
483-
gen.generate(util)
485+
for (const cls of util.split(/\s+/)) {
486+
gen.generate(cls)
487+
}
484488
}
485489
const css = gen.toCSS(false)
486490
const elapsed = performance.now() - start
487491

488492
expect(elapsed).toBeLessThan(300)
489493
expect(css.length).toBeGreaterThan(0)
494+
expect(css).toContain('.hover\\:scale-105:hover')
490495
})
491496

492497
it('should handle progressive enhancement patterns efficiently', () => {

0 commit comments

Comments
 (0)