Files
genoffice/apps/docs/tests/doc-theme-css.test.ts
T
merrick-2002andGenOffice 7023e635bd Sync snapshot (2026-08-10) (#80)
- Improve DOCX rendering fidelity, pagination, image handling, and Markdown paste
- Expand PDF text editing with paragraph reflow, style preservation, and more reliable text matching
- Harden XLSX sheet deletion, localized date formats, multi-row autofit, charts, and system font support
- Add inline AI turn rollback and sent-attachment visibility across editors
- Add Office-style ScreenTips and refine ribbons, dialogs, menus, and themes
- Improve Slides cropping, rotate and flip tools, printing, animations, and editing workflows
- Add shell settings, configurable save locations, account credits, and update recovery guidance
- Add Intel macOS and RPM download support and refresh public documentation

Co-authored-by: GenOffice <merrick-2002@users.noreply.github.com>
2026-08-10 23:17:11 +08:00

128 lines
5.1 KiB
TypeScript

/**
* Applying a theme has to change the page, not only the saved file.
* docThemeCss is generated from live theme state, so the render reflects a pick
* immediately.
*/
import { describe, expect, it } from 'vitest'
import type { ParsedDocFull, StyleDisplay, StyleInfo } from '@genoffice/docx-engine'
import { docLineFactor, docStyleCss, docThemeCss } from '../src/renderer/doc-style-css'
describe('docThemeCss', () => {
it('emits body and heading fonts from the theme font pair', () => {
const css = docThemeCss({ major: 'Trebuchet MS', minor: 'Trebuchet MS' }, null)
expect(css).toContain('.doc-page {')
expect(css).toContain('Trebuchet MS')
expect(css).toContain('.doc-page h1')
})
it('emits the accent color for headings and the ribbon accent variable', () => {
const css = docThemeCss(null, { accent1: '90C226' })
expect(css).toContain('--theme-accent:#90C226')
expect(css).toContain('color:#90C226')
})
it('is empty without a theme, so document CSS stays authoritative', () => {
expect(docThemeCss(null, null)).toBe('')
expect(docThemeCss({ major: '', minor: '' }, {})).toBe('')
})
})
describe('docLineFactor — CJK factor source', () => {
const parsedWith = (
normalDisplay: StyleDisplay | undefined,
eastAsiaFont?: string,
): ParsedDocFull => {
const styles = new Map<string, StyleInfo>()
if (normalDisplay) {
styles.set('Normal', {
styleId: 'Normal',
name: 'Normal',
type: 'paragraph',
isDefault: true,
display: normalDisplay,
} as StyleInfo)
}
return {
styles,
docDefaults: eastAsiaFont ? { eastAsiaFont } : {},
blocks: [],
} as unknown as ParsedDocFull
}
it("Normal's declared EA face wins over docDefaults", () => {
const parsed = parsedWith({ font: 'Noto Sans KR', fontAscii: 'Calibri' }, 'SimSun')
expect(docLineFactor(parsed, true)).toBe(1.4583)
})
it('a Latin-only Normal (font === fontAscii) does not override the docDefaults EA font', () => {
const parsed = parsedWith({ font: 'Calibri', fontAscii: 'Calibri' }, 'SimSun')
expect(docLineFactor(parsed, true)).toBe(1.7)
})
it('falls back to the SimSun-class factor without any declared EA font', () => {
expect(docLineFactor(parsedWith(undefined), true)).toBe(1.7)
})
it('a same-slot Korean Normal (Malgun in both slots) still wins for the kr factor', () => {
;(globalThis as { CSS?: unknown }).CSS ??= { escape: (s: string) => s }
const parsed = parsedWith({ font: 'Malgun Gothic', fontAscii: 'Malgun Gothic' }, 'Batang')
expect(docStyleCss(parsed)).toContain('--doc-line-factor-kr:1.775')
expect(docLineFactor(parsed, true)).toBe(1.775)
})
it('kr factor skips a Latin-only Normal and reads the docDefaults EA font', () => {
// node test env has no CSS.escape
;(globalThis as { CSS?: unknown }).CSS ??= { escape: (s: string) => s }
const parsed = parsedWith({ font: 'Calibri', fontAscii: 'Calibri' }, 'Malgun Gothic')
expect(docStyleCss(parsed)).toContain('--doc-line-factor-kr:1.775')
})
})
describe('docStyleCss — paragraph spacing fallback', () => {
const parsedWith = (docDefaults: Record<string, unknown>): ParsedDocFull => {
;(globalThis as { CSS?: unknown }).CSS ??= { escape: (s: string) => s }
return { styles: new Map(), docDefaults, blocks: [] } as unknown as ParsedDocFull
}
const blockRule = (css: string): string =>
css.split('\n').find((l) => l.startsWith('.doc-page p,')) ?? ''
it('undeclared spacing renders with zero margins like Word', () => {
const rule = blockRule(docStyleCss(parsedWith({})))
expect(rule).toContain('margin-top:0.0pt')
expect(rule).toContain('margin-bottom:0.0pt')
})
it('declared docDefaults spacing still applies', () => {
const rule = blockRule(docStyleCss(parsedWith({ spaceAfterTwips: 200, spaceBeforeTwips: 40 })))
expect(rule).toContain('margin-top:round(down, 2.0pt, var(--doc-grid-pitch,0.0001px))')
expect(rule).toContain('margin-bottom:round(down, 10.0pt, var(--doc-grid-pitch,0.0001px))')
})
})
describe('docStyleCss — style indent vs list geometry', () => {
const parsedWithStyle = (display: StyleDisplay): ParsedDocFull => {
;(globalThis as { CSS?: unknown }).CSS ??= { escape: (s: string) => s }
const styles = new Map<string, StyleInfo>()
styles.set('ListParagraph', {
styleId: 'ListParagraph',
name: 'List Paragraph',
type: 'paragraph',
display,
} as StyleInfo)
return { styles, docDefaults: {}, blocks: [] } as unknown as ParsedDocFull
}
// Word overrides indents per property (direct > numbering > style), never adds them:
// the style indent must skip .doc-li margins and only feed the --li-left fallback
it('style w:ind skips list items and becomes the --li-left fallback', () => {
const css = docStyleCss(parsedWithStyle({ indentLeftTwips: 720 }))
expect(css).toContain(
'.doc-page [data-style="ListParagraph"]:not(.doc-li) { margin-inline-start:36.0pt }',
)
expect(css).toContain(
'.doc-page .doc-li[data-style="ListParagraph"] { --style-li-left:36.0pt }',
)
expect(css).not.toContain('margin-left')
})
})