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 3f2b1f77..a99251f0 100644 Binary files a/test/epubs/test_jpeg_images.epub and b/test/epubs/test_jpeg_images.epub differ diff --git a/test/epubs/test_png_images.epub b/test/epubs/test_png_images.epub index 3efdd0b2..5a405f88 100644 Binary files a/test/epubs/test_png_images.epub and b/test/epubs/test_png_images.epub differ