mirror of
https://github.com/readest/readest.git
synced 2026-11-29 19:02:28 +00:00
fix(layout): keep the indent on paragraphs with a footnote icon (#6828)
* fix(layout): keep the indent on paragraphs with a footnote icon The indent exemption for image-only paragraphs used selectors like p:has(> a:only-child > img:only-child). :only-child ignores text nodes, so a body paragraph holding a footnote icon (<a class="duokan-footnote"><img> </a>, common in Chinese EPUBs) matched too and lost its indent. The linked variants came in with #6281 (v0.12.10); the older span variant had the same flaw for <p>text<span><img></span></p>. applyImageStyle now marks a paragraph whose only content is images (class image-only, no text) and the rule uses that class in place of the four :only-child selectors. Verified: - New test in style-dom.test.ts runs the rule's selectors after applyImageStyle. On the old code it fails with the footnote, footnote-end and span-icon paragraphs in the exempted list; with the fix only the figure, linked and wrapped image paragraphs are exempt. - Reporter's book on the dev server (web, Chrome): 24 footnote paragraphs in chapter 1 went from 0px to 32px indent, same as plain paragraphs. The book has no image-only paragraph, so the exemption was checked by adding one to the rendered chapter: 0px. - pnpm test (13641 passed), pnpm lint clean. Not run: other platforms (CSS-only change in a shared path). Reported by @sungt on X. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(layout): exempt only paragraphs that start with an image from the indent Review follow-up on #6828. The two selectors kept from #1568 ignore text too: `p:has(> img:not(.has-text-siblings))` matched `Text<br><img>` and `p:has(> a:first-child + img:last-child)` matched `Text<a></a><img>`, dropping the indent from text. The image-only class from the first commit also stopped exempting `<p><img>Text</p>` (a drop cap), which `img:only-child` used to cover. The indent only moves the first line, so the class now marks a paragraph with no text before its first image (`leading-image`) and is the rule's only selector. Verified: the selector test now also covers a captioned figure, a drop cap, `Text<br><img>` and `Text<a></a><img>`. Against main's style.ts it fails with footnote, footnote-end, icon, text-break and text-anchor exempted; against the first commit it fails with dropcap missing and text-break and text-anchor exempted; with this change exactly figure, linked, wrapped, captioned and dropcap are exempt. Reporter's book on the dev server: the 24 footnote paragraphs keep the 32px indent. pnpm test (13641 passed) and pnpm lint clean. 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
e82f0c0f44
commit
19d6c70c39
3 changed files with 43 additions and 24 deletions
|
|
@ -604,6 +604,33 @@ describe('applyImageStyle', () => {
|
|||
expect(img.style.pointerEvents).toBe('');
|
||||
});
|
||||
});
|
||||
|
||||
// :only-child ignores text nodes, so a selector like
|
||||
// p:has(> a:only-child > img:only-child) also matched a body paragraph with
|
||||
// a footnote icon (<a class="duokan-footnote"><img></a>) and dropped its
|
||||
// indent.
|
||||
it('exempts paragraphs that start with an image from the indent, not text paragraphs with an inline image', () => {
|
||||
document.body.innerHTML = `
|
||||
<p id="figure"><img src="a.png"/></p>
|
||||
<p id="linked"><a href="#"><img src="a.png"/></a></p>
|
||||
<p id="wrapped"><span><a href="#"><img src="a.png"/></a></span></p>
|
||||
<p id="captioned"><a id="anchor"></a><img src="a.png"/><br/>Caption</p>
|
||||
<p id="dropcap"><img src="d.png"/>ext</p>
|
||||
<p id="footnote">Text<a class="duokan-footnote" href="#"><img src="n.png"/></a>more text</p>
|
||||
<p id="footnote-end">Text<a class="duokan-footnote" href="#"><img src="n.png"/></a></p>
|
||||
<p id="icon">Text<span><img src="i.png"/></span></p>
|
||||
<p id="text-break">Text<br/><img src="a.png"/></p>
|
||||
<p id="text-anchor">Text<a id="anchor2"></a><img src="a.png"/></p>
|
||||
`;
|
||||
applyImageStyle(document);
|
||||
|
||||
const css = getStyles(makeViewSettings({ textIndent: 2 }));
|
||||
const block = css.split('}').find((b) => b.includes('(#6198)'))!;
|
||||
const selector = block.slice(block.lastIndexOf('*/') + 2, block.indexOf('{'));
|
||||
const exempt = Array.from(document.querySelectorAll(selector)).map((p) => p.id);
|
||||
|
||||
expect(exempt).toEqual(['figure', 'linked', 'wrapped', 'captioned', 'dropcap']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('applyLinkHitArea', () => {
|
||||
|
|
|
|||
|
|
@ -1147,19 +1147,3 @@ describe('link hit area in link-dense sections', () => {
|
|||
expect(css).not.toMatch(/(^|\n)\s*a\s*\{\s*position: relative !important;/);
|
||||
});
|
||||
});
|
||||
|
||||
describe('paragraph indent exemption for image-only paragraphs', () => {
|
||||
// A full-width inline image that takes the paragraph indent overhangs the
|
||||
// column by the indent and paints a strip on the next page (#6198). The
|
||||
// exemption must also see an image wrapped in a link, which is how Wikipedia
|
||||
// (and most sites) mark up a figure: <p><span><a><img></a></span></p>.
|
||||
it('drops the indent for an image wrapped in a link, with or without a span', () => {
|
||||
const css = getStyles(makeViewSettings({ textIndent: 2 }));
|
||||
const rule = css
|
||||
.split('}')
|
||||
.find((block) => block.includes('text-indent: initial !important') && block.includes('img'));
|
||||
expect(rule).toBeDefined();
|
||||
expect(rule).toContain('p:has(> a:only-child > img:only-child)');
|
||||
expect(rule).toContain('p:has(> span:only-child > a:only-child > img:only-child)');
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -753,14 +753,11 @@ const getParagraphLayoutStyles = (
|
|||
dd.aligned-justify, div.aligned-justify {
|
||||
${!justify && overrideLayout ? 'text-align: initial !important;' : ''};
|
||||
}
|
||||
/* An image that is the paragraph's whole content must not take the indent:
|
||||
sized to the column, it would overhang by the indent and paint a strip on
|
||||
the next page (#6198). Linked images (<a><img>, <span><a><img>) included. */
|
||||
p:has(> img:only-child), p:has(> span:only-child > img:only-child),
|
||||
p:has(> a:only-child > img:only-child),
|
||||
p:has(> span:only-child > a:only-child > img:only-child),
|
||||
p:has(> img:not(.has-text-siblings)),
|
||||
p:has(> a:first-child + img:last-child) {
|
||||
/* An image that starts a paragraph must not take the indent: sized to the
|
||||
column, it would overhang by the indent and paint a strip on the next page
|
||||
(#6198). applyImageStyle marks such paragraphs; selectors can't see text,
|
||||
so :only-child also matched a footnote icon inside text. */
|
||||
p.leading-image {
|
||||
text-indent: initial !important;
|
||||
}
|
||||
blockquote[align="center"], div[align="center"],
|
||||
|
|
@ -1689,6 +1686,14 @@ export const applyImageStyle = (document: Document) => {
|
|||
? parseFloat(heightAttr)
|
||||
: NaN;
|
||||
const noEnlarge = img.getAttribute('zy-enlarge-src') === 'none';
|
||||
const paragraph = img.closest('p');
|
||||
let leadingImageParagraph: HTMLParagraphElement | null = null;
|
||||
if (paragraph) {
|
||||
const before = document.createRange();
|
||||
before.setStart(paragraph, 0);
|
||||
before.setEndBefore(img);
|
||||
if (!before.toString().trim()) leadingImageParagraph = paragraph;
|
||||
}
|
||||
|
||||
let inlineWithText = false;
|
||||
let keepBaseline = false;
|
||||
|
|
@ -1718,6 +1723,7 @@ export const applyImageStyle = (document: Document) => {
|
|||
inlineWithText,
|
||||
keepBaseline,
|
||||
noEnlarge,
|
||||
leadingImageParagraph,
|
||||
};
|
||||
});
|
||||
|
||||
|
|
@ -1728,7 +1734,9 @@ export const applyImageStyle = (document: Document) => {
|
|||
inlineWithText,
|
||||
keepBaseline,
|
||||
noEnlarge,
|
||||
leadingImageParagraph,
|
||||
} of plans) {
|
||||
leadingImageParagraph?.classList.add('leading-image');
|
||||
if (!isNaN(percentWidth)) {
|
||||
img.style.width = `${(percentWidth / 100) * window.innerWidth}px`;
|
||||
img.removeAttribute('width');
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue