fix: image centering bleed (#1096)

## 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 **_
This commit is contained in:
martin brook
2026-02-23 20:17:37 +00:00
committed by GitHub
parent 052f497b9e
commit f8a9f1f07a
4 changed files with 36 additions and 0 deletions

View File

@@ -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 <h1> (default
// Center) followed by an image-only <p> 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<uint8_t>(CssTextAlign::None))
? CssTextAlign::Justify
: static_cast<CssTextAlign>(self->paragraphAlignment);
self->currentTextBlock->setBlockStyle(style);
}
}
}

View File

@@ -607,6 +607,17 @@ def main():
<img src="images/cache_test_2.jpg" alt="Cache test 2"/>
<p>Navigate back to Page A - it should load faster from cache.</p>
"""), [('cache_test_2.jpg', images['cache_test_2.jpg'])]),
("9. Alignment Bleed", make_chapter("Image Centering Bleed Test", """
<p>Tests that image centering does not bleed into following text blocks (issue #1026).</p>
<p>Set Paragraph Alignment to Justify and Embedded Style to OFF before testing.</p>
<p>All paragraphs below the images should be justified, not centered.</p>
<h1 class="hidden"></h1>
<p><img src="images/centering_test.jpg" alt="Test image"/></p>
<div>
<p>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.</p>
<p>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.</p>
</div>
"""), []), # 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():
<img src="images/cache_test_2.png" alt="Cache test 2"/>
<p>Navigate back to Page A - it should load faster from cache.</p>
"""), [('cache_test_2.png', images['cache_test_2.png'])]),
("9. Alignment Bleed", make_chapter("Image Centering Bleed Test", """
<p>Tests that image centering does not bleed into following text blocks (issue #1026).</p>
<p>Set Paragraph Alignment to Justify and Embedded Style to OFF before testing.</p>
<p>All paragraphs below the images should be justified, not centered.</p>
<h1 class="hidden"></h1>
<p><img src="images/centering_test.png" alt="Test image"/></p>
<div>
<p>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.</p>
<p>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.</p>
</div>
"""), []), # centering_test.png already included by chapter 4
]
create_epub(OUTPUT_DIR / 'test_png_images.epub', 'PNG Image Tests', png_chapters)

Binary file not shown.

Binary file not shown.