From f8a9f1f07a1e1167a42bc5fb64c766be0529583d Mon Sep 17 00:00:00 2001 From: martin brook Date: Mon, 23 Feb 2026 20:17:37 +0000 Subject: [PATCH] fix: image centering bleed (#1096) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary * Fixes #1026 * Added a reproducer chapter to the epub generator for this case * The fix is in endElement — when leaving a block/header element that had an empty text block, reset the alignment to the user's default so it doesn't bleed into the next sibling. This preserves accumulated margins from parent elements while preventing stale alignment from carrying across. ## Additional Context ### Before fix ![20260222_210029262](https://github.com/user-attachments/assets/263e4608-18cf-418b-871a-1c9a71822bdf) ![20260222_210040995](https://github.com/user-attachments/assets/9f0fdea1-5abf-4f1c-b35d-d35c8309456a) ![20260222_210052640](https://github.com/user-attachments/assets/b77dbadc-f347-400b-994a-17d0f5f073d8) ### After fix ![20260222_211037007](https://github.com/user-attachments/assets/294e15b3-ee40-4c21-8f5b-bd6b40d43d8d) ![20260222_211045139](https://github.com/user-attachments/assets/74107cf9-08a2-4737-be7f-ed0b5648ca6f) --- ### AI Usage While CrossPoint doesn't have restrictions on AI tools in contributing, please be transparent about their usage as it helps set the right context for reviewers. Did you use AI tools to help write this code? _**< PARTIALLY **_ --- .../Epub/parsers/ChapterHtmlSlimParser.cpp | 14 +++++++++++ scripts/generate_test_epub.py | 22 ++++++++++++++++++ test/epubs/test_jpeg_images.epub | Bin 322106 -> 322942 bytes test/epubs/test_png_images.epub | Bin 97163 -> 97996 bytes 4 files changed, 36 insertions(+) diff --git a/lib/Epub/Epub/parsers/ChapterHtmlSlimParser.cpp b/lib/Epub/Epub/parsers/ChapterHtmlSlimParser.cpp index 90bf8fee..d502933d 100644 --- a/lib/Epub/Epub/parsers/ChapterHtmlSlimParser.cpp +++ b/lib/Epub/Epub/parsers/ChapterHtmlSlimParser.cpp @@ -789,6 +789,20 @@ void XMLCALL ChapterHtmlSlimParser::endElement(void* userData, const XML_Char* n if (headerOrBlockTag) { self->currentCssStyle.reset(); self->updateEffectiveInlineStyle(); + + // Reset alignment on empty text blocks to prevent stale alignment from bleeding + // into the next sibling element. This fixes issue #1026 where an empty

(default + // Center) followed by an image-only

causes Center to persist through the chain + // of empty block reuse into subsequent text paragraphs. + // Margins/padding are preserved so parent element spacing still accumulates correctly. + if (self->currentTextBlock && self->currentTextBlock->isEmpty()) { + auto style = self->currentTextBlock->getBlockStyle(); + style.textAlignDefined = false; + style.alignment = (self->paragraphAlignment == static_cast(CssTextAlign::None)) + ? CssTextAlign::Justify + : static_cast(self->paragraphAlignment); + self->currentTextBlock->setBlockStyle(style); + } } } diff --git a/scripts/generate_test_epub.py b/scripts/generate_test_epub.py index 0d465e10..e585420c 100644 --- a/scripts/generate_test_epub.py +++ b/scripts/generate_test_epub.py @@ -607,6 +607,17 @@ def main(): Cache test 2

Navigate back to Page A - it should load faster from cache.

"""), [('cache_test_2.jpg', images['cache_test_2.jpg'])]), + ("9. Alignment Bleed", make_chapter("Image Centering Bleed Test", """ +

Tests that image centering does not bleed into following text blocks (issue #1026).

+

Set Paragraph Alignment to Justify and Embedded Style to OFF before testing.

+

All paragraphs below the images should be justified, not centered.

+

+

Test image

+
+

FIRST PARAGRAPH after image. This paragraph follows an empty heading and an image-only paragraph. With the bug present, this text appears centered instead of justified because the empty heading's default Center alignment bleeds through the chain of empty text blocks. Lorem ipsum dolor sit amet, consectetur adipiscing elit sed do eiusmod tempor.

+

SECOND PARAGRAPH in the same div. This paragraph should always be justified because the first paragraph's text block was flushed. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident sunt in culpa qui officia.

+
+"""), []), # centering_test.jpg already included by chapter 4 ] create_epub(OUTPUT_DIR / 'test_jpeg_images.epub', 'JPEG Image Tests', jpeg_chapters) @@ -661,6 +672,17 @@ def main(): Cache test 2

Navigate back to Page A - it should load faster from cache.

"""), [('cache_test_2.png', images['cache_test_2.png'])]), + ("9. Alignment Bleed", make_chapter("Image Centering Bleed Test", """ +

Tests that image centering does not bleed into following text blocks (issue #1026).

+

Set Paragraph Alignment to Justify and Embedded Style to OFF before testing.

+

All paragraphs below the images should be justified, not centered.

+

+

Test image

+
+

FIRST PARAGRAPH after image. This paragraph follows an empty heading and an image-only paragraph. With the bug present, this text appears centered instead of justified because the empty heading's default Center alignment bleeds through the chain of empty text blocks. Lorem ipsum dolor sit amet, consectetur adipiscing elit sed do eiusmod tempor.

+

SECOND PARAGRAPH in the same div. This paragraph should always be justified because the first paragraph's text block was flushed. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident sunt in culpa qui officia.

+
+"""), []), # centering_test.png already included by chapter 4 ] create_epub(OUTPUT_DIR / 'test_png_images.epub', 'PNG Image Tests', png_chapters) diff --git a/test/epubs/test_jpeg_images.epub b/test/epubs/test_jpeg_images.epub index 3f2b1f77606fce58d111ee9bf2f61a737b0df6f1..a99251f06497a11a11e9b80dd92da6d661b89b43 100644 GIT binary patch delta 2304 zcmZ`)dpy(YAOCLqS~-$SbBPm&lO4B^h=^*;LPIRq)p5xs%ve&2CdAKuLL+7&hg=&W zbs}{tR5NsvkxMR{(Bx9+;@Cp}^n1R4JfG+Nd4E2i*Z1{$KhOK_%WdD-+eGcGVITtF zFM|@tbWsBsf5Qy$w^~(*14Iy9gl6fBt|EBEDKYB1Rjv!Qa}Y#i=ox+%ue3)>Y{PqIZCnL!;rt854RH ztc)6;yp-A6Ei9a)dUtXyFKM#-Q}1K)XlX>qnG21J`{;+{jb3o(Vg3God38#6#L$cr z)pHAd{mXyJ(&%)VTwF%6D}rPXm(sHdGV1&dUntDYftvc83ya*alQu)U%r9WZy4nfv z6uvej(9=?_3@rbMW+eh;+SMP&x|A<5AXg*lx-BR|UH#;|TyVyTz=hnQEM;=yW^Ifw zao|4l*+Z=4ya_y8L|aZ9FXB)s*S*_N=Q;K8p?}@PEgn|56=og@R;-uno}6CL*JRY^ z+iIrue#~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v$di{o&?W~31e1i1qHzfoB5u8=?2mS-cpX~Ag delta 1415 zcmex&OL*56Vcr05W)=|!5LmIuVFa|S%8FfrkO?iPLF(R4#(_aDA zMozzYpG9Q)-WM$FldUbjfE5{8-i-k(Dsh|n|2j~S4p`A-gK00o^3v0-6G4o{kv|}& z#{5)@0|`uT3}^fgVzf6$Fm7*-V4Bec;%%3^&7=fY+3tLYX}j|s=BK$}acwUaGqCvf z6mJ%7Pq3Ya(|h)R0NR-VwsZT2DJ+Z-C0nMl2zY_D9ly__#K;FUIOgyyeudAB3=HQv zrr&(bqQv5MoA1Q*|BqS3>RWqq4|HZOw0n}*@zLouNSW~Zkz51dgp`BDSpPvbB%Y>Z;!T-Z zozbh3uYBV;maHQ`smeAepD9!6WVGZ&o^M}w*RgNQmN;tt{^PmjAA^=ner%|9qCiATxnf+PmzExhc zPP4}K+^s(_ubr`V``IK`@8teNTNl>)SBr#&#hi9|vMH8VrsBr_gCP%>Z!2_u&nEIL zW64qbyKH?nGC@BMXhv_^n6AtfuQm1mM&bP97OOpsIHM!2DzcKD^VQ40{k&In&85k> zM@X&P^x~ltR}B9%26!_vi7>-Um+9TlS&Tthar(jMERJ9X>kAfpFvI-?i#wPx5hil^ z1q(RePFHvdRg?6R#S^S%9ZclIOQ;&#S5P(euUI_5YEHpKcwV#kf<I(2?WdkV?1;UlA L3=GvzSb#hLO9)=S diff --git a/test/epubs/test_png_images.epub b/test/epubs/test_png_images.epub index 3efdd0b2e99bfca2c661d0d9a1731b0962f801b4..5a405f8820c3b7323c7fbf7328eef2b40b81d621 100644 GIT binary patch delta 2228 zcmZvdc{G%5AII<8VCZEUB1^I_QCX7FM9D51rj!OV7z{Gj!HiKF+mrRRWG9506p<9N zx6$IkM9G>dzBO3*S*dsjJARBiUFLZ zf5Ra|SRcaKEXO%p-5&51$Q6-1FGRS)T1@#T2^L4laLFwl{-31oK)aOi#$Q;s)UbiR zje1I%OmDHw%o9wmkAlKUb42E#H=7II`oKtB@z=28&kC^786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