1
1
mirror of https://github.com/Kozea/WeasyPrint.git synced 2024-10-05 08:27:22 +03:00

Handle percentages for column groups

This commit is contained in:
Guillaume Ayoub 2013-08-03 16:35:19 +02:00
parent 5b4b67e67f
commit fd5cbad513
2 changed files with 95 additions and 3 deletions

View File

@ -387,15 +387,18 @@ def table_and_columns_preferred_widths(context, box, outer=True,
# Point #4
for column_group, column_group_width in column_groups_widths:
# TODO: handle percentages for column group widths
if (column_group_width and column_group_width != 'auto' and
column_group_width.unit != '%'):
(column_group_width.unit != '%' or resolved_table_width)):
column_indexes = [
column.grid_x for column in column_group.children]
columns_width = sum(
column_preferred_minimum_widths[index]
for index in column_indexes)
column_group_width = column_group_width.value
if column_group_width.unit == '%':
column_group_width = (
column_group_width.value / 100. * table.width)
else:
column_group_width = column_group_width.value
if column_group_width > columns_width:
added_space = (
(column_group_width - columns_width) / len(column_indexes))

View File

@ -1203,6 +1203,95 @@ def test_auto_layout_table():
assert td_2.width == 60
assert table.width == 200
# Column group width
page, = parse('''
<table style="width: 200px">
<colgroup style="width: 100px">
<col />
<col />
</colgroup>
<col style="width: 100px" />
<tbody>
<tr>
<td>a</td>
<td>a</td>
<td>abc</td>
</tr>
</tbody>
</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 td_1.width == 50
assert td_2.width == 50
assert td_3.width == 100
assert table.width == 200
# Column group width as percentage
page, = parse('''
<table style="width: 200px">
<colgroup style="width: 100px">
<col />
<col />
</colgroup>
<colgroup style="width: 50%">
<col />
<col />
</colgroup>
<tbody>
<tr>
<td>a</td>
<td>a</td>
<td>abc</td>
<td>abc</td>
</tr>
</tbody>
</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, td_4 = row.children
assert td_1.width == 50
assert td_2.width == 50
assert td_3.width == 50
assert td_4.width == 50
assert table.width == 200
# Wrong column group width
page, = parse('''
<table style="width: 200px">
<colgroup style="width: 80%">
<col />
<col />
</colgroup>
<tbody>
<tr>
<td>a</td>
<td>a</td>
</tr>
</tbody>
</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 = row.children
assert td_1.width == 100
assert td_2.width == 100
assert table.width == 200
# Column width as percentage and cell width in pixels
page, = parse('''
<table style="width: 200px">