File: test_block.py

package info (click to toggle)
weasyprint 67.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,896 kB
  • sloc: python: 61,025; makefile: 12
file content (1098 lines) | stat: -rw-r--r-- 32,581 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
"""Tests for blocks layout."""

import pytest

from weasyprint.formatting_structure import boxes

from ..testing_utils import assert_no_logs, render_pages


@assert_no_logs
def test_block_widths():
    page, = render_pages('''
      <style>
        @page { margin: 0; size: 120px 2000px }
        body { margin: 0 }
        div { margin: 10px }
        p { padding: 2px; border-width: 1px; border-style: solid }
      </style>
      <div>
        <p></p>
        <p style="width: 50px"></p>
      </div>
      <div style="direction: rtl">
        <p style="width: 50px; direction: rtl"></p>
      </div>
      <div>
        <p style="margin: 0 10px 0 20px"></p>
        <p style="width: 50px; margin-left: 20px; margin-right: auto"></p>
        <p style="width: 50px; margin-left: auto; margin-right: 20px"></p>
        <p style="width: 50px; margin: auto"></p>

        <p style="margin-left: 20px; margin-right: auto"></p>
        <p style="margin-left: auto; margin-right: 20px"></p>
        <p style="margin: auto"></p>

        <p style="width: 200px; margin: auto"></p>

        <p style="min-width: 200px; margin: auto"></p>
        <p style="max-width: 50px; margin: auto"></p>
        <p style="min-width: 50px; margin: auto"></p>

        <p style="width: 70%"></p>
      </div>
    ''')
    html, = page.children
    assert html.element_tag == 'html'
    body, = html.children
    assert body.element_tag == 'body'
    assert body.width == 120

    divs = body.children

    paragraphs = []
    for div in divs:
        assert isinstance(div, boxes.BlockBox)
        assert div.element_tag == 'div'
        assert div.width == 100
        for paragraph in div.children:
            assert isinstance(paragraph, boxes.BlockBox)
            assert paragraph.element_tag == 'p'
            assert paragraph.padding_left == 2
            assert paragraph.padding_right == 2
            assert paragraph.border_left_width == 1
            assert paragraph.border_right_width == 1
            paragraphs.append(paragraph)

    assert len(paragraphs) == 15

    # width is 'auto'
    assert paragraphs[0].width == 94
    assert paragraphs[0].margin_left == 0
    assert paragraphs[0].margin_right == 0

    # No 'auto', over-constrained equation with ltr, the initial
    # 'margin-right: 0' was ignored.
    assert paragraphs[1].width == 50
    assert paragraphs[1].margin_left == 0

    # No 'auto', over-constrained equation with rtl, the initial
    # 'margin-left: 0' was ignored.
    assert paragraphs[2].width == 50
    assert paragraphs[2].margin_right == 0

    # width is 'auto'
    assert paragraphs[3].width == 64
    assert paragraphs[3].margin_left == 20

    # margin-right is 'auto'
    assert paragraphs[4].width == 50
    assert paragraphs[4].margin_left == 20

    # margin-left is 'auto'
    assert paragraphs[5].width == 50
    assert paragraphs[5].margin_left == 24

    # Both margins are 'auto', remaining space is split in half
    assert paragraphs[6].width == 50
    assert paragraphs[6].margin_left == 22

    # width is 'auto', other 'auto' are set to 0
    assert paragraphs[7].width == 74
    assert paragraphs[7].margin_left == 20

    # width is 'auto', other 'auto' are set to 0
    assert paragraphs[8].width == 74
    assert paragraphs[8].margin_left == 0

    # width is 'auto', other 'auto' are set to 0
    assert paragraphs[9].width == 94
    assert paragraphs[9].margin_left == 0

    # sum of non-auto initially is too wide, set auto values to 0
    assert paragraphs[10].width == 200
    assert paragraphs[10].margin_left == 0

    # Constrained by min-width, same as above
    assert paragraphs[11].width == 200
    assert paragraphs[11].margin_left == 0

    # Constrained by max-width, same as paragraphs[6]
    assert paragraphs[12].width == 50
    assert paragraphs[12].margin_left == 22

    # NOT constrained by min-width
    assert paragraphs[13].width == 94
    assert paragraphs[13].margin_left == 0

    # 70%
    assert paragraphs[14].width == 70
    assert paragraphs[14].margin_left == 0


