mirror of
https://github.com/readest/readest.git
synced 2026-12-02 18:04:49 +00:00
fix(reader): preserve publisher font lists and category fallbacks (#7302)
Some checks are pending
CodeQL Advanced / Analyze (actions) (push) Waiting to run
CodeQL Advanced / Analyze (javascript-typescript) (push) Waiting to run
CodeQL Advanced / Analyze (rust) (push) Waiting to run
Publish Docker image / build (linux/amd64, ubuntu-latest) (push) Waiting to run
Publish Docker image / build (linux/arm64, ubuntu-24.04-arm) (push) Waiting to run
Publish Docker image / merge (push) Blocked by required conditions
Build with Nix / tests (push) Waiting to run
PR checks / build_web_app (push) Waiting to run
PR checks / test_web_app (1) (push) Waiting to run
PR checks / nix_flake_check (push) Waiting to run
PR checks / rust_lint (push) Waiting to run
PR checks / koplugin_rust_lint (push) Waiting to run
PR checks / test_web_app (2) (push) Waiting to run
PR checks / test_web_browser (push) Waiting to run
PR checks / test_extensions (push) Waiting to run
PR checks / build_tauri_app (push) Waiting to run
Scorecard supply-chain security / Scorecard analysis (push) Waiting to run
Deploy to vercel on merge / build_and_deploy (push) Waiting to run
Some checks are pending
CodeQL Advanced / Analyze (actions) (push) Waiting to run
CodeQL Advanced / Analyze (javascript-typescript) (push) Waiting to run
CodeQL Advanced / Analyze (rust) (push) Waiting to run
Publish Docker image / build (linux/amd64, ubuntu-latest) (push) Waiting to run
Publish Docker image / build (linux/arm64, ubuntu-24.04-arm) (push) Waiting to run
Publish Docker image / merge (push) Blocked by required conditions
Build with Nix / tests (push) Waiting to run
PR checks / build_web_app (push) Waiting to run
PR checks / test_web_app (1) (push) Waiting to run
PR checks / nix_flake_check (push) Waiting to run
PR checks / rust_lint (push) Waiting to run
PR checks / koplugin_rust_lint (push) Waiting to run
PR checks / test_web_app (2) (push) Waiting to run
PR checks / test_web_browser (push) Waiting to run
PR checks / test_extensions (push) Waiting to run
PR checks / build_tauri_app (push) Waiting to run
Scorecard supply-chain security / Scorecard analysis (push) Waiting to run
Deploy to vercel on merge / build_and_deploy (push) Waiting to run
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 <noreply@anthropic.com>
This commit is contained in:
parent
ba8cc03573
commit
e9490ee4a9
3 changed files with 114 additions and 19 deletions
|
|
@ -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 =
|
|||
'<p id="inline" style="font-family: "Inline Serif", serif">e</p>' +
|
||||
'<p id="apostrophe" style="font-family: "O\'Neill", serif">h</p>' +
|
||||
'<p id="inline-font" style=\'font: bold 1em "Inline Sans", sans-serif\'>f</p>' +
|
||||
'<p id="plain">g</p>';
|
||||
'<p id="plain">g</p><p id="quoted">i</p><p id="commented">j</p>' +
|
||||
'<p id="mixed">k</p><p id="mixed-short">l</p>' +
|
||||
'<p id="commented-inline" style="font-family: PublisherSerif, /* fallback */ serif">m</p>';
|
||||
|
||||
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'] });
|
||||
|
|
|
|||
|
|
@ -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)))',
|
||||
);
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -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<string>();
|
||||
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)')
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue