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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

//! Layout for CSS block-level elements.
//!
//! As a terminology note, the term *absolute positioning* here refers to elements with position
//! `absolute` or `fixed`. The term *positioned element* refers to elements with position
//! `relative`, `absolute`, and `fixed`. The term *containing block* (occasionally abbreviated as
//! *CB*) is the containing block for the current flow, which differs from the static containing
//! block if the flow is absolutely-positioned.
//!
//! "CSS 2.1" or "CSS 2.2" refers to the editor's draft of the W3C "Cascading Style Sheets Level 2
//! Revision 2 (CSS 2.2) Specification" available here:
//!
//!   <http://dev.w3.org/csswg/css2/>
//!
//! "INTRINSIC" refers to L. David Baron's "More Precise Definitions of Inline Layout and Table
//! Layout" available here:
//!
//!   <http://dbaron.org/css/intrinsic/>
//!
//! "CSS-SIZING" refers to the W3C "CSS Intrinsic & Extrinsic Sizing Module Level 3" document
//! available here:
//!
//!   <http://dev.w3.org/csswg/css-sizing/>

use std::cmp::{max, min};
use std::fmt;
use std::sync::Arc;

use app_units::{Au, MAX_AU};
use base::print_tree::PrintTree;
use bitflags::bitflags;
use euclid::default::{Point2D, Rect, SideOffsets2D, Size2D};
use log::{debug, trace};
use serde::{Serialize, Serializer};
use servo_geometry::MaxRect;
use style::computed_values::box_sizing::T as BoxSizing;
use style::computed_values::display::T as Display;
use style::computed_values::float::T as Float;
use style::computed_values::overflow_x::T as StyleOverflow;
use style::computed_values::position::T as Position;
use style::computed_values::text_align::T as TextAlign;
use style::context::SharedStyleContext;
use style::logical_geometry::{LogicalMargin, LogicalPoint, LogicalRect, LogicalSize, WritingMode};
use style::properties::ComputedValues;
use style::servo::restyle_damage::ServoRestyleDamage;
use style::values::computed::{Margin, MaxSize, Size};

use crate::context::LayoutContext;
use crate::display_list::items::DisplayListSection;
use crate::display_list::{
    BorderPaintingMode, DisplayListBuildState, StackingContextCollectionFlags,
    StackingContextCollectionState,
};
use crate::floats::{ClearType, FloatKind, Floats, PlacementInfo};
use crate::flow::{
    BaseFlow, EarlyAbsolutePositionInfo, Flow, FlowClass, FlowFlags, ForceNonfloatedFlag,
    FragmentationContext, GetBaseFlow, ImmutableFlowUtils, LateAbsolutePositionInfo, OpaqueFlow,
};
use crate::flow_list::FlowList;
use crate::fragment::{
    CoordinateSystem, Fragment, FragmentBorderBoxIterator, FragmentFlags, Overflow,
};
use crate::incremental::RelayoutMode;
use crate::model::{
    AdjoiningMargins, CollapsibleMargins, IntrinsicISizes, MarginCollapseInfo, MaybeAuto,
};
use crate::traversal::PreorderFlowTraversal;
use crate::{layout_debug, layout_debug_scope, sequential};

/// Information specific to floated blocks.
#[derive(Clone, Serialize)]
pub struct FloatedBlockInfo {
    /// The amount of inline size that is available for the float.
    pub containing_inline_size: Au,

    /// The float ceiling, relative to `BaseFlow::position::cur_b` (i.e. the top part of the border
    /// box).
    pub float_ceiling: Au,

    /// Left or right?
    pub float_kind: FloatKind,
}

impl FloatedBlockInfo {
    pub fn new(float_kind: FloatKind) -> FloatedBlockInfo {
        FloatedBlockInfo {
            containing_inline_size: Au(0),
            float_ceiling: Au(0),
            float_kind,
        }
    }
}

/// The solutions for the block-size-and-margins constraint equation.
#[derive(Clone, Copy)]
struct BSizeConstraintSolution {
    block_start: Au,
    block_size: Au,
    margin_block_start: Au,
    margin_block_end: Au,
}

impl BSizeConstraintSolution {
    fn new(
        block_start: Au,
        block_size: Au,
        margin_block_start: Au,
        margin_block_end: Au,
    ) -> BSizeConstraintSolution {
        BSizeConstraintSolution {
            block_start,
            block_size,
            margin_block_start,
            margin_block_end,
        }
    }

    /// Solve the vertical constraint equation for absolute non-replaced elements.
    ///
    /// CSS Section 10.6.4
    /// Constraint equation:
    /// block-start + block-end + block-size + margin-block-start + margin-block-end
    /// = absolute containing block block-size - (vertical padding and border)
    /// [aka available_block-size]
    ///
    /// Return the solution for the equation.
    fn solve_vertical_constraints_abs_nonreplaced(
        block_size: MaybeAuto,
        block_start_margin: MaybeAuto,
        block_end_margin: MaybeAuto,
        block_start: MaybeAuto,
        block_end: MaybeAuto,
        content_block_size: Au,
        available_block_size: Au,
    ) -> BSizeConstraintSolution {
        let (block_start, block_size, margin_block_start, margin_block_end) =
            match (block_start, block_end, block_size) {
                (MaybeAuto::Auto, MaybeAuto::Auto, MaybeAuto::Auto) => {
                    let margin_block_start = block_start_margin.specified_or_zero();
                    let margin_block_end = block_end_margin.specified_or_zero();
                    // Now it is the same situation as block-start Specified and block-end
                    // and block-size Auto.
                    let block_size = content_block_size;
                    // Use a dummy value for `block_start`, since it has the static position.
                    (Au(0), block_size, margin_block_start, margin_block_end)
                },
                (
                    MaybeAuto::Specified(block_start),
                    MaybeAuto::Specified(block_end),
                    MaybeAuto::Specified(block_size),
                ) => {
                    match (block_start_margin, block_end_margin) {
                        (MaybeAuto::Auto, MaybeAuto::Auto) => {
                            let total_margin_val =
                                available_block_size - block_start - block_end - block_size;
                            (
                                block_start,
                                block_size,
                                total_margin_val.scale_by(0.5),
                                total_margin_val.scale_by(0.5),
                            )
                        },
                        (MaybeAuto::Specified(margin_block_start), MaybeAuto::Auto) => {
                            let sum = block_start + block_end + block_size + margin_block_start;
                            (
                                block_start,
                                block_size,
                                margin_block_start,
                                available_block_size - sum,
                            )
                        },
                        (MaybeAuto::Auto, MaybeAuto::Specified(margin_block_end)) => {
                            let sum = block_start + block_end + block_size + margin_block_end;
                            (
                                block_start,
                                block_size,
                                available_block_size - sum,
                                margin_block_end,
                            )
                        },
                        (
                            MaybeAuto::Specified(margin_block_start),
                            MaybeAuto::Specified(margin_block_end),
                        ) => {
                            // Values are over-constrained. Ignore value for 'block-end'.
                            (
                                block_start,
                                block_size,
                                margin_block_start,
                                margin_block_end,
                            )
                        },
                    }
                },

                // For the rest of the cases, auto values for margin are set to 0

                // If only one is Auto, solve for it
                (
                    MaybeAuto::Auto,
                    MaybeAuto::Specified(block_end),
                    MaybeAuto::Specified(block_size),
                ) => {
                    let margin_block_start = block_start_margin.specified_or_zero();
                    let margin_block_end = block_end_margin.specified_or_zero();
                    let sum = block_end + block_size + margin_block_start + margin_block_end;
                    (
                        available_block_size - sum,
                        block_size,
                        margin_block_start,
                        margin_block_end,
                    )
                },
                (
                    MaybeAuto::Specified(block_start),
                    MaybeAuto::Auto,
                    MaybeAuto::Specified(block_size),
                ) => {
                    let margin_block_start = block_start_margin.specified_or_zero();
                    let margin_block_end = block_end_margin.specified_or_zero();
                    (
                        block_start,
                        block_size,
                        margin_block_start,
                        margin_block_end,
                    )
                },
                (
                    MaybeAuto::Specified(block_start),
                    MaybeAuto::Specified(block_end),
                    MaybeAuto::Auto,
                ) => {
                    let margin_block_start = block_start_margin.specified_or_zero();
                    let margin_block_end = block_end_margin.specified_or_zero();
                    let sum = block_start + block_end + margin_block_start + margin_block_end;
                    (
                        block_start,
                        available_block_size - sum,
                        margin_block_start,
                        margin_block_end,
                    )
                },

                // If block-size is auto, then block-size is content block-size. Solve for the
                // non-auto value.
                (MaybeAuto::Specified(block_start), MaybeAuto::Auto, MaybeAuto::Auto) => {
                    let margin_block_start = block_start_margin.specified_or_zero();
                    let margin_block_end = block_end_margin.specified_or_zero();
                    let block_size = content_block_size;
                    (
                        block_start,
                        block_size,
                        margin_block_start,
                        margin_block_end,
                    )
                },
                (MaybeAuto::Auto, MaybeAuto::Specified(block_end), MaybeAuto::Auto) => {
                    let margin_block_start = block_start_margin.specified_or_zero();
                    let margin_block_end = block_end_margin.specified_or_zero();
                    let block_size = content_block_size;
                    let sum = block_end + block_size + margin_block_start + margin_block_end;
                    (
                        available_block_size - sum,
                        block_size,
                        margin_block_start,
                        margin_block_end,
                    )
                },

                (MaybeAuto::Auto, MaybeAuto::Auto, MaybeAuto::Specified(block_size)) => {
                    let margin_block_start = block_start_margin.specified_or_zero();
                    let margin_block_end = block_end_margin.specified_or_zero();
                    // Use a dummy value for `block_start`, since it has the static position.
                    (Au(0), block_size, margin_block_start, margin_block_end)
                },
            };

        BSizeConstraintSolution::new(
            block_start,
            block_size,
            margin_block_start,
            margin_block_end,
        )
    }

    /// Solve the vertical constraint equation for absolute replaced elements.
    ///
    /// Assumption: The used value for block-size has already been calculated.
    ///
    /// CSS Section 10.6.5
    /// Constraint equation:
    /// block-start + block-end + block-size + margin-block-start + margin-block-end
    /// = absolute containing block block-size - (vertical padding and border)
    /// [aka available block-size]
    ///
    /// Return the solution for the equation.
    fn solve_vertical_constraints_abs_replaced(
        block_size: Au,
        block_start_margin: MaybeAuto,
        block_end_margin: MaybeAuto,
        block_start: MaybeAuto,
        block_end: MaybeAuto,
        _: Au,
        available_block_size: Au,
    ) -> BSizeConstraintSolution {
        let (block_start, block_size, margin_block_start, margin_block_end) =
            match (block_start, block_end) {
                (MaybeAuto::Auto, MaybeAuto::Auto) => {
                    let margin_block_start = block_start_margin.specified_or_zero();
                    let margin_block_end = block_end_margin.specified_or_zero();
                    // Use a dummy value for `block_start`, since it has the static position.
                    (Au(0), block_size, margin_block_start, margin_block_end)
                },
                (MaybeAuto::Specified(block_start), MaybeAuto::Specified(block_end)) => {
                    match (block_start_margin, block_end_margin) {
                        (MaybeAuto::Auto, MaybeAuto::Auto) => {
                            let total_margin_val =
                                available_block_size - block_start - block_end - block_size;
                            (
                                block_start,
                                block_size,
                                total_margin_val.scale_by(0.5),
                                total_margin_val.scale_by(0.5),
                            )
                        },
                        (MaybeAuto::Specified(margin_block_start), MaybeAuto::Auto) => {
                            let sum = block_start + block_end + block_size + margin_block_start;
                            (
                                block_start,
                                block_size,
                                margin_block_start,
                                available_block_size - sum,
                            )
                        },
                        (MaybeAuto::Auto, MaybeAuto::Specified(margin_block_end)) => {
                            let sum = block_start + block_end + block_size + margin_block_end;
                            (
                                block_start,
                                block_size,
                                available_block_size - sum,
                                margin_block_end,
                            )
                        },
                        (
                            MaybeAuto::Specified(margin_block_start),
                            MaybeAuto::Specified(margin_block_end),
                        ) => {
                            // Values are over-constrained. Ignore value for 'block-end'.
                            (
                                block_start,
                                block_size,
                                margin_block_start,
                                margin_block_end,
                            )
                        },
                    }
                },

                // If only one is Auto, solve for it
                (MaybeAuto::Auto, MaybeAuto::Specified(block_end)) => {
                    let margin_block_start = block_start_margin.specified_or_zero();
                    let margin_block_end = block_end_margin.specified_or_zero();
                    let sum = block_end + block_size + margin_block_start + margin_block_end;
                    (
                        available_block_size - sum,
                        block_size,
                        margin_block_start,
                        margin_block_end,
                    )
                },
                (MaybeAuto::Specified(block_start), MaybeAuto::Auto) => {
                    let margin_block_start = block_start_margin.specified_or_zero();
                    let margin_block_end = block_end_margin.specified_or_zero();
                    (
                        block_start,
                        block_size,
                        margin_block_start,
                        margin_block_end,
                    )
                },
            };
        BSizeConstraintSolution::new(
            block_start,
            block_size,
            margin_block_start,
            margin_block_end,
        )
    }
}

/// Performs block-size calculations potentially multiple times, taking
/// (assuming an horizontal writing mode) `height`, `min-height`, and `max-height`
/// into account. After each call to `next()`, the caller must call `.try()` with the
/// current calculated value of `height`.
///
/// See CSS 2.1 § 10.7.
pub struct CandidateBSizeIterator {
    block_size: MaybeAuto,
    max_block_size: Option<Au>,
    min_block_size: Au,
    pub candidate_value: Au,
    status: CandidateBSizeIteratorStatus,
}

impl CandidateBSizeIterator {
    /// Creates a new candidate block-size iterator. `block_container_block-size` is `None` if the block-size
    /// of the block container has not been determined yet. It will always be `Some` in the case of
    /// absolutely-positioned containing blocks.
    pub fn new(
        fragment: &Fragment,
        block_container_block_size: Option<Au>,
    ) -> CandidateBSizeIterator {
        // Per CSS 2.1 § 10.7, (assuming an horizontal writing mode,)
        // percentages in `min-height` and `max-height` refer to the height of
        // the containing block.
        // If that is not determined yet by the time we need to resolve
        // `min-height` and `max-height`, percentage values are ignored.

        let block_size = MaybeAuto::from_option(
            fragment
                .style
                .content_block_size()
                .maybe_to_used_value(block_container_block_size),
        );

        let max_block_size = fragment
            .style
            .max_block_size()
            .maybe_to_used_value(block_container_block_size);

        let min_block_size = MaybeAuto::from_option(
            fragment
                .style
                .min_block_size()
                .maybe_to_used_value(block_container_block_size),
        )
        .specified_or_zero();

        // If the style includes `box-sizing: border-box`, subtract the border and padding.
        let adjustment_for_box_sizing = match fragment.style.get_position().box_sizing {
            BoxSizing::BorderBox => fragment.border_padding.block_start_end(),
            BoxSizing::ContentBox => Au(0),
        };

        return CandidateBSizeIterator {
            block_size: block_size.map(|size| adjust(size, adjustment_for_box_sizing)),
            max_block_size: max_block_size.map(|size| adjust(size, adjustment_for_box_sizing)),
            min_block_size: adjust(min_block_size, adjustment_for_box_sizing),
            candidate_value: Au(0),
            status: CandidateBSizeIteratorStatus::Initial,
        };

        fn adjust(size: Au, delta: Au) -> Au {
            max(size - delta, Au(0))
        }
    }
}

impl Iterator for CandidateBSizeIterator {
    type Item = MaybeAuto;
    fn next(&mut self) -> Option<MaybeAuto> {
        self.status = match self.status {
            CandidateBSizeIteratorStatus::Initial => CandidateBSizeIteratorStatus::Trying,
            CandidateBSizeIteratorStatus::Trying => match self.max_block_size {
                Some(max_block_size) if self.candidate_value > max_block_size => {
                    CandidateBSizeIteratorStatus::TryingMax
                },
                _ if self.candidate_value < self.min_block_size => {
                    CandidateBSizeIteratorStatus::TryingMin
                },
                _ => CandidateBSizeIteratorStatus::Found,
            },
            CandidateBSizeIteratorStatus::TryingMax => {
                if self.candidate_value < self.min_block_size {
                    CandidateBSizeIteratorStatus::TryingMin
                } else {
                    CandidateBSizeIteratorStatus::Found
                }
            },
            CandidateBSizeIteratorStatus::TryingMin | CandidateBSizeIteratorStatus::Found => {
                CandidateBSizeIteratorStatus::Found
            },
        };

        match self.status {
            CandidateBSizeIteratorStatus::Trying => Some(self.block_size),
            CandidateBSizeIteratorStatus::TryingMax => {
                Some(MaybeAuto::Specified(self.max_block_size.unwrap()))
            },
            CandidateBSizeIteratorStatus::TryingMin => {
                Some(MaybeAuto::Specified(self.min_block_size))
            },
            CandidateBSizeIteratorStatus::Found => None,
            CandidateBSizeIteratorStatus::Initial => panic!(),
        }
    }
}

enum CandidateBSizeIteratorStatus {
    Initial,
    Trying,
    TryingMax,
    TryingMin,
    Found,
}

// A helper function used in block-size calculation.
fn translate_including_floats(cur_b: &mut Au, delta: Au, floats: &mut Floats) {
    *cur_b += delta;
    let writing_mode = floats.writing_mode;
    floats.translate(LogicalSize::new(writing_mode, Au(0), -delta));
}

/// The real assign-block-sizes traversal for flows with position 'absolute'.
///
/// This is a traversal of an Absolute Flow tree.
/// - Relatively positioned flows and the Root flow start new Absolute flow trees.
/// - The kids of a flow in this tree will be the flows for which it is the
///   absolute Containing Block.
/// - Thus, leaf nodes and inner non-root nodes are all Absolute Flows.
///
/// A Flow tree can have several Absolute Flow trees (depending on the number
/// of relatively positioned flows it has).
///
/// Note that flows with position 'fixed' just form a flat list as they all
/// have the Root flow as their CB.
pub struct AbsoluteAssignBSizesTraversal<'a>(pub &'a SharedStyleContext<'a>);

impl<'a> PreorderFlowTraversal for AbsoluteAssignBSizesTraversal<'a> {
    #[inline]
    fn process(&self, flow: &mut dyn Flow) {
        if !flow.is_block_like() {
            return;
        }

        // This flow might not be an absolutely positioned flow if it is the root of the tree.
        let block = flow.as_mut_block();
        if !block
            .base
            .flags
            .contains(FlowFlags::IS_ABSOLUTELY_POSITIONED)
        {
            return;
        }

        if !block
            .base
            .restyle_damage
            .intersects(ServoRestyleDamage::REFLOW_OUT_OF_FLOW | ServoRestyleDamage::REFLOW)
        {
            return;
        }

        block.calculate_absolute_block_size_and_margins(self.0);
    }
}

pub enum BlockType {
    Replaced,
    NonReplaced,
    AbsoluteReplaced,
    AbsoluteNonReplaced,
    FloatReplaced,
    FloatNonReplaced,
    InlineBlockReplaced,
    InlineBlockNonReplaced,
    InlineFlexItem,
}

#[derive(Clone, PartialEq)]
pub enum MarginsMayCollapseFlag {
    MarginsMayCollapse,
    MarginsMayNotCollapse,
}

#[derive(Debug, PartialEq)]
pub enum FormattingContextType {
    None,
    Block,
    Other,
}

#[allow(unsafe_code)]
unsafe impl crate::flow::HasBaseFlow for BlockFlow {}

// A block formatting context.
#[derive(Serialize)]
#[repr(C)]
pub struct BlockFlow {
    /// Data common to all flows.
    pub base: BaseFlow,

    /// The associated fragment.
    pub fragment: Fragment,

    /// Additional floating flow members.
    pub float: Option<Box<FloatedBlockInfo>>,

    /// Various flags.
    flags: BlockFlowFlags,
}

bitflags! {
    #[derive(Clone, Copy)]
    struct BlockFlowFlags: u8 {
        /// If this is set, then this block flow is the root flow.
        const IS_ROOT = 0b0000_0001;
        /// If this is set, then this block flow has overflow and it will scroll.
        const HAS_SCROLLING_OVERFLOW = 0b0000_0010;
    }
}

impl Serialize for BlockFlowFlags {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        self.bits().serialize(serializer)
    }
}

impl BlockFlow {
    pub fn from_fragment(fragment: Fragment) -> BlockFlow {
        BlockFlow::from_fragment_and_float_kind(fragment, None)
    }

    pub fn from_fragment_and_float_kind(
        fragment: Fragment,
        float_kind: Option<FloatKind>,
    ) -> BlockFlow {
        let writing_mode = fragment.style().writing_mode;
        BlockFlow {
            base: BaseFlow::new(
                Some(fragment.style()),
                writing_mode,
                match float_kind {
                    Some(_) => ForceNonfloatedFlag::FloatIfNecessary,
                    None => ForceNonfloatedFlag::ForceNonfloated,
                },
            ),
            fragment,
            float: float_kind.map(|kind| Box::new(FloatedBlockInfo::new(kind))),
            flags: BlockFlowFlags::empty(),
        }
    }

    /// Return the type of this block.
    ///
    /// This determines the algorithm used to calculate inline-size, block-size, and the
    /// relevant margins for this Block.
    pub fn block_type(&self) -> BlockType {
        if self
            .base
            .flags
            .contains(FlowFlags::IS_ABSOLUTELY_POSITIONED)
        {
            if self.fragment.is_replaced() {
                BlockType::AbsoluteReplaced
            } else {
                BlockType::AbsoluteNonReplaced
            }
        } else if self.is_inline_flex_item() {
            BlockType::InlineFlexItem
        } else if self.base.flags.is_float() {
            if self.fragment.is_replaced() {
                BlockType::FloatReplaced
            } else {
                BlockType::FloatNonReplaced
            }
        } else if self.is_inline_block_or_inline_flex() {
            if self.fragment.is_replaced() {
                BlockType::InlineBlockReplaced
            } else {
                BlockType::InlineBlockNonReplaced
            }
        } else if self.fragment.is_replaced() {
            BlockType::Replaced
        } else {
            BlockType::NonReplaced
        }
    }

    /// Compute the actual inline size and position for this block.
    pub fn compute_used_inline_size(
        &mut self,
        shared_context: &SharedStyleContext,
        containing_block_inline_size: Au,
    ) {
        let block_type = self.block_type();
        match block_type {
            BlockType::AbsoluteReplaced => {
                let inline_size_computer = AbsoluteReplaced;
                inline_size_computer.compute_used_inline_size(
                    self,
                    shared_context,
                    containing_block_inline_size,
                );
            },
            BlockType::AbsoluteNonReplaced => {
                let inline_size_computer = AbsoluteNonReplaced;
                inline_size_computer.compute_used_inline_size(
                    self,
                    shared_context,
                    containing_block_inline_size,
                );
            },
            BlockType::FloatReplaced => {
                let inline_size_computer = FloatReplaced;
                inline_size_computer.compute_used_inline_size(
                    self,
                    shared_context,
                    containing_block_inline_size,
                );
            },
            BlockType::FloatNonReplaced => {
                let inline_size_computer = FloatNonReplaced;
                inline_size_computer.compute_used_inline_size(
                    self,
                    shared_context,
                    containing_block_inline_size,
                );
            },
            BlockType::InlineBlockReplaced => {
                let inline_size_computer = InlineBlockReplaced;
                inline_size_computer.compute_used_inline_size(
                    self,
                    shared_context,
                    containing_block_inline_size,
                );
            },
            BlockType::InlineBlockNonReplaced => {
                let inline_size_computer = InlineBlockNonReplaced;
                inline_size_computer.compute_used_inline_size(
                    self,
                    shared_context,
                    containing_block_inline_size,
                );
            },
            BlockType::Replaced => {
                let inline_size_computer = BlockReplaced;
                inline_size_computer.compute_used_inline_size(
                    self,
                    shared_context,
                    containing_block_inline_size,
                );
            },
            BlockType::NonReplaced => {
                let inline_size_computer = BlockNonReplaced;
                inline_size_computer.compute_used_inline_size(
                    self,
                    shared_context,
                    containing_block_inline_size,
                );
            },
            BlockType::InlineFlexItem => {
                let inline_size_computer = InlineFlexItem;
                inline_size_computer.compute_used_inline_size(
                    self,
                    shared_context,
                    containing_block_inline_size,
                );
            },
        }
    }

    /// Return this flow's fragment.
    pub fn fragment(&mut self) -> &mut Fragment {
        &mut self.fragment
    }

    pub fn stacking_relative_border_box(&self, coor: CoordinateSystem) -> Rect<Au> {
        self.fragment.stacking_relative_border_box(
            &self.base.stacking_relative_position,
            &self
                .base
                .early_absolute_position_info
                .relative_containing_block_size,
            self.base
                .early_absolute_position_info
                .relative_containing_block_mode,
            coor,
        )
    }

    /// Return the size of the containing block for the given immediate absolute descendant of this
    /// flow.
    ///
    /// Right now, this only gets the containing block size for absolutely positioned elements.
    /// Note: We assume this is called in a top-down traversal, so it is ok to reference the CB.
    #[inline]
    pub fn containing_block_size(
        &self,
        viewport_size: &Size2D<Au>,
        descendant: OpaqueFlow,
    ) -> LogicalSize<Au> {
        debug_assert!(self
            .base
            .flags
            .contains(FlowFlags::IS_ABSOLUTELY_POSITIONED));
        if self.is_fixed() || self.is_root() {
            // Initial containing block is the CB for the root
            LogicalSize::from_physical(self.base.writing_mode, *viewport_size)
        } else {
            self.base
                .absolute_cb
                .generated_containing_block_size(descendant)
        }
    }

    /// Return shrink-to-fit inline-size.
    ///
    /// This is where we use the preferred inline-sizes and minimum inline-sizes
    /// calculated in the bubble-inline-sizes traversal.
    pub fn get_shrink_to_fit_inline_size(&self, available_inline_size: Au) -> Au {
        let content_intrinsic_inline_sizes = self.content_intrinsic_inline_sizes();
        min(
            content_intrinsic_inline_sizes.preferred_inline_size,
            max(
                content_intrinsic_inline_sizes.minimum_inline_size,
                available_inline_size,
            ),
        )
    }

    /// If this is the root flow, shifts all kids down and adjusts our size to account for
    /// root flow margins, which should never be collapsed according to CSS § 8.3.1.
    ///
    /// TODO(#2017, pcwalton): This is somewhat inefficient (traverses kids twice); can we do
    /// better?
    fn adjust_fragments_for_collapsed_margins_if_root(
        &mut self,
        shared_context: &SharedStyleContext,
    ) {
        if !self.is_root() {
            return;
        }

        let (block_start_margin_value, block_end_margin_value) = match self.base.collapsible_margins
        {
            CollapsibleMargins::CollapseThrough(_) => {
                panic!("Margins unexpectedly collapsed through root flow.")
            },
            CollapsibleMargins::Collapse(block_start_margin, block_end_margin) => {
                (block_start_margin.collapse(), block_end_margin.collapse())
            },
            CollapsibleMargins::None(block_start, block_end) => (block_start, block_end),
        };

        // Shift all kids down (or up, if margins are negative) if necessary.
        if block_start_margin_value != Au(0) {
            for kid in self.base.child_iter_mut() {
                let kid_base = kid.mut_base();
                kid_base.position.start.b += block_start_margin_value
            }
        }

        // FIXME(#2003, pcwalton): The max is taken here so that you can scroll the page, but this
        // is not correct behavior according to CSS 2.1 § 10.5. Instead I think we should treat the
        // root element as having `overflow: scroll` and use the layers-based scrolling
        // infrastructure to make it scrollable.
        let viewport_size = LogicalSize::from_physical(
            self.fragment.style.writing_mode,
            shared_context.viewport_size(),
        );
        let block_size = max(
            viewport_size.block,
            self.fragment.border_box.size.block + block_start_margin_value + block_end_margin_value,
        );

        self.base.position.size.block = block_size;
        self.fragment.border_box.size.block = block_size;
    }

    // FIXME: Record enough info to deal with fragmented decorations.
    // See https://drafts.csswg.org/css-break/#break-decoration
    // For borders, this might be `enum FragmentPosition { First, Middle, Last }`
    fn clone_with_children(&self, new_children: FlowList) -> BlockFlow {
        BlockFlow {
            base: self.base.clone_with_children(new_children),
            fragment: self.fragment.clone(),
            float: self.float.clone(),
            ..*self
        }
    }

    /// Writes in the size of the relative containing block for children. (This information
    /// is also needed to handle RTL.)
    fn propagate_early_absolute_position_info_to_children(&mut self) {
        for kid in self.base.child_iter_mut() {
            kid.mut_base().early_absolute_position_info = EarlyAbsolutePositionInfo {
                relative_containing_block_size: self.fragment.content_box().size,
                relative_containing_block_mode: self.fragment.style().writing_mode,
            }
        }
    }

    /// Assign block-size for current flow.
    ///
    /// * Collapse margins for flow's children and set in-flow child flows' block offsets now that
    ///   we know their block-sizes.
    /// * Calculate and set the block-size of the current flow.
    /// * Calculate block-size, vertical margins, and block offset for the flow's box using CSS §
    ///   10.6.7.
    ///
    /// For absolute flows, we store the calculated content block-size for the flow. We defer the
    /// calculation of the other values until a later traversal at root flow.
    ///
    /// When `fragmentation_context` is given (not `None`), this should fit as much of the content
    /// as possible within the available block size.
    /// If there is more content (that doesn’t fit), this flow is *fragmented*
    /// with the extra content moved to another fragment (a flow like this one) which is returned.
    /// See `Flow::fragment`.
    ///
    /// The return value is always `None` when `fragmentation_context` is `None`.
    ///
    /// `inline(always)` because this is only ever called by in-order or non-in-order top-level
    /// methods.
    #[inline(always)]
    pub fn assign_block_size_block_base(
        &mut self,
        layout_context: &LayoutContext,
        mut fragmentation_context: Option<FragmentationContext>,
        margins_may_collapse: MarginsMayCollapseFlag,
    ) -> Option<Arc<dyn Flow>> {
        let _scope = layout_debug_scope!("assign_block_size_block_base {:x}", self.base.debug_id());

        let mut break_at = None;
        let content_box = self.fragment.content_box();
        if self
            .base
            .restyle_damage
            .contains(ServoRestyleDamage::REFLOW)
        {
            // Our current border-box position.
            let mut cur_b = Au(0);

            // Absolute positioning establishes a block formatting context. Don't propagate floats
            // in or out. (But do propagate them between kids.)
            if self
                .base
                .flags
                .contains(FlowFlags::IS_ABSOLUTELY_POSITIONED) ||
                margins_may_collapse != MarginsMayCollapseFlag::MarginsMayCollapse
            {
                self.base.floats = Floats::new(self.fragment.style.writing_mode);
            }

            let writing_mode = self.base.floats.writing_mode;
            self.base.floats.translate(LogicalSize::new(
                writing_mode,
                -self.fragment.inline_start_offset(),
                Au(0),
            ));

            // The sum of our block-start border and block-start padding.
            let block_start_offset = self.fragment.border_padding.block_start;
            translate_including_floats(&mut cur_b, block_start_offset, &mut self.base.floats);

            let can_collapse_block_start_margin_with_kids = margins_may_collapse ==
                MarginsMayCollapseFlag::MarginsMayCollapse &&
                !self
                    .base
                    .flags
                    .contains(FlowFlags::IS_ABSOLUTELY_POSITIONED) &&
                self.fragment.border_padding.block_start == Au(0);
            let mut margin_collapse_info = MarginCollapseInfo::initialize_block_start_margin(
                &self.fragment,
                can_collapse_block_start_margin_with_kids,
            );

            // At this point, `cur_b` is at the content edge of our box. Now iterate over children.
            let mut floats = self.base.floats.clone();
            let thread_id = self.base.thread_id;
            let (mut had_floated_children, mut had_children_with_clearance) = (false, false);
            for (child_index, kid) in self.base.child_iter_mut().enumerate() {
                if kid
                    .base()
                    .flags
                    .contains(FlowFlags::IS_ABSOLUTELY_POSITIONED)
                {
                    // Assume that the *hypothetical box* for an absolute flow starts immediately
                    // after the margin-end border edge of the previous flow.
                    if kid
                        .base()
                        .flags
                        .contains(FlowFlags::BLOCK_POSITION_IS_STATIC)
                    {
                        let previous_bottom_margin = margin_collapse_info.current_float_ceiling();

                        kid.mut_base().position.start.b = cur_b +
                            kid.base()
                                .collapsible_margins
                                .block_start_margin_for_noncollapsible_context() +
                            previous_bottom_margin
                    }
                    kid.place_float_if_applicable();
                    if !kid.base().flags.is_float() {
                        kid.assign_block_size_for_inorder_child_if_necessary(
                            layout_context,
                            thread_id,
                            content_box,
                        );
                    }

                    // Skip the collapsing and float processing for absolute flow kids and continue
                    // with the next flow.
                    continue;
                }

                let previous_b = cur_b;
                if let Some(ctx) = fragmentation_context {
                    let child_ctx = FragmentationContext {
                        available_block_size: ctx.available_block_size - cur_b,
                        this_fragment_is_empty: ctx.this_fragment_is_empty,
                    };
                    if let Some(remaining) = kid.fragment(layout_context, Some(child_ctx)) {
                        break_at = Some((child_index + 1, Some(remaining)));
                    }
                }

                // Assign block-size now for the child if it might have floats in and we couldn't
                // before.
                kid.mut_base().floats = floats.clone();
                if kid.base().flags.is_float() {
                    had_floated_children = true;
                    kid.mut_base().position.start.b = cur_b;
                    {
                        let kid_block = kid.as_mut_block();
                        let float_ceiling = margin_collapse_info.current_float_ceiling();
                        kid_block.float.as_mut().unwrap().float_ceiling = float_ceiling
                    }
                    kid.place_float_if_applicable();

                    let kid_base = kid.mut_base();
                    floats = kid_base.floats.clone();
                    continue;
                }

                // If we have clearance, assume there are no floats in.
                //
                // FIXME(#2008, pcwalton): This could be wrong if we have `clear: left` or `clear:
                // right` and there are still floats to impact, of course. But this gets
                // complicated with margin collapse. Possibly the right thing to do is to lay out
                // the block again in this rare case. (Note that WebKit can lay blocks out twice;
                // this may be related, although I haven't looked into it closely.)
                if kid.base().flags.clears_floats() {
                    kid.mut_base().floats = Floats::new(self.fragment.style.writing_mode)
                }

                // Lay the child out if this was an in-order traversal.
                let need_to_process_child_floats = kid
                    .assign_block_size_for_inorder_child_if_necessary(
                        layout_context,
                        thread_id,
                        content_box,
                    );

                if !had_children_with_clearance &&
                    floats.is_present() &&
                    (kid.base().flags.contains(FlowFlags::CLEARS_LEFT) ||
                        kid.base().flags.contains(FlowFlags::CLEARS_RIGHT))
                {
                    had_children_with_clearance = true
                }

                // Handle any (possibly collapsed) top margin.
                let delta = margin_collapse_info.advance_block_start_margin(
                    &kid.base().collapsible_margins,
                    !had_children_with_clearance,
                );
                translate_including_floats(&mut cur_b, delta, &mut floats);

                // Collapse-through margins should be placed at the top edge,
                // so we'll handle the delta after the bottom margin is processed
                if let CollapsibleMargins::CollapseThrough(_) = kid.base().collapsible_margins {
                    cur_b -= delta;
                }

                // Clear past the floats that came in, if necessary.
                let clearance = match (
                    kid.base().flags.contains(FlowFlags::CLEARS_LEFT),
                    kid.base().flags.contains(FlowFlags::CLEARS_RIGHT),
                ) {
                    (false, false) => Au(0),
                    (true, false) => floats.clearance(ClearType::Left),
                    (false, true) => floats.clearance(ClearType::Right),
                    (true, true) => floats.clearance(ClearType::Both),
                };
                translate_including_floats(&mut cur_b, clearance, &mut floats);

                // At this point, `cur_b` is at the border edge of the child.
                kid.mut_base().position.start.b = cur_b;

                // Now pull out the child's outgoing floats. We didn't do this immediately after
                // the `assign_block_size_for_inorder_child_if_necessary` call because clearance on
                // a block operates on the floats that come *in*, not the floats that go *out*.
                if need_to_process_child_floats {
                    floats = kid.mut_base().floats.clone()
                }

                // Move past the child's border box. Do not use the `translate_including_floats`
                // function here because the child has already translated floats past its border
                // box.
                let kid_base = kid.mut_base();
                cur_b += kid_base.position.size.block;

                // Handle any (possibly collapsed) block-end margin.
                let delta =
                    margin_collapse_info.advance_block_end_margin(&kid_base.collapsible_margins);
                translate_including_floats(&mut cur_b, delta, &mut floats);

                // Collapse-through margin should be placed at the top edge of the flow.
                let collapse_delta = match kid_base.collapsible_margins {
                    CollapsibleMargins::CollapseThrough(_) => {
                        let delta = margin_collapse_info.current_float_ceiling();
                        cur_b += delta;
                        kid_base.position.start.b += delta;
                        delta
                    },
                    _ => Au(0),
                };

                if break_at.is_some() {
                    break;
                }

                if let Some(ref mut ctx) = fragmentation_context {
                    if cur_b > ctx.available_block_size && !ctx.this_fragment_is_empty {
                        break_at = Some((child_index, None));
                        cur_b = previous_b;
                        break;
                    }
                    ctx.this_fragment_is_empty = false
                }

                // For consecutive collapse-through flows, their top margin should be calculated
                // from the same baseline.
                cur_b -= collapse_delta;
            }

            // Add in our block-end margin and compute our collapsible margins.
            let can_collapse_block_end_margin_with_kids = margins_may_collapse ==
                MarginsMayCollapseFlag::MarginsMayCollapse &&
                !self
                    .base
                    .flags
                    .contains(FlowFlags::IS_ABSOLUTELY_POSITIONED) &&
                self.fragment.border_padding.block_end == Au(0);
            let (collapsible_margins, delta) = margin_collapse_info
                .finish_and_compute_collapsible_margins(
                    &self.fragment,
                    self.base.block_container_explicit_block_size,
                    can_collapse_block_end_margin_with_kids,
                    !had_floated_children,
                );
            self.base.collapsible_margins = collapsible_margins;
            translate_including_floats(&mut cur_b, delta, &mut floats);

            let mut block_size = cur_b - block_start_offset;
            let is_root = self.is_root();

            if is_root ||
                self.formatting_context_type() != FormattingContextType::None ||
                self.base
                    .flags
                    .contains(FlowFlags::IS_ABSOLUTELY_POSITIONED)
            {
                // The content block-size includes all the floats per CSS 2.1 § 10.6.7. The easiest
                // way to handle this is to just treat it as clearance.
                block_size += floats.clearance(ClearType::Both);
            }

            if self
                .base
                .flags
                .contains(FlowFlags::IS_ABSOLUTELY_POSITIONED)
            {
                // FIXME(#2003, pcwalton): The max is taken here so that you can scroll the page,
                // but this is not correct behavior according to CSS 2.1 § 10.5. Instead I think we
                // should treat the root element as having `overflow: scroll` and use the layers-
                // based scrolling infrastructure to make it scrollable.
                if is_root {
                    let viewport_size = LogicalSize::from_physical(
                        self.fragment.style.writing_mode,
                        layout_context.shared_context().viewport_size(),
                    );
                    block_size = max(viewport_size.block, block_size)
                }

                // Store the content block-size for use in calculating the absolute flow's
                // dimensions later.
                //
                // FIXME(pcwalton): This looks not idempotent. Is it?
                self.fragment.border_box.size.block = block_size;
            }

            if self
                .base
                .flags
                .contains(FlowFlags::IS_ABSOLUTELY_POSITIONED)
            {
                self.propagate_early_absolute_position_info_to_children();
                // Return early until the absolute flow tree traversal at root flow.
                // Assigning block size for absolute flow will happen in `traverse_absolute_flows` below.
                return None;
            }

            // Compute any explicitly-specified block size.
            // Can't use `for` because we assign to `candidate_block_size_iterator.candidate_value`.
            let mut candidate_block_size_iterator = CandidateBSizeIterator::new(
                &self.fragment,
                self.base.block_container_explicit_block_size,
            );
            while let Some(candidate_block_size) = candidate_block_size_iterator.next() {
                candidate_block_size_iterator.candidate_value = match candidate_block_size {
                    MaybeAuto::Auto => block_size,
                    MaybeAuto::Specified(value) => value,
                }
            }

            // Adjust `cur_b` as necessary to account for the explicitly-specified block-size.
            block_size = candidate_block_size_iterator.candidate_value;
            let delta = block_size - (cur_b - block_start_offset);
            translate_including_floats(&mut cur_b, delta, &mut floats);

            // Take border and padding into account.
            let block_end_offset = self.fragment.border_padding.block_end;
            translate_including_floats(&mut cur_b, block_end_offset, &mut floats);

            // Now that `cur_b` is at the block-end of the border box, compute the final border box
            // position.
            self.fragment.border_box.size.block = cur_b;
            self.fragment.border_box.start.b = Au(0);
            self.base.position.size.block = cur_b;

            self.propagate_early_absolute_position_info_to_children();

            // Translate the current set of floats back into the parent coordinate system in the
            // inline direction, and store them in the flow so that flows that come later in the
            // document can access them.
            floats.translate(LogicalSize::new(
                writing_mode,
                self.fragment.inline_start_offset(),
                Au(0),
            ));
            self.base.floats = floats;
            self.adjust_fragments_for_collapsed_margins_if_root(layout_context.shared_context());
        } else {
            // We don't need to reflow, but we still need to perform in-order traversals if
            // necessary.
            let thread_id = self.base.thread_id;
            for kid in self.base.child_iter_mut() {
                kid.assign_block_size_for_inorder_child_if_necessary(
                    layout_context,
                    thread_id,
                    content_box,
                );
            }
        }

        if (&*self as &dyn Flow).contains_roots_of_absolute_flow_tree() {
            // Assign block-sizes for all flows in this absolute flow tree.
            // This is preorder because the block-size of an absolute flow may depend on
            // the block-size of its containing block, which may also be an absolute flow.
            let assign_abs_b_sizes = AbsoluteAssignBSizesTraversal(layout_context.shared_context());
            assign_abs_b_sizes.traverse_absolute_flows(&mut *self);
        }

        // Don't remove the dirty bits yet if we're absolutely-positioned, since our final size
        // has not been calculated yet. (See `calculate_absolute_block_size_and_margins` for that.)
        // Also don't remove the dirty bits if we're a block formatting context since our inline
        // size has not yet been computed. (See `assign_inline_position_for_formatting_context()`.)
        if (self.base.flags.is_float() ||
            self.formatting_context_type() == FormattingContextType::None) &&
            !self
                .base
                .flags
                .contains(FlowFlags::IS_ABSOLUTELY_POSITIONED)
        {
            self.base
                .restyle_damage
                .remove(ServoRestyleDamage::REFLOW_OUT_OF_FLOW | ServoRestyleDamage::REFLOW);
            self.fragment
                .restyle_damage
                .remove(ServoRestyleDamage::REFLOW_OUT_OF_FLOW | ServoRestyleDamage::REFLOW);
        }

        break_at.and_then(|(i, child_remaining)| {
            if i == self.base.children.len() && child_remaining.is_none() {
                None
            } else {
                let mut children = self.base.children.split_off(i);
                if let Some(child) = child_remaining {
                    children.push_front_arc(child);
                }
                Some(Arc::new(self.clone_with_children(children)) as Arc<dyn Flow>)
            }
        })
    }

    /// Add placement information about current float flow for use by the parent.
    ///
    /// Also, use information given by parent about other floats to find out our relative position.
    ///
    /// This does not give any information about any float descendants because they do not affect
    /// elements outside of the subtree rooted at this float.
    ///
    /// This function is called on a kid flow by a parent. Therefore, `assign_block_size_float` was
    /// already called on this kid flow by the traversal function. So, the values used are
    /// well-defined.
    pub fn place_float(&mut self) {
        let block_size = self.fragment.border_box.size.block;
        let clearance = match self.fragment.clear() {
            None => Au(0),
            Some(clear) => self.base.floats.clearance(clear),
        };

        let float_info: FloatedBlockInfo = (**self.float.as_ref().unwrap()).clone();

        // Our `position` field accounts for positive margins, but not negative margins. (See
        // calculation of `extra_inline_size_from_margin` below.) Negative margins must be taken
        // into account for float placement, however. So we add them in here.
        let inline_size_for_float_placement =
            self.base.position.size.inline + min(Au(0), self.fragment.margin.inline_start_end());

        let info = PlacementInfo {
            size: LogicalSize::new(
                self.fragment.style.writing_mode,
                inline_size_for_float_placement,
                block_size + self.fragment.margin.block_start_end(),
            )
            .convert(
                self.fragment.style.writing_mode,
                self.base.floats.writing_mode,
            ),
            ceiling: clearance + float_info.float_ceiling,
            max_inline_size: float_info.containing_inline_size,
            kind: float_info.float_kind,
        };

        // Place the float and return the `Floats` back to the parent flow.
        // After, grab the position and use that to set our position.
        self.base.floats.add_float(&info);

        // FIXME (mbrubeck) Get the correct container size for self.base.floats;
        let container_size = Size2D::new(self.base.block_container_inline_size, Au(0));

        // Move in from the margin edge, as per CSS 2.1 § 9.5, floats may not overlap anything on
        // their margin edges.
        let float_offset = self
            .base
            .floats
            .last_float_pos()
            .unwrap()
            .convert(
                self.base.floats.writing_mode,
                self.base.writing_mode,
                container_size,
            )
            .start;
        let margin_offset = LogicalPoint::new(
            self.base.writing_mode,
            Au(0),
            self.fragment.margin.block_start,
        );

        let mut origin = LogicalPoint::new(
            self.base.writing_mode,
            self.base.position.start.i,
            self.base.position.start.b,
        );
        origin = origin.add_point(&float_offset).add_point(&margin_offset);
        self.base.position =
            LogicalRect::from_point_size(self.base.writing_mode, origin, self.base.position.size);
    }

    pub fn explicit_block_containing_size(
        &self,
        shared_context: &SharedStyleContext,
    ) -> Option<Au> {
        if self.is_root() || self.is_fixed() {
            let viewport_size = LogicalSize::from_physical(
                self.fragment.style.writing_mode,
                shared_context.viewport_size(),
            );
            Some(viewport_size.block)
        } else if self
            .base
            .flags
            .contains(FlowFlags::IS_ABSOLUTELY_POSITIONED) &&
            self.base.block_container_explicit_block_size.is_none()
        {
            self.base
                .absolute_cb
                .explicit_block_containing_size(shared_context)
        } else {
            self.base.block_container_explicit_block_size
        }
    }

    pub fn explicit_block_size(&self, containing_block_size: Option<Au>) -> Option<Au> {
        let content_block_size = self.fragment.style().content_block_size();

        match content_block_size {
            Size::LengthPercentage(ref lp) => lp.maybe_to_used_value(containing_block_size),
            _ => {
                let container_size = containing_block_size?;
                let (block_start, block_end) = {
                    let position = self.fragment.style().logical_position();
                    (
                        MaybeAuto::from_inset(position.block_start, container_size),
                        MaybeAuto::from_inset(position.block_end, container_size),
                    )
                };

                match (block_start, block_end) {
                    (MaybeAuto::Specified(block_start), MaybeAuto::Specified(block_end)) => {
                        let available_block_size =
                            container_size - self.fragment.border_padding.block_start_end();

                        // Non-auto margin-block-start and margin-block-end values have already been
                        // calculated during assign-inline-size.
                        let margin = self.fragment.style().logical_margin();
                        let margin_block_start = match margin.block_start {
                            Margin::Auto => MaybeAuto::Auto,
                            _ => MaybeAuto::Specified(self.fragment.margin.block_start),
                        };
                        let margin_block_end = match margin.block_end {
                            Margin::Auto => MaybeAuto::Auto,
                            _ => MaybeAuto::Specified(self.fragment.margin.block_end),
                        };

                        let margin_block_start = margin_block_start.specified_or_zero();
                        let margin_block_end = margin_block_end.specified_or_zero();
                        let sum = block_start + block_end + margin_block_start + margin_block_end;
                        Some(available_block_size - sum)
                    },
                    (_, _) => None,
                }
            },
        }
    }

    fn calculate_absolute_block_size_and_margins(&mut self, shared_context: &SharedStyleContext) {
        let opaque_self = OpaqueFlow::from_flow(self);
        let containing_block_block_size = self
            .containing_block_size(&shared_context.viewport_size(), opaque_self)
            .block;

        // This is the stored content block-size value from assign-block-size
        let content_block_size = self.fragment.border_box.size.block;

        let mut solution = None;
        {
            // Non-auto margin-block-start and margin-block-end values have already been
            // calculated during assign-inline-size.
            let margin = self.fragment.style().logical_margin();
            let margin_block_start = match margin.block_start {
                Margin::Auto => MaybeAuto::Auto,
                _ => MaybeAuto::Specified(self.fragment.margin.block_start),
            };
            let margin_block_end = match margin.block_end {
                Margin::Auto => MaybeAuto::Auto,
                _ => MaybeAuto::Specified(self.fragment.margin.block_end),
            };

            let block_start;
            let block_end;
            {
                let position = self.fragment.style().logical_position();
                block_start =
                    MaybeAuto::from_inset(position.block_start, containing_block_block_size);
                block_end = MaybeAuto::from_inset(position.block_end, containing_block_block_size);
            }

            let available_block_size =
                containing_block_block_size - self.fragment.border_padding.block_start_end();
            if self.fragment.is_replaced() {
                // Calculate used value of block-size just like we do for inline replaced elements.
                // TODO: Pass in the containing block block-size when Fragment's
                // assign-block-size can handle it correctly.
                self.fragment.assign_replaced_block_size_if_necessary();
                // TODO: Right now, this content block-size value includes the
                // margin because of erroneous block-size calculation in fragment.
                // Check this when that has been fixed.
                let block_size_used_val = self.fragment.border_box.size.block -
                    self.fragment.border_padding.block_start_end();
                solution = Some(
                    BSizeConstraintSolution::solve_vertical_constraints_abs_replaced(
                        block_size_used_val,
                        margin_block_start,
                        margin_block_end,
                        block_start,
                        block_end,
                        content_block_size,
                        available_block_size,
                    ),
                )
            } else {
                let mut candidate_block_size_iterator =
                    CandidateBSizeIterator::new(&self.fragment, Some(containing_block_block_size));

                // Can't use `for` because we assign to
                // `candidate_block_size_iterator.candidate_value`.
                while let Some(block_size_used_val) = candidate_block_size_iterator.next() {
                    solution = Some(
                        BSizeConstraintSolution::solve_vertical_constraints_abs_nonreplaced(
                            block_size_used_val,
                            margin_block_start,
                            margin_block_end,
                            block_start,
                            block_end,
                            content_block_size,
                            available_block_size,
                        ),
                    );

                    candidate_block_size_iterator.candidate_value = solution.unwrap().block_size;
                }
            }
        }

        let solution = solution.unwrap();
        self.fragment.margin.block_start = solution.margin_block_start;
        self.fragment.margin.block_end = solution.margin_block_end;
        self.fragment.border_box.start.b = Au(0);

        if !self
            .base
            .flags
            .contains(FlowFlags::BLOCK_POSITION_IS_STATIC)
        {
            self.base.position.start.b = solution.block_start + self.fragment.margin.block_start
        }

        let block_size = solution.block_size + self.fragment.border_padding.block_start_end();

        self.fragment.border_box.size.block = block_size;
        self.base.position.size.block = block_size;

        self.base
            .restyle_damage
            .remove(ServoRestyleDamage::REFLOW_OUT_OF_FLOW | ServoRestyleDamage::REFLOW);
        self.fragment
            .restyle_damage
            .remove(ServoRestyleDamage::REFLOW_OUT_OF_FLOW | ServoRestyleDamage::REFLOW);
    }

    /// Compute inline size based using the `block_container_inline_size` set by the parent flow.
    ///
    /// This is run in the `AssignISizes` traversal.
    fn propagate_and_compute_used_inline_size(&mut self, shared_context: &SharedStyleContext) {
        let containing_block_inline_size = self.base.block_container_inline_size;
        self.compute_used_inline_size(shared_context, containing_block_inline_size);
        if self.base.flags.is_float() {
            self.float.as_mut().unwrap().containing_inline_size = containing_block_inline_size
        }
    }

    /// Assigns the computed inline-start content edge and inline-size to all the children of this
    /// block flow. The given `callback`, if supplied, will be called once per child; it is
    /// currently used to push down column sizes for tables.
    ///
    /// `#[inline(always)]` because this is called only from block or table inline-size assignment
    /// and the code for block layout is significantly simpler.
    #[inline(always)]
    pub fn propagate_assigned_inline_size_to_children<F>(
        &mut self,
        shared_context: &SharedStyleContext,
        inline_start_content_edge: Au,
        inline_end_content_edge: Au,
        content_inline_size: Au,
        mut callback: F,
    ) where
        F: FnMut(&mut dyn Flow, usize, Au, WritingMode, &mut Au, &mut Au),
    {
        let flags = self.base.flags;

        let opaque_self = OpaqueFlow::from_flow(self);

        // Calculate non-auto block size to pass to children.
        let box_border = match self.fragment.style().get_position().box_sizing {
            BoxSizing::BorderBox => self.fragment.border_padding.block_start_end(),
            BoxSizing::ContentBox => Au(0),
        };
        let parent_container_size = self.explicit_block_containing_size(shared_context);
        // https://drafts.csswg.org/css-ui-3/#box-sizing
        let mut explicit_content_size = self.explicit_block_size(parent_container_size).map(|x| {
            if x < box_border {
                Au(0)
            } else {
                x - box_border
            }
        });
        if self.is_root() {
            explicit_content_size = max(parent_container_size, explicit_content_size);
        }
        // Calculate containing block inline size.
        let containing_block_size = if flags.contains(FlowFlags::IS_ABSOLUTELY_POSITIONED) {
            self.containing_block_size(&shared_context.viewport_size(), opaque_self)
                .inline
        } else {
            content_inline_size
        };
        // FIXME (mbrubeck): Get correct mode for absolute containing block
        let containing_block_mode = self.base.writing_mode;

        let mut inline_start_margin_edge = inline_start_content_edge;
        let mut inline_end_margin_edge = inline_end_content_edge;

        for (i, kid) in self.base.child_iter_mut().enumerate().peekable() {
            kid.mut_base().block_container_explicit_block_size = explicit_content_size;

            // The inline-start margin edge of the child flow is at our inline-start content edge,
            // and its inline-size is our content inline-size.
            let kid_mode = kid.base().writing_mode;
            {
                // Don't assign positions to children unless they're going to be reflowed.
                // Otherwise, the position we assign might be incorrect and never fixed up. (Issue
                // #13704.)
                //
                // For instance, floats have their true inline position calculated in
                // `assign_block_size()`, which won't do anything unless `REFLOW` is set. So, if a
                // float child does not have `REFLOW` set, we must be careful to avoid touching its
                // inline position, as no logic will run afterward to set its true value.
                let kid_base = kid.mut_base();
                let reflow_damage = if kid_base.flags.contains(FlowFlags::IS_ABSOLUTELY_POSITIONED)
                {
                    ServoRestyleDamage::REFLOW_OUT_OF_FLOW
                } else {
                    ServoRestyleDamage::REFLOW
                };
                if kid_base
                    .flags
                    .contains(FlowFlags::INLINE_POSITION_IS_STATIC) &&
                    kid_base.restyle_damage.contains(reflow_damage)
                {
                    kid_base.position.start.i =
                        if kid_mode.is_bidi_ltr() == containing_block_mode.is_bidi_ltr() {
                            inline_start_content_edge
                        } else {
                            // The kid's inline 'start' is at the parent's 'end'
                            inline_end_content_edge
                        };
                }
                kid_base.block_container_inline_size = content_inline_size;
                kid_base.block_container_writing_mode = containing_block_mode;
            }

            // Call the callback to propagate extra inline size information down to the child. This
            // is currently used for tables.
            callback(
                kid,
                i,
                content_inline_size,
                containing_block_mode,
                &mut inline_start_margin_edge,
                &mut inline_end_margin_edge,
            );

            // Per CSS 2.1 § 16.3.1, text alignment propagates to all children in flow.
            //
            // TODO(#2265, pcwalton): Do this in the cascade instead.
            let containing_block_text_align = self.fragment.style().get_inherited_text().text_align;
            kid.mut_base().text_align = containing_block_text_align;

            // Handle `text-indent` on behalf of any inline children that we have. This is
            // necessary because any percentages are relative to the containing block, which only
            // we know.
            if kid.is_inline_flow() {
                kid.as_mut_inline().first_line_indentation = self
                    .fragment
                    .style()
                    .get_inherited_text()
                    .text_indent
                    .length
                    .to_used_value(containing_block_size);
            }
        }
    }

    /// Determines the type of formatting context this is. See the definition of
    /// `FormattingContextType`.
    pub fn formatting_context_type(&self) -> FormattingContextType {
        if self
            .base
            .flags
            .contains(FlowFlags::IS_ABSOLUTELY_POSITIONED)
        {
            return FormattingContextType::Other;
        }
        if self.is_inline_flex_item() || self.is_block_flex_item() {
            return FormattingContextType::Other;
        }
        let style = self.fragment.style();
        if style.get_box().float != Float::None {
            return FormattingContextType::Other;
        }
        match style.get_box().display {
            Display::TableCell |
            Display::TableCaption |
            Display::TableRowGroup |
            Display::Table |
            Display::InlineBlock |
            Display::Flex => FormattingContextType::Other,
            _ if style.get_box().overflow_x != StyleOverflow::Visible ||
                style.get_box().overflow_y != StyleOverflow::Visible ||
                style.is_multicol() =>
            {
                FormattingContextType::Block
            },
            _ => FormattingContextType::None,
        }
    }

    /// Per CSS 2.1 § 9.5, block formatting contexts' inline widths and positions are affected by
    /// the presence of floats. This is the part of the assign-heights traversal that computes
    /// the final inline position and width for such flows.
    ///
    /// Note that this is part of the assign-block-sizes traversal, not the assign-inline-sizes
    /// traversal as one might expect. That is because, in general, float placement cannot occur
    /// until heights are assigned. To work around this unfortunate circular dependency, by the
    /// time we get here we have already estimated the width of the block formatting context based
    /// on the floats we could see at the time of inline-size assignment. The job of this function,
    /// therefore, is not only to assign the final size but also to perform the layout again for
    /// this block formatting context if our speculation was wrong.
    fn assign_inline_position_for_formatting_context(
        &mut self,
        layout_context: &LayoutContext,
        content_box: LogicalRect<Au>,
    ) {
        debug_assert_ne!(self.formatting_context_type(), FormattingContextType::None);

        if !self
            .base
            .restyle_damage
            .intersects(ServoRestyleDamage::REFLOW_OUT_OF_FLOW | ServoRestyleDamage::REFLOW)
        {
            return;
        }

        // We do this first to avoid recomputing our inline size when we propagate it.
        self.base
            .restyle_damage
            .remove(ServoRestyleDamage::REFLOW_OUT_OF_FLOW | ServoRestyleDamage::REFLOW);
        self.fragment
            .restyle_damage
            .remove(ServoRestyleDamage::REFLOW_OUT_OF_FLOW | ServoRestyleDamage::REFLOW);

        // The code below would completely wreck the layout if run on a flex item, however:
        //   * Flex items are always the children of flex containers.
        //   * Flex containers only contain flex items.
        //   * Floats cannot intrude into flex containers.
        //   * Floats cannot escape flex items.
        //   * Flex items cannot also be floats.
        // Therefore, a flex item cannot be impacted by a float.
        // See also: https://www.w3.org/TR/css-flexbox-1/#flex-containers
        if !self.base.might_have_floats_in() {
            return;
        }

        // If you remove the might_have_floats_in conditional, this will go off.
        // TODO(servo#30572) revert to debug_assert!() once underlying bug is fixed
        #[cfg(debug_assertions)]
        if self.is_inline_flex_item() {
            log::warn!("debug assertion failed! !self.is_inline_flex_item()");
        }

        // Compute the available space for us, based on the actual floats.
        let rect = self.base.floats.available_rect(
            Au(0),
            self.fragment.border_box.size.block,
            content_box.size.inline,
        );
        let available_inline_size = if let Some(rect) = rect {
            // Offset our position by whatever displacement is needed to not impact the floats.
            // Also, account for margins sliding behind floats.
            let inline_offset = if self.fragment.margin.inline_start < rect.start.i {
                // Do not do anything for negative margins; those are handled separately.
                rect.start.i - max(Au(0), self.fragment.margin.inline_start)
            } else {
                Au(0)
            };
            self.base.position.start.i = content_box.start.i + inline_offset;
            // Handle the end margin sliding behind the float.
            let end = content_box.size.inline - rect.start.i - rect.size.inline;
            let inline_end_offset = if self.fragment.margin.inline_end < end {
                end - max(Au(0), self.fragment.margin.inline_end)
            } else {
                Au(0)
            };
            content_box.size.inline - inline_offset - inline_end_offset
        } else {
            content_box.size.inline
        } - self.fragment.margin.inline_start_end();
        let max_inline_size = self
            .fragment
            .style()
            .max_inline_size()
            .to_used_value(self.base.block_container_inline_size)
            .unwrap_or(MAX_AU);
        let min_inline_size = self
            .fragment
            .style()
            .min_inline_size()
            .to_used_value(self.base.block_container_inline_size)
            .unwrap_or(Au(0));
        let specified_inline_size = self.fragment.style().content_inline_size();
        let container_size = self.base.block_container_inline_size;
        let inline_size = match specified_inline_size.to_used_value(container_size) {
            Some(size) => match self.fragment.style().get_position().box_sizing {
                BoxSizing::BorderBox => size,
                BoxSizing::ContentBox => size + self.fragment.border_padding.inline_start_end(),
            },
            None => max(min_inline_size, min(available_inline_size, max_inline_size)),
        };
        self.base.position.size.inline = inline_size + self.fragment.margin.inline_start_end();

        // If float speculation failed, fixup our layout, and re-layout all the children.
        if self.fragment.margin_box_inline_size() != self.base.position.size.inline {
            debug!("assign_inline_position_for_formatting_context: float speculation failed");
            // Fix-up our own layout.
            // We can't just traverse_flow_tree_preorder ourself, because that would re-run
            // float speculation, instead of acting on the actual results.
            self.fragment.border_box.size.inline = inline_size;
            // Assign final-final inline sizes on all our children.
            self.assign_inline_sizes(layout_context);
            // Re-run layout on our children.
            for child in self.base.child_iter_mut() {
                sequential::reflow(child, layout_context, RelayoutMode::Force);
            }
            // Assign our final-final block size.
            self.assign_block_size(layout_context);
        }

        debug_assert_eq!(
            self.fragment.margin_box_inline_size(),
            self.base.position.size.inline
        );
    }

    fn is_inline_block_or_inline_flex(&self) -> bool {
        self.fragment.style().get_box().display == Display::InlineBlock ||
            self.fragment.style().get_box().display == Display::InlineFlex
    }

    /// Computes the content portion (only) of the intrinsic inline sizes of this flow. This is
    /// used for calculating shrink-to-fit width. Assumes that intrinsic sizes have already been
    /// computed for this flow.
    fn content_intrinsic_inline_sizes(&self) -> IntrinsicISizes {
        let (border_padding, margin) = self.fragment.surrounding_intrinsic_inline_size();
        IntrinsicISizes {
            minimum_inline_size: self.base.intrinsic_inline_sizes.minimum_inline_size -
                border_padding -
                margin,
            preferred_inline_size: self.base.intrinsic_inline_sizes.preferred_inline_size -
                border_padding -
                margin,
        }
    }

    /// Computes intrinsic inline sizes for a block.
    pub fn bubble_inline_sizes_for_block(&mut self, consult_children: bool) {
        let _scope = layout_debug_scope!("block::bubble_inline_sizes {:x}", self.base.debug_id());

        let mut flags = self.base.flags;
        if self.definitely_has_zero_block_size() {
            // This is kind of a hack for Acid2. But it's a harmless one, because (a) this behavior
            // is unspecified; (b) it matches the behavior one would intuitively expect, since
            // floats don't flow around blocks that take up no space in the block direction.
            flags.remove(FlowFlags::CONTAINS_TEXT_OR_REPLACED_FRAGMENTS);
        } else if self.fragment.is_text_or_replaced() {
            flags.insert(FlowFlags::CONTAINS_TEXT_OR_REPLACED_FRAGMENTS);
        } else {
            flags.remove(FlowFlags::CONTAINS_TEXT_OR_REPLACED_FRAGMENTS);
            for kid in self.base.children.iter() {
                if kid
                    .base()
                    .flags
                    .contains(FlowFlags::CONTAINS_TEXT_OR_REPLACED_FRAGMENTS)
                {
                    flags.insert(FlowFlags::CONTAINS_TEXT_OR_REPLACED_FRAGMENTS);
                    break;
                }
            }
        }

        // Find the maximum inline-size from children.
        //
        // See: https://lists.w3.org/Archives/Public/www-style/2014Nov/0085.html
        //
        // FIXME(pcwalton): This doesn't exactly follow that algorithm at the moment.
        // FIXME(pcwalton): This should consider all float descendants, not just children.
        let mut computation = self.fragment.compute_intrinsic_inline_sizes();
        let (mut left_float_width, mut right_float_width) = (Au(0), Au(0));
        let (mut left_float_width_accumulator, mut right_float_width_accumulator) = (Au(0), Au(0));
        let mut preferred_inline_size_of_children_without_text_or_replaced_fragments = Au(0);
        for kid in self.base.child_iter_mut() {
            if kid
                .base()
                .flags
                .contains(FlowFlags::IS_ABSOLUTELY_POSITIONED) ||
                !consult_children
            {
                continue;
            }

            let child_base = kid.mut_base();
            let float_kind = child_base.flags.float_kind();
            computation.content_intrinsic_sizes.minimum_inline_size = max(
                computation.content_intrinsic_sizes.minimum_inline_size,
                child_base.intrinsic_inline_sizes.minimum_inline_size,
            );

            if child_base.flags.contains(FlowFlags::CLEARS_LEFT) {
                left_float_width = max(left_float_width, left_float_width_accumulator);
                left_float_width_accumulator = Au(0)
            }
            if child_base.flags.contains(FlowFlags::CLEARS_RIGHT) {
                right_float_width = max(right_float_width, right_float_width_accumulator);
                right_float_width_accumulator = Au(0)
            }

            match (
                float_kind,
                child_base
                    .flags
                    .contains(FlowFlags::CONTAINS_TEXT_OR_REPLACED_FRAGMENTS),
            ) {
                (Float::None, true) => {
                    computation.content_intrinsic_sizes.preferred_inline_size = max(
                        computation.content_intrinsic_sizes.preferred_inline_size,
                        child_base.intrinsic_inline_sizes.preferred_inline_size,
                    );
                },
                (Float::None, false) => {
                    preferred_inline_size_of_children_without_text_or_replaced_fragments = max(
                        preferred_inline_size_of_children_without_text_or_replaced_fragments,
                        child_base.intrinsic_inline_sizes.preferred_inline_size,
                    )
                },
                (Float::Left, _) => {
                    left_float_width_accumulator +=
                        child_base.intrinsic_inline_sizes.preferred_inline_size;
                },
                (Float::Right, _) => {
                    right_float_width_accumulator +=
                        child_base.intrinsic_inline_sizes.preferred_inline_size;
                },
            }
        }

        left_float_width = max(left_float_width, left_float_width_accumulator);
        right_float_width = max(right_float_width, right_float_width_accumulator);

        computation.content_intrinsic_sizes.preferred_inline_size =
            computation.content_intrinsic_sizes.preferred_inline_size +
                left_float_width +
                right_float_width;
        computation.content_intrinsic_sizes.preferred_inline_size = max(
            computation.content_intrinsic_sizes.preferred_inline_size,
            preferred_inline_size_of_children_without_text_or_replaced_fragments,
        );

        self.base.intrinsic_inline_sizes = computation.finish();
        self.base.flags = flags
    }

    pub fn overflow_style_may_require_clip_scroll_node(&self) -> bool {
        match (
            self.fragment.style().get_box().overflow_x,
            self.fragment.style().get_box().overflow_y,
        ) {
            (StyleOverflow::Auto, _) |
            (StyleOverflow::Scroll, _) |
            (StyleOverflow::Hidden, _) |
            (_, StyleOverflow::Auto) |
            (_, StyleOverflow::Scroll) |
            (_, StyleOverflow::Hidden) => true,
            (_, _) => false,
        }
    }

    pub fn compute_inline_sizes(&mut self, shared_context: &SharedStyleContext) {
        if !self
            .base
            .restyle_damage
            .intersects(ServoRestyleDamage::REFLOW_OUT_OF_FLOW | ServoRestyleDamage::REFLOW)
        {
            return;
        }

        debug!(
            "assign_inline_sizes({}): assigning inline_size for flow",
            if self.base.flags.is_float() {
                "float"
            } else {
                "block"
            }
        );
        trace!("BlockFlow before assigning: {:?}", &self);

        self.base.floats = Floats::new(self.base.writing_mode);

        self.initialize_container_size_for_root(shared_context);

        // Our inline-size was set to the inline-size of the containing block by the flow's parent.
        // Now compute the real value.
        self.propagate_and_compute_used_inline_size(shared_context);

        self.guess_inline_size_for_block_formatting_context_if_necessary();

        trace!("BlockFlow after assigning: {:?}", &self);
    }

    /// If this is the root flow, initialize values that would normally be set by the parent.
    ///
    /// Should be called during `assign_inline_sizes` for flows that may be the root.
    pub fn initialize_container_size_for_root(&mut self, shared_context: &SharedStyleContext) {
        if self.is_root() {
            debug!("Setting root position");
            self.base.position.start = LogicalPoint::zero(self.base.writing_mode);
            self.base.block_container_inline_size =
                LogicalSize::from_physical(self.base.writing_mode, shared_context.viewport_size())
                    .inline;
            self.base.block_container_writing_mode = self.base.writing_mode;
        }
    }

    fn guess_inline_size_for_block_formatting_context_if_necessary(&mut self) {
        // We don't need to guess anything unless this is a block formatting context.
        if self.formatting_context_type() != FormattingContextType::Block {
            return;
        }

        // If `max-width` is set, then don't perform this speculation. We guess that the
        // page set `max-width` in order to avoid hitting floats. The search box on Google
        // SERPs falls into this category.
        if !matches!(self.fragment.style.max_inline_size(), MaxSize::None) {
            return;
        }

        // At this point, we know we can't precisely compute the inline-size of this block now,
        // because floats might affect it. Speculate that its inline-size is equal to the
        // inline-size computed above minus the inline-size of the previous left and/or right
        // floats.
        let speculated_left_float_size = if self.fragment.margin.inline_start >= Au(0) &&
            self.base.speculated_float_placement_in.left > self.fragment.margin.inline_start
        {
            self.base.speculated_float_placement_in.left - self.fragment.margin.inline_start
        } else {
            Au(0)
        };
        let speculated_right_float_size = if self.fragment.margin.inline_end >= Au(0) &&
            self.base.speculated_float_placement_in.right > self.fragment.margin.inline_end
        {
            self.base.speculated_float_placement_in.right - self.fragment.margin.inline_end
        } else {
            Au(0)
        };
        self.fragment.border_box.size.inline = self.fragment.border_box.size.inline -
            speculated_left_float_size -
            speculated_right_float_size
    }

    fn definitely_has_zero_block_size(&self) -> bool {
        if !self
            .fragment
            .style
            .content_block_size()
            .is_definitely_zero()
        {
            return false;
        }
        let border_width = self.fragment.border_width();
        if border_width.block_start != Au(0) || border_width.block_end != Au(0) {
            return false;
        }
        let padding = self.fragment.style.logical_padding();
        padding.block_start.is_definitely_zero() && padding.block_end.is_definitely_zero()
    }

    pub fn is_inline_flex_item(&self) -> bool {
        self.fragment
            .flags
            .contains(FragmentFlags::IS_INLINE_FLEX_ITEM)
    }

    pub fn is_block_flex_item(&self) -> bool {
        self.fragment
            .flags
            .contains(FragmentFlags::IS_BLOCK_FLEX_ITEM)
    }

    pub fn mark_scrolling_overflow(&mut self, has_scrolling_overflow: bool) {
        if has_scrolling_overflow {
            self.flags.insert(BlockFlowFlags::HAS_SCROLLING_OVERFLOW);
        } else {
            self.flags.remove(BlockFlowFlags::HAS_SCROLLING_OVERFLOW);
        }
    }

    pub fn has_scrolling_overflow(&self) -> bool {
        self.flags.contains(BlockFlowFlags::HAS_SCROLLING_OVERFLOW)
    }

    // Return offset from original position because of `position: sticky`.
    pub fn sticky_position(&self) -> SideOffsets2D<MaybeAuto> {
        let containing_block_size = &self
            .base
            .early_absolute_position_info
            .relative_containing_block_size;
        let writing_mode = self
            .base
            .early_absolute_position_info
            .relative_containing_block_mode;
        let offsets = self.fragment.style().logical_position();
        let as_margins = LogicalMargin::new(
            writing_mode,
            MaybeAuto::from_inset(offsets.block_start, containing_block_size.inline),
            MaybeAuto::from_inset(offsets.inline_end, containing_block_size.inline),
            MaybeAuto::from_inset(offsets.block_end, containing_block_size.inline),
            MaybeAuto::from_inset(offsets.inline_start, containing_block_size.inline),
        );
        as_margins.to_physical(writing_mode)
    }

    pub fn background_border_section(&self) -> DisplayListSection {
        if self.base.flags.is_float() {
            DisplayListSection::BackgroundAndBorders
        } else if self
            .base
            .flags
            .contains(FlowFlags::IS_ABSOLUTELY_POSITIONED)
        {
            if self.fragment.establishes_stacking_context() {
                DisplayListSection::BackgroundAndBorders
            } else {
                DisplayListSection::BlockBackgroundsAndBorders
            }
        } else {
            DisplayListSection::BlockBackgroundsAndBorders
        }
    }
}

impl Flow for BlockFlow {
    fn class(&self) -> FlowClass {
        FlowClass::Block
    }

    fn as_mut_block(&mut self) -> &mut BlockFlow {
        self
    }

    fn as_block(&self) -> &BlockFlow {
        self
    }

    /// Pass 1 of reflow: computes minimum and preferred inline-sizes.
    ///
    /// Recursively (bottom-up) determine the flow's minimum and preferred inline-sizes. When
    /// called on this flow, all child flows have had their minimum and preferred inline-sizes set.
    /// This function must decide minimum/preferred inline-sizes based on its children's
    /// inline-sizes and the dimensions of any fragments it is responsible for flowing.
    fn bubble_inline_sizes(&mut self) {
        // If this block has a fixed width, just use that for the minimum and preferred width,
        // rather than bubbling up children inline width.
        // FIXME(emilio): This should probably be writing-mode-aware.
        let consult_children = self
            .fragment
            .style()
            .get_position()
            .width
            .maybe_to_used_value(None)
            .is_none();
        self.bubble_inline_sizes_for_block(consult_children);
        self.fragment
            .restyle_damage
            .remove(ServoRestyleDamage::BUBBLE_ISIZES);
    }

    /// Recursively (top-down) determines the actual inline-size of child contexts and fragments.
    /// When called on this context, the context has had its inline-size set by the parent context.
    ///
    /// Dual fragments consume some inline-size first, and the remainder is assigned to all child
    /// (block) contexts.
    fn assign_inline_sizes(&mut self, layout_context: &LayoutContext) {
        let _scope = layout_debug_scope!("block::assign_inline_sizes {:x}", self.base.debug_id());

        let shared_context = layout_context.shared_context();
        self.compute_inline_sizes(shared_context);

        // Move in from the inline-start border edge.
        let inline_start_content_edge =
            self.fragment.border_box.start.i + self.fragment.border_padding.inline_start;

        let padding_and_borders = self.fragment.border_padding.inline_start_end();

        // Distance from the inline-end margin edge to the inline-end content edge.
        let inline_end_content_edge =
            self.fragment.margin.inline_end + self.fragment.border_padding.inline_end;

        let content_inline_size = self.fragment.border_box.size.inline - padding_and_borders;

        self.propagate_assigned_inline_size_to_children(
            shared_context,
            inline_start_content_edge,
            inline_end_content_edge,
            content_inline_size,
            |_, _, _, _, _, _| {},
        );
    }

    fn place_float_if_applicable<'a>(&mut self) {
        if self.base.flags.is_float() {
            self.place_float();
        }
    }

    fn assign_block_size_for_inorder_child_if_necessary(
        &mut self,
        layout_context: &LayoutContext,
        parent_thread_id: u8,
        content_box: LogicalRect<Au>,
    ) -> bool {
        if self.base.flags.is_float() {
            return false;
        }

        let is_formatting_context = self.formatting_context_type() != FormattingContextType::None;
        if !self
            .base
            .flags
            .contains(FlowFlags::IS_ABSOLUTELY_POSITIONED) &&
            is_formatting_context
        {
            self.assign_inline_position_for_formatting_context(layout_context, content_box);
        }

        if (self as &dyn Flow).floats_might_flow_through() {
            self.base.thread_id = parent_thread_id;
            if self
                .base
                .restyle_damage
                .intersects(ServoRestyleDamage::REFLOW_OUT_OF_FLOW | ServoRestyleDamage::REFLOW)
            {
                self.assign_block_size(layout_context);
                // Don't remove the restyle damage; `assign_block_size` decides whether that is
                // appropriate (which in the case of e.g. absolutely-positioned flows, it is not).
            }
            return true;
        }

        if is_formatting_context {
            // If this is a formatting context and definitely did not have floats in, then we must
            // translate the floats past us.
            let writing_mode = self.base.floats.writing_mode;
            let delta = self.base.position.size.block;
            self.base
                .floats
                .translate(LogicalSize::new(writing_mode, Au(0), -delta));
            return true;
        }

        false
    }

    fn assign_block_size(&mut self, ctx: &LayoutContext) {
        let remaining = Flow::fragment(self, ctx, None);
        debug_assert!(remaining.is_none());
    }

    fn fragment(
        &mut self,
        layout_context: &LayoutContext,
        fragmentation_context: Option<FragmentationContext>,
    ) -> Option<Arc<dyn Flow>> {
        if self.fragment.is_replaced() {
            let _scope = layout_debug_scope!(
                "assign_replaced_block_size_if_necessary {:x}",
                self.base.debug_id()
            );

            // Assign block-size for fragment if it is an image fragment.
            self.fragment.assign_replaced_block_size_if_necessary();
            if !self
                .base
                .flags
                .contains(FlowFlags::IS_ABSOLUTELY_POSITIONED)
            {
                self.base.position.size.block = self.fragment.border_box.size.block;
                let mut block_start =
                    AdjoiningMargins::from_margin(self.fragment.margin.block_start);
                let block_end = AdjoiningMargins::from_margin(self.fragment.margin.block_end);
                if self.fragment.border_box.size.block == Au(0) {
                    block_start.union(block_end);
                    self.base.collapsible_margins =
                        CollapsibleMargins::CollapseThrough(block_start);
                } else {
                    self.base.collapsible_margins =
                        CollapsibleMargins::Collapse(block_start, block_end);
                }
                self.base
                    .restyle_damage
                    .remove(ServoRestyleDamage::REFLOW_OUT_OF_FLOW | ServoRestyleDamage::REFLOW);
                self.fragment
                    .restyle_damage
                    .remove(ServoRestyleDamage::REFLOW_OUT_OF_FLOW | ServoRestyleDamage::REFLOW);
            }
            None
        } else if self.is_root() ||
            self.formatting_context_type() != FormattingContextType::None ||
            self.base.flags.contains(FlowFlags::MARGINS_CANNOT_COLLAPSE)
        {
            // Root element margins should never be collapsed according to CSS § 8.3.1.
            debug!("{}", self.is_root());
            debug!(
                "assign_block_size: assigning block_size for root flow {:#x?}",
                self.base().debug_id()
            );
            trace!("BlockFlow before assigning: {:?}", &self);
            let flow = self.assign_block_size_block_base(
                layout_context,
                fragmentation_context,
                MarginsMayCollapseFlag::MarginsMayNotCollapse,
            );
            trace!("BlockFlow after assigning: {:?}", &self);
            flow
        } else {
            debug!(
                "assign_block_size: assigning block_size for block {:#x?}",
                self.base().debug_id()
            );
            trace!("BlockFlow before assigning: {:?}", &self);
            let flow = self.assign_block_size_block_base(
                layout_context,
                fragmentation_context,
                MarginsMayCollapseFlag::MarginsMayCollapse,
            );
            trace!("BlockFlow after assigning: {:?}", &self);
            flow
        }
    }

    fn compute_stacking_relative_position(&mut self, _layout_context: &LayoutContext) {
        // FIXME (mbrubeck): Get the real container size, taking the container writing mode into
        // account.  Must handle vertical writing modes.
        let container_size = Size2D::new(self.base.block_container_inline_size, Au(0));

        if self.is_root() {
            self.base.clip = Rect::max_rect();
        }

        if self
            .base
            .flags
            .contains(FlowFlags::IS_ABSOLUTELY_POSITIONED)
        {
            let position_start = self
                .base
                .position
                .start
                .to_physical(self.base.writing_mode, container_size);

            // Compute our position relative to the nearest ancestor stacking context. This will be
            // passed down later as part of containing block details for absolute descendants.
            let absolute_stacking_relative_position = if self.is_fixed() {
                // The viewport is initially at (0, 0).
                position_start
            } else {
                // Absolute position of the containing block + position of absolute
                // flow w.r.t. the containing block.
                self.base
                    .late_absolute_position_info
                    .stacking_relative_position_of_absolute_containing_block +
                    position_start.to_vector()
            };

            if !self.base.writing_mode.is_vertical() {
                if !self
                    .base
                    .flags
                    .contains(FlowFlags::INLINE_POSITION_IS_STATIC)
                {
                    self.base.stacking_relative_position.x = absolute_stacking_relative_position.x
                }
                if !self
                    .base
                    .flags
                    .contains(FlowFlags::BLOCK_POSITION_IS_STATIC)
                {
                    self.base.stacking_relative_position.y = absolute_stacking_relative_position.y
                }
            } else {
                if !self
                    .base
                    .flags
                    .contains(FlowFlags::INLINE_POSITION_IS_STATIC)
                {
                    self.base.stacking_relative_position.y = absolute_stacking_relative_position.y
                }
                if !self
                    .base
                    .flags
                    .contains(FlowFlags::BLOCK_POSITION_IS_STATIC)
                {
                    self.base.stacking_relative_position.x = absolute_stacking_relative_position.x
                }
            }
        }

        // For relatively-positioned descendants, the containing block formed by a block is just
        // the content box. The containing block for absolutely-positioned descendants, on the
        // other hand, is established in other circumstances (see `is_absolute_containing_block').
        let relative_offset = self.fragment.relative_position(
            &self
                .base
                .early_absolute_position_info
                .relative_containing_block_size,
        );
        if self.is_absolute_containing_block() {
            let border_box_origin =
                (self.fragment.border_box - self.fragment.style.logical_border_width()).start;
            self.base
                .late_absolute_position_info
                .stacking_relative_position_of_absolute_containing_block =
                self.base.stacking_relative_position.to_point() +
                    (border_box_origin + relative_offset)
                        .to_physical(self.base.writing_mode, container_size)
                        .to_vector()
        }

        // Compute absolute position info for children.
        let stacking_relative_position_of_absolute_containing_block_for_children =
            if self.fragment.establishes_stacking_context() {
                let logical_border_width = self.fragment.style().logical_border_width();
                let position = LogicalPoint::new(
                    self.base.writing_mode,
                    logical_border_width.inline_start,
                    logical_border_width.block_start,
                );
                let position = position.to_physical(self.base.writing_mode, container_size);

                // Some blocks establish a stacking context, but not a containing block for
                // absolutely positioned elements. An example of this might be a block that has
                // `position: static` and `opacity` set. In these cases, absolutely-positioned
                // children will not be positioned relative to us but will instead be positioned
                // relative to our containing block.
                if self.is_absolute_containing_block() {
                    position
                } else {
                    position - self.base.stacking_relative_position
                }
            } else {
                self.base
                    .late_absolute_position_info
                    .stacking_relative_position_of_absolute_containing_block
            };
        let late_absolute_position_info_for_children = LateAbsolutePositionInfo {
            stacking_relative_position_of_absolute_containing_block:
                stacking_relative_position_of_absolute_containing_block_for_children,
        };
        let container_size_for_children =
            self.base.position.size.to_physical(self.base.writing_mode);

        // Compute the origin and clipping rectangle for children.
        let relative_offset = relative_offset
            .to_physical(self.base.writing_mode)
            .to_vector();
        let is_stacking_context = self.fragment.establishes_stacking_context();
        let origin_for_children = if is_stacking_context {
            // We establish a stacking context, so the position of our children is vertically
            // correct, but has to be adjusted to accommodate horizontal margins. (Note the
            // calculation involving `position` below and recall that inline-direction flow
            // positions are relative to the edges of the margin box.)
            //
            // FIXME(pcwalton): Is this vertical-writing-direction-safe?
            let margin = self.fragment.margin.to_physical(self.base.writing_mode);
            Point2D::new(-margin.left, Au(0))
        } else {
            self.base.stacking_relative_position.to_point() + relative_offset
        };

        // Process children.
        for kid in self.base.child_iter_mut() {
            if kid
                .base()
                .flags
                .contains(FlowFlags::INLINE_POSITION_IS_STATIC) ||
                kid.base()
                    .flags
                    .contains(FlowFlags::BLOCK_POSITION_IS_STATIC)
            {
                let kid_base = kid.mut_base();
                let physical_position = kid_base
                    .position
                    .to_physical(kid_base.writing_mode, container_size_for_children);

                // Set the inline and block positions as necessary.
                if !kid_base.writing_mode.is_vertical() {
                    if kid_base
                        .flags
                        .contains(FlowFlags::INLINE_POSITION_IS_STATIC)
                    {
                        kid_base.stacking_relative_position.x =
                            origin_for_children.x + physical_position.origin.x
                    }
                    if kid_base.flags.contains(FlowFlags::BLOCK_POSITION_IS_STATIC) {
                        kid_base.stacking_relative_position.y =
                            origin_for_children.y + physical_position.origin.y
                    }
                } else {
                    if kid_base
                        .flags
                        .contains(FlowFlags::INLINE_POSITION_IS_STATIC)
                    {
                        kid_base.stacking_relative_position.y =
                            origin_for_children.y + physical_position.origin.y
                    }
                    if kid_base.flags.contains(FlowFlags::BLOCK_POSITION_IS_STATIC) {
                        kid_base.stacking_relative_position.x =
                            origin_for_children.x + physical_position.origin.x
                    }
                }
            }

            kid.mut_base().late_absolute_position_info = late_absolute_position_info_for_children;
        }
    }

    fn mark_as_root(&mut self) {
        self.flags.insert(BlockFlowFlags::IS_ROOT)
    }

    fn is_root(&self) -> bool {
        self.flags.contains(BlockFlowFlags::IS_ROOT)
    }

    /// The 'position' property of this flow.
    fn positioning(&self) -> Position {
        self.fragment.style.get_box().position
    }

    /// Return the dimensions of the containing block generated by this flow for absolutely-
    /// positioned descendants. For block flows, this is the padding box.
    fn generated_containing_block_size(&self, _: OpaqueFlow) -> LogicalSize<Au> {
        (self.fragment.border_box - self.fragment.style().logical_border_width()).size
    }

    /// Returns true if this flow contains fragments that are roots of an absolute flow tree.
    fn contains_roots_of_absolute_flow_tree(&self) -> bool {
        self.contains_relatively_positioned_fragments() ||
            self.is_root() ||
            self.fragment.has_filter_transform_or_perspective()
    }

    /// Returns true if this is an absolute containing block.
    fn is_absolute_containing_block(&self) -> bool {
        self.contains_positioned_fragments() || self.fragment.has_filter_transform_or_perspective()
    }

    fn update_late_computed_inline_position_if_necessary(&mut self, inline_position: Au) {
        if self
            .base
            .flags
            .contains(FlowFlags::IS_ABSOLUTELY_POSITIONED) &&
            self.fragment
                .style()
                .logical_position()
                .inline_start
                .is_auto() &&
            self.fragment
                .style()
                .logical_position()
                .inline_end
                .is_auto()
        {
            self.base.position.start.i = inline_position
        }
    }

    fn update_late_computed_block_position_if_necessary(&mut self, block_position: Au) {
        if self
            .base
            .flags
            .contains(FlowFlags::IS_ABSOLUTELY_POSITIONED) &&
            self.fragment
                .style()
                .logical_position()
                .block_start
                .is_auto() &&
            self.fragment.style().logical_position().block_end.is_auto()
        {
            self.base.position.start.b = block_position
        }
    }

    fn collect_stacking_contexts(&mut self, state: &mut StackingContextCollectionState) {
        self.collect_stacking_contexts_for_block(state, StackingContextCollectionFlags::empty());
    }

    fn build_display_list(&mut self, state: &mut DisplayListBuildState) {
        self.build_display_list_for_block(state, BorderPaintingMode::Separate);
    }

    fn repair_style(&mut self, new_style: &crate::ServoArc<ComputedValues>) {
        self.fragment.repair_style(new_style)
    }

    fn compute_overflow(&self) -> Overflow {
        let flow_size = self.base.position.size.to_physical(self.base.writing_mode);
        self.fragment.compute_overflow(
            &flow_size,
            &self
                .base
                .early_absolute_position_info
                .relative_containing_block_size,
        )
    }

    fn iterate_through_fragment_border_boxes(
        &self,
        iterator: &mut dyn FragmentBorderBoxIterator,
        level: i32,
        stacking_context_position: &Point2D<Au>,
    ) {
        if !iterator.should_process(&self.fragment) {
            return;
        }

        iterator.process(
            &self.fragment,
            level,
            &self
                .fragment
                .stacking_relative_border_box(
                    &self.base.stacking_relative_position,
                    &self
                        .base
                        .early_absolute_position_info
                        .relative_containing_block_size,
                    self.base
                        .early_absolute_position_info
                        .relative_containing_block_mode,
                    CoordinateSystem::Own,
                )
                .translate(stacking_context_position.to_vector()),
        );
    }

    fn mutate_fragments(&mut self, mutator: &mut dyn FnMut(&mut Fragment)) {
        (*mutator)(&mut self.fragment)
    }

    fn print_extra_flow_children(&self, print_tree: &mut PrintTree) {
        print_tree.add_item(format!("↑↑ Fragment for block:{:?}", self.fragment));
    }
}

impl fmt::Debug for BlockFlow {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "{:?}({:x}) {:?}",
            self.class(),
            self.base.debug_id(),
            self.base
        )
    }
}

/// The inputs for the inline-sizes-and-margins constraint equation.
#[derive(Clone, Copy, Debug)]
pub struct ISizeConstraintInput {
    pub computed_inline_size: MaybeAuto,
    pub inline_start_margin: MaybeAuto,
    pub inline_end_margin: MaybeAuto,
    pub inline_start: MaybeAuto,
    pub inline_end: MaybeAuto,
    pub available_inline_size: Au,
}

impl ISizeConstraintInput {
    pub fn new(
        computed_inline_size: MaybeAuto,
        inline_start_margin: MaybeAuto,
        inline_end_margin: MaybeAuto,
        inline_start: MaybeAuto,
        inline_end: MaybeAuto,
        available_inline_size: Au,
    ) -> ISizeConstraintInput {
        ISizeConstraintInput {
            computed_inline_size,
            inline_start_margin,
            inline_end_margin,
            inline_start,
            inline_end,
            available_inline_size,
        }
    }
}

/// The solutions for the inline-size-and-margins constraint equation.
#[derive(Clone, Copy, Debug)]
pub struct ISizeConstraintSolution {
    pub inline_start: Au,
    pub inline_size: Au,
    pub margin_inline_start: Au,
    pub margin_inline_end: Au,
}

impl ISizeConstraintSolution {
    pub fn new(
        inline_size: Au,
        margin_inline_start: Au,
        margin_inline_end: Au,
    ) -> ISizeConstraintSolution {
        ISizeConstraintSolution {
            inline_start: Au(0),
            inline_size,
            margin_inline_start,
            margin_inline_end,
        }
    }

    fn for_absolute_flow(
        inline_start: Au,
        inline_size: Au,
        margin_inline_start: Au,
        margin_inline_end: Au,
    ) -> ISizeConstraintSolution {
        ISizeConstraintSolution {
            inline_start,
            inline_size,
            margin_inline_start,
            margin_inline_end,
        }
    }
}

// Trait to encapsulate the ISize and Margin calculation.
//
// CSS Section 10.3
pub trait ISizeAndMarginsComputer {
    /// Instructs the fragment to compute its border and padding.
    fn compute_border_and_padding(&self, block: &mut BlockFlow, containing_block_inline_size: Au) {
        block
            .fragment
            .compute_border_and_padding(containing_block_inline_size);
    }

    /// Compute the inputs for the ISize constraint equation.
    ///
    /// This is called only once to compute the initial inputs. For calculations involving
    /// minimum and maximum inline-size, we don't need to recompute these.
    fn compute_inline_size_constraint_inputs(
        &self,
        block: &mut BlockFlow,
        parent_flow_inline_size: Au,
        shared_context: &SharedStyleContext,
    ) -> ISizeConstraintInput {
        let containing_block_inline_size =
            self.containing_block_inline_size(block, parent_flow_inline_size, shared_context);

        block
            .fragment
            .compute_block_direction_margins(containing_block_inline_size);
        block
            .fragment
            .compute_inline_direction_margins(containing_block_inline_size);
        self.compute_border_and_padding(block, containing_block_inline_size);

        let mut computed_inline_size =
            self.initial_computed_inline_size(block, parent_flow_inline_size, shared_context);
        let style = block.fragment.style();
        match (computed_inline_size, style.get_position().box_sizing) {
            (MaybeAuto::Specified(size), BoxSizing::BorderBox) => {
                computed_inline_size =
                    MaybeAuto::Specified(size - block.fragment.border_padding.inline_start_end())
            },
            (MaybeAuto::Auto, BoxSizing::BorderBox) | (_, BoxSizing::ContentBox) => {},
        }

        let margin = style.logical_margin();
        let position = style.logical_position();

        let available_inline_size =
            containing_block_inline_size - block.fragment.border_padding.inline_start_end();
        ISizeConstraintInput::new(
            computed_inline_size,
            MaybeAuto::from_margin(margin.inline_start, containing_block_inline_size),
            MaybeAuto::from_margin(margin.inline_end, containing_block_inline_size),
            MaybeAuto::from_inset(position.inline_start, containing_block_inline_size),
            MaybeAuto::from_inset(position.inline_end, containing_block_inline_size),
            available_inline_size,
        )
    }

