Skip to content

Commit a866279

Browse files
committed
Fix margin collapsing between caption and table wrapper.
1 parent abcd386 commit a866279

File tree

2 files changed

+64
-4
lines changed

2 files changed

+64
-4
lines changed

weasyprint/layout/blocks.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,6 @@ def block_container_layout(context, box, max_position_y, skip_stack,
461461
new_containing_block = box
462462

463463
if not new_containing_block.is_table_wrapper:
464-
# TODO: there's no collapsing margins inside tables, right?
465464
resolve_percentages(child, new_containing_block)
466465
if (child.is_in_normal_flow() and
467466
last_in_flow_child is None and
@@ -493,7 +492,7 @@ def block_container_layout(context, box, max_position_y, skip_stack,
493492
adjoining_margins = []
494493
position_y = box.content_box_y()
495494

496-
if adjoining_margins and isinstance(child, boxes.TableBox):
495+
if adjoining_margins and box.is_table_wrapper:
497496
collapsed_margin = collapse_margin(adjoining_margins)
498497
child.position_y += collapsed_margin
499498
position_y += collapsed_margin
@@ -625,8 +624,11 @@ def block_container_layout(context, box, max_position_y, skip_stack,
625624
# not adjoining. (position_y is not used afterwards.)
626625
adjoining_margins = []
627626

628-
if box.border_bottom_width or box.padding_bottom or (
629-
establishes_formatting_context(box) or box.is_for_root_element):
627+
if (box.border_bottom_width or
628+
box.padding_bottom or
629+
establishes_formatting_context(box) or
630+
box.is_for_root_element or
631+
box.is_table_wrapper):
630632
position_y += collapse_margin(adjoining_margins)
631633
adjoining_margins = []
632634

weasyprint/tests/test_layout/test_table.py

+58
Original file line numberDiff line numberDiff line change
@@ -2075,3 +2075,61 @@ def test_inline_table_baseline(vertical_align, table_position_y):
20752075
assert text1.position_y == text2.position_y == 0
20762076
assert table.height == 10 * 2
20772077
assert table.position_y == table_position_y
2078+
2079+
2080+
@assert_no_logs
2081+
def test_table_caption_margin_top():
2082+
page, = render_pages('''
2083+
<style>
2084+
table { margin: 20px; }
2085+
caption, h1, h2 { margin: 20px; height: 10px }
2086+
td { height: 10px }
2087+
</style>
2088+
<h1></h1>
2089+
<table>
2090+
<caption></caption>
2091+
<tr>
2092+
<td></td>
2093+
</tr>
2094+
</table>
2095+
<h2></h2>
2096+
''')
2097+
html, = page.children
2098+
body, = html.children
2099+
h1, wrapper, h2 = body.children
2100+
caption, table = wrapper.children
2101+
tbody, = table.children
2102+
assert (h1.content_box_x(), h1.content_box_y()) == (20, 20)
2103+
assert (wrapper.content_box_x(), wrapper.content_box_y()) == (20, 50)
2104+
assert (caption.content_box_x(), caption.content_box_y()) == (40, 70)
2105+
assert (tbody.content_box_x(), tbody.content_box_y()) == (20, 100)
2106+
assert (h2.content_box_x(), h2.content_box_y()) == (20, 130)
2107+
2108+
2109+
@assert_no_logs
2110+
def test_table_caption_margin_bottom():
2111+
page, = render_pages('''
2112+
<style>
2113+
table { margin: 20px; }
2114+
caption, h1, h2 { margin: 20px; height: 10px; caption-side: bottom }
2115+
td { height: 10px }
2116+
</style>
2117+
<h1></h1>
2118+
<table>
2119+
<caption></caption>
2120+
<tr>
2121+
<td></td>
2122+
</tr>
2123+
</table>
2124+
<h2></h2>
2125+
''')
2126+
html, = page.children
2127+
body, = html.children
2128+
h1, wrapper, h2 = body.children
2129+
table, caption = wrapper.children
2130+
tbody, = table.children
2131+
assert (h1.content_box_x(), h1.content_box_y()) == (20, 20)
2132+
assert (wrapper.content_box_x(), wrapper.content_box_y()) == (20, 50)
2133+
assert (tbody.content_box_x(), tbody.content_box_y()) == (20, 50)
2134+
assert (caption.content_box_x(), caption.content_box_y()) == (40, 80)
2135+
assert (h2.content_box_x(), h2.content_box_y()) == (20, 130)

0 commit comments

Comments
 (0)