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