Skip to content

Fix simple grid content widths#2749

Open
moreaki wants to merge 2 commits into
Kozea:mainfrom
moreaki:fix-grid-content-width
Open

Fix simple grid content widths#2749
moreaki wants to merge 2 commits into
Kozea:mainfrom
moreaki:fix-grid-content-width

Conversation

@moreaki
Copy link
Copy Markdown
Contributor

@moreaki moreaki commented Apr 22, 2026

What changed

This adds a small intrinsic-width helper for grid containers used by preferred width calculations.

Before this change, min_content_width() and max_content_width() treated GridContainerBox like a regular block container. That means shrink-to-fit contexts, such as floats and flex/grid items using intrinsic size contributions, could see a multi-column grid as if its intrinsic width were just the width of one child block. In the minimal example discussed in #2354/#2145, a two-column grid containing abc def twice reported a width close to one abc def, causing each grid item to wrap to abc/def.

The new helper handles simple, non-spanning row-flow grid containers by summing per-track intrinsic contributions and gutters. It is deliberately conservative: it improves common grid content width cases without attempting to implement the complete CSS Grid intrinsic sizing algorithm.

Investigation summary

I followed the #2354 link chain and related work before making this patch:

What this fixes

The PR fixes the maintainer’s minimal intrinsic-size example from #2354/#2145:

<div style="float: left">
  <div style="display: grid; grid: 1fr / 1fr 1fr; border: 1px solid red">
    <div>abc def</div>
    <div>abc def</div>
  </div>
</div>

With this patch, the float’s max-content width includes both grid tracks, and the two abc def items no longer wrap as if the grid were one child block wide.

The PR now also includes the exact flex-column/grid sample from the #2354 issue comment as test_grid_container_in_flex_column. On current main, that standalone sample already lays out as a stretched flex item, so this test is included as related reference coverage; the behavior-changing regression remains the shrink-to-fit grid content width case above.

What this does not fix

This is not full CSS Grid intrinsic sizing support. In particular, this PR does not claim to solve:

The intent is to provide a small reviewable base for one narrow part of #2354: simple grid containers contributing a sensible min/max-content width in shrink-to-fit contexts.

Tests

  • venv/bin/python -m ruff check weasyprint/layout/grid.py weasyprint/layout/preferred.py tests/layout/test_grid.py
  • venv/bin/python -m pytest tests/layout/test_grid.py
  • venv/bin/python -m pytest tests/layout/test_preferred.py tests/layout/test_flex.py
  • venv/bin/python -m pytest -> 4027 passed, 40 xfailed

Addresses part of #2354.
Related to #2145, #2644, #2342, #1891, #1665, #1652, #2362, #2628, #2647.

@moreaki
Copy link
Copy Markdown
Contributor Author

moreaki commented Apr 22, 2026

I added the exact flex-column/grid sample from the #2354 issue comment as test_grid_container_in_flex_column in commit 26e28bd.

One note for review: this standalone sample already lays out as a stretched flex item on current main in my local run, so the new test is reference coverage for the related issue comment rather than the behavior-changing regression. The regression fixed by this PR is still the shrink-to-fit intrinsic grid width case, where the grid container previously contributed only one child block width and wrapped the two abc def items.

Validation after the added test:

  • venv/bin/python -m ruff check weasyprint/layout/grid.py weasyprint/layout/preferred.py tests/layout/test_grid.py
  • venv/bin/python -m pytest tests/layout/test_grid.py -> 52 passed, 1 xfailed
  • venv/bin/python -m pytest -> 4027 passed, 40 xfailed

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.

1 participant