From e9490ee4a9eb9c1eda262be279c19cbacb6ba6f2 Mon Sep 17 00:00:00 2001 From: Huang Xin Date: Wed, 4 Nov 2026 03:02:19 +0800 Subject: [PATCH] fix(reader): preserve publisher font lists and category fallbacks (#7302) Tokenize family lists locally so quotes, comments, CSS escapes and function commas remain intact. Use the same tokenization for generic detection and replacement, and nest every eligible category in publisher list order. Preserve the existing encoded-apostrophe fix from aae1b6517. Verification: - New tests failed before the fix: quoted names were changed, commented generics were missed, and later enabled categories kept PublisherMixed. Four unit and two computed-style browser regressions failed. - Related unit suites: 224 passed; focused Chromium suites: 10 passed, including the existing encoded O'Neill regression. - Running web reader with generated EPUB: quoted/apostrophe names preserved, commented serif and later monospace switches work, original fonts restored. - Formatting, lint and the full unit suite run through the pre-push hook. - Native Linux, Android and iOS checks not run for this parser follow-up. Co-authored-by: Claude Code --- .../utils/publisher-fonts.browser.test.ts | 33 +++++++++- .../src/__tests__/utils/style.test.ts | 39 +++++++++++- apps/readest-app/src/utils/style.ts | 61 +++++++++++++------ 3 files changed, 114 insertions(+), 19 deletions(-) diff --git a/apps/readest-app/src/__tests__/utils/publisher-fonts.browser.test.ts b/apps/readest-app/src/__tests__/utils/publisher-fonts.browser.test.ts index 7ddea0d2c..6fd60de3b 100644 --- a/apps/readest-app/src/__tests__/utils/publisher-fonts.browser.test.ts +++ b/apps/readest-app/src/__tests__/utils/publisher-fonts.browser.test.ts @@ -25,6 +25,10 @@ const BOOK_CSS = ` #sans { font: italic 20px/1.5 PublisherSans, sans-serif; } #mono { font-family: PublisherMono, monospace; } #decorative { font-family: Decorative, fantasy; } + #quoted { font-family: "Decorative, serif, ornaments", fantasy; } + #commented { font-family: PublisherSerif, /* fallback */ serif; } + #mixed { font-family: PublisherMixed, serif, monospace; } + #mixed-short { font: 16px PublisherMixed, serif, monospace; } `; const BODY = @@ -32,7 +36,9 @@ const BODY = '

e

' + '

h

' + '

f

' + - '

g

'; + '

g

i

j

' + + '

k

l

' + + '

m

'; const render = async () => { const frame = document.createElement('iframe'); @@ -112,6 +118,31 @@ describe('Ignore publisher font names', () => { expect(font('mono')).toMatch(/^PublisherMono/); }); + it('preserves quoted names and recognizes commented generic entries', async () => { + const { apply, font } = await render(); + apply({}); + expect(font('quoted')).toBe('"Decorative, serif, ornaments", fantasy'); + expect(font('commented')).toMatch(/^PublisherSerif,/); + apply({ ignorePublisherFonts: ['serif'] }); + expect(font('quoted')).toBe('"Decorative, serif, ornaments", fantasy'); + expect(font('commented')).toMatch(/^ReaderSerif,/); + expect(font('commented-inline')).toMatch(/^ReaderSerif,/); + }); + + it('uses a later enabled category and restores mixed publisher lists', async () => { + const { apply, font } = await render(); + for (const id of ['mixed', 'mixed-short']) { + apply({}); + expect(font(id)).toMatch(/^PublisherMixed,/); + apply({ ignorePublisherFonts: ['monospace'] }); + expect(font(id)).toMatch(/^ReaderMono,/); + apply({ ignorePublisherFonts: ['serif', 'monospace'] }); + expect(font(id)).toMatch(/^ReaderSerif,/); + apply({}); + expect(font(id)).toMatch(/^PublisherMixed,/); + } + }); + it('keeps the rest of a font shorthand', async () => { const { apply, style } = await render(); apply({ ignorePublisherFonts: ['sans-serif'] }); diff --git a/apps/readest-app/src/__tests__/utils/style.test.ts b/apps/readest-app/src/__tests__/utils/style.test.ts index 6e13fb259..7a7461b6f 100644 --- a/apps/readest-app/src/__tests__/utils/style.test.ts +++ b/apps/readest-app/src/__tests__/utils/style.test.ts @@ -224,6 +224,43 @@ describe('transformStylesheet', () => { }); }); + describe('publisher font list parsing (#7276)', () => { + it.each([ + '"Decorative, serif, ornaments", fantasy', + '"A /* serif, */ B", fantasy', + String.raw`"A \"quoted, serif\" name", fantasy`, + String.raw`Publisher\,serif, fantasy`, + 'var(--publisher, serif), fantasy', + ])('preserves quoted, escaped and functional names: %s', (families) => { + const css = `p { font-family: ${families}; }`; + expect(transformStylesheet(css, VW, VH, VERTICAL)).toBe(css); + expect(transformInlineFonts(`font-family: ${families}`)).toBe(`font-family: ${families}`); + expect(transformInlineFonts(`font: 16px ${families}`)).toBe(`font: 16px ${families}`); + }); + + it('recognizes generics surrounded by comments without altering quoted comment text', () => { + const families = '"A /* serif, */ B", /* fallback */ serif /* end */'; + expect(transformStylesheet(`p { font-family: ${families}; }`, VW, VH, VERTICAL)).toContain( + 'var(--book-serif, "A /* serif, */ B", /* fallback */ var(--serif, serif) /* end */)', + ); + for (const prefix of ['font-family:', 'font: 16px']) { + expect(transformInlineFonts(`${prefix} ${families}`)).toBe( + `${prefix} var(--book-serif, ${families})`, + ); + } + }); + + it('nests every eligible category in list order', () => { + const families = 'Publisher, serif, monospace'; + expect(transformInlineFonts(`font-family: ${families}`)).toBe( + `font-family: var(--book-serif, var(--book-monospace, ${families}))`, + ); + expect(transformInlineFonts(`font: 16px ${families}`)).toBe( + `font: 16px var(--book-serif, var(--book-monospace, ${families}))`, + ); + }); + }); + describe('font-family generic replacements', () => { it('replaces serif with var(--serif, serif)', () => { const css = '.text { font-family: serif; }'; @@ -253,7 +290,7 @@ describe('transformStylesheet', () => { const css = '.text { font-family: Helvetica, sans-serif, monospace; }'; const result = transformStylesheet(css, VW, VH, VERTICAL); expect(result).toContain( - 'font-family: var(--book-sans-serif, Helvetica, var(--sans-serif, sans-serif), var(--monospace, monospace))', + 'font-family: var(--book-sans-serif, var(--book-monospace, Helvetica, var(--sans-serif, sans-serif), var(--monospace, monospace)))', ); }); diff --git a/apps/readest-app/src/utils/style.ts b/apps/readest-app/src/utils/style.ts index c4c467345..07ba764ba 100644 --- a/apps/readest-app/src/utils/style.ts +++ b/apps/readest-app/src/utils/style.ts @@ -1282,15 +1282,51 @@ export const applyTranslationStyle = (viewSettings: ViewSettings) => { // "Ignore publisher font names" (#7276): a list that names a generic family is // wrapped as `var(--book-serif, Palatino, serif)`. --book-serif is only defined // while that category's switch is on, so until then the book's list applies. -const wrapPublisherFonts = (families: string, rewritten = families) => { - const generic = families - .split(',') - .map((family) => family.trim().toLowerCase()) - .find((family) => /^(serif|sans-serif|monospace)$/.test(family)); - if (!generic) return rewritten; +const wrapPublisherFonts = (families: string, rewriteGenerics = false) => { + // Strings, comments and escapes are indivisible; commas inside functions + // also belong to the current family. Use the same tokens for detection and + // replacement so quoted names and comment text stay byte-for-byte intact. + const tokens = + families.match( + /\/\*[\s\S]*?\*\/|"(?:\\[\s\S]|[^"\\])*"|'(?:\\[\s\S]|[^'\\])*'|\\(?:[0-9a-fA-F]{1,6}\s?|[^\n\r\f])|[\w-]+|\s+|[^\s]/g, + ) ?? []; + const generics = new Set(); + let depth = 0; + let start = 0; + const finishFamily = (end: number) => { + const names: number[] = []; + for (let i = start; i < end; i++) { + if (!/^\s+$|^\/\*/.test(tokens[i]!)) names.push(i); + } + const index = names[0]; + if ( + names.length === 1 && + index !== undefined && + /^(serif|sans-serif|monospace)$/i.test(tokens[index]!) + ) { + const name = tokens[index]!.toLowerCase(); + generics.add(name); + if (rewriteGenerics) tokens[index] = `var(--${name}, ${name})`; + } + }; + for (let i = 0; i < tokens.length; i++) { + if (tokens[i] === '(') depth++; + else if (tokens[i] === ')') depth--; + else if (tokens[i] === ',' && depth === 0) { + finishFamily(i); + start = i + 1; + } + } + finishFamily(tokens.length); + if (!generics.size) return families; + const rewritten = tokens.join(''); const list = rewritten.trim(); const lead = rewritten.slice(0, rewritten.indexOf(list)); - return `${lead}var(--book-${generic}, ${list})${rewritten.slice(lead.length + list.length)}`; + const wrapped = [...generics].reduceRight( + (fallback, name) => `var(--book-${name}, ${fallback})`, + list, + ); + return lead + wrapped + rewritten.slice(lead.length + list.length); }; const FONT_SIZE = @@ -1641,16 +1677,7 @@ export const transformStylesheet = ( .replace(/(font-family\s*:\s*)([^;{}]*)/gi, (_match: string, prefix: string, value: string) => { const important = /\s*!\s*important\s*$/i.exec(value); const families = important ? value.slice(0, important.index) : value; - const rewritten = families - .split(',') - .map((family: string) => { - const generic = /^(serif|sans-serif|monospace)$/i.exec(family.trim()); - if (!generic) return family; - const name = generic[1]!.toLowerCase(); - return family.replace(generic[1]!, `var(--${name}, ${name})`); - }) - .join(','); - return prefix + wrapPublisherFonts(families, rewritten) + (important ? important[0] : ''); + return prefix + wrapPublisherFonts(families, true) + (important ? important[0] : ''); }) .replace(/([\s;])font-weight\s*:\s*normal/gi, '$1font-weight: var(--font-weight)') .replace(/([\s;])color\s*:\s*black/gi, '$1color: var(--theme-fg-color)')