@assert_no_logs
def test_block_heights_p():
    page, = render_pages('''
      <style>
        @page { margin: 0; size: 100px 20000px }
        html, body { margin: 0 }
        div { margin: 4px; border: 2px solid; padding: 4px }
        /* Use top margins so that margin collapsing doesn't change result */
        p { margin: 16px 0 0; border: 4px solid; padding: 8px; height: 50px }
      </style>
      <div>
        <p></p>
        <!-- Not in normal flow: don't contribute to the parent’s height -->
        <p style="position: absolute"></p>
        <p style="float: left"></p>
      </div>
      <div> <p></p> <p></p> <p></p> </div>
      <div style="height: 20px"> <p></p> </div>
      <div style="height: 120px"> <p></p> </div>
      <div style="max-height: 20px"> <p></p> </div>
      <div style="min-height: 120px"> <p></p> </div>
      <div style="min-height: 20px"> <p></p> </div>
      <div style="max-height: 120px"> <p></p> </div>
    ''')
    html, = page.children
    body, = html.children
    heights = [div.height for div in body.children]
    assert heights == [90, 90 * 3, 20, 120, 20, 120, 90, 90]


@assert_no_logs
def test_block_heights_img():
    page, = render_pages('''
      <style>
        body { height: 200px; font-size: 0 }
      </style>
      <div>
        <img src=pattern.png style="height: 40px">
      </div>
      <div style="height: 10%">
        <img src=pattern.png style="height: 40px">
      </div>
      <div style="max-height: 20px">
        <img src=pattern.png style="height: 40px">
      </div>
      <div style="max-height: 10%">
        <img src=pattern.png style="height: 40px">
      </div>
      <div style="min-height: 20px"></div>
      <div style="min-height: 10%"></div>
    ''')
    html, = page.children
    body, = html.children
    heights = [div.height for div in body.children]
    assert heights == [40, 20, 20, 20, 20, 20]


@assert_no_logs
def test_block_heights_img_no_body_height():
    # Same but with no height on body: percentage *-height is ignored
    page, = render_pages('''
      <style>
        body { font-size: 0 }
      </style>
        <div>
          <img src=pattern.png style="height: 40px">
        </div>
        <div style="height: 10%">
          <img src=pattern.png style="height: 40px">
        </div>
        <div style="max-height: 20px">
          <img src=pattern.png style="height: 40px">
        </div>
        <div style="max-height: 10%">
          <img src=pattern.png style="height: 40px">
        </div>
        <div style="min-height: 20px"></div>
        <div style="min-height: 10%"></div>
    ''')
    html, = page.children
    body, = html.children
    heights = [div.height for div in body.children]
    assert heights == [40, 40, 20, 40, 20, 0]


@assert_no_logs
def test_block_percentage_heights_no_html_height():
    page, = render_pages('''
      <style>
        html, body { margin: 0 }
        body { height: 50% }
      </style>
    ''')
    html, = page.children
    assert html.element_tag == 'html'
    body, = html.children
    assert body.element_tag == 'body'

    # Since html’s height depend on body’s, body’s 50% means 'auto'
    assert body.height == 0


@assert_no_logs
def test_block_percentage_heights():
    page, = render_pages('''
      <style>
        html, body { margin: 0 }
        html { height: 300px }
        body { height: 50% }
      </style>
    ''')
    html, = page.children
    assert html.element_tag == 'html'
    body, = html.children
    assert body.element_tag == 'body'

    # This time the percentage makes sense
    assert body.height == 150


