File: test_boxes.py

package info (click to toggle)
weasyprint 62.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,964 kB
  • sloc: python: 54,652; makefile: 12
file content (1249 lines) | stat: -rw-r--r-- 40,892 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
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
"""Test that the "before layout" box tree is correctly constructed."""

import pytest

from weasyprint.css import PageType, get_all_computed_styles
from weasyprint.formatting_structure import boxes, build
from weasyprint.layout.page import set_page_type_computed_styles

from .testing_utils import (  # isort:skip
    FakeHTML, assert_no_logs, assert_tree, capture_logs, parse, parse_all,
    render_pages)


@assert_no_logs
def test_box_tree():
    assert_tree(parse('<p>'), [('p', 'Block', [])])
    assert_tree(parse('''
      <style>
        span { display: inline-block }
      </style>
      <p>Hello <em>World <img src="pattern.png"><span>L</span></em>!</p>'''), [
          ('p', 'Block', [
            ('p', 'Text', 'Hello '),
            ('em', 'Inline', [
                ('em', 'Text', 'World '),
                ('img', 'InlineReplaced', '<replaced>'),
                ('span', 'InlineBlock', [
                    ('span', 'Text', 'L')])]),
            ('p', 'Text', '!')])])


@assert_no_logs
def test_html_entities():
    for quote in ['"', '&quot;', '&#x22;', '&#34;']:
        assert_tree(parse(f'<p>{quote}abc{quote}'), [
            ('p', 'Block', [
                ('p', 'Text', '"abc"')])])


@assert_no_logs
def test_inline_in_block_1():
    source = '<div>Hello, <em>World</em>!\n<p>Lipsum.</p></div>'
    expected = [
        ('div', 'Block', [
            ('div', 'Block', [
                ('div', 'Line', [
                    ('div', 'Text', 'Hello, '),
                    ('em', 'Inline', [
                        ('em', 'Text', 'World')]),
                    ('div', 'Text', '! ')])]),
            ('p', 'Block', [
                ('p', 'Line', [
                    ('p', 'Text', 'Lipsum.')])])])]
    box = parse(source)
    box = build.inline_in_block(box)
    assert_tree(box, expected)


@assert_no_logs
def test_inline_in_block_2():
    source = '<div><p>Lipsum.</p>Hello, <em>World</em>!\n</div>'
    expected = [
        ('div', 'Block', [
            ('p', 'Block', [
                ('p', 'Line', [
                    ('p', 'Text', 'Lipsum.')])]),
            ('div', 'Block', [
                ('div', 'Line', [
                    ('div', 'Text', 'Hello, '),
                    ('em', 'Inline', [
                        ('em', 'Text', 'World')]),
                    ('div', 'Text', '! ')])])])]
    box = parse(source)
    box = build.inline_in_block(box)
    assert_tree(box, expected)


@assert_no_logs
def test_inline_in_block_3():
    # Absolutes are left in the lines to get their static position later.
    source = '''<p>Hello <em style="position:absolute;
                                    display: block">World</em>!</p>'''
    expected = [
        ('p', 'Block', [
            ('p', 'Line', [
                ('p', 'Text', 'Hello '),
                ('em', 'Block', [
                    ('em', 'Line', [
                        ('em', 'Text', 'World')])]),
                ('p', 'Text', '!')])])]
    box = parse(source)
    box = build.inline_in_block(box)
    assert_tree(box, expected)
    box = build.block_in_inline(box)
    assert_tree(box, expected)


@assert_no_logs
def test_inline_in_block_4():
    # Floats are pull to the top of their containing blocks
    source = '<p>Hello <em style="float: left">World</em>!</p>'
    box = parse(source)
    box = build.inline_in_block(box)
    box = build.block_in_inline(box)
    assert_tree(box, [
        ('p', 'Block', [
            ('p', 'Line', [
                ('p', 'Text', 'Hello '),
                ('em', 'Block', [
                    ('em', 'Line', [
                        ('em', 'Text', 'World')])]),
                ('p', 'Text', '!')])])])


@assert_no_logs
def test_block_in_inline():
    box = parse('''
      <style>
        p { display: inline-block; }
        span, i { display: block; }
      </style>
      <p>Lorem <em>ipsum <strong>dolor <span>sit</span>
      <span>amet,</span></strong><span><em>conse<i>''')
    box = build.inline_in_block(box)
    assert_tree(box, [
        ('body', 'Line', [
            ('p', 'InlineBlock', [
                ('p', 'Line', [
                    ('p', 'Text', 'Lorem '),
                    ('em', 'Inline', [
                        ('em', 'Text', 'ipsum '),
                        ('strong', 'Inline', [
                            ('strong', 'Text', 'dolor '),
                            ('span', 'Block', [  # This block is "pulled up"
                                ('span', 'Line', [
                                    ('span', 'Text', 'sit')])]),
                            ('strong', 'Text', ' '),
                            ('span', 'Block', [  # This block is "pulled up"
                                ('span', 'Line', [
                                    ('span', 'Text', 'amet,')])])]),
                        ('span', 'Block', [  # This block is "pulled up"
                            ('span', 'Line', [
                                ('em', 'Inline', [
                                    ('em', 'Text', 'conse'),
                                    ('i', 'Block', [])])])])])])])])])

    box = build.block_in_inline(box)
    assert_tree(box, [
        ('body', 'Line', [
            ('p', 'InlineBlock', [
                ('p', 'Block', [
                    ('p', 'Line', [
                        ('p', 'Text', 'Lorem '),
                        ('em', 'Inline', [
                            ('em', 'Text', 'ipsum '),
                            ('strong', 'Inline', [
                                ('strong', 'Text', 'dolor ')])])])]),
                ('span', 'Block', [
                    ('span', 'Line', [
                        ('span', 'Text', 'sit')])]),
                ('p', 'Block', [
                    ('p', 'Line', [
                        ('em', 'Inline', [
                            ('strong', 'Inline', [
                                ('strong', 'Text', ' ')])])])]),
                ('span', 'Block', [
                    ('span', 'Line', [
                        ('span', 'Text', 'amet,')])]),

                ('p', 'Block', [
                    ('p', 'Line', [
                        ('em', 'Inline', [
                            ('strong', 'Inline', [])])])]),
                ('span', 'Block', [
                    ('span', 'Block', [
                        ('span', 'Line', [
                            ('em', 'Inline', [
                                ('em', 'Text', 'conse')])])]),
                    ('i', 'Block', []),
                    ('span', 'Block', [
                        ('span', 'Line', [
                            ('em', 'Inline', [])])])]),
                ('p', 'Block', [
                    ('p', 'Line', [
                        ('em', 'Inline', [])])])])])])


@assert_no_logs
def test_styles():
    box = parse('''
      <style>
        span { display: block; }
        * { margin: 42px }
        html { color: blue }
      </style>
      <p>Lorem <em>ipsum <strong>dolor <span>sit</span>
        <span>amet,</span></strong><span>consectetur</span></em></p>''')
    box = build.inline_in_block(box)
    box = build.block_in_inline(box)

    descendants = list(box.descendants())
    assert len(descendants) == 31
    assert descendants[0] == box

    for child in descendants:
        # All boxes inherit the color
        assert child.style['color'] == (0, 0, 1, 1)  # blue
        # Only non-anonymous boxes have margins
        assert child.style['margin_top'] in ((0, 'px'), (42, 'px'))


@assert_no_logs
def test_whitespace():
    assert_tree(parse_all('''
      <p>Lorem \t\r\n  ipsum\t<strong>  dolor
        <img src=pattern.png> sit
        <span style="position: absolute"></span> <em> amet </em>
        consectetur</strong>.</p>
      <pre>\t  foo\n</pre>
      <pre style="white-space: pre-wrap">\t  foo\n</pre>
      <pre style="white-space: pre-line">\t  foo\n</pre>
    '''), [
        ('p', 'Block', [
            ('p', 'Line', [
                ('p', 'Text', 'Lorem ipsum '),
                ('strong', 'Inline', [
                    ('strong', 'Text', 'dolor '),
                    ('img', 'InlineReplaced', '<replaced>'),
                    ('strong', 'Text', ' sit '),
                    ('span', 'Block', []),
                    ('em', 'Inline', [
                        ('em', 'Text', 'amet ')]),
                    ('strong', 'Text', 'consectetur')]),
                ('p', 'Text', '.')])]),
        ('pre', 'Block', [
            ('pre', 'Line', [
                # pre
                ('pre', 'Text', '\t  foo\n')])]),
        ('pre', 'Block', [
            ('pre', 'Line', [
                # pre-wrap
                ('pre', 'Text', '\t  foo\n')])]),
        ('pre', 'Block', [
            ('pre', 'Line', [
                # pre-line
                ('pre', 'Text', ' foo\n')])])])


@assert_no_logs
@pytest.mark.parametrize('page_type, top, right, bottom, left', (
    (PageType(side='left', first=True, index=0, blank=None, name=None),
     20, 3, 3, 10),
    (PageType(side='right', first=True, index=0, blank=None, name=None),
     20, 10, 3, 3),
    (PageType(side='left', first=None, index=1, blank=None, name=None),
     10, 3, 3, 10),
    (PageType(side='right', first=None, index=1, blank=None, name=None),
     10, 10, 3, 3),
    (PageType(side='right', first=None, index=1, blank=None, name='name'),
     5, 10, 3, 15),
    (PageType(side='right', first=None, index=2, blank=None, name='name'),
     5, 10, 1, 15),
    (PageType(side='right', first=None, index=8, blank=None, name='name'),
     5, 10, 2, 15),
))
def test_page_style(page_type, top, right, bottom, left):
    document = FakeHTML(string='''
      <style>
        @page { margin: 3px }
        @page name { margin-left: 15px; margin-top: 5px }
        @page :nth(3) { margin-bottom: 1px }
        @page :nth(5n+4) { margin-bottom: 2px }
        @page :first { margin-top: 20px }
        @page :right { margin-right: 10px; margin-top: 10px }
        @page :left { margin-left: 10px; margin-top: 10px }
      </style>
    ''')
    style_for = get_all_computed_styles(document)

    # Force the generation of the style for this page type as it's generally
    # only done during the rendering.
    set_page_type_computed_styles(page_type, document, style_for)

    style = style_for(page_type)
    assert style['margin_top'] == (top, 'px')
    assert style['margin_right'] == (right, 'px')
    assert style['margin_bottom'] == (bottom, 'px')
    assert style['margin_left'] == (left, 'px')


@assert_no_logs
def test_images_1():
    with capture_logs() as logs:
        result = parse_all('''
          <p><img src=pattern.png
            /><img alt="No src"
            /><img src=inexistent.jpg alt="Inexistent src" /></p>
        ''')
    assert len(logs) == 1
    assert 'ERROR: Failed to load image' in logs[0]
    assert 'inexistent.jpg' in logs[0]
    assert_tree(result, [
        ('p', 'Block', [
            ('p', 'Line', [
                ('img', 'InlineReplaced', '<replaced>'),
                ('img', 'Inline', [
                    ('img', 'Text', 'No src')]),
                ('img', 'Inline', [
                    ('img', 'Text', 'Inexistent src')])])])])


@assert_no_logs
def test_images_2():
    with capture_logs() as logs:
        result = parse_all('<p><img src=pattern.png alt="No base_url">',
                           base_url=None)
    assert len(logs) == 1
    assert 'ERROR: Relative URI reference without a base URI' in logs[0]
    assert_tree(result, [
        ('p', 'Block', [
            ('p', 'Line', [
                ('img', 'Inline', [
                    ('img', 'Text', 'No base_url')])])])])


@assert_no_logs
def test_tables_1():
    # Rules in https://www.w3.org/TR/CSS21/tables.html#anonymous-boxes

    # Rule 1.3
    # Also table model: https://www.w3.org/TR/CSS21/tables.html#model
    assert_tree(parse_all('''
      <x-table>
        <x-tr>
          <x-th>foo</x-th>
          <x-th>bar</x-th>
        </x-tr>
        <x-tfoot></x-tfoot>
        <x-thead><x-th></x-th></x-thead>
        <x-caption style="caption-side: bottom"></x-caption>
        <x-thead></x-thead>
        <x-col></x-col>
        <x-caption>top caption</x-caption>
        <x-tr>
          <x-td>baz</x-td>
        </x-tr>
      </x-table>
    '''), [
        ('x-table', 'Block', [
            ('x-caption', 'TableCaption', [
                ('x-caption', 'Line', [
                    ('x-caption', 'Text', 'top caption')])]),
            ('x-table', 'Table', [
                ('x-table', 'TableColumnGroup', [
                    ('x-col', 'TableColumn', [])]),
                ('x-thead', 'TableRowGroup', [
                    ('x-thead', 'TableRow', [
                        ('x-th', 'TableCell', [])])]),
                ('x-table', 'TableRowGroup', [
                    ('x-tr', 'TableRow', [
                        ('x-th', 'TableCell', [
                            ('x-th', 'Line', [
                                ('x-th', 'Text', 'foo')])]),
                        ('x-th', 'TableCell', [
                            ('x-th', 'Line', [
                                ('x-th', 'Text', 'bar')])])])]),
                ('x-thead', 'TableRowGroup', []),
                ('x-table', 'TableRowGroup', [
                    ('x-tr', 'TableRow', [
                        ('x-td', 'TableCell', [
                            ('x-td', 'Line', [
                                ('x-td', 'Text', 'baz')])])])]),
                ('x-tfoot', 'TableRowGroup', [])]),
            ('x-caption', 'TableCaption', [])])])


@assert_no_logs
def test_tables_2():
    # Rules 1.4 and 3.1
    assert_tree(parse_all('''
      <span style="display: table-cell">foo</span>
      <span style="display: table-cell">bar</span>
    '''), [
        ('body', 'Block', [
            ('body', 'Table', [
                ('body', 'TableRowGroup', [
                    ('body', 'TableRow', [
                        ('span', 'TableCell', [
                            ('span', 'Line', [
                                ('span', 'Text', 'foo')])]),
                        ('span', 'TableCell', [
                            ('span', 'Line', [
                                ('span', 'Text', 'bar')])])])])])])])


@assert_no_logs
def test_tables_3():
    # https://www.w3.org/TR/CSS21/tables.html#anonymous-boxes
    # Rules 1.1 and 1.2
    # Rule XXX (not in the spec): column groups have at least one column child
    assert_tree(parse_all('''
      <span style="display: table-column-group">
        1
        <em style="display: table-column">
          2
          <strong>3</strong>
        </em>
        <strong>4</strong>
      </span>
      <ins style="display: table-column-group"></ins>
    '''), [
        ('body', 'Block', [
            ('body', 'Table', [
                ('span', 'TableColumnGroup', [
                    ('em', 'TableColumn', [])]),
                ('ins', 'TableColumnGroup', [
                    ('ins', 'TableColumn', [])])])])])