    /// Set the used values for inline-size and margins from the relevant constraint equation.
    /// This is called only once.
    ///
    /// Set:
    /// * Used values for content inline-size, inline-start margin, and inline-end margin for this
    ///   flow's box;
    /// * Inline-start coordinate of this flow's box;
    /// * Inline-start coordinate of the flow with respect to its containing block (if this is an
    ///   absolute flow).
    fn set_inline_size_constraint_solutions(
        &self,
        block: &mut BlockFlow,
        solution: ISizeConstraintSolution,
    ) {
        let inline_size;
        let extra_inline_size_from_margin;
        {
            let block_mode = block.base.writing_mode;

            // FIXME (mbrubeck): Get correct containing block for positioned blocks?
            let container_mode = block.base.block_container_writing_mode;
            let container_size = block.base.block_container_inline_size;

            let fragment = block.fragment();
            fragment.margin.inline_start = solution.margin_inline_start;
            fragment.margin.inline_end = solution.margin_inline_end;

            // The associated fragment has the border box of this flow.
            inline_size = solution.inline_size + fragment.border_padding.inline_start_end();
            fragment.border_box.size.inline = inline_size;

            // Start border edge.
            // FIXME (mbrubeck): Handle vertical writing modes.
            fragment.border_box.start.i =
                if container_mode.is_bidi_ltr() == block_mode.is_bidi_ltr() {
                    fragment.margin.inline_start
                } else {
                    // The parent's "start" direction is the child's "end" direction.
                    container_size - inline_size - fragment.margin.inline_end
                };

            // To calculate the total size of this block, we also need to account for any
            // additional size contribution from positive margins. Negative margins means the block
            // isn't made larger at all by the margin.
            extra_inline_size_from_margin =
                max(Au(0), fragment.margin.inline_start) + max(Au(0), fragment.margin.inline_end);
        }

        // We also resize the block itself, to ensure that overflow is not calculated
        // as the inline-size of our parent. We might be smaller and we might be larger if we
        // overflow.
        block.mut_base().position.size.inline = inline_size + extra_inline_size_from_margin;
    }

    /// Set the inline coordinate of the given flow if it is absolutely positioned.
    fn set_inline_position_of_flow_if_necessary(
        &self,
        _: &mut BlockFlow,
        _: ISizeConstraintSolution,
    ) {
    }

    /// Solve the inline-size and margins constraints for this block flow.
    fn solve_inline_size_constraints(
        &self,
        block: &mut BlockFlow,
        input: &ISizeConstraintInput,
    ) -> ISizeConstraintSolution;

    fn initial_computed_inline_size(
        &self,
        block: &mut BlockFlow,
        parent_flow_inline_size: Au,
        shared_context: &SharedStyleContext,
    ) -> MaybeAuto {
        let inline_size =
            self.containing_block_inline_size(block, parent_flow_inline_size, shared_context);

        MaybeAuto::from_option(
            block
                .fragment()
                .style()
                .content_inline_size()
                .to_used_value(inline_size),
        )
    }

    fn containing_block_inline_size(
        &self,
        _: &mut BlockFlow,
        parent_flow_inline_size: Au,
        _: &SharedStyleContext,
    ) -> Au {
        parent_flow_inline_size
    }

    /// Compute the used value of inline-size, taking care of min-inline-size and max-inline-size.
    ///
    /// CSS Section 10.4: Minimum and Maximum inline-sizes
    fn compute_used_inline_size(
        &self,
        block: &mut BlockFlow,
        shared_context: &SharedStyleContext,
        parent_flow_inline_size: Au,
    ) {
        let mut input = self.compute_inline_size_constraint_inputs(
            block,
            parent_flow_inline_size,
            shared_context,
        );

        let containing_block_inline_size =
            self.containing_block_inline_size(block, parent_flow_inline_size, shared_context);

        let mut solution = self.solve_inline_size_constraints(block, &input);

        // If the tentative used inline-size is greater than 'max-inline-size', inline-size should
        // be recalculated, but this time using the computed value of 'max-inline-size' as the
        // computed value for 'inline-size'.
        match block
            .fragment()
            .style()
            .max_inline_size()
            .to_used_value(containing_block_inline_size)
        {
            Some(max_inline_size) if max_inline_size < solution.inline_size => {
                input.computed_inline_size = MaybeAuto::Specified(max_inline_size);
                solution = self.solve_inline_size_constraints(block, &input);
            },
            _ => {},
        }

        // If the resulting inline-size is smaller than 'min-inline-size', inline-size should be
        // recalculated, but this time using the value of 'min-inline-size' as the computed value
        // for 'inline-size'.
        let computed_min_inline_size = block
            .fragment()
            .style()
            .min_inline_size()
            .to_used_value(containing_block_inline_size)
            .unwrap_or(Au(0));
        if computed_min_inline_size > solution.inline_size {
            input.computed_inline_size = MaybeAuto::Specified(computed_min_inline_size);
            solution = self.solve_inline_size_constraints(block, &input);
        }

        self.set_inline_size_constraint_solutions(block, solution);
        self.set_inline_position_of_flow_if_necessary(block, solution);
    }

    /// Computes inline-start and inline-end margins and inline-size.
    ///
    /// This is used by both replaced and non-replaced Blocks.
    ///
    /// CSS 2.1 Section 10.3.3.
    /// Constraint Equation: margin-inline-start + margin-inline-end + inline-size =
    /// available_inline-size
    /// where available_inline-size = CB inline-size - (horizontal border + padding)
    fn solve_block_inline_size_constraints(
        &self,
        block: &mut BlockFlow,
        input: &ISizeConstraintInput,
    ) -> ISizeConstraintSolution {
        let (computed_inline_size, inline_start_margin, inline_end_margin, available_inline_size) = (
            input.computed_inline_size,
            input.inline_start_margin,
            input.inline_end_margin,
            input.available_inline_size,
        );

        // Check for direction of parent flow (NOT Containing Block)
        let block_mode = block.base.writing_mode;
        let container_mode = block.base.block_container_writing_mode;
        let block_align = block.base.text_align;

        // FIXME (mbrubeck): Handle vertical writing modes.
        let parent_has_same_direction = container_mode.is_bidi_ltr() == block_mode.is_bidi_ltr();

        // If inline-size is not 'auto', and inline-size + margins > available_inline-size, all
        // 'auto' margins are treated as 0.
        let (inline_start_margin, inline_end_margin) = match computed_inline_size {
            MaybeAuto::Auto => (inline_start_margin, inline_end_margin),
            MaybeAuto::Specified(inline_size) => {
                let inline_start = inline_start_margin.specified_or_zero();
                let inline_end = inline_end_margin.specified_or_zero();

                if (inline_start + inline_end + inline_size) > available_inline_size {
                    (
                        MaybeAuto::Specified(inline_start),
                        MaybeAuto::Specified(inline_end),
                    )
                } else {
                    (inline_start_margin, inline_end_margin)
                }
            },
        };

        // Invariant: inline-start_margin + inline-size + inline-end_margin ==
        // available_inline-size
        let (inline_start_margin, inline_size, inline_end_margin) =
            match (inline_start_margin, computed_inline_size, inline_end_margin) {
                // If all have a computed value other than 'auto', the system is over-constrained.
                (
                    MaybeAuto::Specified(margin_start),
                    MaybeAuto::Specified(inline_size),
                    MaybeAuto::Specified(margin_end),
                ) => {
                    // servo_left, servo_right, and servo_center are used to implement
                    // the "align descendants" rule in HTML5 § 14.2.
                    if block_align == TextAlign::MozCenter {
                        // Ignore any existing margins, and make the inline-start and
                        // inline-end margins equal.
                        let margin = (available_inline_size - inline_size).scale_by(0.5);
                        (margin, inline_size, margin)
                    } else {
                        let ignore_end_margin = match block_align {
                            TextAlign::MozLeft => block_mode.is_bidi_ltr(),
                            TextAlign::MozRight => !block_mode.is_bidi_ltr(),
                            _ => parent_has_same_direction,
                        };
                        if ignore_end_margin {
                            (
                                margin_start,
                                inline_size,
                                available_inline_size - (margin_start + inline_size),
                            )
                        } else {
                            (
                                available_inline_size - (margin_end + inline_size),
                                inline_size,
                                margin_end,
                            )
                        }
                    }
                },
                // If exactly one value is 'auto', solve for it
                (
                    MaybeAuto::Auto,
                    MaybeAuto::Specified(inline_size),
                    MaybeAuto::Specified(margin_end),
                ) => (
                    available_inline_size - (inline_size + margin_end),
                    inline_size,
                    margin_end,
                ),
                (
                    MaybeAuto::Specified(margin_start),
                    MaybeAuto::Auto,
                    MaybeAuto::Specified(margin_end),
                ) => (
                    margin_start,
                    available_inline_size - (margin_start + margin_end),
                    margin_end,
                ),
                (
                    MaybeAuto::Specified(margin_start),
                    MaybeAuto::Specified(inline_size),
                    MaybeAuto::Auto,
                ) => (
                    margin_start,
                    inline_size,
                    available_inline_size - (margin_start + inline_size),
                ),

                // If inline-size is set to 'auto', any other 'auto' value becomes '0',
                // and inline-size is solved for
                (MaybeAuto::Auto, MaybeAuto::Auto, MaybeAuto::Specified(margin_end)) => {
                    (Au(0), available_inline_size - margin_end, margin_end)
                },
                (MaybeAuto::Specified(margin_start), MaybeAuto::Auto, MaybeAuto::Auto) => {
                    (margin_start, available_inline_size - margin_start, Au(0))
                },
                (MaybeAuto::Auto, MaybeAuto::Auto, MaybeAuto::Auto) => {
                    (Au(0), available_inline_size, Au(0))
                },

                // If inline-start and inline-end margins are auto, they become equal
                (MaybeAuto::Auto, MaybeAuto::Specified(inline_size), MaybeAuto::Auto) => {
                    let margin = (available_inline_size - inline_size).scale_by(0.5);
                    (margin, inline_size, margin)
                },
            };

        ISizeConstraintSolution::new(inline_size, inline_start_margin, inline_end_margin)
    }
}

/// The different types of Blocks.
///
/// They mainly differ in the way inline-size and block-sizes and margins are calculated
/// for them.
pub struct AbsoluteNonReplaced;
pub struct AbsoluteReplaced;
pub struct BlockNonReplaced;
pub struct BlockReplaced;
pub struct FloatNonReplaced;
pub struct FloatReplaced;
pub struct InlineBlockNonReplaced;
pub struct InlineBlockReplaced;
pub struct InlineFlexItem;

impl ISizeAndMarginsComputer for AbsoluteNonReplaced {
    /// Solve the horizontal constraint equation for absolute non-replaced elements.
    ///
    /// CSS Section 10.3.7
    /// Constraint equation:
    /// inline-start + inline-end + inline-size + margin-inline-start + margin-inline-end
    /// = absolute containing block inline-size - (horizontal padding and border)
    /// [aka available inline-size]
    ///
    /// Return the solution for the equation.
    fn solve_inline_size_constraints(
        &self,
        block: &mut BlockFlow,
        input: &ISizeConstraintInput,
    ) -> ISizeConstraintSolution {
        let &ISizeConstraintInput {
            computed_inline_size,
            inline_start_margin,
            inline_end_margin,
            inline_start,
            inline_end,
            available_inline_size,
            ..
        } = input;

        // Check for direction of parent flow (NOT Containing Block)
        let block_mode = block.base.writing_mode;
        let container_mode = block.base.block_container_writing_mode;

        // FIXME (mbrubeck): Handle vertical writing modes.
        let parent_has_same_direction = container_mode.is_bidi_ltr() == block_mode.is_bidi_ltr();

        let (inline_start, inline_size, margin_inline_start, margin_inline_end) =
            match (inline_start, inline_end, computed_inline_size) {
                (MaybeAuto::Auto, MaybeAuto::Auto, MaybeAuto::Auto) => {
                    let margin_start = inline_start_margin.specified_or_zero();
                    let margin_end = inline_end_margin.specified_or_zero();
                    // Now it is the same situation as inline-start Specified and inline-end
                    // and inline-size Auto.

                    // Set inline-end to zero to calculate inline-size.
                    let inline_size = block.get_shrink_to_fit_inline_size(
                        available_inline_size - (margin_start + margin_end),
                    );
                    (Au(0), inline_size, margin_start, margin_end)
                },
                (
                    MaybeAuto::Specified(inline_start),
                    MaybeAuto::Specified(inline_end),
                    MaybeAuto::Specified(inline_size),
                ) => {
                    match (inline_start_margin, inline_end_margin) {
                        (MaybeAuto::Auto, MaybeAuto::Auto) => {
                            let total_margin_val =
                                available_inline_size - inline_start - inline_end - inline_size;
                            if total_margin_val < Au(0) {
                                if parent_has_same_direction {
                                    // margin-inline-start becomes 0
                                    (inline_start, inline_size, Au(0), total_margin_val)
                                } else {
                                    // margin-inline-end becomes 0, because it's toward the parent's
                                    // inline-start edge.
                                    (inline_start, inline_size, total_margin_val, Au(0))
                                }
                            } else {
                                // Equal margins
                                (
                                    inline_start,
                                    inline_size,
                                    total_margin_val.scale_by(0.5),
                                    total_margin_val.scale_by(0.5),
                                )
                            }
                        },
                        (MaybeAuto::Specified(margin_start), MaybeAuto::Auto) => {
                            let sum = inline_start + inline_end + inline_size + margin_start;
                            (
                                inline_start,
                                inline_size,
                                margin_start,
                                available_inline_size - sum,
                            )
                        },
                        (MaybeAuto::Auto, MaybeAuto::Specified(margin_end)) => {
                            let sum = inline_start + inline_end + inline_size + margin_end;
                            (
                                inline_start,
                                inline_size,
                                available_inline_size - sum,
                                margin_end,
                            )
                        },
                        (MaybeAuto::Specified(margin_start), MaybeAuto::Specified(margin_end)) => {
                            // Values are over-constrained.
                            let sum = inline_start + inline_size + margin_start + margin_end;
                            if parent_has_same_direction {
                                // Ignore value for 'inline-end'
                                (inline_start, inline_size, margin_start, margin_end)
                            } else {
                                // Ignore value for 'inline-start'
                                (
                                    available_inline_size - sum,
                                    inline_size,
                                    margin_start,
                                    margin_end,
                                )
                            }
                        },
                    }
                },
                // For the rest of the cases, auto values for margin are set to 0

                // If only one is Auto, solve for it
                (
                    MaybeAuto::Auto,
                    MaybeAuto::Specified(inline_end),
                    MaybeAuto::Specified(inline_size),
                ) => {
                    let margin_start = inline_start_margin.specified_or_zero();
                    let margin_end = inline_end_margin.specified_or_zero();
                    let sum = inline_end + inline_size + margin_start + margin_end;
                    (
                        available_inline_size - sum,
                        inline_size,
                        margin_start,
                        margin_end,
                    )
                },
                (
                    MaybeAuto::Specified(inline_start),
                    MaybeAuto::Auto,
                    MaybeAuto::Specified(inline_size),
                ) => {
                    let margin_start = inline_start_margin.specified_or_zero();
                    let margin_end = inline_end_margin.specified_or_zero();
                    (inline_start, inline_size, margin_start, margin_end)
                },
                (
                    MaybeAuto::Specified(inline_start),
                    MaybeAuto::Specified(inline_end),
                    MaybeAuto::Auto,
                ) => {
                    let margin_start = inline_start_margin.specified_or_zero();
                    let margin_end = inline_end_margin.specified_or_zero();
                    let sum = inline_start + inline_end + margin_start + margin_end;
                    (
                        inline_start,
                        available_inline_size - sum,
                        margin_start,
                        margin_end,
                    )
                },

                // If inline-size is auto, then inline-size is shrink-to-fit. Solve for the
                // non-auto value.
                (MaybeAuto::Specified(inline_start), MaybeAuto::Auto, MaybeAuto::Auto) => {
                    let margin_start = inline_start_margin.specified_or_zero();
                    let margin_end = inline_end_margin.specified_or_zero();
                    // Set inline-end to zero to calculate inline-size
                    let inline_size = block.get_shrink_to_fit_inline_size(
                        available_inline_size - (margin_start + margin_end),
                    );
                    (inline_start, inline_size, margin_start, margin_end)
                },
                (MaybeAuto::Auto, MaybeAuto::Specified(inline_end), MaybeAuto::Auto) => {
                    let margin_start = inline_start_margin.specified_or_zero();
                    let margin_end = inline_end_margin.specified_or_zero();
                    // Set inline-start to zero to calculate inline-size
                    let inline_size = block.get_shrink_to_fit_inline_size(
                        available_inline_size - (margin_start + margin_end),
                    );
                    let sum = inline_end + inline_size + margin_start + margin_end;
                    (
                        available_inline_size - sum,
                        inline_size,
                        margin_start,
                        margin_end,
                    )
                },

                (MaybeAuto::Auto, MaybeAuto::Auto, MaybeAuto::Specified(inline_size)) => {
                    let margin_start = inline_start_margin.specified_or_zero();
                    let margin_end = inline_end_margin.specified_or_zero();
                    // Setting 'inline-start' to static position because direction is 'ltr'.
                    // TODO: Handle 'rtl' when it is implemented.
                    (Au(0), inline_size, margin_start, margin_end)
                },
            };
        ISizeConstraintSolution::for_absolute_flow(
            inline_start,
            inline_size,
            margin_inline_start,
            margin_inline_end,
        )
    }

