Skip to content

Commit

Permalink
Don't respect table cell width when content doesn't fit
Browse files Browse the repository at this point in the history
Fix #906.
  • Loading branch information
liZe committed Jul 24, 2019
1 parent 620e075 commit 8d10c01
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 11 deletions.
49 changes: 38 additions & 11 deletions weasyprint/layout/preferred.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@ def min_content_width(context, box, outer=True):
This is the width by breaking at every line-break opportunity.
"""
if isinstance(box, (
if box.is_table_wrapper:
return table_and_columns_preferred_widths(context, box, outer)[0]
elif isinstance(box, boxes.TableCellBox):
return table_cell_min_content_width(context, box, outer)
elif isinstance(box, (
boxes.BlockContainerBox, boxes.TableColumnBox, boxes.FlexBox)):
if box.is_table_wrapper:
return table_and_columns_preferred_widths(context, box, outer)[0]
else:
return block_min_content_width(context, box, outer)
return block_min_content_width(context, box, outer)
elif isinstance(box, boxes.TableColumnGroupBox):
return column_group_content_width(context, box)
elif isinstance(box, (boxes.InlineBox, boxes.LineBox)):
Expand All @@ -69,12 +70,13 @@ def max_content_width(context, box, outer=True):
This is the width by only breaking at forced line breaks.
"""
if isinstance(box, (
if box.is_table_wrapper:
return table_and_columns_preferred_widths(context, box, outer)[1]
elif isinstance(box, boxes.TableCellBox):
return table_cell_max_content_width(context, box, outer)
elif isinstance(box, (
boxes.BlockContainerBox, boxes.TableColumnBox, boxes.FlexBox)):
if box.is_table_wrapper:
return table_and_columns_preferred_widths(context, box, outer)[1]
else:
return block_max_content_width(context, box, outer)
return block_max_content_width(context, box, outer)
elif isinstance(box, boxes.TableColumnGroupBox):
return column_group_content_width(context, box)
elif isinstance(box, (boxes.InlineBox, boxes.LineBox)):
Expand Down Expand Up @@ -206,7 +208,7 @@ def inline_max_content_width(context, box, outer=True, is_line_start=False):


def column_group_content_width(context, box):
"""Return the *-content width for an ``TableColumnGroupBox``."""
"""Return the *-content width for a ``TableColumnGroupBox``."""
width = box.style['width']
if width == 'auto' or width.unit == '%':
width = 0
Expand All @@ -217,6 +219,31 @@ def column_group_content_width(context, box):
return adjust(box, False, width)


def table_cell_min_content_width(context, box, outer):
"""Return the min-content width for a ``TableCellBox``."""
children_widths = [
min_content_width(context, child, outer=True)
for child in box.children
if not child.is_absolutely_positioned()]
children_min_width = margin_width(
box, max(children_widths) if children_widths else 0)

width = box.style['width']
if width != 'auto' and width.unit == 'px':
cell_min_width = adjust(box, outer, width.value)
else:
cell_min_width = 0

return max(children_min_width, cell_min_width)


def table_cell_max_content_width(context, box, outer):
"""Return the max-content width for a ``TableCellBox``."""
return max(
table_cell_min_content_width(context, box, outer),
block_max_content_width(context, box, outer))


def inline_line_widths(context, box, outer, is_line_start, minimum,
skip_stack=None, first_line=False):
if box.style['text_indent'].unit == '%':
Expand Down
29 changes: 29 additions & 0 deletions weasyprint/tests/test_layout/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,35 @@ def test_layout_table_auto_4():
assert table.width == 27 # 3 * spacing + 4 + 4 + 2 * b1 + 2 * b2


@assert_no_logs
def test_layout_table_auto_5():
page, = render_pages('''
<style>
@font-face { src: url(AHEM____.TTF); font-family: ahem }
* { font-family: ahem }
</style>
<table style="width: 1000px; font-family: ahem">
<tr>
<td style="width: 40px">aa aa aa aa</td>
<td style="width: 40px">aaaaaaaaaaa</td>
<td>This will take the rest of the width</td>
</tr>
</table>
''')
html, = page.children
body, = html.children
table_wrapper, = body.children
table, = table_wrapper.children
row_group, = table.children
row, = row_group.children
td_1, td_2, td_3 = row.children

assert table.width == 1000
assert td_1.width == 40
assert td_2.width == 11 * 16
assert td_3.width == 1000 - 40 - 11 * 16


@assert_no_logs
def test_layout_table_auto_6():
page, = render_pages('''
Expand Down

0 comments on commit 8d10c01

Please sign in to comment.