fix(layout): clip nowrapped lines along the line only, keeping footnote icons whole (#6801)

* fix(layout): clip nowrapped lines along the line only, keeping footnote icons whole

Readest adds `overflow: clip !important` to any book rule with
`white-space: nowrap`, so a long unwrapped line can't spill past the column
(#2663). `overflow: clip` clips both axes, so whatever rose above the line
box was cut too: a footnote icon in a <sup> with `vertical-align: top` on a
paragraph's first line showed only a sliver. The clip is now on the inline
axis only (overflow-x, or overflow-y in vertical writing); the other axis
stays visible. Closes #6797.

Reported by laraon on GitHub.

Verified: the two new style.test.ts cases fail on the old code (no
overflow-x/overflow-y clip) and pass now. A repro EPUB built from the
issue's HTML and CSS (the attached zip returns 404) on dev-web in
Chromium: before, the first-line icons' top edge hit-tested to <body>
(clipped); after, every icon hits its link, and a 2976px nowrapped line in
a 432px paragraph is still clipped at the paragraph edge. The utils and
transformer test files (176), the three layout browser tests that run
transformStylesheet, and pnpm lint pass.
Not run: the reporter's own EPUB, Windows WebView2 and Android, vertical
writing in a real book.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(layout): keep the two-axis clip for nowrapped rules that size their box

Narrows the #6797 fix. A white-space: nowrap rule that sets height,
max-height or a line clamp may rely on its box clipping the content, so it
keeps overflow: clip on both axes; only unsized nowrapped elements get the
line-axis clip. line-height and min-height don't count as sizing, so the
reporter's .adapt-poem-c rule still lets the footnote icon show.

Verified: the new "keeps clipping both axes" test fails on the previous
commit and passes now; the reporter's exact rule (line-height 1.7) still
gets overflow-x only; style.test.ts 103/103, the utils and document test
files (204) and pnpm lint 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:
Huang Xin 2026-10-14 23:30:49 +08:00 • committed by GitHub
parent d78dd97d50
commit e58e324ea9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 47 additions and 6 deletions

View file

@ -37,16 +37,51 @@ describe('transformStylesheet', () => {
});
describe('white-space nowrap', () => {
it('adds overflow clip !important when no overflow is set', () => {
// A nowrapped line must not spill past the column into the margin or the
// next page, so it is clipped along the line. Clipping the other axis too
// cut off whatever rises above or hangs below the line box, such as a
// top-aligned footnote icon in a <sup> on the first line (#6797).
it('clips a nowrapped element along the line only', () => {
const css = '.nowrap { white-space: nowrap; }';
const result = transformStylesheet(css, VW, VH, VERTICAL);
expect(result).toContain('overflow: clip !important');
expect(result).toContain('overflow-x: clip !important');
expect(result).not.toContain('overflow-y: clip');
expect(result).not.toMatch(/[^-]overflow: clip/);
});
it('does not add overflow clip when overflow is already set', () => {
it('clips a nowrapped element vertically in vertical writing', () => {
const css = '.nowrap { white-space: nowrap; }';
const result = transformStylesheet(css, VW, VH, true);
expect(result).toContain('overflow-y: clip !important');
expect(result).not.toContain('overflow-x: clip');
});
// A nowrapped rule that sizes its own box (height, max-height, line clamp)
// may rely on the box clipping its content, so it keeps the old clip on
// both axes; only unsized nowrapped elements let content rise past the line.
it('keeps clipping both axes when the rule sets its own height', () => {
for (const sizing of ['height: 2em', 'max-height: 3em', '-webkit-line-clamp: 2']) {
const css = `.box { white-space: nowrap; ${sizing}; }`;
const result = transformStylesheet(css, VW, VH, VERTICAL);
expect(result).toContain('overflow: clip !important');
expect(result).not.toContain('overflow-x: clip');
}
});
it('treats line-height and min-height as unsized (the #6797 rule)', () => {
const css =
'.adapt-poem-c { text-indent: 0; text-align: center; font-size: 1.0em; ' +
'margin: 0.85em auto; word-break: break-all; white-space: nowrap; ' +
'letter-spacing: normal; line-height: 1.7; min-height: 1em; }';
const result = transformStylesheet(css, VW, VH, VERTICAL);
expect(result).toContain('overflow-x: clip !important');
expect(result).not.toMatch(/[^-]overflow: clip/);
});
it('does not add a clip when overflow is already set', () => {
const css = '.nowrap { white-space: nowrap; overflow: hidden; }';
const result = transformStylesheet(css, VW, VH, VERTICAL);
expect(result).not.toContain('overflow: clip !important');
expect(result).not.toContain('clip !important');
});
});

View file

@ -1252,12 +1252,18 @@ export const transformStylesheet = (
return match;
});
// clip nowrapped elements
// Clip nowrapped elements along the line, so a long line can't spill past
// the column. The other axis stays visible: clipping it cut off whatever
// rises above the line box, like a top-aligned footnote icon (#6797). A rule
// that sizes its own box keeps the clip on both axes.
const lineClip = vertical ? 'overflow-y: clip !important;' : 'overflow-x: clip !important;';
css = css.replace(ruleRegex, (match, selector, block) => {
const hasWhiteSpaceNowrap = /white-space\s*:\s*nowrap\s*[;$]/.test(block);
if (hasWhiteSpaceNowrap) {
if (!/overflow\s*:/.test(block)) {
block = block.replace(/}$/, ' overflow: clip !important; }');
const sizesBox = /(?:^|[;{\s])(?:max-)?height\s*:|line-clamp\s*:/.test(block);
const clip = sizesBox ? 'overflow: clip !important;' : lineClip;
block = block.replace(/}$/, ` ${clip} }`);
}
return selector + block;
}