mirror of
https://github.com/readest/readest.git
synced 2026-11-30 03:13:39 +00:00
fix(layout): keep a percentage-width image next to text at its size (#7072)
* fix(layout): keep a percentage-width image next to text at its size An image that shares its parent with text is taken for a glyph standing in for a character and clamped to 1em (#1555). A chapter title image the book sizes as a share of the line, `<h1><img class="p"/>收音机</h1>` with `.p { width: 30%; height: auto }`, got the same clamp and shrank to an icon. A glyph image is never sized in percent, so an image whose computed width is a percentage is left out of the inline-image class. computedStyleMap keeps the percentage that getComputedStyle resolves to pixels; where it is missing (Firefox) the behaviour is unchanged. Verification: - inline-image-style.browser.test.ts fails without the fix under default, Override and Use Book Layout: "expected 20 to be close to 180"; passes with it, and a glyph image next to text is still 1em. - Reporter's EPUB in dev-web (Chromium): the 收音机 title image went from 129x22.7 px to 129x128.8 px, matching Sigil. - Colour modes, main vs this branch in dev-web, Sepia theme: the reporter's title image has the same filter and blend in dark, dark + Override Book Color and dark + Invert Image In Dark Mode; only its size changes. A 30%-wide white-background PNG in a paragraph with text loses the inline-image blend (screen/multiply): in light and dark without Override Book Color it showed a white background on main too (now at full size, not squashed to 129x16); in dark + Override Book Color it now takes the blend and filter every other picture gets. - style-dom, style, style-get-styles and paginator-duokan-cover tests pass; pnpm lint passes. - Not run: Windows/WebView2 build, CEF build, Firefox web, iOS/Android, e-ink. Reported by DGZYZLH on GitHub. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(layout): treat a calc() percentage width as percentage-sized too computedStyleMap returns a CSSMathSum for `width: calc(30% - 1px)`, not a CSSUnitValue with unit 'percent', so such a title image beside text was still clamped to 1em. Check the serialized value for '%' instead. Verified: new inline-image-style.browser test "a title image with a calc() percentage width keeps its size" fails without the change (expected 20 to be close to 179) and passes with it; the other cases in the file, paginator-duokan-cover.browser (10) and the style unit suites (284) pass; pnpm lint and pnpm format:check pass. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
dbf96ba7b2
commit
1d00df6ed4
2 changed files with 117 additions and 1 deletions
|
|
@ -0,0 +1,110 @@
|
|||
import { describe, test, expect, afterEach } from 'vitest';
|
||||
|
||||
import { applyImageStyle, getStyles, ThemeCode } from '@/utils/style';
|
||||
import { ViewSettings } from '@/types/book';
|
||||
import {
|
||||
DEFAULT_BOOK_FONT,
|
||||
DEFAULT_BOOK_LAYOUT,
|
||||
DEFAULT_BOOK_LANGUAGE,
|
||||
DEFAULT_BOOK_STYLE,
|
||||
DEFAULT_VIEW_CONFIG,
|
||||
DEFAULT_TTS_CONFIG,
|
||||
DEFAULT_TRANSLATOR_CONFIG,
|
||||
DEFAULT_ANNOTATOR_CONFIG,
|
||||
DEFAULT_SCREEN_CONFIG,
|
||||
} from '@/services/constants';
|
||||
|
||||
// An image next to text is taken for a glyph standing in for a character and
|
||||
// clamped to 1em. A title image the book sizes as a share of the line, like
|
||||
// `<h1><img class="p"/>收音机</h1>` with `.p { width: 30% }`, was clamped too
|
||||
// and shrank to an icon (#7039).
|
||||
const settings = {
|
||||
...DEFAULT_BOOK_FONT,
|
||||
...DEFAULT_BOOK_LAYOUT,
|
||||
...DEFAULT_BOOK_LANGUAGE,
|
||||
...DEFAULT_BOOK_STYLE,
|
||||
...DEFAULT_VIEW_CONFIG,
|
||||
...DEFAULT_TTS_CONFIG,
|
||||
...DEFAULT_TRANSLATOR_CONFIG,
|
||||
...DEFAULT_ANNOTATOR_CONFIG,
|
||||
...DEFAULT_SCREEN_CONFIG,
|
||||
} as ViewSettings;
|
||||
|
||||
const themeCode = {
|
||||
bg: '#ffffff',
|
||||
fg: '#000000',
|
||||
primary: '#3366cc',
|
||||
isDarkMode: false,
|
||||
palette: {
|
||||
'base-100': '#ffffff',
|
||||
'base-200': '#f0f0f0',
|
||||
'base-300': '#e0e0e0',
|
||||
'base-content': '#000000',
|
||||
neutral: '#808080',
|
||||
'neutral-content': '#ffffff',
|
||||
primary: '#3366cc',
|
||||
secondary: '#6699cc',
|
||||
accent: '#33cc99',
|
||||
},
|
||||
} as ThemeCode;
|
||||
|
||||
const SQUARE = `data:image/svg+xml,${encodeURIComponent(
|
||||
'<svg xmlns="http://www.w3.org/2000/svg" width="200" height="200"><rect width="200" height="200"/></svg>',
|
||||
)}`;
|
||||
|
||||
const added: Element[] = [];
|
||||
|
||||
// The book's CSS first, then the reader's, as the paginator injects them
|
||||
const render = async (bookCss: string, html: string, layout: Partial<ViewSettings> = {}) => {
|
||||
for (const css of [bookCss, getStyles({ ...settings, ...layout }, themeCode)]) {
|
||||
const style = document.createElement('style');
|
||||
style.textContent = css;
|
||||
document.head.appendChild(style);
|
||||
added.push(style);
|
||||
}
|
||||
const container = document.createElement('div');
|
||||
container.style.width = '600px';
|
||||
container.innerHTML = html;
|
||||
document.body.appendChild(container);
|
||||
added.push(container);
|
||||
const img = container.querySelector('img')!;
|
||||
await img.decode();
|
||||
applyImageStyle(document);
|
||||
return img;
|
||||
};
|
||||
|
||||
describe('images inline with text', () => {
|
||||
afterEach(() => {
|
||||
for (const el of added.splice(0)) el.remove();
|
||||
});
|
||||
|
||||
test.each([
|
||||
['default layout', {}],
|
||||
['Override Book Layout', { overrideLayout: true }],
|
||||
['Use Book Layout', { useBookLayout: true }],
|
||||
])('a title image with a percentage width keeps its size under %s (#7039)', async (_, layout) => {
|
||||
const img = await render(
|
||||
'.p { width: 30%; height: auto; }',
|
||||
`<h1 style="font-size: 20px"><img class="p" src="${SQUARE}"/>收音机</h1>`,
|
||||
layout,
|
||||
);
|
||||
|
||||
expect(img.getBoundingClientRect().height).toBeCloseTo(180, 0);
|
||||
});
|
||||
|
||||
test('a title image with a calc() percentage width keeps its size', async () => {
|
||||
const img = await render(
|
||||
'.p { width: calc(30% - 1px); height: auto; }',
|
||||
`<h1 style="font-size: 20px"><img class="p" src="${SQUARE}"/>收音机</h1>`,
|
||||
);
|
||||
|
||||
expect(img.getBoundingClientRect().height).toBeCloseTo(179, 0);
|
||||
});
|
||||
|
||||
test('a glyph image next to text is still clamped to 1em', async () => {
|
||||
const img = await render('', `<p style="font-size: 20px">生<img src="${SQUARE}"/>字</p>`);
|
||||
|
||||
expect(img.classList.contains('has-text-siblings')).toBe(true);
|
||||
expect(img.getBoundingClientRect().height).toBeCloseTo(20, 0);
|
||||
});
|
||||
});
|
||||
|
|
@ -1733,7 +1733,13 @@ export const applyImageStyle = (document: Document) => {
|
|||
const isInline = childNodes.every(
|
||||
(node) => node.nodeType !== Node.ELEMENT_NODE || (node as Element).tagName !== 'BR',
|
||||
);
|
||||
inlineWithText = hasTextSiblings && isInline;
|
||||
// A glyph image standing in for a character is never sized as a share
|
||||
// of the line; a title image the book sizes that way keeps it (#7039).
|
||||
// computedStyleMap keeps the percentage that getComputedStyle resolves,
|
||||
// also inside a calc() (serialized as e.g. `calc(30% - 1px)`).
|
||||
const width = img.computedStyleMap?.().get('width');
|
||||
const percentSized = !!width && width.toString().includes('%');
|
||||
inlineWithText = hasTextSiblings && isInline && !percentSized;
|
||||
if (inlineWithText) {
|
||||
// Only supply Readest's baseline default when the book leaves
|
||||
// vertical-align at its initial value; an author-set value (e.g. a CJK
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue