You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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 complete CSS Grid track sizing algorithm for intrinsic contributions.
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.
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.
It is deliberately conservative: it improves common grid content width cases without attempting to implement the complete CSS Grid intrinsic sizing algorithm.
In this case, it may be better to have a reliable intrinsic sizing algorithm than a workaround.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changed
This adds a small intrinsic-width helper for grid containers used by preferred width calculations.
Before this change,
min_content_width()andmax_content_width()treatedGridContainerBoxlike 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 containingabc deftwice reported a width close to oneabc def, causing each grid item to wrap toabc/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:
aspect-rationot supported in CSS #1891:aspect-ratiois unsupported.row-gapandgapproperties in flex layout #1665: flexgapsupport, later handled by the flex rewrite.box-sizing: border-box#2628/Handlebox-sizing: border-boxin grid layout #2643 (box-sizing: border-boxin grid layout, merged), and Support grid-auto-flow: column #2142/Support grid-auto-flow: column #2162 (grid-auto-flow: column, merged).What this fixes
The PR fixes the maintainer’s minimal intrinsic-size example from #2354/#2145:
With this patch, the float’s max-content width includes both grid tracks, and the two
abc defitems 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:
aspect-ratiosupport (aspect-rationot supported in CSS #1891);min-content/max-contentcases;auto-fit/auto-fill;automargins for grid items;min-*/max-*constraints on grid items;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.pyvenv/bin/python -m pytest tests/layout/test_grid.pyvenv/bin/python -m pytest tests/layout/test_preferred.py tests/layout/test_flex.pyvenv/bin/python -m pytest-> 4027 passed, 40 xfailedAddresses part of #2354.
Related to #2145, #2644, #2342, #1891, #1665, #1652, #2362, #2628, #2647.