@assert_no_logs
def test_tables_4():
    # Rules 2.1 then 2.3
    assert_tree(parse_all('<x-table>foo <div></div></x-table>'), [
        ('x-table', 'Block', [
            ('x-table', 'Table', [
                ('x-table', 'TableRowGroup', [
                    ('x-table', 'TableRow', [
                        ('x-table', 'TableCell', [
                            ('x-table', 'Block', [
                                ('x-table', 'Line', [
                                    ('x-table', 'Text', 'foo ')])]),
                            ('div', 'Block', [])])])])])])])


@assert_no_logs
def test_tables_5():
    # Rule 2.2
    assert_tree(parse_all('<x-thead style="display: table-header-group">'
                          '<div></div><x-td></x-td></x-thead>'), [
        ('body', 'Block', [
            ('body', 'Table', [
                ('x-thead', 'TableRowGroup', [
                    ('x-thead', 'TableRow', [
                        ('x-thead', 'TableCell', [
                            ('div', 'Block', [])]),
                        ('x-td', 'TableCell', [])])])])])])


@assert_no_logs
def test_tables_6():
    # Rule 3.2
    assert_tree(parse_all('<span><x-tr></x-tr></span>'), [
        ('body', 'Line', [
            ('span', 'Inline', [
                ('span', 'InlineBlock', [
                    ('span', 'InlineTable', [
                        ('span', 'TableRowGroup', [
                            ('x-tr', 'TableRow', [])])])])])])])


@assert_no_logs
def test_tables_7():
    # Rule 3.1
    # Also, rule 1.3 does not apply: whitespace before and after is preserved
    assert_tree(parse_all('''
      <span>
        <em style="display: table-cell"></em>
        <em style="display: table-cell"></em>
      </span>
    '''), [
        ('body', 'Line', [
            ('span', 'Inline', [
                # Whitespace is preserved in table handling, then collapsed
                # into a single space.
                ('span', 'Text', ' '),
                ('span', 'InlineBlock', [
                    ('span', 'InlineTable', [
                        ('span', 'TableRowGroup', [
                            ('span', 'TableRow', [
                                ('em', 'TableCell', []),
                                ('em', 'TableCell', [])])])])]),
                ('span', 'Text', ' ')])])])


@assert_no_logs
def test_tables_8():
    # Rule 3.2
    assert_tree(parse_all('<x-tr></x-tr>\t<x-tr></x-tr>'), [
        ('body', 'Block', [
            ('body', 'Table', [
                ('body', 'TableRowGroup', [
                    ('x-tr', 'TableRow', []),
                    ('x-tr', 'TableRow', [])])])])])


@assert_no_logs
def test_tables_9():
    assert_tree(parse_all('<x-col></x-col>\n<x-colgroup></x-colgroup>'), [
        ('body', 'Block', [
            ('body', 'Table', [
                ('body', 'TableColumnGroup', [
                    ('x-col', 'TableColumn', [])]),
                ('x-colgroup', 'TableColumnGroup', [
                    ('x-colgroup', 'TableColumn', [])])])])])


@assert_no_logs
def test_table_style():
    html = parse_all('<table style="margin: 1px; padding: 2px"></table>')
    body, = html.children
    wrapper, = body.children
    table, = wrapper.children
    assert isinstance(wrapper, boxes.BlockBox)
    assert isinstance(table, boxes.TableBox)
    assert wrapper.style['margin_top'] == (1, 'px')
    assert wrapper.style['padding_top'] == (0, 'px')
    assert table.style['margin_top'] == (0, 'px')
    assert table.style['padding_top'] == (2, 'px')


@assert_no_logs
def test_column_style():
    html = parse_all('''
      <table>
        <col span=3 style="width: 10px"></col>
        <col span=2></col>
      </table>
    ''')
    body, = html.children
    wrapper, = body.children
    table, = wrapper.children
    colgroup, = table.column_groups
    widths = [col.style['width'] for col in colgroup.children]
    assert widths == [(10, 'px'), (10, 'px'), (10, 'px'), 'auto', 'auto']
    assert [col.grid_x for col in colgroup.children] == [0, 1, 2, 3, 4]
    # copies, not the same box object
    assert colgroup.children[0] is not colgroup.children[1]


@assert_no_logs
def test_nested_grid_x():
    html = parse_all('''
      <table>
        <col span=2></col>
        <colgroup span=2></colgroup>
        <colgroup>
          <col></col>
          <col span=2></col>
        </colgroup>
        <col></col>
      </table>
    ''')
    body, = html.children
    wrapper, = body.children
    table, = wrapper.children
    grid = [(colgroup.grid_x, [col.grid_x for col in colgroup.children])
            for colgroup in table.column_groups]
    assert grid == [(0, [0, 1]), (2, [2, 3]), (4, [4, 5, 6]), (7, [7])]


@assert_no_logs
def test_colspan_rowspan_1():
    # +---+---+---+
    # | A | B | C | X
    # +---+---+---+
    # | D |     E | X
    # +---+---+   +---+
    # |  F ...|   |   |   <-- overlap
    # +---+---+---+   +
    # | H | X   X | G |
    # +---+---+   +   +
    # | I | J | X |   |
    # +---+---+   +---+

    # X: empty cells
    html = parse_all('''
      <table>
        <tr>
          <td>A <td>B <td>C
        </tr>
        <tr>
          <td>D <td colspan=2 rowspan=2>E
        </tr>
        <tr>
          <td colspan=2>F <td rowspan=0>G
        </tr>
        <tr>
          <td>H
        </tr>
        <tr>
          <td>I <td>J
        </tr>
      </table>
    ''')
    body, = html.children
    wrapper, = body.children
    table, = wrapper.children
    group, = table.children
    assert [[c.grid_x for c in row.children] for row in group.children] == [
        [0, 1, 2],
        [0, 1],
        [0, 3],
        [0],
        [0, 1],
    ]
    assert [[c.colspan for c in row.children] for row in group.children] == [
        [1, 1, 1],
        [1, 2],
        [2, 1],
        [1],
        [1, 1],
    ]
    assert [[c.rowspan for c in row.children] for row in group.children] == [
        [1, 1, 1],
        [1, 2],
        [1, 3],
        [1],
        [1, 1],
    ]


@assert_no_logs
def test_colspan_rowspan_2():
    # A cell box cannot extend beyond the last row box of a table.
    html = parse_all('''
        <table>
            <tr>
                <td rowspan=5></td>
                <td></td>
            </tr>
            <tr>
                <td></td>
            </tr>
        </table>
    ''')
    body, = html.children
    wrapper, = body.children
    table, = wrapper.children
    group, = table.children
    assert [[c.grid_x for c in row.children] for row in group.children] == [
        [0, 1],
        [1],
    ]
    assert [[c.colspan for c in row.children] for row in group.children] == [
        [1, 1],
        [1],
    ]
    assert [[c.rowspan for c in row.children] for row in group.children] == [
        [2, 1],  # Not 5
        [1],
    ]


@assert_no_logs
def test_before_after_1():
    assert_tree(parse_all('''
      <style>
        p:before { content: normal }
        div:before { content: none }
        section::before { color: black }
      </style>
      <p></p>
      <div></div>
      <section></section>
    '''), [
        # No content in pseudo-element, no box generated
        ('p', 'Block', []),
        ('div', 'Block', []),
        ('section', 'Block', [])])


@assert_no_logs
def test_before_after_2():
    assert_tree(parse_all('''
      <style>
        p:before { content: 'a' 'b' }
        p::after { content: 'd' 'e' }
      </style>
      <p> c </p>
    '''), [
        ('p', 'Block', [
            ('p', 'Line', [
                ('p::before', 'Inline', [
                    ('p::before', 'Text', 'ab')]),
                ('p', 'Text', ' c '),
                ('p::after', 'Inline', [
                    ('p::after', 'Text', 'de')])])])])


@assert_no_logs
def test_before_after_3():
    assert_tree(parse_all('''
      <style>
        a[href]:before { content: '[' attr(href) '] ' }
      </style>
      <p><a href="some url">some text</a></p>
    '''), [
        ('p', 'Block', [
            ('p', 'Line', [
                ('a', 'Inline', [
                    ('a::before', 'Inline', [
                        ('a::before', 'Text', '[some url] ')]),
                    ('a', 'Text', 'some text')])])])])


@assert_no_logs
def test_before_after_4():
    assert_tree(parse_all('''
      <style>
        body { quotes: '«' '»' '“' '”' }
        q:before { content: open-quote ' '}
        q:after { content: ' ' close-quote }
      </style>
      <p><q>Lorem ipsum <q>dolor</q> sit amet</q></p>
    '''), [
        ('p', 'Block', [
            ('p', 'Line', [
                ('q', 'Inline', [
                    ('q::before', 'Inline', [
                        ('q::before', 'Text', '« ')]),
                    ('q', 'Text', 'Lorem ipsum '),
                    ('q', 'Inline', [
                        ('q::before', 'Inline', [
                            ('q::before', 'Text', '“ ')]),
                        ('q', 'Text', 'dolor'),
                        ('q::after', 'Inline', [
                            ('q::after', 'Text', ' ”')])]),
                    ('q', 'Text', ' sit amet'),
                    ('q::after', 'Inline', [
                        ('q::after', 'Text', ' »')])])])])])


@assert_no_logs
def test_before_after_5():
    with capture_logs() as logs:
        assert_tree(parse_all('''
          <style>
            p:before {
              content: 'a' url(pattern.png) 'b';

              /* Invalid, ignored in favor of the one above.
                 Regression test: this used to crash: */
              content: some-function(nested-function(something));
            }
          </style>
          <p>c</p>
        '''), [
            ('p', 'Block', [
                ('p', 'Line', [
                    ('p::before', 'Inline', [
                        ('p::before', 'Text', 'a'),
                        ('p::before', 'InlineReplaced', '<replaced>'),
                        ('p::before', 'Text', 'b')]),
                    ('p', 'Text', 'c')])])])
    assert len(logs) == 1
    assert 'nested-function(' in logs[0]
    assert 'invalid value' in logs[0]


@assert_no_logs
def test_quotes_auto():
    assert_tree(parse_all('''
      <style>
        body { quotes: auto }
        q:before { content: open-quote ' '}
        q:after { content: ' ' close-quote }
      </style>
      <p><q>Lorem ipsum <q>dolor</q> sit amet</q></p>
    '''), [
        ('p', 'Block', [
            ('p', 'Line', [
                ('q', 'Inline', [
                    ('q::before', 'Inline', [
                        ('q::before', 'Text', '“ ')]),
                    ('q', 'Text', 'Lorem ipsum '),
                    ('q', 'Inline', [
                        ('q::before', 'Inline', [
                            ('q::before', 'Text', '‘ ')]),
                        ('q', 'Text', 'dolor'),
                        ('q::after', 'Inline', [
                            ('q::after', 'Text', ' ’')])]),
                    ('q', 'Text', ' sit amet'),
                    ('q::after', 'Inline', [
                        ('q::after', 'Text', ' ”')])])])])])


@assert_no_logs
def test_quotes_none():
    assert_tree(parse_all('''
      <style>
        body { quotes: none }
        q:before { content: open-quote ' '}
        q:after { content: ' ' close-quote }
      </style>
      <p><q>Lorem ipsum <q>dolor</q> sit amet</q></p>
    '''), [
        ('p', 'Block', [
            ('p', 'Line', [
                ('q', 'Inline', [
                    ('q::before', 'Inline', [
                        ('q::before', 'Text', ' ')]),
                    ('q', 'Text', 'Lorem ipsum '),
                    ('q', 'Inline', [
                        ('q::before', 'Inline', [
                            ('q::before', 'Text', ' ')]),
                        ('q', 'Text', 'dolor'),
                        ('q::after', 'Inline', [
                            ('q::after', 'Text', ' ')])]),
                    ('q', 'Text', ' sit amet'),
                    ('q::after', 'Inline', [
                        ('q::after', 'Text', ' ')])])])])])


@assert_no_logs
def test_quotes_lang():
    assert_tree(parse_all('''
      <style>
        q:before { content: open-quote ' '}
        q:after { content: ' ' close-quote }
      </style>
      <p lang="fr"><q>Lorem ipsum <q>dolor</q> sit amet</q></p>
    '''), [
        ('p', 'Block', [
            ('p', 'Line', [
                ('q', 'Inline', [
                    ('q::before', 'Inline', [
                        ('q::before', 'Text', '« ')]),
                    ('q', 'Text', 'Lorem ipsum '),
                    ('q', 'Inline', [
                        ('q::before', 'Inline', [
                            ('q::before', 'Text', '« ')]),
                        ('q', 'Text', 'dolor'),
                        ('q::after', 'Inline', [
                            ('q::after', 'Text', ' »')])]),
                    ('q', 'Text', ' sit amet'),
                    ('q::after', 'Inline', [
                        ('q::after', 'Text', ' »')])])])])])


@assert_no_logs
def test_quotes_lang_alternate():
    assert_tree(parse_all('''
      <style>
        q:before { content: open-quote ' '}
        q:after { content: ' ' close-quote }
      </style>
      <p lang="fr_CH"><q>Lorem ipsum <q>dolor</q> sit amet</q></p>
    '''), [
        ('p', 'Block', [
            ('p', 'Line', [
                ('q', 'Inline', [
                    ('q::before', 'Inline', [
                        ('q::before', 'Text', '« ')]),
                    ('q', 'Text', 'Lorem ipsum '),
                    ('q', 'Inline', [
                        ('q::before', 'Inline', [
                            ('q::before', 'Text', '‹ ')]),
                        ('q', 'Text', 'dolor'),
                        ('q::after', 'Inline', [
                            ('q::after', 'Text', ' ›')])]),
                    ('q', 'Text', ' sit amet'),
                    ('q::after', 'Inline', [
                        ('q::after', 'Text', ' »')])])])])])


@assert_no_logs
def test_quotes_lang_parent():
    assert_tree(parse_all('''
      <style>
        q:before { content: open-quote ' '}
        q:after { content: ' ' close-quote }
      </style>
      <p lang="fr_CH_alt"><q>Lorem ipsum <q>dolor</q> sit amet</q></p>
    '''), [
        ('p', 'Block', [
            ('p', 'Line', [
                ('q', 'Inline', [
                    ('q::before', 'Inline', [
                        ('q::before', 'Text', '« ')]),
                    ('q', 'Text', 'Lorem ipsum '),
                    ('q', 'Inline', [
                        ('q::before', 'Inline', [
                            ('q::before', 'Text', '‹ ')]),
                        ('q', 'Text', 'dolor'),
                        ('q::after', 'Inline', [
                            ('q::after', 'Text', ' ›')])]),
                    ('q', 'Text', ' sit amet'),
                    ('q::after', 'Inline', [
                        ('q::after', 'Text', ' »')])])])])])


@assert_no_logs
def test_margin_boxes():
    page_1, page_2 = render_pages('''
      <style>
        @page {
          /* Make the page content area only 10px high and wide,
             so every word in <p> end up on a page of its own. */
          size: 30px;
          margin: 10px;
          @top-center { content: "Title" }
        }
        @page :first {
          @bottom-left { content: "foo" }
          @bottom-left-corner { content: "baz" }
        }
      </style>
      <p>lorem ipsum
    ''')
    assert page_1.children[0].element_tag == 'html'
    assert page_2.children[0].element_tag == 'html'

    margin_boxes_1 = [box.at_keyword for box in page_1.children[1:]]
    margin_boxes_2 = [box.at_keyword for box in page_2.children[1:]]
    assert margin_boxes_1 == ['@top-center', '@bottom-left',
                              '@bottom-left-corner']
    assert margin_boxes_2 == ['@top-center']

    html, top_center = page_2.children
    line_box, = top_center.children
    text_box, = line_box.children
    assert text_box.text == 'Title'


@assert_no_logs
def test_margin_box_string_set_1():
    # Test that both pages get string in the `bottom-center` margin box
    page_1, page_2 = render_pages('''
      <style>
        @page {
          @bottom-center { content: string(text_header) }
        }
        p {
          string-set: text_header content();
        }
        .page {
          page-break-before: always;
        }
      </style>
      <p>first assignment</p>
      <div class="page"></div>
    ''')

    html, bottom_center = page_2.children
    line_box, = bottom_center.children
    text_box, = line_box.children
    assert text_box.text == 'first assignment'

    html, bottom_center = page_1.children
    line_box, = bottom_center.children
    text_box, = line_box.children
    assert text_box.text == 'first assignment'


@assert_no_logs
def test_margin_box_string_set_2():
    def simple_string_set_test(content_val, extra_style=""):
        page_1, = render_pages('''
          <style>
            @page {
              @top-center { content: string(text_header) }
            }
            p {
              string-set: text_header content(%s);
            }
            %s
          </style>
          <p>first assignment</p>
        ''' % (content_val, extra_style))

        html, top_center = page_1.children
        line_box, = top_center.children
        text_box, = line_box.children
        if content_val in ('before', 'after'):
            assert text_box.text == 'pseudo'
        else:
            assert text_box.text == 'first assignment'

    # Test each accepted value of `content()` as an arguemnt to `string-set`
    for value in ('', 'text', 'before', 'after'):
        if value in ('before', 'after'):
            extra_style = 'p:%s{content: "pseudo"}' % value
            simple_string_set_test(value, extra_style)
        else:
            simple_string_set_test(value)


@assert_no_logs
def test_margin_box_string_set_3():
    # Test `first` (default value) ie. use the first assignment on the page
    page_1, = render_pages('''
      <style>
        @page {
          @top-center { content: string(text_header, first) }
        }
        p {
          string-set: text_header content();
        }
      </style>
      <p>first assignment</p>
      <p>Second assignment</p>
    ''')

    html, top_center = page_1.children
    line_box, = top_center.children
    text_box, = line_box.children
    assert text_box.text == 'first assignment'


@assert_no_logs
def test_margin_box_string_set_4():
    # test `first-except` ie. exclude from page on which value is assigned
    page_1, page_2 = render_pages('''
      <style>
        @page {
          @top-center { content: string(header_nofirst, first-except) }
        }
        p{
          string-set: header_nofirst content();
        }
        .page{
          page-break-before: always;
        }
      </style>
      <p>first_excepted</p>
      <div class="page"></div>
    ''')
    html, top_center = page_1.children
    assert len(top_center.children) == 0

    html, top_center = page_2.children
    line_box, = top_center.children
    text_box, = line_box.children
    assert text_box.text == 'first_excepted'


@assert_no_logs
def test_margin_box_string_set_5():
    # Test `last` ie. use the most-recent assignment
    page_1, = render_pages('''
      <style>
        @page {
          @top-center { content: string(header_last, last) }
        }
        p {
          string-set: header_last content();
        }
      </style>
      <p>String set</p>
      <p>Second assignment</p>
    ''')

    html, top_center = page_1.children[:2]
    line_box, = top_center.children

    text_box, = line_box.children
    assert text_box.text == 'Second assignment'


@assert_no_logs
def test_margin_box_string_set_6():
    # Test multiple complex string-set values
    page_1, = render_pages('''
      <style>
        @page {
          @top-center { content: string(text_header, first) }
          @bottom-center { content: string(text_footer, last) }
        }
        html { counter-reset: a }
        body { counter-increment: a }
        ul { counter-reset: b }
        li {
          counter-increment: b;
          string-set:
            text_header content(before) "-" content() "-" content(after)
                        counter(a, upper-roman) '.' counters(b, '|'),
            text_footer content(before) '-' attr(class)
                        counters(b, '|') "/" counter(a, upper-roman);
        }
        li:before { content: 'before!' }
        li:after { content: 'after!' }
        li:last-child:before { content: 'before!last' }
        li:last-child:after { content: 'after!last' }
      </style>
      <ul>
        <li class="firstclass">first
        <li>
          <ul>
            <li class="secondclass">second
    ''')

    html, top_center, bottom_center = page_1.children
    top_line_box, = top_center.children
    top_text_box, = top_line_box.children
    assert top_text_box.text == 'before!-first-after!I.1'
    bottom_line_box, = bottom_center.children
    bottom_text_box, = bottom_line_box.children
    assert bottom_text_box.text == 'before!last-secondclass2|1/I'


def test_margin_box_string_set_7():
    # Test regression: https://github.com/Kozea/WeasyPrint/issues/722
    page_1, = render_pages('''
      <style>
        img { string-set: left attr(alt) }
        img + img { string-set: right attr(alt) }
        @page { @top-left  { content: '[' string(left)  ']' }
                @top-right { content: '{' string(right) '}' } }
      </style>
      <img src=pattern.png alt="Chocolate">
      <img src=no_such_file.png alt="Cake">
    ''')

    html, top_left, top_right = page_1.children
    left_line_box, = top_left.children
    left_text_box, = left_line_box.children
    assert left_text_box.text == '[Chocolate]'
    right_line_box, = top_right.children
    right_text_box, = right_line_box.children
    assert right_text_box.text == '{Cake}'


@assert_no_logs
def test_margin_box_string_set_8():
    # Test regression: https://github.com/Kozea/WeasyPrint/issues/726
    page_1, page_2, page_3 = render_pages('''
      <style>
        @page { @top-left  { content: '[' string(left) ']' } }
        p { page-break-before: always }
        .initial { string-set: left 'initial' }
        .empty   { string-set: left ''        }
        .space   { string-set: left ' '       }
      </style>

      <p class="initial">Initial</p>
      <p class="empty">Empty</p>
      <p class="space">Space</p>
    ''')
    html, top_left = page_1.children
    left_line_box, = top_left.children
    left_text_box, = left_line_box.children
    assert left_text_box.text == '[initial]'

    html, top_left = page_2.children
    left_line_box, = top_left.children
    left_text_box, = left_line_box.children
    assert left_text_box.text == '[]'

    html, top_left = page_3.children
    left_line_box, = top_left.children
    left_text_box, = left_line_box.children
    assert left_text_box.text == '[ ]'


@assert_no_logs
def test_margin_box_string_set_9():
    # Test that named strings are case-sensitive
    # See https://github.com/Kozea/WeasyPrint/pull/827
    page_1, = render_pages('''
      <style>
        @page {
          @top-center {
            content: string(text_header, first)
                     ' ' string(TEXT_header, first)
          }
        }
        p { string-set: text_header content() }
        div { string-set: TEXT_header content() }
      </style>
      <p>first assignment</p>
      <div>second assignment</div>
    ''')

    html, top_center = page_1.children
    line_box, = top_center.children
    text_box, = line_box.children
    assert text_box.text == 'first assignment second assignment'


@assert_no_logs
def test_margin_box_string_set_10():
    page_1, page_2, page_3, page_4 = render_pages('''
      <style>
        @page { @top-left  { content: '[' string(p, start) ']' } }
        p { string-set: p content(); page-break-after: always }
      </style>
      <article></article>
      <p>1</p>
      <article></article>
      <p>2</p>
      <p>3</p>
      <article></article>
    ''')
    html, top_left = page_1.children
    left_line_box, = top_left.children
    left_text_box, = left_line_box.children
    assert left_text_box.text == '[]'

    html, top_left = page_2.children
    left_line_box, = top_left.children
    left_text_box, = left_line_box.children
    assert left_text_box.text == '[1]'

    html, top_left = page_3.children
    left_line_box, = top_left.children
    left_text_box, = left_line_box.children
    assert left_text_box.text == '[3]'

    html, top_left = page_4.children
    left_line_box, = top_left.children
    left_text_box, = left_line_box.children
    assert left_text_box.text == '[3]'


@assert_no_logs
def test_page_counters():
    """Test page-based counters."""
    pages = render_pages('''
      <style>
        @page {
          /* Make the page content area only 10px high and wide,
             so every word in <p> end up on a page of its own. */
          size: 30px;
          margin: 10px;
          @bottom-center {
            content: "Page " counter(page) " of " counter(pages) ".";
          }
        }
      </style>
      <p>lorem ipsum dolor
    ''')
    for page_number, page in enumerate(pages, 1):
        html, bottom_center = page.children
        line_box, = bottom_center.children
        text_box, = line_box.children
        assert text_box.text == f'Page {page_number} of 3.'


@assert_no_logs
@pytest.mark.parametrize('html', (
    '<html style="display: none">',
    '<html style="display: none">abc',
    '<html style="display: none"><p>abc',
    '<body style="display: none"><p>abc',
))
def test_display_none_root(html):
    box = parse_all(html)
    assert box.style['display'] == ('block', 'flow')
    assert not box.children