Skip to content

Commit

Permalink
Deduplicate main outer extra calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
liZe committed Feb 8, 2025
1 parent 1b891db commit 9c6c90f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
15 changes: 15 additions & 0 deletions tests/layout/test_flex.py
Original file line number Diff line number Diff line change
Expand Up @@ -1146,3 +1146,18 @@ def test_flex_item_auto_margin():
div, = article.children
assert div.height == 10
assert div.width == 10


@assert_no_logs
def test_flex_item_auto_margin_flex_basis():
page, = render_pages('''
<article style="display: flex">
<div style="margin-left: auto; height: 10px; flex-basis: 10px"></div>
</article>
''')
html, = page.children
body, = html.children
article, = body.children
div, = article.children
assert div.height == 10
assert div.width == 10
32 changes: 13 additions & 19 deletions weasyprint/layout/flex.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,30 +218,24 @@ def flex_layout(context, box, bottom_space, skip_stack, containing_block,
if child.style[axis] == 'auto':
flex_basis = 'content'
else:
if axis == 'width':
flex_basis = child.width
outer_extra = child.border_width() - flex_basis
if child.margin_left != 'auto':
outer_extra += child.margin_left
if child.margin_right != 'auto':
outer_extra += child.margin_right
else:
flex_basis = child.height
outer_extra = child.border_height() - flex_basis
if child.margin_top != 'auto':
outer_extra += child.margin_top
if child.margin_bottom != 'auto':
outer_extra += child.margin_bottom
else:
if axis == 'width':
outer_extra = child.margin_width() - flex_basis
else:
outer_extra = child.margin_height() - flex_basis
flex_basis = child.width if axis == 'width' else child.height

# 9.2.3.A If the item has a definite used flex basis, that’s the flex
# base size.
if flex_basis != 'content':
child.flex_base_size = flex_basis
if axis == 'width':
outer_extra = child.border_left_width + child.border_right_width
if child.margin_left != 'auto':
outer_extra += child.margin_left
if child.margin_right != 'auto':
outer_extra += child.margin_right
else:
outer_extra = child.border_top_width + child.border_bottom_width
if child.margin_top != 'auto':
outer_extra += child.margin_top
if child.margin_bottom != 'auto':
outer_extra += child.margin_bottom
elif False:
# TODO: 9.2.3.B If the flex item has … an intrinsic aspect ratio, a
# used flex basis of 'content', and a definite cross size, then the
Expand Down

0 comments on commit 9c6c90f

Please sign in to comment.