1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
|
"""Tests for floating boxes layout."""
import pytest
from weasyprint.formatting_structure import boxes
from ..testing_utils import assert_no_logs, render_pages
def outer_area(box):
"""Return the (x, y, w, h) rectangle for the outer area of a box."""
return (box.position_x, box.position_y,
box.margin_width(), box.margin_height())
@assert_no_logs
def test_floats_1():
# adjacent-floats-001
page, = render_pages('''
<style>
div { float: left }
img { width: 100px; vertical-align: top }
</style>
<div><img src=pattern.png /></div>
<div><img src=pattern.png /></div>''')
html, = page.children
body, = html.children
div_1, div_2 = body.children
assert outer_area(div_1) == (0, 0, 100, 100)
assert outer_area(div_2) == (100, 0, 100, 100)
@assert_no_logs
def test_floats_2():
# c414-flt-fit-000
page, = render_pages('''
<style>
body { width: 290px }
div { float: left; width: 100px; }
img { width: 60px; vertical-align: top }
</style>
<div><img src=pattern.png /><!-- 1 --></div>
<div><img src=pattern.png /><!-- 2 --></div>
<div><img src=pattern.png /><!-- 4 --></div>
<img src=pattern.png /><!-- 3
--><img src=pattern.png /><!-- 5 -->''')
html, = page.children
body, = html.children
div_1, div_2, div_4, anon_block = body.children
line_3, line_5 = anon_block.children
img_3, = line_3.children
img_5, = line_5.children
assert outer_area(div_1) == (0, 0, 100, 60)
assert outer_area(div_2) == (100, 0, 100, 60)
assert outer_area(img_3) == (200, 0, 60, 60)
assert outer_area(div_4) == (0, 60, 100, 60)
assert outer_area(img_5) == (100, 60, 60, 60)
@assert_no_logs
def test_floats_3():
# c414-flt-fit-002
page, = render_pages('''
<style type="text/css">
body { width: 200px }
p { width: 70px; height: 20px }
.left { float: left }
.right { float: right }
</style>
<p class="left"> ⇦ A 1 </p>
<p class="left"> ⇦ B 2 </p>
<p class="left"> ⇦ A 3 </p>
<p class="right"> B 4 ⇨ </p>
<p class="left"> ⇦ A 5 </p>
<p class="right"> B 6 ⇨ </p>
<p class="right"> B 8 ⇨ </p>
<p class="left"> ⇦ A 7 </p>
<p class="left"> ⇦ A 9 </p>
<p class="left"> ⇦ B 10 </p>
''')
html, = page.children
body, = html.children
positions = [(paragraph.position_x, paragraph.position_y)
for paragraph in body.children]
assert positions == [
(0, 0), (70, 0), (0, 20), (130, 20), (0, 40), (130, 40),
(130, 60), (0, 60), (0, 80), (70, 80), ]
@assert_no_logs
def test_floats_4():
# c414-flt-wrap-000 ... more or less
page, = render_pages('''
<style>
body { width: 100px }
p { float: left; height: 100px }
img { width: 60px; vertical-align: top }
</style>
<p style="width: 20px"></p>
<p style="width: 100%"></p>
<img src=pattern.png /><img src=pattern.png />
''')
html, = page.children
body, = html.children
p_1, p_2, anon_block = body.children
line_1, line_2 = anon_block.children
assert anon_block.position_y == 0
assert (line_1.position_x, line_1.position_y) == (20, 0)
assert (line_2.position_x, line_2.position_y) == (0, 200)
@assert_no_logs
def test_floats_5():
# c414-flt-wrap-000 with text ... more or less
page, = render_pages('''
<style>
@font-face { src: url(weasyprint.otf); font-family: weasyprint }
body { width: 100px; font: 60px weasyprint; }
p { float: left; height: 100px }
img { width: 60px; vertical-align: top }
</style>
<p style="width: 20px"></p>
<p style="width: 100%"></p>
A B
''')
html, = page.children
body, = html.children
p_1, p_2, anon_block = body.children
line_1, line_2 = anon_block.children
assert anon_block.position_y == 0
assert (line_1.position_x, line_1.position_y) == (20, 0)
assert (line_2.position_x, line_2.position_y) == (0, 200)
@assert_no_logs
def test_floats_6():
# floats-placement-vertical-001b
page, = render_pages('''
<style>
body { width: 90px; font-size: 0 }
img { vertical-align: top }
</style>
<body>
<span>
<img src=pattern.png style="width: 50px" />
<img src=pattern.png style="width: 50px" />
<img src=pattern.png style="float: left; width: 30px" />
</span>
''')
html, = page.children
body, = html.children
line_1, line_2 = body.children
span_1, = line_1.children
span_2, = line_2.children
img_1, = span_1.children
img_2, img_3 = span_2.children
assert outer_area(img_1) == (0, 0, 50, 50)
assert outer_area(img_2) == (30, 50, 50, 50)
assert outer_area(img_3) == (0, 50, 30, 30)
@assert_no_logs
def test_floats_7():
# Variant of the above: no <span>
page, = render_pages('''
<style>
body { width: 90px; font-size: 0 }
img { vertical-align: top }
</style>
<body>
<img src=pattern.png style="width: 50px" />
<img src=pattern.png style="width: 50px" />
<img src=pattern.png style="float: left; width: 30px" />
''')
html, = page.children
body, = html.children
line_1, line_2 = body.children
img_1, = line_1.children
img_2, img_3 = line_2.children
assert outer_area(img_1) == (0, 0, 50, 50)
assert outer_area(img_2) == (30, 50, 50, 50)
assert outer_area(img_3) == (0, 50, 30, 30)
@assert_no_logs
def test_floats_8():
# Floats do no affect other pages
page_1, page_2 = render_pages('''
<style>
body { width: 90px; font-size: 0 }
img { vertical-align: top }
</style>
<body>
<img src=pattern.png style="float: left; width: 30px" />
<img src=pattern.png style="width: 50px" />
<div style="page-break-before: always"></div>
<img src=pattern.png style="width: 50px" />
''')
html, = page_1.children
body, = html.children
float_img, anon_block, = body.children
line, = anon_block.children
img_1, = line.children
assert outer_area(float_img) == (0, 0, 30, 30)
assert outer_area(img_1) == (30, 0, 50, 50)
html, = page_2.children
body, = html.children
div, anon_block = body.children
line, = anon_block.children
img_2, = line.children
@assert_no_logs
def test_floats_9():
# Regression test
# https://github.com/Kozea/WeasyPrint/issues/263
page, = render_pages('''<div style="top:100%; float:left">''')
@assert_no_logs
def test_floats_page_breaks_1():
# Tests floated images shorter than the page
pages = render_pages('''
<style>
@page { size: 100px; margin: 10px }
img { height: 45px; width:70px; float: left;}
</style>
<body>
<img src=pattern.png>
<!-- page break should be here !!! -->
<img src=pattern.png>
''')
assert len(pages) == 2
page_images = []
for page in pages:
images = [d for d in page.descendants() if d.element_tag == 'img']
assert all([img.element_tag == 'img' for img in images])
assert all([img.position_x == 10 for img in images])
page_images.append(images)
positions_y = [[img.position_y for img in images]
for images in page_images]
assert positions_y == [[10], [10]]
@assert_no_logs
def test_floats_page_breaks_2():
# Tests floated images taller than the page
pages = render_pages('''
<style>
@page { size: 100px; margin: 10px }
img { height: 81px; width:70px; float: left;}
</style>
<body>
<img src=pattern.png>
<!-- page break should be here !!! -->
<img src=pattern.png>
''')
assert len(pages) == 2
page_images = []
for page in pages:
images = [d for d in page.descendants() if d.element_tag == 'img']
assert all([img.element_tag == 'img' for img in images])
assert all([img.position_x == 10 for img in images])
page_images.append(images)
positions_y = [[img.position_y for img in images]
for images in page_images]
assert positions_y == [[10], [10]]
@assert_no_logs
def test_floats_page_breaks_3():
# Tests floated images shorter than the page
pages = render_pages('''
<style>
@page { size: 100px; margin: 10px }
img { height: 30px; width:70px; float: left;}
</style>
<body>
<img src=pattern.png>
<img src=pattern.png>
<!-- page break should be here !!! -->
<img src=pattern.png>
<img src=pattern.png>
<!-- page break should be here !!! -->
<img src=pattern.png>
''')
assert len(pages) == 3
page_images = []
for page in pages:
images = [d for d in page.descendants() if d.element_tag == 'img']
assert all([img.element_tag == 'img' for img in images])
assert all([img.position_x == 10 for img in images])
page_images.append(images)
positions_y = [[img.position_y for img in images]
for images in page_images]
assert positions_y == [[10, 40], [10, 40], [10]]
@assert_no_logs
def test_preferred_widths_1():
def get_float_width(body_width):
page, = render_pages('''
<style>
@font-face { src: url(weasyprint.otf); font-family: weasyprint }
</style>
<body style="width: %spx; font-family: weasyprint">
<p style="white-space: pre-line; float: left">
Lorem ipsum dolor sit amet,
consectetur elit
</p>
<!-- ^ No-break space here -->
''' % body_width)
html, = page.children
body, = html.children
paragraph, = body.children
return paragraph.width
# Preferred minimum width:
assert get_float_width(10) == len('consectetur elit') * 16
# Preferred width:
assert get_float_width(1000000) == len('Lorem ipsum dolor sit amet,') * 16
@assert_no_logs
def test_preferred_widths_2():
# Non-regression test:
# Incorrect whitespace handling in preferred width used to cause
# unnecessary line break.
page, = render_pages('''
<p style="float: left">Lorem <em>ipsum</em> dolor.</p>
''')
html, = page.children
body, = html.children
paragraph, = body.children
assert len(paragraph.children) == 1
assert isinstance(paragraph.children[0], boxes.LineBox)
@assert_no_logs
def test_preferred_widths_3():
page, = render_pages('''
<style>img { width: 20px }</style>
<p style="float: left">
<img src=pattern.png><img src=pattern.png><br>
<img src=pattern.png></p>
''')
html, = page.children
body, = html.children
paragraph, = body.children
assert paragraph.width == 40
@assert_no_logs
def test_preferred_widths_4():
page, = render_pages(
'<style>'
' @font-face { src: url(weasyprint.otf); font-family: weasyprint }'
' p { font: 20px weasyprint }'
'</style>'
'<p style="float: left">XX<br>XX<br>X</p>')
html, = page.children
body, = html.children
paragraph, = body.children
assert paragraph.width == 40
@assert_no_logs
def test_preferred_widths_5():
# The space is the start of the line is collapsed.
page, = render_pages(
'<style>'
' @font-face { src: url(weasyprint.otf); font-family: weasyprint }'
' p { font: 20px weasyprint }'
'</style>'
'<p style="float: left">XX<br> XX<br>X</p>')
html, = page.children
body, = html.children
paragraph, = body.children
assert paragraph.width == 40
@assert_no_logs
def test_float_in_inline_1():
page, = render_pages('''
<style>
@font-face { src: url(weasyprint.otf); font-family: weasyprint }
body {
font-family: weasyprint;
font-size: 20px;
}
p {
width: 14em;
text-align: justify;
}
span {
float: right;
}
</style>
<p>
aa bb <a><span>cc</span> ddd</a> ee ff
</p>
''')
html, = page.children
body, = html.children
paragraph, = body.children
line1, line2 = paragraph.children
p1, a, p2 = line1.children
assert p1.width == 6 * 20
assert p1.text == 'aa bb '
assert p1.position_x == 0 * 20
assert p2.width == 3 * 20
assert p2.text == ' ee'
assert p2.position_x == 9 * 20
span, a_text = a.children
assert a_text.width == 3 * 20 # leading space collapse
assert a_text.text == 'ddd'
assert a_text.position_x == 6 * 20
assert span.width == 2 * 20
assert span.children[0].children[0].text == 'cc'
assert span.position_x == 12 * 20
p3, = line2.children
assert p3.width == 2 * 20
@assert_no_logs
def test_float_in_inline_2():
page, = render_pages('''
<style>
@font-face { src: url(weasyprint.otf); font-family: weasyprint }
@page {
size: 10em;
}
article {
font-family: weasyprint;
line-height: 1;
}
div {
float: left;
width: 50%;
}
</style>
<article>
<span>
<div>a b c</div>
1 2 3 4 5 6
</span>
</article>''')
html, = page.children
body, = html.children
article, = body.children
line1, line2 = article.children
span1, = line1.children
div, text = span1.children
assert div.children[0].children[0].text.strip() == 'a b c'
assert text.text.strip() == '1 2 3'
span2, = line2.children
text, = span2.children
assert text.text.strip() == '4 5 6'
@assert_no_logs
def test_float_in_inline_3():
page, = render_pages('''
<style>
@font-face { src: url(weasyprint.otf); font-family: weasyprint }
@page {
size: 10em;
}
article {
font-family: weasyprint;
line-height: 1;
}
div {
float: left;
width: 50%;
}
</style>
<article>
<span>
1 2 3 <div>a b c</div> 4 5 6
</span>
</article>''')
html, = page.children
body, = html.children
article, = body.children
line1, line2 = article.children
span1, = line1.children
text, div = span1.children
assert text.text.strip() == '1 2 3'
assert div.children[0].children[0].text.strip() == 'a b c'
span2, = line2.children
text, = span2.children
assert text.text.strip() == '4 5 6'
@assert_no_logs
def test_float_in_inline_4():
page, = render_pages('''
<style>
@font-face { src: url(weasyprint.otf); font-family: weasyprint }
@page {
size: 10em;
}
article {
font-family: weasyprint;
line-height: 1;
}
div {
float: left;
width: 50%;
}
</style>
<article>
<span>
1 2 3 4 <div>a b c</div> 5 6
</span>
</article>''')
html, = page.children
body, = html.children
article, = body.children
line1, line2 = article.children
span1, div = line1.children
text1, text2 = span1.children
assert text1.text.strip() == '1 2 3 4'
assert text2.text.strip() == '5'
assert div.position_y == 16
assert div.children[0].children[0].text.strip() == 'a b c'
span2, = line2.children
text, = span2.children
assert text.text.strip() == '6'
@assert_no_logs
def test_float_next_line():
page, = render_pages('''
<style>
@font-face { src: url(weasyprint.otf); font-family: weasyprint }
body {
font-family: weasyprint;
font-size: 20px;
}
p {
text-align: justify;
width: 13em;
}
span {
float: left;
}
</style>
<p>pp pp pp pp <a><span>ppppp</span> aa</a> pp pp pp pp pp</p>''')
html, = page.children
body, = html.children
paragraph, = body.children
line1, line2, line3 = paragraph.children
assert len(line1.children) == 1
assert len(line3.children) == 1
a, p = line2.children
span, a_text = a.children
assert span.position_x == 0
assert span.width == 5 * 20
assert a_text.position_x == a.position_x == 5 * 20
assert a_text.width == a.width == 2 * 20
assert p.position_x == 7 * 20
@assert_no_logs
def test_float_text_indent_1():
page, = render_pages('''
<style>
@font-face { src: url(weasyprint.otf); font-family: weasyprint }
body {
font-family: weasyprint;
font-size: 20px;
}
p {
text-align: justify;
text-indent: 1em;
width: 14em;
}
span {
float: left;
}
</style>
<p><a>aa <span>float</span> aa</a></p>''')
html, = page.children
body, = html.children
paragraph, = body.children
line1, = paragraph.children
a, = line1.children
a1, span, a2 = a.children
span_text, = span.children
assert span.position_x == span_text.position_x == 0
assert span.width == span_text.width == (
(1 + 5) * 20) # text-indent + span text
assert a1.width == 3 * 20
assert a1.position_x == (1 + 5 + 1) * 20 # span + a1 text-indent
assert a2.width == 2 * 20 # leading space collapse
assert a2.position_x == (1 + 5 + 1 + 3) * 20 # span + a1 t-i + a1
@assert_no_logs
def test_float_text_indent_2():
page, = render_pages('''
<style>
@font-face { src: url(weasyprint.otf); font-family: weasyprint }
body {
font-family: weasyprint;
font-size: 20px;
}
p {
text-align: justify;
text-indent: 1em;
width: 14em;
}
span {
float: left;
}
</style>
<p>
oooooooooooo
<a>aa <span>float</span> aa</a></p>''')
html, = page.children
body, = html.children
paragraph, = body.children
line1, line2 = paragraph.children
p1, = line1.children
assert p1.position_x == 1 * 20 # text-indent
assert p1.width == 12 * 20 # p text
a, = line2.children
a1, span, a2 = a.children
span_text, = span.children
assert span.position_x == span_text.position_x == 0
assert span.width == span_text.width == (
(1 + 5) * 20) # text-indent + span text
assert a1.width == 3 * 20
assert a1.position_x == (1 + 5) * 20 # span
assert a2.width == 2 * 20 # leading space collapse
assert a2.position_x == (1 + 5 + 3) * 20 # span + a1
@assert_no_logs
def test_float_text_indent_3():
page, = render_pages('''
<style>
@font-face { src: url(weasyprint.otf); font-family: weasyprint }
body {
font-family: weasyprint;
font-size: 20px;
}
p {
text-align: justify;
text-indent: 1em;
width: 14em;
}
span {
float: right;
}
</style>
<p>
oooooooooooo
<a>aa <span>float</span> aa</a>
oooooooooooo
</p>''')
html, = page.children
body, = html.children
paragraph, = body.children
line1, line2, line3 = paragraph.children
p1, = line1.children
assert p1.position_x == 1 * 20 # text-indent
assert p1.width == 12 * 20 # p text
a, = line2.children
a1, span, a2 = a.children
span_text, = span.children
assert span.position_x == span_text.position_x == (14 - 5 - 1) * 20
assert span.width == span_text.width == (
(1 + 5) * 20) # text-indent + span text
assert a1.position_x == 0 # span
assert a2.width == 2 * 20 # leading space collapse
assert a2.position_x == (14 - 5 - 1 - 2) * 20
p2, = line3.children
assert p2.position_x == 0
assert p2.width == 12 * 20 # p text
@assert_no_logs
def test_float_previous_break():
page1, page2 = render_pages('''
<style>
@font-face { src: url(weasyprint.otf); font-family: weasyprint }
@page {
size: 13px;
}
body {
font-family: weasyprint;
font-size: 2px;
line-height: 1;
}
p {
break-after: avoid;
}
section {
break-inside: avoid;
float: left;
}
</style>
<article>
oooooo
oooooo
oooooo
oooooo
</article>
<p>xxxxxx</p>
<p>yyyyyy</p>
<section>
<div>aaaaaa</div>
<div>cccccc</div>
</section>
<article>
dddddd
</article>''')
html, = page1.children
body, = html.children
article, = body.children
html, = page2.children
body, = html.children
p1, p2, section, article = body.children
@pytest.mark.xfail
@assert_no_logs
def test_float_fail():
page, = render_pages('''
<style>
@font-face { src: url(weasyprint.otf); font-family: weasyprint }
body {
font-family: weasyprint;
font-size: 20px;
}
p {
text-align: justify;
width: 12em;
}
span {
float: left;
background: red;
}
a {
background: yellow;
}
</style>
<p>bb bb pp bb pp pb <a><span>pp pp</span> apa</a> bb bb</p>''')
html, = page.children
body, = html.children
paragraph, = body.children
line1, line2, line3 = paragraph.children
def test_float_table_aborted_row():
page1, page2 = render_pages('''
<style>
@font-face {src: url(weasyprint.otf); font-family: weasyprint}
@page {size: 10px 7px}
body {font-family: weasyprint; font-size: 2px; line-height: 1}
div {float: right; orphans: 1}
td {break-inside: avoid}
</style>
<table><tbody>
<tr><td>abc</td></tr>
<tr><td>abc</td></tr>
<tr><td>def <div>f<br>g</div> ghi</td></tr>
</tbody></table>
''')
html, = page1.children
body, = html.children
table_wrapper, = body.children
table, = table_wrapper.children
tbody, = table.children
for tr in tbody.children:
td, = tr.children
line, = td.children
textbox, = line.children
assert textbox.text == 'abc'
html, = page2.children
body, = html.children
table_wrapper, = body.children
table, = table_wrapper.children
tbody, = table.children
tr, = tbody.children
td, = tr.children
line1, line2 = td.children
textbox, div = line1.children
assert textbox.text == 'def '
textbox, = line2.children
assert textbox.text == 'ghi'
line1, line2 = div.children
textbox, br = line1.children
assert textbox.text == 'f'
textbox, = line2.children
assert textbox.text == 'g'
|