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
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
use super::*;
use crate::document::paint::geometry::AxisSelectivePaintClip;
use crate::layout::assets::{DocumentPageIndex, FragmentainerOrdinal};
use crate::layout::block::float::FLOAT_EPSILON;
use crate::layout::block::formatting_boxes_have_eligible_multicol_spanner;
use crate::layout::paint_ops::FragmentedDecorationSlice;
/// A definite block-size constraint prepared for a normal-flow replay.
///
/// CSS 2.2 requires the second pass after a winning `min-height` or
/// `max-height` constraint to use that constraint as the computed `height`.
/// CSS Box Sizing makes a fixed sizing value refer to either the content or
/// border box, so retaining that coordinate system prevents replay from
/// applying padding and borders a second time.
/// <https://www.w3.org/TR/CSS22/visudet.html#min-max-heights>
/// <https://www.w3.org/TR/css-sizing-3/#box-model>
#[derive(Debug, Clone, Copy, PartialEq)]
enum PostLayoutHeightReplay {
ContentBox(ContentBoxLength),
BorderBox(BorderBoxLength),
}
impl PostLayoutHeightReplay {
fn from_specified_length(value: LayoutLength, box_sizing: BoxSizing) -> Self {
match box_sizing {
BoxSizing::ContentBox => Self::ContentBox(content_box_pt(value.points().max(0.0))),
BoxSizing::BorderBox => Self::BorderBox(border_box_pt(value.points().max(0.0))),
}
}
fn content_box_length(self, vertical_non_content: NonContentLength) -> ContentBoxLength {
match self {
Self::ContentBox(value) => value,
Self::BorderBox(value) => border_box_to_content_box_length(value, vertical_non_content),
}
}
fn as_used_height(self) -> css::ComputedLengthPercentageOrAuto {
let value = match self {
Self::ContentBox(value) => value.points(),
Self::BorderBox(value) => value.points(),
};
css::ComputedLengthPercentageOrAuto::LengthPercentage(
css::ComputedLengthPercentage::from_points(value),
)
}
}
/// Return the min/max constraint that CSS 2.2 substitutes for `height`.
///
/// Maximum constraints apply first, then minimum constraints. The returned
/// value remains in the sizing property's specified box-model space, while
/// comparison happens in content-box space.
/// <https://www.w3.org/TR/CSS22/visudet.html#min-max-heights>
fn post_layout_height_replay_constraint(
style: &ComputedStyle,
percentage_basis: BlockSizePercentageBasis,
vertical_non_content: NonContentLength,
tentative_content_height: ContentBoxLength,
) -> Option<PostLayoutHeightReplay> {
let min = match style.box_values.min_height.clone() {
css::ComputedLengthPercentageOrAuto::LengthPercentage(value) => {
Some(PostLayoutHeightReplay::from_specified_length(
used_length_percentage(value, percentage_basis),
style.box_sizing,
))
}
_ => None,
};
let max = used_length_percentage_or_auto(style.box_values.max_height.clone(), percentage_basis)
.map(|value| PostLayoutHeightReplay::from_specified_length(value, style.box_sizing));
let after_max = max.map_or(tentative_content_height, |constraint| {
tentative_content_height.min(constraint.content_box_length(vertical_non_content))
});
if let Some(min) = min
&& after_max < min.content_box_length(vertical_non_content)
{
return Some(min);
}
max.filter(|constraint| {
tentative_content_height > constraint.content_box_length(vertical_non_content)
})
}
/// Permission to measure descendants for the deferred-overflow path.
///
/// CSS overflow can move descendant paint independently only after the
/// principal box has a definite content height. Keeping that height in this
/// capability prevents an auto-height box from being measured as if its
/// absent size were zero.
/// <https://www.w3.org/TR/css-sizing-3/#definite>
/// <https://www.w3.org/TR/css-overflow-3/#overflow>
#[derive(Debug, Clone, Copy)]
struct DeferredDescendantOverflowProbe {
content_height: DefinitePhysicalContentHeight,
vertical_root_block_size: Option<LayoutLength>,
}
/// Owns the structural child representation selected for one block layout.
///
/// A block either borrows its caller's frozen children or owns the normalized
/// stream it built locally. Keeping the two cases separate avoids a
/// self-reference when the block-layout controller itself moves to the heap.
enum BlockChildStorage<'a> {
Borrowed(Option<&'a [box_tree::FormattingBox<'a>]>),
Owned(Vec<box_tree::FormattingBox<'a>>),
}
impl<'a> BlockChildStorage<'a> {
fn as_slice(&self) -> Option<&[box_tree::FormattingBox<'a>]> {
match self {
Self::Borrowed(children) => *children,
Self::Owned(children) => Some(children),
}
}
}
/// Durable parent state retained while normal-flow descendants recurse.
///
/// `BlockLayoutGeometry` contains the resolved used style, and a list marker
/// owns another computed style. Both must survive child layout, but neither
/// belongs in every recursive block-layout frame.
struct BlockLayoutPassState<'a> {
child_storage: BlockChildStorage<'a>,
geometry: BlockLayoutGeometry,
post_layout_constraint_replay_snapshot: Option<Box<LayoutSnapshot>>,
list_marker: Option<Box<ListMarker>>,
}
impl DeferredDescendantOverflowProbe {
fn new(
content_height: Option<DefinitePhysicalContentHeight>,
has_deferred_overflow_candidate: bool,
multicol_spanner_speculation_depth: usize,
overflow_contribution: DescendantOverflowContribution,
vertical_root_block_size: Option<LayoutLength>,
) -> Option<Self> {
(has_deferred_overflow_candidate
&& multicol_spanner_speculation_depth == 0
&& overflow_contribution == DescendantOverflowContribution::Scrollable)
.then_some(content_height)
.flatten()
.map(|content_height| Self {
content_height,
vertical_root_block_size,
})
}
fn content_height(self) -> PhysicalContentHeight {
self.content_height.value()
}
fn is_vertical_root(self) -> bool {
self.vertical_root_block_size.is_some()
}
}
impl<'a> LayoutBuilder<'a> {
/// Builds the temporary style for the root's leading inline collection.
///
/// This intentionally remains out of the recursive block controller: a
/// complete computed style is needed only while collecting that initial
/// inline run.
#[inline(never)]
fn leading_inline_style_without_after(style: &ComputedStyle) -> Box<ComputedStyle> {
let mut leading_style = Box::new(style.clone());
leading_style.after_style = None;
leading_style
}
/// Resolves the auto-height margin-collapse view outside the recursive
/// block controller's frame.
#[inline(never)]
fn auto_height_margin_collapse_style(style: &ComputedStyle) -> Box<ComputedStyle> {
let mut used_style = Box::new(style.clone());
used_style
.box_values
.height
.replace_with_used(css::ComputedLengthPercentageOrAuto::Auto);
used_style
}
/// Heap-own a list marker before it can survive recursive child layout.
#[inline(never)]
fn boxed_list_marker(
&mut self,
element: &Element,
style: &ComputedStyle,
previous_containing_block_direction: Direction,
) -> Option<Box<ListMarker>> {
self.marker_for_list_item(element, style, previous_containing_block_direction)
.map(Box::new)
}
/// Captures page paint state outside the recursive block controller.
#[inline(never)]
fn boxed_paint_checkpoint(&self) -> Box<crate::document::paint::page::PaintCheckpoint> {
Box::new(self.current_page.paint_checkpoint())
}
/// Resolves the used-style facts needed before normal-flow layout.
///
/// The temporary used style is deliberately confined to this nonrecursive
/// preparation boundary. A positioned box exits here; a normal-flow box
/// retains only the small containment and spanner decisions.
#[inline(never)]
fn prepare_block_normal_flow(
&mut self,
element: &Element,
style: &ComputedStyle,
stylesheets: &Stylesheets<'_>,
child_boxes: Option<&[box_tree::FormattingBox<'_>]>,
) -> Option<(UsedPropertyContainment, bool)> {
let mut used_style = self.style_with_current_used_lengths(style);
if element.tag.eq_ignore_ascii_case("fieldset") {
// HTML gives a fieldset a used flow-root (or inline-block) role
// without changing its computed `display` value.
// <https://html.spec.whatwg.org/multipage/rendering.html#the-fieldset-and-legend-elements>
used_style.display = used_style.display.with_inner(DisplayInner::FlowRoot);
}
if matches!(used_style.position, Position::Absolute | Position::Fixed) {
self.layout_positioned_block_with_static_source(
element,
style,
stylesheets,
child_boxes,
None,
);
return None;
}
Some((
used_property_containment(element, &used_style),
self.multicol_spanner_fragmentation_depth > 0
&& self.fragmentation_suppression_depth == 0
&& used_style.column_span == css::ColumnSpan::All,
))
}
/// Replays a winning post-layout block-size constraint at an explicit
/// call boundary so the snapshot and cloned style cannot reserve space in
/// the ordinary recursive block-layout frame.
#[allow(clippy::too_many_arguments)]
#[inline(never)]
fn replay_post_layout_constraint(
&mut self,
snapshot: Box<LayoutSnapshot>,
element: &Element,
style: &ComputedStyle,
stylesheets: &Stylesheets<'_>,
run_in_children: &[box_tree::FormattingBox<'_>],
child_boxes: Option<&[box_tree::FormattingBox<'_>]>,
descendant_percentage_height_basis: Option<BlockSizePercentageBasis>,
descendant_percentage_height_context: Option<DescendantBlockPercentageContext>,
replayed_item_logical_inline_size: Option<LogicalInlineContentSize>,
principal_box_paint_mode: PrincipalBoxPaintMode,
replay_height: PostLayoutHeightReplay,
) {
let mut constrained_style = Box::new(style.clone());
constrained_style
.box_values
.height
.replace_with_used(replay_height.as_used_height());
self.restore(*snapshot);
self.layout_block_with_descendant_percentage_height_basis(
element,
&constrained_style,
stylesheets,
run_in_children,
child_boxes,
descendant_percentage_height_basis,
descendant_percentage_height_context,
replayed_item_logical_inline_size,
principal_box_paint_mode,
);
}
#[inline(never)]
fn layout_prepared_block_children(
&mut self,
input: Box<BlockFlowChildrenPhaseInput<'_, '_>>,
) -> BlockFlowChildrenPhaseOutcome {
#[cfg(all(feature = "stack-profile", target_os = "macos"))]
let _stack_profile_scope = stack_profile::enter("layout_prepared_block_children");
self.layout_block_flow_children_phase(input)
}
/// Measure a block formatting context root in one candidate float band.
///
/// The percentage basis remains the containing block's full content
/// width: float avoidance changes a BFC root's available inline space,
/// not the percentage basis inherited by its descendants.
/// <https://www.w3.org/TR/CSS22/visuren.html#floats>
#[allow(clippy::too_many_arguments)]
fn measure_float_avoiding_bfc(
&mut self,
element: &Element,
style: &ComputedStyle,
stylesheets: &Stylesheets<'_>,
child_boxes: Option<&[box_tree::FormattingBox<'_>]>,
containing_inline_size: f32,
containing_inline_span: PageInlineSpan,
band: FloatBand,
) -> FloatAvoidanceCandidate {
let band_left = band.left();
let band_width = band.width();
let auto_border_box_width = (band_width < containing_inline_size - FLOAT_EPSILON)
.then_some(float_avoiding_auto_border_box_width(
PageInlineSpan::new(band_left, band_width),
containing_inline_span,
style.margin.left,
style.margin.right,
));
let constrained_auto_width = auto_border_box_width.is_some();
let mut candidate_geometry = self.block_layout_geometry_in_inline_span(
element,
style,
stylesheets,
child_boxes,
BlockLayoutInlineConstraint {
// Width and margin resolution still use the CSS containing
// span. Only an auto-width root gets the residual band as a
// selected border-box placement below.
containing_inline_span,
percentage_basis: PercentageBasis::definite(LogicalInlineContentSize::new(
content_box_pt(containing_inline_size),
)),
physical_width_percentage_basis: PhysicalContentWidth::new(content_box_pt(
containing_inline_size,
)),
auto_border_box_width,
},
);
// A BFC root's border box may meet the float at the band edge while
// its margin overlaps the exclusion. The ordinary block-width
// resolver intentionally places a margin box from the containing
// span, so normalize the candidate back to the edge selected by float
// avoidance before testing collision:
// <https://www.w3.org/TR/CSS22/visuren.html#floats>.
if constrained_auto_width {
let border_box_width = candidate_geometry.outer_inline().width().points();
let border_box_left = if style.direction == Direction::Rtl {
band_left + (band_width - border_box_width).max(0.0)
} else {
band_left
};
candidate_geometry.reanchor_float_avoiding_border_box(PageInlineSpan::new(
border_box_left,
border_box_width,
));
}
let candidate_style = &candidate_geometry.style;
let estimated_outer_height = self
.estimate_element_height(
element,
candidate_style,
stylesheets,
candidate_geometry.outer_inline().width().points(),
child_boxes,
)
.unwrap_or(
candidate_style.margin.top
+ candidate_style.line_height
+ candidate_style.margin.bottom,
);
let border_box_height =
(estimated_outer_height - candidate_style.margin.top - candidate_style.margin.bottom)
.max(0.0);
candidate_geometry.float_avoidance_candidate(border_box_pt(border_box_height))
}
pub(in crate::layout) fn layout_block(
&mut self,
element: &Element,
style: &ComputedStyle,
stylesheets: &Stylesheets<'_>,
run_in_children: &[box_tree::FormattingBox<'_>],
child_boxes: Option<&[box_tree::FormattingBox<'_>]>,
) {
self.layout_block_with_descendant_percentage_height_basis(
element,
style,
stylesheets,
run_in_children,
child_boxes,
None,
None,
None,
PrincipalBoxPaintMode::RootPaints,
);
}
#[allow(clippy::too_many_arguments)]
pub(in crate::layout) fn layout_block_with_descendant_percentage_height_basis(
&mut self,
element: &Element,
style: &ComputedStyle,
stylesheets: &Stylesheets<'_>,
run_in_children: &[box_tree::FormattingBox<'_>],
child_boxes: Option<&[box_tree::FormattingBox<'_>]>,
descendant_percentage_height_basis: Option<BlockSizePercentageBasis>,
descendant_percentage_height_context: Option<DescendantBlockPercentageContext>,
replayed_item_logical_inline_size: Option<LogicalInlineContentSize>,
principal_box_paint_mode: PrincipalBoxPaintMode,
) {
#[cfg(all(feature = "stack-profile", target_os = "macos"))]
let _stack_profile_scope =
stack_profile::enter("layout_block_with_descendant_percentage_height_basis");
// Multicolumn layout and CSS block-in-inline splitting both require
// the normalized formatting tree. Build that structure from the
// unscaled cascade parent before this block crosses its normal-flow
// used-value boundary.
let requires_block_in_inline_normalization =
has_block_in_inline_split_boundary_with_font_metrics(
element,
style,
stylesheets,
&self.ancestors,
&mut self.font_system,
);
let requires_table_internal_fixup =
has_unwrapped_table_internal_descendant_with_font_metrics(
element,
style,
stylesheets,
&self.ancestors,
&mut self.font_system,
);
let requires_run_in_normalization = has_direct_run_in_child_with_font_metrics(
element,
style,
stylesheets,
&self.ancestors,
&mut self.font_system,
);
let requires_ruby_structure = has_ruby_formatting_descendant_with_font_metrics(
element,
style,
stylesheets,
&self.ancestors,
&mut self.font_system,
&mut self.ruby_formatting_descendants,
);
// Tree-abiding generated pseudos participate in the originating
// element's child list, including as flex and grid items. The DOM
// traversal intentionally visits only principal element children,
// while the frozen formatting tree retains `::before`/`::after`
// boxes and their display blockification.
// <https://www.w3.org/TR/css-pseudo-4/#generated-content>
// <https://www.w3.org/TR/css-position-3/#position-property>
let requires_generated_child_tree = style
.before_style
.iter()
.chain(style.after_style.iter())
.any(|pseudo| pseudo.content.is_generated());
// The HTML rendered-legend selection is defined over generated child
// boxes, not just DOM elements. Build the frozen structural tree even
// for an otherwise ordinary fieldset so direct DOM traversal and
// replay traverse the same candidate set.
// <https://html.spec.whatwg.org/multipage/rendering.html#the-fieldset-and-legend-elements>
let requires_fieldset_structure = element.tag.eq_ignore_ascii_case("fieldset");
let mut child_storage = if child_boxes.is_none()
&& (matches!(style.column_count, css::ColumnCount::Count(_))
|| matches!(style.column_width, css::ComputedColumnWidth::Length(_))
|| matches!(style.column_height, css::ComputedColumnHeight::Length(_))
// Freeze a zoomed subtree from its computed parent. The DOM
// inline fast path otherwise cascades descendants from this
// block's used values and multiplies their effective zoom.
// <https://drafts.csswg.org/css-viewport/#zoom-property>
|| style.effective_zoom.factor() != 1.0
|| requires_block_in_inline_normalization
|| requires_table_internal_fixup
|| requires_run_in_normalization
|| requires_ruby_structure
|| requires_generated_child_tree
|| requires_fieldset_structure)
{
BlockChildStorage::Owned(self.build_frozen_child_boxes_with_current_ancestors(
element,
stylesheets,
style,
))
} else {
BlockChildStorage::Borrowed(child_boxes)
};
// A rendered legend is laid out before the anonymous fieldset content
// box even when generated content or source text precedes it. Keep
// the original boxes themselves intact—only their rendering order is
// changed—so counters, selectors, and source metadata remain owned by
// their original principal or pseudo boxes.
// <https://html.spec.whatwg.org/multipage/rendering.html#the-fieldset-and-legend-elements>
if requires_fieldset_structure {
let Some(children) = child_storage.as_slice() else {
unreachable!("fieldset structure always builds child boxes")
};
if let Some(legend_index) =
box_tree::FieldsetFormattingBox::from_children(children).rendered_legend_index
{
let mut reordered = Vec::with_capacity(children.len());
reordered.push(children[legend_index].clone());
reordered.extend_from_slice(&children[..legend_index]);
reordered.extend_from_slice(&children[legend_index + 1..]);
child_storage = BlockChildStorage::Owned(reordered);
}
}
let child_boxes = child_storage.as_slice();
// Block line layout consumes its own style directly for line-height
// and baseline geometry, unlike box sizing which clones a used style
// internally. Normalize this boundary before collecting inline items.
// <https://drafts.csswg.org/css-viewport/#zoom-property>
let source_style = style;
let Some((containment, fragments_as_promoted_spanner)) =
self.prepare_block_normal_flow(element, source_style, stylesheets, child_boxes)
else {
return;
};
let fragmentainer_kind = self.active_fragmentainer_kind();
let mut geometry =
self.block_layout_geometry(element, source_style, stylesheets, child_boxes);
if let Some(logical_inline_size) = replayed_item_logical_inline_size {
// An isolated item replay can enter through a containing span that
// differs from the principal box's already-selected content-box
// inline measure. The typed replay boundary performed that box
// conversion before element dispatch; retain it for this root
// only rather than relabeling the containing availability.
// <https://www.w3.org/TR/css-flexbox-1/#flex-items>
// <https://www.w3.org/TR/css-sizing-3/#box-sizing>
// <https://www.w3.org/TR/css-writing-modes-4/#logical-to-physical>
geometry.content_logical_inline_size = logical_inline_size;
}
// All normal-flow state that must remain valid while descendants
// recurse belongs to this controller. In particular, geometry owns
// a full used style and the child representation may own a frozen
// formatting tree.
let mut state = Box::new(BlockLayoutPassState {
child_storage,
geometry,
post_layout_constraint_replay_snapshot: None,
list_marker: None,
});
let BlockLayoutPassState {
child_storage,
geometry,
post_layout_constraint_replay_snapshot,
list_marker,
} = &mut *state;
let child_boxes = child_storage.as_slice();
let definite_principal_fits_current_column = fragmentainer_kind
== FragmentainerKind::Column
&& !fragments_as_promoted_spanner
&& geometry.style.writing_mode == WritingMode::HorizontalTb
&& !containment.size
&& !fragmentainer_kind.is_forced_break(geometry.style.break_before)
&& !formatting_boxes_have_forced_break_in(child_boxes, fragmentainer_kind)
&& geometry.definite_content_height.is_some_and(|height| {
let available =
(self.cursor_y + geometry.relative_offset.y() - self.page_bottom()).max(0.0);
let outer_height = geometry.style.margin.top
+ geometry.vertical_non_content.points()
+ height.value().points().max(0.0)
+ geometry.style.margin.bottom;
outer_height <= available + 0.01
});
let definite_vertical_root_block_size = (fragmentainer_kind == FragmentainerKind::Page
&& geometry.style.writing_mode == self.principal_flow.writing_mode
&& self.containing_block_writing_mode == self.principal_flow.writing_mode
&& WritingModeAxes::new(
self.principal_flow.writing_mode,
self.principal_flow.used_direction(),
)
.swaps_physical_axes()
&& matches!(
geometry.style.box_values.width.clone(),
css::ComputedLengthPercentageOrAuto::LengthPercentage(_)
)
&& geometry.outer_inline().width().points()
> self
.current_page_context
.logical_block_size(self.principal_flow.writing_mode)
+ 0.01)
.then(|| layout_pt(geometry.outer_inline().width().points()));
let deferred_overflow_probe = DeferredDescendantOverflowProbe::new(
geometry.definite_content_height,
fragments_as_promoted_spanner
|| definite_principal_fits_current_column
|| definite_vertical_root_block_size.is_some(),
self.multicol_spanner_speculation_depth,
descendant_overflow_contribution_for_element(element, &geometry.style),
definite_vertical_root_block_size,
);
if let Some(probe) = deferred_overflow_probe {
let descendants_overflow = if probe.is_vertical_root() {
// The speculative pass is authoritative: logical block-size
// estimation cannot see every normal-flow formatting-box shape.
// Enter it for a fixed vertical root block with in-flow children
// and retain only the captured range that actually paints.
child_boxes.map_or_else(
|| {
has_direct_flow_child_with_font_metrics(
element,
&geometry.style,
stylesheets,
&mut self.font_system,
)
},
|children| {
children.iter().any(|child| match child {
box_tree::FormattingBox::AnonymousBlock(_) => true,
_ => child.element_parts().is_some_and(|(_, _, child_style, _)| {
style_is_in_normal_flow(child_style)
&& child_style.display.is_block_level()
}),
})
},
)
} else {
self.definite_block_descendants_overflow(
child_boxes,
stylesheets,
geometry.content_inline().width().points(),
probe,
)
};
if descendants_overflow {
self.layout_definite_block_with_deferred_descendant_overflow(
element,
&geometry.style,
stylesheets,
run_in_children,
child_boxes,
descendant_percentage_height_basis,
descendant_percentage_height_context,
replayed_item_logical_inline_size,
probe,
geometry.vertical_non_content,
principal_box_paint_mode,
);
return;
}
}
// CSS 2 resolves a winning min/max constraint by redoing layout with
// the constrained value as the used height. Keep a rollback point so
// a post-layout constraint can replay the complete fragmentation
// decision, rather than retagging fragments that were assigned while
// the principal box was still auto-sized.
// <https://www.w3.org/TR/CSS2/visudet.html#min-max-heights>
let may_need_post_layout_constraint_replay = geometry.definite_content_height.is_none()
&& (used_min_height(&geometry.style, geometry.containing_block_content_height)
.is_some()
|| used_non_replaced_max_height(
&geometry.style,
geometry.containing_block_percentage_context,
)
.is_some());
*post_layout_constraint_replay_snapshot =
may_need_post_layout_constraint_replay.then(|| Box::new(self.snapshot()));
self.begin_clamp_line_slot_capture();
self.apply_forced_break_before_box_in(fragmentainer_kind, &geometry.style);
// A class-A prebreak repositions this source box before it has made
// layout progress. Recompute geometry once in the destination because
// that fragmentainer can have different available space, but never
// retry the same source box again: a repeated prebreak can otherwise
// create an unbounded sequence of empty fragmentainers.
// <https://www.w3.org/TR/css-break-3/#possible-breaks>
let mut prebroken_before_layout = false;
loop {
let prebreak_content_height = geometry
.definite_content_height
.map(|height| height.value().points())
.or_else(|| {
// A clipped overflow box is a non-fragmenting formatting
// context. When `break-inside` avoids this fragmentainer,
// measure its auto content so the class-A break before the
// box can keep that otherwise monolithic box intact.
// <https://www.w3.org/TR/css-break-3/#break-inside>
(fragmentainer_kind.avoids_break_inside(&geometry.style)
&& self.element_used_overflow_clips(element, &geometry.style))
.then(|| {
self.estimate_block_like_height(
element,
&geometry.style,
stylesheets,
geometry.content_inline().width().points(),
child_boxes,
) - geometry.style.margin.top
- geometry.vertical_non_content.points()
- geometry.style.margin.bottom
})
})
.or_else(|| {
let establishes_multicol =
matches!(geometry.style.column_count, css::ColumnCount::Count(_))
|| matches!(
geometry.style.column_width,
css::ComputedColumnWidth::Length(_)
)
|| matches!(
geometry.style.column_height,
css::ComputedColumnHeight::Length(_)
);
(fragmentainer_kind == FragmentainerKind::Column && establishes_multicol)
.then(|| {
child_boxes.and_then(|children| {
self.estimate_multicol_auto_block_size(
&geometry.style,
stylesheets,
children,
geometry.content_inline().width().points(),
)
})
})
.flatten()
});
let current_fragmentainer = self.fragmentainer_from_page_cursor(
PageTopBlockPosition::new(self.cursor_y + geometry.relative_offset.y()),
);
let empty_destination_fragmentainer = match fragmentainer_kind {
FragmentainerKind::Page => {
let next_context = self
.fragmentainer_override
.filter(|override_| override_.kind == FragmentainerKind::Page)
.map(|override_| override_.context_for_fragmentainer(self.pages.len() + 1))
.unwrap_or_else(|| {
self.resolved_page_context(
self.destination_document_page_number(self.pages.len() + 2),
false,
)
});
Fragmentainer::new(
layout_pt(next_context.area_height()),
layout_pt(next_context.area_height()),
)
}
FragmentainerKind::Column => {
let next_capacity = self
.fragmentainer_override
.map(|override_| {
override_
.context_for_fragmentainer(self.pages.len() + 1)
.area_height()
})
.unwrap_or_else(|| self.page_area_height());
Fragmentainer::new(layout_pt(next_capacity), layout_pt(next_capacity))
}
};
let should_prebreak = !prebroken_before_layout
&& self.out_of_flow_prebreak_suppression_depth == 0
&& !fragments_as_promoted_spanner
&& should_prebreak_definite_block(DefiniteBlockBreakContext {
// A multicol formatting context has a measurable row-grid
// block size even when its principal `height` is auto. If
// its first anonymous column cannot make progress in the
// remaining outer column but the complete grid fits in an
// empty one, CSS fragmentation places it at that earlier
// class-A opportunity instead of creating a subpixel first
// column.
// <https://www.w3.org/TR/css-break-3/#unforced-breaks>
definite_content_height: prebreak_content_height,
vertical_non_content: geometry.vertical_non_content,
style: &geometry.style,
current_fragmentainer,
empty_destination_fragmentainer,
fragmentainer_has_occupied_flow: self.current_page_has_content()
|| self.cursor_y < self.page_top() - 0.01,
at_page_top: self.cursor_is_at_page_top(),
suppress_for_avoid_retry: self.avoid_inside_retry_depth > 0,
});
if !should_prebreak {
break;
}
self.push_page();
*geometry = self.block_layout_geometry(element, source_style, stylesheets, child_boxes);
prebroken_before_layout = true;
}
let style = &geometry.style;
// Inline atomic descendants can be measured while preparing the
// block's child-flow strategy, before the child phase itself begins.
// A supplied flex replay basis is authoritative, including an
// explicit indefinite value; otherwise use the principal block's
// definite content height.
// <https://drafts.csswg.org/css-flexbox/#definite-sizes> and
// <https://www.w3.org/TR/css-sizing-3/#percentage-sizing>
let preparatory_descendant_percentage_context = descendant_percentage_height_context
.or_else(|| {
descendant_percentage_height_basis
.map(DescendantBlockPercentageContext::from_percentage_basis)
})
.unwrap_or_else(|| {
DescendantBlockPercentageContext::formatting_context(
geometry
.definite_content_height
.map(|height| height.value().content_box_length()),
BlockSizeBasisSource::ContainingBlock,
)
});
let preparatory_descendant_percentage_context = if preparatory_descendant_percentage_context
.is_definite()
|| element.document_compatibility_mode != dom::DocumentCompatibilityMode::Quirks
{
preparatory_descendant_percentage_context
} else {
// HTML's documented parsing modes, rather than source-text
// inference, select this browser-compatible quirks behavior.
// Preserve a definite ancestor block basis through auto-height
// wrappers only in a quirks document.
self.block_percentage_context_stack.current_context()
};
let preparatory_descendant_percentage_basis =
preparatory_descendant_percentage_context.percentage_basis();
self.block_percentage_context_stack
.push_context(preparatory_descendant_percentage_context);
let defer_own_decoration_promotion = self.defer_next_block_decoration_promotion;
self.defer_next_block_decoration_promotion = false;
let mut suppress_own_principal_box_decoration = !principal_box_paint_mode.root_paints();
let containing_left = self.content_left;
let containing_right = self.content_right;
let containing_inline_size = (containing_right - containing_left).max(0.0);
// Relative positioning normally enters before descendant layout so
// the descendants paint in the shifted coordinate space. A cleared
// relative box is the exception: `clear` must first resolve at its
// unshifted normal-flow border edge (CSS 2.2, 9.5.2).
if matches!(
geometry.style.position,
Position::Relative | Position::Sticky
) && geometry.style.clear == Clear::None
{
self.cursor_y += geometry.relative_offset.y();
}
let mut block_align_content_offset_y = 0.0;
let starts_at_page_top = self.cursor_is_at_page_top() && self.truncate_page_start_margins;
// CSS 2.2 defines clearance from the hypothetical border edge after
// adjoining parent/first-child margins have collapsed. Resolve that
// complete start-margin set before moving the border edge for `clear`;
// the child traversal receives an explicit marker so it does not
// consume that same descendant contribution a second time.
// <https://www.w3.org/TR/CSS22/visuren.html#flow-control>
// <https://www.w3.org/TR/CSS22/box.html#collapsing-margins>
// The root margin is a canvas inset and cannot collapse through its
// first child. The selected `body` remains a normal block parent for
// its in-flow children, so its adjoining first-child margin still
// collapses under CSS 2.2's ordinary block formatting rules.
//
// Treating the body itself as a collapse boundary adds its canvas
// margin to the child's own start margin, unlike browser layout.
// <https://www.w3.org/TR/CSS22/box.html#collapsing-margins>
let is_document_canvas = self.element_uses_document_canvas_flow(element);
let can_adjoin_first_child_margin =
!is_document_canvas || !element.tag.eq_ignore_ascii_case("html");
let hypothetical_start_margin =
if geometry.style.clear != Clear::None && can_adjoin_first_child_margin {
if let Some(children) = child_boxes {
if can_collapse_block_start_margin(
element,
&geometry.style,
geometry.border_edges,
has_direct_inline_content_box(children),
self.used_overflow_for_element(element, &geometry.style),
) {
clear_none_hypothetical_start_margin_from_boxes(
element,
&geometry.style,
children,
self.document_canvas_overflow,
)
.map(SemanticLengthExt::points)
.unwrap_or(geometry.style.margin.top)
} else {
geometry.style.margin.top
}
} else if can_collapse_block_start_margin(
element,
&geometry.style,
geometry.border_edges,
has_direct_inline_content_before_first_flow_child_dom_with_font_metrics(
element,
&geometry.style,
stylesheets,
&self.ancestors,
&mut self.font_system,
),
self.used_overflow_for_element(element, &geometry.style),
) {
let mut resolver = DomStyleResolver::with_font_system(&mut self.font_system);
clear_none_hypothetical_start_margin_dom_with_resolver(
element,
&geometry.style,
stylesheets,
&self.ancestors,
&mut resolver,
self.document_canvas_overflow,
)
.map(SemanticLengthExt::points)
.unwrap_or(geometry.style.margin.top)
} else {
geometry.style.margin.top
}
} else {
geometry.style.margin.top
};
let applied_start_margin =
page_start_margin(layout_pt(hypothetical_start_margin), starts_at_page_top);
// A transparent parent may have received only its local placement
// delta after an ancestor consumed part of a larger adjoining start
// margin set. Its own first child must compare against that complete
// set, or the descendant contribution is applied a second time.
let inherited_adjoining_start_margin = self
.inherited_adjoining_start_margins
.current_for(element.id);
let inherited_float_replay_clearance_boundary =
self.current_float_replay_clearance_boundary();
let mut introduced_float_replay_clearance_boundary = None;
// The inherited value is the complete adjoining margin set, not a
// margin that has already survived destination-fragmentainer
// truncation. A transparent wrapper can propagate that set after an
// avoided ancestor moves to a fresh fragmentainer. Apply the same
// page-start rule as the wrapper's own margin before handing the set
// to its first child, or each nested wrapper subtracts the discarded
// source-fragment margin again and pulls the child above the page area.
// <https://www.w3.org/TR/css-break-3/#break-margins>
let descendant_applied_start_margin = inherited_adjoining_start_margin
.map(InheritedAdjoiningStartMargin::complete_margin)
.map(|margin| page_start_margin(margin, starts_at_page_top))
.unwrap_or(applied_start_margin);
let margin_edge_top = self.cursor_y;
self.cursor_y -= applied_start_margin.points();
let establishes_independent_bfc = geometry
.style
.display
.establishes_block_formatting_context()
|| style_establishes_line_clamp_formatting_context(&geometry.style)
|| layout_containment_applies_to_element(element, &geometry.style)
|| paint_containment_applies_to_element(element, &geometry.style)
|| self.element_used_overflow_clips(element, &geometry.style)
|| block_align_content_establishes_independent_formatting_context(
geometry.style.align_content,
);
let start_margin_arrangement = if !establishes_independent_bfc {
let clearance = self.resolve_block_clearance(BlockClearanceRequest::block_flow(
geometry.style.clear,
FloatPlacementAxes::new(
self.containing_block_writing_mode,
self.containing_block_direction,
),
PageTopBlockPosition::new(self.cursor_y),
PageTopBlockPosition::new(margin_edge_top),
applied_start_margin,
(geometry.style.clear != Clear::None)
.then(|| {
inherited_adjoining_start_margin
.map(InheritedAdjoiningStartMargin::parent_start_clearance_hypothesis)
})
.flatten(),
));
if clearance.clearance.is_introduced() {
introduced_float_replay_clearance_boundary = Some(
FloatReplayClearanceBoundary::new(clearance.used_border_edge),
);
}
let start_margin_arrangement = match clearance.clearance {
BlockClearance::NotIntroduced => BlockStartMarginArrangement::Adjoining {
applied_start_margin: descendant_applied_start_margin,
},
introduced => {
BlockStartMarginArrangement::from_clearance(introduced, applied_start_margin)
}
};
self.cursor_y = match start_margin_arrangement {
BlockStartMarginArrangement::Adjoining { .. } => {
clearance.used_border_edge.points()
}
BlockStartMarginArrangement::SeparatedByClearance {
adjusted_top_margin,
} => adjusted_top_margin
.border_edge_from(PageTopBlockPosition::new(margin_edge_top))
.points(),
};
// A zero-height cleared block can still be the first normal-flow
// content in its destination column. Keep that column before
// temporary multicolumn pages are projected; float continuation
// itself remains a parallel flow and never sets this ownership.
// <https://drafts.csswg.org/css-break/#parallel-flows>
if clearance.fragmentainer_progress.advanced() {
self.mark_current_page_flow_content();
}
start_margin_arrangement
} else {
BlockStartMarginArrangement::Adjoining {
applied_start_margin: descendant_applied_start_margin,
}
};
let float_replay_clearance_boundary = if establishes_independent_bfc {
None
} else {
introduced_float_replay_clearance_boundary.or(inherited_float_replay_clearance_boundary)
};
if establishes_independent_bfc && geometry.style.float == Float::None {
// A BFC root's `clear` is resolved from its hypothetical margin
// edge. This permits negative clearance when its start margin
// would otherwise put the border edge below an adjoining float.
let bfc_clearance = self.resolve_block_clearance(BlockClearanceRequest::bfc_root(
geometry.style.clear,
FloatPlacementAxes::new(
self.containing_block_writing_mode,
self.containing_block_direction,
),
PageTopBlockPosition::new(margin_edge_top),
PageTopBlockPosition::new(self.cursor_y),
applied_start_margin,
));
let bfc_clearance_applied = bfc_clearance.clearance.is_introduced();
if bfc_clearance_applied {
self.cursor_y = bfc_clearance.used_border_edge.points();
}
if bfc_clearance.fragmentainer_progress.advanced() {
self.mark_current_page_flow_content();
}
if self.containing_block_writing_mode == WritingMode::HorizontalTb
&& geometry.style.writing_mode == WritingMode::HorizontalTb
{
let context = self
.float_contexts
.last()
.expect("root float context exists")
.clone();
let page_index = self.current_float_page_index();
if context.has_css_float_on_page(page_index) {
let clear = if bfc_clearance_applied {
Clear::None
} else {
geometry.style.clear
};
let placement_axes = FloatPlacementAxes::new(
self.containing_block_writing_mode,
self.containing_block_direction,
);
let containing_inline_span =
PageInlineSpan::from_edges(containing_left, containing_right);
let normal_border_top = self.cursor_y;
let mut solve_placement = |top| {
context.avoiding_bfc_root_position(
page_index,
top,
clear,
placement_axes,
containing_left,
containing_right,
|band, _candidate_top| {
self.measure_float_avoiding_bfc(
element,
style,
stylesheets,
child_boxes,
containing_inline_size,
containing_inline_span,
band,
)
},
)
};
// A positive adjoining start margin can cross an active float
// before it puts the BFC root's border box below that float.
// Test the margin edge first. When the root cannot occupy
// that band, retain only the portion of the margin needed to
// reach the float's block-end edge, just as clearance does.
// If it fits beside the float, the normal margin placement is
// retained and solved at the border edge below.
// <https://www.w3.org/TR/CSS22/visuren.html#floats>
let margin_edge_placement = (applied_start_margin.points() > FLOAT_EPSILON
&& !bfc_clearance_applied)
.then(|| solve_placement(PageTopBlockPosition::new(margin_edge_top)));
let placement = match margin_edge_placement {
Some(placement)
if placement.placement.origin.top_y()
< margin_edge_top - FLOAT_EPSILON
&& placement.placement.origin.top_y()
> normal_border_top + FLOAT_EPSILON =>
{
placement
}
_ => solve_placement(PageTopBlockPosition::new(normal_border_top)),
};
self.cursor_y = placement.placement.origin.top_y();
let available_span = placement.placement.available_span;
let containing_inline_span =
PageInlineSpan::from_edges(containing_left, containing_right);
let auto_border_box_width = (available_span.width()
< containing_inline_size - FLOAT_EPSILON)
.then_some(float_avoiding_auto_border_box_width(
available_span,
containing_inline_span,
style.margin.left,
style.margin.right,
));
let constrained_auto_width = auto_border_box_width.is_some();
*geometry = self.block_layout_geometry_in_inline_span(
element,
style,
stylesheets,
child_boxes,
BlockLayoutInlineConstraint {
// The residual band controls auto-width sizing, but it
// is not the block's CSS containing span. Resolve the
// final width and percentage-dependent geometry from
// the original containing block, then apply the
// measured candidate's border-box origin below.
containing_inline_span,
percentage_basis: PercentageBasis::definite(
LogicalInlineContentSize::new(content_box_pt(
containing_inline_size,
)),
),
physical_width_percentage_basis: PhysicalContentWidth::new(
content_box_pt(containing_inline_size),
),
auto_border_box_width,
},
);
if constrained_auto_width {
geometry.reanchor_float_avoiding_border_box(
placement.candidate.normal_flow_border_box_inline_span,
);
}
}
} else {
// In a vertical principal flow, float avoidance advances the
// physical block-axis slab. Reuse the logical exclusion query
// used by table, grid, flex, and replaced BFC roots, then
// project its selected physical origin into this block's
// pre-layout border-box geometry.
// <https://www.w3.org/TR/CSS22/visuren.html#floats>
// <https://www.w3.org/TR/css-writing-modes-4/#logical-to-physical>
let border_box_width = geometry.outer_inline().width().points();
let estimated_outer_height = self
.estimate_element_height(
element,
&geometry.style,
stylesheets,
geometry.outer_inline().width().points(),
child_boxes,
)
.unwrap_or(
geometry.style.margin.top
+ geometry.style.line_height
+ geometry.style.margin.bottom,
);
let placement = self.place_float_avoiding_margin_box(
PageTopBlockPosition::new(self.cursor_y),
margin_box_size_pt(
geometry.style.margin.left + border_box_width + geometry.style.margin.right,
estimated_outer_height,
),
geometry.style.clear,
self.containing_block_direction,
);
// A `vertical-rl` root with a short enough physical inline
// extent fits in the inline track below an inline-start
// (physical-top) float. It should move down that inline axis,
// rather than needlessly creating the next block-axis slab.
// An over-tall root still uses the slab selected above.
// <https://www.w3.org/TR/css-writing-modes-4/#block-flow>
let inline_fit_below_top_float = (self.containing_block_writing_mode
== WritingMode::VerticalRl)
.then(|| {
self.float_contexts
.last()
.expect("root float context exists")
.shapes
.iter()
.filter(|shape| {
shape.is_css_float()
&& shape.page_index == self.current_float_page_index()
&& shape.side == UsedFloatSide::Top
})
.map(|shape| shape.physical_margin_box().bottom_y())
.filter(|bottom| {
estimated_outer_height
<= *bottom - self.page_bottom() + FLOAT_EPSILON
})
.min_by(f32::total_cmp)
})
.flatten();
self.cursor_y = inline_fit_below_top_float.unwrap_or(placement.origin.top_y());
geometry.reanchor_float_avoiding_border_box(PageInlineSpan::new(
placement.origin.x() + geometry.style.margin.left,
border_box_width,
));
}
}
let selected_orthogonal_inline_layout = geometry.selected_orthogonal_inline_layout.take();
let style = &geometry.style;
let height_depends_on_intrinsic_content =
needs_intrinsic_height_contribution(style.box_values.height.value().clone())
|| needs_intrinsic_height_contribution(style.box_values.min_height.clone())
|| needs_intrinsic_height_contribution(style.box_values.max_height.clone());
let block_line_trim = self.effective_text_box_line_trim_for_style(style);
let relative_offset = geometry.relative_offset;
// Relative positioning shifts the box's painted position and its
// descendants, but it does not change the box's normal-flow position.
// Resolve margins, `clear`, and BFC float avoidance first; applying a
// relative block offset before clearance would let clearance cancel a
// negative `top` offset and incorrectly change following flow.
// <https://www.w3.org/TR/CSS22/visuren.html#relative-positioning>
if matches!(style.position, Position::Relative | Position::Sticky)
&& style.clear != Clear::None
{
self.cursor_y += relative_offset.y();
}
let border_edges = geometry.border_edges;
let border_widths = border_edges.to_css_edges();
let vertical_non_content = geometry.vertical_non_content;
let principal_fragment_decoration =
FragmentDecoration::for_box_decoration_break(style.box_decoration_break, false, false);
let principal_fragment_decoration_reservation = FragmentDecorationReservation::new(
principal_fragment_decoration,
non_content_pt(border_widths.top + style.padding.top),
non_content_pt(style.padding.bottom + border_widths.bottom),
);
let vertical_extras = vertical_non_content.points();
let containing_block_content_height = geometry.containing_block_content_height;
let containing_block_percentage_context = geometry.containing_block_percentage_context;
let containing_block_height_basis = containing_block_content_height;
let definite_content_height_for_children = geometry.definite_content_height;
let definite_content_height =
definite_content_height_for_children.map(|height| height.value().points());
// Multicolumn sizing consumes the used block constraint before it
// chooses a local column set. Resolve `lh` here, at the block's used
// line-height boundary, so a finite `height: 2lh` does not degrade
// into an auto-height balanced row.
let mut multicol_used_height = style.box_values.height.clone();
multicol_used_height.resolve_line_height_relative_lengths(layout_pt(style.line_height));
let multicol_content_height =
definite_content_height.or_else(|| multicol_used_height.length_if_no_percent());
let outer_inline = geometry.outer_inline();
let content_inline = geometry.content_inline();
let content_width = content_inline.width().points();
let mut content_logical_inline_size = geometry.content_logical_inline_size().points();
if element.tag.eq_ignore_ascii_case("body")
&& self.principal_flow.is_source_body(element)
&& style.writing_mode == WritingMode::VerticalRl
{
// A propagated body is the initial canvas. Its visual inline-end
// margin offsets painting, but does not shorten the inline span
// available to its canvas descendants.
// <https://www.w3.org/TR/css-writing-modes-4/#principal-flow>
content_logical_inline_size +=
match inline_start_side(style.writing_mode, style.used_direction()) {
PhysicalSide::Top => style.margin.bottom,
PhysicalSide::Bottom => style.margin.top,
PhysicalSide::Left | PhysicalSide::Right => {
unreachable!("a vertical writing mode must have a vertical inline axis")
}
};
}
let built_multicol_child_boxes;
let child_boxes = if child_boxes.is_none()
&& (matches!(style.column_count, css::ColumnCount::Count(_))
|| matches!(style.column_width, css::ComputedColumnWidth::Length(_))
|| matches!(style.column_height, css::ComputedColumnHeight::Length(_)))
{
built_multicol_child_boxes = self.build_frozen_child_boxes_with_current_ancestors(
element,
stylesheets,
source_style,
);
Some(built_multicol_child_boxes.as_slice())
} else {
child_boxes
};
let outer_x = outer_inline.span().left_x();
let inner_x = content_inline.span().left_x();
let inner_width = content_inline.width().points();
if self.principal_flow.is_source_body(element)
&& inline_start_side(style.writing_mode, style.used_direction()) == PhysicalSide::Bottom
{
self.principal_inline_end_inset = style.margin.bottom;
}
if self.principal_flow.is_source_body(element)
&& self.principal_flow.writing_mode == WritingMode::HorizontalTb
{
self.principal_body_block_end_inset = layout_pt(style.margin.right);
}
// The propagated body is the first box entered from the document's
// used principal flow. A bottom-origin vertical principal flow cannot
// inherit the legacy page-top cursor at that boundary: its first
// child must start at physical page bottom before ordinary decreasing
// Y block layout converts that inline progress into page coordinates.
// Later descendants retain their own containing-block cursor.
// <https://drafts.csswg.org/css-writing-modes-4/#principal-flow>
// <https://drafts.csswg.org/css-writing-modes-4/#logical-to-physical>
if self.principal_flow.is_source_body(element)
&& inline_start_side(style.writing_mode, style.used_direction()) == PhysicalSide::Bottom
{
self.cursor_y = self.page_bottom();
}
// <https://www.w3.org/TR/css-writing-modes-4/#logical-to-physical>
let block_top = self.cursor_y;
let propagated_vertical_body_canvas = self.principal_flow.is_source_body(element)
&& WritingModeAxes::new(style.writing_mode, style.used_direction())
.swaps_physical_axes();
if propagated_vertical_body_canvas
&& self.root_principal_flow_context.active_canvas.is_none()
{
self.root_principal_flow_context.active_canvas = Some(ActiveDocumentCanvas {
body: Some(element.id),
inline_end_inset: layout_pt(
match inline_start_side(style.writing_mode, style.used_direction()) {
PhysicalSide::Top => style.margin.bottom,
PhysicalSide::Bottom => style.margin.top,
PhysicalSide::Left | PhysicalSide::Right => {
unreachable!("a vertical writing mode must have a vertical inline axis")
}
},
),
inline_origin: PageTopBlockPosition::new(block_top),
block_track_occupancy: layout_pt(
(self.current_page_context.right() - self.page_left()).max(0.0),
),
trailing_child_block_margin: layout_pt(0.0),
});
}
let mut fragmented_definite_block = false;
let mut vertical_root_fragmented_block = false;
let mut vertical_root_page_fragmentation = None;
let principal_block_size_disposition = definite_content_height.map_or(
PrincipalBlockSizeDisposition::ContentSized,
|content_height| {
PrincipalBlockSizeDisposition::Fixed(
FixedPrincipalBlockSize::from_resolved_content_height(
PhysicalContentHeight::new(content_box_pt(content_height)),
style.box_sizing,
vertical_non_content,
),
)
},
);
// Fixed-principal fragmentation has a stronger invariant than a
// numeric used height: an automatic block can have a final used
// height, including one constrained by min/max sizing, without having
// supplied a definite size before its descendants were laid out.
// Keep the fragmentation capability tied to the disposition rather
// than letting later cursor arithmetic erase that distinction.
// <https://www.w3.org/TR/CSS2/visudet.html#min-max-heights>
// <https://www.w3.org/TR/css-break-3/#breaking-rules>
let fixed_principal_block_size = match principal_block_size_disposition {
PrincipalBlockSizeDisposition::ContentSized => None,
PrincipalBlockSizeDisposition::Fixed(size) => Some(size),
};
debug_assert!(match principal_block_size_disposition {
PrincipalBlockSizeDisposition::ContentSized => true,
PrincipalBlockSizeDisposition::Fixed(size) => match size.specified_box() {
FixedPrincipalBlockSpecifiedBox::ContentBox => true,
FixedPrincipalBlockSpecifiedBox::BorderBox(border_box_height) => {
border_box_height.points() + 0.01 >= size.content_height().points()
}
},
});
// In a vertical writing mode, physical `height` is the logical inline
// size. Keep the already-resolved principal-box disposition distinct
// from selected text occupancy: a line sequence can size an automatic
// box but cannot replace this definite used inline size.
// <https://www.w3.org/TR/css-writing-modes-4/#dimension-mapping>
let vertical_inline_size_is_definite =
style.writing_mode.has_vertical_lines() && fixed_principal_block_size.is_some();
let positioning_containing_block_mode =
PositionedContainingBlockMode::for_element(element, style);
let paint_checkpoint = self.boxed_paint_checkpoint();
let paint_page_index = self.pages.len();
let positioned_layer_start = self.positioned_layers.len();
let positioned_overflow_clip_boundary = PositionedOverflowClipBoundary::new(
self.containing_blocks.len(),
self.fixed_containing_blocks.len(),
);
let pending_paint_fragment_start = self.pending_paint_fragments.len();
let pending_positioned_page_span_target_at_start = self
.pending_positioned_fragmentation
.materialized_destination_end();
let static_scroll_snap_scope =
self.begin_static_scroll_snap_scope(style, element.tag.eq_ignore_ascii_case("html"));
let block_start_page_context = self.current_page_context;
let block_start_page_index = self.pages.len();
self.cursor_y -= border_widths.top + style.padding.top;
let content_top = self.cursor_y;
let first_fragment_start = self.current_page_context.top() - content_top;
let decoration_reservation = FragmentDecorationReservation::new(
FragmentDecoration::for_box_decoration_break(style.box_decoration_break, false, false),
non_content_pt(border_widths.top + style.padding.top),
non_content_pt(style.padding.bottom + border_widths.bottom),
);
// The generic page-flow cursor is physical top-to-bottom. Vertical
// root fragmentation has a separate logical projection and must not
// inherit these physical continuation insets.
let fragment_top_offset = if style.writing_mode == WritingMode::HorizontalTb
&& style.box_decoration_break == css::BoxDecorationBreak::Clone
{
FragmentTopOffset::cloned_block_decoration(
first_fragment_start,
decoration_reservation.block_start().points(),
decoration_reservation.block_end().points(),
)
} else {
FragmentTopOffset::unreserved(first_fragment_start)
};
self.fragment_top_offsets.push(fragment_top_offset);
self.add_bookmark(element, style, paint_space_point(inner_x, block_top));
self.add_page_anchor(element, style);
let descendant_bookmark_start = self.bookmarks.len();
let previous_left = self.content_left;
let previous_right = self.content_right;
let previous_containing_block_direction = self.containing_block_direction;
let previous_containing_block_writing_mode = self.containing_block_writing_mode;
let is_vertical_orthogonal_flow = style.writing_mode.has_vertical_lines()
&& writing_modes_are_orthogonal(
previous_containing_block_writing_mode,
style.writing_mode,
);
let is_vertical_parallel_flow_auto_inline_size = style.writing_mode.has_vertical_lines()
&& !writing_modes_are_orthogonal(
previous_containing_block_writing_mode,
style.writing_mode,
)
&& style.box_values.height.is_auto()
&& !needs_intrinsic_height_contribution(style.box_values.min_height.clone())
&& !needs_intrinsic_height_contribution(style.box_values.max_height.clone());
self.content_left = inner_x;
self.content_right = inner_x + inner_width;
// A contained root or body keeps its ordinary principal box, but it
// does not supply the document canvas. Canvas-only geometry (the
// fragment insets consumed by descendants and viewport overflow) must
// follow the used propagation result rather than the element name.
// <https://drafts.csswg.org/css-contain-1/#containment-layout>
if is_document_canvas {
self.document_canvas_fragment_insets.push(FragmentOffsets {
left: inner_x - self.current_page_context.left(),
right: self.current_page_context.right() - (inner_x + inner_width),
top: self.current_page_context.top() - content_top,
});
}
// A containing block exports its used inline base direction. In
// vertical typographic mode, `text-orientation: upright` forces this
// value to LTR without changing the computed value inherited by an
// orthogonal descendant.
// <https://drafts.csswg.org/css-writing-modes-4/#text-orientation>
self.containing_block_direction = style.used_direction();
self.containing_block_writing_mode = style.writing_mode;
self.content_logical_inline_size_stack
.push(content_logical_inline_size);
let parent_child_available_space = self.current_child_available_space();
let inherited_orthogonal_available_height =
parent_child_available_space.orthogonal_available_height;
let mut child_available_space = child_available_space_for_block(
style,
PhysicalContentWidth::new(content_inline.width()),
definite_content_height_for_children,
inherited_orthogonal_available_height,
self.initial_containing_block_physical_height(),
);
if style.writing_mode.has_vertical_lines() && style.box_values.width.is_auto() {
// The used physical width of an auto-sized vertical block may be
// content-derived, but that does not make it a percentage basis
// for an orthogonal child: resolving the child from that value
// would create the sizing cycle described by CSS Writing Modes.
// Export the containing block's available physical-width basis
// instead. This is normal orthogonal-flow behavior, not a
// document-canvas exception; an unpropagated body still obtains
// its basis from its actual containing block through this path.
// <https://www.w3.org/TR/css-writing-modes-4/#orthogonal-flows>
child_available_space = child_available_space
.with_orthogonal_physical_width_percentage_basis(
parent_child_available_space.orthogonal_physical_width_percentage_basis,
);
}
self.child_available_space_stack.push(child_available_space);
let container_unit_scope = self.push_container_unit_context(
style,
PhysicalContentWidth::new(content_inline.width()),
PhysicalContentHeight::new(content_box_pt(definite_content_height.unwrap_or(0.0))),
);
if establishes_independent_bfc {
self.push_float_context();
}
let positioned_containing_block_scope =
if let Some(mode) = positioning_containing_block_mode {
// Absolute descendants resolve percentage block sizes against the
// positioned ancestor's final padding box. An auto-height block
// gets that height from its in-flow children, which have not yet
// been laid out at this point. Estimate the same normal-flow
// contribution before entering the positioned-descendant scope so
// those descendants do not observe the line-height placeholder.
// The ordinary child pass remains authoritative for final flow
// geometry and fragmentation.
// <https://www.w3.org/TR/css-position-3/#def-cb>
let positioning_content_height = definite_content_height.unwrap_or_else(|| {
(self.estimate_block_like_height(
element,
&geometry.style,
stylesheets,
content_width,
None,
) - geometry.style.margin.top
- geometry.style.margin.bottom
- vertical_extras)
.max(0.0)
});
let containing_block = ContainingBlock::from_page_top_rect(
geometry.padding_box_top_rect(block_top, positioning_content_height),
);
Some(self.push_positioned_containing_block(mode, containing_block))
} else {
None
};
let overflow_clip_content_height = (!height_depends_on_intrinsic_content)
.then(|| {
used_content_box_height_or_auto(
style,
layout_pt(self.page_area_height()),
vertical_non_content,
)
.map(SemanticLengthExt::points)
})
.flatten()
.map(|height| {
constrain_content_height(
style,
content_box_pt(height),
PercentageBasis::definite(layout_pt(content_width)),
)
.points()
});
let used_overflow_clips = self.element_used_overflow_clips(element, style);
// A deferred effect is required when the used height is intrinsic,
// when paint containment supplies the clip, or when a longhand axis
// creates the scroll container independently of the legacy shorthand
// field. The retained effect owns all visual-overflow clipping: a
// primitive clip would mutate descendant source geometry before a
// later CSS transform is applied.
// <https://www.w3.org/TR/css-overflow-3/#overflow-clip-edge>
// <https://www.w3.org/TR/css-contain-1/#containment-paint>
let paint_containment_applies = paint_containment_applies_to_element(element, style);
// A rectangular primitive clip would discard the corners before the
// final paint effect can apply their border-radius and `corner-shape`
// contour. Keep those descendants intact until that typed contour is
// emitted as one effect scope.
// <https://drafts.csswg.org/css-backgrounds-3/#corner-clipping> and
// <https://drafts.csswg.org/css-borders-4/#corner-shaping>.
let has_single_border_shape = match &style.border_shape {
css::BorderShape::None | css::BorderShape::Pair { .. } => false,
css::BorderShape::Circle(_)
| css::BorderShape::Ellipse(_)
| css::BorderShape::Path(_)
| css::BorderShape::Inset(_)
| css::BorderShape::Polygon(_) => true,
};
let needs_contoured_overflow_clip = used_overflow_clips
&& (!style.border_radius.clone().is_zero() || has_single_border_shape);
let needs_deferred_overflow_clip = used_overflow_clips
&& (self.active_fragmentainer_kind() != FragmentainerKind::Column
|| definite_content_height.is_some()
|| paint_containment_applies
|| needs_contoured_overflow_clip);
// The normal deferred path preserves an element-level PDF scope until
// its used block size is known. When one padding-box dimension is
// already known to be empty, though, no descendant can survive that
// scope. Keep the active layout clip as well so the public paint
// projection culls descendants immediately (and so no inherited
// overflow clip is accidentally popped).
// <https://www.w3.org/TR/css-overflow-3/#overflow-clipping>
let has_empty_known_overflow_clip =
content_width + style.padding.left + style.padding.right <= 0.0
|| overflow_clip_content_height
.is_some_and(|height| height + style.padding.top + style.padding.bottom <= 0.0);
let overflow_clip_active = if used_overflow_clips
&& style_clips_overflow(style)
&& !paint_containment_applies
&& (!needs_deferred_overflow_clip
|| has_empty_known_overflow_clip
// An inward edge can reject whole descendant primitives
// before retained PDF clipping. This is exact geometric
// culling, and prevents paint that merely touches the edge
// from contributing antialiased coverage outside it.
|| style.overflow_clip_margin.offset.get() < 0.0
|| style.overflow_clip_margin.reference_box
== css::OverflowClipMarginBox::Border)
// The deferred effect scopes one element-level clip around the
// completed paint fragment. Keeping an eager clip as well turns
// it into a synthetic clip for each text line, which can cut ink
// that lies inside the element's real overflow clip edge.
{
let clip_content_height = overflow_clip_content_height.unwrap_or_else(|| {
(block_top
- border_widths.top
- style.padding.top
- style.padding.bottom
- self.page_bottom())
.max(0.0)
});
let border_box = PageTopRect::new(
outer_x,
block_top,
content_width
+ style.padding.left
+ style.padding.right
+ border_widths.left
+ border_widths.right,
clip_content_height
+ style.padding.top
+ style.padding.bottom
+ border_widths.top
+ border_widths.bottom,
)
.paint_rect();
let edge = resolve_overflow_clip_edge(
border_box,
style,
border_widths,
self.used_overflow_axes_for_element(element, style),
false,
None,
)
.expect("clipping overflow requires a resolved edge");
self.push_overflow_clip(edge.eager_clip());
true
} else {
false
};
let has_single_unbreakable_inline_line = fragmentainer_kind == FragmentainerKind::Column
&& content_top - self.page_bottom() <= css::CSS_PX_TO_PT + 0.01
&& child_boxes.is_some_and(|children| {
self.block_has_single_unbreakable_inline_line(
element,
style,
children,
content_width,
)
});
let propagated_viewport_clip = self
.document_canvas_overflow
.is_viewport_overflow_source(element)
&& self
.document_canvas_overflow
.viewport_clips_block_fragmentation();
// A nested orthogonal flow consumes the outer fragmentainer through
// its own physical margin box, not once for every descendant's
// logical block advance. While its definite logical inline axis is
// laid out locally, descendant progression is horizontal and must
// not drive the page's physical-y cursor across fragmentainers.
// <https://www.w3.org/TR/css-writing-modes-4/#orthogonal-flows>
// <https://www.w3.org/TR/css-break-3/#fragmentation-model>
// <https://www.w3.org/TR/css-break-3/#monolithic>
let isolates_orthogonal_fragmentation =
is_vertical_orthogonal_flow && vertical_inline_size_is_definite;
let suppresses_descendant_fragmentation = (used_overflow_clips
&& definite_content_height.is_some())
|| has_single_unbreakable_inline_line
|| (containment.size && fragmentainer_kind == FragmentainerKind::Column)
|| propagated_viewport_clip
|| isolates_orthogonal_fragmentation;
if suppresses_descendant_fragmentation {
// Scroll containers with a definite size, an unbreakable line in
// a zero-height column, size-contained column subjects, and a
// fixed-inline-size orthogonal flow establish one outer-flow
// fragmentation unit. An auto-height overflow box instead
// fragments with its contents, so the used block size can be
// established from all of its fragments.
// <https://www.w3.org/TR/css-contain-1/#containment-size>
// <https://www.w3.org/TR/css-break-3/#monolithic>
self.fragmentation_suppression_depth += 1;
}
// Capture the static-position containing block before any child work.
// Initial inline layout can encounter an out-of-flow descendant before
// the later block-child traversal begins. Its `justify-self:auto`
// must resolve from this block's non-inherited `justify-items`, not
// from an outer block whose mutable inline span happens to be active.
// `align-items` does not apply to hypothetical block-level children.
// <https://www.w3.org/TR/css-position-3/#static-position>
// <https://drafts.csswg.org/css-align-3/#justify-items-property>
let static_content_height = if style.writing_mode.has_vertical_lines() {
// `height` is the logical inline size of a vertical block. The
// static-position rectangle for a block child must span that
// physical vertical inline axis, even though the legacy
// `definite_content_height` path is a block-axis sizing input.
// An auto-sized vertical parent can defer its own used inline
// extent until after child layout; its static rectangle still
// uses the definite available inline span of its containing
// block. Without this fallback an RTL vertical source becomes a
// zero-height alignment container and places an abspos child's
// physical top at the wrong inline edge.
// <https://drafts.csswg.org/css-position-3/#staticpos-rect>
// <https://www.w3.org/TR/css-writing-modes-4/#abstract-box>
let own_inline_span = if is_vertical_parallel_flow_auto_inline_size {
(content_logical_inline_size - vertical_extras).max(0.0)
} else {
content_logical_inline_size
};
if vertical_inline_size_is_definite {
// A specified physical height is the vertical box's definite
// logical inline span. The ancestor measure is a fallback
// only for an auto-sized parent; taking the maximum here
// would incorrectly expand (for example) `height: 100px` to
// the initial containing block's height when establishing an
// abspos static-position alignment container.
own_inline_span
} else {
own_inline_span.max(
containing_block_content_height
.points()
.map(|height| (height - vertical_extras).max(0.0))
.unwrap_or(0.0)
.max(
self.content_logical_inline_size_stack
.last()
.copied()
.map(|height| (height - vertical_extras).max(0.0))
.unwrap_or(0.0),
),
)
}
} else {
definite_content_height.unwrap_or_else(|| {
containing_block_content_height
.points()
.map(|height| (height - vertical_extras).max(0.0))
.unwrap_or(0.0)
})
};
self.static_position_containing_blocks
.push(StaticPositionContainingBlock::new(
WritingModeAxes::new(style.writing_mode, style.used_direction()),
PageTopRect::new(
self.content_left,
content_top,
(self.content_right - self.content_left).max(0.0),
static_content_height,
),
style.justify_items,
));
*list_marker = self.boxed_list_marker(element, style, previous_containing_block_direction);
let has_pending_outside_marker_anchor = self.begin_outside_marker_anchor(
list_marker.as_deref(),
style,
PageInlineSpan::from_edges(self.content_left, self.content_right),
);
// Generated pseudo-elements are tree-abiding boxes whose `content`
// is evaluated by the pseudo-content path. They must never fall back
// to the originating element's DOM children merely because an atomic
// inline/float replay did not carry a frozen child list.
// <https://www.w3.org/TR/css-pseudo-4/#generated-content>
let has_generated_content = style.content.is_generated();
let has_tree_abiding_generated_children = style
.before_style
.iter()
.chain(style.after_style.iter())
.any(|pseudo| pseudo.content.is_generated());
// A normalized child stream already owns its inline breaks. Inspecting
// the raw DOM as well would resurrect a `<br>` whose computed display
// was suppressed during box-tree construction (notably Appendix B's
// `display: contents` → `none` rule for HTML controls).
// <https://drafts.csswg.org/css-display-3/#unbox-html>
let has_explicit_line_break = !has_generated_content
&& child_boxes.is_none()
&& element_has_direct_line_break(element);
let root_has_generated_content = element.tag.eq_ignore_ascii_case("html")
&& style
.before_style
.iter()
.chain(style.after_style.iter())
.any(|pseudo| pseudo.content.is_generated());
let requires_suppressed_dom_source_collection = !has_generated_content
&& child_boxes.is_none_or(has_non_inline_formatting_box)
&& requires_suppressed_dom_source_collection_with_font_metrics(
element,
style,
stylesheets,
&self.ancestors,
&mut self.font_system,
);
let ordered_mixed_flow_required = !has_generated_content
// Root pseudos are tree-abiding children. The ordered DOM path
// has no pseudo source entries, so it would drop a block-level
// `html::before` before the propagated principal-flow child
// traversal can lay it out.
// <https://www.w3.org/TR/css-pseudo-4/#generated-content>
&& !root_has_generated_content
&& !requires_block_in_inline_normalization
&& !requires_run_in_normalization
&& (((child_boxes.is_none() || child_boxes.is_some_and(has_non_inline_formatting_box))
&& has_ordered_mixed_flow_content_with_font_metrics(
element,
style,
stylesheets,
&self.ancestors,
&mut self.font_system,
))
|| requires_suppressed_dom_source_collection
// Formatting-tree normalization stores direct `<br>` nodes
// outside the block-child sequence. When a block also has
// normal-flow children, replaying all direct inline content
// before that sequence changes DOM order; retain the ordered
// mixed-flow path instead.
// <https://html.spec.whatwg.org/multipage/text-level-semantics.html#the-br-element>
|| (has_explicit_line_break
&& child_boxes.is_some_and(has_non_inline_formatting_box)));
let has_normalized_flow_children = !has_generated_content
&& child_boxes
.map(has_non_inline_formatting_box)
.unwrap_or(false);
let has_direct_dom_flow_child = !has_generated_content
&& child_boxes.is_none()
&& has_direct_flow_child_with_font_metrics(
element,
style,
stylesheets,
&mut self.font_system,
);
let direct_float_children = style.display.is_flow()
&& !has_generated_content
&& !has_tree_abiding_generated_children
&& has_direct_float_only_source_with_font_metrics(
element,
style,
stylesheets,
&mut self.font_system,
);
let inline_sequence_required = !ordered_mixed_flow_required
&& !has_generated_content
&& !has_normalized_flow_children
&& child_boxes
.map(|boxes| {
formatting_box_has_inline_content(boxes)
&& boxes
.iter()
.any(|box_| !formatting_box_can_only_create_phantom_line_boxes(box_))
})
.unwrap_or(false);
let traversal_mode = classify_atomic_principal_flow(
ordered_mixed_flow_required,
direct_float_children,
inline_sequence_required,
);
let has_run_in_inline_content = !run_in_children.is_empty();
// If normalization consumed a run-in source's children, do not replay
// its original DOM text here. Inline pseudo content that survives in
// normalized inline boxes is handled through `InlineSequence`.
let normalized_children_empty = child_boxes.is_some_and(|boxes| boxes.is_empty());
let detached_normalized_text = normalized_children_empty
&& !has_generated_content
&& !inline_text_for_style(element, style).is_empty();
let text = if normalized_children_empty
|| has_generated_content
|| matches!(traversal_mode, ChildTraversalMode::OrderedMixed)
|| matches!(traversal_mode, ChildTraversalMode::DirectFloatChildren)
|| has_normalized_flow_children
|| matches!(traversal_mode, ChildTraversalMode::InlineSequence)
{
String::new()
} else if is_document_canvas {
own_inline_text_for_style(element, style)
} else if has_direct_dom_flow_child
&& matches!(traversal_mode, ChildTraversalMode::BlockChildren)
{
// The DOM fallback lays direct block-flow children in its later
// child traversal. Their descendant text must not be flattened
// into a synthetic leading anonymous line in this parent.
// <https://www.w3.org/TR/CSS22/visuren.html#block-formatting>
own_inline_text_for_style(element, style)
} else {
inline_text_for_style(element, style)
};
// Frozen root children already contain tree-abiding `::after` boxes.
// Do not also collect the pseudo from the root style in the leading
// inline run; that run precedes the propagated body canvas and would
// both duplicate the pseudo and reverse its DOM order.
// <https://www.w3.org/TR/css-pseudo-4/#generated-content>
let defer_root_after_pseudo = element.tag.eq_ignore_ascii_case("html")
&& child_boxes.is_some()
&& style
.after_style
.as_deref()
.is_some_and(|after| after.content.is_generated());
let leading_inline_style =
defer_root_after_pseudo.then(|| Self::leading_inline_style_without_after(style));
let inline_style = leading_inline_style.as_deref().unwrap_or(style);
let has_generated_inline_content = !detached_normalized_text
&& ((has_generated_content && generated_content_has_non_phantom_inline_content(style))
|| generated_content_has_non_phantom_inline_content(inline_style)
|| (child_boxes.is_none()
&& (inline_style.before_style.is_some()
|| inline_style.after_style.is_some())));
// Once normalization has exposed block-flow children, its anonymous
// inline runs own the descendant inline source. Recollecting the raw
// DOM solely because it has a styled inline descendant would replay
// text on the parent before the normalized sequence.
let has_styled_inline_descendant =
!matches!(traversal_mode, ChildTraversalMode::DirectFloatChildren)
&& !has_normalized_flow_children
&& has_styled_inline_descendant_with_font_metrics(
element,
style,
stylesheets,
&self.ancestors,
&mut self.font_system,
);
// The canvas box has no own text run. When its direct children are
// exclusively ordinary inline boxes, collect that one source stream
// instead of dropping it while preserving all mixed/block fallback
// paths for more complex canvas children.
let has_plain_document_canvas_inline_stream = is_document_canvas
&& child_boxes.is_none()
&& has_only_direct_in_flow_inline_dom_content_with_font_metrics(
element,
style,
stylesheets,
&self.ancestors,
&mut self.font_system,
);
// A `<br>` creates a line box even when the surrounding text has
// collapsed away. Treat it as collectable inline content so its
// forced boundaries are laid out—and fragmented—through the shared
// line-record path rather than being discarded as empty text.
// <https://html.spec.whatwg.org/multipage/text-level-semantics.html#the-br-element>
let has_collectable_inline_content = inline_text_has_non_phantom_content(&text, style)
|| has_generated_inline_content
|| has_explicit_line_break
|| has_styled_inline_descendant
|| has_plain_document_canvas_inline_stream;
let use_inline_items = has_collectable_inline_content
&& (has_plain_document_canvas_inline_stream
|| has_styled_inline_descendant
|| has_generated_inline_content
|| plain_inline_content_needs_inline_items(&text, style)
|| has_explicit_line_break
|| style.text_align.justifies()
|| self.active_float_exclusions_at(PageBlockSpan::new(
self.cursor_y,
style.line_height,
)));
let run_in_inline_items_laid_out =
has_run_in_inline_content && !has_normalized_flow_children && child_boxes.is_some();
// An inline-only multicolumn pass owns both its anonymous column
// fragments and their gap decorations. Do not subsequently route the
// same source through the generic block-column path, which would
// infer a second, incompatible set of occupied columns from the
// post-layout cursor.
// <https://www.w3.org/TR/css-multicol-1/#the-multi-column-model>
let mut laid_out_inline_multicol = false;
let mut preceding_inline_local_cutoff = false;
let mut preceding_inline_clamp_block_advance = crate::units::content_box_pt(0.0);
let mut preceding_inline_has_non_phantom_line = false;
// Retain a committed simple vertical inline sequence when available.
// Vertical auto-size geometry consumes that exact layout rather than
// reconstructing the same text after selecting its inline measure.
let mut committed_vertical_inline_sequence = None;
if run_in_inline_items_laid_out {
let child_boxes = child_boxes.expect("run-in layout requires frozen target children");
self.layout_run_in_inline_items_block(
element,
style,
stylesheets,
run_in_children,
child_boxes,
element.attrs.get("href").map(String::as_str),
list_marker.as_deref(),
);
preceding_inline_has_non_phantom_line = true;
// Ordered mixed-flow traversal owns every inline run, including a
// standalone `<br>` between floated or block-level siblings. Laying
// the parent inline items here as well would collect the complete DOM
// subtree and place those floats a second time before the ordered
// traversal reaches their source positions.
// <https://www.w3.org/TR/CSS22/visuren.html#anonymous-block-level>
} else if has_collectable_inline_content
&& !matches!(traversal_mode, ChildTraversalMode::OrderedMixed)
{
let pushed_text_box_trim = self.push_text_box_line_trim_scope(block_line_trim);
if use_inline_items {
let laid_out_multicol_inline_items = self.layout_multicol_inline_items_block(
element,
style,
stylesheets,
None,
(0.0, 0.0),
element.attrs.get("href").map(String::as_str),
list_marker.as_deref(),
multicol_content_height,
);
laid_out_inline_multicol = laid_out_multicol_inline_items;
preceding_inline_has_non_phantom_line |= laid_out_multicol_inline_items;
if !laid_out_multicol_inline_items {
let inline_outcome;
committed_vertical_inline_sequence =
if let Some(selected) = selected_orthogonal_inline_layout.as_ref() {
debug_assert_eq!(
content_logical_inline_size,
selected.logical_inline_measure.points(),
"orthogonal auto geometry must paint at its selected inline measure"
);
self.paint_inline_line_sequence(&selected.line_sequence, inline_style);
self.layout_frozen_inline_replay_positioned_descendants(
&selected.frozen_replay_input,
stylesheets,
);
inline_outcome = selected.line_sequence.layout_outcome();
Some(selected.line_sequence.clone())
} else {
let (sequence, outcome) = self.layout_inline_items_block(
element,
inline_style,
stylesheets,
// The frozen inline stream is also the source used
// for this block's intrinsic geometry. Recollecting
// the DOM here can flatten an inline-block into
// styled text edges, losing its line-box block-size
// contribution after the orthogonal auto-size pass
// selected a width from that contribution.
// <https://www.w3.org/TR/css-inline-3/#line-boxes>
// <https://www.w3.org/TR/css-writing-modes-4/#orthogonal-flows>
child_boxes,
(0.0, 0.0),
element.attrs.get("href").map(String::as_str),
list_marker.as_deref(),
);
inline_outcome = outcome;
sequence
};
preceding_inline_has_non_phantom_line |= inline_outcome.has_non_phantom_line;
preceding_inline_local_cutoff = committed_vertical_inline_sequence
.as_ref()
.is_some_and(|sequence| sequence.has_local_continuation_cutoff);
preceding_inline_clamp_block_advance = committed_vertical_inline_sequence
.as_ref()
.map(|sequence| sequence.layout_outcome().clamp_block_advance)
.unwrap_or_else(|| crate::units::content_box_pt(0.0));
}
} else if style.display.is_list_item() {
self.layout_list_text_block(
&text,
inline_style,
0.0,
0.0,
element.attrs.get("href").map(String::as_str),
list_marker.as_deref(),
);
preceding_inline_has_non_phantom_line = true;
} else {
let laid_out_multicol_text = self.layout_multicol_text_block(
&text,
inline_style,
0.0,
0.0,
element.attrs.get("href").map(String::as_str),
multicol_content_height,
);
laid_out_inline_multicol = laid_out_multicol_text;
preceding_inline_has_non_phantom_line |= laid_out_multicol_text;
if !laid_out_multicol_text {
let outcome = self.layout_text_block(
&text,
style,
0.0,
0.0,
element.attrs.get("href").map(String::as_str),
);
preceding_inline_local_cutoff = outcome.has_local_continuation_cutoff;
preceding_inline_clamp_block_advance = outcome.clamp_block_advance;
preceding_inline_has_non_phantom_line |= outcome.has_non_phantom_line;
}
}
self.pop_text_box_line_trim_scope(pushed_text_box_trim);
}
let mut box_inline_has_flow_effects = false;
let mut laid_out_box_inline_multicol = false;
if !has_run_in_inline_content
&& matches!(traversal_mode, ChildTraversalMode::InlineSequence)
&& !(has_collectable_inline_content && use_inline_items)
&& let Some(child_boxes) = child_boxes
{
let pushed_text_box_trim = self.push_text_box_line_trim_scope(block_line_trim);
// Collected text, forced-break boxes, and atomic inline fragments
// go directly through the shared multicol line sequence. Atomic
// fragments retain their owned paint/positioning state in the
// sequence, so an ordinary anonymous-block pass here would paint
// them once before the column planner paints them again.
// <https://www.w3.org/TR/css-multicol-1/#the-multi-column-model>
laid_out_box_inline_multicol = list_marker.is_none()
&& self.layout_multicol_inline_items_block(
element,
style,
stylesheets,
has_atomic_inline_formatting_box(child_boxes).then_some(child_boxes),
(0.0, 0.0),
element.attrs.get("href").map(String::as_str),
None,
multicol_content_height,
);
if !laid_out_box_inline_multicol {
box_inline_has_flow_effects = self.layout_anonymous_block(
style,
child_boxes,
stylesheets,
list_marker.as_deref(),
);
}
preceding_inline_has_non_phantom_line |=
laid_out_box_inline_multicol || box_inline_has_flow_effects;
self.pop_text_box_line_trim_scope(pushed_text_box_trim);
}
// A list item's marker belongs to its principal flow, not to a
// particular anonymous column set. For an inside marker followed by
// a spanner, the frozen child sequence starts with an anonymous
// block (or directly with the spanner), so the column planner has no
// root-level inline formatter in which to create that first marker
// line. Commit it once before the segmented flow; outside markers
// retain their deferred first-line anchor below.
// <https://drafts.csswg.org/css-lists-3/#list-style-position-property>
let laid_out_inside_multicol_marker = !laid_out_inline_multicol
&& !laid_out_box_inline_multicol
&& text.is_empty()
&& style_establishes_multicol_formatting_context(style)
&& list_marker
.as_ref()
.is_some_and(|marker| marker.participates_in_first_line())
&& child_boxes.is_some_and(formatting_boxes_have_eligible_multicol_spanner);
if laid_out_inside_multicol_marker {
let pushed_text_box_trim = self.push_text_box_line_trim_scope(block_line_trim);
self.layout_list_text_block(
"",
style,
0.0,
0.0,
element.attrs.get("href").map(String::as_str),
list_marker.as_deref(),
);
self.pop_text_box_line_trim_scope(pushed_text_box_trim);
preceding_inline_has_non_phantom_line = true;
}
let laid_out_column_children = laid_out_inline_multicol
|| laid_out_box_inline_multicol
|| (!laid_out_inline_multicol
&& text.is_empty()
&& (self.layout_definition_list_columns(element, style, stylesheets, child_boxes)
|| self
.layout_simple_block_child_columns(
element,
// Column planning creates its own used style;
// begin from the frozen cascade source rather
// than the block's already-zoomed geometry.
source_style,
stylesheets,
child_boxes,
multicol_content_height,
)
.is_multicol_layout()));
if laid_out_column_children
&& let Some(marker) = list_marker.as_ref()
&& marker.paints_outside()
&& !self.outside_marker_anchor_is_pending(marker)
{
// A multicol list item still owns one marker at the principal
// box's block start. The column planner consumes the block
// children, so paint that marker here rather than falling through
// to the non-column empty-content fallback below.
// <https://www.w3.org/TR/css-lists-3/#marker-position>
let fallback_baseline_offset = self.inline_box_text_line_layout_baseline_offset(style);
self.paint_outside_marker(
marker,
style,
OutsideMarkerAnchor {
principal_line_inline_span: PageInlineSpan::from_edges(
self.content_left,
self.content_right,
),
formatted_line_block_start: PageTopBlockPosition::new(content_top),
alphabetic_baseline: PageTopBlockPosition::new(content_top)
.toward_block_end(layout_pt(fallback_baseline_offset)),
},
);
}
let has_inside_marker_line_content = list_marker.as_ref().is_some_and(|marker| {
marker.participates_in_first_line() && marker.has_in_flow_content()
});
let has_direct_inline_content = has_run_in_inline_content
|| box_inline_has_flow_effects
|| child_boxes.is_some_and(has_direct_inline_content_box)
// Atomic inline/replaced boxes establish a line box even when
// their source contains no text. Their used line metrics must
// keep the owning block from self-collapsing after paint.
// <https://www.w3.org/TR/css-inline-3/#line-box>
|| child_boxes.is_some_and(has_atomic_inline_formatting_box)
|| has_collectable_inline_content
|| laid_out_column_children
// The marker is generated inline-level content of the principal
// list-item box. It prevents the principal box from being
// self-collapsing even when the DOM contributes only collapsible
// whitespace.
// <https://drafts.csswg.org/css-lists-3/#marker-layout>
|| has_inside_marker_line_content;
if style.writing_mode != WritingMode::HorizontalTb
&& has_direct_inline_content
&& !vertical_inline_size_is_definite
{
let vertical_inline_height =
if matches!(traversal_mode, ChildTraversalMode::InlineSequence)
&& let Some(marker) = list_marker.as_ref()
&& marker.participates_in_first_line()
{
child_boxes
.map(|child_boxes| {
self.intrinsic_inline_measurement_for_boxes_with_marker(
child_boxes,
style,
marker,
stylesheets,
content_logical_inline_size,
)
.physical_height(style)
})
.unwrap_or(0.0)
} else if let Some(sequence) = committed_vertical_inline_sequence.as_ref() {
sequence.occupied_physical_inline_extent(style).points()
} else if let Some(marker) = list_marker.as_ref()
&& marker.participates_in_first_line()
&& !text.is_empty()
{
// In vertical writing, the physical height consumed by an
// inside marker is part of the line's inline-axis advance.
// Measuring only the principal text makes successive list
// items overlap and ignores marker font-size changes.
// <https://drafts.csswg.org/css-writing-modes-4/#vertical-layout>
// <https://drafts.csswg.org/css-lists-3/#marker-position>
let mut items = Vec::new();
self.push_inside_marker_items(marker, style, None, &mut items);
self.push_inline_words(
&text,
style,
None,
0.0,
InlineVisualOffset::zero(),
&mut items,
);
self.intrinsic_inline_measurement_for_items(
items,
style,
content_logical_inline_size,
)
.physical_height(style)
} else if matches!(traversal_mode, ChildTraversalMode::InlineSequence) {
child_boxes
.map(|child_boxes| {
self.intrinsic_inline_measurement_for_boxes(
child_boxes,
style,
stylesheets,
content_logical_inline_size,
)
.physical_height(style)
})
.unwrap_or(0.0)
} else if !text.is_empty() {
// This fallback still has styled inline descendants in the
// DOM-backed collector. Measuring its flattened text under
// the block style would discard an inner atomic inline's
// effective layout footprint: in particular a horizontal
// text-combine-upright composition must contribute its one-em
// square, not its uncompressed horizontal advance.
//
// Use the same styled item collection and intrinsic graph as
// line layout, which forms TCY before measuring its atomic
// parent participant.
// <https://drafts.csswg.org/css-writing-modes-4/#text-combine-layout>
// <https://drafts.csswg.org/css-sizing-3/#intrinsic>
self.intrinsic_inline_measurement_for_element(
element,
style,
stylesheets,
child_boxes,
content_logical_inline_size,
)
.physical_height(style)
} else {
0.0
};
if vertical_inline_height > 0.0 {
self.cursor_y = self.cursor_y.min(content_top - vertical_inline_height);
}
}
if let Some(marker) = list_marker.as_ref()
&& !inline_text_has_non_phantom_content(&text, inline_style)
&& !has_collectable_inline_content
&& !matches!(traversal_mode, ChildTraversalMode::InlineSequence)
&& !laid_out_column_children
{
if marker.paints_outside() {
if self.cursor_y - style.font_size < self.page_bottom() {
self.push_page();
}
if !has_pending_outside_marker_anchor {
let anchor = self.outside_marker_fallback_anchor(
style,
PageInlineSpan::from_edges(self.content_left, self.content_right),
);
self.paint_outside_marker(marker, style, anchor);
}
} else if marker.has_in_flow_content() {
// An inside marker is inline-level content. It therefore
// establishes the principal line box even when the only DOM
// text was collapsible whitespace (as in an empty HTML
// `<li>` formatted across source lines).
// <https://drafts.csswg.org/css-lists-3/#list-style-position-property>
// <https://drafts.csswg.org/css-inline-3/#line-box>
let pushed_text_box_trim = self.push_text_box_line_trim_scope(block_line_trim);
self.layout_list_text_block("", style, 0.0, 0.0, None, Some(marker));
self.pop_text_box_line_trim_scope(pushed_text_box_trim);
}
}
let can_collapse_start_margin = can_adjoin_first_child_margin
&& can_collapse_block_start_margin(
element,
style,
border_edges,
child_boxes.map_or_else(
|| {
has_direct_inline_content_before_first_flow_child_dom_with_font_metrics(
element,
style,
stylesheets,
&self.ancestors,
&mut self.font_system,
)
},
has_direct_inline_content_box,
),
self.used_overflow_for_element(element, style),
);
let can_collapse_end_margin = can_collapse_block_end_margin(
element,
style,
geometry.containing_block_content_height,
border_edges,
has_direct_inline_content,
self.used_overflow_for_element(element, style),
);
let margin_collapse_style = height_behaves_as_auto_for_margin_collapse(
style,
geometry.containing_block_content_height,
)
.then(|| Self::auto_height_margin_collapse_style(style));
let margin_collapse_style = margin_collapse_style.as_deref().unwrap_or(style);
let self_collapsing_block = !has_direct_inline_content
&& if let Some(child_boxes) = child_boxes {
is_self_collapsing_block_box(
element,
margin_collapse_style,
child_boxes,
self.document_canvas_overflow,
)
} else {
is_self_collapsing_block_dom_with_font_metrics(
element,
margin_collapse_style,
stylesheets,
&self.ancestors,
&mut self.font_system,
self.document_canvas_overflow,
)
};
// Relative positioning percentages resolve against the normal-flow
// containing block even when that block does not establish an
// absolute-positioning containing block. Keep this scope separate
// from `containing_blocks`, which intentionally tracks only the
// positioned-containing-block chain.
// <https://www.w3.org/TR/css-position-3/#relative-positioning>.
self.normal_flow_relative_containing_blocks
.push(NormalFlowRelativeContainingBlock {
physical_content_width: PhysicalContentWidth::new(content_box_pt(content_width)),
physical_content_height: definite_content_height
.map(|height| PhysicalContentHeight::new(content_box_pt(height))),
});
let children_outcome =
self.with_float_replay_clearance_scope(float_replay_clearance_boundary, |layout| {
layout.layout_prepared_block_children(Box::new(BlockFlowChildrenPhaseInput {
fragmentainer_kind,
element,
style,
stylesheets,
child_boxes,
can_collapse_start_margin,
can_collapse_end_margin,
start_margin_arrangement,
starts_at_page_top,
laid_out_column_children,
traversal_mode,
run_in_inline_items_laid_out,
has_preceding_inline_flow_content: preceding_inline_has_non_phantom_line
&& !matches!(traversal_mode, ChildTraversalMode::OrderedMixed),
preceding_inline_local_cutoff,
preceding_inline_clamp_block_advance,
discard_region_limit: None,
direct_automatic_block_size_constraint:
super::children::state::direct_automatic_block_size_constraint(style),
descendant_percentage_height_context: preparatory_descendant_percentage_context,
}))
});
#[cfg(all(feature = "stack-profile", target_os = "macos"))]
let _stack_profile_scope = stack_profile::enter("finish_prepared_block_layout");
self.normal_flow_relative_containing_blocks.pop();
self.static_position_containing_blocks.pop();
self.block_percentage_context_stack.pop();
if has_pending_outside_marker_anchor {
self.finish_outside_marker_anchor();
}
let pending_end_margin_collapse = children_outcome.pending_end_margin_collapse;
let margin_collapse_boundary = match start_margin_arrangement.margin_collapse_boundary() {
BlockMarginCollapseBoundary::Adjoining => {
children_outcome.adjoining_margin_set_boundary
}
BlockMarginCollapseBoundary::SeparatedByClearance => {
BlockMarginCollapseBoundary::SeparatedByClearance
}
};
// A descendant clearance boundary invalidates the provisional paint
// offset accumulated while its start margins were assumed to adjoin
// this principal box. The box keeps its normal-flow border edge;
// clearance owns the separated descendant placement below it.
// <https://www.w3.org/TR/CSS22/box.html#collapsing-margins>
// <https://www.w3.org/TR/CSS22/visuren.html#flow-control>
let collapsed_start_margin_offset =
if margin_collapse_boundary == BlockMarginCollapseBoundary::SeparatedByClearance {
layout_pt(0.0)
} else {
children_outcome.collapsed_start_margin_offset
};
// Keep the rendered legend's source-fragment geometry at the parent
// decoration boundary. The next decoration pass consumes it to
// exclude only the fieldset border, never descendant backgrounds.
// <https://html.spec.whatwg.org/multipage/rendering.html#the-fieldset-and-legend-elements>
let rendered_legend = children_outcome.rendered_legend;
let inline_capture = self.finish_clamp_line_slot_capture();
let clamp_line_slots =
inline_capture.line_slots + children_outcome.descendant_clamp_line_slots;
let has_local_continuation_cutoff = inline_capture.has_local_continuation_cutoff
|| children_outcome.has_local_continuation_cutoff;
let in_flow_child_fragment_end =
(self.pages.len() > paint_page_index).then_some(InFlowFragmentEnd {
page_index: self.pages.len(),
cursor: PageTopBlockPosition::new(self.cursor_y),
});
if suppresses_descendant_fragmentation {
self.fragmentation_suppression_depth -= 1;
}
let mut independent_bfc_had_float_content = false;
if establishes_independent_bfc
&& has_auto_height(style)
&& let Some((float_page_index, float_bottom)) =
self.current_float_context_last_fragment_end()
{
independent_bfc_had_float_content = true;
while self.pages.len() < float_page_index {
// Floats are out of flow, so their paint alone does not make
// this anonymous column non-empty. The auto-height BFC itself
// nevertheless has normal-flow geometry through every column
// required to contain those floats; preserve that structural
// occupancy before advancing, or `push_page` will replace the
// empty column instead of materializing the continuation.
// <https://www.w3.org/TR/CSS22/visudet.html#root-height>
// <https://www.w3.org/TR/css-break-3/#fragmentation-model>
if !self.current_page_has_content() {
self.mark_current_page_flow_content();
}
self.push_page();
}
self.cursor_y = self.cursor_y.min(float_bottom.points());
}
if establishes_independent_bfc {
self.pop_float_context();
}
if let Some(scope) = positioned_containing_block_scope {
self.pop_positioned_containing_block(scope);
}
self.pop_overflow_clip(overflow_clip_active);
self.pop_container_unit_context(container_unit_scope);
self.child_available_space_stack.pop();
self.content_logical_inline_size_stack.pop();
if propagated_vertical_body_canvas {
let block_end_inset = match block_start_side(style.writing_mode) {
PhysicalSide::Left => style.margin.right,
PhysicalSide::Right => style.margin.left,
PhysicalSide::Top | PhysicalSide::Bottom => {
unreachable!("a vertical document canvas has a horizontal block axis")
}
};
if let Some(active_canvas) = self.root_principal_flow_context.active_canvas.take() {
debug_assert_eq!(active_canvas.body, Some(element.id));
self.root_principal_flow_context.completed_canvas = Some(CompletedDocumentCanvas {
body: active_canvas.body,
// A fragmented body completes on its final fragmentainer,
// not necessarily the page on which its canvas began.
// Root content following the canvas must therefore make
// its placement decision against this committed source
// fragment.
source_page: DocumentPageIndex::new(self.pages.len()),
source_block_track: PageInlineSpan::from_edges(
self.content_left,
self.content_right,
),
inline_origin: active_canvas.inline_origin,
inline_end_inset: active_canvas.inline_end_inset,
block_end_inset: layout_pt(block_end_inset),
block_track_occupancy: active_canvas.block_track_occupancy,
trailing_child_block_margin: active_canvas.trailing_child_block_margin,
});
}
}
self.restore_page_area_parent_context_after_page_transition(
previous_left,
previous_right,
block_start_page_context,
block_start_page_index,
);
if is_document_canvas {
self.document_canvas_fragment_insets.pop();
}
self.containing_block_direction = previous_containing_block_direction;
self.containing_block_writing_mode = previous_containing_block_writing_mode;
if self_collapsing_block
&& !independent_bfc_had_float_content
&& self.pages.len() == paint_page_index
&& margin_collapse_boundary == BlockMarginCollapseBoundary::Adjoining
// Adjoining block margins require no block-axis border or
// padding. Retaining a nonzero vertical edge is essential even
// for an otherwise empty positioned body: that edge is the
// principal decoration of its stacking context.
// <https://www.w3.org/TR/CSS22/box.html#collapsing-margins>
&& vertical_extras == 0.0
{
self.cursor_y = content_top;
}
let mut block_end_margin_to_consume = style.margin.bottom;
if let Some(pending) = pending_end_margin_collapse {
let content_height_with_child_margin = content_top - self.cursor_y;
let content_height_without_child_margin =
content_height_with_child_margin - pending.child_consumed_margin.points();
self.cursor_y += pending.child_consumed_margin.points();
if block_end_margin_collapse_survives_height_constraints(
style,
PhysicalContentWidth::new(content_inline.width()),
vertical_non_content,
PhysicalContentHeight::new(content_box_pt(content_height_without_child_margin)),
) {
block_end_margin_to_consume = pending.collapsed_margin.points();
}
}
if is_vertical_orthogonal_flow && !vertical_inline_size_is_definite {
// Orthogonal auto sizing has selected the vertical box's used
// logical inline content size through fit-content negotiation.
// That used size controls its own box geometry, even though the
// fallback that selected it remains an indefinite percentage
// basis for descendants.
// <https://www.w3.org/TR/css-writing-modes-4/#orthogonal-auto>
self.cursor_y = content_top - content_logical_inline_size;
} else if is_vertical_parallel_flow_auto_inline_size && self.cursor_y >= content_top - 0.01
{
// In vertical writing, physical height is logical inline size. A
// normal-flow block with an automatic inline size fills its
// containing block's available inline size, just as an automatic
// physical width does in horizontal block flow. The inline layout
// pass selected that content-box size, so retain it rather than
// collapsing to the height contributed by inline contents.
// <https://www.w3.org/TR/css-writing-modes-4/#dimension-mapping>
self.cursor_y = content_top - content_logical_inline_size;
} else if definite_content_height.is_some()
|| used_min_height(style, containing_block_height_basis).is_some()
|| used_non_replaced_max_height(style, containing_block_percentage_context).is_some()
|| style.box_values.min_height == css::ComputedLengthPercentageOrAuto::Stretch
|| style.box_values.max_height == css::ComputedLengthPercentageOrAuto::Stretch
|| height_depends_on_intrinsic_content
|| intrinsic_physical_height_is_contained(style)
{
// Size containment fixes the principal box's used size as if it
// had no content. Descendants are still laid out and painted in
// place, but their overflow does not move following siblings.
// <https://www.w3.org/TR/css-contain-1/#containment-size>
let current_content_height = if intrinsic_physical_height_is_contained(style) {
style
.contain_intrinsic_size
.height
.clone()
.map(|height| {
used_length_percentage(
height,
PercentageBasis::definite(layout_pt(content_width.max(0.0))),
)
.points()
})
.unwrap_or(0.0)
} else {
content_top - self.cursor_y
};
// Once child layout has crossed an outer fragmentainer, the
// current cursor reports only the final fragment's local extent.
// CSS sizing instead constrains the block's continuous automatic
// height, before that source flow is split into fragmentainers.
// <https://www.w3.org/TR/CSS2/visudet.html#min-max-heights>
// <https://www.w3.org/TR/css-break-3/#parallel-flows>
let measured_content_height = if self.pages.len() > paint_page_index {
promoted_spanner_paint_slices(
paint_page_index,
self.pages.len(),
content_top,
self.cursor_y,
block_start_page_context,
self.current_page_context,
self.fragmentainer_override,
)
.iter()
.map(|slice| (slice.top - slice.bottom).max(0.0))
.sum()
} else {
current_content_height
};
// An intrinsic min/max constraint is resolved only after in-flow
// content has been measured, but it must constrain the automatic
// preferred height supplied by aspect-ratio rather than replace
// it with that content height. This lets calc-size(auto, …) use
// the content-derived automatic minimum independently from the
// ratio-derived preferred size.
// <https://www.w3.org/TR/css-sizing-4/#aspect-ratio> and
// <https://drafts.csswg.org/css-values-5/#calc-size>.
let aspect_ratio_preferred_height = style
.box_values
.height
.is_auto()
.then(|| {
non_replaced_aspect_ratio_content_height(
style,
content_width,
border_widths.left
+ border_widths.right
+ style.padding.left
+ style.padding.right,
vertical_extras,
)
})
.flatten();
let mut requested_content_height = definite_content_height
.or(aspect_ratio_preferred_height)
.unwrap_or_else(|| {
used_non_replaced_content_box_height_or_auto(
style,
containing_block_percentage_context,
vertical_non_content,
)
.map(SemanticLengthExt::points)
.unwrap_or(measured_content_height)
});
// A preferred `calc-size()` block size substitutes its automatic
// basis only after in-flow content has been measured. At this
// point normal-flow layout has that content contribution, so
// retain CSS Math bounds such as `min(size, 100px)` instead of
// treating the preferred size as ordinary `auto`.
// <https://drafts.csswg.org/css-values-5/#calc-size>.
if definite_content_height.is_none()
&& let css::ComputedLengthPercentageOrAuto::CalcSize(value) =
&*style.box_values.height
{
requested_content_height = calc_size_intrinsic_constraint(
value.clone(),
style.box_sizing,
PercentageBasis::definite(content_box_pt(content_width)),
vertical_non_content,
content_box_pt(measured_content_height),
content_box_pt(measured_content_height),
)
.map(SemanticLengthExt::points)
.unwrap_or(requested_content_height);
}
// A preferred aspect ratio supplies the automatic preferred block
// size, but it does not discard the content-based automatic
// minimum of an ordinary flow box. The final used height must
// therefore accommodate in-flow content when `min-height:auto`.
// <https://drafts.csswg.org/css-sizing-4/#aspect-ratio>
if style.box_values.height.is_auto()
&& style
.aspect_ratio
.preferred_ratio_for_non_replaced(false)
.is_some()
&& style.box_values.min_height.is_auto()
&& !style.overflow_y.is_scrollable()
&& !intrinsic_physical_height_is_contained(style)
{
requested_content_height = requested_content_height.max(measured_content_height);
}
let height = if height_depends_on_intrinsic_content {
constrain_non_replaced_height_with_intrinsic(
style,
content_box_pt(requested_content_height),
content_box_pt(current_content_height),
content_box_pt(current_content_height),
containing_block_percentage_context,
non_content_pt(vertical_extras),
)
.points()
} else {
containing_block_content_height.points().map_or_else(
|| {
constrain_non_replaced_content_height(
style,
content_box_pt(requested_content_height),
containing_block_percentage_context,
)
.points()
},
|basis| {
constrain_height_with_stretch_fit(
style,
content_box_pt(requested_content_height),
layout_pt(basis),
layout_pt(style.margin.top + style.margin.bottom),
vertical_non_content,
)
.points()
},
)
};
// A winning post-layout min/max constraint reruns CSS sizing with
// a fixed used height. It changes the principal box's fragment
// ownership but cannot become a percentage basis for descendants:
// they were laid out against the original auto-height containing
// block before this constraint was known.
// <https://www.w3.org/TR/CSS2/visudet.html#min-max-heights>
// <https://www.w3.org/TR/css-break-3/#parallel-flows>
let post_layout_height_replay = definite_content_height
.is_none()
.then(|| {
post_layout_height_replay_constraint(
style,
containing_block_height_basis,
vertical_non_content,
content_box_pt(requested_content_height),
)
})
.flatten()
.filter(|constraint| {
(requested_content_height
- constraint.content_box_length(vertical_non_content).points())
.abs()
> 0.01
});
if let Some(replay_height) = post_layout_height_replay {
// The constrained used height must drive the complete second
// layout pass. In particular, fragmentation has to assign
// the principal box before its visible overflow reaches later
// columns. Preserve the percentage basis selected before the
// clamp: a post-layout constraint never makes descendants'
// percentage heights definite retroactively.
// <https://www.w3.org/TR/CSS2/visudet.html#min-max-heights>
// <https://www.w3.org/TR/css-break-3/#parallel-flows>
self.replay_post_layout_constraint(
post_layout_constraint_replay_snapshot
.take()
.expect("post-layout min/max constraint replay has a snapshot"),
element,
style,
stylesheets,
run_in_children,
child_boxes,
Some(preparatory_descendant_percentage_basis),
Some(preparatory_descendant_percentage_context),
replayed_item_logical_inline_size,
principal_box_paint_mode,
replay_height,
);
return;
}
if self.pages.len() == paint_page_index
&& style.writing_mode == WritingMode::HorizontalTb
{
let free_space = height - current_content_height;
block_align_content_offset_y = if laid_out_column_children {
multicol_align_content_y_offset(style.align_content, free_space)
} else {
block_align_content_y_offset_for_style(style, free_space)
};
}
let fixed_principal_block_overflows_fragmentainer = fixed_principal_block_size
.is_some()
&& self.fragmentation_suppression_depth == 0
&& style.writing_mode == WritingMode::HorizontalTb
&& !(containment.size && fragmentainer_kind == FragmentainerKind::Column)
&& !has_single_unbreakable_inline_line
// Root/body overflow propagated to the viewport is clipped
// by the viewport rather than fragmented into additional
// document pages.
// <https://drafts.csswg.org/css-overflow-3/#overflow-propagation>
&& !propagated_viewport_clip
&& height > content_top - self.page_bottom() + 0.01;
if self.pages.len() > paint_page_index && fixed_principal_block_overflows_fragmentainer
{
// Descendant layout may already have crossed one or more
// fragmentainers before the definite principal size is
// applied (for example, a line starting at an exhausted page
// edge or nested multicol rows). Consume only the unoccupied
// remainder of the authored block size; reapplying the full
// size double-counts those fragments, while using a page-local
// subtraction drops the remaining continuous extent.
// <https://www.w3.org/TR/css-break-3/#fragmentation-model>
// <https://www.w3.org/TR/css-multicol-1/#pagination-and-overflow-outside-multicol>
let consumed_content_height = promoted_spanner_paint_slices(
paint_page_index,
self.pages.len(),
content_top,
self.cursor_y,
block_start_page_context,
self.current_page_context,
self.fragmentainer_override,
)
.iter()
.map(|slice| (slice.top - slice.bottom).max(0.0))
.sum::<f32>();
let remaining_content_height = (height - consumed_content_height).max(0.0);
if remaining_content_height > 0.01 {
self.consume_definite_block_size_through_fragmentainers(
self.cursor_y,
remaining_content_height,
);
}
fragmented_definite_block = true;
} else if self.pages.len() == paint_page_index
&& (fragments_as_promoted_spanner || fixed_principal_block_overflows_fragmentainer)
{
let vertical_fragmentation = if self.active_fragmentainer_kind()
== FragmentainerKind::Page
&& style.writing_mode == self.principal_flow.writing_mode
&& self.containing_block_writing_mode == self.principal_flow.writing_mode
{
self.consume_vertical_root_page_block_size(
layout_pt(outer_inline.width().points()),
PageTopBlockPosition::new(block_top),
)
} else {
None
};
if let Some(vertical_fragmentation) = vertical_fragmentation {
fragmented_definite_block = vertical_fragmentation.fragments.len() > 1;
vertical_root_fragmented_block = true;
vertical_root_page_fragmentation = Some(vertical_fragmentation);
suppress_own_principal_box_decoration = true;
} else if let Some(fixed_size) = fixed_principal_block_size {
self.consume_fixed_principal_block_size_through_fragmentainers(
content_top,
fixed_size,
principal_fragment_decoration,
principal_fragment_decoration_reservation,
);
fragmented_definite_block = self.pages.len() > paint_page_index;
} else {
// A content-sized promoted spanner is fragmented by its
// child flow. Its final numeric used height is not an
// fixed principal extent to consume again.
self.cursor_y = content_top - height;
}
} else {
self.cursor_y = content_top - height;
}
}
self.fragment_top_offsets.pop();
self.cursor_y -= style.padding.bottom + border_widths.bottom;
let block_bottom = self.cursor_y;
let fragmented_spanner_slices = if !vertical_root_fragmented_block
&& (fragments_as_promoted_spanner || fragmented_definite_block)
&& self.pages.len() > paint_page_index
{
promoted_spanner_paint_slices(
paint_page_index,
self.pages.len(),
block_top,
block_bottom,
block_start_page_context,
self.current_page_context,
self.fragmentainer_override,
)
} else {
Vec::new()
};
// An ordinary auto-sized block that crosses fragmentainers owns one
// fragment box in every fragmentainer it reaches. Its decoration is
// therefore painted per fragment, just like a definite promoted
// spanner, rather than being attached only to the final page on which
// used-size resolution happens. In particular, a forced column break
// extends every continued fragment through the remaining column and
// its background must cover that extent.
// <https://www.w3.org/TR/css-break-3/#break-decoration>
// <https://www.w3.org/TR/css-backgrounds-3/#box-decoration-break>
// A fragmented float is a parallel fragmentation flow. Its
// continuation must not manufacture principal-box fragments for an
// ordinary ancestor: only normal-flow and independently positioned
// descendant paint can extend this decoration span. BFC float
// containment is handled separately when resolving the BFC's used
// auto block size.
// <https://drafts.csswg.org/css-break/#parallel-flows>
// <https://www.w3.org/TR/css-backgrounds-3/#box-decoration-break>
let out_of_flow_fragmentainer_end = self
.pending_paint_fragments
.get(pending_paint_fragment_start..)
.unwrap_or_default()
.iter()
.filter(|fragment| fragment.kind != PendingPaintFragmentKind::FragmentedFloat)
.map(|fragment| fragment.page_index)
.chain(
self.positioned_layers
.get(positioned_layer_start..)
.unwrap_or_default()
.iter()
.map(|layer| layer.page_index),
)
.chain(
self.pending_positioned_fragmentation
.materialized_destination_end()
.filter(|target| {
pending_positioned_page_span_target_at_start
.is_none_or(|initial| *target > initial)
}),
)
.max();
// A deferred out-of-flow placement can discover a static position
// beyond the current column set while its in-flow containing block is
// still committing the next fragment. That placement must not make
// the auto-height ancestor manufacture further box fragments; its
// immediate continuation is the committed decoration boundary.
let block_fragmentainer_end = out_of_flow_fragmentainer_end
.map_or(self.pages.len(), |end| {
end.max(self.pages.len()).min(self.pages.len() + 1)
});
let out_of_flow_continues_block = block_fragmentainer_end > self.pages.len();
let block_end_page_context = if out_of_flow_continues_block {
self.fragmentainer_override
.map(|override_| override_.context_for_fragmentainer(block_fragmentainer_end))
.unwrap_or_else(|| self.resolved_page_context(block_fragmentainer_end + 1, false))
} else {
self.current_page_context
};
// A parallel descendant flow may continue after this block's own
// principal box has ended. That continuation must not extend the
// block's decoration range: it is an occupied destination containing
// descendant overflow, not another fragment of this box. Retain the
// actual principal block end here and let the pending descendant
// paint carry its own later-fragmentainer assignment.
//
// CSS Fragmentation defines parallel flows independently, and
// `box-decoration-break: clone` applies only to box fragments—not to
// every fragmentainer that happens to contain descendant ink.
// <https://www.w3.org/TR/css-break-3/#parallel-flows>
// <https://www.w3.org/TR/css-break-3/#break-decoration>
let decoration_block_bottom = block_bottom;
let mut fragmented_block_slices = if !vertical_root_fragmented_block
&& fragmented_spanner_slices.is_empty()
&& block_fragmentainer_end > paint_page_index
&& (principal_block_size_disposition
.fixed_content_height()
.is_none()
|| fragmented_definite_block)
{
promoted_spanner_paint_slices(
paint_page_index,
block_fragmentainer_end,
block_top,
decoration_block_bottom,
block_start_page_context,
block_end_page_context,
self.fragmentainer_override,
)
} else {
Vec::new()
};
let preserves_nominal_wrapped_multicol_extent =
style_establishes_multicol_formatting_context(style)
&& style.column_wrap != css::ColumnWrap::Nowrap
&& (!matches!(style.column_height, css::ComputedColumnHeight::Auto)
|| style.column_wrap == css::ColumnWrap::Wrap);
// A float continuation remains a parallel paint flow, but it can
// expose a class-A boundary with its immediately adjacent in-flow
// content and thereby leave a class-C opportunity after clearance.
// If that normal-flow continuation already produced a fragment of an
// auto-sized ancestor, its background owns the fragment's remaining
// block extent. Do not trim that existing box fragment to the float's
// local ink bounds: that would expose the multicol principal
// background in the class-C gap.
// <https://drafts.csswg.org/css-break-3/#parallel-flows>
// <https://drafts.csswg.org/css-break-3/#possible-breaks>
let has_pending_parallel_float_continuation = self
.pending_paint_fragments
.get(pending_paint_fragment_start..)
.unwrap_or_default()
.iter()
.any(|fragment| fragment.kind == PendingPaintFragmentKind::FragmentedFloat);
let has_materialized_parallel_float_continuation = self
.current_float_context_last_fragment_end()
.is_some_and(|(page_index, _)| page_index > paint_page_index);
let has_float_break_context = self
.float_contexts
.last()
.expect("root float context exists")
.has_css_float();
let has_parallel_float_continuation = has_pending_parallel_float_continuation
|| has_materialized_parallel_float_continuation
|| has_float_break_context;
if style.box_values.height.is_auto()
&& self.active_fragmentainer_kind() == FragmentainerKind::Column
&& !preserves_nominal_wrapped_multicol_extent
&& !has_parallel_float_continuation
{
// Fragmentation progress and an auto-sized principal box's
// decoration extent are independent. Trim intermediate slices to
// the committed child paint rather than the full capacity that a
// forced break consumed.
// <https://www.w3.org/TR/css-break-3/#break-decoration>
for slice in &mut fragmented_block_slices {
if slice.page_index >= self.pages.len() {
continue;
}
if let Some(bounds) = self.pages[slice.page_index].paint_fragment().bounds() {
let committed_bottom = slice.bottom.max(bounds.y());
// Even an otherwise empty leading fragment owns its
// block-start edge. Keep enough of that fragment to
// paint its border and padding instead of trimming it to
// zero when a descendant takes a forced break before it
// produces any paint in this fragmentainer.
// <https://www.w3.org/TR/css-break-3/#break-decoration>
let leading_decoration_bottom =
slice.top - (style.padding.top + border_widths.top);
slice.bottom = if slice.owns_block_start {
committed_bottom.min(leading_decoration_bottom)
} else {
committed_bottom
};
}
}
}
let block_height = if fragmented_spanner_slices.is_empty() {
(block_top - block_bottom).max(0.0)
} else {
fragmented_spanner_slices
.iter()
.map(|slice| (slice.top - slice.bottom).max(0.0))
.sum()
};
let paint_block_top = block_top - collapsed_start_margin_offset.points();
let paint_block_height = (block_height - collapsed_start_margin_offset.points()).max(0.0);
let border_box = geometry.border_box_top_rect(paint_block_top, paint_block_height);
let border_paint_rect = border_box.page_top_rect().paint_rect();
let vertical_root_border_paint_rect = vertical_root_page_fragmentation.as_ref().map(|_| {
// In vertical writing, the legacy block-layout rectangle's
// physical height follows its physical `height` property. The
// principal box's logical inline span is instead the used
// vertical extent selected before its horizontal block-axis
// fragments are assigned. Reconstruct that one physical source
// rectangle at the Writing Modes boundary before projection.
// <https://www.w3.org/TR/css-writing-modes-4/#dimension-mapping>
paint_space_rect(
outer_x,
self.current_page_context.top() - (content_logical_inline_size + vertical_extras),
outer_inline.width().points(),
content_logical_inline_size + vertical_extras,
)
});
let vertical_root_fragmentation_handled = vertical_root_page_fragmentation.is_some();
let rendered_legend_border_exclusion = element
.tag
.eq_ignore_ascii_case("fieldset")
.then_some(rendered_legend)
.flatten()
.and_then(|legend| legend.border_exclusion(border_paint_rect, paint_page_index));
self.record_static_scroll_snap_area(element, style, border_paint_rect);
self.record_static_scroll_target_area(element.is_target, border_paint_rect, style);
// CSS Break permits an oversized monolithic box to be sliced when it
// cannot fit a fragmentainer. Keep decoration-only size-contained
// boxes intact (matching replaced elements), while allowing boxes
// with fragmentable contents to use Spindrift's contiguous slice path.
// <https://www.w3.org/TR/css-break-3/#breaking-rules>
let retain_size_contained_monolithic_paint = containment.size
&& (border_paint_rect.size.height <= self.page_area_height() + 0.01
|| crate::text::trim_css_collapsible_whitespace(&inline_text_for_style(
element, style,
))
.is_empty());
if element.tag.eq_ignore_ascii_case("html") {
self.record_document_canvas_root_positioning_area(
PaintBackgroundArea::from_paint_rect(border_paint_rect),
);
self.document_canvas_overflow.record_auto_overflow(
border_paint_rect.size.width,
border_paint_rect.size.height,
self.current_page_context.area_width(),
self.current_page_context.area_height(),
);
}
// Auto-height overflow clips know their inline and block-start edges
// before child layout, but the block-end edge is only available after
// resolving the used height. CSS Overflow clips to the used padding box:
// <https://www.w3.org/TR/css-overflow-3/#overflow-clip-edge>.
let used_overflow_axes = self.used_overflow_axes_for_element(element, style);
let deferred_overflow_edge = needs_deferred_overflow_clip
.then(|| {
resolve_overflow_clip_edge(
border_paint_rect,
style,
border_widths,
used_overflow_axes,
paint_containment_applies,
None,
)
})
.flatten();
let deferred_overflow_clip = deferred_overflow_edge.as_ref().map(|edge| edge.clip.bounds);
let deferred_axis_selective_overflow_clip = deferred_overflow_edge
.as_ref()
.filter(|edge| edge.clips_x != edge.clips_y)
.map(|edge| AxisSelectivePaintClip::new(edge.clip.bounds, edge.clips_x, edge.clips_y));
let deferred_box_content_contour = deferred_overflow_edge
.as_ref()
.filter(|edge| edge.clips_x && edge.clips_y)
.map(|edge| edge.clip.clone())
.filter(|clip| !matches!(&clip.contour, BoxContentContour::Rect));
if block_height > 0.0 {
self.mark_current_page_flow_content();
}
// A definite principal box that fits its originating fragmentainer
// keeps its decoration there even when visible descendant overflow
// materializes later fragmentainers. Those later pages belong to the
// descendant paint, not to fragments of the principal box.
// <https://www.w3.org/TR/css-break-3/#fragmentation-model>
let background_page_index = if principal_block_size_disposition
.fixed_content_height()
.is_some()
&& !fragmented_definite_block
&& !fragments_as_promoted_spanner
{
paint_page_index
} else {
self.pages.len()
};
let propagates_document_canvas_properties =
self.element_propagates_document_canvas_properties(element, style);
// The root element's border is part of the root stacking context's
// background/border phase. It must not be promoted with ordinary
// block descendants, because negative positioned descendants paint
// only after that root phase.
// <https://www.w3.org/TR/CSS22/zindex.html>
let is_root_element = element.tag.eq_ignore_ascii_case("html");
let mut own_background_primitives = Vec::new();
let mut own_outline_primitives = Vec::new();
if propagates_document_canvas_properties && style.visibility == Visibility::Visible {
self.capture_document_canvas_background(element, style);
}
// Only the source selected by CSS Backgrounds paints the document
// canvas. A body whose background does not propagate remains ordinary
// descendant paint and must be captured by an ancestor root transform.
// <https://drafts.csswg.org/css-backgrounds-3/#special-backgrounds>
// <https://drafts.csswg.org/css-transforms-1/#transform-rendering>
let propagates_document_canvas_background = propagates_document_canvas_properties
&& self.element_paints_document_canvas_background(element);
if propagates_document_canvas_background {
// Capturing the root background creates the canvas-background
// record. Preserve the used root box after that creation: a
// position-fixed root can have a much smaller positioning area
// than the canvas it paints through, and percentages in
// `background-size` resolve against this box rather than the
// page canvas.
// <https://www.w3.org/TR/css-backgrounds-3/#special-backgrounds>
// <https://www.w3.org/TR/css-backgrounds-3/#the-background-size>
if element.tag.eq_ignore_ascii_case("html") {
self.record_document_canvas_root_positioning_area(
PaintBackgroundArea::from_paint_rect(border_paint_rect),
);
}
if !suppress_own_principal_box_decoration
&& (used_border_width(style) > layout_pt(0.0)
|| style.border_image.source.is_image())
&& style.visibility == Visibility::Visible
{
// CSS Backgrounds propagates the root/body background to the
// canvas, but borders are not canvas backgrounds; they remain
// ordinary element border painting behind descendants:
// <https://www.w3.org/TR/css-backgrounds-3/#special-backgrounds>.
let mut border_style = style.clone();
border_style.background.background_color = css::BackgroundColor::TRANSPARENT;
border_style.background.background_image = css::ComputedImage::None;
border_style.background.background_layers.clear();
own_background_primitives =
self.box_background_primitives(border_paint_rect, &border_style);
}
} else if !suppress_own_principal_box_decoration
&& !vertical_root_fragmented_block
&& fragmented_spanner_slices.is_empty()
&& fragmented_block_slices.is_empty()
&& ((border_paint_rect.size.width > 0.0 && border_paint_rect.size.height > 0.0)
|| used_border_width(style) > layout_pt(0.0)
|| style.border_image.source.is_image())
&& (style.background.background_color.is_potentially_visible()
|| style.background.background_image.is_image()
|| style.border_image.source.is_image()
|| used_border_width(style) > layout_pt(0.0))
&& style.visibility == Visibility::Visible
{
// Once body-to-canvas propagation is disabled, the body is an
// ordinary contained principal block. Its background therefore
// uses its own border box; only the resolver-selected root/body
// source can paint the page canvas.
// <https://drafts.csswg.org/css-backgrounds-3/#special-backgrounds>
if let Some(exclusion) = rendered_legend_border_exclusion.as_ref()
&& let Some((backgrounds, borders, deferred_images)) =
self.split_background_and_normal_border_primitives(border_paint_rect, style)
{
own_background_primitives = backgrounds;
own_background_primitives.extend(
self.clip_rectangular_border_primitives(borders, &exclusion.visible_regions),
);
own_background_primitives.extend(deferred_images);
} else {
own_background_primitives =
self.box_background_primitives(border_paint_rect, style);
}
}
if !suppress_own_principal_box_decoration
&& !vertical_root_fragmented_block
&& fragmented_spanner_slices.is_empty()
&& fragmented_block_slices.is_empty()
&& border_paint_rect.size.width > 0.0
&& border_paint_rect.size.height > 0.0
&& style.visibility == Visibility::Visible
{
own_outline_primitives = self.box_outline_primitives(border_paint_rect, style);
}
let has_own_background_primitives = !own_background_primitives.is_empty();
let has_own_outline_primitives = !own_outline_primitives.is_empty();
let scroll_content_bounds = self
.current_page
.paint_tree_fragment_since(&paint_checkpoint)
.bounds()
.map(PaintClip::paint_rect)
.unwrap_or(border_paint_rect);
let scroll_padding_box = deferred_overflow_clip
.map(PaintClip::paint_rect)
.unwrap_or(border_paint_rect);
let static_scroll_offset = self.finish_static_scroll_snap_scope(
static_scroll_snap_scope,
scroll_padding_box,
scroll_content_bounds,
);
let static_scroll_translation =
crate::layout::scroll_snap::static_scroll_translation(static_scroll_offset, style);
// Positioned descendants escape normal-flow paint capture. Those whose
// containing block is this box or one of its descendants remain its
// scrolling contents; an out-of-flow descendant whose containing block
// is outside this box is not clipped or scrolled by this intervening
// ancestor. Apply the effects before eligible layers replay into the
// ancestor stacking context.
// <https://www.w3.org/TR/CSS22/visufx.html#overflow-clipping>
// <https://www.w3.org/TR/css-overflow-3/#scrollable>
if static_scroll_translation.x != 0.0 || static_scroll_translation.y != 0.0 {
for layer in self
.positioned_layers
.get_mut(positioned_layer_start..)
.unwrap_or_default()
{
if !positioned_overflow_clip_boundary.applies_to(layer) {
continue;
}
*layer = layer.clone().translated(static_scroll_translation);
}
}
if let Some(overflow_clip) = deferred_overflow_clip {
let content_overflow_clip = match deferred_box_content_contour.as_ref() {
Some(ResolvedBoxContentClip {
contour: BoxContentContour::Empty,
..
}) => PaintClip::new(overflow_clip.x(), overflow_clip.y(), 0.0, 0.0),
_ => overflow_clip,
};
for layer in self
.positioned_layers
.get_mut(positioned_layer_start..)
.unwrap_or_default()
{
if !positioned_overflow_clip_boundary.applies_to(layer) {
continue;
}
// This escaped positioned layer still belongs to this scroll
// container. An overflow scope that cannot exclude any of its
// recorded ink is not a paint-order boundary. In
// particular, retaining it changes PDF edge coverage where
// a later opaque in-flow background fully covers the layer.
// <https://www.w3.org/TR/css-overflow-3/#overflow-clip-edge>
// A rectangular-bounds containment check cannot prove that a
// layer fits a curved `border-shape` contour. Keep the
// scope so positioned descendants are clipped at the shape,
// even when they lie wholly inside its rectangular bounds.
let layer_is_wholly_inside_clip = deferred_box_content_contour.is_none()
&& layer
.context
.bounds
.is_some_and(|bounds| content_overflow_clip.contains(bounds));
if layer_is_wholly_inside_clip {
continue;
}
let effective_bounds = layer
.context
.effects
.overflow_clip_bounds()
.and_then(|existing| existing.intersect(content_overflow_clip))
.unwrap_or(content_overflow_clip);
layer
.context
.effects
.intersect_overflow_clip_bounds(content_overflow_clip);
// Out-of-flow positioned descendants escape the ordinary
// fragment capture, so carry the enclosing contour onto the
// layer that will later be replayed into the ancestor stack.
// The rectangular clip above remains the conservative bounds
// for links and raster culling; this typed contour supplies
// the visible inner edge.
if let Some(mut contour) = deferred_box_content_contour.clone() {
contour.bounds = effective_bounds;
layer.context.effects.overflow_clip_effect = Some(
crate::document::paint::contours::OverflowClipEffect::Contoured(contour),
);
}
}
}
self.translate_aligned_block_descendant_bookmarks(
descendant_bookmark_start,
paint_page_index,
0.0,
block_align_content_offset_y,
);
if self.preserve_scoped_paint_public_order
&& !vertical_root_fragmentation_handled
&& self.pages.len() == paint_page_index
&& block_align_content_offset_y.abs() <= 0.01
&& !vertical_block_align_content_needs_fragment_bounds(style)
{
let mut fragment = self
.current_page
.paint_tree_fragment_since(&paint_checkpoint);
// Descendant block backgrounds are captured in BackgroundBorder,
// but that band is reserved for this box's own decoration by the
// overflow helper. Promote descendants before creating the clip
// scope so their paint remains part of the scrolling contents.
fragment.promote_background_border_to_in_flow_block();
fragment.promote_outline_to_in_flow_outline();
if static_scroll_translation.x != 0.0 || static_scroll_translation.y != 0.0 {
fragment = fragment.translated(static_scroll_translation);
}
if let Some(overflow_clip) = deferred_overflow_clip {
fragment = if let Some(contour) = deferred_box_content_contour {
fragment.with_contents_effect_scoped_to_box_content_contour(contour)
} else if paint_containment_applies {
fragment.with_paint_containment_contents_effect_scoped_to_rect(
&self.current_page,
overflow_clip,
)
} else if let Some(axis_selective_clip) = deferred_axis_selective_overflow_clip {
fragment.with_contents_effect_scoped_to_axis_selective_rect(axis_selective_clip)
} else {
// Determine whether a descendant needs the effect from
// its untrimmed ink bounds. Clipping primitives first
// makes overflowing paint appear contained and erases
// the PDF clip scope that must remain authoritative.
fragment.with_contents_effect_scoped_to_rect_preserving_contained_ink(
&self.current_page,
overflow_clip,
)
};
}
if is_root_element {
fragment.promote_background_border_to_in_flow_block();
fragment.promote_outline_to_in_flow_outline();
}
if background_page_index == paint_page_index {
self.current_page.prepend_recorded_primitives_to_fragment(
&mut fragment,
PaintBand::BackgroundBorder,
own_background_primitives,
);
self.current_page.append_recorded_primitives_to_fragment(
&mut fragment,
PaintBand::Outline,
own_outline_primitives,
);
}
// A synthesized continuation has no descendant paint tree that
// could promote this principal-box decoration later. Its
// background therefore needs its resolved parent-level phase
// immediately.
if !is_root_element {
fragment.promote_background_border_to_in_flow_block();
fragment.promote_outline_to_in_flow_outline();
}
if retain_size_contained_monolithic_paint {
fragment = fragment.with_monolithic_fragmentation_scope(
PaintClip::from_paint_rect(border_paint_rect),
);
}
if ((has_own_background_primitives || has_own_outline_primitives)
|| deferred_overflow_clip.is_some())
&& !fragment.is_empty()
{
self.current_page
.replace_paint_tree_since_with_fragment(&paint_checkpoint, fragment);
}
self.cursor_y -= block_end_margin_to_consume;
self.last_block_layout_outcome = BlockLayoutOutcome {
consumed_bottom_margin: layout_pt(block_end_margin_to_consume),
margin_collapse_boundary,
physical_border_box_inline_span: outer_inline.width(),
static_border_box: Some(border_paint_rect),
clamp_line_slots,
has_local_continuation_cutoff,
in_flow_child_fragment_end,
};
if matches!(style.position, Position::Relative | Position::Sticky) {
self.cursor_y -= relative_offset.y();
}
self.apply_forced_break_after_box_in(fragmentainer_kind, style);
return;
}
let fragments =
self.take_positioned_fragments_since(paint_page_index, (*paint_checkpoint).clone());
let vertical_root_projected_fragments = if let Some(fragmentation) =
vertical_root_page_fragmentation.as_ref()
{
let vertical_border_rect = vertical_root_border_paint_rect
.expect("vertical root fragmentation supplies a logical border rect");
let axes = FlowAxes::new(style.writing_mode, style.used_direction());
let first_fragment_inline_offset = (fragmentation.first_inline_origin.points()
- self.current_page_context.top())
.abs();
let source_extent = LogicalSize {
inline: vertical_border_rect.size.height,
block: vertical_border_rect.size.width,
};
let source_origin = PageTopPoint::new(
vertical_border_rect.origin.x,
vertical_border_rect.origin.y + vertical_border_rect.size.height,
);
// Capture descendants before appending the projected principal-box
// decoration; otherwise the newly materialized decorations would
// be replayed a second time as source content.
let captured_source = self
.current_page
.take_paint_fragment_since((*paint_checkpoint).clone());
self.vertical_root_block_fragment_paint(
&fragmentation.fragments,
style,
vertical_border_rect,
);
let projected = self.project_vertical_root_fragment_paint(
captured_source,
&fragmentation.fragments,
axes,
source_origin,
source_extent,
);
let projected = if first_fragment_inline_offset > 0.01 {
let first_fragment_translation = match axes.inline_start_side() {
PhysicalSide::Top => PaintTranslation::new(0.0, -first_fragment_inline_offset),
PhysicalSide::Bottom => {
PaintTranslation::new(0.0, first_fragment_inline_offset)
}
PhysicalSide::Left | PhysicalSide::Right => {
unreachable!("vertical root page flow has a vertical logical inline axis")
}
};
projected
.into_iter()
.map(|(page_index, fragment)| {
if page_index == fragmentation.fragments[0].page_index {
(page_index, fragment.translated(first_fragment_translation))
} else {
(page_index, fragment)
}
})
.collect()
} else {
projected
};
Some(projected)
} else {
None
};
if let Some(projected) = vertical_root_projected_fragments {
for (page_index, fragment) in projected {
if page_index < self.pages.len() {
self.pages[page_index]
.append_paint_fragment_owned(fragment, PaintTranslation::identity());
} else {
self.current_page
.append_paint_fragment_owned(fragment, PaintTranslation::identity());
}
}
self.cursor_y -= block_end_margin_to_consume;
self.last_block_layout_outcome = BlockLayoutOutcome {
consumed_bottom_margin: layout_pt(block_end_margin_to_consume),
margin_collapse_boundary,
physical_border_box_inline_span: outer_inline.width(),
static_border_box: Some(border_paint_rect),
clamp_line_slots,
has_local_continuation_cutoff,
in_flow_child_fragment_end,
};
if matches!(style.position, Position::Relative | Position::Sticky) {
self.cursor_y -= relative_offset.y();
}
self.apply_forced_break_after_box_in(fragmentainer_kind, style);
return;
}
let first_descendant_paint_page = fragments
.iter()
.filter(|(_, fragment)| !fragment.is_empty())
.map(|(page_index, _)| *page_index)
.min();
// If an auto-height block has no start-edge material or direct inline
// content, and its first child prebreaks, the block's first fragment
// begins with that child. Painting a decoration-only fragment in the
// preceding remainder would manufacture background before the box has
// generated any fragment content.
// <https://www.w3.org/TR/css-break-3/#box-splitting>
let suppress_leading_empty_block_fragment = out_of_flow_fragmentainer_end.is_none()
&& fragmentainer_kind == FragmentainerKind::Page
&& has_auto_height(style)
&& !has_direct_inline_content
&& border_widths.top <= 0.01
&& style.padding.top <= 0.01
&& first_descendant_paint_page.is_some_and(|page| page > paint_page_index);
let mut decorated_block_pages = Vec::new();
let mut translated_vertical_bookmarks = false;
for (page_index, mut fragment) in fragments {
// A non-fragmented overflow-clipping box owns only its originating
// fragmentainer. Descendant overflow must not manufacture later
// paged-media fragments: those pages are outside the scrollport
// and are discarded before the container's clip is applied.
// <https://www.w3.org/TR/css-overflow-3/#scrollable-overflow>
if deferred_overflow_clip.is_some()
&& !has_auto_height(style)
&& !fragmented_definite_block
&& fragmented_block_slices.is_empty()
&& fragmented_spanner_slices.is_empty()
&& page_index != paint_page_index
{
continue;
}
if suppress_leading_empty_block_fragment
&& first_descendant_paint_page.is_some_and(|first_page| page_index < first_page)
{
continue;
}
// This captured fragment contains descendant block decorations in
// the BackgroundBorder band. Move them into the in-flow phase
// before applying the container's overflow scope; otherwise the
// scope deliberately preserves BackgroundBorder for the container
// itself and descendant backgrounds can escape the clip.
// <https://www.w3.org/TR/css-overflow-3/#overflow-clipping>
fragment.promote_background_border_to_in_flow_block();
fragment.promote_outline_to_in_flow_outline();
let mut block_align_content_offset_x = 0.0;
if page_index == paint_page_index {
block_align_content_offset_x = vertical_block_align_content_x_offset(
style,
content_inline.span(),
fragment.bounds(),
);
if block_align_content_offset_x.abs() > 0.01 && !translated_vertical_bookmarks {
self.translate_aligned_block_descendant_bookmarks(
descendant_bookmark_start,
paint_page_index,
block_align_content_offset_x,
0.0,
);
translated_vertical_bookmarks = true;
}
}
if page_index == paint_page_index
&& (block_align_content_offset_x.abs() > 0.01
|| block_align_content_offset_y.abs() > 0.01)
{
fragment = fragment.translated(PaintTranslation::new(
block_align_content_offset_x,
block_align_content_offset_y,
));
}
if page_index == paint_page_index
&& (static_scroll_translation.x != 0.0 || static_scroll_translation.y != 0.0)
{
fragment = fragment.translated(static_scroll_translation);
}
let fragment_local_overflow_edge = fragmented_block_slices
.iter()
.find(|slice| slice.page_index == page_index)
.and_then(|slice| {
let decoration = slice.decoration(style.box_decoration_break);
let slice_height = (slice.top - slice.bottom).max(0.0);
let decoration_height = if style.box_decoration_break
== css::BoxDecorationBreak::Clone
{
let capacity = if slice.page_index == paint_page_index {
block_start_page_context.area_height()
} else {
self.fragmentainer_override
.map(|override_| override_.continuation_context().area_height())
.unwrap_or_else(|| self.current_page_context.area_height())
};
(slice_height + style.padding.bottom + border_widths.bottom).min(capacity)
} else {
slice_height
};
let mut fragment_style = style.clone();
suppress_fragmented_box_edges(
&mut fragment_style,
decoration.owns_block_start(),
decoration.owns_block_end(),
);
resolve_overflow_clip_edge(
paint_space_rect(
outer_x,
slice.top - decoration_height,
outer_inline.width().points(),
decoration_height,
),
&fragment_style,
used_border_widths(&fragment_style),
used_overflow_axes,
paint_containment_applies,
None,
)
});
let fragment_overflow_clip = deferred_overflow_clip.map(|overflow_clip| {
if has_auto_height(style) && page_index != paint_page_index {
// An auto-height overflow BFC fragments with its
// contents. Its destination fragment owns a fresh
// overflow clip at that fragment's painted inline origin,
// rather than retaining the source page's clip edge.
// <https://www.w3.org/TR/css-break-3/#box-splitting>
// <https://www.w3.org/TR/css-overflow-3/#overflow-clipping>
fragment.bounds().map_or(overflow_clip, |bounds| {
let destination_height =
overflow_clip.height() + (overflow_clip.y() - bounds.y()).max(0.0);
let canvas_block_start = self
.document_canvas_fragment_insets
.iter()
.map(|inset| inset.top)
.sum::<f32>();
PaintClip::new(
bounds.x(),
bounds.y(),
overflow_clip.width(),
destination_height + canvas_block_start,
)
})
} else {
overflow_clip
}
});
if let Some(edge) = fragment_local_overflow_edge {
fragment = if edge.clips_x && edge.clips_y {
match edge.clip.contour {
BoxContentContour::Rect if paint_containment_applies => fragment
.with_paint_containment_contents_effect_scoped_to_rect(
&self.current_page,
edge.clip.bounds,
),
BoxContentContour::Rect => fragment
.with_contents_effect_scoped_to_rect_preserving_contained_ink(
&self.current_page,
edge.clip.bounds,
),
_ => fragment.with_contents_effect_scoped_to_box_content_contour(edge.clip),
}
} else {
fragment.with_contents_effect_scoped_to_axis_selective_rect(
AxisSelectivePaintClip::new(edge.clip.bounds, edge.clips_x, edge.clips_y),
)
};
} else if let Some(overflow_clip) = fragment_overflow_clip {
fragment = if let Some(contour) = deferred_box_content_contour.clone() {
fragment.with_contents_effect_scoped_to_box_content_contour(contour)
} else if paint_containment_applies {
fragment.with_paint_containment_contents_effect_scoped_to_rect(
&self.current_page,
overflow_clip,
)
} else if let Some(axis_selective_clip) = deferred_axis_selective_overflow_clip {
fragment.with_contents_effect_scoped_to_axis_selective_rect(axis_selective_clip)
} else {
fragment.with_contents_effect_scoped_to_rect_preserving_contained_ink(
&self.current_page,
overflow_clip,
)
};
}
if let Some(slice) = fragmented_block_slices
.iter()
.find(|slice| slice.page_index == page_index)
{
let mut fragment_style = style.clone();
let decoration = slice.decoration(style.box_decoration_break);
if propagates_document_canvas_background {
suppress_document_canvas_background(&mut fragment_style);
}
suppress_fragmented_box_edges(
&mut fragment_style,
decoration.owns_block_start(),
decoration.owns_block_end(),
);
if style.visibility == Visibility::Visible {
let slice_height = (slice.top - slice.bottom).max(0.0);
let decoration_height = if style.box_decoration_break
== css::BoxDecorationBreak::Clone
{
let capacity = if slice.page_index == paint_page_index {
block_start_page_context.area_height()
} else {
self.fragmentainer_override
.map(|override_| override_.continuation_context().area_height())
.unwrap_or_else(|| self.current_page_context.area_height())
};
// Child layout enters a continuation below its
// cloned block-start edge, so this source slice
// already contains that edge. Its block-end edge
// remains reserved below the child cursor and
// must be restored to the destination border box.
// Adding both edges would double-count the start;
// adding neither leaves the final clone fragment
// short by its border-plus-padding reservation.
(slice_height + style.padding.bottom + border_widths.bottom).min(capacity)
} else {
slice_height
};
let decoration_bottom = slice.top - decoration_height;
let decoration_bounds = PaintClip::new(
outer_x,
decoration_bottom,
outer_inline.width().points(),
decoration_height,
);
let committed_fragment =
slice.principal_box_fragment(decoration_bounds, decoration);
let fragment_border_rect = paint_space_rect(
outer_x,
decoration_bottom,
outer_inline.width().points(),
decoration_height,
);
let legend_exclusion = rendered_legend.and_then(|legend| {
legend.border_exclusion(fragment_border_rect, slice.page_index)
});
let backgrounds = self.box_background_primitives_with_legend_border_exclusion(
fragment_border_rect,
fragmented_slice_background_positioning_border_rect(
slice,
&fragmented_block_slices,
border_paint_rect,
fragment_border_rect,
style.box_decoration_break,
),
&fragment_style,
legend_exclusion.as_ref(),
);
let outlines = self.box_outline_primitives(
paint_space_rect(
outer_x,
decoration_bottom,
outer_inline.width().points(),
decoration_height,
),
&fragment_style,
);
if committed_fragment
.kind()
.principal_box()
.expect("principal block slice retains decoration geometry")
.decoration()
.is_clone()
{
fragment.prepend_monolithic_primitives_in_band(
PaintBand::BackgroundBorder,
decoration_bounds,
backgrounds,
);
fragment.append_monolithic_primitives_in_band(
PaintBand::Outline,
decoration_bounds,
outlines,
);
} else {
fragment
.prepend_primitives_in_band(PaintBand::BackgroundBorder, backgrounds);
fragment.append_primitives_in_band(PaintBand::Outline, outlines);
}
}
decorated_block_pages.push(page_index);
} else if page_index == background_page_index {
if is_root_element {
// Descendant block backgrounds recorded by the root
// still belong to normal flow, while the root's own
// border remains in Appendix E's earlier root phase.
fragment.promote_background_border_to_in_flow_block();
fragment.promote_outline_to_in_flow_outline();
}
fragment.prepend_primitives_in_band(
PaintBand::BackgroundBorder,
own_background_primitives.clone(),
);
fragment
.append_primitives_in_band(PaintBand::Outline, own_outline_primitives.clone());
}
if !defer_own_decoration_promotion && !is_root_element {
fragment.promote_background_border_to_in_flow_block();
fragment.promote_outline_to_in_flow_outline();
}
if retain_size_contained_monolithic_paint {
fragment = fragment.with_monolithic_fragmentation_scope(
PaintClip::from_paint_rect(border_paint_rect),
);
}
if fragment.is_empty() {
continue;
}
if page_index < self.pages.len() {
self.pages[page_index]
.append_paint_fragment_owned(fragment, PaintTranslation::identity());
} else {
self.current_page
.append_paint_fragment_owned(fragment, PaintTranslation::identity());
}
}
// A fragment can contain only the principal box's decoration (for
// example when a forced break occurs beside an empty child). Preserve
// that fragment even when there was no descendant paint tree to which
// the decoration could be prepended above.
for slice in fragmented_block_slices
.iter()
.filter(|slice| !decorated_block_pages.contains(&slice.page_index))
.filter(|slice| {
!suppress_leading_empty_block_fragment
|| first_descendant_paint_page
.is_none_or(|first_page| slice.page_index >= first_page)
})
{
if style.visibility != Visibility::Visible {
continue;
}
let mut fragment_style = style.clone();
let decoration = slice.decoration(style.box_decoration_break);
if propagates_document_canvas_background {
suppress_document_canvas_background(&mut fragment_style);
}
suppress_fragmented_box_edges(
&mut fragment_style,
decoration.owns_block_start(),
decoration.owns_block_end(),
);
let mut fragment = PaintFragment::from_primitives(Vec::new(), Vec::new());
let slice_height = (slice.top - slice.bottom).max(0.0);
let decoration_height = if style.box_decoration_break == css::BoxDecorationBreak::Clone
{
let capacity = if slice.page_index == paint_page_index {
block_start_page_context.area_height()
} else {
self.fragmentainer_override
.map(|override_| override_.continuation_context().area_height())
.unwrap_or_else(|| self.current_page_context.area_height())
};
(slice_height + style.padding.bottom + border_widths.bottom).min(capacity)
} else {
slice_height
};
let decoration_bottom = slice.top - decoration_height;
let decoration_bounds = PaintClip::new(
outer_x,
decoration_bottom,
outer_inline.width().points(),
decoration_height,
);
let committed_fragment = slice.principal_box_fragment(decoration_bounds, decoration);
let fragment_border_rect = paint_space_rect(
outer_x,
decoration_bottom,
outer_inline.width().points(),
decoration_height,
);
let legend_exclusion = rendered_legend
.and_then(|legend| legend.border_exclusion(fragment_border_rect, slice.page_index));
let backgrounds = self.box_background_primitives_with_legend_border_exclusion(
fragment_border_rect,
fragmented_slice_background_positioning_border_rect(
slice,
&fragmented_block_slices,
border_paint_rect,
fragment_border_rect,
style.box_decoration_break,
),
&fragment_style,
legend_exclusion.as_ref(),
);
let outlines = self.box_outline_primitives(
paint_space_rect(
outer_x,
decoration_bottom,
outer_inline.width().points(),
decoration_height,
),
&fragment_style,
);
if committed_fragment
.kind()
.principal_box()
.expect("principal block slice retains decoration geometry")
.decoration()
.is_clone()
{
fragment.prepend_monolithic_primitives_in_band(
PaintBand::BackgroundBorder,
decoration_bounds,
backgrounds,
);
fragment.append_monolithic_primitives_in_band(
PaintBand::Outline,
decoration_bounds,
outlines,
);
} else {
fragment.prepend_primitives_in_band(PaintBand::BackgroundBorder, backgrounds);
fragment.append_primitives_in_band(PaintBand::Outline, outlines);
}
if !defer_own_decoration_promotion {
fragment.promote_background_border_to_in_flow_block();
fragment.promote_outline_to_in_flow_outline();
}
if fragment.is_empty() {
continue;
}
if slice.page_index < self.pages.len() {
self.pages[slice.page_index]
.append_paint_fragment_owned(fragment, PaintTranslation::identity());
} else if slice.page_index == self.pages.len() {
self.current_page
.append_paint_fragment_owned(fragment, PaintTranslation::identity());
} else {
self.pending_paint_fragments.push(PendingPaintFragment {
page_index: slice.page_index,
fragment,
kind: PendingPaintFragmentKind::InFlowOverflow,
});
}
}
if style.visibility == Visibility::Visible {
for slice in &fragmented_spanner_slices {
let mut fragment_style = style.clone();
let decoration = slice.decoration(style.box_decoration_break);
if propagates_document_canvas_background {
suppress_document_canvas_background(&mut fragment_style);
}
suppress_fragmented_box_edges(
&mut fragment_style,
decoration.owns_block_start(),
decoration.owns_block_end(),
);
let mut fragment = PaintFragment::from_primitives(Vec::new(), Vec::new());
let slice_height = (slice.top - slice.bottom).max(0.0);
let decoration_height =
if style.box_decoration_break == css::BoxDecorationBreak::Clone {
let capacity = if slice.page_index == paint_page_index {
block_start_page_context.area_height()
} else {
self.fragmentainer_override
.map(|override_| override_.continuation_context().area_height())
.unwrap_or_else(|| self.current_page_context.area_height())
};
(slice_height + style.padding.bottom + border_widths.bottom).min(capacity)
} else {
slice_height
};
let decoration_bottom = slice.top - decoration_height;
let decoration_bounds = PaintClip::new(
outer_x,
decoration_bottom,
outer_inline.width().points(),
decoration_height,
);
let committed_fragment =
slice.principal_box_fragment(decoration_bounds, decoration);
let fragment_border_rect = paint_space_rect(
outer_x,
decoration_bottom,
outer_inline.width().points(),
decoration_height,
);
let legend_exclusion = rendered_legend.and_then(|legend| {
legend.border_exclusion(fragment_border_rect, slice.page_index)
});
let backgrounds = self.box_background_primitives_with_legend_border_exclusion(
fragment_border_rect,
fragmented_slice_background_positioning_border_rect(
slice,
&fragmented_spanner_slices,
border_paint_rect,
fragment_border_rect,
style.box_decoration_break,
),
&fragment_style,
legend_exclusion.as_ref(),
);
let outlines = self.box_outline_primitives(
paint_space_rect(
outer_x,
decoration_bottom,
outer_inline.width().points(),
decoration_height,
),
&fragment_style,
);
if committed_fragment
.kind()
.principal_box()
.expect("principal block slice retains decoration geometry")
.decoration()
.is_clone()
{
fragment.prepend_monolithic_primitives_in_band(
PaintBand::BackgroundBorder,
decoration_bounds,
backgrounds,
);
fragment.append_monolithic_primitives_in_band(
PaintBand::Outline,
decoration_bounds,
outlines,
);
} else {
fragment.prepend_primitives_in_band(PaintBand::BackgroundBorder, backgrounds);
fragment.append_primitives_in_band(PaintBand::Outline, outlines);
}
// See the equivalent ordinary-fragment loop above: this is
// the sole paint owner for a synthesized definite span.
fragment.promote_background_border_to_in_flow_block();
fragment.promote_outline_to_in_flow_outline();
if slice.page_index < self.pages.len() {
self.pages[slice.page_index]
.append_paint_fragment_owned(fragment, PaintTranslation::identity());
} else {
self.current_page
.append_paint_fragment_owned(fragment, PaintTranslation::identity());
}
}
}
self.cursor_y -= block_end_margin_to_consume;
self.last_block_layout_outcome = BlockLayoutOutcome {
consumed_bottom_margin: layout_pt(block_end_margin_to_consume),
margin_collapse_boundary,
physical_border_box_inline_span: outer_inline.width(),
static_border_box: Some(border_paint_rect),
clamp_line_slots,
has_local_continuation_cutoff,
in_flow_child_fragment_end,
};
if matches!(style.position, Position::Relative | Position::Sticky) {
self.cursor_y -= relative_offset.y();
}
self.apply_forced_break_after_box_in(fragmentainer_kind, style);
}
/// Position the content end of a definite promoted spanner through the
/// current outer fragmentainer sequence.
///
/// Unlike an ordinary class-A sibling, a promoted spanner may itself be
/// fragmented by an enclosing page or multicol context. Consuming the
/// definite size continuously avoids leaving unused remainder space before
/// moving to the next outer fragmentainer.
/// <https://www.w3.org/TR/css-multicol-1/#spanning-columns>
/// <https://www.w3.org/TR/css-break-3/#breaking-rules>
/// Consume a fixed principal block through page or outer-column
/// fragmentainers.
///
/// CSS sizing preserves a definite descendant percentage basis as a
/// content-box length, but a cloned fixed `border-box` consumes its
/// destination extent a fragment at a time. Keep that projection at the
/// fragmentation cursor rather than converting the size to an untyped
/// scalar at layout entry.
/// <https://www.w3.org/TR/css-sizing-3/#box-model>
/// <https://www.w3.org/TR/css-break-3/#box-model-for-breaking>
fn consume_fixed_principal_block_size_through_fragmentainers(
&mut self,
content_top: f32,
size: FixedPrincipalBlockSize,
decoration: FragmentDecoration,
reservation: FragmentDecorationReservation,
) {
if let Some(mut progress) = size.cloned_border_box_progress(decoration, reservation) {
self.consume_cloned_border_box_through_fragmentainers(content_top, &mut progress);
} else {
self.consume_definite_block_size_through_fragmentainers(
content_top,
size.content_height().points(),
);
}
}
/// Consume a cloned fixed border-box's destination budget through the
/// active fragmentainer sequence.
///
/// The fragmentainer cursor advances only by the content portion returned
/// by [`ClonedBorderBoxProgress`]; each fragment's repeated decoration is
/// already represented by its destination page coordinates.
fn consume_cloned_border_box_through_fragmentainers(
&mut self,
content_top: f32,
progress: &mut ClonedBorderBoxProgress,
) {
self.cursor_y = content_top;
let consume_current = |progress: &mut ClonedBorderBoxProgress, content_capacity: f32| {
progress.consume_content_capacity(layout_pt(content_capacity))
};
if self.active_fragmentainer_kind() == FragmentainerKind::Column {
let available = (self.cursor_y - self.page_bottom()).max(0.0);
self.cursor_y -= consume_current(progress, available).points();
if progress.is_complete() {
return;
}
self.mark_current_page_flow_content();
let materialization_limit = self
.positioned_scratch_page_limit()
.unwrap_or(MAX_MATERIALIZED_COLUMN_FRAGMENTAINERS);
while !progress.is_complete() && self.pages.len() + 1 < materialization_limit {
self.push_page();
let destination_content_capacity =
(self.cursor_y - self.page_bottom()).max(css::CSS_PX_TO_PT);
self.cursor_y -= consume_current(progress, destination_content_capacity).points();
if progress.is_complete() {
return;
}
self.mark_current_page_flow_content();
}
if !progress.is_complete() {
self.cursor_y = self.page_top() - progress.remaining_border_box().points();
}
return;
}
let available = (self.cursor_y - self.page_bottom()).max(0.0);
self.cursor_y -= consume_current(progress, available).points();
if progress.is_complete() {
return;
}
self.mark_current_page_flow_content();
let materialization_limit = self
.positioned_scratch_page_limit()
.unwrap_or(MAX_MATERIALIZED_PAGE_FRAGMENTAINERS);
while !progress.is_complete() && self.pages.len() + 1 < materialization_limit {
self.push_page();
let destination_content_capacity =
(self.cursor_y - self.page_bottom()).max(css::CSS_PX_TO_PT);
self.cursor_y -= consume_current(progress, destination_content_capacity).points();
if progress.is_complete() {
return;
}
self.mark_current_page_flow_content();
}
if !progress.is_complete() {
self.cursor_y = self.page_top() - progress.remaining_border_box().points();
}
}
/// Consume a definite physical block size continuously through page or
/// outer-column fragmentainers.
///
/// Oversized monolithic boxes remain one layout object, but CSS
/// Fragmentation still places their graphical slices in every crossed
/// fragmentainer and resumes following flow at the continuous block-end.
/// This primitive is shared by definite blocks, promoted spanners, and
/// oversized atomic line boxes.
/// <https://www.w3.org/TR/css-break-3/#monolithic>
pub(in crate::layout) fn consume_definite_block_size_through_fragmentainers(
&mut self,
content_top: f32,
height: f32,
) {
self.cursor_y = content_top;
let mut remaining = height.max(0.0);
if self.active_fragmentainer_kind() == FragmentainerKind::Column {
let available = (self.cursor_y - self.page_bottom()).max(0.0);
if remaining <= available + 0.01 {
self.cursor_y -= remaining;
return;
}
if available > 0.01 {
self.cursor_y -= available;
remaining -= available;
self.mark_current_page_flow_content();
}
let materialization_limit = self
.positioned_scratch_page_limit()
.unwrap_or(MAX_MATERIALIZED_COLUMN_FRAGMENTAINERS);
while remaining > 0.01 && self.pages.len() + 1 < materialization_limit {
self.push_page();
// `push_page` enters below every active cloned block-start
// edge, while `page_bottom` retains their block-end edges.
// Compute the usable content capacity from those destination
// coordinates instead of from raw column height. A raw
// chunk would let a definite box consume its cloned border
// and padding, shortening the final principal fragment.
// <https://www.w3.org/TR/css-break-3/#box-model-for-breaking>
let destination_capacity =
(self.cursor_y - self.page_bottom()).max(css::CSS_PX_TO_PT);
let consumed = remaining.min(destination_capacity);
self.cursor_y -= consumed;
remaining -= consumed;
if remaining > 0.01 {
self.mark_current_page_flow_content();
} else {
return;
}
}
if remaining > 0.01 {
// The retained current page stands in for the last conceptual
// off-canvas column so following invisible flow preserves its
// block offset without allocating the skipped prefix.
self.cursor_y = self.page_top() - remaining;
}
return;
}
let available = (self.cursor_y - self.page_bottom()).max(0.0);
if remaining <= available + 0.01 {
self.cursor_y -= remaining;
return;
}
if available > 0.01 {
self.cursor_y -= available;
remaining -= available;
// The box occupies this fragment even when its background and
// descendants are appended after used-size resolution. Mark that
// occupancy so the empty-page guard does not collapse a real
// definite block fragment into its continuation.
self.mark_current_page_flow_content();
}
let materialization_limit = self
.positioned_scratch_page_limit()
.unwrap_or(MAX_MATERIALIZED_PAGE_FRAGMENTAINERS);
while remaining > 0.01 && self.pages.len() + 1 < materialization_limit {
self.push_page();
// Every page fragmentainer may establish a distinct page area.
// Preserve the source box's continuous block progress in
// `remaining`, but resolve the destination capacity only after
// advancing into that destination context.
// <https://drafts.csswg.org/css-break-4/#varying-size-fragmentainers>
let destination_capacity = (self.cursor_y - self.page_bottom()).max(css::CSS_PX_TO_PT);
let consumed = remaining.min(destination_capacity);
self.cursor_y -= consumed;
remaining -= consumed;
if remaining > 0.01 {
self.mark_current_page_flow_content();
} else {
return;
}
}
if remaining > 0.01 {
// Retain the bounded materialized prefix for pathological
// lengths. The current destination keeps the source's remaining
// logical progress without allocating an unbounded page run.
self.cursor_y = self.page_top() - remaining;
}
}
fn definite_block_descendants_overflow(
&mut self,
child_boxes: Option<&[box_tree::FormattingBox<'_>]>,
stylesheets: &Stylesheets<'_>,
available_width: f32,
probe: DeferredDescendantOverflowProbe,
) -> bool {
let Some(child_boxes) = child_boxes else {
return false;
};
let estimated = child_boxes
.iter()
.filter_map(|child| match child {
box_tree::FormattingBox::AnonymousBlock(box_) => {
formatting_box_has_inline_content(&box_.children)
.then_some(box_.style.line_height)
}
_ => child
.element_parts()
.and_then(|(element, _, style, children)| {
style_is_in_normal_flow(style).then(|| {
self.estimate_element_height(
element,
style,
stylesheets,
available_width,
Some(children),
)
})
})
.flatten(),
})
.sum::<f32>();
estimated > probe.content_height().points() + 0.01
}
/// Lay out overflowing descendants independently from a definite
/// principal box's normal-flow end position.
///
/// CSS overflow does not enlarge a definite-height box. Descendant paint
/// can therefore reach later outer fragmentainers while the following
/// sibling starts at the principal box's authored block-end. This applies
/// both to a promoted spanner and to an ordinary definite box that fits in
/// its current column. Spindrift already uses this speculative/deferred-paint
/// model for fragmented floats; the same mechanism preserves counters,
/// bookmarks, links, named strings, and running elements while restoring
/// normal-flow geometry.
/// <https://www.w3.org/TR/css-multicol-1/#spanning-columns>
/// <https://www.w3.org/TR/css-break-3/#fragmentation-model>
#[allow(clippy::too_many_arguments)]
fn layout_definite_block_with_deferred_descendant_overflow(
&mut self,
element: &Element,
style: &ComputedStyle,
stylesheets: &Stylesheets<'_>,
run_in_children: &[box_tree::FormattingBox<'_>],
child_boxes: Option<&[box_tree::FormattingBox<'_>]>,
descendant_percentage_height_basis: Option<BlockSizePercentageBasis>,
descendant_percentage_height_context: Option<DescendantBlockPercentageContext>,
replayed_item_logical_inline_size: Option<LogicalInlineContentSize>,
probe: DeferredDescendantOverflowProbe,
vertical_non_content: NonContentLength,
principal_box_paint_mode: PrincipalBoxPaintMode,
) {
let fragmentainer_kind = self.active_fragmentainer_kind();
let snapshot = self.snapshot();
let paint_page_index = self.pages.len();
let paint_checkpoint = self.current_page.paint_checkpoint();
self.multicol_spanner_speculation_depth += 1;
self.fragmentation_suppression_depth += 1;
self.layout_block_with_descendant_percentage_height_basis(
element,
style,
stylesheets,
run_in_children,
child_boxes,
descendant_percentage_height_basis,
descendant_percentage_height_context,
replayed_item_logical_inline_size,
principal_box_paint_mode,
);
self.fragmentation_suppression_depth -= 1;
self.multicol_spanner_speculation_depth -= 1;
let clearance_crossed_fragmentainer = self.pages.len() > snapshot.page_count()
&& self.last_block_layout_outcome.margin_collapse_boundary
== BlockMarginCollapseBoundary::SeparatedByClearance;
let captured_fragments =
self.take_positioned_fragments_since(paint_page_index, paint_checkpoint);
let fragments = captured_fragments
.into_iter()
.flat_map(|(page_index, fragment)| {
if clearance_crossed_fragmentainer {
// This definite box has no block-size contribution, but
// its normal-flow source crossed float continuation
// fragmentainers before its overflowing descendants
// painted. Retain that source assignment: projecting
// from the pre-clear snapshot would replay the descendant
// in the original column.
// <https://www.w3.org/TR/CSS22/visuren.html#flow-control>
// <https://drafts.csswg.org/css-break/#parallel-flows>
vec![(page_index, fragment)]
} else if probe.is_vertical_root() {
vertical_root_continuous_fragmentainer_paint_slices(
&snapshot,
fragment,
self.principal_flow,
)
} else {
continuous_fragmentainer_paint_slices(&snapshot, fragment)
}
})
.collect::<Vec<_>>();
let side_effects = self.deferred_layout_side_effects_since(&snapshot);
let counter_set = self.counter_set.clone();
let quote_depth = self.quote_depth;
let next_assignment_id = self.next_assignment_id;
let next_paint_source_order = self.next_paint_source_order;
self.restore(snapshot);
self.counter_set = counter_set;
self.quote_depth = quote_depth;
self.next_assignment_id = next_assignment_id;
self.next_paint_source_order = next_paint_source_order;
self.apply_deferred_layout_side_effects(side_effects);
for (page_index, fragment) in fragments {
if page_index == self.pages.len() {
self.current_page
.append_paint_fragment_owned(fragment, PaintTranslation::identity());
self.mark_current_page_flow_content();
} else {
self.pending_paint_fragments.push(PendingPaintFragment {
page_index,
fragment,
kind: PendingPaintFragmentKind::InFlowOverflow,
});
}
}
self.apply_forced_break_before_box_in(fragmentainer_kind, style);
let starts_at_page_top = self.cursor_is_at_page_top() && self.truncate_page_start_margins;
self.cursor_y -=
page_start_margin(layout_pt(style.margin.top), starts_at_page_top).points();
let definite_content_height = probe.content_height().points();
let border_box_height = definite_content_height + vertical_non_content.points();
let block_top = self.cursor_y;
if let Some(block_size) = probe.vertical_root_block_size {
let fragmentation = self
.consume_vertical_root_page_block_size(
block_size,
PageTopBlockPosition::new(block_top),
)
.expect("a qualifying vertical root block must have a page continuation");
debug_assert!(fragmentation.fragments.len() > 1);
} else {
self.consume_definite_block_size_through_fragmentainers(block_top, border_box_height);
}
self.cursor_y -= style.margin.bottom;
self.last_block_layout_outcome = BlockLayoutOutcome {
consumed_bottom_margin: layout_pt(style.margin.bottom),
// This deferred replay has already consumed the principal box's
// normal-flow placement above. Its synthetic continuation does
// not resolve `clear`, so it cannot introduce a new clearance
// margin-collapse boundary.
margin_collapse_boundary: BlockMarginCollapseBoundary::Adjoining,
physical_border_box_inline_span: border_box_pt(0.0),
static_border_box: None,
clamp_line_slots: 0,
has_local_continuation_cutoff: false,
in_flow_child_fragment_end: None,
};
self.apply_forced_break_after_box_in(fragmentainer_kind, style);
}
}
#[cfg(test)]
mod tests {
use super::*;
fn definite_content_height(value: f32) -> DefinitePhysicalContentHeight {
DefinitePhysicalContentHeight::new(PhysicalContentHeight::new(content_box_pt(value)))
}
fn length(value: f32) -> css::ComputedLengthPercentageOrAuto {
css::ComputedLengthPercentageOrAuto::LengthPercentage(
css::ComputedLengthPercentage::from_points(value),
)
}
fn replay_length(value: PostLayoutHeightReplay) -> f32 {
match value.as_used_height() {
css::ComputedLengthPercentageOrAuto::LengthPercentage(value) => value.length_points(),
_ => unreachable!("replay height is always a fixed length"),
}
}
#[test]
fn deferred_descendant_overflow_probe_requires_definite_scrollable_candidate() {
let height = definite_content_height(24.0);
assert!(
DeferredDescendantOverflowProbe::new(
None,
true,
0,
DescendantOverflowContribution::Scrollable,
None,
)
.is_none()
);
assert!(
DeferredDescendantOverflowProbe::new(
Some(height),
false,
0,
DescendantOverflowContribution::Scrollable,
None,
)
.is_none()
);
assert!(
DeferredDescendantOverflowProbe::new(
Some(height),
true,
1,
DescendantOverflowContribution::Scrollable,
None,
)
.is_none()
);
assert!(
DeferredDescendantOverflowProbe::new(
Some(height),
true,
0,
DescendantOverflowContribution::InkOnly,
None,
)
.is_none()
);
let probe = DeferredDescendantOverflowProbe::new(
Some(height),
true,
0,
DescendantOverflowContribution::Scrollable,
Some(layout_pt(40.0)),
)
.expect("eligible definite-height block retains its overflow probe");
assert_eq!(
probe.content_height(),
PhysicalContentHeight::new(content_box_pt(24.0))
);
assert!(probe.is_vertical_root());
}
#[test]
fn post_layout_height_replay_preserves_border_box_min_height() {
let mut style = ComputedStyle::initial();
style.box_sizing = BoxSizing::BorderBox;
*style.box_values.height = length(10.0);
style.box_values.min_height = length(25.0);
let vertical_non_content = non_content_pt(15.0);
let replay = post_layout_height_replay_constraint(
&style,
PercentageBasis::indefinite(),
vertical_non_content,
content_box_pt(0.0),
)
.expect("the minimum height wins");
assert_eq!(replay_length(replay), 25.0);
assert_eq!(
replay.content_box_length(vertical_non_content),
content_box_pt(10.0)
);
}
#[test]
fn post_layout_height_replay_preserves_border_box_max_height() {
let mut style = ComputedStyle::initial();
style.box_sizing = BoxSizing::BorderBox;
*style.box_values.height = length(100.0);
style.box_values.max_height = length(25.0);
let vertical_non_content = non_content_pt(15.0);
let replay = post_layout_height_replay_constraint(
&style,
PercentageBasis::indefinite(),
vertical_non_content,
content_box_pt(85.0),
)
.expect("the maximum height wins");
assert_eq!(replay_length(replay), 25.0);
assert_eq!(
replay.content_box_length(vertical_non_content),
content_box_pt(10.0)
);
}
#[test]
fn post_layout_height_replay_keeps_content_box_constraints_unmodified() {
let mut style = ComputedStyle::initial();
style.box_values.max_height = length(25.0);
let replay = post_layout_height_replay_constraint(
&style,
PercentageBasis::indefinite(),
non_content_pt(15.0),
content_box_pt(100.0),
)
.expect("the maximum height wins");
assert_eq!(
replay,
PostLayoutHeightReplay::ContentBox(content_box_pt(25.0))
);
assert_eq!(replay_length(replay), 25.0);
}
#[test]
fn post_layout_height_replay_applies_maximum_before_minimum() {
let mut style = ComputedStyle::initial();
style.box_values.min_height = length(50.0);
style.box_values.max_height = length(25.0);
let replay = post_layout_height_replay_constraint(
&style,
PercentageBasis::indefinite(),
non_content_pt(0.0),
content_box_pt(100.0),
)
.expect("the minimum wins after the maximum cap");
assert_eq!(
replay,
PostLayoutHeightReplay::ContentBox(content_box_pt(50.0))
);
}
}
/// Whether a descendant contributes a forced boundary to this fragmented
/// flow.
///
/// A definite principal box can defer ordinary visible overflow without
/// moving its normal-flow end, but a forced descendant break establishes a
/// real boundary in the descendant's parallel fragmented flow and must remain
/// materialized by the regular fragmentation algorithm.
/// <https://www.w3.org/TR/css-break-3/#forced-breaks>
fn formatting_boxes_have_forced_break_in(
boxes: Option<&[box_tree::FormattingBox<'_>]>,
fragmentainer_kind: FragmentainerKind,
) -> bool {
boxes.is_some_and(|boxes| {
boxes.iter().any(|box_| match box_ {
box_tree::FormattingBox::AnonymousBlock(anonymous) => {
fragmentainer_kind.is_forced_break(anonymous.style.break_before)
|| fragmentainer_kind.is_forced_break(anonymous.style.break_after)
|| formatting_boxes_have_forced_break_in(
Some(&anonymous.children),
fragmentainer_kind,
)
}
box_tree::FormattingBox::InlineSplitBlockContext(context) => {
formatting_boxes_have_forced_break_in(
Some(&context.core.children),
fragmentainer_kind,
)
}
_ => box_.element_parts().is_some_and(|(_, _, style, children)| {
style_is_in_normal_flow(style)
&& (fragmentainer_kind.is_forced_break(style.break_before)
|| fragmentainer_kind.is_forced_break(style.break_after)
|| formatting_boxes_have_forced_break_in(
Some(children),
fragmentainer_kind,
))
}),
})
})
}
#[derive(Debug, Clone, Copy)]
struct PromotedSpannerPaintSlice {
page_index: usize,
top: f32,
bottom: f32,
owns_block_start: bool,
owns_block_end: bool,
}
impl PromotedSpannerPaintSlice {
fn decoration(self, decoration_break: css::BoxDecorationBreak) -> FragmentDecoration {
FragmentDecoration::for_box_decoration_break(
decoration_break,
self.owns_block_start,
self.owns_block_end,
)
}
fn principal_box_fragment(
self,
border_box: PaintClip,
decoration: FragmentDecoration,
) -> CommittedContainerFragment<()> {
CommittedContainerFragment::principal(
FragmentainerOrdinal::new(self.page_index),
(),
border_box,
decoration,
)
}
}
/// Map the continuous source background-positioning area into one physical
/// page/column fragmentainer.
///
/// With `box-decoration-break: slice`, CSS Backgrounds positions a background
/// once for the unfragmented box, then CSS Fragmentation clips that result in
/// each fragment. The layout engine stores page paint in page-local physical
/// coordinates, so every continuation needs the accumulated earlier
/// block-span translation before resolving `background-position` and
/// `background-size`. The fragment border rectangle remains the clip area.
/// Logical block progression has already been projected to this physical
/// fragmentainer boundary; keeping that projection here prevents a vertical
/// writing-mode caller from independently reinterpreting the offset.
///
/// <https://www.w3.org/TR/css-backgrounds-3/#background-position>
/// <https://www.w3.org/TR/css-backgrounds-3/#box-decoration-break>
/// <https://www.w3.org/TR/css-break-3/#fragmentation-model>
/// <https://www.w3.org/TR/css-writing-modes-4/#logical-to-physical>
fn fragmented_slice_background_positioning_border_rect(
slice: &PromotedSpannerPaintSlice,
slices: &[PromotedSpannerPaintSlice],
source_border_rect: PaintRect,
fragment_border_rect: PaintRect,
decoration_break: css::BoxDecorationBreak,
) -> PaintRect {
let preceding_block_span = slices
.iter()
.take_while(|candidate| candidate.page_index < slice.page_index)
.map(|candidate| (candidate.top - candidate.bottom).max(0.0))
.sum::<f32>();
FragmentedDecorationSlice::new(
source_border_rect,
fragment_border_rect,
PaintTranslation::new(0.0, preceding_block_span),
slice.owns_block_start,
slice.owns_block_end,
)
.positioning_border_rect(decoration_break)
}
/// Slice continuous paint from one source coordinate space into its outer
/// fragmentainers.
///
/// Overflow is laid out in one unbounded source coordinate space, then clipped
/// and translated into the current remainder and full continuation
/// fragmentainers. This is the paint counterpart of the independently tracked
/// normal-flow block size. Definite spanners and oversized atomic line boxes
/// use this same projection so paint and flow consume identical fragmentainer
/// distances.
/// <https://www.w3.org/TR/css-break-3/#fragmentation-model>
pub(in crate::layout) fn continuous_fragmentainer_paint_slices(
snapshot: &LayoutSnapshot,
fragment: PaintFragment,
) -> Vec<(usize, PaintFragment)> {
let Some(bounds) = fragment.bounds() else {
return Vec::new();
};
let first_context = snapshot.current_page_context();
let continuation_context = snapshot
.fragmentainer_override()
.map(|override_| override_.continuation_context())
.unwrap_or(first_context);
let source_bottom = bounds.y();
let mut source_top = snapshot.cursor_y().max(source_bottom);
let mut page_index = snapshot.page_count();
let mut first_slice = true;
let mut slices = Vec::new();
while source_top > source_bottom + 0.01 {
let context = if first_slice {
first_context
} else {
continuation_context
};
let target_top = if first_slice {
snapshot.cursor_y()
} else {
context.top()
};
let capacity = if first_slice {
(snapshot.cursor_y() - context.bottom()).max(0.0)
} else {
context.area_height().max(0.0)
};
if capacity <= 0.01 {
page_index += 1;
first_slice = false;
continue;
}
let slice_height = (source_top - source_bottom).min(capacity);
let source_clip = PageTopRect::new(
context.left(),
source_top,
context.area_width(),
slice_height,
)
.paint_clip();
let slice = fragment.clone().clipped_to_rect(source_clip);
if !slice.is_empty() {
slices.push((
page_index,
slice.translated(PaintTranslation::new(0.0, target_top - source_top)),
));
}
source_top -= slice_height;
page_index += 1;
first_slice = false;
}
slices
}
/// Slice deferred fixed-box overflow through the root's vertical page flow.
///
/// The captured source remains a continuous physical paint canvas, but CSS
/// Fragmentation connects page content areas in the root element's block-flow
/// direction. A vertical root therefore slices physical X while preserving
/// the source's physical Y (logical inline) overflow. Later slices restart
/// at a fresh page area, which is the zero-decoration overflow origin defined
/// for paginated fixed-box overflow.
/// <https://www.w3.org/TR/css-break-3/#parallel-flows>
/// <https://www.w3.org/TR/css-break-3/#transforms>
/// <https://www.w3.org/TR/css-writing-modes-4/#block-flow>
fn vertical_root_continuous_fragmentainer_paint_slices(
snapshot: &LayoutSnapshot,
fragment: PaintFragment,
principal_flow: DocumentPrincipalFlow,
) -> Vec<(usize, PaintFragment)> {
let Some(bounds) = fragment.bounds() else {
return Vec::new();
};
let axes = FlowAxes::new(principal_flow.writing_mode, principal_flow.used_direction());
debug_assert!(
WritingModeAxes::new(principal_flow.writing_mode, principal_flow.used_direction(),)
.swaps_physical_axes()
);
let source_origin = PageTopPoint::new(bounds.x(), bounds.y() + bounds.height());
let source_extent = LogicalSize {
inline: bounds.height(),
block: bounds.width(),
};
let first_context = snapshot.current_page_context();
let continuation_context = snapshot
.fragmentainer_override()
.map(|override_| override_.continuation_context())
.unwrap_or(first_context);
let capacity = first_context.logical_block_size(principal_flow.writing_mode);
let ranges = root_page_block_slices(layout_pt(source_extent.block), layout_pt(capacity));
let mut slices = Vec::new();
for (slice_index, range) in ranges.into_iter().enumerate() {
let first_slice = slice_index == 0;
let context = if first_slice {
first_context
} else {
continuation_context
};
let destination_origin = if first_slice {
// The source box retains its actual first-fragment inline origin.
// Continuations instead begin at the empty overflow box at the
// destination fragmentainer's inline start.
PageTopPoint::new(context.left(), source_origin.top_y())
} else {
PageTopPoint::new(context.left(), context.top())
};
let projection = FragmentainerProjection::new(FragmentainerProjectionInput {
source_axes: axes,
source_origin,
source_extent,
source_slice: LogicalRect {
origin: LogicalPoint {
inline: 0.0,
block: range.source_block_start.points(),
},
size: LogicalSize {
inline: source_extent.inline,
block: range.block_size.points(),
},
},
destination_axes: axes,
destination_origin,
destination_extent: LogicalSize {
inline: source_extent.inline,
block: capacity,
},
destination_slice: LogicalRect {
origin: LogicalPoint {
inline: 0.0,
block: 0.0,
},
size: LogicalSize {
inline: source_extent.inline,
block: range.block_size.points(),
},
},
destination_page_area: PageTopRect::new(
context.left(),
context.top(),
context.area_width(),
context.area_height(),
),
});
let slice = fragment
.clone()
.with_primitives_clipped_to_physical_axis_range_preserving_cross_axis_overflow(
css::PhysicalAxis::Horizontal,
projection.source_clip(),
true,
)
.translated(projection.destination_translation());
if !slice.is_empty() {
slices.push((snapshot.page_count() + slice_index, slice));
}
}
slices
}
fn promoted_spanner_paint_slices(
first_page_index: usize,
last_page_index: usize,
block_top: f32,
block_bottom: f32,
first_context: PageContext,
last_context: PageContext,
fragmentainer_override: Option<FragmentainerOverride>,
) -> Vec<PromotedSpannerPaintSlice> {
let continuation_context = fragmentainer_override
.map(|override_| override_.continuation_context())
.unwrap_or(last_context);
(first_page_index..=last_page_index)
.filter_map(|page_index| {
let context = if page_index == first_page_index {
first_context
} else if page_index == last_page_index {
last_context
} else {
continuation_context
};
let top = if page_index == first_page_index {
block_top
} else {
context.top()
};
let bottom = if page_index == last_page_index {
block_bottom
} else {
context.bottom()
};
(top > bottom + 0.01).then_some(PromotedSpannerPaintSlice {
page_index,
top,
bottom,
owns_block_start: page_index == first_page_index,
owns_block_end: page_index == last_page_index,
})
})
.collect()
}
pub(in crate::layout) fn suppress_fragmented_box_edges(
style: &mut ComputedStyle,
owns_block_start: bool,
owns_block_end: bool,
) {
if style.box_decoration_break == css::BoxDecorationBreak::Clone {
return;
}
if !owns_block_start {
suppress_promoted_spanner_physical_edge(style, block_start_side(style.writing_mode));
}
if !owns_block_end {
suppress_promoted_spanner_physical_edge(style, block_end_side(style.writing_mode));
}
}
/// Remove the box background that CSS Backgrounds propagates to the document
/// canvas while retaining borders and other fragment-local decoration.
///
/// A propagated root/body background is painted once in the canvas coordinate
/// system; cloning it into every page fragment would re-anchor image layers:
/// <https://www.w3.org/TR/css-backgrounds-3/#special-backgrounds>.
fn suppress_document_canvas_background(style: &mut ComputedStyle) {
style.background.background_color = css::BackgroundColor::TRANSPARENT;
style.background.background_image = css::ComputedImage::None;
style.background.background_layers.clear();
}
fn suppress_promoted_spanner_physical_edge(style: &mut ComputedStyle, side: PhysicalSide) {
let zero = css::ComputedLengthPercentage::ZERO;
match side {
PhysicalSide::Top => {
style.padding.top = 0.0;
style.border_widths.top = 0.0;
style.box_values.padding.top = zero.clone();
style.border_width_values.top = zero;
style.border_styles.top = css::BorderStyle::None;
}
PhysicalSide::Right => {
style.padding.right = 0.0;
style.border_widths.right = 0.0;
style.box_values.padding.right = zero.clone();
style.border_width_values.right = zero;
style.border_styles.right = css::BorderStyle::None;
}
PhysicalSide::Bottom => {
style.padding.bottom = 0.0;
style.border_widths.bottom = 0.0;
style.box_values.padding.bottom = zero.clone();
style.border_width_values.bottom = zero;
style.border_styles.bottom = css::BorderStyle::None;
}
PhysicalSide::Left => {
style.padding.left = 0.0;
style.border_widths.left = 0.0;
style.box_values.padding.left = zero.clone();
style.border_width_values.left = zero;
style.border_styles.left = css::BorderStyle::None;
}
}
}