Skip to content

Commit

Permalink
Merge pull request #1884 from kygoh/fix_issue_1278
Browse files Browse the repository at this point in the history
Propose fix for issue #1278
  • Loading branch information
liZe authored Jan 29, 2024
2 parents c6468e5 + f11fcc4 commit ca60caf
Show file tree
Hide file tree
Showing 7 changed files with 536 additions and 290 deletions.
220 changes: 220 additions & 0 deletions tests/draw/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -1298,3 +1298,223 @@ def test_tables_23(assert_pixels):
<table>
<thead><tr><td>abcde</td><td>abcde</td></tr></thead>
<tbody><tr><td>abc abc</td><td></td></tr></tbody>''')


@assert_no_logs
def test_running_elements_table_border_collapse(assert_pixels):
assert_pixels(2 * '''
KK_____________
KK_____________
_______________
_______________
_______________
KKKKKKK________
KRRKRRK________
KRRKRRK________
KKKKKKK________
KRRKRRK________
KRRKRRK________
KKKKKKK________
_______________
_______________
_______________
''', '''
<style>
@font-face { src: url(weasyprint.otf); font-family: weasyprint }
@page {
margin: 0 0 10px 0;
size: 15px;
@bottom-left { content: element(table) }
}
body { font: 2px/1 weasyprint }
table {
border: 1px solid black;
border-collapse: collapse;
color: red;
position: running(table);
}
td { border: 1px solid black }
div { page-break-after: always }
</style>
<table>
<tr> <td>A</td> <td>B</td> </tr>
<tr> <td>C</td> <td>D</td> </tr>
</table>
<div>1</div>
<div>2</div>
''')


@assert_no_logs
def test_running_elements_table_border_collapse_empty(assert_pixels):
assert_pixels(2 * '''
KK________
KK________
__________
__________
__________
__________
__________
__________
__________
__________
''', '''
<style>
@font-face { src: url(weasyprint.otf); font-family: weasyprint }
@page {
margin: 0 0 5px 0;
size: 10px;
@bottom-left { content: element(table) }
}
body { font: 2px/1 weasyprint }
table {
border: 1px solid black;
border-collapse: collapse;
color: red;
position: running(table);
}
td { border: 1px solid black }
div { page-break-after: always }
</style>
<table></table>
<div>1</div>
<div>2</div>
''')


@pytest.mark.xfail
@assert_no_logs
def test_running_elements_table_border_collapse_border_style(assert_pixels):
assert_pixels(2 * '''
KK_____________
KK_____________
_______________
_______________
_______________
KKKZ___________
KRR_RR_________
KRR_RR_________
KKKK__Z________
KRRKRRK________
KRRKRRK________
KKKKKKK________
_______________
_______________
_______________
''', '''
<style>
@font-face { src: url(weasyprint.otf); font-family: weasyprint }
@page {
margin: 0 0 10px 0;
size: 15px;
@bottom-left { content: element(table) }
}
body { font: 2px/1 weasyprint }
table {
border: 1px solid black;
border-collapse: collapse;
color: red;
position: running(table);
}
td { border: 1px solid black }
div { page-break-after: always }
</style>
<table>
<tr> <td>A</td> <td style="border-style: hidden">B</td> </tr>
<tr> <td>C</td> <td style="border-style: none">D</td> </tr>
</table>
<div>1</div>
<div>2</div>
''')


@assert_no_logs
def test_running_elements_table_border_collapse_span(assert_pixels):
assert_pixels(2 * '''
KK_____________
KK_____________
_______________
_______________
_______________
KKKKKKKKKK_____
KRRKRRKRRK_____
KRRKRRKRRK_____
K__KKKKKKK_____
K__KRR___K_____
K__KRR___K_____
KKKKKKKKKK_____
_______________
_______________
_______________
''', '''
<style>
@font-face { src: url(weasyprint.otf); font-family: weasyprint }
@page {
margin: 0 0 10px 0;
size: 15px;
@bottom-left { content: element(table) }
}
body { font: 2px/1 weasyprint }
table {
border: 1px solid black;
border-collapse: collapse;
color: red;
position: running(table);
}
td { border: 1px solid black }
div { page-break-after: always }
</style>
<table>
<tr> <td rowspan=2>A</td> <td>B</td> <td>C</td> </tr>
<tr> <td colspan=2>D</td> </tr>
</table>
<div>1</div>
<div>2</div>
''')


@pytest.mark.xfail
@assert_no_logs
def test_running_elements_table_border_collapse_margin(assert_pixels):
assert_pixels(2 * '''
KK_____________
KK_____________
_______________
_______________
_______________
_______________
____KKKKKKK____
____KRRKRRK____
____KRRKRRK____
____KKKKKKK____
____KRRKRRK____
____KRRKRRK____
____KKKKKKK____
_______________
_______________
''', '''
<style>
@font-face { src: url(weasyprint.otf); font-family: weasyprint }
@page {
margin: 0 0 10px 0;
size: 15px;
@bottom-center { content: element(table); width: 100% }
}
body { font: 2px/1 weasyprint }
table {
border: 1px solid black;
border-collapse: collapse;
color: red;
margin: 1px auto;
position: running(table);
}
td { border: 1px solid black }
div { page-break-after: always }
</style>
<table>
<tr> <td>A</td> <td>B</td> </tr>
<tr> <td>C</td> <td>D</td> </tr>
</table>
<div>1</div>
<div>2</div>
''')
134 changes: 133 additions & 1 deletion tests/layout/test_table.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
"""Tests for layout of tables."""

import pytest
from weasyprint.formatting_structure import boxes
from weasyprint.layout.table import collapse_table_borders

from ..testing_utils import assert_no_logs, capture_logs, render_pages
from ..testing_utils import (
assert_no_logs, capture_logs, parse_all, render_pages)


def _get_grid(html, grid_width, grid_height):
html = parse_all(html)
body, = html.children
table_wrapper, = body.children
table, = table_wrapper.children
border_lists = collapse_table_borders(table, grid_width, grid_height)
return tuple(
[[(style, width, color) if width else None
for _score, (style, width, color) in border]
for border in border_list]
for border_list in border_lists)


@assert_no_logs
Expand Down Expand Up @@ -2991,3 +3007,119 @@ def test_min_width_with_overflow():

assert table1_td1.min_width == table2_td1.min_width
assert table1_td1.width == table2_td1.width


black = (0, 0, 0, 1)
red = (1, 0, 0, 1)
green = (0, 1, 0, 1) # lime in CSS
blue = (0, 0, 1, 1)
yellow = (1, 1, 0, 1)
black_3 = ('solid', 3, black)
red_1 = ('solid', 1, red)
yellow_5 = ('solid', 5, yellow)
green_5 = ('solid', 5, green)
dashed_blue_5 = ('dashed', 5, blue)


@assert_no_logs
def test_border_collapse_1():
html = parse_all('<table></table>')
body, = html.children
table_wrapper, = body.children
table, = table_wrapper.children
assert isinstance(table, boxes.TableBox)
assert not hasattr(table, 'collapsed_border_grid')

grid = _get_grid('<table style="border-collapse: collapse"></table>', 0, 0)
assert grid == ([], [])


@assert_no_logs
def test_border_collapse_2():
vertical_borders, horizontal_borders = _get_grid('''
<style>td { border: 1px solid red }</style>
<table style="border-collapse: collapse; border: 3px solid black">
<tr> <td>A</td> <td>B</td> </tr>
<tr> <td>C</td> <td>D</td> </tr>
</table>
''', 2, 2)
assert vertical_borders == [
[black_3, red_1, black_3],
[black_3, red_1, black_3],
]
assert horizontal_borders == [
[black_3, black_3],
[red_1, red_1],
[black_3, black_3],
]


@assert_no_logs
def test_border_collapse_3():
# hidden vs. none
vertical_borders, horizontal_borders = _get_grid('''
<style>table, td { border: 3px solid }</style>
<table style="border-collapse: collapse">
<tr> <td>A</td> <td style="border-style: hidden">B</td> </tr>
<tr> <td>C</td> <td style="border-style: none">D</td> </tr>
</table>
''', 2, 2)
assert vertical_borders == [
[black_3, None, None],
[black_3, black_3, black_3],
]
assert horizontal_borders == [
[black_3, None],
[black_3, None],
[black_3, black_3],
]


@assert_no_logs
def test_border_collapse_4():
vertical_borders, horizontal_borders = _get_grid('''
<style>td { border: 1px solid red }</style>
<table style="border-collapse: collapse; border: 5px solid yellow">
<col style="border: 3px solid black" />
<tr> <td></td> <td></td> <td></td> </tr>
<tr> <td></td> <td style="border: 5px dashed blue"></td>
<td style="border: 5px solid lime"></td> </tr>
<tr> <td></td> <td></td> <td></td> </tr>
<tr> <td></td> <td></td> <td></td> </tr>
</table>
''', 3, 4)
assert vertical_borders == [
[yellow_5, black_3, red_1, yellow_5],
[yellow_5, dashed_blue_5, green_5, green_5],
[yellow_5, black_3, red_1, yellow_5],
[yellow_5, black_3, red_1, yellow_5],
]
assert horizontal_borders == [
[yellow_5, yellow_5, yellow_5],
[red_1, dashed_blue_5, green_5],
[red_1, dashed_blue_5, green_5],
[red_1, red_1, red_1],
[yellow_5, yellow_5, yellow_5],
]


@assert_no_logs
def test_border_collapse_5():
# rowspan and colspan
vertical_borders, horizontal_borders = _get_grid('''
<style>col, tr { border: 3px solid }</style>
<table style="border-collapse: collapse">
<col /><col /><col />
<tr> <td rowspan=2></td> <td></td> <td></td> </tr>
<tr> <td colspan=2></td> </tr>
</table>
''', 3, 2)
assert vertical_borders == [
[black_3, black_3, black_3, black_3],
[black_3, black_3, None, black_3],
]
assert horizontal_borders == [
[black_3, black_3, black_3],
[None, black_3, black_3],
[black_3, black_3, black_3],
]
Loading

0 comments on commit ca60caf

Please sign in to comment.