Skip to content

Preserve replaced box height on relayout#2767

Draft
taxilian wants to merge 1 commit into
Kozea:mainfrom
taxilian:codex/preserve-replaced-height
Draft

Preserve replaced box height on relayout#2767
taxilian wants to merge 1 commit into
Kozea:mainfrom
taxilian:codex/preserve-replaced-height

Conversation

@taxilian

@taxilian taxilian commented May 5, 2026

Copy link
Copy Markdown

What changed

resolve_percentages now preserves an already computed numeric height for block replaced boxes when the containing block height is auto.

This prevents a relayout pass from changing a floated replaced box, such as an image, back to height = 'auto' after replaced-box layout has computed its used height.

Why

In documents with floated images and page remakes, a block replaced box can be laid out again after its height has already been resolved from its intrinsic ratio. Resetting that height to the string auto can leave the box in float exclusion data with a non-numeric height. Later calls to margin_height() then try to add that string to numeric padding or border values and raise a TypeError.

Tests

  • python -m pytest tests/layout/test_image.py::test_images_auto_height_block_replaced_relayout -q
  • python -m pytest tests/layout/test_image.py tests/layout/test_float.py -q
  • python -m ruff check weasyprint/layout/percent.py tests/layout/test_image.py

Background

I hit this when trying to format a book I use Weasyprint with (hambook.org) for a kindle sized screen. Due to time constraints, the fix, repro case, and PR were all figured out by various AI tools, but as best I can tell it seems reasonable. Could be you know a better fix, but at least the test I think shows the failure; I can upload another repro case if needed.

@taxilian
taxilian marked this pull request as ready for review May 5, 2026 21:52
@grewn0uille

Copy link
Copy Markdown
Member

Hi,

Thanks for your PR. Could you please share a sample you’re sure that it shows the issue?

You think the test shows the failure, so we can’t be sure that the fix is actually fixing the "right" thing. And it’d be better for the project quality to know why we’re adding lines of code :)

@taxilian

Copy link
Copy Markdown
Author

I had one, may take me a bit to track down where it was. I was formating a book for a small device screen - 3.75 x 4.25 inches, IIRC. I should be able to locate it.

@taxilian

Copy link
Copy Markdown
Author

@liZe

liZe commented Jul 12, 2026

Copy link
Copy Markdown
Member

@taxilian Thanks for your example, but it’s huge and I can’t reproduce the bug with it. The test included explicitly calls resolve_percentages after the layout, it’s not a good example that shows the bug with a normal rendering.

Could you please provide a smaller sample? I tried to find a way to reproduce with the description in your first comment, but I didn’t find a way so far.

@liZe
liZe marked this pull request as draft July 12, 2026 14:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants