diff --git a/packages/cli/src/linter/tailwind/v4/font-family.test.ts b/packages/cli/src/linter/tailwind/v4/font-family.test.ts new file mode 100644 index 0000000..a958b64 --- /dev/null +++ b/packages/cli/src/linter/tailwind/v4/font-family.test.ts @@ -0,0 +1,67 @@ +// Copyright 2026 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import { describe, it, expect } from 'bun:test'; +import { TailwindV4EmitterHandler } from './handler.js'; +import { ModelHandler } from '../../model/handler.js'; + +const emitter = new TailwindV4EmitterHandler(); +const modelHandler = new ModelHandler(); + +function fontFamilyOf(fontFamily: string): string | undefined { + const { designSystem } = modelHandler.execute({ + sourceMap: new Map(), + typography: { body: { fontFamily, fontSize: '16px' } }, + }); + const result = emitter.execute(designSystem); + if (!result.success) throw new Error('Expected success'); + return result.data.theme.fontFamily?.['body']; +} + +// A CSS string is always a single family name (CSS Fonts 4 §2.1), so a +// fallback stack must be emitted as a comma-separated list of names, and +// generic families must stay unquoted keywords. +describe('TailwindV4EmitterHandler font-family lists', () => { + it('quotes a single family name', () => { + expect(fontFamilyOf('Inter')).toBe('"Inter"'); + expect(fontFamilyOf('Plus Jakarta Sans')).toBe('"Plus Jakarta Sans"'); + }); + + it('emits a fallback stack as separate family names', () => { + expect(fontFamilyOf('Inter, sans-serif')).toBe('"Inter", sans-serif'); + expect(fontFamilyOf('Inter,Helvetica Neue,Arial,sans-serif')) + .toBe('"Inter", "Helvetica Neue", "Arial", sans-serif'); + }); + + it('leaves generic family keywords unquoted', () => { + expect(fontFamilyOf('monospace')).toBe('monospace'); + expect(fontFamilyOf('system-ui')).toBe('system-ui'); + expect(fontFamilyOf('ui-sans-serif, system-ui')).toBe('ui-sans-serif, system-ui'); + expect(fontFamilyOf('Georgia, Serif')).toBe('"Georgia", serif'); + }); + + it('does not double-quote names that are already quoted', () => { + expect(fontFamilyOf('"Public Sans", Arial, sans-serif')).toBe('"Public Sans", "Arial", sans-serif'); + expect(fontFamilyOf("'Fira Code', monospace")).toBe('"Fira Code", monospace'); + }); + + it('keeps a comma inside a quoted family name', () => { + expect(fontFamilyOf('"Foo, Bar", serif')).toBe('"Foo, Bar", serif'); + }); + + it('still escapes quotes, backslashes and line terminators inside a name', () => { + expect(fontFamilyOf('Fancy "Font"')).toBe('"Fancy \\"Font\\""'); + expect(fontFamilyOf('Evil\nFamily, serif')).toBe('"Evil\\a Family", serif'); + }); +}); diff --git a/packages/cli/src/linter/tailwind/v4/handler.ts b/packages/cli/src/linter/tailwind/v4/handler.ts index e84ec0a..8cf566f 100644 --- a/packages/cli/src/linter/tailwind/v4/handler.ts +++ b/packages/cli/src/linter/tailwind/v4/handler.ts @@ -78,7 +78,7 @@ export class TailwindV4EmitterHandler implements TailwindV4EmitterSpec { const fontWeight: Record = {}; for (const [name, typo] of state.typography) { const cssKey = toCssKey(name); - if (typo.fontFamily) fontFamily[cssKey] = cssStringLiteral(typo.fontFamily); + if (typo.fontFamily) fontFamily[cssKey] = cssFontFamilyList(typo.fontFamily); if (typo.fontSize) fontSize[cssKey] = dimToString(typo.fontSize); if (typo.lineHeight) lineHeight[cssKey] = dimToString(typo.lineHeight); if (typo.letterSpacing) letterSpacing[cssKey] = dimToString(typo.letterSpacing); @@ -123,6 +123,54 @@ function dimToString(dim: ResolvedDimension): string { return `${dim.value}${dim.unit}`; } +/** CSS generic font families (CSS Fonts 4 §4.1.1); these must stay unquoted keywords. */ +const GENERIC_FONT_FAMILIES = new Set([ + 'serif', 'sans-serif', 'cursive', 'fantasy', 'monospace', 'system-ui', 'math', + 'emoji', 'fangsong', 'ui-serif', 'ui-sans-serif', 'ui-monospace', 'ui-rounded', +]); + +/** + * Convert a DESIGN.md fontFamily value (e.g. `Inter, sans-serif`) into a CSS + * font-family list. A CSS string is always a single family name, so each + * family in a fallback stack is quoted on its own and generic families are + * left as bare keywords. Commas inside a quoted family name are preserved. + */ +function cssFontFamilyList(value: string): string { + const families = splitFontFamilies(value); + if (families.length === 0) return cssStringLiteral(value); + return families + .map((family) => { + const quoted = /^(["'])([\s\S]*)\1$/.exec(family); + if (quoted) return cssStringLiteral(quoted[2]!); + const generic = family.toLowerCase(); + return GENERIC_FONT_FAMILIES.has(generic) ? generic : cssStringLiteral(family); + }) + .join(', '); +} + +/** Split a font-family list on top-level commas, ignoring commas inside quotes. */ +function splitFontFamilies(value: string): string[] { + const families: string[] = []; + let current = ''; + let quote: string | null = null; + for (const ch of value) { + if (quote) { + if (ch === quote) quote = null; + current += ch; + } else if (ch === '"' || ch === "'") { + quote = ch; + current += ch; + } else if (ch === ',') { + families.push(current); + current = ''; + } else { + current += ch; + } + } + families.push(current); + return families.map((f) => f.trim()).filter((f) => f.length > 0); +} + /** * Wrap a string value in double quotes, escaping embedded `\` and `"`. * Produces a CSS-safe string literal suitable for font-family values.