@assert_no_logs
@pytest.mark.parametrize('size', [
    ('width: 10%; height: 1000px',),
    ('max-width: 10%; max-height: 1000px; height: 2000px',),
    ('width: 5%; min-width: 10%; min-height: 1000px',),
    ('width: 10%; height: 1000px; min-width: auto; max-height: none',),
])
def test_box_sizing(size):
    # https://www.w3.org/TR/css-ui-3/#box-sizing
    page, = render_pages('''
      <style>
        @page { size: 100000px }
        body { width: 10000px; margin: 0 }
        div { %s; margin: 100px; padding: 10px; border: 1px solid }
      </style>
      <div></div>

      <div style="box-sizing: content-box"></div>
      <div style="box-sizing: padding-box"></div>
      <div style="box-sizing: border-box"></div>
    ''' % size)
    html, = page.children
    body, = html.children
    div_1, div_2, div_3, div_4 = body.children
    for div in div_1, div_2:
        assert div.style['box_sizing'] == 'content-box'
        assert div.width == 1000
        assert div.height == 1000
        assert div.padding_width() == 1020
        assert div.padding_height() == 1020
        assert div.border_width() == 1022
        assert div.border_height() == 1022
        assert div.margin_height() == 1222
        # margin_width() is the width of the containing block

    # padding-box
    assert div_3.style['box_sizing'] == 'padding-box'
    assert div_3.width == 980  # 1000 - 20
    assert div_3.height == 980
    assert div_3.padding_width() == 1000
    assert div_3.padding_height() == 1000
    assert div_3.border_width() == 1002
    assert div_3.border_height() == 1002
    assert div_3.margin_height() == 1202

    # border-box
    assert div_4.style['box_sizing'] == 'border-box'
    assert div_4.width == 978  # 1000 - 20 - 2
    assert div_4.height == 978
    assert div_4.padding_width() == 998
    assert div_4.padding_height() == 998
    assert div_4.border_width() == 1000
    assert div_4.border_height() == 1000
    assert div_4.margin_height() == 1200


@assert_no_logs
@pytest.mark.parametrize('size', [
    ('width: 0; height: 0'),
    ('max-width: 0; max-height: 0'),
    ('min-width: 0; min-height: 0; width: 0; height: 0'),
])
def test_box_sizing_zero(size):
    # https://www.w3.org/TR/css-ui-3/#box-sizing
    page, = render_pages('''
      <style>
        @page { size: 100000px }
        body { width: 10000px; margin: 0 }
        div { %s; margin: 100px; padding: 10px; border: 1px solid }
      </style>
      <div></div>

      <div style="box-sizing: content-box"></div>
      <div style="box-sizing: padding-box"></div>
      <div style="box-sizing: border-box"></div>
    ''' % size)
    html, = page.children
    body, = html.children
    for div in body.children:
        assert div.width == 0
        assert div.height == 0
        assert div.padding_width() == 20
        assert div.padding_height() == 20
        assert div.border_width() == 22
        assert div.border_height() == 22
        assert div.margin_height() == 222
        # margin_width() is the width of the containing block


COLLAPSING = (
    ('10px', '15px', 15),  # not 25
    # "The maximum of the absolute values of the negative adjoining margins is
    # deducted from the maximum of the positive adjoining margins"
    ('-10px', '15px', 5),
    ('10px', '-15px', -5),
    ('-10px', '-15px', -15),
    ('10px', 'auto', 10),  # 'auto' is 0
)
NOT_COLLAPSING = (
    ('10px', '15px', 25),
    ('-10px', '15px', 5),
    ('10px', '-15px', -5),
    ('-10px', '-15px', -25),
    ('10px', 'auto', 10),  # 'auto' is 0
)


@pytest.mark.parametrize(('margin_1', 'margin_2', 'result'), COLLAPSING)
def test_vertical_space_1(margin_1, margin_2, result):
    # Siblings
    page, = render_pages('''
      <style>
        p { font: 20px/1 serif } /* block height == 20px */
        #p1 { margin-bottom: %s }
        #p2 { margin-top: %s }
      </style>
      <p id=p1>Lorem ipsum
      <p id=p2>dolor sit amet
    ''' % (margin_1, margin_2))
    html, = page.children
    body, = html.children
    p1, p2 = body.children
    p1_bottom = p1.content_box_y() + p1.height
    p2_top = p2.content_box_y()
    assert p2_top - p1_bottom == result


@pytest.mark.parametrize(('margin_1', 'margin_2', 'result'), COLLAPSING)
def test_vertical_space_2(margin_1, margin_2, result):
    # Not siblings, first is nested
    page, = render_pages('''
      <style>
        p { font: 20px/1 serif } /* block height == 20px */
        #p1 { margin-bottom: %s }
        #p2 { margin-top: %s }
      </style>
      <div>
        <p id=p1>Lorem ipsum
      </div>
      <p id=p2>dolor sit amet
    ''' % (margin_1, margin_2))
    html, = page.children
    body, = html.children
    div, p2 = body.children
    p1, = div.children
    p1_bottom = p1.content_box_y() + p1.height
    p2_top = p2.content_box_y()
    assert p2_top - p1_bottom == result


@pytest.mark.parametrize(('margin_1', 'margin_2', 'result'), COLLAPSING)
def test_vertical_space_3(margin_1, margin_2, result):
    # Not siblings, second is nested
    page, = render_pages('''
      <style>
        p { font: 20px/1 serif } /* block height == 20px */
        #p1 { margin-bottom: %s }
        #p2 { margin-top: %s }
      </style>
      <p id=p1>Lorem ipsum
      <div>
        <p id=p2>dolor sit amet
      </div>
    ''' % (margin_1, margin_2))
    html, = page.children
    body, = html.children
    p1, div = body.children
    p2, = div.children
    p1_bottom = p1.content_box_y() + p1.height
    p2_top = p2.content_box_y()
    assert p2_top - p1_bottom == result


@pytest.mark.parametrize(('margin_1', 'margin_2', 'result'), COLLAPSING)
def test_vertical_space_4(margin_1, margin_2, result):
    # Not siblings, second is doubly nested
    page, = render_pages('''
      <style>
        p { font: 20px/1 serif } /* block height == 20px */
        #p1 { margin-bottom: %s }
        #p2 { margin-top: %s }
      </style>
      <p id=p1>Lorem ipsum
      <div>
        <div>
            <p id=p2>dolor sit amet
        </div>
      </div>
    ''' % (margin_1, margin_2))
    html, = page.children
    body, = html.children
    p1, div1 = body.children
    div2, = div1.children
    p2, = div2.children
    p1_bottom = p1.content_box_y() + p1.height
    p2_top = p2.content_box_y()
    assert p2_top - p1_bottom == result


@pytest.mark.parametrize(('margin_1', 'margin_2', 'result'), COLLAPSING)
def test_vertical_space_5(margin_1, margin_2, result):
    # Collapsing with children
    page, = render_pages('''
      <style>
        p { font: 20px/1 serif } /* block height == 20px */
        #div1 { margin-top: %s }
        #div2 { margin-top: %s }
      </style>
      <p>Lorem ipsum
      <div id=div1>
        <div id=div2>
          <p id=p2>dolor sit amet
        </div>
      </div>
    ''' % (margin_1, margin_2))
    html, = page.children
    body, = html.children
    p1, div1 = body.children
    div2, = div1.children
    p2, = div2.children
    p1_bottom = p1.content_box_y() + p1.height
    p2_top = p2.content_box_y()
    # Parent and element edge are the same:
    assert div1.border_box_y() == p2.border_box_y()
    assert div2.border_box_y() == p2.border_box_y()
    assert p2_top - p1_bottom == result


@pytest.mark.parametrize(('margin_1', 'margin_2', 'result'), NOT_COLLAPSING)
def test_vertical_space_6(margin_1, margin_2, result):
    # Block formatting context: Not collapsing with children
    page, = render_pages('''
      <style>
        p { font: 20px/1 serif } /* block height == 20px */
        #div1 { margin-top: %s; overflow: hidden }
        #div2 { margin-top: %s }
      </style>
      <p>Lorem ipsum
      <div id=div1>
        <div id=div2>
          <p id=p2>dolor sit amet
        </div>
      </div>
    ''' % (margin_1, margin_2))
    html, = page.children
    body, = html.children
    p1, div1 = body.children
    div2, = div1.children
    p2, = div2.children
    p1_bottom = p1.content_box_y() + p1.height
    p2_top = p2.content_box_y()
    assert p2_top - p1_bottom == result


@pytest.mark.parametrize(('margin_1', 'margin_2', 'result'), COLLAPSING)
def test_vertical_space_7(margin_1, margin_2, result):
    # Collapsing through an empty div
    page, = render_pages('''
      <style>
        p { font: 20px/1 serif } /* block height == 20px */
        #p1 { margin-bottom: %s }
        #p2 { margin-top: %s }
        div { margin-bottom: %s; margin-top: %s }
      </style>
      <p id=p1>Lorem ipsum
      <div></div>
      <p id=p2>dolor sit amet
    ''' % (2 * (margin_1, margin_2)))
    html, = page.children
    body, = html.children
    p1, div, p2 = body.children
    p1_bottom = p1.content_box_y() + p1.height
    p2_top = p2.content_box_y()
    assert p2_top - p1_bottom == result


@pytest.mark.parametrize(('margin_1', 'margin_2', 'result'), NOT_COLLAPSING)
def test_vertical_space_8(margin_1, margin_2, result):
    # The root element does not collapse
    page, = render_pages('''
      <style>
        html { margin-top: %s }
        body { margin-top: %s }
      </style>
      <p>Lorem ipsum
    ''' % (margin_1, margin_2))
    html, = page.children
    body, = html.children
    p1, = body.children
    p1_top = p1.content_box_y()
    # Vertical space from y=0
    assert p1_top == result


@pytest.mark.parametrize(('margin_1', 'margin_2', 'result'), COLLAPSING)
def test_vertical_space_9(margin_1, margin_2, result):
    # <body> DOES collapse
    page, = render_pages('''
      <style>
        body { margin-top: %s }
        div { margin-top: %s }
      </style>
      <div>
        <p>Lorem ipsum
    ''' % (margin_1, margin_2))
    html, = page.children
    body, = html.children
    div, = body.children
    p1, = div.children
    p1_top = p1.content_box_y()
    # Vertical space from y=0
    assert p1_top == result


@assert_no_logs
def test_box_decoration_break_block_slice():
    # https://www.w3.org/TR/css-backgrounds-3/#the-box-decoration-break
    page_1, page_2 = render_pages('''
      <style>
        @page { size: 100px }
        p { padding: 2px; border: 3px solid; margin: 5px }
        img { display: block; height: 40px }
      </style>
      <p>
        <img src=pattern.png>
        <img src=pattern.png>
        <img src=pattern.png>
        <img src=pattern.png>''')
    html, = page_1.children
    body, = html.children
    paragraph, = body.children
    img_1, img_2 = paragraph.children
    assert paragraph.position_y == 0
    assert paragraph.margin_top == 5
    assert paragraph.border_top_width == 3
    assert paragraph.padding_top == 2
    assert paragraph.content_box_y() == 10
    assert img_1.position_y == 10
    assert img_2.position_y == 50
    assert paragraph.height == 90
    assert paragraph.margin_bottom == 0
    assert paragraph.border_bottom_width == 0
    assert paragraph.padding_bottom == 0
    assert paragraph.margin_height() == 100

    html, = page_2.children
    body, = html.children
    paragraph, = body.children
    img_1, img_2 = paragraph.children
    assert paragraph.position_y == 0
    assert paragraph.margin_top == 0
    assert paragraph.border_top_width == 0
    assert paragraph.padding_top == 0
    assert paragraph.content_box_y() == 0
    assert img_1.position_y == 0
    assert img_2.position_y == 40
    assert paragraph.height == 80
    assert paragraph.padding_bottom == 2
    assert paragraph.border_bottom_width == 3
    assert paragraph.margin_bottom == 5
    assert paragraph.margin_height() == 90


