diff --git a/apps/readest-app/src/__tests__/utils/inline-image-style.browser.test.ts b/apps/readest-app/src/__tests__/utils/inline-image-style.browser.test.ts new file mode 100644 index 000000000..e8fa171b1 --- /dev/null +++ b/apps/readest-app/src/__tests__/utils/inline-image-style.browser.test.ts @@ -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 +// `

收音机

` 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( + '', +)}`; + +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 = {}) => { + 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; }', + `

收音机

`, + 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; }', + `

收音机

`, + ); + + expect(img.getBoundingClientRect().height).toBeCloseTo(179, 0); + }); + + test('a glyph image next to text is still clamped to 1em', async () => { + const img = await render('', `

生字

`); + + expect(img.classList.contains('has-text-siblings')).toBe(true); + expect(img.getBoundingClientRect().height).toBeCloseTo(20, 0); + }); +}); diff --git a/apps/readest-app/src/utils/style.ts b/apps/readest-app/src/utils/style.ts index 5f40c7cc8..18e4f3dee 100644 --- a/apps/readest-app/src/utils/style.ts +++ b/apps/readest-app/src/utils/style.ts @@ -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