    fn containing_block_inline_size(
        &self,
        block: &mut BlockFlow,
        _: Au,
        shared_context: &SharedStyleContext,
    ) -> Au {
        let opaque_block = OpaqueFlow::from_flow(block);
        block
            .containing_block_size(&shared_context.viewport_size(), opaque_block)
            .inline
    }

    fn set_inline_position_of_flow_if_necessary(
        &self,
        block: &mut BlockFlow,
        solution: ISizeConstraintSolution,
    ) {
        // Set the inline position of the absolute flow wrt to its containing block.
        if !block
            .base
            .flags
            .contains(FlowFlags::INLINE_POSITION_IS_STATIC)
        {
            block.base.position.start.i = solution.inline_start;
        }
    }
}

impl ISizeAndMarginsComputer for AbsoluteReplaced {
    /// Solve the horizontal constraint equation for absolute replaced elements.
    ///
    /// CSS Section 10.3.8
    /// Constraint equation:
    /// inline-start + inline-end + inline-size + margin-inline-start + margin-inline-end
    /// = absolute containing block inline-size - (horizontal padding and border)
    /// [aka available_inline-size]
    ///
    /// Return the solution for the equation.
    fn solve_inline_size_constraints(
        &self,
        _: &mut BlockFlow,
        input: &ISizeConstraintInput,
    ) -> ISizeConstraintSolution {
        let &ISizeConstraintInput {
            computed_inline_size,
            inline_start_margin,
            inline_end_margin,
            inline_start,
            inline_end,
            available_inline_size,
            ..
        } = input;
        // TODO: Check for direction of static-position Containing Block (aka
        // parent flow, _not_ the actual Containing Block) when right-to-left
        // is implemented
        // Assume direction is 'ltr' for now
        // TODO: Handle all the cases for 'rtl' direction.

        let inline_size = match computed_inline_size {
            MaybeAuto::Specified(w) => w,
            _ => panic!(
                "{} {}",
                "The used value for inline_size for absolute replaced flow",
                "should have already been calculated by now."
            ),
        };

        let (inline_start, inline_size, margin_inline_start, margin_inline_end) =
            match (inline_start, inline_end) {
                (MaybeAuto::Auto, MaybeAuto::Auto) => {
                    let margin_start = inline_start_margin.specified_or_zero();
                    let margin_end = inline_end_margin.specified_or_zero();
                    (Au(0), inline_size, margin_start, margin_end)
                },
                // If only one is Auto, solve for it
                (MaybeAuto::Auto, MaybeAuto::Specified(inline_end)) => {
                    let margin_start = inline_start_margin.specified_or_zero();
                    let margin_end = inline_end_margin.specified_or_zero();
                    let sum = inline_end + inline_size + margin_start + margin_end;
                    (
                        available_inline_size - sum,
                        inline_size,
                        margin_start,
                        margin_end,
                    )
                },
                (MaybeAuto::Specified(inline_start), MaybeAuto::Auto) => {
                    let margin_start = inline_start_margin.specified_or_zero();
                    let margin_end = inline_end_margin.specified_or_zero();
                    (inline_start, inline_size, margin_start, margin_end)
                },
                (MaybeAuto::Specified(inline_start), MaybeAuto::Specified(inline_end)) => {
                    match (inline_start_margin, inline_end_margin) {
                        (MaybeAuto::Auto, MaybeAuto::Auto) => {
                            let total_margin_val =
                                available_inline_size - inline_start - inline_end - inline_size;
                            if total_margin_val < Au(0) {
                                // margin-inline-start becomes 0 because direction is 'ltr'.
                                (inline_start, inline_size, Au(0), total_margin_val)
                            } else {
                                // Equal margins
                                (
                                    inline_start,
                                    inline_size,
                                    total_margin_val.scale_by(0.5),
                                    total_margin_val.scale_by(0.5),
                                )
                            }
                        },
                        (MaybeAuto::Specified(margin_start), MaybeAuto::Auto) => {
                            let sum = inline_start + inline_end + inline_size + margin_start;
                            (
                                inline_start,
                                inline_size,
                                margin_start,
                                available_inline_size - sum,
                            )
                        },
                        (MaybeAuto::Auto, MaybeAuto::Specified(margin_end)) => {
                            let sum = inline_start + inline_end + inline_size + margin_end;
                            (
                                inline_start,
                                inline_size,
                                available_inline_size - sum,
                                margin_end,
                            )
                        },
                        (MaybeAuto::Specified(margin_start), MaybeAuto::Specified(margin_end)) => {
                            // Values are over-constrained.
                            // Ignore value for 'inline-end' cos direction is 'ltr'.
                            (inline_start, inline_size, margin_start, margin_end)
                        },
                    }
                },
            };
        ISizeConstraintSolution::for_absolute_flow(
            inline_start,
            inline_size,
            margin_inline_start,
            margin_inline_end,
        )
    }

    /// Calculate used value of inline-size just like we do for inline replaced elements.
    fn initial_computed_inline_size(
        &self,
        block: &mut BlockFlow,
        _: Au,
        shared_context: &SharedStyleContext,
    ) -> MaybeAuto {
        let opaque_block = OpaqueFlow::from_flow(block);
        let containing_block_inline_size = block
            .containing_block_size(&shared_context.viewport_size(), opaque_block)
            .inline;
        let container_block_size = block.explicit_block_containing_size(shared_context);
        let fragment = block.fragment();
        fragment.assign_replaced_inline_size_if_necessary(
            containing_block_inline_size,
            container_block_size,
        );
        // For replaced absolute flow, the rest of the constraint solving will
        // take inline-size to be specified as the value computed here.
        MaybeAuto::Specified(fragment.content_box().size.inline)
    }

    fn containing_block_inline_size(
        &self,
        block: &mut BlockFlow,
        _: Au,
        shared_context: &SharedStyleContext,
    ) -> Au {
        let opaque_block = OpaqueFlow::from_flow(block);
        block
            .containing_block_size(&shared_context.viewport_size(), opaque_block)
            .inline
    }

    fn set_inline_position_of_flow_if_necessary(
        &self,
        block: &mut BlockFlow,
        solution: ISizeConstraintSolution,
    ) {
        // Set the x-coordinate of the absolute flow wrt to its containing block.
        block.base.position.start.i = solution.inline_start;
    }
}

impl ISizeAndMarginsComputer for BlockNonReplaced {
    /// Compute inline-start and inline-end margins and inline-size.
    fn solve_inline_size_constraints(
        &self,
        block: &mut BlockFlow,
        input: &ISizeConstraintInput,
    ) -> ISizeConstraintSolution {
        self.solve_block_inline_size_constraints(block, input)
    }
}

impl ISizeAndMarginsComputer for BlockReplaced {
    /// Compute inline-start and inline-end margins and inline-size.
    ///
    /// ISize has already been calculated. We now calculate the margins just
    /// like for non-replaced blocks.
    fn solve_inline_size_constraints(
        &self,
        block: &mut BlockFlow,
        input: &ISizeConstraintInput,
    ) -> ISizeConstraintSolution {
        match input.computed_inline_size {
            MaybeAuto::Specified(_) => {},
            MaybeAuto::Auto => {
                panic!("BlockReplaced: inline_size should have been computed by now")
            },
        };
        self.solve_block_inline_size_constraints(block, input)
    }

    /// Calculate used value of inline-size just like we do for inline replaced elements.
    fn initial_computed_inline_size(
        &self,
        block: &mut BlockFlow,
        parent_flow_inline_size: Au,
        shared_context: &SharedStyleContext,
    ) -> MaybeAuto {
        let container_block_size = block.explicit_block_containing_size(shared_context);
        let fragment = block.fragment();
        fragment.assign_replaced_inline_size_if_necessary(
            parent_flow_inline_size,
            container_block_size,
        );
        // For replaced block flow, the rest of the constraint solving will
        // take inline-size to be specified as the value computed here.
        MaybeAuto::Specified(fragment.content_box().size.inline)
    }
}

impl ISizeAndMarginsComputer for FloatNonReplaced {
    /// CSS Section 10.3.5
    ///
    /// If inline-size is computed as 'auto', the used value is the 'shrink-to-fit' inline-size.
    fn solve_inline_size_constraints(
        &self,
        block: &mut BlockFlow,
        input: &ISizeConstraintInput,
    ) -> ISizeConstraintSolution {
        let (computed_inline_size, inline_start_margin, inline_end_margin, available_inline_size) = (
            input.computed_inline_size,
            input.inline_start_margin,
            input.inline_end_margin,
            input.available_inline_size,
        );
        let margin_inline_start = inline_start_margin.specified_or_zero();
        let margin_inline_end = inline_end_margin.specified_or_zero();
        let available_inline_size_float =
            available_inline_size - margin_inline_start - margin_inline_end;
        let shrink_to_fit = block.get_shrink_to_fit_inline_size(available_inline_size_float);
        let inline_size = computed_inline_size.specified_or_default(shrink_to_fit);
        debug!(
            "assign_inline_sizes_float -- inline_size: {:?}",
            inline_size
        );
        ISizeConstraintSolution::new(inline_size, margin_inline_start, margin_inline_end)
    }
}

impl ISizeAndMarginsComputer for FloatReplaced {
    /// CSS Section 10.3.5
    ///
    /// If inline-size is computed as 'auto', the used value is the 'shrink-to-fit' inline-size.
    fn solve_inline_size_constraints(
        &self,
        _: &mut BlockFlow,
        input: &ISizeConstraintInput,
    ) -> ISizeConstraintSolution {
        let (computed_inline_size, inline_start_margin, inline_end_margin) = (
            input.computed_inline_size,
            input.inline_start_margin,
            input.inline_end_margin,
        );
        let margin_inline_start = inline_start_margin.specified_or_zero();
        let margin_inline_end = inline_end_margin.specified_or_zero();
        let inline_size = match computed_inline_size {
            MaybeAuto::Specified(w) => w,
            MaybeAuto::Auto => {
                panic!("FloatReplaced: inline_size should have been computed by now")
            },
        };
        debug!(
            "assign_inline_sizes_float -- inline_size: {:?}",
            inline_size
        );
        ISizeConstraintSolution::new(inline_size, margin_inline_start, margin_inline_end)
    }

    /// Calculate used value of inline-size just like we do for inline replaced elements.
    fn initial_computed_inline_size(
        &self,
        block: &mut BlockFlow,
        parent_flow_inline_size: Au,
        shared_context: &SharedStyleContext,
    ) -> MaybeAuto {
        let container_block_size = block.explicit_block_containing_size(shared_context);
        let fragment = block.fragment();
        fragment.assign_replaced_inline_size_if_necessary(
            parent_flow_inline_size,
            container_block_size,
        );
        // For replaced block flow, the rest of the constraint solving will
        // take inline-size to be specified as the value computed here.
        MaybeAuto::Specified(fragment.content_box().size.inline)
    }
}

impl ISizeAndMarginsComputer for InlineBlockNonReplaced {
    /// Compute inline-start and inline-end margins and inline-size.
    fn solve_inline_size_constraints(
        &self,
        block: &mut BlockFlow,
        input: &ISizeConstraintInput,
    ) -> ISizeConstraintSolution {
        let (computed_inline_size, inline_start_margin, inline_end_margin, available_inline_size) = (
            input.computed_inline_size,
            input.inline_start_margin,
            input.inline_end_margin,
            input.available_inline_size,
        );

        // For inline-blocks, `auto` margins compute to 0.
        let inline_start_margin = inline_start_margin.specified_or_zero();
        let inline_end_margin = inline_end_margin.specified_or_zero();

        // If inline-size is set to 'auto', and this is an inline block, use the
        // shrink to fit algorithm (see CSS 2.1 § 10.3.9)
        let inline_size = match computed_inline_size {
            MaybeAuto::Auto => block.get_shrink_to_fit_inline_size(
                available_inline_size - (inline_start_margin + inline_end_margin),
            ),
            MaybeAuto::Specified(inline_size) => inline_size,
        };

        ISizeConstraintSolution::new(inline_size, inline_start_margin, inline_end_margin)
    }
}

impl ISizeAndMarginsComputer for InlineBlockReplaced {
    /// Compute inline-start and inline-end margins and inline-size.
    ///
    /// ISize has already been calculated. We now calculate the margins just
    /// like for non-replaced blocks.
    fn solve_inline_size_constraints(
        &self,
        block: &mut BlockFlow,
        input: &ISizeConstraintInput,
    ) -> ISizeConstraintSolution {
        debug_assert!(match input.computed_inline_size {
            MaybeAuto::Specified(_) => true,
            MaybeAuto::Auto => false,
        });

        let (computed_inline_size, inline_start_margin, inline_end_margin, available_inline_size) = (
            input.computed_inline_size,
            input.inline_start_margin,
            input.inline_end_margin,
            input.available_inline_size,
        );

        // For inline-blocks, `auto` margins compute to 0.
        let inline_start_margin = inline_start_margin.specified_or_zero();
        let inline_end_margin = inline_end_margin.specified_or_zero();

        // If inline-size is set to 'auto', and this is an inline block, use the
        // shrink to fit algorithm (see CSS 2.1 § 10.3.9)
        let inline_size = match computed_inline_size {
            MaybeAuto::Auto => block.get_shrink_to_fit_inline_size(
                available_inline_size - (inline_start_margin + inline_end_margin),
            ),
            MaybeAuto::Specified(inline_size) => inline_size,
        };

        ISizeConstraintSolution::new(inline_size, inline_start_margin, inline_end_margin)
    }

    /// Calculate used value of inline-size just like we do for inline replaced elements.
    fn initial_computed_inline_size(
        &self,
        block: &mut BlockFlow,
        parent_flow_inline_size: Au,
        shared_context: &SharedStyleContext,
    ) -> MaybeAuto {
        let container_block_size = block.explicit_block_containing_size(shared_context);
        let fragment = block.fragment();
        fragment.assign_replaced_inline_size_if_necessary(
            parent_flow_inline_size,
            container_block_size,
        );
        // For replaced block flow, the rest of the constraint solving will
        // take inline-size to be specified as the value computed here.
        MaybeAuto::Specified(fragment.content_box().size.inline)
    }
}

impl ISizeAndMarginsComputer for InlineFlexItem {
    // Replace the default method directly to prevent recalculating and setting margins again
    // which has already been set by its parent.
    fn compute_used_inline_size(
        &self,
        block: &mut BlockFlow,
        shared_context: &SharedStyleContext,
        parent_flow_inline_size: Au,
    ) {
        let container_block_size = block.explicit_block_containing_size(shared_context);
        block.fragment.assign_replaced_inline_size_if_necessary(
            parent_flow_inline_size,
            container_block_size,
        );
    }

    // The used inline size and margins are set by parent flex flow, do nothing here.
    fn solve_inline_size_constraints(
        &self,
        block: &mut BlockFlow,
        _: &ISizeConstraintInput,
    ) -> ISizeConstraintSolution {
        let fragment = block.fragment();
        ISizeConstraintSolution::new(
            fragment.border_box.size.inline,
            fragment.margin.inline_start,
            fragment.margin.inline_end,
        )
    }
}