@assert_no_logs
def test_box_decoration_break_block_clone():
    # https://www.w3.org/TR/css-backgrounds-3/#the-box-decoration-break
    page_1, page_2 = render_pages('''
      <style>
        @page { size: 100px }
        p { padding: 2px; border: 3px solid; margin: 5px;
            box-decoration-break: clone }
        img { display: block; height: 40px }
      </style>
      <p>
        <img src=pattern.png>
        <img src=pattern.png>
        <img src=pattern.png>
        <img src=pattern.png>''')
    html, = page_1.children
    body, = html.children
    paragraph, = body.children
    img_1, img_2 = paragraph.children
    assert paragraph.position_y == 0
    assert paragraph.margin_top == 5
    assert paragraph.border_top_width == 3
    assert paragraph.padding_top == 2
    assert paragraph.content_box_y() == 10
    assert img_1.position_y == 10
    assert img_2.position_y == 50
    assert paragraph.height == 80
    # TODO: bottom margin should be 0
    # https://www.w3.org/TR/css-break-3/#valdef-box-decoration-break-clone
    # "Cloned margins are truncated on block-level boxes."
    # See issue #115.
    assert paragraph.margin_bottom == 5
    assert paragraph.border_bottom_width == 3
    assert paragraph.padding_bottom == 2
    assert paragraph.margin_height() == 100

    html, = page_2.children
    body, = html.children
    paragraph, = body.children
    img_1, img_2 = paragraph.children
    assert paragraph.position_y == 0
    assert paragraph.margin_top == 0
    assert paragraph.border_top_width == 3
    assert paragraph.padding_top == 2
    assert paragraph.content_box_y() == 5
    assert img_1.position_y == 5
    assert img_2.position_y == 45
    assert paragraph.height == 80
    assert paragraph.padding_bottom == 2
    assert paragraph.border_bottom_width == 3
    assert paragraph.margin_bottom == 5
    assert paragraph.margin_height() == 95


@assert_no_logs
def test_box_decoration_break_clone_bottom_padding():
    page_1, page_2 = render_pages('''
      <style>
        @page { size: 80px; margin: 0 }
        div { height: 20px }
        article { padding: 12px; box-decoration-break: clone }
      </style>
      <article>
        <div>a</div>
        <div>b</div>
        <div>c</div>
      </article>''')
    html, = page_1.children
    body, = html.children
    article, = body.children
    assert article.height == 80 - 2 * 12
    div_1, div_2 = article.children
    assert div_1.position_y == 12
    assert div_2.position_y == 12 + 20

    html, = page_2.children
    body, = html.children
    article, = body.children
    assert article.height == 20
    div, = article.children
    assert div.position_y == 12


@pytest.mark.xfail
@assert_no_logs
def test_box_decoration_break_slice_bottom_padding():  # pragma: no cover
    # Last div fits in first, but not article's padding. As it is impossible to
    # break between a parent and its last child, put last child on next page.
    # TODO: at the end of block_container_layout, we should check that the box
    # with its bottom border/padding doesn't cross the bottom line. If it does,
    # we should re-render the box with a bottom_space including the bottom
    # border/padding.
    page_1, page_2 = render_pages('''
      <style>
        @page { size: 80px; margin: 0 }
        div { height: 20px }
        article { padding: 12px; box-decoration-break: slice }
      </style>
      <article>
        <div>a</div>
        <div>b</div>
        <div>c</div>
      </article>''')
    html, = page_1.children
    body, = html.children
    article, = body.children
    assert article.height == 80 - 12
    div_1, div_2 = article.children
    assert div_1.position_y == 12
    assert div_2.position_y == 12 + 20

    html, = page_2.children
    body, = html.children
    article, = body.children
    assert article.height == 20
    div, = article.children
    assert div.position_y == 0


@pytest.mark.xfail
@assert_no_logs
def test_nested_blocks_padding_border():  # pragma: no cover
    # Same as previous issue, with nested blocks.
    page_1, page_2 = render_pages('''
      <style>
        @page { size: 4px 12px; margin: 0 }
        html { font: 2px/1 weasyprint }
        article, section, div {
          border-top: 1px solid; border-bottom: 1px solid;
          padding-top: 1px; padding-bottom: 1px;
        }
      </style>
      <article>
        <section>
          <div>
            aaa
            bbb
          </div>
        </section>
      </article>''')
    html, = page_1.children
    body, = html.children
    div_1, = body.children
    div_2, = div_1.children
    div_3, = div_2.children

    html, = page_2.children
    body, = html.children
    div_1, = body.children
    div_2, = div_1.children
    div_3, = div_2.children


@assert_no_logs
def test_overflow_non_collapsing_parent():
    page_1, page_2 = render_pages('''
      <style>
        @page { size: 6px 10px; margin: 0 }
        html { font: 2px/1 weasyprint }
        div { border-bottom: 3px solid }
        p { margin-bottom: 2px }
      </style>
      abc
      def
      <div>
        <p>
          aaa
        </p>
      </div>
      ghi''')
    html, = page_1.children
    body, = html.children
    lines, = body.children
    line_1, line_2 = lines.children

    html, = page_2.children
    body, = html.children
    section, lines = body.children


@assert_no_logs
def test_overflow_auto():
    page, = render_pages('''
      <article style="overflow: auto">
        <div style="float: left; height: 50px; margin: 10px">bla bla bla</div>
          toto toto''')
    html, = page.children
    body, = html.children
    article, = body.children
    assert article.height == 50 + 10 + 10


@assert_no_logs
def test_overflow_hidden_in_flow_layout():
    page, = render_pages('''
      <div style="overflow: hidden; height: 3px;">
        <div>abc</div>
        <div style="height: 100px; margin: 50px;">def</div>
      </div>
    ''')
    html, = page.children
    body, = html.children
    parent_div, = body.children
    assert parent_div.height == 3


@assert_no_logs
def test_overflow_hidden_out_of_flow_layout():
    page, = render_pages('''
      <div style="overflow: hidden; height: 3px;">
        <div style="float: left;">abc</div>
        <div style="float: right; height: 100px; margin: 50px;">def</div>
      </div>
    ''')
    html, = page.children
    body, = html.children
    parent_div, = body.children
    assert parent_div.height == 3


@assert_no_logs
def test_box_margin_top_repagination():
    # Regression test for #943.
    page_1, page_2 = render_pages('''
      <style>
        @page { size: 50px }
        :root { line-height: 1; font-size: 10px }
        a::before { content: target-counter(attr(href), page) }
        div { margin: 20px 0 0; background: yellow }
      </style>
      <p><a href="#title"></a></p>
      <div>1<br/>1<br/>2<br/>2</div>
      <h1 id="title">title</h1>
    ''')
    html, = page_1.children
    body, = html.children
    p, div = body.children
    assert div.margin_top == 20
    assert div.padding_box_y() == 10 + 20

    html, = page_2.children
    body, = html.children
    div, h1 = body.children
    assert div.margin_top == 0
    assert div.padding_box_y() == 0


@assert_no_logs
def test_continue_discard():
    page_1, = render_pages('''
      <style>
        @page { size: 80px; margin: 0 }
        div { display: inline-block; width: 100%; height: 25px }
        article { continue: discard; border: 1px solid; line-height: 1 }
      </style>
      <article>
        <div>a</div>
        <div>b</div>
        <div>c</div>
        <div>d</div>
        <div>e</div>
        <div>f</div>
      </article>''')
    html, = page_1.children
    body, = html.children
    article, = body.children
    assert article.height == 3 * 25
    div_1, div_2, div_3 = article.children
    assert div_1.position_y == 1
    assert div_2.position_y == 1 + 25
    assert div_3.position_y == 1 + 25 * 2
    assert article.border_bottom_width == 1


@assert_no_logs
def test_continue_discard_children():
    page_1, = render_pages('''
      <style>
        @page { size: 80px; margin: 0 }
        div { display: inline-block; width: 100%; height: 25px }
        section { border: 1px solid }
        article { continue: discard; border: 1px solid; line-height: 1 }
      </style>
      <article>
        <section>
          <div>a</div>
          <div>b</div>
          <div>c</div>
          <div>d</div>
          <div>e</div>
          <div>f</div>
        </section>
      </article>''')
    html, = page_1.children
    body, = html.children
    article, = body.children
    assert article.height == 2 + 3 * 25
    section, = article.children
    assert section.height == 3 * 25
    div_1, div_2, div_3 = section.children
    assert div_1.position_y == 2
    assert div_2.position_y == 2 + 25
    assert div_3.position_y == 2 + 25 * 2
    assert article.border_bottom_width == 1


