-
-
Notifications
You must be signed in to change notification settings - Fork 644
/
test_page.py
1384 lines (1251 loc) · 39.3 KB
/
test_page.py
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
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
weasyprint.tests.layout.page
----------------------------
Tests for pages layout.
"""
import pytest
from weasyprint.formatting_structure import boxes
from ..testing_utils import assert_no_logs, render_pages
@assert_no_logs
@pytest.mark.parametrize('size, width, height', (
('auto', 793, 1122),
('2in 10in', 192, 960),
('242px', 242, 242),
('letter', 816, 1056),
('letter portrait', 816, 1056),
('letter landscape', 1056, 816),
('portrait', 793, 1122),
('landscape', 1122, 793),
))
def test_page_size_basic(size, width, height):
"""Test the layout for ``@page`` properties."""
page, = render_pages('<style>@page { size: %s; }</style>' % size)
assert int(page.margin_width()) == width
assert int(page.margin_height()) == height
@assert_no_logs
def test_page_size_with_margin():
page, = render_pages('''<style>
@page { size: 200px 300px; margin: 10px 10% 20% 1in }
body { margin: 8px }
</style>
<p style="margin: 0">''')
assert page.margin_width() == 200
assert page.margin_height() == 300
assert page.position_x == 0
assert page.position_y == 0
assert page.width == 84 # 200px - 10% - 1 inch
assert page.height == 230 # 300px - 10px - 20%
html, = page.children
assert html.element_tag == 'html'
assert html.position_x == 96 # 1in
assert html.position_y == 10 # root element’s margins do not collapse
assert html.width == 84
body, = html.children
assert body.element_tag == 'body'
assert body.position_x == 96 # 1in
assert body.position_y == 10
# body has margins in the UA stylesheet
assert body.margin_left == 8
assert body.margin_right == 8
assert body.margin_top == 8
assert body.margin_bottom == 8
assert body.width == 68
paragraph, = body.children
assert paragraph.element_tag == 'p'
assert paragraph.position_x == 104 # 1in + 8px
assert paragraph.position_y == 18 # 10px + 8px
assert paragraph.width == 68
@assert_no_logs
def test_page_size_with_margin_border_padding():
page, = render_pages('''<style> @page {
size: 100px; margin: 1px 2px; padding: 4px 8px;
border-width: 16px 32px; border-style: solid;
}</style>''')
assert page.width == 16 # 100 - 2 * 42
assert page.height == 58 # 100 - 2 * 21
html, = page.children
assert html.element_tag == 'html'
assert html.position_x == 42 # 2 + 8 + 32
assert html.position_y == 21 # 1 + 4 + 16
@assert_no_logs
@pytest.mark.parametrize('margin, top, right, bottom, left', (
('auto', 15, 10, 15, 10),
('5px 5px auto auto', 5, 5, 25, 15),
))
def test_page_size_margins(margin, top, right, bottom, left):
page, = render_pages('''<style>@page {
size: 106px 206px; width: 80px; height: 170px;
padding: 1px; border: 2px solid; margin: %s }</style>''' % margin)
assert page.margin_top == top
assert page.margin_right == right
assert page.margin_bottom == bottom
assert page.margin_left == left
@assert_no_logs
@pytest.mark.parametrize('style, width, height', (
('size: 4px 10000px; width: 100px; height: 100px;'
'padding: 1px; border: 2px solid; margin: 3px',
112, 112),
('size: 1000px; margin: 100px; max-width: 500px; min-height: 1500px',
700, 1700),
('size: 1000px; margin: 100px; min-width: 1500px; max-height: 500px',
1700, 700),
))
def test_page_size_over_constrained(style, width, height):
page, = render_pages('<style>@page { %s }</style>' % style)
assert page.margin_width() == width
assert page.margin_height() == height
@assert_no_logs
@pytest.mark.parametrize('html', (
'<div>1</div>',
'<div></div>',
'<img src=pattern.png>'
))
def test_page_breaks(html):
pages = render_pages('''
<style>
@page { size: 100px; margin: 10px }
body { margin: 0 }
div { height: 30px; font-size: 20px }
img { height: 30px; display: block }
</style>
%s''' % (5 * html))
page_children = []
for page in pages:
html, = page.children
body, = html.children
children = body.children
assert all([child.element_tag in ('div', 'img') for child in children])
assert all([child.position_x == 10 for child in children])
page_children.append(children)
assert [
[child.position_y for child in page_child]
for page_child in page_children] == [[10, 40], [10, 40], [10]]
@assert_no_logs
def test_page_breaks_complex_1():
page_1, page_2, page_3, page_4 = render_pages('''
<style>
@page { margin: 10px }
@page :left { margin-left: 50px }
@page :right { margin-right: 50px }
html { page-break-before: left }
div { page-break-after: left }
ul { page-break-before: always }
</style>
<div>1</div>
<p>2</p>
<p>3</p>
<article>
<section>
<ul><li>4</li></ul>
</section>
</article>
''')
# The first page is a right page on rtl, but not here because of
# page-break-before on the root element.
assert page_1.margin_left == 50 # left page
assert page_1.margin_right == 10
html, = page_1.children
body, = html.children
div, = body.children
line, = div.children
text, = line.children
assert div.element_tag == 'div'
assert text.text == '1'
html, = page_2.children
assert page_2.margin_left == 10
assert page_2.margin_right == 50 # right page
assert not html.children # empty page to get to a left page
assert page_3.margin_left == 50 # left page
assert page_3.margin_right == 10
html, = page_3.children
body, = html.children
p_1, p_2 = body.children
assert p_1.element_tag == 'p'
assert p_2.element_tag == 'p'
assert page_4.margin_left == 10
assert page_4.margin_right == 50 # right page
html, = page_4.children
body, = html.children
article, = body.children
section, = article.children
ulist, = section.children
assert ulist.element_tag == 'ul'
@assert_no_logs
def test_page_breaks_complex_2():
# Reference for the following test:
# Without any 'avoid', this breaks after the <div>
page_1, page_2 = render_pages('''
<style>
@page { size: 140px; margin: 0 }
img { height: 25px; vertical-align: top }
p { orphans: 1; widows: 1 }
</style>
<img src=pattern.png>
<div>
<p><img src=pattern.png><br/><img src=pattern.png><p>
<p><img src=pattern.png><br/><img src=pattern.png><p>
</div><!-- page break here -->
<img src=pattern.png>
''')
html, = page_1.children
body, = html.children
img_1, div = body.children
assert img_1.position_y == 0
assert img_1.height == 25
assert div.position_y == 25
assert div.height == 100
html, = page_2.children
body, = html.children
img_2, = body.children
assert img_2.position_y == 0
assert img_2.height == 25
@assert_no_logs
def test_page_breaks_complex_3():
# Adding a few page-break-*: avoid, the only legal break is
# before the <div>
page_1, page_2 = render_pages('''
<style>
@page { size: 140px; margin: 0 }
img { height: 25px; vertical-align: top }
p { orphans: 1; widows: 1 }
</style>
<img src=pattern.png><!-- page break here -->
<div>
<p style="page-break-inside: avoid">
<img src=pattern.png><br/><img src=pattern.png></p>
<p style="page-break-before: avoid; page-break-after: avoid; widows: 2"
><img src=pattern.png><br/><img src=pattern.png></p>
</div>
<img src=pattern.png>
''')
html, = page_1.children
body, = html.children
img_1, = body.children
assert img_1.position_y == 0
assert img_1.height == 25
html, = page_2.children
body, = html.children
div, img_2 = body.children
assert div.position_y == 0
assert div.height == 100
assert img_2.position_y == 100
assert img_2.height == 25
@assert_no_logs
def test_page_breaks_complex_4():
page_1, page_2 = render_pages('''
<style>
@page { size: 140px; margin: 0 }
img { height: 25px; vertical-align: top }
p { orphans: 1; widows: 1 }
</style>
<img src=pattern.png><!-- page break here -->
<div>
<div>
<p style="page-break-inside: avoid">
<img src=pattern.png><br/><img src=pattern.png></p>
<p style="page-break-before:avoid; page-break-after:avoid; widows:2"
><img src=pattern.png><br/><img src=pattern.png></p>
</div>
<img src=pattern.png>
</div>
''')
html, = page_1.children
body, = html.children
img_1, = body.children
assert img_1.position_y == 0
assert img_1.height == 25
html, = page_2.children
body, = html.children
outer_div, = body.children
inner_div, img_2 = outer_div.children
assert inner_div.position_y == 0
assert inner_div.height == 100
assert img_2.position_y == 100
assert img_2.height == 25
@assert_no_logs
def test_page_breaks_complex_5():
# Reference for the next test
page_1, page_2, page_3 = render_pages('''
<style>
@page { size: 100px; margin: 0 }
img { height: 30px; display: block; }
p { orphans: 1; widows: 1 }
</style>
<div>
<img src=pattern.png style="page-break-after: always">
<section>
<img src=pattern.png>
<img src=pattern.png>
</section>
</div>
<img src=pattern.png><!-- page break here -->
<img src=pattern.png>
''')
html, = page_1.children
body, = html.children
div, = body.children
assert div.height == 100
html, = page_2.children
body, = html.children
div, img_4 = body.children
assert div.height == 60
assert img_4.height == 30
html, = page_3.children
body, = html.children
img_5, = body.children
assert img_5.height == 30
@assert_no_logs
def test_page_breaks_complex_6():
page_1, page_2, page_3 = render_pages('''
<style>
@page { size: 100px; margin: 0 }
img { height: 30px; display: block; }
p { orphans: 1; widows: 1 }
</style>
<div>
<img src=pattern.png style="page-break-after: always">
<section>
<img src=pattern.png><!-- page break here -->
<img src=pattern.png style="page-break-after: avoid">
</section>
</div>
<img src=pattern.png style="page-break-after: avoid">
<img src=pattern.png>
''')
html, = page_1.children
body, = html.children
div, = body.children
assert div.height == 100
html, = page_2.children
body, = html.children
div, = body.children
section, = div.children
img_2, = section.children
assert img_2.height == 30
# TODO: currently this is 60: we do not increase the used height of blocks
# to make them fill the blank space at the end of the age when we remove
# children from them for some break-*: avoid.
# See TODOs in blocks.block_container_layout
# assert div.height == 100
html, = page_3.children
body, = html.children
div, img_4, img_5, = body.children
assert div.height == 30
assert img_4.height == 30
assert img_5.height == 30
@assert_no_logs
def test_page_breaks_complex_7():
page_1, page_2, page_3 = render_pages('''
<style>
@page { @bottom-center { content: counter(page) } }
@page:blank { @bottom-center { content: none } }
</style>
<p style="page-break-after: right">foo</p>
<p>bar</p>
''')
assert len(page_1.children) == 2 # content and @bottom-center
assert len(page_2.children) == 1 # content only
assert len(page_3.children) == 2 # content and @bottom-center
@assert_no_logs
def test_page_breaks_complex_8():
page_1, page_2 = render_pages('''
<style>
@page { size: 75px; margin: 0 }
div { height: 20px }
</style>
<div></div>
<section>
<div></div>
<div style="page-break-after: avoid">
<div style="position: absolute"></div>
<div style="position: fixed"></div>
</div>
</section>
<div></div>
''')
html, = page_1.children
body, _div = html.children
div_1, section = body.children
div_2, = section.children
assert div_1.position_y == 0
assert div_2.position_y == 20
assert div_1.height == 20
assert div_2.height == 20
html, = page_2.children
body, = html.children
section, div_4 = body.children
div_3, = section.children
absolute, fixed = div_3.children
assert div_3.position_y == 0
assert div_4.position_y == 20
assert div_3.height == 20
assert div_4.height == 20
@assert_no_logs
@pytest.mark.parametrize('break_after, margin_break, margin_top', (
('page', 'auto', 5),
('auto', 'auto', 0),
('page', 'keep', 5),
('auto', 'keep', 5),
('page', 'discard', 0),
('auto', 'discard', 0),
))
def test_margin_break(break_after, margin_break, margin_top):
page_1, page_2 = render_pages('''
<style>
@page { size: 70px; margin: 0 }
div { height: 63px; margin: 5px 0 8px;
break-after: %s; margin-break: %s }
</style>
<section>
<div></div>
</section>
<section>
<div></div>
</section>
''' % (break_after, margin_break))
html, = page_1.children
body, = html.children
section, = body.children
div, = section.children
assert div.margin_top == 5
html, = page_2.children
body, = html.children
section, = body.children
div, = section.children
assert div.margin_top == margin_top
@pytest.mark.xfail
@assert_no_logs
def test_margin_break_clearance():
page_1, page_2 = render_pages('''
<style>
@page { size: 70px; margin: 0 }
div { height: 63px; margin: 5px 0 8px; break-after: page }
</style>
<section>
<div></div>
</section>
<section>
<div style="border-top: 1px solid black">
<div></div>
</div>
</section>
''')
html, = page_1.children
body, = html.children
section, = body.children
div, = section.children
assert div.margin_top == 5
html, = page_2.children
body, = html.children
section, = body.children
div_1, = section.children
assert div_1.margin_top == 0
div_2, = div_1.children
assert div_2.margin_top == 5
assert div_2.content_box_y() == 5
@assert_no_logs
@pytest.mark.parametrize('direction, page_break, pages_number', (
('ltr', 'recto', 3),
('ltr', 'verso', 2),
('rtl', 'recto', 3),
('rtl', 'verso', 2),
('ltr', 'right', 3),
('ltr', 'left', 2),
('rtl', 'right', 2),
('rtl', 'left', 3),
))
def test_recto_verso_break(direction, page_break, pages_number):
pages = render_pages('''
<style>
html { direction: %s }
p { break-before: %s }
</style>
abc
<p>def</p>
''' % (direction, page_break))
assert len(pages) == pages_number
@assert_no_logs
def test_page_names_1():
pages = render_pages('''
<style>
@page { size: 100px 100px }
section { page: small }
</style>
<div>
<section>large</section>
</div>
''')
page1, = pages
assert (page1.width, page1.height) == (100, 100)
@assert_no_logs
def test_page_names_2():
pages = render_pages('''
<style>
@page { size: 100px 100px }
@page narrow { margin: 1px }
section { page: small }
</style>
<div>
<section>large</section>
</div>
''')
page1, = pages
assert (page1.width, page1.height) == (100, 100)
@assert_no_logs
def test_page_names_3():
pages = render_pages('''
<style>
@page { margin: 0 }
@page narrow { size: 100px 200px }
@page large { size: 200px 100px }
div { page: narrow }
section { page: large }
</style>
<div>
<section>large</section>
<section>large</section>
<p>narrow</p>
</div>
''')
page1, page2 = pages
assert (page1.width, page1.height) == (200, 100)
html, = page1.children
body, = html.children
div, = body.children
section1, section2 = div.children
assert section1.element_tag == section2.element_tag == 'section'
assert (page2.width, page2.height) == (100, 200)
html, = page2.children
body, = html.children
div, = body.children
p, = div.children
assert p.element_tag == 'p'
@assert_no_logs
def test_page_names_4():
pages = render_pages('''
<style>
@page { size: 200px 200px; margin: 0 }
@page small { size: 100px 100px }
p { page: small }
</style>
<section>normal</section>
<section>normal</section>
<p>small</p>
<section>small</section>
''')
page1, page2 = pages
assert (page1.width, page1.height) == (200, 200)
html, = page1.children
body, = html.children
section1, section2 = body.children
assert section1.element_tag == section2.element_tag == 'section'
assert (page2.width, page2.height) == (100, 100)
html, = page2.children
body, = html.children
p, section = body.children
assert p.element_tag == 'p'
assert section.element_tag == 'section'
@assert_no_logs
def test_page_names_5():
pages = render_pages('''
<style>
@page { size: 200px 200px; margin: 0 }
@page small { size: 100px 100px }
div { page: small }
</style>
<section><p>a</p>b</section>
<section>c<div>d</div></section>
''')
page1, page2 = pages
assert (page1.width, page1.height) == (200, 200)
html, = page1.children
body, = html.children
section1, section2 = body.children
assert section1.element_tag == section2.element_tag == 'section'
p, line = section1.children
line, = section2.children
assert (page2.width, page2.height) == (100, 100)
html, = page2.children
body, = html.children
section2, = body.children
div, = section2.children
@assert_no_logs
def test_page_names_6():
pages = render_pages('''
<style>
@page { margin: 0 }
@page large { size: 200px 200px }
@page small { size: 100px 100px }
section { page: large }
div { page: small }
</style>
<section>a<p>b</p>c</section>
<section>d<div>e</div>f</section>
''')
page1, page2, page3 = pages
assert (page1.width, page1.height) == (200, 200)
html, = page1.children
body, = html.children
section1, section2 = body.children
assert section1.element_tag == section2.element_tag == 'section'
line1, p, line2 = section1.children
line, = section2.children
assert (page2.width, page2.height) == (100, 100)
html, = page2.children
body, = html.children
section2, = body.children
div, = section2.children
assert (page3.width, page3.height) == (200, 200)
html, = page3.children
body, = html.children
section2, = body.children
line, = section2.children
@assert_no_logs
def test_page_names_7():
pages = render_pages('''
<style>
@page { size: 200px 200px; margin: 0 }
@page small { size: 100px 100px }
p { page: small; break-before: right }
</style>
<section>normal</section>
<section>normal</section>
<p>small</p>
<section>small</section>
''')
page1, page2, page3 = pages
assert (page1.width, page1.height) == (200, 200)
html, = page1.children
body, = html.children
section1, section2 = body.children
assert section1.element_tag == section2.element_tag == 'section'
assert (page2.width, page2.height) == (200, 200)
html, = page2.children
assert not html.children
assert (page3.width, page3.height) == (100, 100)
html, = page3.children
body, = html.children
p, section = body.children
assert p.element_tag == 'p'
assert section.element_tag == 'section'
@assert_no_logs
def test_page_names_8():
pages = render_pages('''
<style>
@page small { size: 100px 100px }
section { page: small }
p { line-height: 80px }
</style>
<section>
<p>small</p>
<p>small</p>
</section>
''')
page1, page2 = pages
assert (page1.width, page1.height) == (100, 100)
html, = page1.children
body, = html.children
section, = body.children
p, = section.children
assert section.element_tag == 'section'
assert p.element_tag == 'p'
assert (page2.width, page2.height) == (100, 100)
html, = page2.children
body, = html.children
section, = body.children
p, = section.children
assert section.element_tag == 'section'
assert p.element_tag == 'p'
@assert_no_logs
def test_page_names_9():
pages = render_pages('''
<style>
@page { size: 200px 200px }
@page small { size: 100px 100px }
section { break-after: page; page: small }
article { page: small }
</style>
<section>
<div>big</div>
<div>big</div>
</section>
<article>
<div>small</div>
<div>small</div>
</article>
''')
page1, page2, = pages
assert (page1.width, page1.height) == (100, 100)
html, = page1.children
body, = html.children
section, = body.children
assert section.element_tag == 'section'
assert (page2.width, page2.height) == (100, 100)
html, = page2.children
body, = html.children
article, = body.children
assert article.element_tag == 'article'
@assert_no_logs
@pytest.mark.parametrize('style, line_counts', (
('orphans: 2; widows: 2', [4, 3]),
('orphans: 5; widows: 2', [0, 7]),
('orphans: 2; widows: 4', [3, 4]),
('orphans: 4; widows: 4', [0, 7]),
('orphans: 2; widows: 2; page-break-inside: avoid', [0, 7]),
))
def test_orphans_widows_avoid(style, line_counts):
pages = render_pages('''
<style>
@page { size: 200px }
h1 { height: 120px }
p { line-height: 20px;
width: 1px; /* line break at each word */
%s }
</style>
<h1>Tasty test</h1>
<!-- There is room for 4 lines after h1 on the fist page -->
<p>one two three four five six seven</p>
''' % style)
for i, page in enumerate(pages):
html, = page.children
body, = html.children
body_children = body.children if i else body.children[1:] # skip h1
count = len(body_children[0].children) if body_children else 0
assert line_counts.pop(0) == count
assert not line_counts
@assert_no_logs
def test_page_and_linebox_breaking():
# Empty <span/> tests a corner case in skip_first_whitespace()
pages = render_pages('''
<style>
@font-face { src: url(weasyprint.otf); font-family: weasyprint }
@page { size: 100px; margin: 2px; border: 1px solid }
body { margin: 0 }
div { font-family: weasyprint; font-size: 20px }
</style>
<div><span/>1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15</div>
''')
texts = []
for page in pages:
html, = page.children
body, = html.children
div, = body.children
lines = div.children
for line in lines:
line_texts = []
for child in line.descendants():
if isinstance(child, boxes.TextBox):
line_texts.append(child.text)
texts.append(''.join(line_texts))
assert len(pages) == 4
assert ''.join(texts) == '1,2,3,4,5,6,7,8,9,10,11,12,13,14,15'
@assert_no_logs
def test_margin_boxes_fixed_dimension_1():
# Corner boxes
page, = render_pages('''
<style>
@page {
@top-left-corner {
content: 'top_left';
padding: 10px;
}
@top-right-corner {
content: 'top_right';
padding: 10px;
}
@bottom-left-corner {
content: 'bottom_left';
padding: 10px;
}
@bottom-right-corner {
content: 'bottom_right';
padding: 10px;
}
size: 1000px;
margin-top: 10%;
margin-bottom: 40%;
margin-left: 20%;
margin-right: 30%;
}
</style>
''')
html, top_left, top_right, bottom_left, bottom_right = page.children
for margin_box, text in zip(
[top_left, top_right, bottom_left, bottom_right],
['top_left', 'top_right', 'bottom_left', 'bottom_right']):
line, = margin_box.children
text, = line.children
assert text == text
# Check positioning and Rule 1 for fixed dimensions
assert top_left.position_x == 0
assert top_left.position_y == 0
assert top_left.margin_width() == 200 # margin-left
assert top_left.margin_height() == 100 # margin-top
assert top_right.position_x == 700 # size-x - margin-right
assert top_right.position_y == 0
assert top_right.margin_width() == 300 # margin-right
assert top_right.margin_height() == 100 # margin-top
assert bottom_left.position_x == 0
assert bottom_left.position_y == 600 # size-y - margin-bottom
assert bottom_left.margin_width() == 200 # margin-left
assert bottom_left.margin_height() == 400 # margin-bottom
assert bottom_right.position_x == 700 # size-x - margin-right
assert bottom_right.position_y == 600 # size-y - margin-bottom
assert bottom_right.margin_width() == 300 # margin-right
assert bottom_right.margin_height() == 400 # margin-bottom
@assert_no_logs
def test_margin_boxes_fixed_dimension_2():
# Test rules 2 and 3
page, = render_pages('''
<style>
@page {
margin: 100px 200px;
@bottom-left-corner { content: ""; margin: 60px }
}
</style>
''')
html, margin_box = page.children
assert margin_box.margin_width() == 200
assert margin_box.margin_left == 60
assert margin_box.margin_right == 60
assert margin_box.width == 80 # 200 - 60 - 60
assert margin_box.margin_height() == 100
# total was too big, the outside margin was ignored:
assert margin_box.margin_top == 60
assert margin_box.margin_bottom == 40 # Not 60
assert margin_box.height == 0 # But not negative
@assert_no_logs
def test_margin_boxes_fixed_dimension_3():
# Test rule 3 with a non-auto inner dimension
page, = render_pages('''
<style>
@page {
margin: 100px;
@left-middle { content: ""; margin: 10px; width: 130px }
}
</style>
''')
html, margin_box = page.children
assert margin_box.margin_width() == 100
assert margin_box.margin_left == -40 # Not 10px
assert margin_box.margin_right == 10
assert margin_box.width == 130 # As specified
@assert_no_logs
def test_margin_boxes_fixed_dimension_4():
# Test rule 4
page, = render_pages('''
<style>
@page {
margin: 100px;
@left-bottom {
content: "";
margin-left: 10px;
margin-right: auto;
width: 70px;
}
}
</style>
''')
html, margin_box = page.children
assert margin_box.margin_width() == 100
assert margin_box.margin_left == 10 # 10px this time, no over-constrain
assert margin_box.margin_right == 20
assert margin_box.width == 70 # As specified
@assert_no_logs
def test_margin_boxes_fixed_dimension_5():
# Test rules 2, 3 and 4
page, = render_pages('''
<style>
@page {
margin: 100px;
@right-top {
content: "";
margin-right: 10px;
margin-left: auto;
width: 130px;
}
}
</style>
''')
html, margin_box = page.children
assert margin_box.margin_width() == 100
assert margin_box.margin_left == 0 # rule 2
assert margin_box.margin_right == -30 # rule 3, after rule 2
assert margin_box.width == 130 # As specified
@assert_no_logs
def test_margin_boxes_fixed_dimension_6():
# Test rule 5
page, = render_pages('''
<style>
@page {
margin: 100px;
@top-left { content: ""; margin-top: 10px; margin-bottom: auto }
}
</style>
''')
html, margin_box = page.children
assert margin_box.margin_height() == 100
assert margin_box.margin_top == 10
assert margin_box.margin_bottom == 0
assert margin_box.height == 90
@assert_no_logs
def test_margin_boxes_fixed_dimension_7():