Skip to content

Commit

Permalink
Don’t crash when grid items have auto margins
Browse files Browse the repository at this point in the history
Auto margins are not handled (there already was a TODO for this), but at least
it doesn’t crash anymore.

Fix #2154.
  • Loading branch information
liZe committed May 14, 2024
1 parent 22b35bb commit 0019940
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
21 changes: 21 additions & 0 deletions tests/layout/test_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -835,3 +835,24 @@ def test_grid_margin():
assert div_b.height == 4
assert div_c.height == 6
assert article.height == 10


@assert_no_logs
def test_grid_item_margin():
# Regression test for https://github.com/Kozea/WeasyPrint/issues/2154
page, = render_pages('''
<style>
@font-face { src: url(weasyprint.otf); font-family: weasyprint }
article { display: grid }
div { margin: auto }
</style>
<article>
<div>a</div>
<div>b</div>
</article>
''')
html, = page.children
body, = html.children
article, = body.children
div_a, div_b = article.children
# TODO: Test auto margin values.
12 changes: 11 additions & 1 deletion weasyprint/layout/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -1119,6 +1119,17 @@ def grid_layout(context, box, bottom_space, skip_stack, containing_block,
height = (
sum(size for size, _ in rows_sizes[y:y+height]) +
(height - 1) * row_gap)

# TODO: Apply auto margin.
if child.margin_top == 'auto':
child.margin_top = 0
if child.margin_right == 'auto':
child.margin_right = 0
if child.margin_bottom == 'auto':
child.margin_bottom = 0
if child.margin_left == 'auto':
child.margin_left = 0

child_width = width - (
child.margin_left + child.border_left_width + child.padding_left +
child.margin_right + child.border_right_width + child.padding_right)
Expand Down Expand Up @@ -1156,7 +1167,6 @@ def grid_layout(context, box, bottom_space, skip_stack, containing_block,
# TODO: Support fragmentation in grid rows.
continue

# TODO: Apply auto margins.
if justify_self & {'normal', 'stretch'}:
new_child.width = max(child_width, new_child.width)
else:
Expand Down

0 comments on commit 0019940

Please sign in to comment.