@assert_no_logs
def test_block_in_block_with_bottom_padding():
    # Regression test for #1476.
    page_1, page_2 = render_pages('''
      <style>
        @page { size: 8em 3.5em }
        body { line-height: 1; font-family: weasyprint }
        div { padding-bottom: 1em }
      </style>
      abc def
      <div>
        <p>
          ghi jkl
          mno pqr
        </p>
      </div>
      stu vwx''')

    html, = page_1.children
    body, = html.children
    anon_body, div = body.children
    line, = anon_body.children
    assert line.height == 16
    assert line.children[0].text == 'abc def'
    p, = div.children
    line, = p.children
    assert line.height == 16
    assert line.children[0].text == 'ghi jkl'

    html, = page_2.children
    body, = html.children
    div, anon_body = body.children
    p, = div.children
    line, = p.children
    assert line.height == 16
    assert line.children[0].text == 'mno pqr'
    line, = anon_body.children
    assert line.height == 16
    assert line.content_box_y() == 16 + 16  # p content + div padding
    assert line.children[0].text == 'stu vwx'


@assert_no_logs
def test_page_breaks_1():
    # last div does not fit, pushed to next page
    pages = render_pages('''
      <style>
        @page{
          size: 110px;
          margin: 10px;
          padding: 0;
        }
        .large {
          width: 10px;
          height: 60px;
        }
        .small {
          width: 10px;
          height: 20px;
        }
      </style>
      <body>
        <div class="large"></div>
        <div class="small"></div>
        <div class="large"></div>
    ''')

    assert len(pages) == 2
    page_divs = []
    for page in pages:
        divs = [div for div in page.descendants() if div.element_tag == 'div']
        assert all([div.element_tag == 'div' for div in divs])
        page_divs.append(divs)
    positions_y = [[div.position_y for div in divs] for divs in page_divs]
    assert positions_y == [[10, 70], [10]]


@assert_no_logs
def test_page_breaks_2():
    # last div does not fit, pushed to next page
    # center div must not
    pages = render_pages('''
      <style>
        @page{
          size: 110px;
          margin: 10px;
          padding: 0;
        }
        .large {
          width: 10px;
          height: 60px;
        }
        .small {
          width: 10px;
          height: 20px;
          page-break-after: avoid;
        }
      </style>
      <body>
        <div class="large"></div>
        <div class="small"></div>
        <div class="large"></div>
    ''')

    assert len(pages) == 2
    page_divs = []
    for page in pages:
        divs = [div for div in page.descendants() if div.element_tag == 'div']
        assert all([div.element_tag == 'div' for div in divs])
        page_divs.append(divs)
    positions_y = [[div.position_y for div in divs] for divs in page_divs]
    assert positions_y == [[10], [10, 30]]


@assert_no_logs
def test_page_breaks_3():
    # center div must be the last element,
    # but div won't fit and will get pushed anyway
    pages = render_pages('''
      <style>
        @page{
          size: 110px;
          margin: 10px;
          padding: 0;
        }
        .large {
          width: 10px;
          height: 80px;
        }
        .small {
          width: 10px;
          height: 20px;
          page-break-after: avoid;
        }
      </style>
      <body>
        <div class="large"></div>
        <div class="small"></div>
        <div class="large"></div>
    ''')

    assert len(pages) == 3
    page_divs = []
    for page in pages:
        divs = [div for div in page.descendants() if div.element_tag == 'div']
        assert all([div.element_tag == 'div' for div in divs])
        page_divs.append(divs)
    positions_y = [[div.position_y for div in divs] for divs in page_divs]
    assert positions_y == [[10], [10], [10]]


@assert_no_logs
def test_page_break_child_margin_no_collapse():
    # Regression test for #2453.
    page1, page2 = render_pages('''
      <style>
        @page{ size: 6px }
      </style>
      <body>
        <div style="height: 2px"></div>
        <section>
          <div style="height: 3px; margin-bottom: 2px"></div>
          <div style="display: flex">
            <span style="font: 2px weasyprint">a</span>
          </div>
        </section>
      </body>
    ''')
    html, = page1.children
    body, = html.children
    div, section = body.children
    div, = section.children

    html, = page2.children
    body, = html.children
    section, = body.children
    div, = section.children