summaryrefslogtreecommitdiffstats
path: root/layout/base/nsCSSFrameConstructor.cpp
blob: 149f2f24bf9ce201761bea8d2b0bee2a3caac030 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 http://mozilla.org/MPL/2.0/. */

/*
 * construction of a frame tree that is nearly isomorphic to the content
 * tree and updating of that tree in response to dynamic changes
 */

#include "nsCSSFrameConstructor.h"

#include "mozilla/AutoRestore.h"
#include "mozilla/ComputedStyleInlines.h"
#include "mozilla/DebugOnly.h"
#include "mozilla/ErrorResult.h"
#include "mozilla/ManualNAC.h"
#include "mozilla/dom/BindContext.h"
#include "mozilla/dom/BrowsingContext.h"
#include "mozilla/dom/GeneratedImageContent.h"
#include "mozilla/dom/HTMLSelectElement.h"
#include "mozilla/dom/HTMLSharedListElement.h"
#include "mozilla/dom/HTMLSummaryElement.h"
#include "mozilla/Likely.h"
#include "mozilla/LinkedList.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/PresShell.h"
#include "mozilla/PresShellInlines.h"
#include "mozilla/PrintedSheetFrame.h"
#include "mozilla/ProfilerLabels.h"
#include "mozilla/ProfilerMarkers.h"
#include "mozilla/ScopeExit.h"
#include "mozilla/ServoBindings.h"
#include "mozilla/ServoStyleSetInlines.h"
#include "mozilla/StaticPrefs_browser.h"
#include "mozilla/StaticPrefs_layout.h"
#include "mozilla/StaticPrefs_mathml.h"
#include "mozilla/Unused.h"
#include "RetainedDisplayListBuilder.h"
#include "nsAbsoluteContainingBlock.h"
#include "nsCSSPseudoElements.h"
#include "nsCheckboxRadioFrame.h"
#include "nsCRT.h"
#include "nsAtom.h"
#include "nsIFrameInlines.h"
#include "nsGkAtoms.h"
#include "nsPresContext.h"
#include "mozilla/dom/Document.h"
#include "mozilla/dom/DocumentInlines.h"
#include "nsTableFrame.h"
#include "nsTableColFrame.h"
#include "nsTableRowFrame.h"
#include "nsTableCellFrame.h"
#include "nsFileControlFrame.h"
#include "nsHTMLParts.h"
#include "nsUnicharUtils.h"
#include "nsViewManager.h"
#include "nsStyleConsts.h"
#include "nsXULElement.h"
#include "nsContainerFrame.h"
#include "nsNameSpaceManager.h"
#include "nsComboboxControlFrame.h"
#include "nsListControlFrame.h"
#include "nsPlaceholderFrame.h"
#include "nsTableRowGroupFrame.h"
#include "nsIFormControl.h"
#include "nsCSSAnonBoxes.h"
#include "nsTextFragment.h"
#include "nsIAnonymousContentCreator.h"
#include "nsContentUtils.h"
#include "nsIScriptError.h"
#ifdef XP_MACOSX
#  include "nsIDocShell.h"
#endif
#include "ChildIterator.h"
#include "nsError.h"
#include "nsLayoutUtils.h"
#include "nsFlexContainerFrame.h"
#include "nsGridContainerFrame.h"
#include "RubyUtils.h"
#include "nsRubyFrame.h"
#include "nsRubyBaseFrame.h"
#include "nsRubyBaseContainerFrame.h"
#include "nsRubyTextFrame.h"
#include "nsRubyTextContainerFrame.h"
#include "nsImageFrame.h"
#include "nsIObjectLoadingContent.h"
#include "nsTArray.h"
#include "mozilla/dom/CharacterData.h"
#include "mozilla/dom/Element.h"
#include "mozilla/dom/ElementInlines.h"
#include "mozilla/dom/HTMLInputElement.h"
#include "nsAutoLayoutPhase.h"
#include "nsStyleStructInlines.h"
#include "nsPageContentFrame.h"
#include "mozilla/RestyleManager.h"
#include "StickyScrollContainer.h"
#include "nsFieldSetFrame.h"
#include "nsInlineFrame.h"
#include "nsBlockFrame.h"
#include "nsCanvasFrame.h"
#include "nsFirstLetterFrame.h"
#include "nsGfxScrollFrame.h"
#include "nsPageFrame.h"
#include "nsPageSequenceFrame.h"
#include "nsTableWrapperFrame.h"
#include "nsIScrollableFrame.h"
#include "nsBackdropFrame.h"
#include "nsTransitionManager.h"

#include "nsIPopupContainer.h"
#ifdef ACCESSIBILITY
#  include "nsAccessibilityService.h"
#endif

#undef NOISY_FIRST_LETTER

#include "nsMathMLParts.h"
#include "mozilla/SVGGradientFrame.h"

#include "nsRefreshDriver.h"
#include "nsTextNode.h"
#include "ActiveLayerTracker.h"

using namespace mozilla;
using namespace mozilla::dom;

nsIFrame* NS_NewHTMLCanvasFrame(PresShell* aPresShell, ComputedStyle* aStyle);

nsIFrame* NS_NewHTMLVideoFrame(PresShell* aPresShell, ComputedStyle* aStyle);
nsIFrame* NS_NewHTMLAudioFrame(PresShell* aPresShell, ComputedStyle* aStyle);

nsContainerFrame* NS_NewSVGOuterSVGFrame(PresShell* aPresShell,
                                         ComputedStyle* aStyle);
nsContainerFrame* NS_NewSVGOuterSVGAnonChildFrame(PresShell* aPresShell,
                                                  ComputedStyle* aStyle);
nsIFrame* NS_NewSVGInnerSVGFrame(PresShell* aPresShell, ComputedStyle* aStyle);
nsIFrame* NS_NewSVGGeometryFrame(PresShell* aPresShell, ComputedStyle* aStyle);
nsIFrame* NS_NewSVGGFrame(PresShell* aPresShell, ComputedStyle* aStyle);
nsContainerFrame* NS_NewSVGForeignObjectFrame(PresShell* aPresShell,
                                              ComputedStyle* aStyle);
nsIFrame* NS_NewSVGAFrame(PresShell* aPresShell, ComputedStyle* aStyle);
nsIFrame* NS_NewSVGSwitchFrame(PresShell* aPresShell, ComputedStyle* aStyle);
nsIFrame* NS_NewSVGSymbolFrame(PresShell* aPresShell, ComputedStyle* aStyle);
nsIFrame* NS_NewSVGTextFrame(PresShell* aPresShell, ComputedStyle* aStyle);
nsIFrame* NS_NewSVGContainerFrame(PresShell* aPresShell, ComputedStyle* aStyle);
nsIFrame* NS_NewSVGUseFrame(PresShell* aPresShell, ComputedStyle* aStyle);
nsIFrame* NS_NewSVGViewFrame(PresShell* aPresShell, ComputedStyle* aStyle);
extern nsIFrame* NS_NewSVGLinearGradientFrame(PresShell* aPresShell,
                                              ComputedStyle* aStyle);
extern nsIFrame* NS_NewSVGRadialGradientFrame(PresShell* aPresShell,
                                              ComputedStyle* aStyle);
extern nsIFrame* NS_NewSVGStopFrame(PresShell* aPresShell,
                                    ComputedStyle* aStyle);
nsContainerFrame* NS_NewSVGMarkerFrame(PresShell* aPresShell,
                                       ComputedStyle* aStyle);
nsContainerFrame* NS_NewSVGMarkerAnonChildFrame(PresShell* aPresShell,
                                                ComputedStyle* aStyle);
extern nsIFrame* NS_NewSVGImageFrame(PresShell* aPresShell,
                                     ComputedStyle* aStyle);
nsIFrame* NS_NewSVGClipPathFrame(PresShell* aPresShell, ComputedStyle* aStyle);
nsIFrame* NS_NewSVGFilterFrame(PresShell* aPresShell, ComputedStyle* aStyle);
nsIFrame* NS_NewSVGPatternFrame(PresShell* aPresShell, ComputedStyle* aStyle);
nsIFrame* NS_NewSVGMaskFrame(PresShell* aPresShell, ComputedStyle* aStyle);
nsIFrame* NS_NewSVGFEContainerFrame(PresShell* aPresShell,
                                    ComputedStyle* aStyle);
nsIFrame* NS_NewSVGFELeafFrame(PresShell* aPresShell, ComputedStyle* aStyle);
nsIFrame* NS_NewSVGFEImageFrame(PresShell* aPresShell, ComputedStyle* aStyle);
nsIFrame* NS_NewSVGFEUnstyledLeafFrame(PresShell* aPresShell,
                                       ComputedStyle* aStyle);
nsIFrame* NS_NewFileControlLabelFrame(PresShell*, ComputedStyle*);
nsIFrame* NS_NewComboboxLabelFrame(PresShell*, ComputedStyle*);
nsIFrame* NS_NewMiddleCroppingLabelFrame(PresShell*, ComputedStyle*);

#include "mozilla/dom/NodeInfo.h"
#include "prenv.h"
#include "nsNodeInfoManager.h"
#include "nsContentCreatorFunctions.h"

#ifdef DEBUG
// Set the environment variable GECKO_FRAMECTOR_DEBUG_FLAGS to one or
// more of the following flags (comma separated) for handy debug
// output.
static bool gNoisyContentUpdates = false;
static bool gReallyNoisyContentUpdates = false;
static bool gNoisyInlineConstruction = false;

struct FrameCtorDebugFlags {
  const char* name;
  bool* on;
};

static FrameCtorDebugFlags gFlags[] = {
    {"content-updates", &gNoisyContentUpdates},
    {"really-noisy-content-updates", &gReallyNoisyContentUpdates},
    {"noisy-inline", &gNoisyInlineConstruction}};

#  define NUM_DEBUG_FLAGS (sizeof(gFlags) / sizeof(gFlags[0]))
#endif

//------------------------------------------------------------------

nsIFrame* NS_NewLeafBoxFrame(PresShell* aPresShell, ComputedStyle* aStyle);

nsIFrame* NS_NewRangeFrame(PresShell* aPresShell, ComputedStyle* aStyle);

nsIFrame* NS_NewTextBoxFrame(PresShell* aPresShell, ComputedStyle* aStyle);

nsIFrame* NS_NewSplitterFrame(PresShell* aPresShell, ComputedStyle* aStyle);

nsIFrame* NS_NewMenuPopupFrame(PresShell* aPresShell, ComputedStyle* aStyle);

nsIFrame* NS_NewTreeBodyFrame(PresShell* aPresShell, ComputedStyle* aStyle);

nsHTMLScrollFrame* NS_NewHTMLScrollFrame(PresShell* aPresShell,
                                         ComputedStyle* aStyle, bool aIsRoot);

nsIFrame* NS_NewSliderFrame(PresShell* aPresShell, ComputedStyle* aStyle);

nsIFrame* NS_NewScrollbarFrame(PresShell* aPresShell, ComputedStyle* aStyle);

nsIFrame* NS_NewScrollbarButtonFrame(PresShell*, ComputedStyle*);
nsIFrame* NS_NewSimpleXULLeafFrame(PresShell*, ComputedStyle*);

nsIFrame* NS_NewXULImageFrame(PresShell*, ComputedStyle*);
nsIFrame* NS_NewImageFrameForContentProperty(PresShell*, ComputedStyle*);
nsIFrame* NS_NewImageFrameForGeneratedContentIndex(PresShell*, ComputedStyle*);
nsIFrame* NS_NewImageFrameForListStyleImage(PresShell*, ComputedStyle*);

// Returns true if aFrame is an anonymous flex/grid item.
static inline bool IsAnonymousItem(const nsIFrame* aFrame) {
  return aFrame->Style()->GetPseudoType() == PseudoStyleType::anonymousItem;
}

// Returns true IFF the given nsIFrame is a nsFlexContainerFrame and represents
// a -webkit-{inline-}box container.
static inline bool IsFlexContainerForLegacyWebKitBox(const nsIFrame* aFrame) {
  return aFrame->IsFlexContainerFrame() &&
         aFrame->HasAnyStateBits(NS_STATE_FLEX_IS_EMULATING_LEGACY_WEBKIT_BOX);
}

#if DEBUG
static void AssertAnonymousFlexOrGridItemParent(const nsIFrame* aChild,
                                                const nsIFrame* aParent) {
  MOZ_ASSERT(IsAnonymousItem(aChild), "expected an anonymous item child frame");
  MOZ_ASSERT(aParent, "expected a parent frame");
  MOZ_ASSERT(aParent->IsFlexOrGridContainer(),
             "anonymous items should only exist as children of flex/grid "
             "container frames");
}
#else
#  define AssertAnonymousFlexOrGridItemParent(x, y) PR_BEGIN_MACRO PR_END_MACRO
#endif

#define ToCreationFunc(_func)                              \
  [](PresShell* aPs, ComputedStyle* aStyle) -> nsIFrame* { \
    return _func(aPs, aStyle);                             \
  }

/**
 * True if aFrame is an actual inline frame in the sense of non-replaced
 * display:inline CSS boxes.  In other words, it can be affected by {ib}
 * splitting and can contain first-letter frames.  Basically, this is either an
 * inline frame (positioned or otherwise) or an line frame (this last because
 * it can contain first-letter and because inserting blocks in the middle of it
 * needs to terminate it).
 */
static bool IsInlineFrame(const nsIFrame* aFrame) {
  return aFrame->IsLineParticipant();
}

/**
 * True for display: contents elements.
 */
static inline bool IsDisplayContents(const Element* aElement) {
  return aElement->IsDisplayContents();
}

static inline bool IsDisplayContents(const nsIContent* aContent) {
  return aContent->IsElement() && IsDisplayContents(aContent->AsElement());
}

/**
 * True if aFrame is an instance of an SVG frame class or is an inline/block
 * frame being used for SVG text.
 */
static bool IsFrameForSVG(const nsIFrame* aFrame) {
  return aFrame->IsSVGFrame() || aFrame->IsInSVGTextSubtree();
}

static bool IsLastContinuationForColumnContent(const nsIFrame* aFrame) {
  MOZ_ASSERT(aFrame);
  return aFrame->Style()->GetPseudoType() == PseudoStyleType::columnContent &&
         !aFrame->GetNextContinuation();
}

/**
 * Returns true iff aFrame explicitly prevents its descendants from floating
 * (at least, down to the level of descendants which themselves are
 * float-containing blocks -- those will manage the floating status of any
 * lower-level descendents inside them, of course).
 */
static bool ShouldSuppressFloatingOfDescendants(nsIFrame* aFrame) {
  return aFrame->IsFlexOrGridContainer() || aFrame->IsMathMLFrame();
}

// Return true if column-span descendants should be suppressed under aFrame's
// subtree (until a multi-column container re-establishing a block formatting
// context). Basically, this is testing whether aFrame establishes a new block
// formatting context or not.
static bool ShouldSuppressColumnSpanDescendants(nsIFrame* aFrame) {
  if (aFrame->Style()->GetPseudoType() == PseudoStyleType::columnContent) {
    // Never suppress column-span under ::-moz-column-content frames.
    return false;
  }

  if (aFrame->IsInlineFrame()) {
    // Allow inline frames to have column-span block children.
    return false;
  }

  if (!aFrame->IsBlockFrameOrSubclass() ||
      aFrame->HasAnyStateBits(NS_BLOCK_BFC | NS_FRAME_OUT_OF_FLOW) ||
      aFrame->IsFixedPosContainingBlock()) {
    // Need to suppress column-span if we:
    // - Are a different block formatting context,
    // - Are an out-of-flow frame, OR
    // - Establish a containing block for fixed-position descendants
    //
    // For example, the children of a column-span never need to be further
    // processed even if there is a nested column-span child. Because a
    // column-span always creates its own block formatting context, a nested
    // column-span child won't be in the same block formatting context with the
    // nearest multi-column ancestor. This is the same case as if the
    // column-span is outside of a multi-column hierarchy.
    return true;
  }

  return false;
}

// Reparent a frame into a wrapper frame that is a child of its old parent.
static void ReparentFrame(RestyleManager* aRestyleManager,
                          nsContainerFrame* aNewParentFrame, nsIFrame* aFrame,
                          bool aForceStyleReparent) {
  aFrame->SetParent(aNewParentFrame);
  // We reparent frames for two reasons: to put them inside ::first-line, and to
  // put them inside some wrapper anonymous boxes.
  if (aForceStyleReparent) {
    aRestyleManager->ReparentComputedStyleForFirstLine(aFrame);
  }
}

static void ReparentFrames(nsCSSFrameConstructor* aFrameConstructor,
                           nsContainerFrame* aNewParentFrame,
                           const nsFrameList& aFrameList,
                           bool aForceStyleReparent) {
  RestyleManager* restyleManager = aFrameConstructor->RestyleManager();
  for (nsIFrame* f : aFrameList) {
    ReparentFrame(restyleManager, aNewParentFrame, f, aForceStyleReparent);
  }
}

//----------------------------------------------------------------------
//
// When inline frames get weird and have block frames in them, we
// annotate them to help us respond to incremental content changes
// more easily.

static inline bool IsFramePartOfIBSplit(nsIFrame* aFrame) {
  bool result = aFrame->HasAnyStateBits(NS_FRAME_PART_OF_IBSPLIT);
  MOZ_ASSERT(!result || static_cast<nsBlockFrame*>(do_QueryFrame(aFrame)) ||
                 static_cast<nsInlineFrame*>(do_QueryFrame(aFrame)),
             "only block/inline frames can have NS_FRAME_PART_OF_IBSPLIT");
  return result;
}

static nsContainerFrame* GetIBSplitSibling(nsIFrame* aFrame) {
  MOZ_ASSERT(IsFramePartOfIBSplit(aFrame), "Shouldn't call this");

  // We only store the "ib-split sibling" annotation with the first
  // frame in the continuation chain. Walk back to find that frame now.
  return aFrame->FirstContinuation()->GetProperty(nsIFrame::IBSplitSibling());
}

static nsContainerFrame* GetIBSplitPrevSibling(nsIFrame* aFrame) {
  MOZ_ASSERT(IsFramePartOfIBSplit(aFrame), "Shouldn't call this");

  // We only store the ib-split sibling annotation with the first
  // frame in the continuation chain. Walk back to find that frame now.
  return aFrame->FirstContinuation()->GetProperty(
      nsIFrame::IBSplitPrevSibling());
}

static nsContainerFrame* GetLastIBSplitSibling(nsIFrame* aFrame) {
  for (nsIFrame *frame = aFrame, *next;; frame = next) {
    next = GetIBSplitSibling(frame);
    if (!next) {
      return static_cast<nsContainerFrame*>(frame);
    }
  }
  MOZ_ASSERT_UNREACHABLE("unreachable code");
  return nullptr;
}

static void SetFrameIsIBSplit(nsContainerFrame* aFrame,
                              nsContainerFrame* aIBSplitSibling) {
  MOZ_ASSERT(aFrame, "bad args!");

  // We should be the only continuation
  NS_ASSERTION(!aFrame->GetPrevContinuation(),
               "assigning ib-split sibling to other than first continuation!");
  NS_ASSERTION(!aFrame->GetNextContinuation() ||
                   IsFramePartOfIBSplit(aFrame->GetNextContinuation()),
               "should have no non-ib-split continuations here");

  // Mark the frame as ib-split.
  aFrame->AddStateBits(NS_FRAME_PART_OF_IBSPLIT);

  if (aIBSplitSibling) {
    NS_ASSERTION(!aIBSplitSibling->GetPrevContinuation(),
                 "assigning something other than the first continuation as the "
                 "ib-split sibling");

    // Store the ib-split sibling (if we were given one) with the
    // first frame in the flow.
    aFrame->SetProperty(nsIFrame::IBSplitSibling(), aIBSplitSibling);
    aIBSplitSibling->SetProperty(nsIFrame::IBSplitPrevSibling(), aFrame);
  }
}

static nsIFrame* GetIBContainingBlockFor(nsIFrame* aFrame) {
  MOZ_ASSERT(
      IsFramePartOfIBSplit(aFrame),
      "GetIBContainingBlockFor() should only be called on known IB frames");

  // Get the first "normal" ancestor of the target frame.
  nsIFrame* parentFrame;
  do {
    parentFrame = aFrame->GetParent();

    if (!parentFrame) {
      NS_ERROR("no unsplit block frame in IB hierarchy");
      return aFrame;
    }

    // Note that we ignore non-ib-split frames which have a pseudo on their
    // ComputedStyle -- they're not the frames we're looking for!  In
    // particular, they may be hiding a real parent that _is_ in an ib-split.
    if (!IsFramePartOfIBSplit(parentFrame) &&
        !parentFrame->Style()->IsPseudoOrAnonBox())
      break;

    aFrame = parentFrame;
  } while (1);

  // post-conditions
  NS_ASSERTION(parentFrame,
               "no normal ancestor found for ib-split frame "
               "in GetIBContainingBlockFor");
  NS_ASSERTION(parentFrame != aFrame,
               "parentFrame is actually the child frame - bogus reslt");

  return parentFrame;
}

// Find the multicol containing block suitable for reframing.
//
// Note: this function may not return a ColumnSetWrapperFrame. For example, if
// the multicol containing block has "overflow:scroll" style, HTMLScrollFrame is
// returned because ColumnSetWrapperFrame is the scrolled frame which has the
// -moz-scrolled-content pseudo style. We may walk up "too far", but in terms of
// correctness of reframing, it's OK.
static nsContainerFrame* GetMultiColumnContainingBlockFor(nsIFrame* aFrame) {
  MOZ_ASSERT(aFrame->HasAnyStateBits(NS_FRAME_HAS_MULTI_COLUMN_ANCESTOR),
             "Should only be called if the frame has a multi-column ancestor!");

  nsContainerFrame* current = aFrame->GetParent();
  while (current &&
         (current->HasAnyStateBits(NS_FRAME_HAS_MULTI_COLUMN_ANCESTOR) ||
          current->Style()->IsPseudoOrAnonBox())) {
    current = current->GetParent();
  }

  MOZ_ASSERT(current,
             "No multicol containing block in a valid column hierarchy?");

  return current;
}

// This is a bit slow, but sometimes we need it.
static bool ParentIsWrapperAnonBox(nsIFrame* aParent) {
  nsIFrame* maybeAnonBox = aParent;
  if (maybeAnonBox->Style()->GetPseudoType() == PseudoStyleType::cellContent) {
    // The thing that would maybe be a wrapper anon box is the cell.
    maybeAnonBox = maybeAnonBox->GetParent();
  }
  return maybeAnonBox->Style()->IsWrapperAnonBox();
}

static bool InsertSeparatorBeforeAccessKey() {
  static bool sInitialized = false;
  static bool sValue = false;
  if (!sInitialized) {
    sInitialized = true;

    const char* prefName = "intl.menuitems.insertseparatorbeforeaccesskeys";
    nsAutoString val;
    Preferences::GetLocalizedString(prefName, val);
    sValue = val.EqualsLiteral("true");
  }
  return sValue;
}

static bool AlwaysAppendAccessKey() {
  static bool sInitialized = false;
  static bool sValue = false;
  if (!sInitialized) {
    sInitialized = true;
    const char* prefName = "intl.menuitems.alwaysappendaccesskeys";
    nsAutoString val;
    Preferences::GetLocalizedString(prefName, val);
    sValue = val.EqualsLiteral("true");
  }
  return sValue;
}

//----------------------------------------------------------------------

// Block/inline frame construction logic. We maintain a few invariants here:
//
// 1. Block frames contain block and inline frames.
//
// 2. Inline frames only contain inline frames. If an inline parent has a block
// child then the block child is migrated upward until it lands in a block
// parent (the inline frames containing block is where it will end up).

inline void SetInitialSingleChild(nsContainerFrame* aParent, nsIFrame* aFrame) {
  MOZ_ASSERT(!aFrame->GetNextSibling(), "Should be using a frame list");
  aParent->SetInitialChildList(FrameChildListID::Principal,
                               nsFrameList(aFrame, aFrame));
}

// -----------------------------------------------------------

// Structure used when constructing formatting object trees. Contains
// state information needed for absolutely positioned elements
namespace mozilla {
struct AbsoluteFrameList final : public nsFrameList {
  // Containing block for absolutely positioned elements.
  nsContainerFrame* mContainingBlock;

  explicit AbsoluteFrameList(nsContainerFrame* aContainingBlock = nullptr)
      : mContainingBlock(aContainingBlock) {}

  // Transfer frames in aOther to this list. aOther becomes empty after this
  // operation.
  AbsoluteFrameList(AbsoluteFrameList&& aOther) = default;
  AbsoluteFrameList& operator=(AbsoluteFrameList&& aOther) = default;

#ifdef DEBUG
  // XXXbz Does this need a debug-only assignment operator that nulls out the
  // childList in the AbsoluteFrameList we're copying?  Introducing a difference
  // between debug and non-debug behavior seems bad, so I guess not...
  ~AbsoluteFrameList() {
    NS_ASSERTION(!FirstChild(),
                 "Dangling child list.  Someone forgot to insert it?");
  }
#endif
};
}  // namespace mozilla

// -----------------------------------------------------------

// Structure for saving the existing state when pushing/poping containing
// blocks. The destructor restores the state to its previous state
class MOZ_STACK_CLASS nsFrameConstructorSaveState {
 public:
  ~nsFrameConstructorSaveState();

 private:
  // Pointer to struct whose data we save/restore.
  AbsoluteFrameList* mList = nullptr;

  // The saved pointer to the fixed list.
  AbsoluteFrameList* mSavedFixedList = nullptr;

  // Copy of original frame list. This can be the original absolute list or a
  // float list.
  AbsoluteFrameList mSavedList;

  // The name of the child list in which our frames would belong.
  mozilla::FrameChildListID mChildListID = FrameChildListID::Principal;
  nsFrameConstructorState* mState = nullptr;

  friend class nsFrameConstructorState;
};

// Structure used for maintaining state information during the
// frame construction process
class MOZ_STACK_CLASS nsFrameConstructorState {
 public:
  nsPresContext* mPresContext;
  PresShell* mPresShell;
  nsCSSFrameConstructor* mFrameConstructor;

  // Containing block information for out-of-flow frames.
  //
  // Floats are easy. Whatever is our float CB.
  //
  // Regular abspos elements are easy too. Its containing block can be the
  // nearest abspos element, or the ICB (the canvas frame).
  //
  // Top layer abspos elements are always children of the ICB, but we can get
  // away with having two different lists (mAbsoluteList and
  // mTopLayerAbsoluteList), because because top layer frames cause
  // non-top-layer frames to be contained inside (so any descendants of a top
  // layer abspos can never share containing block with it, unless they're also
  // in the top layer).
  //
  // Regular fixed elements however are trickier. Fixed elements can be
  // contained in one of three lists:
  //
  //  * mAbsoluteList, if our abspos cb is also a fixpos cb (e.g., is
  //                   transformed or has a filter).
  //
  //  * mAncestorFixedList, if the fixpos cb is an ancestor element other than
  //                        the viewport frame, (so, a transformed / filtered
  //                        ancestor).
  //
  //  * mRealFixedList, which is also the fixed list used for the top layer
  //                    fixed items, which is the fixed list of the viewport
  //                    frame.
  //
  // It is important that mRealFixedList is shared between regular and top layer
  // fixpos elements, since no-top-layer descendants of top layer fixed elements
  // could share ICB and vice versa, so without that there would be no guarantee
  // of layout ordering between them.
  AbsoluteFrameList mFloatedList;
  AbsoluteFrameList mAbsoluteList;
  AbsoluteFrameList mTopLayerAbsoluteList;
  AbsoluteFrameList mAncestorFixedList;
  AbsoluteFrameList mRealFixedList;

  // Never null, always pointing to one of the lists documented above.
  AbsoluteFrameList* mFixedList;

  // What `page: auto` resolves to. This is the used page-name of the parent
  // frame. Updated by AutoFrameConstructionPageName.
  const nsAtom* mAutoPageNameValue = nullptr;

  nsCOMPtr<nsILayoutHistoryState> mFrameState;
  // These bits will be added to the state bits of any frame we construct
  // using this state.
  nsFrameState mAdditionalStateBits{0};

  // If false (which is the default) then call SetPrimaryFrame() as needed
  // during frame construction.  If true, don't make any SetPrimaryFrame()
  // calls, except for generated content which doesn't have a primary frame
  // yet.  The mCreatingExtraFrames == true mode is meant to be used for
  // construction of random "extra" frames for elements via normal frame
  // construction APIs (e.g. replication of things across pages in paginated
  // mode).
  bool mCreatingExtraFrames;

  // This keeps track of whether we have found a "rendered legend" for
  // the current FieldSetFrame.
  bool mHasRenderedLegend;

  nsTArray<RefPtr<nsIContent>> mGeneratedContentWithInitializer;

#ifdef DEBUG
  // Record the float containing block candidate passed into
  // MaybePushFloatContainingBlock() to keep track that we've call the method to
  // handle the float CB scope before processing the CB's children. It is reset
  // in ConstructFramesFromItemList().
  nsContainerFrame* mFloatCBCandidate = nullptr;
#endif

  // Constructor
  // Use the passed-in history state.
  nsFrameConstructorState(
      PresShell* aPresShell, nsContainerFrame* aFixedContainingBlock,
      nsContainerFrame* aAbsoluteContainingBlock,
      nsContainerFrame* aFloatContainingBlock,
      already_AddRefed<nsILayoutHistoryState> aHistoryState);
  // Get the history state from the pres context's pres shell.
  nsFrameConstructorState(PresShell* aPresShell,
                          nsContainerFrame* aFixedContainingBlock,
                          nsContainerFrame* aAbsoluteContainingBlock,
                          nsContainerFrame* aFloatContainingBlock);

  ~nsFrameConstructorState();

  // Process the frame insertions for all the out-of-flow nsAbsoluteItems.
  void ProcessFrameInsertionsForAllLists();

  // Function to push the existing absolute containing block state and
  // create a new scope. Code that uses this function should get matching
  // logic in GetAbsoluteContainingBlock.
  // Also makes aNewAbsoluteContainingBlock the containing block for
  // fixed-pos elements if necessary.
  // aPositionedFrame is the frame whose style actually makes
  // aNewAbsoluteContainingBlock a containing block. E.g. for a scrollable
  // element aPositionedFrame is the element's primary frame and
  // aNewAbsoluteContainingBlock is the scrolled frame.
  void PushAbsoluteContainingBlock(
      nsContainerFrame* aNewAbsoluteContainingBlock, nsIFrame* aPositionedFrame,
      nsFrameConstructorSaveState& aSaveState);

  // Function to forbid floats descendants under aFloatCBCandidate, or open a
  // new float containing block scope for aFloatCBCandidate. The current
  // state is saved in aSaveState if a new scope is pushed.
  void MaybePushFloatContainingBlock(nsContainerFrame* aFloatCBCandidate,
                                     nsFrameConstructorSaveState& aSaveState);

  // Helper function for MaybePushFloatContainingBlock().
  void PushFloatContainingBlock(nsContainerFrame* aNewFloatContainingBlock,
                                nsFrameConstructorSaveState& aSaveState);

  // Function to return the proper geometric parent for a frame with display
  // struct given by aStyleDisplay and parent's frame given by
  // aContentParentFrame.
  nsContainerFrame* GetGeometricParent(
      const nsStyleDisplay& aStyleDisplay,
      nsContainerFrame* aContentParentFrame) const;

  // Collect absolute frames in mAbsoluteList which are proper descendants
  // of aNewParent, and reparent them to aNewParent.
  //
  // Note: This function does something unusual that moves absolute items
  // after their frames are constructed under a column hierarchy which has
  // column-span elements. Do not use this if you're not dealing with
  // columns.
  void ReparentAbsoluteItems(nsContainerFrame* aNewParent);

  // Collect floats in mFloatedList which are proper descendants of aNewParent,
  // and reparent them to aNewParent.
  //
  // Note: This function does something unusual that moves floats after their
  // frames are constructed under a column hierarchy which has column-span
  // elements. Do not use this if you're not dealing with columns.
  void ReparentFloats(nsContainerFrame* aNewParent);

  /**
   * Function to add a new frame to the right frame list.  This MUST be called
   * on frames before their children have been processed if the frames might
   * conceivably be out-of-flow; otherwise cleanup in error cases won't work
   * right.  Also, this MUST be called on frames after they have been
   * initialized.
   * @param aNewFrame the frame to add
   * @param aFrameList the list to add in-flow frames to
   * @param aContent the content pointer for aNewFrame
   * @param aParentFrame the parent frame for the content if it were in-flow
   * @param aCanBePositioned pass false if the frame isn't allowed to be
   *        positioned
   * @param aCanBeFloated pass false if the frame isn't allowed to be
   *        floated
   */
  void AddChild(nsIFrame* aNewFrame, nsFrameList& aFrameList,
                nsIContent* aContent, nsContainerFrame* aParentFrame,
                bool aCanBePositioned = true, bool aCanBeFloated = true,
                bool aInsertAfter = false,
                nsIFrame* aInsertAfterFrame = nullptr);

  /**
   * Function to return the fixed-pos element list.  Normally this will just
   * hand back the fixed-pos element list, but in case we're dealing with a
   * transformed element that's acting as an abs-pos and fixed-pos container,
   * we'll hand back the abs-pos list.  Callers should use this function if they
   * want to get the list acting as the fixed-pos item parent.
   */
  AbsoluteFrameList& GetFixedList() { return *mFixedList; }
  const AbsoluteFrameList& GetFixedList() const { return *mFixedList; }

 protected:
  friend class nsFrameConstructorSaveState;

  /**
   * ProcessFrameInsertions takes the frames in aFrameList and adds them as
   * kids to the aChildListID child list of |aFrameList.containingBlock|.
   */
  void ProcessFrameInsertions(AbsoluteFrameList& aFrameList,
                              mozilla::FrameChildListID aChildListID);

  /**
   * GetOutOfFlowFrameList selects the out-of-flow frame list the new
   * frame should be added to. If the frame shouldn't be added to any
   * out-of-flow list, it returns nullptr. The corresponding type of
   * placeholder is also returned via the aPlaceholderType parameter
   * if this method doesn't return nullptr. The caller should check
   * whether the returned list really has a containing block.
   */
  AbsoluteFrameList* GetOutOfFlowFrameList(nsIFrame* aNewFrame,
                                           bool aCanBePositioned,
                                           bool aCanBeFloated,
                                           nsFrameState* aPlaceholderType);

  void ConstructBackdropFrameFor(nsIContent* aContent, nsIFrame* aFrame);
};

nsFrameConstructorState::nsFrameConstructorState(
    PresShell* aPresShell, nsContainerFrame* aFixedContainingBlock,
    nsContainerFrame* aAbsoluteContainingBlock,
    nsContainerFrame* aFloatContainingBlock,
    already_AddRefed<nsILayoutHistoryState> aHistoryState)
    : mPresContext(aPresShell->GetPresContext()),
      mPresShell(aPresShell),
      mFrameConstructor(aPresShell->FrameConstructor()),
      mFloatedList(aFloatContainingBlock),
      mAbsoluteList(aAbsoluteContainingBlock),
      mTopLayerAbsoluteList(mFrameConstructor->GetCanvasFrame()),
      mAncestorFixedList(aFixedContainingBlock),
      mRealFixedList(
          static_cast<nsContainerFrame*>(mFrameConstructor->GetRootFrame())),
      // See PushAbsoluteContaningBlock below
      mFrameState(aHistoryState),
      mCreatingExtraFrames(false),
      mHasRenderedLegend(false) {
  MOZ_COUNT_CTOR(nsFrameConstructorState);
  mFixedList = [&] {
    if (aFixedContainingBlock == aAbsoluteContainingBlock) {
      return &mAbsoluteList;
    }
    if (aAbsoluteContainingBlock == mRealFixedList.mContainingBlock) {
      return &mRealFixedList;
    }
    return &mAncestorFixedList;
  }();
}

nsFrameConstructorState::nsFrameConstructorState(
    PresShell* aPresShell, nsContainerFrame* aFixedContainingBlock,
    nsContainerFrame* aAbsoluteContainingBlock,
    nsContainerFrame* aFloatContainingBlock)
    : nsFrameConstructorState(
          aPresShell, aFixedContainingBlock, aAbsoluteContainingBlock,
          aFloatContainingBlock,
          aPresShell->GetDocument()->GetLayoutHistoryState()) {}

nsFrameConstructorState::~nsFrameConstructorState() {
  MOZ_COUNT_DTOR(nsFrameConstructorState);
  ProcessFrameInsertionsForAllLists();
  for (auto& content : Reversed(mGeneratedContentWithInitializer)) {
    content->RemoveProperty(nsGkAtoms::genConInitializerProperty);
  }
}

void nsFrameConstructorState::ProcessFrameInsertionsForAllLists() {
  ProcessFrameInsertions(mFloatedList, FrameChildListID::Float);
  ProcessFrameInsertions(mAbsoluteList, FrameChildListID::Absolute);
  ProcessFrameInsertions(mTopLayerAbsoluteList, FrameChildListID::Absolute);
  ProcessFrameInsertions(*mFixedList, FrameChildListID::Fixed);
  ProcessFrameInsertions(mRealFixedList, FrameChildListID::Fixed);
}

void nsFrameConstructorState::PushAbsoluteContainingBlock(
    nsContainerFrame* aNewAbsoluteContainingBlock, nsIFrame* aPositionedFrame,
    nsFrameConstructorSaveState& aSaveState) {
  MOZ_ASSERT(!!aNewAbsoluteContainingBlock == !!aPositionedFrame,
             "We should have both or none");
  aSaveState.mList = &mAbsoluteList;
  aSaveState.mChildListID = FrameChildListID::Absolute;
  aSaveState.mState = this;
  aSaveState.mSavedList = std::move(mAbsoluteList);
  aSaveState.mSavedFixedList = mFixedList;
  mAbsoluteList = AbsoluteFrameList(aNewAbsoluteContainingBlock);
  mFixedList = [&] {
    if (!aPositionedFrame || aPositionedFrame->IsFixedPosContainingBlock()) {
      // See if we need to treat abspos and fixedpos the same. This happens if
      // we're a transformed/filtered/etc element, or if we force a null abspos
      // containing block (for mathml for example).
      return &mAbsoluteList;
    }
    if (aPositionedFrame->StyleDisplay()->mTopLayer == StyleTopLayer::Top) {
      // If our new CB is in the top layer, and isn't a fixed CB itself, we also
      // escape the usual containment.
      return &mRealFixedList;
    }
    if (mFixedList == &mAbsoluteList) {
      // If we were pointing to our old absolute list, keep pointing to it.
      return &aSaveState.mSavedList;
    }
    // Otherwise keep pointing to the current thing (another ancestor's
    // absolute list, or the real fixed list, doesn't matter).
    return mFixedList;
  }();

  if (aNewAbsoluteContainingBlock) {
    aNewAbsoluteContainingBlock->MarkAsAbsoluteContainingBlock();
  }
}

void nsFrameConstructorState::MaybePushFloatContainingBlock(
    nsContainerFrame* aFloatCBCandidate,
    nsFrameConstructorSaveState& aSaveState) {
  // The logic here needs to match the logic in GetFloatContainingBlock().
  if (ShouldSuppressFloatingOfDescendants(aFloatCBCandidate)) {
    // Pushing a null float containing block forbids any frames from being
    // floated until a new float containing block is pushed. See implementation
    // of nsFrameConstructorState::AddChild().
    //
    // XXX we should get rid of null float containing blocks and teach the
    // various frame classes to deal with floats instead.
    PushFloatContainingBlock(nullptr, aSaveState);
  } else if (aFloatCBCandidate->IsFloatContainingBlock()) {
    PushFloatContainingBlock(aFloatCBCandidate, aSaveState);
  }

#ifdef DEBUG
  mFloatCBCandidate = aFloatCBCandidate;
#endif
}

void nsFrameConstructorState::PushFloatContainingBlock(
    nsContainerFrame* aNewFloatContainingBlock,
    nsFrameConstructorSaveState& aSaveState) {
  MOZ_ASSERT(!aNewFloatContainingBlock ||
                 aNewFloatContainingBlock->IsFloatContainingBlock(),
             "Please push a real float containing block!");
  NS_ASSERTION(
      !aNewFloatContainingBlock ||
          !ShouldSuppressFloatingOfDescendants(aNewFloatContainingBlock),
      "We should not push a frame that is supposed to _suppress_ "
      "floats as a float containing block!");
  aSaveState.mList = &mFloatedList;
  aSaveState.mSavedList = std::move(mFloatedList);
  aSaveState.mChildListID = FrameChildListID::Float;
  aSaveState.mState = this;
  mFloatedList = AbsoluteFrameList(aNewFloatContainingBlock);
}

nsContainerFrame* nsFrameConstructorState::GetGeometricParent(
    const nsStyleDisplay& aStyleDisplay,
    nsContainerFrame* aContentParentFrame) const {
  // If there is no container for a fixed, absolute, or floating root
  // frame, we will ignore the positioning.  This hack is originally
  // brought to you by the letter T: tables, since other roots don't
  // even call into this code.  See bug 178855.
  //
  // XXX Disabling positioning in this case is a hack.  If one was so inclined,
  // one could support this either by (1) inserting a dummy block between the
  // table and the canvas or (2) teaching the canvas how to reflow positioned
  // elements. (1) has the usual problems when multiple frames share the same
  // content (notice all the special cases in this file dealing with inner
  // tables and table wrappers which share the same content). (2) requires some
  // work and possible factoring.
  //
  // XXXbz couldn't we just force position to "static" on roots and
  // float to "none"?  That's OK per CSS 2.1, as far as I can tell.

  if (aContentParentFrame && aContentParentFrame->IsInSVGTextSubtree()) {
    return aContentParentFrame;
  }

  if (aStyleDisplay.IsFloatingStyle() && mFloatedList.mContainingBlock) {
    NS_ASSERTION(!aStyleDisplay.IsAbsolutelyPositionedStyle(),
                 "Absolutely positioned _and_ floating?");
    return mFloatedList.mContainingBlock;
  }

  if (aStyleDisplay.mTopLayer != StyleTopLayer::None) {
    MOZ_ASSERT(aStyleDisplay.mTopLayer == StyleTopLayer::Top,
               "-moz-top-layer should be either none or top");
    MOZ_ASSERT(aStyleDisplay.IsAbsolutelyPositionedStyle(),
               "Top layer items should always be absolutely positioned");
    if (aStyleDisplay.mPosition == StylePositionProperty::Fixed) {
      MOZ_ASSERT(mRealFixedList.mContainingBlock, "No root frame?");
      return mRealFixedList.mContainingBlock;
    }
    MOZ_ASSERT(aStyleDisplay.mPosition == StylePositionProperty::Absolute);
    MOZ_ASSERT(mTopLayerAbsoluteList.mContainingBlock);
    return mTopLayerAbsoluteList.mContainingBlock;
  }

  if (aStyleDisplay.mPosition == StylePositionProperty::Absolute &&
      mAbsoluteList.mContainingBlock) {
    return mAbsoluteList.mContainingBlock;
  }

  if (aStyleDisplay.mPosition == StylePositionProperty::Fixed &&
      mFixedList->mContainingBlock) {
    return mFixedList->mContainingBlock;
  }

  return aContentParentFrame;
}

void nsFrameConstructorState::ReparentAbsoluteItems(
    nsContainerFrame* aNewParent) {
  // Bug 1491727: This function might not conform to the spec. See
  // https://github.com/w3c/csswg-drafts/issues/1894.

  MOZ_ASSERT(aNewParent->HasAnyStateBits(NS_FRAME_HAS_MULTI_COLUMN_ANCESTOR),
             "Restrict the usage under column hierarchy.");

  AbsoluteFrameList newAbsoluteItems(aNewParent);

  nsIFrame* current = mAbsoluteList.FirstChild();
  while (current) {
    nsIFrame* placeholder = current->GetPlaceholderFrame();

    if (nsLayoutUtils::IsProperAncestorFrame(aNewParent, placeholder)) {
      nsIFrame* next = current->GetNextSibling();
      mAbsoluteList.RemoveFrame(current);
      newAbsoluteItems.AppendFrame(aNewParent, current);
      current = next;
    } else {
      current = current->GetNextSibling();
    }
  }

  if (newAbsoluteItems.NotEmpty()) {
    // ~nsFrameConstructorSaveState() will move newAbsoluteItems to
    // aNewParent's absolute child list.
    nsFrameConstructorSaveState absoluteSaveState;

    // It doesn't matter whether aNewParent has position style or not. Caller
    // won't call us if we can't have absolute children.
    PushAbsoluteContainingBlock(aNewParent, aNewParent, absoluteSaveState);
    mAbsoluteList = std::move(newAbsoluteItems);
  }
}

void nsFrameConstructorState::ReparentFloats(nsContainerFrame* aNewParent) {
  MOZ_ASSERT(aNewParent->HasAnyStateBits(NS_FRAME_HAS_MULTI_COLUMN_ANCESTOR),
             "Restrict the usage under column hierarchy.");
  MOZ_ASSERT(
      aNewParent->IsFloatContainingBlock(),
      "Why calling this method if aNewParent is not a float containing block?");

  // Gather floats that should reparent under aNewParent.
  AbsoluteFrameList floats(aNewParent);
  nsIFrame* current = mFloatedList.FirstChild();
  while (current) {
    nsIFrame* placeholder = current->GetPlaceholderFrame();
    nsIFrame* next = current->GetNextSibling();
    if (nsLayoutUtils::IsProperAncestorFrame(aNewParent, placeholder)) {
      mFloatedList.RemoveFrame(current);
      floats.AppendFrame(aNewParent, current);
    }
    current = next;
  }

  if (floats.NotEmpty()) {
    // Make floats move into aNewParent's float child list in
    // ~nsFrameConstructorSaveState() when destructing floatSaveState.
    nsFrameConstructorSaveState floatSaveState;
    PushFloatContainingBlock(aNewParent, floatSaveState);
    mFloatedList = std::move(floats);
  }
}

AbsoluteFrameList* nsFrameConstructorState::GetOutOfFlowFrameList(
    nsIFrame* aNewFrame, bool aCanBePositioned, bool aCanBeFloated,
    nsFrameState* aPlaceholderType) {
  const nsStyleDisplay* disp = aNewFrame->StyleDisplay();
  if (aCanBeFloated && disp->IsFloatingStyle()) {
    *aPlaceholderType = PLACEHOLDER_FOR_FLOAT;
    return &mFloatedList;
  }

  if (aCanBePositioned) {
    if (disp->mTopLayer != StyleTopLayer::None) {
      *aPlaceholderType = PLACEHOLDER_FOR_TOPLAYER;
      if (disp->mPosition == StylePositionProperty::Fixed) {
        *aPlaceholderType |= PLACEHOLDER_FOR_FIXEDPOS;
        return &mRealFixedList;
      }
      *aPlaceholderType |= PLACEHOLDER_FOR_ABSPOS;
      return &mTopLayerAbsoluteList;
    }
    if (disp->mPosition == StylePositionProperty::Absolute) {
      *aPlaceholderType = PLACEHOLDER_FOR_ABSPOS;
      return &mAbsoluteList;
    }
    if (disp->mPosition == StylePositionProperty::Fixed) {
      *aPlaceholderType = PLACEHOLDER_FOR_FIXEDPOS;
      return mFixedList;
    }
  }
  return nullptr;
}

void nsFrameConstructorState::ConstructBackdropFrameFor(nsIContent* aContent,
                                                        nsIFrame* aFrame) {
  MOZ_ASSERT(aFrame->StyleDisplay()->mTopLayer == StyleTopLayer::Top);
  nsContainerFrame* frame = do_QueryFrame(aFrame);
  if (!frame) {
    NS_WARNING("Cannot create backdrop frame for non-container frame");
    return;
  }

  ComputedStyle* parentStyle = nsLayoutUtils::GetStyleFrame(aFrame)->Style();
  RefPtr<ComputedStyle> style =
      mPresShell->StyleSet()->ResolvePseudoElementStyle(
          *aContent->AsElement(), PseudoStyleType::backdrop, nullptr,
          parentStyle);
  MOZ_ASSERT(style->StyleDisplay()->mTopLayer == StyleTopLayer::Top);
  nsContainerFrame* parentFrame =
      GetGeometricParent(*style->StyleDisplay(), nullptr);

  nsBackdropFrame* backdropFrame =
      new (mPresShell) nsBackdropFrame(style, mPresShell->GetPresContext());
  backdropFrame->Init(aContent, parentFrame, nullptr);

  nsFrameState placeholderType;
  AbsoluteFrameList* frameList =
      GetOutOfFlowFrameList(backdropFrame, true, true, &placeholderType);
  MOZ_ASSERT(placeholderType & PLACEHOLDER_FOR_TOPLAYER);

  nsIFrame* placeholder = nsCSSFrameConstructor::CreatePlaceholderFrameFor(
      mPresShell, aContent, backdropFrame, frame, nullptr, placeholderType);
  frame->SetInitialChildList(FrameChildListID::Backdrop,
                             nsFrameList(placeholder, placeholder));

  frameList->AppendFrame(nullptr, backdropFrame);
}

void nsFrameConstructorState::AddChild(
    nsIFrame* aNewFrame, nsFrameList& aFrameList, nsIContent* aContent,
    nsContainerFrame* aParentFrame, bool aCanBePositioned, bool aCanBeFloated,
    bool aInsertAfter, nsIFrame* aInsertAfterFrame) {
  MOZ_ASSERT(!aNewFrame->GetNextSibling(), "Shouldn't happen");

  nsFrameState placeholderType;
  AbsoluteFrameList* outOfFlowFrameList = GetOutOfFlowFrameList(
      aNewFrame, aCanBePositioned, aCanBeFloated, &placeholderType);

  // The comments in GetGeometricParent regarding root table frames
  // all apply here, unfortunately. Thus, we need to check whether
  // the returned frame items really has containing block.
  nsFrameList* frameList;
  if (outOfFlowFrameList && outOfFlowFrameList->mContainingBlock) {
    MOZ_ASSERT(aNewFrame->GetParent() == outOfFlowFrameList->mContainingBlock,
               "Parent of the frame is not the containing block?");
    frameList = outOfFlowFrameList;
  } else {
    frameList = &aFrameList;
    placeholderType = nsFrameState(0);
  }

  if (placeholderType) {
    NS_ASSERTION(frameList != &aFrameList,
                 "Putting frame in-flow _and_ want a placeholder?");
    nsIFrame* placeholderFrame =
        nsCSSFrameConstructor::CreatePlaceholderFrameFor(
            mPresShell, aContent, aNewFrame, aParentFrame, nullptr,
            placeholderType);

    placeholderFrame->AddStateBits(mAdditionalStateBits);
    // Add the placeholder frame to the flow
    aFrameList.AppendFrame(nullptr, placeholderFrame);

    if (placeholderType & PLACEHOLDER_FOR_TOPLAYER) {
      ConstructBackdropFrameFor(aContent, aNewFrame);
    }
  }
#ifdef DEBUG
  else {
    NS_ASSERTION(aNewFrame->GetParent() == aParentFrame,
                 "In-flow frame has wrong parent");
  }
#endif

  if (aInsertAfter) {
    frameList->InsertFrame(nullptr, aInsertAfterFrame, aNewFrame);
  } else {
    frameList->AppendFrame(nullptr, aNewFrame);
  }
}

// Some of this function's callers recurse 1000 levels deep in crashtests. On
// platforms where stack limits are low, we can't afford to incorporate this
// function's `AutoTArray`s into its callers' stack frames, so disable inlining.
MOZ_NEVER_INLINE void nsFrameConstructorState::ProcessFrameInsertions(
    AbsoluteFrameList& aFrameList, FrameChildListID aChildListID) {
  MOZ_ASSERT(&aFrameList == &mFloatedList || &aFrameList == &mAbsoluteList ||
             &aFrameList == &mTopLayerAbsoluteList ||
             &aFrameList == &mAncestorFixedList || &aFrameList == mFixedList ||
             &aFrameList == &mRealFixedList);
  MOZ_ASSERT_IF(&aFrameList == &mFloatedList,
                aChildListID == FrameChildListID::Float);
  MOZ_ASSERT_IF(&aFrameList == &mAbsoluteList || &aFrameList == mFixedList,
                aChildListID == FrameChildListID::Absolute ||
                    aChildListID == FrameChildListID::Fixed);
  MOZ_ASSERT_IF(&aFrameList == &mTopLayerAbsoluteList,
                aChildListID == FrameChildListID::Absolute);
  MOZ_ASSERT_IF(&aFrameList == mFixedList && &aFrameList != &mAbsoluteList,
                aChildListID == FrameChildListID::Fixed);
  MOZ_ASSERT_IF(&aFrameList == &mAncestorFixedList,
                aChildListID == FrameChildListID::Fixed);
  MOZ_ASSERT_IF(&aFrameList == &mRealFixedList,
                aChildListID == FrameChildListID::Fixed);

  if (aFrameList.IsEmpty()) {
    return;
  }

  nsContainerFrame* containingBlock = aFrameList.mContainingBlock;

  NS_ASSERTION(containingBlock, "Child list without containing block?");

  if (aChildListID == FrameChildListID::Fixed) {
    // Put this frame on the transformed-frame's abs-pos list instead, if
    // it has abs-pos children instead of fixed-pos children.
    aChildListID = containingBlock->GetAbsoluteListID();
  }

  // Insert the frames hanging out in aItems.  We can use SetInitialChildList()
  // if the containing block hasn't been reflowed yet (so NS_FRAME_FIRST_REFLOW
  // is set) and doesn't have any frames in the aChildListID child list yet.
  const nsFrameList& childList = containingBlock->GetChildList(aChildListID);
  if (childList.IsEmpty() &&
      containingBlock->HasAnyStateBits(NS_FRAME_FIRST_REFLOW)) {
    // If we're injecting absolutely positioned frames, inject them on the
    // absolute containing block
    if (aChildListID == containingBlock->GetAbsoluteListID()) {
      containingBlock->GetAbsoluteContainingBlock()->SetInitialChildList(
          containingBlock, aChildListID, std::move(aFrameList));
    } else {
      containingBlock->SetInitialChildList(aChildListID, std::move(aFrameList));
    }
  } else if (aChildListID == FrameChildListID::Fixed ||
             aChildListID == FrameChildListID::Absolute) {
    // The order is not important for abs-pos/fixed-pos frame list, just
    // append the frame items to the list directly.
    mFrameConstructor->AppendFrames(containingBlock, aChildListID,
                                    std::move(aFrameList));
  } else {
    // Note that whether the frame construction context is doing an append or
    // not is not helpful here, since it could be appending to some frame in
    // the middle of the document, which means we're not necessarily
    // appending to the children of the containing block.
    //
    // We need to make sure the 'append to the end of document' case is fast.
    // So first test the last child of the containing block
    nsIFrame* lastChild = childList.LastChild();

    // CompareTreePosition uses placeholder hierarchy for out of flow frames,
    // so this will make out-of-flows respect the ordering of placeholders,
    // which is great because it takes care of anonymous content.
    nsIFrame* firstNewFrame = aFrameList.FirstChild();

    // Cache the ancestor chain so that we can reuse it if needed.
    AutoTArray<nsIFrame*, 20> firstNewFrameAncestors;
    nsIFrame* notCommonAncestor = nullptr;
    if (lastChild) {
      notCommonAncestor = nsLayoutUtils::FillAncestors(
          firstNewFrame, containingBlock, &firstNewFrameAncestors);
    }

    if (!lastChild || nsLayoutUtils::CompareTreePosition(
                          lastChild, firstNewFrame, firstNewFrameAncestors,
                          notCommonAncestor ? containingBlock : nullptr) < 0) {
      // no lastChild, or lastChild comes before the new children, so just
      // append
      mFrameConstructor->AppendFrames(containingBlock, aChildListID,
                                      std::move(aFrameList));
    } else {
      // Try the other children. First collect them to an array so that a
      // reasonable fast binary search can be used to find the insertion point.
      AutoTArray<nsIFrame*, 128> children;
      for (nsIFrame* f = childList.FirstChild(); f != lastChild;
           f = f->GetNextSibling()) {
        children.AppendElement(f);
      }

      nsIFrame* insertionPoint = nullptr;
      int32_t imin = 0;
      int32_t max = children.Length();
      while (max > imin) {
        int32_t imid = imin + ((max - imin) / 2);
        nsIFrame* f = children[imid];
        int32_t compare = nsLayoutUtils::CompareTreePosition(
            f, firstNewFrame, firstNewFrameAncestors,
            notCommonAncestor ? containingBlock : nullptr);
        if (compare > 0) {
          // f is after the new frame.
          max = imid;
          insertionPoint = imid > 0 ? children[imid - 1] : nullptr;
        } else if (compare < 0) {
          // f is before the new frame.
          imin = imid + 1;
          insertionPoint = f;
        } else {
          // This is for the old behavior. Should be removed once it is
          // guaranteed that CompareTreePosition can't return 0!
          // See bug 928645.
          NS_WARNING("Something odd happening???");
          insertionPoint = nullptr;
          for (uint32_t i = 0; i < children.Length(); ++i) {
            nsIFrame* f = children[i];
            if (nsLayoutUtils::CompareTreePosition(
                    f, firstNewFrame, firstNewFrameAncestors,
                    notCommonAncestor ? containingBlock : nullptr) > 0) {
              break;
            }
            insertionPoint = f;
          }
          break;
        }
      }
      mFrameConstructor->InsertFrames(containingBlock, aChildListID,
                                      insertionPoint, std::move(aFrameList));
    }
  }

  MOZ_ASSERT(aFrameList.IsEmpty(), "How did that happen?");
}

nsFrameConstructorSaveState::~nsFrameConstructorSaveState() {
  // Restore the state
  if (mList) {
    MOZ_ASSERT(mState, "Can't have mList set without having a state!");
    mState->ProcessFrameInsertions(*mList, mChildListID);

    if (mList == &mState->mAbsoluteList) {
      mState->mAbsoluteList = std::move(mSavedList);
      mState->mFixedList = mSavedFixedList;
    } else {
      mState->mFloatedList = std::move(mSavedList);
    }

    MOZ_ASSERT(mSavedList.IsEmpty(),
               "Frames in mSavedList should've moved back into mState!");
    MOZ_ASSERT(!mList->LastChild() || !mList->LastChild()->GetNextSibling(),
               "Something corrupted our list!");
  }
}

/**
 * Moves aFrameList from aOldParent to aNewParent.  This updates the parent
 * pointer of the frames in the list, and reparents their views as needed.
 * nsIFrame::SetParent sets the NS_FRAME_HAS_VIEW bit on aNewParent and its
 * ancestors as needed. Then it sets the list as the initial child list
 * on aNewParent, unless aNewParent either already has kids or has been
 * reflowed; in that case it appends the new frames.  Note that this
 * method differs from ReparentFrames in that it doesn't change the kids'
 * style.
 */
// XXXbz Since this is only used for {ib} splits, could we just copy the view
// bits from aOldParent to aNewParent and then use the
// nsFrameList::ApplySetParent?  That would still leave us doing two passes
// over the list, of course; if we really wanted to we could factor out the
// relevant part of ReparentFrameViewList, I suppose...  Or just get rid of
// views, which would make most of this function go away.
static void MoveChildrenTo(nsIFrame* aOldParent, nsContainerFrame* aNewParent,
                           nsFrameList& aFrameList) {
#ifdef DEBUG
  bool sameGrandParent = aOldParent->GetParent() == aNewParent->GetParent();

  if (aNewParent->HasView() || aOldParent->HasView() || !sameGrandParent) {
    // Move the frames into the new view
    nsContainerFrame::ReparentFrameViewList(aFrameList, aOldParent, aNewParent);
  }
#endif

  aFrameList.ApplySetParent(aNewParent);

  if (aNewParent->PrincipalChildList().IsEmpty() &&
      aNewParent->HasAnyStateBits(NS_FRAME_FIRST_REFLOW)) {
    aNewParent->SetInitialChildList(FrameChildListID::Principal,
                                    std::move(aFrameList));
  } else {
    aNewParent->AppendFrames(FrameChildListID::Principal,
                             std::move(aFrameList));
  }
}

static void EnsureAutoPageName(nsFrameConstructorState& aState,
                               const nsContainerFrame* const aFrame) {
  // Check if we need to figure out our used page name.
  // When building the entire document, this should only happen for the
  // root, which will mean the loop will immediately end. Either way, this will
  // only happen once for each time the frame constructor is run.
  if (aState.mAutoPageNameValue) {
    return;
  }

  for (const nsContainerFrame* frame = aFrame; frame;
       frame = frame->GetParent()) {
    if (const nsAtom* maybePageName = frame->GetStylePageName()) {
      aState.mAutoPageNameValue = maybePageName;
      return;
    }
  }
  // Ensure that a root with `page: auto` gets an empty page name
  // https://drafts.csswg.org/css-page-3/#using-named-pages
  aState.mAutoPageNameValue = nsGkAtoms::_empty;
}

nsCSSFrameConstructor::AutoFrameConstructionPageName::
    AutoFrameConstructionPageName(nsFrameConstructorState& aState,
                                  nsIFrame* const aFrame)
    : mState(aState), mNameToRestore(nullptr) {
  if (!aState.mPresContext->IsPaginated()) {
    MOZ_ASSERT(!aState.mAutoPageNameValue,
               "Page name should not have been set");
    return;
  }
#ifdef DEBUG
  MOZ_ASSERT(!aFrame->mWasVisitedByAutoFrameConstructionPageName,
             "Frame should only have been visited once");
  aFrame->mWasVisitedByAutoFrameConstructionPageName = true;
#endif

  EnsureAutoPageName(aState, aFrame->GetParent());
  mNameToRestore = aState.mAutoPageNameValue;

  MOZ_ASSERT(mNameToRestore,
             "Page name should have been found by EnsureAutoPageName");
  if (const nsAtom* maybePageName = aFrame->GetStylePageName()) {
    aState.mAutoPageNameValue = maybePageName;
  }
  aFrame->SetAutoPageValue(aState.mAutoPageNameValue);
}

nsCSSFrameConstructor::AutoFrameConstructionPageName::
    ~AutoFrameConstructionPageName() {
  // This isn't actually useful when not in paginated layout, but it's very
  // likely cheaper to unconditionally write this pointer than to test for
  // paginated layout and then branch on the result.
  mState.mAutoPageNameValue = mNameToRestore;
}

//----------------------------------------------------------------------

nsCSSFrameConstructor::nsCSSFrameConstructor(Document* aDocument,
                                             PresShell* aPresShell)
    : nsFrameManager(aPresShell),
      mDocument(aDocument),
      mFirstFreeFCItem(nullptr),
      mFCItemsInUse(0),
      mCurrentDepth(0),
      mQuotesDirty(false),
      mCountersDirty(false),
      mAlwaysCreateFramesForIgnorableWhitespace(false) {
#ifdef DEBUG
  static bool gFirstTime = true;
  if (gFirstTime) {
    gFirstTime = false;
    char* flags = PR_GetEnv("GECKO_FRAMECTOR_DEBUG_FLAGS");
    if (flags) {
      bool error = false;
      for (;;) {
        char* comma = strchr(flags, ',');
        if (comma) *comma = '\0';

        bool found = false;
        FrameCtorDebugFlags* flag = gFlags;
        FrameCtorDebugFlags* limit = gFlags + NUM_DEBUG_FLAGS;
        while (flag < limit) {
          if (nsCRT::strcasecmp(flag->name, flags) == 0) {
            *(flag->on) = true;
            printf("nsCSSFrameConstructor: setting %s debug flag on\n",
                   flag->name);
            found = true;
            break;
          }
          ++flag;
        }

        if (!found) error = true;

        if (!comma) break;

        *comma = ',';
        flags = comma + 1;
      }

      if (error) {
        printf("Here are the available GECKO_FRAMECTOR_DEBUG_FLAGS:\n");
        FrameCtorDebugFlags* flag = gFlags;
        FrameCtorDebugFlags* limit = gFlags + NUM_DEBUG_FLAGS;
        while (flag < limit) {
          printf("  %s\n", flag->name);
          ++flag;
        }
        printf(
            "Note: GECKO_FRAMECTOR_DEBUG_FLAGS is a comma separated list of "
            "flag\n");
        printf("names (no whitespace)\n");
      }
    }
  }
#endif
}

void nsCSSFrameConstructor::NotifyDestroyingFrame(nsIFrame* aFrame) {
  if (aFrame->StyleDisplay()->IsContainStyle()) {
    mContainStyleScopeManager.DestroyScopesFor(aFrame);
  }

  if (aFrame->HasAnyStateBits(NS_FRAME_GENERATED_CONTENT) &&
      mContainStyleScopeManager.DestroyQuoteNodesFor(aFrame)) {
    QuotesDirty();
  }

  if (aFrame->HasAnyStateBits(NS_FRAME_HAS_CSS_COUNTER_STYLE) &&
      mContainStyleScopeManager.DestroyCounterNodesFor(aFrame)) {
    // Technically we don't need to update anything if we destroyed only
    // USE nodes.  However, this is unlikely to happen in the real world
    // since USE nodes generally go along with INCREMENT nodes.
    CountersDirty();
  }

  RestyleManager()->NotifyDestroyingFrame(aFrame);
}

struct nsGenConInitializer {
  UniquePtr<nsGenConNode> mNode;
  nsGenConList* mList;
  void (nsCSSFrameConstructor::*mDirtyAll)();

  nsGenConInitializer(UniquePtr<nsGenConNode> aNode, nsGenConList* aList,
                      void (nsCSSFrameConstructor::*aDirtyAll)())
      : mNode(std::move(aNode)), mList(aList), mDirtyAll(aDirtyAll) {}
};

already_AddRefed<nsIContent> nsCSSFrameConstructor::CreateGenConTextNode(
    nsFrameConstructorState& aState, const nsAString& aString,
    UniquePtr<nsGenConInitializer> aInitializer) {
  RefPtr<nsTextNode> content = new (mDocument->NodeInfoManager())
      nsTextNode(mDocument->NodeInfoManager());
  content->SetText(aString, false);
  if (aInitializer) {
    aInitializer->mNode->mText = content;
    content->SetProperty(nsGkAtoms::genConInitializerProperty,
                         aInitializer.release(),
                         nsINode::DeleteProperty<nsGenConInitializer>);
    aState.mGeneratedContentWithInitializer.AppendElement(content);
  }
  return content.forget();
}

void nsCSSFrameConstructor::CreateGeneratedContent(
    nsFrameConstructorState& aState, Element& aOriginatingElement,
    ComputedStyle& aPseudoStyle, uint32_t aContentIndex,
    const FunctionRef<void(nsIContent*)> aAddChild) {
  using Type = StyleContentItem::Tag;
  // Get the content value
  const auto& item = aPseudoStyle.StyleContent()->ContentAt(aContentIndex);
  const Type type = item.tag;

  switch (type) {
    case Type::Image: {
      RefPtr c = GeneratedImageContent::Create(*mDocument, aContentIndex);
      aAddChild(c);
      return;
    }

    case Type::String: {
      const auto string = item.AsString().AsString();
      if (string.IsEmpty()) {
        return;
      }
      RefPtr text =
          CreateGenConTextNode(aState, NS_ConvertUTF8toUTF16(string), nullptr);
      aAddChild(text);
      return;
    }

    case Type::Attr: {
      const auto& attr = item.AsAttr();
      RefPtr<nsAtom> attrName = attr.attribute.AsAtom();
      int32_t attrNameSpace = kNameSpaceID_None;
      RefPtr<nsAtom> ns = attr.namespace_url.AsAtom();
      if (!ns->IsEmpty()) {
        nsresult rv = nsNameSpaceManager::GetInstance()->RegisterNameSpace(
            ns.forget(), attrNameSpace);
        NS_ENSURE_SUCCESS_VOID(rv);
      }

      if (mDocument->IsHTMLDocument() && aOriginatingElement.IsHTMLElement()) {
        ToLowerCaseASCII(attrName);
      }

      RefPtr<nsAtom> fallback = attr.fallback.AsAtom();

      nsCOMPtr<nsIContent> content;
      NS_NewAttributeContent(mDocument->NodeInfoManager(), attrNameSpace,
                             attrName, fallback, getter_AddRefs(content));
      aAddChild(content);
      return;
    }

    case Type::Counter:
    case Type::Counters: {
      RefPtr<nsAtom> name;
      CounterStylePtr ptr;
      nsString separator;
      if (type == Type::Counter) {
        auto& counter = item.AsCounter();
        name = counter._0.AsAtom();
        ptr = CounterStylePtr::FromStyle(counter._1);
      } else {
        auto& counters = item.AsCounters();
        name = counters._0.AsAtom();
        CopyUTF8toUTF16(counters._1.AsString(), separator);
        ptr = CounterStylePtr::FromStyle(counters._2);
      }

      auto* counterList = mContainStyleScopeManager.GetOrCreateCounterList(
          aOriginatingElement, name);
      auto node = MakeUnique<nsCounterUseNode>(
          std::move(ptr), std::move(separator), aContentIndex,
          /* aAllCounters = */ type == Type::Counters);

      auto initializer = MakeUnique<nsGenConInitializer>(
          std::move(node), counterList, &nsCSSFrameConstructor::CountersDirty);
      RefPtr c = CreateGenConTextNode(aState, u""_ns, std::move(initializer));
      aAddChild(c);
      return;
    }
    case Type::OpenQuote:
    case Type::CloseQuote:
    case Type::NoOpenQuote:
    case Type::NoCloseQuote: {
      auto node = MakeUnique<nsQuoteNode>(type, aContentIndex);
      auto* quoteList =
          mContainStyleScopeManager.QuoteListFor(aOriginatingElement);
      auto initializer = MakeUnique<nsGenConInitializer>(
          std::move(node), quoteList, &nsCSSFrameConstructor::QuotesDirty);
      RefPtr c = CreateGenConTextNode(aState, u""_ns, std::move(initializer));
      aAddChild(c);
      return;
    }

    case Type::MozLabelContent: {
      nsAutoString accesskey;
      if (!aOriginatingElement.GetAttr(nsGkAtoms::accesskey, accesskey) ||
          accesskey.IsEmpty() || !LookAndFeel::GetMenuAccessKey()) {
        // Easy path: just return a regular value attribute content.
        nsCOMPtr<nsIContent> content;
        NS_NewAttributeContent(mDocument->NodeInfoManager(), kNameSpaceID_None,
                               nsGkAtoms::value, nsGkAtoms::_empty,
                               getter_AddRefs(content));
        aAddChild(content);
        return;
      }

      nsAutoString value;
      aOriginatingElement.GetAttr(nsGkAtoms::value, value);

      auto AppendAccessKeyLabel = [&] {
        // Always append accesskey text in uppercase, see bug 1806167.
        ToUpperCase(accesskey);
        nsAutoString accessKeyLabel = u"("_ns + accesskey + u")"_ns;
        if (!StringEndsWith(value, accessKeyLabel)) {
          if (InsertSeparatorBeforeAccessKey() && !value.IsEmpty() &&
              !NS_IS_SPACE(value.Last())) {
            value.Append(' ');
          }
          value.Append(accessKeyLabel);
        }
      };
      if (AlwaysAppendAccessKey()) {
        AppendAccessKeyLabel();
        RefPtr c = CreateGenConTextNode(aState, value, nullptr);
        aAddChild(c);
        return;
      }

      const auto accessKeyStart = [&]() -> Maybe<size_t> {
        nsAString::const_iterator start, end;
        value.BeginReading(start);
        value.EndReading(end);

        const auto originalStart = start;
        // not appending access key - do case-sensitive search
        // first
        bool found = true;
        if (!FindInReadable(accesskey, start, end)) {
          start = originalStart;
          // didn't find it - perform a case-insensitive search
          found = FindInReadable(accesskey, start, end,
                                 nsCaseInsensitiveStringComparator);
        }
        if (!found) {
          return Nothing();
        }
        return Some(Distance(originalStart, start));
      }();

      if (accessKeyStart.isNothing()) {
        AppendAccessKeyLabel();
        RefPtr c = CreateGenConTextNode(aState, value, nullptr);
        aAddChild(c);
        return;
      }

      if (*accessKeyStart != 0) {
        RefPtr beginning = CreateGenConTextNode(
            aState, Substring(value, 0, *accessKeyStart), nullptr);
        aAddChild(beginning);
      }

      {
        RefPtr accessKeyText = CreateGenConTextNode(
            aState, Substring(value, *accessKeyStart, accesskey.Length()),
            nullptr);
        RefPtr<nsIContent> underline =
            mDocument->CreateHTMLElement(nsGkAtoms::u);
        underline->AppendChildTo(accessKeyText, /* aNotify = */ false,
                                 IgnoreErrors());
        aAddChild(underline);
      }

      size_t accessKeyEnd = *accessKeyStart + accesskey.Length();
      if (accessKeyEnd != value.Length()) {
        RefPtr valueEnd = CreateGenConTextNode(
            aState, Substring(value, *accessKeyStart + accesskey.Length()),
            nullptr);
        aAddChild(valueEnd);
      }
      break;
    }
    case Type::MozAltContent: {
      // Use the "alt" attribute; if that fails and the node is an HTML
      // <input>, try the value attribute and then fall back to some default
      // localized text we have.
      // XXX what if the 'alt' attribute is added later, how will we
      // detect that and do the right thing here?
      if (aOriginatingElement.HasAttr(nsGkAtoms::alt)) {
        nsCOMPtr<nsIContent> content;
        NS_NewAttributeContent(mDocument->NodeInfoManager(), kNameSpaceID_None,
                               nsGkAtoms::alt, nsGkAtoms::_empty,
                               getter_AddRefs(content));
        aAddChild(content);
        return;
      }

      if (aOriginatingElement.IsHTMLElement(nsGkAtoms::input)) {
        if (aOriginatingElement.HasAttr(nsGkAtoms::value)) {
          nsCOMPtr<nsIContent> content;
          NS_NewAttributeContent(mDocument->NodeInfoManager(),
                                 kNameSpaceID_None, nsGkAtoms::value,
                                 nsGkAtoms::_empty, getter_AddRefs(content));
          aAddChild(content);
          return;
        }

        nsAutoString temp;
        nsContentUtils::GetMaybeLocalizedString(
            nsContentUtils::eFORMS_PROPERTIES, "Submit", mDocument, temp);
        RefPtr c = CreateGenConTextNode(aState, temp, nullptr);
        aAddChild(c);
        return;
      }
      break;
    }
  }

  return;
}

void nsCSSFrameConstructor::CreateGeneratedContentFromListStyle(
    nsFrameConstructorState& aState, Element& aOriginatingElement,
    const ComputedStyle& aPseudoStyle,
    const FunctionRef<void(nsIContent*)> aAddChild) {
  const nsStyleList* styleList = aPseudoStyle.StyleList();
  if (!styleList->mListStyleImage.IsNone()) {
    RefPtr<nsIContent> child =
        GeneratedImageContent::CreateForListStyleImage(*mDocument);
    aAddChild(child);
    child = CreateGenConTextNode(aState, u" "_ns, nullptr);
    aAddChild(child);
    return;
  }
  CreateGeneratedContentFromListStyleType(aState, aOriginatingElement,
                                          aPseudoStyle, aAddChild);
}

void nsCSSFrameConstructor::CreateGeneratedContentFromListStyleType(
    nsFrameConstructorState& aState, Element& aOriginatingElement,
    const ComputedStyle& aPseudoStyle,
    const FunctionRef<void(nsIContent*)> aAddChild) {
  const nsStyleList* styleList = aPseudoStyle.StyleList();
  CounterStyle* counterStyle =
      mPresShell->GetPresContext()->CounterStyleManager()->ResolveCounterStyle(
          styleList->mCounterStyle);
  bool needUseNode = false;
  switch (counterStyle->GetStyle()) {
    case ListStyle::None:
      return;
    case ListStyle::Disc:
    case ListStyle::Circle:
    case ListStyle::Square:
    case ListStyle::DisclosureClosed:
    case ListStyle::DisclosureOpen:
      break;
    default:
      const auto* anonStyle = counterStyle->AsAnonymous();
      if (!anonStyle || !anonStyle->IsSingleString()) {
        needUseNode = true;
      }
  }

  auto node = MakeUnique<nsCounterUseNode>(nsCounterUseNode::ForLegacyBullet,
                                           styleList->mCounterStyle);
  if (!needUseNode) {
    nsAutoString text;
    node->GetText(WritingMode(&aPseudoStyle), counterStyle, text);
    // Note that we're done with 'node' in this case.  It's not inserted into
    // any list so it's deleted when we return.
    RefPtr<nsIContent> child = CreateGenConTextNode(aState, text, nullptr);
    aAddChild(child);
    return;
  }

  auto* counterList = mContainStyleScopeManager.GetOrCreateCounterList(
      aOriginatingElement, nsGkAtoms::list_item);
  auto initializer = MakeUnique<nsGenConInitializer>(
      std::move(node), counterList, &nsCSSFrameConstructor::CountersDirty);
  RefPtr<nsIContent> child =
      CreateGenConTextNode(aState, EmptyString(), std::move(initializer));
  aAddChild(child);
}

// Frames for these may not be leaves in the proper sense, but we still don't
// want to expose generated content on them. For the purposes of the page they
// should be leaves.
static bool HasUAWidget(const Element& aOriginatingElement) {
  const ShadowRoot* sr = aOriginatingElement.GetShadowRoot();
  return sr && sr->IsUAWidget();
}

/*
 * aParentFrame - the frame that should be the parent of the generated
 *   content.  This is the frame for the corresponding content node,
 *   which must not be a leaf frame.
 *
 * Any items created are added to aItems.
 *
 * We create an XML element (tag _moz_generated_content_before/after/marker)
 * representing the pseudoelement. We create a DOM node for each 'content'
 * item and make those nodes the children of the XML element. Then we create
 * a frame subtree for the XML element as if it were a regular child of
 * aParentFrame/aParentContent, giving the XML element the ::before, ::after
 * or ::marker style.
 */
void nsCSSFrameConstructor::CreateGeneratedContentItem(
    nsFrameConstructorState& aState, nsContainerFrame* aParentFrame,
    Element& aOriginatingElement, ComputedStyle& aStyle,
    PseudoStyleType aPseudoElement, FrameConstructionItemList& aItems,
    ItemFlags aExtraFlags) {
  MOZ_ASSERT(aPseudoElement == PseudoStyleType::before ||
                 aPseudoElement == PseudoStyleType::after ||
                 aPseudoElement == PseudoStyleType::marker,
             "unexpected aPseudoElement");

  if (HasUAWidget(aOriginatingElement) &&
      !aOriginatingElement.IsHTMLElement(nsGkAtoms::details)) {
    return;
  }

  ServoStyleSet* styleSet = mPresShell->StyleSet();

  // Probe for the existence of the pseudo-element.
  // |ProbePseudoElementStyle| checks the relevant properties for the pseudo.
  // It only returns a non-null value if the pseudo should exist.
  RefPtr<ComputedStyle> pseudoStyle = styleSet->ProbePseudoElementStyle(
      aOriginatingElement, aPseudoElement, nullptr, &aStyle);
  if (!pseudoStyle) {
    return;
  }

  nsAtom* elemName = nullptr;
  nsAtom* property = nullptr;
  switch (aPseudoElement) {
    case PseudoStyleType::before:
      elemName = nsGkAtoms::mozgeneratedcontentbefore;
      property = nsGkAtoms::beforePseudoProperty;
      break;
    case PseudoStyleType::after:
      elemName = nsGkAtoms::mozgeneratedcontentafter;
      property = nsGkAtoms::afterPseudoProperty;
      break;
    case PseudoStyleType::marker:
      // We want to get a marker style even if we match no rules, but we still
      // want to check the result of GeneratedContentPseudoExists.
      elemName = nsGkAtoms::mozgeneratedcontentmarker;
      property = nsGkAtoms::markerPseudoProperty;
      break;
    default:
      MOZ_ASSERT_UNREACHABLE("unexpected aPseudoElement");
  }

  RefPtr<NodeInfo> nodeInfo = mDocument->NodeInfoManager()->GetNodeInfo(
      elemName, nullptr, kNameSpaceID_None, nsINode::ELEMENT_NODE);
  RefPtr<Element> container;
  nsresult rv = NS_NewXMLElement(getter_AddRefs(container), nodeInfo.forget());
  if (NS_FAILED(rv)) {
    return;
  }

  // Cleared when the pseudo is unbound from the tree, so no need to store a
  // strong reference, nor a destructor.
  aOriginatingElement.SetProperty(property, container.get());

  container->SetIsNativeAnonymousRoot();
  container->SetPseudoElementType(aPseudoElement);

  BindContext context(aOriginatingElement, BindContext::ForNativeAnonymous);
  rv = container->BindToTree(context, aOriginatingElement);
  if (NS_FAILED(rv)) {
    container->UnbindFromTree();
    return;
  }

  if (mDocument->DevToolsAnonymousAndShadowEventsEnabled()) {
    container->QueueDevtoolsAnonymousEvent(/* aIsRemove = */ false);
  }

  // Servo has already eagerly computed the style for the container, so we can
  // just stick the style on the element and avoid an additional traversal.
  //
  // We don't do this for pseudos that may trigger animations or transitions,
  // since those need to be kicked off by the traversal machinery.
  //
  // Note that when a pseudo-element animates, we flag the originating element,
  // so we check that flag, but we could also a more expensive (but exhaustive)
  // check using EffectSet::GetEffectSet, for example.
  if (!Servo_ComputedValues_SpecifiesAnimationsOrTransitions(pseudoStyle) &&
      !aOriginatingElement.MayHaveAnimations()) {
    Servo_SetExplicitStyle(container, pseudoStyle);
  } else {
    // If animations are involved, we avoid the SetExplicitStyle optimization
    // above. We need to grab style with animations from the pseudo element and
    // replace old one.
    mPresShell->StyleSet()->StyleNewSubtree(container);
    pseudoStyle = ServoStyleSet::ResolveServoStyle(*container);
  }

  auto AppendChild = [&container, this](nsIContent* aChild) {
    // We don't strictly have to set NODE_IS_IN_NATIVE_ANONYMOUS_SUBTREE
    // here; it would get set under AppendChildTo.  But AppendChildTo might
    // think that we're going from not being anonymous to being anonymous and
    // do some extra work; setting the flag here avoids that.
    aChild->SetFlags(NODE_IS_IN_NATIVE_ANONYMOUS_SUBTREE);
    container->AppendChildTo(aChild, false, IgnoreErrors());
    if (auto* childElement = Element::FromNode(aChild)) {
      // If we created any children elements, Servo needs to traverse them, but
      // the root is already set up.
      mPresShell->StyleSet()->StyleNewSubtree(childElement);
    }
  };
  const uint32_t contentCount = pseudoStyle->StyleContent()->ContentCount();
  for (uint32_t contentIndex = 0; contentIndex < contentCount; contentIndex++) {
    CreateGeneratedContent(aState, aOriginatingElement, *pseudoStyle,
                           contentIndex, AppendChild);
  }
  // If a ::marker has no 'content' then generate it from its 'list-style-*'.
  if (contentCount == 0 && aPseudoElement == PseudoStyleType::marker) {
    CreateGeneratedContentFromListStyle(aState, aOriginatingElement,
                                        *pseudoStyle, AppendChild);
  }
  auto flags = ItemFlags{ItemFlag::IsGeneratedContent} + aExtraFlags;
  AddFrameConstructionItemsInternal(aState, container, aParentFrame, true,
                                    pseudoStyle, flags, aItems);
}

/****************************************************
 **  BEGIN TABLE SECTION
 ****************************************************/

// The term pseudo frame is being used instead of anonymous frame, since
// anonymous frame has been used elsewhere to refer to frames that have
// generated content

// Return whether the given frame is a table pseudo-frame. Note that
// cell-content and table-outer frames have pseudo-types, but are always
// created, even for non-anonymous cells and tables respectively.  So for those
// we have to examine the cell or table frame to see whether it's a pseudo
// frame. In particular, a lone table caption will have a table wrapper as its
// parent, but will also trigger construction of an empty inner table, which
// will be the one we can examine to see whether the wrapper was a pseudo-frame.
static bool IsTablePseudo(nsIFrame* aFrame) {
  auto pseudoType = aFrame->Style()->GetPseudoType();
  return pseudoType != PseudoStyleType::NotPseudo &&
         (pseudoType == PseudoStyleType::table ||
          pseudoType == PseudoStyleType::inlineTable ||
          pseudoType == PseudoStyleType::tableColGroup ||
          pseudoType == PseudoStyleType::tableRowGroup ||
          pseudoType == PseudoStyleType::tableRow ||
          pseudoType == PseudoStyleType::tableCell ||
          (pseudoType == PseudoStyleType::cellContent &&
           aFrame->GetParent()->Style()->GetPseudoType() ==
               PseudoStyleType::tableCell) ||
          (pseudoType == PseudoStyleType::tableWrapper &&
           (aFrame->PrincipalChildList()
                    .FirstChild()
                    ->Style()
                    ->GetPseudoType() == PseudoStyleType::table ||
            aFrame->PrincipalChildList()
                    .FirstChild()
                    ->Style()
                    ->GetPseudoType() == PseudoStyleType::inlineTable)));
}

static bool IsRubyPseudo(nsIFrame* aFrame) {
  return RubyUtils::IsRubyPseudo(aFrame->Style()->GetPseudoType());
}

static bool IsTableOrRubyPseudo(nsIFrame* aFrame) {
  return IsTablePseudo(aFrame) || IsRubyPseudo(aFrame);
}

/* static */
nsCSSFrameConstructor::ParentType nsCSSFrameConstructor::GetParentType(
    LayoutFrameType aFrameType) {
  if (aFrameType == LayoutFrameType::Table) {
    return eTypeTable;
  }
  if (aFrameType == LayoutFrameType::TableRowGroup) {
    return eTypeRowGroup;
  }
  if (aFrameType == LayoutFrameType::TableRow) {
    return eTypeRow;
  }
  if (aFrameType == LayoutFrameType::TableColGroup) {
    return eTypeColGroup;
  }
  if (aFrameType == LayoutFrameType::RubyBaseContainer) {
    return eTypeRubyBaseContainer;
  }
  if (aFrameType == LayoutFrameType::RubyTextContainer) {
    return eTypeRubyTextContainer;
  }
  if (aFrameType == LayoutFrameType::Ruby) {
    return eTypeRuby;
  }

  return eTypeBlock;
}

// Pull all the captions present in aItems out into aCaptions.
static void PullOutCaptionFrames(nsFrameList& aList, nsFrameList& aCaptions) {
  nsIFrame* child = aList.FirstChild();
  while (child) {
    nsIFrame* nextSibling = child->GetNextSibling();
    if (child->StyleDisplay()->mDisplay == StyleDisplay::TableCaption) {
      aList.RemoveFrame(child);
      aCaptions.AppendFrame(nullptr, child);
    }
    child = nextSibling;
  }
}

// Construct the outer, inner table frames and the children frames for the
// table.
// XXX Page break frames for pseudo table frames are not constructed to avoid
// the risk associated with revising the pseudo frame mechanism. The long term
// solution of having frames handle page-break-before/after will solve the
// problem.
nsIFrame* nsCSSFrameConstructor::ConstructTable(nsFrameConstructorState& aState,
                                                FrameConstructionItem& aItem,
                                                nsContainerFrame* aParentFrame,
                                                const nsStyleDisplay* aDisplay,
                                                nsFrameList& aFrameList) {
  MOZ_ASSERT(aDisplay->mDisplay == StyleDisplay::Table ||
                 aDisplay->mDisplay == StyleDisplay::InlineTable,
             "Unexpected call");

  nsIContent* const content = aItem.mContent;
  ComputedStyle* const computedStyle = aItem.mComputedStyle;
  const bool isMathMLContent = content->IsMathMLElement();

  // create the pseudo SC for the table wrapper as a child of the inner SC
  RefPtr<ComputedStyle> outerComputedStyle =
      mPresShell->StyleSet()->ResolveInheritingAnonymousBoxStyle(
          PseudoStyleType::tableWrapper, computedStyle);

  // Create the table wrapper frame which holds the caption and inner table
  // frame
  nsContainerFrame* newFrame;
  if (isMathMLContent)
    newFrame = NS_NewMathMLmtableOuterFrame(mPresShell, outerComputedStyle);
  else
    newFrame = NS_NewTableWrapperFrame(mPresShell, outerComputedStyle);

  nsContainerFrame* geometricParent = aState.GetGeometricParent(
      *outerComputedStyle->StyleDisplay(), aParentFrame);

  // Init the table wrapper frame
  InitAndRestoreFrame(aState, content, geometricParent, newFrame);

  // Create the inner table frame
  nsContainerFrame* innerFrame;
  if (isMathMLContent)
    innerFrame = NS_NewMathMLmtableFrame(mPresShell, computedStyle);
  else
    innerFrame = NS_NewTableFrame(mPresShell, computedStyle);

  InitAndRestoreFrame(aState, content, newFrame, innerFrame);
  innerFrame->AddStateBits(NS_FRAME_OWNS_ANON_BOXES);

  // Put the newly created frames into the right child list
  SetInitialSingleChild(newFrame, innerFrame);

  aState.AddChild(newFrame, aFrameList, content, aParentFrame);

  if (!mRootElementFrame) {
    mRootElementFrame = newFrame;
  }

  nsFrameList childList;

  // Process children
  nsFrameConstructorSaveState absoluteSaveState;

  // Mark the table frame as an absolute container if needed
  newFrame->AddStateBits(NS_FRAME_CAN_HAVE_ABSPOS_CHILDREN);
  if (newFrame->IsAbsPosContainingBlock()) {
    aState.PushAbsoluteContainingBlock(newFrame, newFrame, absoluteSaveState);
  }

  nsFrameConstructorSaveState floatSaveState;
  aState.MaybePushFloatContainingBlock(innerFrame, floatSaveState);

  if (aItem.mFCData->mBits & FCDATA_USE_CHILD_ITEMS) {
    ConstructFramesFromItemList(
        aState, aItem.mChildItems, innerFrame,
        aItem.mFCData->mBits & FCDATA_IS_WRAPPER_ANON_BOX, childList);
  } else {
    ProcessChildren(aState, content, computedStyle, innerFrame, true, childList,
                    false);
  }

  nsFrameList captionList;
  PullOutCaptionFrames(childList, captionList);

  // Set the inner table frame's initial primary list
  innerFrame->SetInitialChildList(FrameChildListID::Principal,
                                  std::move(childList));

  // Set the table wrapper frame's secondary childlist lists
  if (captionList.NotEmpty()) {
    captionList.ApplySetParent(newFrame);
    newFrame->SetInitialChildList(FrameChildListID::Caption,
                                  std::move(captionList));
  }

  return newFrame;
}

static void MakeTablePartAbsoluteContainingBlock(
    nsFrameConstructorState& aState, nsFrameConstructorSaveState& aAbsSaveState,
    nsContainerFrame* aFrame) {
  // If we're positioned, then we need to become an absolute containing block
  // for any absolutely positioned children.
  aFrame->AddStateBits(NS_FRAME_CAN_HAVE_ABSPOS_CHILDREN);
  if (aFrame->IsAbsPosContainingBlock()) {
    aState.PushAbsoluteContainingBlock(aFrame, aFrame, aAbsSaveState);
  }
}

nsIFrame* nsCSSFrameConstructor::ConstructTableRowOrRowGroup(
    nsFrameConstructorState& aState, FrameConstructionItem& aItem,
    nsContainerFrame* aParentFrame, const nsStyleDisplay* aDisplay,
    nsFrameList& aFrameList) {
  MOZ_ASSERT(aDisplay->mDisplay == StyleDisplay::TableRow ||
                 aDisplay->mDisplay == StyleDisplay::TableRowGroup ||
                 aDisplay->mDisplay == StyleDisplay::TableFooterGroup ||
                 aDisplay->mDisplay == StyleDisplay::TableHeaderGroup,
             "Not a row or row group");
  MOZ_ASSERT(aItem.mComputedStyle->StyleDisplay() == aDisplay,
             "Display style doesn't match style");
  nsIContent* const content = aItem.mContent;
  ComputedStyle* const computedStyle = aItem.mComputedStyle;

  nsContainerFrame* newFrame;
  if (aDisplay->mDisplay == StyleDisplay::TableRow) {
    if (content->IsMathMLElement())
      newFrame = NS_NewMathMLmtrFrame(mPresShell, computedStyle);
    else
      newFrame = NS_NewTableRowFrame(mPresShell, computedStyle);
  } else {
    newFrame = NS_NewTableRowGroupFrame(mPresShell, computedStyle);
  }

  InitAndRestoreFrame(aState, content, aParentFrame, newFrame);

  nsFrameConstructorSaveState absoluteSaveState;
  MakeTablePartAbsoluteContainingBlock(aState, absoluteSaveState, newFrame);

  nsFrameConstructorSaveState floatSaveState;
  aState.MaybePushFloatContainingBlock(newFrame, floatSaveState);

  nsFrameList childList;
  if (aItem.mFCData->mBits & FCDATA_USE_CHILD_ITEMS) {
    ConstructFramesFromItemList(
        aState, aItem.mChildItems, newFrame,
        aItem.mFCData->mBits & FCDATA_IS_WRAPPER_ANON_BOX, childList);
  } else {
    ProcessChildren(aState, content, computedStyle, newFrame, true, childList,
                    false);
  }

  newFrame->SetInitialChildList(FrameChildListID::Principal,
                                std::move(childList));
  aFrameList.AppendFrame(nullptr, newFrame);
  return newFrame;
}

nsIFrame* nsCSSFrameConstructor::ConstructTableCol(
    nsFrameConstructorState& aState, FrameConstructionItem& aItem,
    nsContainerFrame* aParentFrame, const nsStyleDisplay* aStyleDisplay,
    nsFrameList& aFrameList) {
  nsIContent* const content = aItem.mContent;
  ComputedStyle* const computedStyle = aItem.mComputedStyle;

  nsTableColFrame* colFrame = NS_NewTableColFrame(mPresShell, computedStyle);
  InitAndRestoreFrame(aState, content, aParentFrame, colFrame);

  NS_ASSERTION(colFrame->Style() == computedStyle, "Unexpected style");

  aFrameList.AppendFrame(nullptr, colFrame);

  // construct additional col frames if the col frame has a span > 1
  int32_t span = colFrame->GetSpan();
  for (int32_t spanX = 1; spanX < span; spanX++) {
    nsTableColFrame* newCol = NS_NewTableColFrame(mPresShell, computedStyle);
    InitAndRestoreFrame(aState, content, aParentFrame, newCol, false);
    aFrameList.LastChild()->SetNextContinuation(newCol);
    newCol->SetPrevContinuation(aFrameList.LastChild());
    aFrameList.AppendFrame(nullptr, newCol);
    newCol->SetColType(eColAnonymousCol);
  }

  return colFrame;
}

nsIFrame* nsCSSFrameConstructor::ConstructTableCell(
    nsFrameConstructorState& aState, FrameConstructionItem& aItem,
    nsContainerFrame* aParentFrame, const nsStyleDisplay* aDisplay,
    nsFrameList& aFrameList) {
  MOZ_ASSERT(aDisplay->mDisplay == StyleDisplay::TableCell, "Unexpected call");

  nsIContent* const content = aItem.mContent;
  ComputedStyle* const computedStyle = aItem.mComputedStyle;
  const bool isMathMLContent = content->IsMathMLElement();

  nsTableFrame* tableFrame =
      static_cast<nsTableRowFrame*>(aParentFrame)->GetTableFrame();
  nsContainerFrame* cellFrame;
  // <mtable> is border separate in mathml.css and the MathML code doesn't
  // implement border collapse. For those users who style <mtable> with border
  // collapse, give them the default non-MathML table frames that understand
  // border collapse. This won't break us because MathML table frames are all
  // subclasses of the default table code, and so we can freely mix <mtable>
  // with <mtr> or <tr>, <mtd> or <td>. What will happen is just that non-MathML
  // frames won't understand MathML attributes and will therefore miss the
  // special handling that the MathML code does.
  if (isMathMLContent && !tableFrame->IsBorderCollapse()) {
    cellFrame = NS_NewMathMLmtdFrame(mPresShell, computedStyle, tableFrame);
  } else {
    // Warning: If you change this and add a wrapper frame around table cell
    // frames, make sure Bug 368554 doesn't regress!
    // See IsInAutoWidthTableCellForQuirk() in nsImageFrame.cpp.
    cellFrame = NS_NewTableCellFrame(mPresShell, computedStyle, tableFrame);
  }

  // Initialize the table cell frame
  InitAndRestoreFrame(aState, content, aParentFrame, cellFrame);
  cellFrame->AddStateBits(NS_FRAME_OWNS_ANON_BOXES);

  // Resolve pseudo style and initialize the body cell frame
  RefPtr<ComputedStyle> innerPseudoStyle =
      mPresShell->StyleSet()->ResolveInheritingAnonymousBoxStyle(
          PseudoStyleType::cellContent, computedStyle);

  nsContainerFrame* cellInnerFrame;
  nsContainerFrame* scrollFrame = nullptr;
  bool isScrollable = false;
  // Create a block frame that will format the cell's content
  if (isMathMLContent) {
    cellInnerFrame = NS_NewMathMLmtdInnerFrame(mPresShell, innerPseudoStyle);
  } else {
    isScrollable = innerPseudoStyle->StyleDisplay()->IsScrollableOverflow() &&
                   !aState.mPresContext->IsPaginated() &&
                   StaticPrefs::layout_tables_scrollable_cells();
    if (isScrollable) {
      innerPseudoStyle = BeginBuildingScrollFrame(
          aState, content, innerPseudoStyle, cellFrame,
          PseudoStyleType::scrolledContent, false, scrollFrame);
    }
    cellInnerFrame = NS_NewBlockFrame(mPresShell, innerPseudoStyle);
  }
  auto* parent = scrollFrame ? scrollFrame : cellFrame;
  InitAndRestoreFrame(aState, content, parent, cellInnerFrame);

  nsFrameConstructorSaveState absoluteSaveState;
  MakeTablePartAbsoluteContainingBlock(aState, absoluteSaveState, cellFrame);

  nsFrameConstructorSaveState floatSaveState;
  aState.MaybePushFloatContainingBlock(cellInnerFrame, floatSaveState);

  nsFrameList childList;
  if (aItem.mFCData->mBits & FCDATA_USE_CHILD_ITEMS) {
    AutoFrameConstructionPageName pageNameTracker(aState, cellInnerFrame);
    ConstructFramesFromItemList(
        aState, aItem.mChildItems, cellInnerFrame,
        aItem.mFCData->mBits & FCDATA_IS_WRAPPER_ANON_BOX, childList);
  } else {
    // Process the child content
    ProcessChildren(aState, content, computedStyle, cellInnerFrame, true,
                    childList, !isMathMLContent);
  }

  cellInnerFrame->SetInitialChildList(FrameChildListID::Principal,
                                      std::move(childList));

  if (isScrollable) {
    FinishBuildingScrollFrame(scrollFrame, cellInnerFrame);
  }
  SetInitialSingleChild(cellFrame, scrollFrame ? scrollFrame : cellInnerFrame);
  aFrameList.AppendFrame(nullptr, cellFrame);
  return cellFrame;
}

static inline bool NeedFrameFor(const nsFrameConstructorState& aState,
                                nsContainerFrame* aParentFrame,
                                nsIContent* aChildContent) {
  // XXX the GetContent() != aChildContent check is needed due to bug 135040.
  // Remove it once that's fixed.
  MOZ_ASSERT(
      !aChildContent->GetPrimaryFrame() || aState.mCreatingExtraFrames ||
          aChildContent->GetPrimaryFrame()->GetContent() != aChildContent,
      "Why did we get called?");

  // don't create a whitespace frame if aParentFrame doesn't want it.
  // always create frames for children in generated content. counter(),
  // quotes, and attr() content can easily change dynamically and we don't
  // want to be reconstructing frames. It's not even clear that these
  // should be considered ignorable just because they evaluate to
  // whitespace.

  // We could handle all this in CreateNeededPseudoContainers or some other
  // place after we build our frame construction items, but that would involve
  // creating frame construction items for whitespace kids that ignores
  // white-space, where we know we'll be dropping them all anyway, and involve
  // an extra walk down the frame construction item list.
  auto excludesIgnorableWhitespace = [](nsIFrame* aParentFrame) {
    return aParentFrame->IsMathMLFrame();
  };
  if (!aParentFrame || !excludesIgnorableWhitespace(aParentFrame) ||
      aParentFrame->IsGeneratedContentFrame() || !aChildContent->IsText()) {
    return true;
  }

  aChildContent->SetFlags(NS_CREATE_FRAME_IF_NON_WHITESPACE |
                          NS_REFRAME_IF_WHITESPACE);
  return !aChildContent->TextIsOnlyWhitespace();
}

/***********************************************
 * END TABLE SECTION
 ***********************************************/

nsIFrame* nsCSSFrameConstructor::ConstructDocElementFrame(
    Element* aDocElement) {
  MOZ_ASSERT(GetRootFrame(),
             "No viewport?  Someone forgot to call ConstructRootFrame!");
  MOZ_ASSERT(!mDocElementContainingBlock,
             "Shouldn't have a doc element containing block here");

  // Resolve a new style for the viewport since it may be affected by a new root
  // element style (e.g. a propagated 'direction').
  //
  // @see ComputedStyle::ApplyStyleFixups
  {
    RefPtr<ComputedStyle> sc =
        mPresShell->StyleSet()->ResolveInheritingAnonymousBoxStyle(
            PseudoStyleType::viewport, nullptr);
    GetRootFrame()->SetComputedStyleWithoutNotification(sc);
  }

  // Ensure the document element is styled at this point.
  if (!aDocElement->HasServoData()) {
    mPresShell->StyleSet()->StyleNewSubtree(aDocElement);
  }
  aDocElement->UnsetFlags(NODE_DESCENDANTS_NEED_FRAMES | NODE_NEEDS_FRAME);

  // Make sure to call UpdateViewportScrollStylesOverride before
  // SetUpDocElementContainingBlock, since it sets up our scrollbar state
  // properly.
  DebugOnly<nsIContent*> propagatedScrollFrom;
  if (nsPresContext* presContext = mPresShell->GetPresContext()) {
    propagatedScrollFrom = presContext->UpdateViewportScrollStylesOverride();
  }

  SetUpDocElementContainingBlock(aDocElement);

  // This has the side-effect of getting `mFrameTreeState` from our docshell.
  //
  // FIXME(emilio): There may be a more sensible time to do this.
  if (!mFrameTreeState) {
    mPresShell->CaptureHistoryState(getter_AddRefs(mFrameTreeState));
  }

  NS_ASSERTION(mDocElementContainingBlock, "Should have parent by now");
  nsFrameConstructorState state(
      mPresShell,
      GetAbsoluteContainingBlock(mDocElementContainingBlock, FIXED_POS),
      nullptr, nullptr, do_AddRef(mFrameTreeState));

  RefPtr<ComputedStyle> computedStyle =
      ServoStyleSet::ResolveServoStyle(*aDocElement);

  const nsStyleDisplay* display = computedStyle->StyleDisplay();

  // --------- IF SCROLLABLE WRAP IN SCROLLFRAME --------

  NS_ASSERTION(!display->IsScrollableOverflow() ||
                   state.mPresContext->IsPaginated() ||
                   propagatedScrollFrom == aDocElement,
               "Scrollbars should have been propagated to the viewport");

  if (MOZ_UNLIKELY(display->mDisplay == StyleDisplay::None)) {
    return nullptr;
  }

  // This implements "The Principal Writing Mode".
  // https://drafts.csswg.org/css-writing-modes-3/#principal-flow
  //
  // If there's a <body> element in an HTML document, its writing-mode,
  // direction, and text-orientation override the root element's used value.
  //
  // We need to copy <body>'s WritingMode to mDocElementContainingBlock before
  // construct mRootElementFrame so that anonymous internal frames such as
  // <html> with table style can copy their parent frame's mWritingMode in
  // nsIFrame::Init().
  MOZ_ASSERT(!mRootElementFrame,
             "We need to copy <body>'s principal writing-mode before "
             "constructing mRootElementFrame.");

  const WritingMode propagatedWM = [&] {
    const WritingMode rootWM(computedStyle);
    if (computedStyle->StyleDisplay()->IsContainAny()) {
      return rootWM;
    }
    Element* body = mDocument->GetBodyElement();
    if (!body) {
      return rootWM;
    }
    RefPtr<ComputedStyle> bodyStyle = ResolveComputedStyle(body);
    if (bodyStyle->StyleDisplay()->IsContainAny()) {
      return rootWM;
    }
    const WritingMode bodyWM(bodyStyle);
    if (bodyWM != rootWM) {
      nsContentUtils::ReportToConsole(nsIScriptError::warningFlag, "Layout"_ns,
                                      mDocument,
                                      nsContentUtils::eLAYOUT_PROPERTIES,
                                      "PrincipalWritingModePropagationWarning");
    }
    return bodyWM;
  }();

  mDocElementContainingBlock->PropagateWritingModeToSelfAndAncestors(
      propagatedWM);

  // Push the absolute containing block now so we can absolutely position the
  // root element
  nsFrameConstructorSaveState canvasCbSaveState;
  mCanvasFrame->AddStateBits(NS_FRAME_CAN_HAVE_ABSPOS_CHILDREN);

  state.PushAbsoluteContainingBlock(mCanvasFrame, mCanvasFrame,
                                    canvasCbSaveState);

  nsFrameConstructorSaveState docElementCbSaveState;
  if (mCanvasFrame != mDocElementContainingBlock) {
    mDocElementContainingBlock->AddStateBits(NS_FRAME_CAN_HAVE_ABSPOS_CHILDREN);
    state.PushAbsoluteContainingBlock(mDocElementContainingBlock,
                                      mDocElementContainingBlock,
                                      docElementCbSaveState);
  }

  // The rules from CSS 2.1, section 9.2.4, have already been applied
  // by the style system, so we can assume that display->mDisplay is
  // either NONE, BLOCK, or TABLE.

  // contentFrame is the primary frame for the root element. frameList contains
  // the children of the initial containing block.
  //
  // The first of those frames is usually `contentFrame`, but it can be
  // different, in particular if the root frame is positioned, in which case
  // contentFrame is the out-of-flow frame and frameList.FirstChild() is the
  // placeholder.
  //
  // The rest of the frames in frameList are the anonymous content of the canvas
  // frame.
  nsContainerFrame* contentFrame;
  nsFrameList frameList;
  bool processChildren = false;

  nsFrameConstructorSaveState absoluteSaveState;

  if (aDocElement->IsSVGElement()) {
    if (!aDocElement->IsSVGElement(nsGkAtoms::svg)) {
      return nullptr;
    }
    // We're going to call the right function ourselves, so no need to give a
    // function to this FrameConstructionData.

    // XXXbz on the other hand, if we converted this whole function to
    // FrameConstructionData/Item, then we'd need the right function
    // here... but would probably be able to get away with less code in this
    // function in general.
    static constexpr FrameConstructionData rootSVGData;
    AutoFrameConstructionItem item(this, &rootSVGData, aDocElement,
                                   do_AddRef(computedStyle), true);

    contentFrame = static_cast<nsContainerFrame*>(ConstructOuterSVG(
        state, item, mDocElementContainingBlock, display, frameList));
  } else if (display->mDisplay == StyleDisplay::Flex ||
             display->mDisplay == StyleDisplay::WebkitBox ||
             display->mDisplay == StyleDisplay::Grid) {
    auto func = [&] {
      if (display->mDisplay == StyleDisplay::Grid) {
        return NS_NewGridContainerFrame;
      }
      return NS_NewFlexContainerFrame;
    }();
    contentFrame = func(mPresShell, computedStyle);
    InitAndRestoreFrame(
        state, aDocElement,
        state.GetGeometricParent(*display, mDocElementContainingBlock),
        contentFrame);
    state.AddChild(contentFrame, frameList, aDocElement,
                   mDocElementContainingBlock);
    processChildren = true;

    contentFrame->AddStateBits(NS_FRAME_CAN_HAVE_ABSPOS_CHILDREN);
    if (contentFrame->IsAbsPosContainingBlock()) {
      state.PushAbsoluteContainingBlock(contentFrame, contentFrame,
                                        absoluteSaveState);
    }
  } else if (display->mDisplay == StyleDisplay::Table) {
    // We're going to call the right function ourselves, so no need to give a
    // function to this FrameConstructionData.

    // XXXbz on the other hand, if we converted this whole function to
    // FrameConstructionData/Item, then we'd need the right function
    // here... but would probably be able to get away with less code in this
    // function in general.
    static constexpr FrameConstructionData rootTableData;
    AutoFrameConstructionItem item(this, &rootTableData, aDocElement,
                                   do_AddRef(computedStyle), true);

    // if the document is a table then just populate it.
    contentFrame = static_cast<nsContainerFrame*>(ConstructTable(
        state, item, mDocElementContainingBlock, display, frameList));
  } else if (display->DisplayInside() == StyleDisplayInside::Ruby) {
    static constexpr FrameConstructionData data(
        &nsCSSFrameConstructor::ConstructBlockRubyFrame);
    AutoFrameConstructionItem item(this, &data, aDocElement,
                                   do_AddRef(computedStyle), true);
    contentFrame = static_cast<nsContainerFrame*>(ConstructBlockRubyFrame(
        state, item,
        state.GetGeometricParent(*display, mDocElementContainingBlock), display,
        frameList));
  } else {
    MOZ_ASSERT(display->mDisplay == StyleDisplay::Block ||
                   display->mDisplay == StyleDisplay::FlowRoot,
               "Unhandled display type for root element");
    contentFrame = NS_NewBlockFrame(mPresShell, computedStyle);
    ConstructBlock(
        state, aDocElement,
        state.GetGeometricParent(*display, mDocElementContainingBlock),
        mDocElementContainingBlock, computedStyle, &contentFrame, frameList,
        contentFrame->IsAbsPosContainingBlock() ? contentFrame : nullptr);
  }

  MOZ_ASSERT(frameList.FirstChild());
  MOZ_ASSERT(frameList.FirstChild()->GetContent() == aDocElement);
  MOZ_ASSERT(contentFrame);

  MOZ_ASSERT(
      processChildren ? !mRootElementFrame : mRootElementFrame == contentFrame,
      "unexpected mRootElementFrame");
  if (processChildren) {
    mRootElementFrame = contentFrame;
  }

  // Figure out which frame has the main style for the document element,
  // assigning it to mRootElementStyleFrame.
  // Backgrounds should be propagated from that frame to the viewport.
  contentFrame->GetParentComputedStyle(&mRootElementStyleFrame);
  bool isChild = mRootElementStyleFrame &&
                 mRootElementStyleFrame->GetParent() == contentFrame;
  if (!isChild) {
    mRootElementStyleFrame = mRootElementFrame;
  }

  if (processChildren) {
    // Still need to process the child content
    nsFrameList childList;

    NS_ASSERTION(
        !contentFrame->IsBlockFrameOrSubclass() && !contentFrame->IsSVGFrame(),
        "Only XUL frames should reach here");

    nsFrameConstructorSaveState floatSaveState;
    state.MaybePushFloatContainingBlock(contentFrame, floatSaveState);

    ProcessChildren(state, aDocElement, computedStyle, contentFrame, true,
                    childList, false);

    // Set the initial child lists
    contentFrame->SetInitialChildList(FrameChildListID::Principal,
                                      std::move(childList));
  }

  nsIFrame* newFrame = frameList.FirstChild();
  // set the primary frame
  aDocElement->SetPrimaryFrame(contentFrame);
  mDocElementContainingBlock->AppendFrames(FrameChildListID::Principal,
                                           std::move(frameList));

  // NOTE(emilio): This is in the reverse order compared to normal anonymous
  // children. We usually generate anonymous kids first, then non-anonymous,
  // but we generate the doc element frame the other way around. This is fine
  // either way, but generating anonymous children in a different order requires
  // changing nsCanvasFrame (and a whole lot of other potentially unknown code)
  // to look at the last child to find the root frame rather than the first
  // child.
  ConstructAnonymousContentForCanvas(
      state, mCanvasFrame, mRootElementFrame->GetContent(), frameList);
  mCanvasFrame->AppendFrames(FrameChildListID::Principal, std::move(frameList));

  return newFrame;
}

nsIFrame* nsCSSFrameConstructor::ConstructRootFrame() {
  AUTO_PROFILER_LABEL_HOT("nsCSSFrameConstructor::ConstructRootFrame",
                          LAYOUT_FrameConstruction);
  AUTO_LAYOUT_PHASE_ENTRY_POINT(mPresShell->GetPresContext(), FrameC);

  ServoStyleSet* styleSet = mPresShell->StyleSet();

  // --------- BUILD VIEWPORT -----------
  RefPtr<ComputedStyle> viewportPseudoStyle =
      styleSet->ResolveInheritingAnonymousBoxStyle(PseudoStyleType::viewport,
                                                   nullptr);
  ViewportFrame* viewportFrame =
      NS_NewViewportFrame(mPresShell, viewportPseudoStyle);

  // XXXbz do we _have_ to pass a null content pointer to that frame?
  // Would it really kill us to pass in the root element or something?
  // What would that break?
  viewportFrame->Init(nullptr, nullptr, nullptr);

  viewportFrame->AddStateBits(NS_FRAME_OWNS_ANON_BOXES);

  // Bind the viewport frame to the root view
  if (nsView* rootView = mPresShell->GetViewManager()->GetRootView()) {
    viewportFrame->SetView(rootView);
    viewportFrame->SyncFrameViewProperties(rootView);
    rootView->SetNeedsWindowPropertiesSync();
  }

  // Make it an absolute container for fixed-pos elements
  viewportFrame->AddStateBits(NS_FRAME_CAN_HAVE_ABSPOS_CHILDREN);
  viewportFrame->MarkAsAbsoluteContainingBlock();

  return viewportFrame;
}

void nsCSSFrameConstructor::SetUpDocElementContainingBlock(
    nsIContent* aDocElement) {
  MOZ_ASSERT(aDocElement, "No element?");
  MOZ_ASSERT(!aDocElement->GetParent(), "Not root content?");
  MOZ_ASSERT(aDocElement->GetUncomposedDoc(), "Not in a document?");
  MOZ_ASSERT(aDocElement->GetUncomposedDoc()->GetRootElement() == aDocElement,
             "Not the root of the document?");

  /*
    how the root frame hierarchy should look

  Galley presentation, with scrolling:

      ViewportFrame [fixed-cb]
        nsHTMLScrollFrame (if needed)
          nsCanvasFrame [abs-cb]
            root element frame (nsBlockFrame, SVGOuterSVGFrame,
                                nsTableWrapperFrame, nsPlaceholderFrame)

  Print presentation, non-XUL

      ViewportFrame
        nsCanvasFrame
          nsPageSequenceFrame
            PrintedSheetFrame
              nsPageFrame
                nsPageContentFrame [fixed-cb]
                  nsCanvasFrame [abs-cb]
                    root element frame (nsBlockFrame, SVGOuterSVGFrame,
                                        nsTableWrapperFrame, nsPlaceholderFrame)

  Print-preview presentation, non-XUL

      ViewportFrame
        nsHTMLScrollFrame
          nsCanvasFrame
            nsPageSequenceFrame
              PrintedSheetFrame
                nsPageFrame
                  nsPageContentFrame [fixed-cb]
                    nsCanvasFrame [abs-cb]
                      root element frame (nsBlockFrame, SVGOuterSVGFrame,
                                          nsTableWrapperFrame,
                                          nsPlaceholderFrame)

  Print/print preview of XUL is not supported.
  [fixed-cb]: the default containing block for fixed-pos content
  [abs-cb]: the default containing block for abs-pos content

  Meaning of nsCSSFrameConstructor fields:
    mRootElementFrame is "root element frame".  This is the primary frame for
      the root element.
    mDocElementContainingBlock is the parent of mRootElementFrame
      (i.e. nsCanvasFrame)
    mPageSequenceFrame is the nsPageSequenceFrame, or null if there isn't
      one
  */

  // --------- CREATE ROOT FRAME -------

  // Create the root frame. The document element's frame is a child of the
  // root frame.
  //
  // The root frame serves two purposes:
  // - reserves space for any margins needed for the document element's frame
  // - renders the document element's background. This ensures the background
  //   covers the entire canvas as specified by the CSS2 spec

  nsPresContext* presContext = mPresShell->GetPresContext();
  const bool isPaginated = presContext->IsRootPaginatedDocument();

  const bool isHTML = aDocElement->IsHTMLElement();
  const bool isXUL = !isHTML && aDocElement->IsXULElement();

  const bool isScrollable = [&] {
    if (isPaginated) {
      return presContext->HasPaginatedScrolling();
    }
    // Never create scrollbars for XUL documents or top level XHTML documents
    // that disable scrolling.
    if (isXUL) {
      return false;
    }
    if (aDocElement->OwnerDoc()->ChromeRulesEnabled() &&
        aDocElement->AsElement()->AttrValueIs(
            kNameSpaceID_None, nsGkAtoms::scrolling, nsGkAtoms::_false,
            eCaseMatters)) {
      return false;
    }
    return true;
  }();

  nsContainerFrame* viewportFrame =
      static_cast<nsContainerFrame*>(GetRootFrame());
  ComputedStyle* viewportPseudoStyle = viewportFrame->Style();

  nsCanvasFrame* rootCanvasFrame =
      NS_NewCanvasFrame(mPresShell, viewportPseudoStyle);
  PseudoStyleType rootPseudo = PseudoStyleType::canvas;
  mCanvasFrame = rootCanvasFrame;
  mDocElementContainingBlock = rootCanvasFrame;

  // --------- IF SCROLLABLE WRAP IN SCROLLFRAME --------

  // If the device supports scrolling (e.g., in galley mode on the screen and
  // for print-preview, but not when printing), then create a scroll frame that
  // will act as the scrolling mechanism for the viewport.
  // XXX Do we even need a viewport when printing to a printer?

  // We no longer need to do overflow propagation here. It's taken care of
  // when we construct frames for the element whose overflow might be
  // propagated
  NS_ASSERTION(!isScrollable || !isXUL,
               "XUL documents should never be scrollable - see above");

  nsContainerFrame* newFrame = rootCanvasFrame;
  RefPtr<ComputedStyle> rootPseudoStyle;
  // we must create a state because if the scrollbars are GFX it needs the
  // state to build the scrollbar frames.
  nsFrameConstructorState state(mPresShell, nullptr, nullptr, nullptr);

  // Start off with the viewport as parent; we'll adjust it as needed.
  nsContainerFrame* parentFrame = viewportFrame;

  ServoStyleSet* styleSet = mPresShell->StyleSet();
  // If paginated, make sure we don't put scrollbars in
  if (!isScrollable) {
    rootPseudoStyle = styleSet->ResolveInheritingAnonymousBoxStyle(
        rootPseudo, viewportPseudoStyle);
  } else {
    rootPseudo = PseudoStyleType::scrolledCanvas;

    // Build the frame. We give it the content we are wrapping which is the
    // document element, the root frame, the parent view port frame, and we
    // should get back the new frame and the scrollable view if one was
    // created.

    // resolve a context for the scrollframe
    RefPtr<ComputedStyle> computedStyle =
        styleSet->ResolveInheritingAnonymousBoxStyle(
            PseudoStyleType::viewportScroll, viewportPseudoStyle);

    // Note that the viewport scrollframe is always built with
    // overflow:auto style. This forces the scroll frame to create
    // anonymous content for both scrollbars. This is necessary even
    // if the HTML or BODY elements are overriding the viewport
    // scroll style to 'hidden' --- dynamic style changes might put
    // scrollbars back on the viewport and we don't want to have to
    // reframe the viewport to create the scrollbar content.
    newFrame = nullptr;
    rootPseudoStyle =
        BeginBuildingScrollFrame(state, aDocElement, computedStyle,
                                 viewportFrame, rootPseudo, true, newFrame);
    parentFrame = newFrame;
  }

  rootCanvasFrame->SetComputedStyleWithoutNotification(rootPseudoStyle);
  rootCanvasFrame->Init(aDocElement, parentFrame, nullptr);

  if (isScrollable) {
    FinishBuildingScrollFrame(parentFrame, rootCanvasFrame);
  }

  if (isPaginated) {
    // Create a page sequence frame
    {
      RefPtr<ComputedStyle> pageSequenceStyle =
          styleSet->ResolveInheritingAnonymousBoxStyle(
              PseudoStyleType::pageSequence, viewportPseudoStyle);
      mPageSequenceFrame =
          NS_NewPageSequenceFrame(mPresShell, pageSequenceStyle);
      mPageSequenceFrame->Init(aDocElement, rootCanvasFrame, nullptr);
      SetInitialSingleChild(rootCanvasFrame, mPageSequenceFrame);
    }

    // Create the first printed sheet frame, as the sole child (for now) of our
    // page sequence frame (mPageSequenceFrame).
    auto* printedSheetFrame =
        ConstructPrintedSheetFrame(mPresShell, mPageSequenceFrame, nullptr);
    SetInitialSingleChild(mPageSequenceFrame, printedSheetFrame);

    MOZ_ASSERT(!mNextPageContentFramePageName,
               "Next page name should not have been set.");

    // Create the first page, as the sole child (for now) of the printed sheet
    // frame that we just created.
    nsCanvasFrame* canvasFrame;
    nsContainerFrame* pageFrame =
        ConstructPageFrame(mPresShell, printedSheetFrame, nullptr, canvasFrame);
    pageFrame->AddStateBits(NS_FRAME_OWNS_ANON_BOXES);
    SetInitialSingleChild(printedSheetFrame, pageFrame);

    // The eventual parent of the document element frame.
    // XXX should this be set for every new page (in ConstructPageFrame)?
    mDocElementContainingBlock = canvasFrame;
  }

  if (viewportFrame->HasAnyStateBits(NS_FRAME_FIRST_REFLOW)) {
    SetInitialSingleChild(viewportFrame, newFrame);
  } else {
    viewportFrame->AppendFrames(FrameChildListID::Principal,
                                nsFrameList(newFrame, newFrame));
  }
}

void nsCSSFrameConstructor::ConstructAnonymousContentForCanvas(
    nsFrameConstructorState& aState, nsContainerFrame* aFrame,
    nsIContent* aDocElement, nsFrameList& aFrameList) {
  NS_ASSERTION(aFrame->IsCanvasFrame(), "aFrame should be canvas frame!");
  MOZ_ASSERT(mRootElementFrame->GetContent() == aDocElement);

  AutoTArray<nsIAnonymousContentCreator::ContentInfo, 4> anonymousItems;
  GetAnonymousContent(aDocElement, aFrame, anonymousItems);
  if (anonymousItems.IsEmpty()) {
    return;
  }

  AutoFrameConstructionItemList itemsToConstruct(this);
  AutoFrameConstructionPageName pageNameTracker(aState, aFrame);
  AddFCItemsForAnonymousContent(aState, aFrame, anonymousItems,
                                itemsToConstruct, pageNameTracker);
  ConstructFramesFromItemList(aState, itemsToConstruct, aFrame,
                              /* aParentIsWrapperAnonBox = */ false,
                              aFrameList);
}

PrintedSheetFrame* nsCSSFrameConstructor::ConstructPrintedSheetFrame(
    PresShell* aPresShell, nsContainerFrame* aParentFrame,
    nsIFrame* aPrevSheetFrame) {
  RefPtr<ComputedStyle> printedSheetPseudoStyle =
      aPresShell->StyleSet()->ResolveNonInheritingAnonymousBoxStyle(
          PseudoStyleType::printedSheet);

  auto* printedSheetFrame =
      NS_NewPrintedSheetFrame(aPresShell, printedSheetPseudoStyle);

  printedSheetFrame->Init(nullptr, aParentFrame, aPrevSheetFrame);

  return printedSheetFrame;
}

nsContainerFrame* nsCSSFrameConstructor::ConstructPageFrame(
    PresShell* aPresShell, nsContainerFrame* aParentFrame,
    nsIFrame* aPrevPageFrame, nsCanvasFrame*& aCanvasFrame) {
  ServoStyleSet* styleSet = aPresShell->StyleSet();

  RefPtr<ComputedStyle> pagePseudoStyle =
      styleSet->ResolveNonInheritingAnonymousBoxStyle(PseudoStyleType::page);

  nsContainerFrame* pageFrame = NS_NewPageFrame(aPresShell, pagePseudoStyle);

  // Initialize the page frame and force it to have a view. This makes printing
  // of the pages easier and faster.
  pageFrame->Init(nullptr, aParentFrame, aPrevPageFrame);

  RefPtr<const nsAtom> pageName;
  if (mNextPageContentFramePageName) {
    pageName = mNextPageContentFramePageName.forget();
  } else if (aPrevPageFrame) {
    pageName = aPrevPageFrame->ComputePageValue();
    MOZ_ASSERT(pageName,
               "Page name from prev-in-flow should not have been null");
  }
  RefPtr<ComputedStyle> pageContentPseudoStyle =
      styleSet->ResolvePageContentStyle(pageName,
                                        StylePagePseudoClassFlags::NONE);

  nsContainerFrame* pageContentFrame = NS_NewPageContentFrame(
      aPresShell, pageContentPseudoStyle, pageName.forget());

  nsPageContentFrame* prevPageContentFrame = nullptr;
  if (aPrevPageFrame) {
    MOZ_ASSERT(aPrevPageFrame->IsPageFrame());
    prevPageContentFrame =
        static_cast<nsPageFrame*>(aPrevPageFrame)->PageContentFrame();
  }
  pageContentFrame->Init(nullptr, pageFrame, prevPageContentFrame);
  if (!prevPageContentFrame) {
    // The canvas is an inheriting anon box, so needs to be "owned" by the page
    // content.
    pageContentFrame->AddStateBits(NS_FRAME_OWNS_ANON_BOXES);
  }
  SetInitialSingleChild(pageFrame, pageContentFrame);
  // Make it an absolute container for fixed-pos elements
  pageContentFrame->AddStateBits(NS_FRAME_CAN_HAVE_ABSPOS_CHILDREN);
  pageContentFrame->MarkAsAbsoluteContainingBlock();

  RefPtr<ComputedStyle> canvasPseudoStyle =
      styleSet->ResolveInheritingAnonymousBoxStyle(PseudoStyleType::canvas,
                                                   pageContentPseudoStyle);

  aCanvasFrame = NS_NewCanvasFrame(aPresShell, canvasPseudoStyle);

  nsIFrame* prevCanvasFrame = nullptr;
  if (prevPageContentFrame) {
    prevCanvasFrame = prevPageContentFrame->PrincipalChildList().FirstChild();
    NS_ASSERTION(prevCanvasFrame, "missing canvas frame");
  }
  aCanvasFrame->Init(nullptr, pageContentFrame, prevCanvasFrame);
  SetInitialSingleChild(pageContentFrame, aCanvasFrame);
  return pageFrame;
}

/* static */
nsIFrame* nsCSSFrameConstructor::CreatePlaceholderFrameFor(
    PresShell* aPresShell, nsIContent* aContent, nsIFrame* aFrame,
    nsContainerFrame* aParentFrame, nsIFrame* aPrevInFlow,
    nsFrameState aTypeBit) {
  RefPtr<ComputedStyle> placeholderStyle =
      aPresShell->StyleSet()->ResolveStyleForPlaceholder();

  // The placeholder frame gets a pseudo style.
  nsPlaceholderFrame* placeholderFrame =
      NS_NewPlaceholderFrame(aPresShell, placeholderStyle, aTypeBit);

  placeholderFrame->Init(aContent, aParentFrame, aPrevInFlow);

  // Associate the placeholder/out-of-flow with each other.
  placeholderFrame->SetOutOfFlowFrame(aFrame);
  aFrame->SetProperty(nsIFrame::PlaceholderFrameProperty(), placeholderFrame);

  aFrame->AddStateBits(NS_FRAME_OUT_OF_FLOW);

  return placeholderFrame;
}

// Clears any lazy bits set in the range [aStartContent, aEndContent).  If
// aEndContent is null, that means to clear bits in all siblings starting with
// aStartContent.  aStartContent must not be null unless aEndContent is also
// null.  We do this so that when new children are inserted under elements whose
// frame is a leaf the new children don't cause us to try to construct frames
// for the existing children again.
static inline void ClearLazyBits(nsIContent* aStartContent,
                                 nsIContent* aEndContent) {
  MOZ_ASSERT(aStartContent || !aEndContent,
             "Must have start child if we have an end child");

  for (nsIContent* cur = aStartContent; cur != aEndContent;
       cur = cur->GetNextSibling()) {
    cur->UnsetFlags(NODE_DESCENDANTS_NEED_FRAMES | NODE_NEEDS_FRAME);
  }
}

/* static */
const nsCSSFrameConstructor::FrameConstructionData*
nsCSSFrameConstructor::FindSelectData(const Element& aElement,
                                      ComputedStyle& aStyle) {
  // Construct a frame-based listbox or combobox
  const auto* sel = dom::HTMLSelectElement::FromNode(aElement);
  MOZ_ASSERT(sel);
  if (sel->IsCombobox()) {
    static constexpr FrameConstructionData sComboboxData{
        ToCreationFunc(NS_NewComboboxControlFrame), 0,
        PseudoStyleType::buttonContent};
    return &sComboboxData;
  }
  // FIXME: Can we simplify this to avoid needing ConstructListboxSelectFrame,
  // and reuse ConstructScrollableBlock or so?
  static constexpr FrameConstructionData sListBoxData{
      &nsCSSFrameConstructor::ConstructListBoxSelectFrame};
  return &sListBoxData;
}

nsIFrame* nsCSSFrameConstructor::ConstructListBoxSelectFrame(
    nsFrameConstructorState& aState, FrameConstructionItem& aItem,
    nsContainerFrame* aParentFrame, const nsStyleDisplay* aStyleDisplay,
    nsFrameList& aFrameList) {
  nsIContent* const content = aItem.mContent;
  ComputedStyle* const computedStyle = aItem.mComputedStyle;

  // Listbox, not combobox
  nsContainerFrame* listFrame =
      NS_NewListControlFrame(mPresShell, computedStyle);

  nsContainerFrame* scrolledFrame =
      NS_NewSelectsAreaFrame(mPresShell, computedStyle);

  // ******* this code stolen from Initialze ScrollFrame ********
  // please adjust this code to use BuildScrollFrame.

  InitializeListboxSelect(aState, listFrame, scrolledFrame, content,
                          aParentFrame, computedStyle, aFrameList);

  return listFrame;
}

void nsCSSFrameConstructor::InitializeListboxSelect(
    nsFrameConstructorState& aState, nsContainerFrame* scrollFrame,
    nsContainerFrame* scrolledFrame, nsIContent* aContent,
    nsContainerFrame* aParentFrame, ComputedStyle* aComputedStyle,
    nsFrameList& aFrameList) {
  // Initialize it
  nsContainerFrame* geometricParent =
      aState.GetGeometricParent(*aComputedStyle->StyleDisplay(), aParentFrame);

  // We don't call InitAndRestoreFrame for scrollFrame because we can only
  // restore the frame state after its parts have been created (in particular,
  // the scrollable view). So we have to split Init and Restore.

  scrollFrame->Init(aContent, geometricParent, nullptr);
  aState.AddChild(scrollFrame, aFrameList, aContent, aParentFrame);
  BuildScrollFrame(aState, aContent, aComputedStyle, scrolledFrame,
                   geometricParent, scrollFrame);
  if (aState.mFrameState) {
    // Restore frame state for the scroll frame
    RestoreFrameStateFor(scrollFrame, aState.mFrameState);
  }

  nsFrameConstructorSaveState floatSaveState;
  aState.MaybePushFloatContainingBlock(scrolledFrame, floatSaveState);

  // Process children
  nsFrameList childList;

  ProcessChildren(aState, aContent, aComputedStyle, scrolledFrame, false,
                  childList, false);

  // Set the scrolled frame's initial child lists
  scrolledFrame->SetInitialChildList(FrameChildListID::Principal,
                                     std::move(childList));
}

nsIFrame* nsCSSFrameConstructor::ConstructFieldSetFrame(
    nsFrameConstructorState& aState, FrameConstructionItem& aItem,
    nsContainerFrame* aParentFrame, const nsStyleDisplay* aStyleDisplay,
    nsFrameList& aFrameList) {
  AutoRestore<bool> savedHasRenderedLegend(aState.mHasRenderedLegend);
  aState.mHasRenderedLegend = false;
  nsIContent* const content = aItem.mContent;
  ComputedStyle* const computedStyle = aItem.mComputedStyle;

  nsContainerFrame* fieldsetFrame =
      NS_NewFieldSetFrame(mPresShell, computedStyle);

  // Initialize it
  InitAndRestoreFrame(aState, content,
                      aState.GetGeometricParent(*aStyleDisplay, aParentFrame),
                      fieldsetFrame);

  fieldsetFrame->AddStateBits(NS_FRAME_OWNS_ANON_BOXES);

  // Resolve style and initialize the frame
  RefPtr<ComputedStyle> fieldsetContentStyle =
      mPresShell->StyleSet()->ResolveInheritingAnonymousBoxStyle(
          PseudoStyleType::fieldsetContent, computedStyle);

  const nsStyleDisplay* fieldsetContentDisplay =
      fieldsetContentStyle->StyleDisplay();
  const bool isScrollable = fieldsetContentDisplay->IsScrollableOverflow();
  nsContainerFrame* scrollFrame = nullptr;
  if (isScrollable) {
    fieldsetContentStyle = BeginBuildingScrollFrame(
        aState, content, fieldsetContentStyle, fieldsetFrame,
        PseudoStyleType::scrolledContent, false, scrollFrame);
  }

  // Create the inner ::-moz-fieldset-content frame.
  nsContainerFrame* contentFrameTop;
  nsContainerFrame* contentFrame;
  auto* parent = scrollFrame ? scrollFrame : fieldsetFrame;
  MOZ_ASSERT(fieldsetContentDisplay->DisplayOutside() ==
             StyleDisplayOutside::Block);
  switch (fieldsetContentDisplay->DisplayInside()) {
    case StyleDisplayInside::Flex:
      contentFrame = NS_NewFlexContainerFrame(mPresShell, fieldsetContentStyle);
      InitAndRestoreFrame(aState, content, parent, contentFrame);
      contentFrameTop = contentFrame;
      break;
    case StyleDisplayInside::Grid:
      contentFrame = NS_NewGridContainerFrame(mPresShell, fieldsetContentStyle);
      InitAndRestoreFrame(aState, content, parent, contentFrame);
      contentFrameTop = contentFrame;
      break;
    default: {
      MOZ_ASSERT(fieldsetContentDisplay->mDisplay == StyleDisplay::Block,
                 "bug in StyleAdjuster::adjust_for_fieldset_content?");

      contentFrame = NS_NewBlockFrame(mPresShell, fieldsetContentStyle);
      if (fieldsetContentStyle->StyleColumn()->IsColumnContainerStyle()) {
        contentFrameTop = BeginBuildingColumns(
            aState, content, parent, contentFrame, fieldsetContentStyle);
      } else {
        // No need to create column container. Initialize content frame.
        InitAndRestoreFrame(aState, content, parent, contentFrame);
        contentFrameTop = contentFrame;
      }

      break;
    }
  }

  aState.AddChild(fieldsetFrame, aFrameList, content, aParentFrame);

  // Process children
  nsFrameConstructorSaveState absoluteSaveState;
  nsFrameList childList;

  contentFrameTop->AddStateBits(NS_FRAME_CAN_HAVE_ABSPOS_CHILDREN);
  if (fieldsetFrame->IsAbsPosContainingBlock()) {
    aState.PushAbsoluteContainingBlock(contentFrameTop, fieldsetFrame,
                                       absoluteSaveState);
  }

  nsFrameConstructorSaveState floatSaveState;
  aState.MaybePushFloatContainingBlock(contentFrame, floatSaveState);

  ProcessChildren(aState, content, computedStyle, contentFrame, true, childList,
                  true);
  nsFrameList fieldsetKids;
  fieldsetKids.AppendFrame(nullptr,
                           scrollFrame ? scrollFrame : contentFrameTop);

  if (!MayNeedToCreateColumnSpanSiblings(contentFrame, childList)) {
    // Set the inner frame's initial child lists.
    contentFrame->SetInitialChildList(FrameChildListID::Principal,
                                      std::move(childList));
  } else {
    // Extract any initial non-column-span kids, and put them in inner frame's
    // child list.
    nsFrameList initialNonColumnSpanKids =
        childList.Split([](nsIFrame* f) { return f->IsColumnSpan(); });
    contentFrame->SetInitialChildList(FrameChildListID::Principal,
                                      std::move(initialNonColumnSpanKids));

    if (childList.NotEmpty()) {
      nsFrameList columnSpanSiblings = CreateColumnSpanSiblings(
          aState, contentFrame, childList,
          // Column content should never be a absolute/fixed positioned
          // containing block. Pass nullptr as aPositionedFrame.
          nullptr);
      FinishBuildingColumns(aState, contentFrameTop, contentFrame,
                            columnSpanSiblings);
    }
  }

  if (isScrollable) {
    FinishBuildingScrollFrame(scrollFrame, contentFrameTop);
  }

  // We use AppendFrames here because the rendered legend will already
  // be present in the principal child list if it exists.
  fieldsetFrame->AppendFrames(FrameChildListID::NoReflowPrincipal,
                              std::move(fieldsetKids));

  return fieldsetFrame;
}

const nsCSSFrameConstructor::FrameConstructionData*
nsCSSFrameConstructor::FindDetailsData(const Element& aElement,
                                       ComputedStyle& aStyle) {
  if (!StaticPrefs::layout_details_force_block_layout()) {
    return nullptr;
  }
  static constexpr FrameConstructionData sBlockData[2] = {
      {&nsCSSFrameConstructor::ConstructNonScrollableBlock},
      {&nsCSSFrameConstructor::ConstructScrollableBlock},
  };
  return &sBlockData[aStyle.StyleDisplay()->IsScrollableOverflow()];
}

nsIFrame* nsCSSFrameConstructor::ConstructBlockRubyFrame(
    nsFrameConstructorState& aState, FrameConstructionItem& aItem,
    nsContainerFrame* aParentFrame, const nsStyleDisplay* aStyleDisplay,
    nsFrameList& aFrameList) {
  nsIContent* const content = aItem.mContent;
  ComputedStyle* const computedStyle = aItem.mComputedStyle;

  nsBlockFrame* blockFrame = NS_NewBlockFrame(mPresShell, computedStyle);
  nsContainerFrame* newFrame = blockFrame;
  nsContainerFrame* geometricParent =
      aState.GetGeometricParent(*aStyleDisplay, aParentFrame);
  AutoFrameConstructionPageName pageNameTracker(aState, blockFrame);
  if ((aItem.mFCData->mBits & FCDATA_MAY_NEED_SCROLLFRAME) &&
      aStyleDisplay->IsScrollableOverflow()) {
    nsContainerFrame* scrollframe = nullptr;
    BuildScrollFrame(aState, content, computedStyle, blockFrame,
                     geometricParent, scrollframe);
    newFrame = scrollframe;
  } else {
    InitAndRestoreFrame(aState, content, geometricParent, blockFrame);
  }

  RefPtr<ComputedStyle> rubyStyle =
      mPresShell->StyleSet()->ResolveInheritingAnonymousBoxStyle(
          PseudoStyleType::blockRubyContent, computedStyle);
  nsContainerFrame* rubyFrame = NS_NewRubyFrame(mPresShell, rubyStyle);
  InitAndRestoreFrame(aState, content, blockFrame, rubyFrame);
  SetInitialSingleChild(blockFrame, rubyFrame);
  blockFrame->AddStateBits(NS_FRAME_OWNS_ANON_BOXES);

  aState.AddChild(newFrame, aFrameList, content, aParentFrame);

  if (!mRootElementFrame) {
    mRootElementFrame = newFrame;
  }

  nsFrameConstructorSaveState absoluteSaveState;
  blockFrame->AddStateBits(NS_FRAME_CAN_HAVE_ABSPOS_CHILDREN);
  if (newFrame->IsAbsPosContainingBlock()) {
    aState.PushAbsoluteContainingBlock(blockFrame, blockFrame,
                                       absoluteSaveState);
  }
  nsFrameConstructorSaveState floatSaveState;
  aState.MaybePushFloatContainingBlock(blockFrame, floatSaveState);

  nsFrameList childList;
  ProcessChildren(aState, content, rubyStyle, rubyFrame, true, childList, false,
                  nullptr);
  rubyFrame->SetInitialChildList(FrameChildListID::Principal,
                                 std::move(childList));

  return newFrame;
}

static nsIFrame* FindAncestorWithGeneratedContentPseudo(nsIFrame* aFrame) {
  for (nsIFrame* f = aFrame->GetParent(); f; f = f->GetParent()) {
    NS_ASSERTION(f->IsGeneratedContentFrame(),
                 "should not have exited generated content");
    auto pseudo = f->Style()->GetPseudoType();
    if (pseudo == PseudoStyleType::before || pseudo == PseudoStyleType::after ||
        pseudo == PseudoStyleType::marker)
      return f;
  }
  return nullptr;
}

/* static */
const nsCSSFrameConstructor::FrameConstructionData*
nsCSSFrameConstructor::FindTextData(const Text& aTextContent,
                                    nsIFrame* aParentFrame) {
  if (aParentFrame && IsFrameForSVG(aParentFrame)) {
    if (!aParentFrame->IsInSVGTextSubtree()) {
      return nullptr;
    }

    // FIXME(bug 1588477) Don't render stuff in display: contents / Shadow DOM
    // subtrees, because TextCorrespondenceRecorder in the SVG text code doesn't
    // really know how to deal with it. This kinda sucks. :(
    if (aParentFrame->GetContent() != aTextContent.GetParent()) {
      return nullptr;
    }

    static constexpr FrameConstructionData sSVGTextData(
        NS_NewTextFrame, FCDATA_IS_LINE_PARTICIPANT | FCDATA_IS_SVG_TEXT);
    return &sSVGTextData;
  }

  static constexpr FrameConstructionData sTextData(NS_NewTextFrame,
                                                   FCDATA_IS_LINE_PARTICIPANT);
  return &sTextData;
}

void nsCSSFrameConstructor::ConstructTextFrame(
    const FrameConstructionData* aData, nsFrameConstructorState& aState,
    nsIContent* aContent, nsContainerFrame* aParentFrame,
    ComputedStyle* aComputedStyle, nsFrameList& aFrameList) {
  MOZ_ASSERT(aData, "Must have frame construction data");

  nsIFrame* newFrame =
      (*aData->mFunc.mCreationFunc)(mPresShell, aComputedStyle);

  InitAndRestoreFrame(aState, aContent, aParentFrame, newFrame);

  // We never need to create a view for a text frame.

  if (newFrame->IsGeneratedContentFrame()) {
    UniquePtr<nsGenConInitializer> initializer(
        static_cast<nsGenConInitializer*>(
            aContent->TakeProperty(nsGkAtoms::genConInitializerProperty)));
    if (initializer) {
      if (initializer->mNode.release()->InitTextFrame(
              initializer->mList,
              FindAncestorWithGeneratedContentPseudo(newFrame), newFrame)) {
        (this->*(initializer->mDirtyAll))();
      }
    }
  }

  // Add the newly constructed frame to the flow
  aFrameList.AppendFrame(nullptr, newFrame);

  if (!aState.mCreatingExtraFrames || (aContent->IsInNativeAnonymousSubtree() &&
                                       !aContent->GetPrimaryFrame())) {
    aContent->SetPrimaryFrame(newFrame);
  }
}

/* static */
const nsCSSFrameConstructor::FrameConstructionData*
nsCSSFrameConstructor::FindDataByInt(int32_t aInt, const Element& aElement,
                                     ComputedStyle& aComputedStyle,
                                     const FrameConstructionDataByInt* aDataPtr,
                                     uint32_t aDataLength) {
  for (const FrameConstructionDataByInt *curData = aDataPtr,
                                        *endData = aDataPtr + aDataLength;
       curData != endData; ++curData) {
    if (curData->mInt == aInt) {
      const FrameConstructionData* data = &curData->mData;
      if (data->mBits & FCDATA_FUNC_IS_DATA_GETTER) {
        return data->mFunc.mDataGetter(aElement, aComputedStyle);
      }

      return data;
    }
  }

  return nullptr;
}

/* static */
const nsCSSFrameConstructor::FrameConstructionData*
nsCSSFrameConstructor::FindDataByTag(const Element& aElement,
                                     ComputedStyle& aStyle,
                                     const FrameConstructionDataByTag* aDataPtr,
                                     uint32_t aDataLength) {
  const nsAtom* tag = aElement.NodeInfo()->NameAtom();
  for (const FrameConstructionDataByTag *curData = aDataPtr,
                                        *endData = aDataPtr + aDataLength;
       curData != endData; ++curData) {
    if (curData->mTag == tag) {
      const FrameConstructionData* data = &curData->mData;
      if (data->mBits & FCDATA_FUNC_IS_DATA_GETTER) {
        return data->mFunc.mDataGetter(aElement, aStyle);
      }

      return data;
    }
  }

  return nullptr;
}

#define SUPPRESS_FCDATA() FrameConstructionData(nullptr, FCDATA_SUPPRESS_FRAME)
#define SIMPLE_INT_CREATE(_int, _func) \
  { int32_t(_int), FrameConstructionData(_func) }
#define SIMPLE_INT_CHAIN(_int, _func) \
  { int32_t(_int), FrameConstructionData(_func) }
#define COMPLEX_INT_CREATE(_int, _func) \
  { int32_t(_int), FrameConstructionData(_func) }

#define SIMPLE_TAG_CREATE(_tag, _func) \
  { nsGkAtoms::_tag, FrameConstructionData(_func) }
#define SIMPLE_TAG_CHAIN(_tag, _func) \
  { nsGkAtoms::_tag, FrameConstructionData(_func) }
#define COMPLEX_TAG_CREATE(_tag, _func) \
  { nsGkAtoms::_tag, FrameConstructionData(_func) }

static nsFieldSetFrame* GetFieldSetFrameFor(nsIFrame* aFrame) {
  auto pseudo = aFrame->Style()->GetPseudoType();
  if (pseudo == PseudoStyleType::fieldsetContent ||
      pseudo == PseudoStyleType::scrolledContent ||
      pseudo == PseudoStyleType::columnSet ||
      pseudo == PseudoStyleType::columnContent) {
    return GetFieldSetFrameFor(aFrame->GetParent());
  }
  return do_QueryFrame(aFrame);
}

/* static */
const nsCSSFrameConstructor::FrameConstructionData*
nsCSSFrameConstructor::FindHTMLData(const Element& aElement,
                                    nsIFrame* aParentFrame,
                                    ComputedStyle& aStyle) {
  MOZ_ASSERT(aElement.IsHTMLElement());
  NS_ASSERTION(!aParentFrame ||
                   aParentFrame->Style()->GetPseudoType() !=
                       PseudoStyleType::fieldsetContent ||
                   aParentFrame->GetParent()->IsFieldSetFrame(),
               "Unexpected parent for fieldset content anon box");

  if (aElement.IsInNativeAnonymousSubtree() &&
      aElement.NodeInfo()->NameAtom() == nsGkAtoms::label && aParentFrame) {
    if (static_cast<nsFileControlFrame*>(do_QueryFrame(aParentFrame))) {
      static constexpr FrameConstructionData sFileLabelData(
          NS_NewFileControlLabelFrame);
      return &sFileLabelData;
    }
    if (aParentFrame->GetParent() &&
        aParentFrame->GetParent()->IsComboboxControlFrame()) {
      static constexpr FrameConstructionData sComboboxLabelData(
          NS_NewComboboxLabelFrame);
      return &sComboboxLabelData;
    }
  }

  static constexpr FrameConstructionDataByTag sHTMLData[] = {
      SIMPLE_TAG_CHAIN(img, nsCSSFrameConstructor::FindImgData),
      SIMPLE_TAG_CHAIN(mozgeneratedcontentimage,
                       nsCSSFrameConstructor::FindGeneratedImageData),
      {nsGkAtoms::br,
       {NS_NewBRFrame, FCDATA_IS_LINE_PARTICIPANT | FCDATA_IS_LINE_BREAK}},
      SIMPLE_TAG_CREATE(wbr, NS_NewWBRFrame),
      SIMPLE_TAG_CHAIN(input, nsCSSFrameConstructor::FindInputData),
      SIMPLE_TAG_CREATE(textarea, NS_NewTextControlFrame),
      SIMPLE_TAG_CHAIN(select, nsCSSFrameConstructor::FindSelectData),
      SIMPLE_TAG_CHAIN(object, nsCSSFrameConstructor::FindObjectData),
      SIMPLE_TAG_CHAIN(embed, nsCSSFrameConstructor::FindObjectData),
      COMPLEX_TAG_CREATE(fieldset,
                         &nsCSSFrameConstructor::ConstructFieldSetFrame),
      SIMPLE_TAG_CREATE(frameset, NS_NewHTMLFramesetFrame),
      SIMPLE_TAG_CREATE(iframe, NS_NewSubDocumentFrame),
      {nsGkAtoms::button,
       {ToCreationFunc(NS_NewHTMLButtonControlFrame),
        FCDATA_ALLOW_BLOCK_STYLES | FCDATA_ALLOW_GRID_FLEX_COLUMN,
        PseudoStyleType::buttonContent}},
      SIMPLE_TAG_CHAIN(canvas, nsCSSFrameConstructor::FindCanvasData),
      SIMPLE_TAG_CREATE(video, NS_NewHTMLVideoFrame),
      SIMPLE_TAG_CREATE(audio, NS_NewHTMLAudioFrame),
      SIMPLE_TAG_CREATE(progress, NS_NewProgressFrame),
      SIMPLE_TAG_CREATE(meter, NS_NewMeterFrame),
      SIMPLE_TAG_CHAIN(details, nsCSSFrameConstructor::FindDetailsData),
  };

  return FindDataByTag(aElement, aStyle, sHTMLData, ArrayLength(sHTMLData));
}

/* static */
const nsCSSFrameConstructor::FrameConstructionData*
nsCSSFrameConstructor::FindGeneratedImageData(const Element& aElement,
                                              ComputedStyle&) {
  if (!aElement.IsInNativeAnonymousSubtree()) {
    return nullptr;
  }

  auto& generatedContent = static_cast<const GeneratedImageContent&>(aElement);
  if (generatedContent.IsForListStyleImageMarker()) {
    static constexpr FrameConstructionData sImgData(
        NS_NewImageFrameForListStyleImage);
    return &sImgData;
  }

  static constexpr FrameConstructionData sImgData(
      NS_NewImageFrameForGeneratedContentIndex);
  return &sImgData;
}

/* static */
const nsCSSFrameConstructor::FrameConstructionData*
nsCSSFrameConstructor::FindImgData(const Element& aElement,
                                   ComputedStyle& aStyle) {
  if (nsImageFrame::ImageFrameTypeFor(aElement, aStyle) !=
      nsImageFrame::ImageFrameType::ForElementRequest) {
    // content: url gets handled by the generic code-path.
    return nullptr;
  }

  static constexpr FrameConstructionData sImgData(NS_NewImageFrame);
  return &sImgData;
}

/* static */
const nsCSSFrameConstructor::FrameConstructionData*
nsCSSFrameConstructor::FindImgControlData(const Element& aElement,
                                          ComputedStyle& aStyle) {
  if (nsImageFrame::ImageFrameTypeFor(aElement, aStyle) !=
      nsImageFrame::ImageFrameType::ForElementRequest) {
    return nullptr;
  }

  static constexpr FrameConstructionData sImgControlData(
      NS_NewImageControlFrame);
  return &sImgControlData;
}

/* static */
const nsCSSFrameConstructor::FrameConstructionData*
nsCSSFrameConstructor::FindSearchControlData(const Element& aElement,
                                             ComputedStyle& aStyle) {
  if (StaticPrefs::layout_forms_input_type_search_enabled()) {
    static constexpr FrameConstructionData sSearchControlData(
        NS_NewSearchControlFrame);
    return &sSearchControlData;
  }

  static constexpr FrameConstructionData sTextControlData(
      NS_NewTextControlFrame);
  return &sTextControlData;
}

/* static */
const nsCSSFrameConstructor::FrameConstructionData*
nsCSSFrameConstructor::FindInputData(const Element& aElement,
                                     ComputedStyle& aStyle) {
  static constexpr FrameConstructionDataByInt sInputData[] = {
      SIMPLE_INT_CREATE(FormControlType::InputCheckbox,
                        ToCreationFunc(NS_NewCheckboxRadioFrame)),
      SIMPLE_INT_CREATE(FormControlType::InputRadio,
                        ToCreationFunc(NS_NewCheckboxRadioFrame)),
      SIMPLE_INT_CREATE(FormControlType::InputFile, NS_NewFileControlFrame),
      SIMPLE_INT_CHAIN(FormControlType::InputImage,
                       nsCSSFrameConstructor::FindImgControlData),
      SIMPLE_INT_CREATE(FormControlType::InputEmail, NS_NewTextControlFrame),
      SIMPLE_INT_CREATE(FormControlType::InputText, NS_NewTextControlFrame),
      SIMPLE_INT_CREATE(FormControlType::InputTel, NS_NewTextControlFrame),
      SIMPLE_INT_CREATE(FormControlType::InputUrl, NS_NewTextControlFrame),
      SIMPLE_INT_CREATE(FormControlType::InputRange, NS_NewRangeFrame),
      SIMPLE_INT_CREATE(FormControlType::InputPassword, NS_NewTextControlFrame),
      {int32_t(FormControlType::InputColor),
       {NS_NewColorControlFrame, 0, PseudoStyleType::buttonContent}},

      SIMPLE_INT_CHAIN(FormControlType::InputSearch,
                       nsCSSFrameConstructor::FindSearchControlData),
      SIMPLE_INT_CREATE(FormControlType::InputNumber, NS_NewNumberControlFrame),
      SIMPLE_INT_CREATE(FormControlType::InputTime, NS_NewDateTimeControlFrame),
      SIMPLE_INT_CREATE(FormControlType::InputDate, NS_NewDateTimeControlFrame),
      SIMPLE_INT_CREATE(FormControlType::InputDatetimeLocal,
                        NS_NewDateTimeControlFrame),
      // TODO: this is temporary until a frame is written: bug 888320
      SIMPLE_INT_CREATE(FormControlType::InputMonth, NS_NewTextControlFrame),
      // TODO: this is temporary until a frame is written: bug 888320
      SIMPLE_INT_CREATE(FormControlType::InputWeek, NS_NewTextControlFrame),
      {int32_t(FormControlType::InputSubmit),
       {ToCreationFunc(NS_NewGfxButtonControlFrame), 0,
        PseudoStyleType::buttonContent}},
      {int32_t(FormControlType::InputReset),
       {ToCreationFunc(NS_NewGfxButtonControlFrame), 0,
        PseudoStyleType::buttonContent}},
      {int32_t(FormControlType::InputButton),
       {ToCreationFunc(NS_NewGfxButtonControlFrame), 0,
        PseudoStyleType::buttonContent}}
      // Keeping hidden inputs out of here on purpose for so they get frames by
      // display (in practice, none).
  };

  auto controlType = HTMLInputElement::FromNode(aElement)->ControlType();

  // radio and checkbox inputs with appearance:none should be constructed
  // by display type.  (Note that we're not checking that appearance is
  // not (respectively) StyleAppearance::Radio and StyleAppearance::Checkbox.)
  if ((controlType == FormControlType::InputCheckbox ||
       controlType == FormControlType::InputRadio) &&
      !aStyle.StyleDisplay()->HasAppearance()) {
    return nullptr;
  }

  return FindDataByInt(int32_t(controlType), aElement, aStyle, sInputData,
                       ArrayLength(sInputData));
}

/* static */
const nsCSSFrameConstructor::FrameConstructionData*
nsCSSFrameConstructor::FindObjectData(const Element& aElement,
                                      ComputedStyle& aStyle) {
  uint32_t type;
  nsCOMPtr<nsIObjectLoadingContent> objContent =
      do_QueryInterface(const_cast<Element*>(&aElement));
  NS_ASSERTION(objContent,
               "embed and object must implement "
               "nsIObjectLoadingContent!");
  objContent->GetDisplayedType(&type);

  static constexpr FrameConstructionDataByInt sObjectData[] = {
      // TODO(emilio): Can we remove the NS_NewEmptyFrame case and just use a
      // subdocument frame here?
      SIMPLE_INT_CREATE(nsIObjectLoadingContent::TYPE_LOADING,
                        NS_NewEmptyFrame),
      SIMPLE_INT_CREATE(nsIObjectLoadingContent::TYPE_DOCUMENT,
                        NS_NewSubDocumentFrame),
      // Nothing for TYPE_FALLBACK so we'll construct frames by display there
  };

  return FindDataByInt((int32_t)type, aElement, aStyle, sObjectData,
                       ArrayLength(sObjectData));
}

/* static */
const nsCSSFrameConstructor::FrameConstructionData*
nsCSSFrameConstructor::FindCanvasData(const Element& aElement,
                                      ComputedStyle& aStyle) {
  // We want to check whether script is enabled on the document that
  // could be painting to the canvas.  That's the owner document of
  // the canvas, except when the owner document is a static document,
  // in which case it's the original document it was cloned from.
  Document* doc = aElement.OwnerDoc();
  if (doc->IsStaticDocument()) {
    doc = doc->GetOriginalDocument();
  }
  if (!doc->IsScriptEnabled()) {
    return nullptr;
  }

  static constexpr FrameConstructionData sCanvasData(
      NS_NewHTMLCanvasFrame, 0, PseudoStyleType::htmlCanvasContent);
  return &sCanvasData;
}

static MOZ_NEVER_INLINE void DestroyFramesInList(PresShell* aPs,
                                                 nsFrameList& aList) {
  nsIFrame::DestroyContext context(aPs);
  aList.DestroyFrames(context);
}

void nsCSSFrameConstructor::ConstructFrameFromItemInternal(
    FrameConstructionItem& aItem, nsFrameConstructorState& aState,
    nsContainerFrame* aParentFrame, nsFrameList& aFrameList) {
  const FrameConstructionData* data = aItem.mFCData;
  NS_ASSERTION(data, "Must have frame construction data");

  uint32_t bits = data->mBits;

  NS_ASSERTION(!(bits & FCDATA_FUNC_IS_DATA_GETTER),
               "Should have dealt with this inside the data finder");

  // Some sets of bits are not compatible with each other
#define CHECK_ONLY_ONE_BIT(_bit1, _bit2)           \
  NS_ASSERTION(!(bits & _bit1) || !(bits & _bit2), \
               "Only one of these bits should be set")
  CHECK_ONLY_ONE_BIT(FCDATA_FUNC_IS_FULL_CTOR,
                     FCDATA_FORCE_NULL_ABSPOS_CONTAINER);
  CHECK_ONLY_ONE_BIT(FCDATA_FUNC_IS_FULL_CTOR, FCDATA_WRAP_KIDS_IN_BLOCKS);
  CHECK_ONLY_ONE_BIT(FCDATA_FUNC_IS_FULL_CTOR, FCDATA_IS_POPUP);
  CHECK_ONLY_ONE_BIT(FCDATA_FUNC_IS_FULL_CTOR, FCDATA_SKIP_ABSPOS_PUSH);
  CHECK_ONLY_ONE_BIT(FCDATA_FUNC_IS_FULL_CTOR,
                     FCDATA_DISALLOW_GENERATED_CONTENT);
  CHECK_ONLY_ONE_BIT(FCDATA_FUNC_IS_FULL_CTOR, FCDATA_ALLOW_BLOCK_STYLES);
  CHECK_ONLY_ONE_BIT(FCDATA_FUNC_IS_FULL_CTOR,
                     FCDATA_CREATE_BLOCK_WRAPPER_FOR_ALL_KIDS);
  CHECK_ONLY_ONE_BIT(FCDATA_WRAP_KIDS_IN_BLOCKS,
                     FCDATA_CREATE_BLOCK_WRAPPER_FOR_ALL_KIDS);
#undef CHECK_ONLY_ONE_BIT
  MOZ_ASSERT(
      !(bits & FCDATA_IS_WRAPPER_ANON_BOX) || (bits & FCDATA_USE_CHILD_ITEMS),
      "Wrapper anon boxes should always have FCDATA_USE_CHILD_ITEMS");
  MOZ_ASSERT(!(bits & FCDATA_ALLOW_GRID_FLEX_COLUMN) ||
                 (bits & FCDATA_CREATE_BLOCK_WRAPPER_FOR_ALL_KIDS),
             "Need the block wrapper bit to create grid/flex/column.");

  // Don't create a subdocument frame for iframes if we're creating extra frames
  if (aState.mCreatingExtraFrames &&
      aItem.mContent->IsHTMLElement(nsGkAtoms::iframe)) {
    return;
  }

  nsIContent* const content = aItem.mContent;
  nsIFrame* newFrame;
  nsIFrame* primaryFrame;
  ComputedStyle* const computedStyle = aItem.mComputedStyle;
  const nsStyleDisplay* display = computedStyle->StyleDisplay();
  if (bits & FCDATA_FUNC_IS_FULL_CTOR) {
    newFrame = (this->*(data->mFunc.mFullConstructor))(
        aState, aItem, aParentFrame, display, aFrameList);
    MOZ_ASSERT(newFrame, "Full constructor failed");
    primaryFrame = newFrame;
  } else {
    newFrame = (*data->mFunc.mCreationFunc)(mPresShell, computedStyle);

    const bool allowOutOfFlow = !(bits & FCDATA_DISALLOW_OUT_OF_FLOW);
    const bool isPopup = aItem.mIsPopup;

    nsContainerFrame* geometricParent =
        (isPopup || allowOutOfFlow)
            ? aState.GetGeometricParent(*display, aParentFrame)
            : aParentFrame;

    // In the non-scrollframe case, primaryFrame and newFrame are equal; in the
    // scrollframe case, newFrame is the scrolled frame while primaryFrame is
    // the scrollframe.
    if ((bits & FCDATA_MAY_NEED_SCROLLFRAME) &&
        display->IsScrollableOverflow()) {
      nsContainerFrame* scrollframe = nullptr;
      BuildScrollFrame(aState, content, computedStyle, newFrame,
                       geometricParent, scrollframe);
      primaryFrame = scrollframe;
    } else {
      InitAndRestoreFrame(aState, content, geometricParent, newFrame);
      primaryFrame = newFrame;
    }

    // If we need to create a block formatting context to wrap our
    // kids, do it now.
    nsIFrame* maybeAbsoluteContainingBlockStyleFrame = primaryFrame;
    nsIFrame* maybeAbsoluteContainingBlock = newFrame;
    nsIFrame* possiblyLeafFrame = newFrame;
    nsContainerFrame* outerFrame = nullptr;
    if (bits & FCDATA_CREATE_BLOCK_WRAPPER_FOR_ALL_KIDS) {
      RefPtr<ComputedStyle> outerStyle =
          mPresShell->StyleSet()->ResolveInheritingAnonymousBoxStyle(
              data->mAnonBoxPseudo, computedStyle);
#ifdef DEBUG
      nsContainerFrame* containerFrame = do_QueryFrame(newFrame);
      MOZ_ASSERT(containerFrame);
#endif
      nsContainerFrame* container = static_cast<nsContainerFrame*>(newFrame);
      nsContainerFrame* innerFrame;
      if (bits & FCDATA_ALLOW_GRID_FLEX_COLUMN) {
        switch (display->DisplayInside()) {
          case StyleDisplayInside::Flex:
            outerFrame = NS_NewFlexContainerFrame(mPresShell, outerStyle);
            InitAndRestoreFrame(aState, content, container, outerFrame);
            innerFrame = outerFrame;
            break;
          case StyleDisplayInside::Grid:
            outerFrame = NS_NewGridContainerFrame(mPresShell, outerStyle);
            InitAndRestoreFrame(aState, content, container, outerFrame);
            innerFrame = outerFrame;
            break;
          default: {
            innerFrame = NS_NewBlockFrame(mPresShell, outerStyle);
            if (outerStyle->StyleColumn()->IsColumnContainerStyle()) {
              outerFrame = BeginBuildingColumns(aState, content, container,
                                                innerFrame, outerStyle);
            } else {
              // No need to create column container. Initialize innerFrame.
              InitAndRestoreFrame(aState, content, container, innerFrame);
              outerFrame = innerFrame;
            }
            break;
          }
        }
      } else {
        innerFrame = NS_NewBlockFrame(mPresShell, outerStyle);
        InitAndRestoreFrame(aState, content, container, innerFrame);
        outerFrame = innerFrame;
      }

      SetInitialSingleChild(container, outerFrame);

      container->AddStateBits(NS_FRAME_OWNS_ANON_BOXES);

      // Now figure out whether newFrame or outerFrame should be the
      // absolute container.
      if (outerFrame->IsAbsPosContainingBlock()) {
        maybeAbsoluteContainingBlock = outerFrame;
        maybeAbsoluteContainingBlockStyleFrame = outerFrame;
        innerFrame->AddStateBits(NS_FRAME_CAN_HAVE_ABSPOS_CHILDREN);
      }

      // Our kids should go into the innerFrame.
      newFrame = innerFrame;
    }

    aState.AddChild(primaryFrame, aFrameList, content, aParentFrame,
                    allowOutOfFlow, allowOutOfFlow);

    nsContainerFrame* newFrameAsContainer = do_QueryFrame(newFrame);
    if (newFrameAsContainer) {
      // Process the child content if requested
      nsFrameList childList;
      nsFrameConstructorSaveState absoluteSaveState;

      if (bits & FCDATA_FORCE_NULL_ABSPOS_CONTAINER) {
        aState.PushAbsoluteContainingBlock(nullptr, nullptr, absoluteSaveState);
      } else if (!(bits & FCDATA_SKIP_ABSPOS_PUSH)) {
        maybeAbsoluteContainingBlock->AddStateBits(
            NS_FRAME_CAN_HAVE_ABSPOS_CHILDREN);
        if (maybeAbsoluteContainingBlockStyleFrame->IsAbsPosContainingBlock()) {
          auto* cf =
              static_cast<nsContainerFrame*>(maybeAbsoluteContainingBlock);
          aState.PushAbsoluteContainingBlock(
              cf, maybeAbsoluteContainingBlockStyleFrame, absoluteSaveState);
        }
      }

      nsFrameConstructorSaveState floatSaveState;
      aState.MaybePushFloatContainingBlock(newFrameAsContainer, floatSaveState);

      if (bits & FCDATA_USE_CHILD_ITEMS) {
        // At this point, we have not set up the auto value for this frame, and
        // no caller will have set it so it is not redundant and therefor will
        // not assert.
        AutoFrameConstructionPageName pageNameTracker(aState,
                                                      newFrameAsContainer);
        ConstructFramesFromItemList(
            aState, aItem.mChildItems, newFrameAsContainer,
            bits & FCDATA_IS_WRAPPER_ANON_BOX, childList);
      } else {
        // Process the child frames.
        ProcessChildren(aState, content, computedStyle, newFrameAsContainer,
                        !(bits & FCDATA_DISALLOW_GENERATED_CONTENT), childList,
                        (bits & FCDATA_ALLOW_BLOCK_STYLES) != 0,
                        possiblyLeafFrame);
      }

      if (bits & FCDATA_WRAP_KIDS_IN_BLOCKS) {
        nsFrameList newList;
        nsFrameList currentBlockList;
        nsIFrame* f;
        while ((f = childList.FirstChild()) != nullptr) {
          bool wrapFrame = IsInlineFrame(f) || IsFramePartOfIBSplit(f);
          if (!wrapFrame) {
            FlushAccumulatedBlock(aState, content, newFrameAsContainer,
                                  currentBlockList, newList);
          }

          childList.RemoveFrame(f);
          if (wrapFrame) {
            currentBlockList.AppendFrame(nullptr, f);
          } else {
            newList.AppendFrame(nullptr, f);
          }
        }
        FlushAccumulatedBlock(aState, content, newFrameAsContainer,
                              currentBlockList, newList);

        if (childList.NotEmpty()) {
          // an error must have occurred, delete unprocessed frames
          DestroyFramesInList(mPresShell, childList);
        }

        childList = std::move(newList);
      }

      if (!(bits & FCDATA_ALLOW_GRID_FLEX_COLUMN) ||
          !MayNeedToCreateColumnSpanSiblings(newFrameAsContainer, childList)) {
        // Set the frame's initial child list. Note that MathML depends on this
        // being called even if childList is empty!
        newFrameAsContainer->SetInitialChildList(FrameChildListID::Principal,
                                                 std::move(childList));
      } else {
        // Extract any initial non-column-span kids, and put them in inner
        // frame's child list.
        nsFrameList initialNonColumnSpanKids =
            childList.Split([](nsIFrame* f) { return f->IsColumnSpan(); });
        newFrameAsContainer->SetInitialChildList(
            FrameChildListID::Principal, std::move(initialNonColumnSpanKids));

        if (childList.NotEmpty()) {
          nsFrameList columnSpanSiblings = CreateColumnSpanSiblings(
              aState, newFrameAsContainer, childList,
              // Column content should never be a absolute/fixed positioned
              // containing block. Pass nullptr as aPositionedFrame.
              nullptr);

          MOZ_ASSERT(outerFrame,
                     "outerFrame should be non-null if multi-column container "
                     "is created.");
          FinishBuildingColumns(aState, outerFrame, newFrameAsContainer,
                                columnSpanSiblings);
        }
      }
    }
  }

  NS_ASSERTION(newFrame->IsLineParticipant() ==
                   ((bits & FCDATA_IS_LINE_PARTICIPANT) != 0),
               "Incorrectly set FCDATA_IS_LINE_PARTICIPANT bits");

  // Even if mCreatingExtraFrames is set, we may need to SetPrimaryFrame for
  // generated content that doesn't have one yet.  Note that we have to examine
  // the frame bit, because by this point mIsGeneratedContent has been cleared
  // on aItem.
  if ((!aState.mCreatingExtraFrames ||
       (aItem.mContent->IsRootOfNativeAnonymousSubtree() &&
        !aItem.mContent->GetPrimaryFrame())) &&
      !(bits & FCDATA_SKIP_FRAMESET)) {
    aItem.mContent->SetPrimaryFrame(primaryFrame);
    ActiveLayerTracker::TransferActivityToFrame(aItem.mContent, primaryFrame);
  }
}

static void GatherSubtreeElements(Element* aElement,
                                  nsTArray<Element*>& aElements) {
  aElements.AppendElement(aElement);
  StyleChildrenIterator iter(aElement);
  for (nsIContent* c = iter.GetNextChild(); c; c = iter.GetNextChild()) {
    if (!c->IsElement()) {
      continue;
    }
    GatherSubtreeElements(c->AsElement(), aElements);
  }
}

nsresult nsCSSFrameConstructor::GetAnonymousContent(
    nsIContent* aParent, nsIFrame* aParentFrame,
    nsTArray<nsIAnonymousContentCreator::ContentInfo>& aContent) {
  nsIAnonymousContentCreator* creator = do_QueryFrame(aParentFrame);
  if (!creator) {
    return NS_OK;
  }

  nsresult rv = creator->CreateAnonymousContent(aContent);
  if (NS_FAILED(rv)) {
    // CreateAnonymousContent failed, e.g. because the page has a <use> loop.
    return rv;
  }

  if (aContent.IsEmpty()) {
    return NS_OK;
  }

  const bool devtoolsEventsEnabled =
      mDocument->DevToolsAnonymousAndShadowEventsEnabled();

  MOZ_ASSERT(aParent->IsElement());
  for (const auto& info : aContent) {
    // get our child's content and set its parent to our content
    nsIContent* content = info.mContent;
    content->SetIsNativeAnonymousRoot();

    BindContext context(*aParent->AsElement(), BindContext::ForNativeAnonymous);
    rv = content->BindToTree(context, *aParent);

    if (NS_FAILED(rv)) {
      content->UnbindFromTree();
      return rv;
    }

    if (devtoolsEventsEnabled) {
      content->QueueDevtoolsAnonymousEvent(/* aIsRemove = */ false);
    }
  }

  // Some situations where we don't cache anonymous content styles:
  //
  // * when visibility or pointer-events is anything other than the initial
  //   value; we rely on visibility and pointer-events inheriting into anonymous
  //   content, but don't bother adding this state to the AnonymousContentKey,
  //   since it's not so common. Note that with overlay scrollbars, scrollbars
  //   always start off with pointer-events: none so we don't need to check for
  //   that in that case.
  //
  // * when the medium is anything other than screen; some UA style sheet rules
  //   apply in e.g. print medium, and will give different results from the
  //   cached styles
  Maybe<bool> computedAllowStyleCaching;
  auto ComputeAllowStyleCaching = [&] {
    if (!StaticPrefs::layout_css_cached_scrollbar_styles_enabled()) {
      return false;
    }
    if (aParentFrame->StyleVisibility()->mVisible != StyleVisibility::Visible) {
      return false;
    }
    nsPresContext* pc = mPresShell->GetPresContext();
    if (!pc->UseOverlayScrollbars() &&
        aParentFrame->StyleUI()->ComputedPointerEvents() !=
            StylePointerEvents::Auto) {
      return false;
    }
    if (pc->Medium() != nsGkAtoms::screen) {
      return false;
    }
    return true;
  };

  auto AllowStyleCaching = [&] {
    if (computedAllowStyleCaching.isNothing()) {
      computedAllowStyleCaching.emplace(ComputeAllowStyleCaching());
    }
    return computedAllowStyleCaching.value();
  };

  // Compute styles for the anonymous content tree.
  ServoStyleSet* styleSet = mPresShell->StyleSet();
  for (auto& info : aContent) {
    Element* e = Element::FromNode(info.mContent);
    if (!e) {
      continue;
    }

    if (info.mKey == AnonymousContentKey::None || !AllowStyleCaching()) {
      // Most NAC subtrees do not use caching of computed styles.  Just go
      // ahead and eagerly style the subtree.
      styleSet->StyleNewSubtree(e);
      continue;
    }

    // We have a NAC subtree for which we can use cached styles.
    AutoTArray<RefPtr<ComputedStyle>, 2> cachedStyles;
    AutoTArray<Element*, 2> elements;

    GatherSubtreeElements(e, elements);
    styleSet->GetCachedAnonymousContentStyles(info.mKey, cachedStyles);

    if (cachedStyles.IsEmpty()) {
      // We haven't stored cached styles for this kind of NAC subtree yet.
      // Eagerly compute those styles, then cache them for later.
      styleSet->StyleNewSubtree(e);
      for (Element* e : elements) {
        if (e->HasServoData()) {
          cachedStyles.AppendElement(ServoStyleSet::ResolveServoStyle(*e));
        } else {
          cachedStyles.AppendElement(nullptr);
        }
      }
      styleSet->PutCachedAnonymousContentStyles(info.mKey,
                                                std::move(cachedStyles));
      continue;
    }

    // We previously stored cached styles for this kind of NAC subtree.
    // Iterate over them and set them on the subtree's elements.
    MOZ_ASSERT(cachedStyles.Length() == elements.Length(),
               "should always produce the same size NAC subtree");
    for (size_t i = 0, len = cachedStyles.Length(); i != len; ++i) {
      if (cachedStyles[i]) {
#ifdef DEBUG
        // Assert that our cached style is the same as one we could compute.
        RefPtr<ComputedStyle> cs = styleSet->ResolveStyleLazily(*elements[i]);
        MOZ_ASSERT(
            cachedStyles[i]->EqualForCachedAnonymousContentStyle(*cs),
            "cached anonymous content styles should be identical to those we "
            "would compute normally");
        // All overlay scrollbars start off as inactive, so we can rely on their
        // pointer-events value being always none.
        MOZ_ASSERT(!mPresShell->GetPresContext()->UseOverlayScrollbars() ||
                   cs->StyleUI()->ComputedPointerEvents() ==
                       StylePointerEvents::None);
#endif
        Servo_SetExplicitStyle(elements[i], cachedStyles[i]);
      }
    }
  }

  return NS_OK;
}

// XUL frames are not allowed to be out of flow.
#define SIMPLE_XUL_FCDATA(_func) \
  FrameConstructionData(_func,   \
                        FCDATA_DISALLOW_OUT_OF_FLOW | FCDATA_SKIP_ABSPOS_PUSH)
#define SCROLLABLE_XUL_FCDATA(_func)                         \
  FrameConstructionData(_func, FCDATA_DISALLOW_OUT_OF_FLOW | \
                                   FCDATA_SKIP_ABSPOS_PUSH | \
                                   FCDATA_MAY_NEED_SCROLLFRAME)
// .. but we allow some XUL frames to be _containers_ for out-of-flow content
// (This is the same as SCROLLABLE_XUL_FCDATA, but w/o FCDATA_SKIP_ABSPOS_PUSH)
#define SCROLLABLE_ABSPOS_CONTAINER_XUL_FCDATA(_func) \
  FrameConstructionData(                              \
      _func, FCDATA_DISALLOW_OUT_OF_FLOW | FCDATA_MAY_NEED_SCROLLFRAME)

#define SIMPLE_XUL_CREATE(_tag, _func) \
  { nsGkAtoms::_tag, SIMPLE_XUL_FCDATA(_func) }
#define SCROLLABLE_XUL_CREATE(_tag, _func) \
  { nsGkAtoms::_tag, SCROLLABLE_XUL_FCDATA(_func) }

/* static */
const nsCSSFrameConstructor::FrameConstructionData*
nsCSSFrameConstructor::FindXULTagData(const Element& aElement,
                                      ComputedStyle& aStyle) {
  MOZ_ASSERT(aElement.IsXULElement());
  static constexpr FrameConstructionData kPopupData(
      NS_NewMenuPopupFrame, FCDATA_IS_POPUP | FCDATA_SKIP_ABSPOS_PUSH);

  static constexpr FrameConstructionDataByTag sXULTagData[] = {
      SIMPLE_XUL_CREATE(image, NS_NewXULImageFrame),
      SIMPLE_XUL_CREATE(treechildren, NS_NewTreeBodyFrame),
      SIMPLE_TAG_CHAIN(label,
                       nsCSSFrameConstructor::FindXULLabelOrDescriptionData),
      SIMPLE_TAG_CHAIN(description,
                       nsCSSFrameConstructor::FindXULLabelOrDescriptionData),
#ifdef XP_MACOSX
      SIMPLE_TAG_CHAIN(menubar, nsCSSFrameConstructor::FindXULMenubarData),
#endif /* XP_MACOSX */
      SIMPLE_XUL_CREATE(iframe, NS_NewSubDocumentFrame),
      SIMPLE_XUL_CREATE(editor, NS_NewSubDocumentFrame),
      SIMPLE_XUL_CREATE(browser, NS_NewSubDocumentFrame),
      SIMPLE_XUL_CREATE(splitter, NS_NewSplitterFrame),
      SIMPLE_XUL_CREATE(scrollbar, NS_NewScrollbarFrame),
      SIMPLE_XUL_CREATE(slider, NS_NewSliderFrame),
      SIMPLE_XUL_CREATE(thumb, NS_NewSimpleXULLeafFrame),
      SIMPLE_XUL_CREATE(scrollcorner, NS_NewSimpleXULLeafFrame),
      SIMPLE_XUL_CREATE(resizer, NS_NewSimpleXULLeafFrame),
      SIMPLE_XUL_CREATE(scrollbarbutton, NS_NewScrollbarButtonFrame),
      {nsGkAtoms::panel, kPopupData},
      {nsGkAtoms::menupopup, kPopupData},
      {nsGkAtoms::tooltip, kPopupData},
  };

  return FindDataByTag(aElement, aStyle, sXULTagData, ArrayLength(sXULTagData));
}

/* static */
const nsCSSFrameConstructor::FrameConstructionData*
nsCSSFrameConstructor::FindXULLabelOrDescriptionData(const Element& aElement,
                                                     ComputedStyle&) {
  // Follow CSS display value if no value attribute
  if (!aElement.HasAttr(nsGkAtoms::value)) {
    return nullptr;
  }

  // Follow CSS display if there's no crop="center".
  if (!aElement.AttrValueIs(kNameSpaceID_None, nsGkAtoms::crop,
                            nsGkAtoms::center, eCaseMatters)) {
    return nullptr;
  }

  static constexpr FrameConstructionData sMiddleCroppingData =
      SIMPLE_XUL_FCDATA(NS_NewMiddleCroppingLabelFrame);
  return &sMiddleCroppingData;
}

#ifdef XP_MACOSX
/* static */
const nsCSSFrameConstructor::FrameConstructionData*
nsCSSFrameConstructor::FindXULMenubarData(const Element& aElement,
                                          ComputedStyle&) {
  if (aElement.OwnerDoc()->IsInChromeDocShell()) {
    BrowsingContext* bc = aElement.OwnerDoc()->GetBrowsingContext();
    bool isRoot = bc && !bc->GetParent();
    if (isRoot) {
      // This is the root.  Suppress the menubar, since on Mac
      // window menus are not attached to the window.
      static constexpr FrameConstructionData sSuppressData = SUPPRESS_FCDATA();
      return &sSuppressData;
    }
  }

  return nullptr;
}
#endif /* XP_MACOSX */

already_AddRefed<ComputedStyle> nsCSSFrameConstructor::BeginBuildingScrollFrame(
    nsFrameConstructorState& aState, nsIContent* aContent,
    ComputedStyle* aContentStyle, nsContainerFrame* aParentFrame,
    PseudoStyleType aScrolledPseudo, bool aIsRoot,
    nsContainerFrame*& aNewFrame) {
  nsContainerFrame* gfxScrollFrame = aNewFrame;

  if (!gfxScrollFrame) {
    gfxScrollFrame = NS_NewHTMLScrollFrame(mPresShell, aContentStyle, aIsRoot);
    InitAndRestoreFrame(aState, aContent, aParentFrame, gfxScrollFrame);
  }

  MOZ_ASSERT(gfxScrollFrame);

  // if there are any anonymous children for the scroll frame, create
  // frames for them.
  //
  // We can't take the normal ProcessChildren path, because the NAC needs to
  // be parented to the scrollframe, and everything else needs to be parented
  // to the scrolledframe.
  AutoTArray<nsIAnonymousContentCreator::ContentInfo, 4> scrollNAC;
  DebugOnly<nsresult> rv =
      GetAnonymousContent(aContent, gfxScrollFrame, scrollNAC);
  MOZ_ASSERT(NS_SUCCEEDED(rv));
  nsFrameList anonymousList;
  if (!scrollNAC.IsEmpty()) {
    nsFrameConstructorSaveState floatSaveState;
    aState.MaybePushFloatContainingBlock(gfxScrollFrame, floatSaveState);

    AutoFrameConstructionItemList items(this);
    AutoFrameConstructionPageName pageNameTracker(aState, gfxScrollFrame);
    AddFCItemsForAnonymousContent(aState, gfxScrollFrame, scrollNAC, items,
                                  pageNameTracker);
    ConstructFramesFromItemList(aState, items, gfxScrollFrame,
                                /* aParentIsWrapperAnonBox = */ false,
                                anonymousList);
  }

  aNewFrame = gfxScrollFrame;
  gfxScrollFrame->AddStateBits(NS_FRAME_OWNS_ANON_BOXES);

  // we used the style that was passed in. So resolve another one.
  ServoStyleSet* styleSet = mPresShell->StyleSet();
  RefPtr<ComputedStyle> scrolledChildStyle =
      styleSet->ResolveInheritingAnonymousBoxStyle(aScrolledPseudo,
                                                   aContentStyle);

  gfxScrollFrame->SetInitialChildList(FrameChildListID::Principal,
                                      std::move(anonymousList));

  return scrolledChildStyle.forget();
}

void nsCSSFrameConstructor::FinishBuildingScrollFrame(
    nsContainerFrame* aScrollFrame, nsIFrame* aScrolledFrame) {
  aScrollFrame->AppendFrames(FrameChildListID::Principal,
                             nsFrameList(aScrolledFrame, aScrolledFrame));
}

/**
 * Called to wrap a gfx scrollframe around a frame. The hierarchy will look like
 * this
 *
 * ------- for gfx scrollbars ------
 *
 *
 *            ScrollFrame
 *                 ^
 *                 |
 *               Frame (scrolled frame you passed in)
 *
 *
 * -----------------------------------
 * LEGEND:
 *
 * ScrollFrame: This is a frame that manages gfx cross platform frame based
 * scrollbars.
 *
 * @param aContent the content node of the child to wrap.
 *
 * @param aScrolledFrame The frame of the content to wrap. This should not be
 * Initialized. This method will initialize it with a scrolled pseudo and no
 * nsIContent. The content will be attached to the scrollframe returned.
 *
 * @param aContentStyle the style that has already been resolved for the content
 * being passed in.
 *
 * @param aParentFrame   The parent to attach the scroll frame to
 *
 * @param aNewFrame The new scrollframe or gfx scrollframe that we create. It
 * will contain the scrolled frame you passed in. (returned) If this is not
 * null,  we'll just use it
 *
 * @param aScrolledContentStyle the style that was resolved for the scrolled
 * frame. (returned)
 */
void nsCSSFrameConstructor::BuildScrollFrame(nsFrameConstructorState& aState,
                                             nsIContent* aContent,
                                             ComputedStyle* aContentStyle,
                                             nsIFrame* aScrolledFrame,
                                             nsContainerFrame* aParentFrame,
                                             nsContainerFrame*& aNewFrame) {
  RefPtr<ComputedStyle> scrolledContentStyle = BeginBuildingScrollFrame(
      aState, aContent, aContentStyle, aParentFrame,
      PseudoStyleType::scrolledContent, false, aNewFrame);

  aScrolledFrame->SetComputedStyleWithoutNotification(scrolledContentStyle);
  InitAndRestoreFrame(aState, aContent, aNewFrame, aScrolledFrame);

  FinishBuildingScrollFrame(aNewFrame, aScrolledFrame);
}

const nsCSSFrameConstructor::FrameConstructionData*
nsCSSFrameConstructor::FindDisplayData(const nsStyleDisplay& aDisplay,
                                       const Element& aElement) {
  static_assert(eParentTypeCount < (1 << (32 - FCDATA_PARENT_TYPE_OFFSET)),
                "Check eParentTypeCount should not overflow");

  // The style system ensures that floated and positioned frames are
  // block-level.
  NS_ASSERTION(
      !(aDisplay.IsFloatingStyle() || aDisplay.IsAbsolutelyPositionedStyle()) ||
          aDisplay.IsBlockOutsideStyle(),
      "Style system did not apply CSS2.1 section 9.7 fixups");

  // If this is "body", try propagating its scroll style to the viewport
  // Note that we need to do this even if the body is NOT scrollable;
  // it might have dynamically changed from scrollable to not scrollable,
  // and that might need to be propagated.
  // XXXbz is this the right place to do this?  If this code moves,
  // make this function static.
  bool propagatedScrollToViewport = false;
  if (aElement.IsHTMLElement(nsGkAtoms::body)) {
    if (nsPresContext* presContext = mPresShell->GetPresContext()) {
      propagatedScrollToViewport =
          presContext->UpdateViewportScrollStylesOverride() == &aElement;
      MOZ_ASSERT(!propagatedScrollToViewport ||
                     !mPresShell->GetPresContext()->IsPaginated(),
                 "Shouldn't propagate scroll in paginated contexts");
    }
  }

  switch (aDisplay.DisplayInside()) {
    case StyleDisplayInside::Flow:
    case StyleDisplayInside::FlowRoot: {
      if (aDisplay.IsInlineFlow()) {
        static constexpr FrameConstructionData data(
            &nsCSSFrameConstructor::ConstructInline,
            FCDATA_IS_INLINE | FCDATA_IS_LINE_PARTICIPANT);
        return &data;
      }

      // If the frame is a block-level frame and is scrollable, then wrap it in
      // a scroll frame.  Except we don't want to do that for paginated contexts
      // for frames that are block-outside and aren't frames for native
      // anonymous stuff.
      // XXX Ignore tables for the time being (except caption)
      const uint32_t kCaptionCtorFlags =
          FCDATA_IS_TABLE_PART | FCDATA_DESIRED_PARENT_TYPE_TO_BITS(eTypeTable);
      const bool caption = aDisplay.mDisplay == StyleDisplay::TableCaption;
      const bool needScrollFrame =
          aDisplay.IsScrollableOverflow() && !propagatedScrollToViewport;
      if (needScrollFrame) {
        const bool suppressScrollFrame =
            mPresShell->GetPresContext()->IsPaginated() &&
            aDisplay.IsBlockOutsideStyle() &&
            !aElement.IsInNativeAnonymousSubtree();
        if (!suppressScrollFrame) {
          static constexpr FrameConstructionData sScrollableBlockData[2] = {
              {&nsCSSFrameConstructor::ConstructScrollableBlock},
              {&nsCSSFrameConstructor::ConstructScrollableBlock,
               kCaptionCtorFlags}};
          return &sScrollableBlockData[caption];
        }
      }

      // Handle various non-scrollable blocks.
      static constexpr FrameConstructionData sNonScrollableBlockData[2] = {
          {&nsCSSFrameConstructor::ConstructNonScrollableBlock},
          {&nsCSSFrameConstructor::ConstructNonScrollableBlock,
           kCaptionCtorFlags}};
      return &sNonScrollableBlockData[caption];
    }
    case StyleDisplayInside::Table: {
      static constexpr FrameConstructionData data(
          &nsCSSFrameConstructor::ConstructTable);
      return &data;
    }
    // NOTE: In the unlikely event that we add another table-part here that
    // has a desired-parent-type (& hence triggers table fixup), we'll need to
    // also update the flexbox chunk in ComputedStyle::ApplyStyleFixups().
    case StyleDisplayInside::TableRowGroup: {
      static constexpr FrameConstructionData data(
          &nsCSSFrameConstructor::ConstructTableRowOrRowGroup,
          FCDATA_IS_TABLE_PART |
              FCDATA_DESIRED_PARENT_TYPE_TO_BITS(eTypeTable));
      return &data;
    }
    case StyleDisplayInside::TableColumn: {
      static constexpr FrameConstructionData data(
          &nsCSSFrameConstructor::ConstructTableCol,
          FCDATA_IS_TABLE_PART |
              FCDATA_DESIRED_PARENT_TYPE_TO_BITS(eTypeColGroup));
      return &data;
    }
    case StyleDisplayInside::TableColumnGroup: {
      static constexpr FrameConstructionData data(
          ToCreationFunc(NS_NewTableColGroupFrame),
          FCDATA_IS_TABLE_PART | FCDATA_DISALLOW_OUT_OF_FLOW |
              FCDATA_SKIP_ABSPOS_PUSH |
              FCDATA_DESIRED_PARENT_TYPE_TO_BITS(eTypeTable));
      return &data;
    }
    case StyleDisplayInside::TableHeaderGroup: {
      static constexpr FrameConstructionData data(
          &nsCSSFrameConstructor::ConstructTableRowOrRowGroup,
          FCDATA_IS_TABLE_PART |
              FCDATA_DESIRED_PARENT_TYPE_TO_BITS(eTypeTable));
      return &data;
    }
    case StyleDisplayInside::TableFooterGroup: {
      static constexpr FrameConstructionData data(
          &nsCSSFrameConstructor::ConstructTableRowOrRowGroup,
          FCDATA_IS_TABLE_PART |
              FCDATA_DESIRED_PARENT_TYPE_TO_BITS(eTypeTable));
      return &data;
    }
    case StyleDisplayInside::TableRow: {
      static constexpr FrameConstructionData data(
          &nsCSSFrameConstructor::ConstructTableRowOrRowGroup,
          FCDATA_IS_TABLE_PART |
              FCDATA_DESIRED_PARENT_TYPE_TO_BITS(eTypeRowGroup));
      return &data;
    }
    case StyleDisplayInside::TableCell: {
      static constexpr FrameConstructionData data(
          &nsCSSFrameConstructor::ConstructTableCell,
          FCDATA_IS_TABLE_PART | FCDATA_DESIRED_PARENT_TYPE_TO_BITS(eTypeRow));
      return &data;
    }
    case StyleDisplayInside::Flex:
    case StyleDisplayInside::WebkitBox: {
      static constexpr FrameConstructionData nonScrollableData(
          ToCreationFunc(NS_NewFlexContainerFrame));
      static constexpr FrameConstructionData data(
          ToCreationFunc(NS_NewFlexContainerFrame),
          FCDATA_MAY_NEED_SCROLLFRAME);
      return MOZ_UNLIKELY(propagatedScrollToViewport) ? &nonScrollableData
                                                      : &data;
    }
    case StyleDisplayInside::Grid: {
      static constexpr FrameConstructionData nonScrollableData(
          ToCreationFunc(NS_NewGridContainerFrame));
      static constexpr FrameConstructionData data(
          ToCreationFunc(NS_NewGridContainerFrame),
          FCDATA_MAY_NEED_SCROLLFRAME);
      return MOZ_UNLIKELY(propagatedScrollToViewport) ? &nonScrollableData
                                                      : &data;
    }
    case StyleDisplayInside::Ruby: {
      static constexpr FrameConstructionData data[] = {
          {&nsCSSFrameConstructor::ConstructBlockRubyFrame,
           FCDATA_MAY_NEED_SCROLLFRAME},
          {ToCreationFunc(NS_NewRubyFrame), FCDATA_IS_LINE_PARTICIPANT}};
      bool isInline = aDisplay.DisplayOutside() == StyleDisplayOutside::Inline;
      return &data[isInline];
    }
    case StyleDisplayInside::RubyBase: {
      static constexpr FrameConstructionData data(
          ToCreationFunc(NS_NewRubyBaseFrame),
          FCDATA_IS_LINE_PARTICIPANT |
              FCDATA_DESIRED_PARENT_TYPE_TO_BITS(eTypeRubyBaseContainer));
      return &data;
    }
    case StyleDisplayInside::RubyBaseContainer: {
      static constexpr FrameConstructionData data(
          ToCreationFunc(NS_NewRubyBaseContainerFrame),
          FCDATA_IS_LINE_PARTICIPANT |
              FCDATA_DESIRED_PARENT_TYPE_TO_BITS(eTypeRuby));
      return &data;
    }
    case StyleDisplayInside::RubyText: {
      static constexpr FrameConstructionData data(
          ToCreationFunc(NS_NewRubyTextFrame),
          FCDATA_IS_LINE_PARTICIPANT |
              FCDATA_DESIRED_PARENT_TYPE_TO_BITS(eTypeRubyTextContainer));
      return &data;
    }
    case StyleDisplayInside::RubyTextContainer: {
      static constexpr FrameConstructionData data(
          ToCreationFunc(NS_NewRubyTextContainerFrame),
          FCDATA_DESIRED_PARENT_TYPE_TO_BITS(eTypeRuby));
      return &data;
    }
    default:
      MOZ_ASSERT_UNREACHABLE("unknown 'display' value");
      return nullptr;
  }
}

nsIFrame* nsCSSFrameConstructor::ConstructScrollableBlock(
    nsFrameConstructorState& aState, FrameConstructionItem& aItem,
    nsContainerFrame* aParentFrame, const nsStyleDisplay* aDisplay,
    nsFrameList& aFrameList) {
  nsIContent* const content = aItem.mContent;
  ComputedStyle* const computedStyle = aItem.mComputedStyle;

  nsContainerFrame* newFrame = nullptr;
  RefPtr<ComputedStyle> scrolledContentStyle = BeginBuildingScrollFrame(
      aState, content, computedStyle,
      aState.GetGeometricParent(*aDisplay, aParentFrame),
      PseudoStyleType::scrolledContent, false, newFrame);

  // Create our block frame
  // pass a temporary stylecontext, the correct one will be set later
  nsContainerFrame* scrolledFrame = NS_NewBlockFrame(mPresShell, computedStyle);

  // Make sure to AddChild before we call ConstructBlock so that we
  // end up before our descendants in fixed-pos lists as needed.
  aState.AddChild(newFrame, aFrameList, content, aParentFrame);

  nsFrameList blockList;
  ConstructBlock(aState, content, newFrame, newFrame, scrolledContentStyle,
                 &scrolledFrame, blockList,
                 newFrame->IsAbsPosContainingBlock() ? newFrame : nullptr);

  MOZ_ASSERT(blockList.OnlyChild() == scrolledFrame,
             "Scrollframe's frameList should be exactly the scrolled frame!");
  FinishBuildingScrollFrame(newFrame, scrolledFrame);

  return newFrame;
}

nsIFrame* nsCSSFrameConstructor::ConstructNonScrollableBlock(
    nsFrameConstructorState& aState, FrameConstructionItem& aItem,
    nsContainerFrame* aParentFrame, const nsStyleDisplay* aDisplay,
    nsFrameList& aFrameList) {
  ComputedStyle* const computedStyle = aItem.mComputedStyle;
  nsContainerFrame* newFrame = NS_NewBlockFrame(mPresShell, computedStyle);
  ConstructBlock(aState, aItem.mContent,
                 aState.GetGeometricParent(*aDisplay, aParentFrame),
                 aParentFrame, computedStyle, &newFrame, aFrameList,
                 newFrame->IsAbsPosContainingBlock() ? newFrame : nullptr);
  return newFrame;
}

void nsCSSFrameConstructor::InitAndRestoreFrame(
    const nsFrameConstructorState& aState, nsIContent* aContent,
    nsContainerFrame* aParentFrame, nsIFrame* aNewFrame, bool aAllowCounters) {
  MOZ_ASSERT(aNewFrame, "Null frame cannot be initialized");

  // Initialize the frame
  aNewFrame->Init(aContent, aParentFrame, nullptr);
  aNewFrame->AddStateBits(aState.mAdditionalStateBits);

  if (aState.mFrameState) {
    // Restore frame state for just the newly created frame.
    RestoreFrameStateFor(aNewFrame, aState.mFrameState);
  }

  if (aAllowCounters &&
      mContainStyleScopeManager.AddCounterChanges(aNewFrame)) {
    CountersDirty();
  }
}

already_AddRefed<ComputedStyle> nsCSSFrameConstructor::ResolveComputedStyle(
    nsIContent* aContent) {
  if (auto* element = Element::FromNode(aContent)) {
    return ServoStyleSet::ResolveServoStyle(*element);
  }

  MOZ_ASSERT(aContent->IsText(),
             "shouldn't waste time creating ComputedStyles for "
             "comments and processing instructions");

  Element* parent = aContent->GetFlattenedTreeParentElement();
  MOZ_ASSERT(parent, "Text out of the flattened tree?");

  // FIXME(emilio): The const_cast is unfortunate, but it's not worse than what
  // we did before.
  //
  // We could use ResolveServoStyle, but that would involve extra unnecessary
  // refcount traffic...
  auto* parentStyle =
      const_cast<ComputedStyle*>(Servo_Element_GetMaybeOutOfDateStyle(parent));
  MOZ_ASSERT(parentStyle,
             "How are we inserting text frames in an unstyled element?");
  return mPresShell->StyleSet()->ResolveStyleForText(aContent, parentStyle);
}

// MathML Mod - RBS
void nsCSSFrameConstructor::FlushAccumulatedBlock(
    nsFrameConstructorState& aState, nsIContent* aContent,
    nsContainerFrame* aParentFrame, nsFrameList& aBlockList,
    nsFrameList& aNewList) {
  if (aBlockList.IsEmpty()) {
    // Nothing to do
    return;
  }

  auto anonPseudo = PseudoStyleType::mozMathMLAnonymousBlock;

  ComputedStyle* parentContext =
      nsIFrame::CorrectStyleParentFrame(aParentFrame, anonPseudo)->Style();
  ServoStyleSet* styleSet = mPresShell->StyleSet();
  RefPtr<ComputedStyle> blockContext =
      styleSet->ResolveInheritingAnonymousBoxStyle(anonPseudo, parentContext);

  // then, create a block frame that will wrap the child frames. Make it a
  // MathML frame so that Get(Absolute/Float)ContainingBlockFor know that this
  // is not a suitable block.
  nsContainerFrame* blockFrame =
      NS_NewMathMLmathBlockFrame(mPresShell, blockContext);

  InitAndRestoreFrame(aState, aContent, aParentFrame, blockFrame);
  ReparentFrames(this, blockFrame, aBlockList, false);
  // We have to walk over aBlockList before we hand it over to blockFrame.
  for (nsIFrame* f : aBlockList) {
    f->SetParentIsWrapperAnonBox();
  }
  // abs-pos and floats are disabled in MathML children so we don't have to
  // worry about messing up those.
  blockFrame->SetInitialChildList(FrameChildListID::Principal,
                                  std::move(aBlockList));
  aNewList.AppendFrame(nullptr, blockFrame);
}

// Only <math> elements can be floated or positioned.  All other MathML
// should be in-flow.
#define SIMPLE_MATHML_CREATE(_tag, _func)             \
  {                                                   \
    nsGkAtoms::_tag, {                                \
      _func, FCDATA_DISALLOW_OUT_OF_FLOW |            \
                 FCDATA_FORCE_NULL_ABSPOS_CONTAINER | \
                 FCDATA_WRAP_KIDS_IN_BLOCKS           \
    }                                                 \
  }

/* static */
const nsCSSFrameConstructor::FrameConstructionData*
nsCSSFrameConstructor::FindMathMLData(const Element& aElement,
                                      ComputedStyle& aStyle) {
  MOZ_ASSERT(aElement.IsMathMLElement());

  nsAtom* tag = aElement.NodeInfo()->NameAtom();

  // Handle <math> specially, because it sometimes produces inlines
  if (tag == nsGkAtoms::math) {
    // The IsBlockOutsideStyle() check must match what
    // specified::Display::equivalent_block_display is checking for
    // already-block-outside things. Though the behavior here for the
    // display:table case is pretty weird...
    if (aStyle.StyleDisplay()->IsBlockOutsideStyle()) {
      static constexpr FrameConstructionData sBlockMathData(
          ToCreationFunc(NS_NewMathMLmathBlockFrame),
          FCDATA_FORCE_NULL_ABSPOS_CONTAINER | FCDATA_WRAP_KIDS_IN_BLOCKS);
      return &sBlockMathData;
    }

    static constexpr FrameConstructionData sInlineMathData(
        ToCreationFunc(NS_NewMathMLmathInlineFrame),
        FCDATA_FORCE_NULL_ABSPOS_CONTAINER | FCDATA_IS_LINE_PARTICIPANT |
            FCDATA_WRAP_KIDS_IN_BLOCKS);
    return &sInlineMathData;
  }

  static constexpr FrameConstructionDataByTag sMathMLData[] = {
      SIMPLE_MATHML_CREATE(annotation_, NS_NewMathMLTokenFrame),
      SIMPLE_MATHML_CREATE(annotation_xml_, NS_NewMathMLmrowFrame),
      SIMPLE_MATHML_CREATE(mi_, NS_NewMathMLTokenFrame),
      SIMPLE_MATHML_CREATE(mn_, NS_NewMathMLTokenFrame),
      SIMPLE_MATHML_CREATE(ms_, NS_NewMathMLTokenFrame),
      SIMPLE_MATHML_CREATE(mtext_, NS_NewMathMLTokenFrame),
      SIMPLE_MATHML_CREATE(mo_, NS_NewMathMLmoFrame),
      SIMPLE_MATHML_CREATE(mfrac_, NS_NewMathMLmfracFrame),
      SIMPLE_MATHML_CREATE(msup_, NS_NewMathMLmmultiscriptsFrame),
      SIMPLE_MATHML_CREATE(msub_, NS_NewMathMLmmultiscriptsFrame),
      SIMPLE_MATHML_CREATE(msubsup_, NS_NewMathMLmmultiscriptsFrame),
      SIMPLE_MATHML_CREATE(munder_, NS_NewMathMLmunderoverFrame),
      SIMPLE_MATHML_CREATE(mover_, NS_NewMathMLmunderoverFrame),
      SIMPLE_MATHML_CREATE(munderover_, NS_NewMathMLmunderoverFrame),
      SIMPLE_MATHML_CREATE(mphantom_, NS_NewMathMLmrowFrame),
      SIMPLE_MATHML_CREATE(mpadded_, NS_NewMathMLmpaddedFrame),
      SIMPLE_MATHML_CREATE(mspace_, NS_NewMathMLmspaceFrame),
      SIMPLE_MATHML_CREATE(none, NS_NewMathMLmrowFrame),
      SIMPLE_MATHML_CREATE(mprescripts_, NS_NewMathMLmrowFrame),
      SIMPLE_MATHML_CREATE(mfenced_, NS_NewMathMLmrowFrame),
      SIMPLE_MATHML_CREATE(mmultiscripts_, NS_NewMathMLmmultiscriptsFrame),
      SIMPLE_MATHML_CREATE(mstyle_, NS_NewMathMLmrowFrame),
      SIMPLE_MATHML_CREATE(msqrt_, NS_NewMathMLmsqrtFrame),
      SIMPLE_MATHML_CREATE(mroot_, NS_NewMathMLmrootFrame),
      SIMPLE_MATHML_CREATE(maction_, NS_NewMathMLmrowFrame),
      SIMPLE_MATHML_CREATE(mrow_, NS_NewMathMLmrowFrame),
      SIMPLE_MATHML_CREATE(merror_, NS_NewMathMLmrowFrame),
      SIMPLE_MATHML_CREATE(menclose_, NS_NewMathMLmencloseFrame),
      SIMPLE_MATHML_CREATE(semantics_, NS_NewMathMLmrowFrame)};

  return FindDataByTag(aElement, aStyle, sMathMLData, ArrayLength(sMathMLData));
}

nsContainerFrame* nsCSSFrameConstructor::ConstructFrameWithAnonymousChild(
    nsFrameConstructorState& aState, FrameConstructionItem& aItem,
    nsContainerFrame* aParentFrame, nsFrameList& aFrameList,
    ContainerFrameCreationFunc aConstructor,
    ContainerFrameCreationFunc aInnerConstructor, PseudoStyleType aInnerPseudo,
    bool aCandidateRootFrame) {
  nsIContent* const content = aItem.mContent;
  ComputedStyle* const computedStyle = aItem.mComputedStyle;

  // Create the outer frame:
  nsContainerFrame* newFrame = aConstructor(mPresShell, computedStyle);

  InitAndRestoreFrame(aState, content,
                      aCandidateRootFrame
                          ? aState.GetGeometricParent(
                                *computedStyle->StyleDisplay(), aParentFrame)
                          : aParentFrame,
                      newFrame);
  newFrame->AddStateBits(NS_FRAME_OWNS_ANON_BOXES);

  // Create the pseudo SC for the anonymous wrapper child as a child of the SC:
  RefPtr<ComputedStyle> scForAnon =
      mPresShell->StyleSet()->ResolveInheritingAnonymousBoxStyle(aInnerPseudo,
                                                                 computedStyle);

  // Create the anonymous inner wrapper frame
  nsContainerFrame* innerFrame = aInnerConstructor(mPresShell, scForAnon);

  InitAndRestoreFrame(aState, content, newFrame, innerFrame);

  // Put the newly created frames into the right child list
  SetInitialSingleChild(newFrame, innerFrame);

  aState.AddChild(newFrame, aFrameList, content, aParentFrame,
                  aCandidateRootFrame, aCandidateRootFrame);

  if (!mRootElementFrame && aCandidateRootFrame) {
    mRootElementFrame = newFrame;
  }

  nsFrameConstructorSaveState floatSaveState;
  aState.MaybePushFloatContainingBlock(innerFrame, floatSaveState);

  nsFrameList childList;

  // Process children
  if (aItem.mFCData->mBits & FCDATA_USE_CHILD_ITEMS) {
    ConstructFramesFromItemList(
        aState, aItem.mChildItems, innerFrame,
        aItem.mFCData->mBits & FCDATA_IS_WRAPPER_ANON_BOX, childList);
  } else {
    ProcessChildren(aState, content, computedStyle, innerFrame, true, childList,
                    false);
  }

  // Set the inner wrapper frame's initial primary list
  innerFrame->SetInitialChildList(FrameChildListID::Principal,
                                  std::move(childList));

  return newFrame;
}

nsIFrame* nsCSSFrameConstructor::ConstructOuterSVG(
    nsFrameConstructorState& aState, FrameConstructionItem& aItem,
    nsContainerFrame* aParentFrame, const nsStyleDisplay* aDisplay,
    nsFrameList& aFrameList) {
  return ConstructFrameWithAnonymousChild(
      aState, aItem, aParentFrame, aFrameList, NS_NewSVGOuterSVGFrame,
      NS_NewSVGOuterSVGAnonChildFrame, PseudoStyleType::mozSVGOuterSVGAnonChild,
      true);
}

nsIFrame* nsCSSFrameConstructor::ConstructMarker(
    nsFrameConstructorState& aState, FrameConstructionItem& aItem,
    nsContainerFrame* aParentFrame, const nsStyleDisplay* aDisplay,
    nsFrameList& aFrameList) {
  return ConstructFrameWithAnonymousChild(
      aState, aItem, aParentFrame, aFrameList, NS_NewSVGMarkerFrame,
      NS_NewSVGMarkerAnonChildFrame, PseudoStyleType::mozSVGMarkerAnonChild,
      false);
}

// Only outer <svg> elements can be floated or positioned.  All other SVG
// should be in-flow.
#define SIMPLE_SVG_FCDATA(_func)                      \
  FrameConstructionData(ToCreationFunc(_func),        \
                        FCDATA_DISALLOW_OUT_OF_FLOW | \
                            FCDATA_SKIP_ABSPOS_PUSH | \
                            FCDATA_DISALLOW_GENERATED_CONTENT)
#define SIMPLE_SVG_CREATE(_tag, _func) \
  { nsGkAtoms::_tag, SIMPLE_SVG_FCDATA(_func) }

/* static */
const nsCSSFrameConstructor::FrameConstructionData*
nsCSSFrameConstructor::FindSVGData(const Element& aElement,
                                   nsIFrame* aParentFrame,
                                   bool aIsWithinSVGText,
                                   bool aAllowsTextPathChild,
                                   ComputedStyle& aStyle) {
  MOZ_ASSERT(aElement.IsSVGElement());

  static constexpr FrameConstructionData sSuppressData = SUPPRESS_FCDATA();
  static constexpr FrameConstructionData sContainerData =
      SIMPLE_SVG_FCDATA(NS_NewSVGContainerFrame);

  bool parentIsSVG = aIsWithinSVGText;
  nsIContent* parentContent =
      aParentFrame ? aParentFrame->GetContent() : nullptr;

  nsAtom* tag = aElement.NodeInfo()->NameAtom();

  // XXXbz should this really be based on the tag of the parent frame's content?
  // Should it not be based on the type of the parent frame (e.g. whether it's
  // an SVG frame)?
  if (parentContent) {
    // It's not clear whether the SVG spec intends to allow any SVG
    // content within svg:foreignObject at all (SVG 1.1, section
    // 23.2), but if it does, it better be svg:svg.  So given that
    // we're allowing it, treat it as a non-SVG parent.
    parentIsSVG =
        parentContent->IsSVGElement() &&
        parentContent->NodeInfo()->NameAtom() != nsGkAtoms::foreignObject;
  }

  if ((tag != nsGkAtoms::svg && !parentIsSVG) ||
      (tag == nsGkAtoms::desc || tag == nsGkAtoms::title ||
       tag == nsGkAtoms::metadata)) {
    // Sections 5.1 and G.4 of SVG 1.1 say that SVG elements other than
    // svg:svg not contained within svg:svg are incorrect, although they
    // don't seem to specify error handling.  Ignore them, since many of
    // our frame classes can't deal.  It *may* be that the document
    // should at that point be considered in error according to F.2, but
    // it's hard to tell.
    //
    // Style mutation can't change this situation, so don't bother
    // adding to the undisplayed content map.
    //
    // We don't currently handle any UI for desc/title/metadata
    return &sSuppressData;
  }

  // We don't need frames for animation elements
  if (aElement.IsSVGAnimationElement()) {
    return &sSuppressData;
  }

  if (tag == nsGkAtoms::svg && !parentIsSVG) {
    // We need outer <svg> elements to have an SVGOuterSVGFrame regardless
    // of whether they fail conditional processing attributes, since various
    // SVG frames assume that one exists.  We handle the non-rendering
    // of failing outer <svg> element contents like <switch> statements,
    // and do the PassesConditionalProcessingTests call in
    // SVGOuterSVGFrame::Init.
    static constexpr FrameConstructionData sOuterSVGData(
        &nsCSSFrameConstructor::ConstructOuterSVG);
    return &sOuterSVGData;
  }

  if (tag == nsGkAtoms::marker) {
    static constexpr FrameConstructionData sMarkerSVGData(
        &nsCSSFrameConstructor::ConstructMarker);
    return &sMarkerSVGData;
  }

  if (!aElement.PassesConditionalProcessingTests()) {
    // Elements with failing conditional processing attributes never get
    // rendered.  Note that this is not where we select which frame in a
    // <switch> to render!  That happens in SVGSwitchFrame::PaintSVG.
    if (aIsWithinSVGText) {
      // SVGTextFrame doesn't handle conditional processing attributes,
      // so don't create frames for descendants of <text> with failing
      // attributes.  We need frames not to be created so that text layout
      // is correct.
      return &sSuppressData;
    }
    // If we're not inside <text>, create an SVGContainerFrame (which is a
    // frame that doesn't render) so that paint servers can still be referenced,
    // even if they live inside an element with failing conditional processing
    // attributes.
    return &sContainerData;
  }

  // Ensure that a stop frame is a child of a gradient and that gradients
  // can only have stop children.
  bool parentIsGradient = aParentFrame && static_cast<SVGGradientFrame*>(
                                              do_QueryFrame(aParentFrame));
  bool stop = (tag == nsGkAtoms::stop);
  if ((parentIsGradient && !stop) || (!parentIsGradient && stop)) {
    return &sSuppressData;
  }

  // Prevent bad frame types being children of filters or parents of filter
  // primitives.  If aParentFrame is null, we know that the frame that will
  // be created will be an nsInlineFrame, so it can never be a filter.
  bool parentIsFilter = aParentFrame && aParentFrame->IsSVGFilterFrame();
  if ((parentIsFilter && !aElement.IsSVGFilterPrimitiveElement()) ||
      (!parentIsFilter && aElement.IsSVGFilterPrimitiveElement())) {
    return &sSuppressData;
  }

  // Prevent bad frame types being children of filter primitives or parents of
  // filter primitive children.  If aParentFrame is null, we know that the frame
  // that will be created will be an nsInlineFrame, so it can never be a filter
  // primitive.
  bool parentIsFEContainerFrame =
      aParentFrame && aParentFrame->IsSVGFEContainerFrame();
  if ((parentIsFEContainerFrame &&
       !aElement.IsSVGFilterPrimitiveChildElement()) ||
      (!parentIsFEContainerFrame &&
       aElement.IsSVGFilterPrimitiveChildElement())) {
    return &sSuppressData;
  }

  // Special cases for text/tspan/textPath, because the kind of frame
  // they get depends on the parent frame.  We ignore 'a' elements when
  // determining the parent, however.
  if (aIsWithinSVGText) {
    // If aIsWithinSVGText is true, then we know that the "SVG text uses
    // CSS frames" pref was true when this SVG fragment was first constructed.
    //
    // FIXME(bug 1588477) Don't render stuff in display: contents / Shadow DOM
    // subtrees, because TextCorrespondenceRecorder in the SVG text code doesn't
    // really know how to deal with it. This kinda sucks. :(
    if (aParentFrame && aParentFrame->GetContent() != aElement.GetParent()) {
      return &sSuppressData;
    }

    // We don't use ConstructInline because we want different behavior
    // for generated content.
    static constexpr FrameConstructionData sTSpanData(
        ToCreationFunc(NS_NewInlineFrame),
        FCDATA_DISALLOW_OUT_OF_FLOW | FCDATA_SKIP_ABSPOS_PUSH |
            FCDATA_DISALLOW_GENERATED_CONTENT | FCDATA_IS_LINE_PARTICIPANT |
            FCDATA_IS_INLINE | FCDATA_USE_CHILD_ITEMS);
    if (tag == nsGkAtoms::textPath) {
      if (aAllowsTextPathChild) {
        return &sTSpanData;
      }
    } else if (tag == nsGkAtoms::tspan || tag == nsGkAtoms::a) {
      return &sTSpanData;
    }
    return &sSuppressData;
  } else if (tag == nsGkAtoms::tspan || tag == nsGkAtoms::textPath) {
    return &sSuppressData;
  }

  static constexpr FrameConstructionDataByTag sSVGData[] = {
      SIMPLE_SVG_CREATE(svg, NS_NewSVGInnerSVGFrame),
      SIMPLE_SVG_CREATE(g, NS_NewSVGGFrame),
      SIMPLE_SVG_CREATE(svgSwitch, NS_NewSVGSwitchFrame),
      SIMPLE_SVG_CREATE(symbol, NS_NewSVGSymbolFrame),
      SIMPLE_SVG_CREATE(polygon, NS_NewSVGGeometryFrame),
      SIMPLE_SVG_CREATE(polyline, NS_NewSVGGeometryFrame),
      SIMPLE_SVG_CREATE(circle, NS_NewSVGGeometryFrame),
      SIMPLE_SVG_CREATE(ellipse, NS_NewSVGGeometryFrame),
      SIMPLE_SVG_CREATE(line, NS_NewSVGGeometryFrame),
      SIMPLE_SVG_CREATE(rect, NS_NewSVGGeometryFrame),
      SIMPLE_SVG_CREATE(path, NS_NewSVGGeometryFrame),
      SIMPLE_SVG_CREATE(defs, NS_NewSVGContainerFrame),
      {nsGkAtoms::text,
       {NS_NewSVGTextFrame,
        FCDATA_DISALLOW_OUT_OF_FLOW | FCDATA_ALLOW_BLOCK_STYLES,
        PseudoStyleType::mozSVGText}},
      {nsGkAtoms::foreignObject,
       {ToCreationFunc(NS_NewSVGForeignObjectFrame),
        FCDATA_DISALLOW_OUT_OF_FLOW, PseudoStyleType::mozSVGForeignContent}},
      SIMPLE_SVG_CREATE(a, NS_NewSVGAFrame),
      SIMPLE_SVG_CREATE(linearGradient, NS_NewSVGLinearGradientFrame),
      SIMPLE_SVG_CREATE(radialGradient, NS_NewSVGRadialGradientFrame),
      SIMPLE_SVG_CREATE(stop, NS_NewSVGStopFrame),
      SIMPLE_SVG_CREATE(use, NS_NewSVGUseFrame),
      SIMPLE_SVG_CREATE(view, NS_NewSVGViewFrame),
      SIMPLE_SVG_CREATE(image, NS_NewSVGImageFrame),
      SIMPLE_SVG_CREATE(clipPath, NS_NewSVGClipPathFrame),
      SIMPLE_SVG_CREATE(filter, NS_NewSVGFilterFrame),
      SIMPLE_SVG_CREATE(pattern, NS_NewSVGPatternFrame),
      SIMPLE_SVG_CREATE(mask, NS_NewSVGMaskFrame),
      SIMPLE_SVG_CREATE(feDistantLight, NS_NewSVGFEUnstyledLeafFrame),
      SIMPLE_SVG_CREATE(fePointLight, NS_NewSVGFEUnstyledLeafFrame),
      SIMPLE_SVG_CREATE(feSpotLight, NS_NewSVGFEUnstyledLeafFrame),
      SIMPLE_SVG_CREATE(feBlend, NS_NewSVGFELeafFrame),
      SIMPLE_SVG_CREATE(feColorMatrix, NS_NewSVGFELeafFrame),
      SIMPLE_SVG_CREATE(feFuncR, NS_NewSVGFEUnstyledLeafFrame),
      SIMPLE_SVG_CREATE(feFuncG, NS_NewSVGFEUnstyledLeafFrame),
      SIMPLE_SVG_CREATE(feFuncB, NS_NewSVGFEUnstyledLeafFrame),
      SIMPLE_SVG_CREATE(feFuncA, NS_NewSVGFEUnstyledLeafFrame),
      SIMPLE_SVG_CREATE(feComposite, NS_NewSVGFELeafFrame),
      SIMPLE_SVG_CREATE(feComponentTransfer, NS_NewSVGFEContainerFrame),
      SIMPLE_SVG_CREATE(feConvolveMatrix, NS_NewSVGFELeafFrame),
      SIMPLE_SVG_CREATE(feDiffuseLighting, NS_NewSVGFEContainerFrame),
      SIMPLE_SVG_CREATE(feDisplacementMap, NS_NewSVGFELeafFrame),
      SIMPLE_SVG_CREATE(feDropShadow, NS_NewSVGFELeafFrame),
      SIMPLE_SVG_CREATE(feFlood, NS_NewSVGFELeafFrame),
      SIMPLE_SVG_CREATE(feGaussianBlur, NS_NewSVGFELeafFrame),
      SIMPLE_SVG_CREATE(feImage, NS_NewSVGFEImageFrame),
      SIMPLE_SVG_CREATE(feMerge, NS_NewSVGFEContainerFrame),
      SIMPLE_SVG_CREATE(feMergeNode, NS_NewSVGFEUnstyledLeafFrame),
      SIMPLE_SVG_CREATE(feMorphology, NS_NewSVGFELeafFrame),
      SIMPLE_SVG_CREATE(feOffset, NS_NewSVGFELeafFrame),
      SIMPLE_SVG_CREATE(feSpecularLighting, NS_NewSVGFEContainerFrame),
      SIMPLE_SVG_CREATE(feTile, NS_NewSVGFELeafFrame),
      SIMPLE_SVG_CREATE(feTurbulence, NS_NewSVGFELeafFrame)};

  const FrameConstructionData* data =
      FindDataByTag(aElement, aStyle, sSVGData, ArrayLength(sSVGData));

  if (!data) {
    data = &sContainerData;
  }

  return data;
}

void nsCSSFrameConstructor::InsertPageBreakItem(
    nsIContent* aContent, FrameConstructionItemList& aItems,
    InsertPageBreakLocation location) {
  RefPtr<ComputedStyle> pseudoStyle =
      mPresShell->StyleSet()->ResolveNonInheritingAnonymousBoxStyle(
          PseudoStyleType::pageBreak);

  MOZ_ASSERT(pseudoStyle->StyleDisplay()->mDisplay == StyleDisplay::Block,
             "Unexpected display");

  static constexpr FrameConstructionData sPageBreakData(NS_NewPageBreakFrame,
                                                        FCDATA_SKIP_FRAMESET);
  if (location == InsertPageBreakLocation::eBefore) {
    aItems.PrependItem(this, &sPageBreakData, aContent, pseudoStyle.forget(),
                       true);
  } else {
    aItems.AppendItem(this, &sPageBreakData, aContent, pseudoStyle.forget(),
                      true);
  }
}

bool nsCSSFrameConstructor::ShouldCreateItemsForChild(
    nsFrameConstructorState& aState, nsIContent* aContent,
    nsContainerFrame* aParentFrame) {
  aContent->UnsetFlags(NODE_DESCENDANTS_NEED_FRAMES | NODE_NEEDS_FRAME);
  // XXX the GetContent() != aContent check is needed due to bug 135040.
  // Remove it once that's fixed.
  if (aContent->GetPrimaryFrame() &&
      aContent->GetPrimaryFrame()->GetContent() == aContent &&
      !aState.mCreatingExtraFrames) {
    MOZ_ASSERT(false,
               "asked to create frame construction item for a node that "
               "already has a frame");
    return false;
  }

  // don't create a whitespace frame if aParent doesn't want it
  if (!NeedFrameFor(aState, aParentFrame, aContent)) {
    return false;
  }

  // never create frames for comments or PIs
  if (aContent->IsComment() || aContent->IsProcessingInstruction()) {
    return false;
  }

  return true;
}

void nsCSSFrameConstructor::AddFrameConstructionItems(
    nsFrameConstructorState& aState, nsIContent* aContent,
    bool aSuppressWhiteSpaceOptimizations, const ComputedStyle& aParentStyle,
    const InsertionPoint& aInsertion, FrameConstructionItemList& aItems,
    ItemFlags aFlags) {
  nsContainerFrame* parentFrame = aInsertion.mParentFrame;
  if (!ShouldCreateItemsForChild(aState, aContent, parentFrame)) {
    return;
  }
  if (MOZ_UNLIKELY(aParentStyle.StyleContent()->mContent.IsNone()) &&
      StaticPrefs::layout_css_element_content_none_enabled()) {
    return;
  }

  RefPtr<ComputedStyle> computedStyle = ResolveComputedStyle(aContent);
  auto flags = aFlags + ItemFlag::AllowPageBreak;
  if (parentFrame) {
    if (parentFrame->IsInSVGTextSubtree()) {
      flags += ItemFlag::IsWithinSVGText;
    }
    if (parentFrame->IsBlockFrame() && parentFrame->GetParent() &&
        parentFrame->GetParent()->IsSVGTextFrame()) {
      flags += ItemFlag::AllowTextPathChild;
    }
  }
  AddFrameConstructionItemsInternal(aState, aContent, parentFrame,
                                    aSuppressWhiteSpaceOptimizations,
                                    computedStyle, flags, aItems);
}

// Whether we should suppress frames for a child under a <select> frame.
//
// Never create frames for non-option/optgroup kids of <select> and non-option
// kids of <optgroup> inside a <select>.
static bool ShouldSuppressFrameInSelect(const nsIContent* aParent,
                                        const nsIContent& aChild) {
  if (!aParent ||
      !aParent->IsAnyOfHTMLElements(nsGkAtoms::select, nsGkAtoms::optgroup,
                                    nsGkAtoms::option)) {
    return false;
  }

  // Allow native anonymous content no matter what.
  if (aChild.IsRootOfNativeAnonymousSubtree()) {
    return false;
  }

  // Options with labels have their label text added in ::before by forms.css.
  // Suppress frames for their child text.
  if (aParent->IsHTMLElement(nsGkAtoms::option)) {
    return aParent->AsElement()->HasNonEmptyAttr(nsGkAtoms::label);
  }

  // If we're in any display: contents subtree, just suppress the frame.
  //
  // We can't be regular NAC, since display: contents has no frame to generate
  // them off.
  if (aChild.GetParent() != aParent) {
    return true;
  }

  // Option is always fine.
  if (aChild.IsHTMLElement(nsGkAtoms::option)) {
    return false;
  }

  // <optgroup> is OK in <select> but not in <optgroup>.
  if (aChild.IsHTMLElement(nsGkAtoms::optgroup) &&
      aParent->IsHTMLElement(nsGkAtoms::select)) {
    return false;
  }

  // Anything else is not ok.
  return true;
}

const nsCSSFrameConstructor::FrameConstructionData*
nsCSSFrameConstructor::FindDataForContent(nsIContent& aContent,
                                          ComputedStyle& aStyle,
                                          nsIFrame* aParentFrame,
                                          ItemFlags aFlags) {
  MOZ_ASSERT(aStyle.StyleDisplay()->mDisplay != StyleDisplay::None &&
                 aStyle.StyleDisplay()->mDisplay != StyleDisplay::Contents,
             "These two special display values should be handled earlier");

  if (auto* text = Text::FromNode(aContent)) {
    return FindTextData(*text, aParentFrame);
  }

  return FindElementData(*aContent.AsElement(), aStyle, aParentFrame, aFlags);
}

const nsCSSFrameConstructor::FrameConstructionData*
nsCSSFrameConstructor::FindElementData(const Element& aElement,
                                       ComputedStyle& aStyle,
                                       nsIFrame* aParentFrame,
                                       ItemFlags aFlags) {
  // Don't create frames for non-SVG element children of SVG elements.
  if (!aElement.IsSVGElement()) {
    if (aParentFrame && IsFrameForSVG(aParentFrame) &&
        !aParentFrame->IsSVGForeignObjectFrame()) {
      return nullptr;
    }
    if (aFlags.contains(ItemFlag::IsWithinSVGText)) {
      return nullptr;
    }
  }

  if (auto* data = FindElementTagData(aElement, aStyle, aParentFrame, aFlags)) {
    return data;
  }

  // Check for 'content: <image-url>' on the element (which makes us ignore
  // 'display' values other than 'none' or 'contents').
  if (nsImageFrame::ShouldCreateImageFrameForContentProperty(aElement,
                                                             aStyle)) {
    static constexpr FrameConstructionData sImgData(
        NS_NewImageFrameForContentProperty);
    return &sImgData;
  }

  const bool shouldBlockify = aFlags.contains(ItemFlag::IsForRenderedLegend) ||
                              aFlags.contains(ItemFlag::IsForOutsideMarker);
  if (shouldBlockify && !aStyle.StyleDisplay()->IsBlockOutsideStyle()) {
    // Make a temp copy of StyleDisplay and blockify its mDisplay value.
    auto display = *aStyle.StyleDisplay();
    bool isRootElement = false;
    uint16_t rawDisplayValue =
        Servo_ComputedValues_BlockifiedDisplay(&aStyle, isRootElement);
    display.mDisplay = StyleDisplay{rawDisplayValue};
    return FindDisplayData(display, aElement);
  }

  const auto& display = *aStyle.StyleDisplay();
  return FindDisplayData(display, aElement);
}

const nsCSSFrameConstructor::FrameConstructionData*
nsCSSFrameConstructor::FindElementTagData(const Element& aElement,
                                          ComputedStyle& aStyle,
                                          nsIFrame* aParentFrame,
                                          ItemFlags aFlags) {
  switch (aElement.GetNameSpaceID()) {
    case kNameSpaceID_XHTML:
      return FindHTMLData(aElement, aParentFrame, aStyle);
    case kNameSpaceID_MathML:
      return FindMathMLData(aElement, aStyle);
    case kNameSpaceID_SVG:
      return FindSVGData(aElement, aParentFrame,
                         aFlags.contains(ItemFlag::IsWithinSVGText),
                         aFlags.contains(ItemFlag::AllowTextPathChild), aStyle);
    case kNameSpaceID_XUL:
      return FindXULTagData(aElement, aStyle);
    default:
      return nullptr;
  }
}

void nsCSSFrameConstructor::AddFrameConstructionItemsInternal(
    nsFrameConstructorState& aState, nsIContent* aContent,
    nsContainerFrame* aParentFrame, bool aSuppressWhiteSpaceOptimizations,
    ComputedStyle* aComputedStyle, ItemFlags aFlags,
    FrameConstructionItemList& aItems) {
  MOZ_ASSERT(aContent->IsText() || aContent->IsElement(),
             "Shouldn't get anything else here!");
  MOZ_ASSERT(aContent->IsInComposedDoc());
  MOZ_ASSERT(!aContent->GetPrimaryFrame() || aState.mCreatingExtraFrames ||
             aContent->NodeInfo()->NameAtom() == nsGkAtoms::area);

  const bool withinSVGText = aFlags.contains(ItemFlag::IsWithinSVGText);
  const bool isGeneratedContent = aFlags.contains(ItemFlag::IsGeneratedContent);
  MOZ_ASSERT(!isGeneratedContent || aComputedStyle->IsPseudoElement(),
             "Generated content should be a pseudo-element");

  FrameConstructionItem* item = nullptr;
  auto cleanupGeneratedContent = mozilla::MakeScopeExit([&]() {
    if (isGeneratedContent && !item) {
      MOZ_ASSERT(!IsDisplayContents(aContent),
                 "This would need to change if we support display: contents "
                 "in generated content");
      aContent->UnbindFromTree();
    }
  });

  // 'display:none' elements never creates any frames at all.
  const nsStyleDisplay& display = *aComputedStyle->StyleDisplay();
  if (display.mDisplay == StyleDisplay::None) {
    return;
  }

  if (display.mDisplay == StyleDisplay::Contents) {
    // See the mDisplay fixup code in StyleAdjuster::adjust.
    MOZ_ASSERT(!aContent->AsElement()->IsRootOfNativeAnonymousSubtree(),
               "display:contents on anonymous content is unsupported");

    // FIXME(bug 1588477): <svg:text>'s TextNodeCorrespondenceRecorder has
    // trouble with everything that looks like display: contents.
    if (withinSVGText) {
      return;
    }

    CreateGeneratedContentItem(aState, aParentFrame, *aContent->AsElement(),
                               *aComputedStyle, PseudoStyleType::before,
                               aItems);

    FlattenedChildIterator iter(aContent);
    InsertionPoint insertion(aParentFrame, aContent);
    for (nsIContent* child = iter.GetNextChild(); child;
         child = iter.GetNextChild()) {
      AddFrameConstructionItems(aState, child, aSuppressWhiteSpaceOptimizations,
                                *aComputedStyle, insertion, aItems, aFlags);
    }
    aItems.SetParentHasNoShadowDOM(!iter.ShadowDOMInvolved());

    CreateGeneratedContentItem(aState, aParentFrame, *aContent->AsElement(),
                               *aComputedStyle, PseudoStyleType::after, aItems);
    return;
  }

  nsIContent* parent = aParentFrame ? aParentFrame->GetContent() : nullptr;
  if (ShouldSuppressFrameInSelect(parent, *aContent)) {
    return;
  }

  if (aContent->IsHTMLElement(nsGkAtoms::legend) && aParentFrame) {
    const nsFieldSetFrame* const fs = GetFieldSetFrameFor(aParentFrame);
    if (fs && !fs->GetLegend() && !aState.mHasRenderedLegend &&
        !aComputedStyle->StyleDisplay()->IsFloatingStyle() &&
        !aComputedStyle->StyleDisplay()->IsAbsolutelyPositionedStyle()) {
      aState.mHasRenderedLegend = true;
      aFlags += ItemFlag::IsForRenderedLegend;
    }
  }

  const FrameConstructionData* const data =
      FindDataForContent(*aContent, *aComputedStyle, aParentFrame, aFlags);
  if (!data || data->mBits & FCDATA_SUPPRESS_FRAME) {
    return;
  }

  const bool isPopup = data->mBits & FCDATA_IS_POPUP;

  const uint32_t bits = data->mBits;

  // Inside colgroups, suppress everything except columns.
  if (aParentFrame && aParentFrame->IsTableColGroupFrame() &&
      (!(bits & FCDATA_IS_TABLE_PART) ||
       display.mDisplay != StyleDisplay::TableColumn)) {
    return;
  }

  const bool canHavePageBreak =
      aFlags.contains(ItemFlag::AllowPageBreak) &&
      aState.mPresContext->IsPaginated() &&
      !display.IsAbsolutelyPositionedStyle() &&
      !(aParentFrame && aParentFrame->IsGridContainerFrame()) &&
      !(bits & FCDATA_IS_TABLE_PART) && !(bits & FCDATA_IS_SVG_TEXT);
  if (canHavePageBreak && display.BreakBefore()) {
    AppendPageBreakItem(aContent, aItems);
  }

  if (!item) {
    item = aItems.AppendItem(this, data, aContent, do_AddRef(aComputedStyle),
                             aSuppressWhiteSpaceOptimizations);
    if (aFlags.contains(ItemFlag::IsForRenderedLegend)) {
      item->mIsRenderedLegend = true;
    }
  }
  item->mIsText = !aContent->IsElement();
  item->mIsGeneratedContent = isGeneratedContent;
  if (isGeneratedContent) {
    // We need to keep this alive until the frame takes ownership.
    // This corresponds to the Release in ConstructFramesFromItem.
    item->mContent->AddRef();
  }
  item->mIsPopup = isPopup;

  if (canHavePageBreak && display.BreakAfter()) {
    AppendPageBreakItem(aContent, aItems);
  }

  if (bits & FCDATA_IS_INLINE) {
    // To correctly set item->mIsAllInline we need to build up our child items
    // right now.
    BuildInlineChildItems(aState, *item,
                          aFlags.contains(ItemFlag::IsWithinSVGText),
                          aFlags.contains(ItemFlag::AllowTextPathChild));
    item->mIsBlock = false;
  } else {
    // Compute a boolean isInline which is guaranteed to be false for blocks
    // (but may also be false for some inlines).
    const bool isInline =
        // Table-internal things are inline-outside if and only if they're kids
        // of inlines, since they'll trigger construction of inline-table
        // pseudos.
        ((bits & FCDATA_IS_TABLE_PART) &&
         (!aParentFrame ||  // No aParentFrame means inline
          aParentFrame->StyleDisplay()->IsInlineFlow())) ||
        // Things that are inline-outside but aren't inline frames are inline
        display.IsInlineOutsideStyle() ||
        // Popups that are certainly out of flow.
        isPopup;

    // Set mIsAllInline conservatively.  It just might be that even an inline
    // that has mIsAllInline false doesn't need an {ib} split.  So this is just
    // an optimization to keep from doing too much work in cases when we can
    // show that mIsAllInline is true..
    item->mIsAllInline =
        isInline ||
        // Figure out whether we're guaranteed this item will be out of flow.
        // This is not a precise test, since one of our ancestor inlines might
        // add an absolute containing block (if it's relatively positioned) when
        // there wasn't such a containing block before.  But it's conservative
        // in the sense that anything that will really end up as an in-flow
        // non-inline will test false here.  In other words, if this test is
        // true we're guaranteed to be inline; if it's false we don't know what
        // we'll end up as.
        //
        // If we make this test precise, we can remove some of the code dealing
        // with the imprecision in ConstructInline and adjust the comments on
        // mIsAllInline and mIsBlock in the header.
        (!(bits & FCDATA_DISALLOW_OUT_OF_FLOW) &&
         aState.GetGeometricParent(display, nullptr));

    // Set mIsBlock conservatively.  It's OK to set it false for some real
    // blocks, but not OK to set it true for things that aren't blocks.  Since
    // isOutOfFlow might be false even in cases when the frame will end up
    // out-of-flow, we can't use it here.  But we _can_ say that the frame will
    // for sure end up in-flow if it's not floated or absolutely positioned.
    item->mIsBlock = !isInline && !display.IsAbsolutelyPositionedStyle() &&
                     !display.IsFloatingStyle() && !(bits & FCDATA_IS_SVG_TEXT);
  }

  if (item->mIsAllInline) {
    aItems.InlineItemAdded();
  } else if (item->mIsBlock) {
    aItems.BlockItemAdded();
  }
}

/**
 * Return true if the frame construction item pointed to by aIter will
 * create a frame adjacent to a line boundary in the frame tree, and that
 * line boundary is induced by a content node adjacent to the frame's
 * content node in the content tree. The latter condition is necessary so
 * that ContentAppended/ContentInserted/ContentRemoved can easily find any
 * text nodes that were suppressed here.
 */
bool nsCSSFrameConstructor::AtLineBoundary(FCItemIterator& aIter) {
  if (aIter.item().mSuppressWhiteSpaceOptimizations) {
    return false;
  }

  if (aIter.AtStart()) {
    if (aIter.List()->HasLineBoundaryAtStart() &&
        !aIter.item().mContent->GetPreviousSibling())
      return true;
  } else {
    FCItemIterator prev = aIter;
    prev.Prev();
    if (prev.item().IsLineBoundary() &&
        !prev.item().mSuppressWhiteSpaceOptimizations &&
        aIter.item().mContent->GetPreviousSibling() == prev.item().mContent)
      return true;
  }

  FCItemIterator next = aIter;
  next.Next();
  if (next.IsDone()) {
    if (aIter.List()->HasLineBoundaryAtEnd() &&
        !aIter.item().mContent->GetNextSibling())
      return true;
  } else {
    if (next.item().IsLineBoundary() &&
        !next.item().mSuppressWhiteSpaceOptimizations &&
        aIter.item().mContent->GetNextSibling() == next.item().mContent)
      return true;
  }

  return false;
}

void nsCSSFrameConstructor::ConstructFramesFromItem(
    nsFrameConstructorState& aState, FCItemIterator& aIter,
    nsContainerFrame* aParentFrame, nsFrameList& aFrameList) {
  FrameConstructionItem& item = aIter.item();
  ComputedStyle* computedStyle = item.mComputedStyle;
  if (item.mIsText) {
    // If this is collapsible whitespace next to a line boundary,
    // don't create a frame. item.IsWhitespace() also sets the
    // NS_CREATE_FRAME_IF_NON_WHITESPACE flag in the text node. (If we
    // end up creating a frame, nsTextFrame::Init will clear the flag.)
    // We don't do this for generated content, because some generated
    // text content is empty text nodes that are about to be initialized.
    // (We check mAdditionalStateBits because only the generated content
    // container's frame construction item is marked with
    // mIsGeneratedContent, and we might not have an aParentFrame.)
    // We don't do it for content that may have Shadow DOM siblings / insertion
    // points, because they make it difficult to correctly create the frame due
    // to dynamic changes.
    // We don't do it for SVG text, since we might need to position and
    // measure the white space glyphs due to x/y/dx/dy attributes.
    if (AtLineBoundary(aIter) &&
        !computedStyle->StyleText()->WhiteSpaceOrNewlineIsSignificant() &&
        aIter.List()->ParentHasNoShadowDOM() &&
        !(aState.mAdditionalStateBits & NS_FRAME_GENERATED_CONTENT) &&
        (item.mFCData->mBits & FCDATA_IS_LINE_PARTICIPANT) &&
        !(item.mFCData->mBits & FCDATA_IS_SVG_TEXT) &&
        !mAlwaysCreateFramesForIgnorableWhitespace && item.IsWhitespace(aState))
      return;

    ConstructTextFrame(item.mFCData, aState, item.mContent, aParentFrame,
                       computedStyle, aFrameList);
    return;
  }

  AutoRestore<nsFrameState> savedStateBits(aState.mAdditionalStateBits);
  if (item.mIsGeneratedContent) {
    // Ensure that frames created here are all tagged with
    // NS_FRAME_GENERATED_CONTENT.
    aState.mAdditionalStateBits |= NS_FRAME_GENERATED_CONTENT;
  }

  // XXXbz maybe just inline ConstructFrameFromItemInternal here or something?
  ConstructFrameFromItemInternal(item, aState, aParentFrame, aFrameList);

  if (item.mIsGeneratedContent) {
    // This corresponds to the AddRef in AddFrameConstructionItemsInternal.
    // The frame owns the generated content now.
    item.mContent->Release();

    // Now that we've passed ownership of item.mContent to the frame, unset
    // our generated content flag so we don't release or unbind it ourselves.
    item.mIsGeneratedContent = false;
  }
}

nsContainerFrame* nsCSSFrameConstructor::GetAbsoluteContainingBlock(
    nsIFrame* aFrame, ContainingBlockType aType) {
  // Starting with aFrame, look for a frame that is absolutely positioned or
  // relatively positioned (and transformed, if aType is FIXED)
  for (nsIFrame* frame = aFrame; frame; frame = frame->GetParent()) {
    if (frame->IsMathMLFrame()) {
      // If it's mathml, bail out -- no absolute positioning out from inside
      // mathml frames.  Note that we don't make this part of the loop
      // condition because of the stuff at the end of this method...
      return nullptr;
    }

    // Look for the ICB.
    if (aType == FIXED_POS) {
      LayoutFrameType t = frame->Type();
      if (t == LayoutFrameType::Viewport || t == LayoutFrameType::PageContent) {
        return static_cast<nsContainerFrame*>(frame);
      }
    }

    // If the frame is positioned, we will probably return it as the containing
    // block (see the exceptions below).  Otherwise, we'll start looking at the
    // parent frame, unless we're dealing with a scrollframe.
    // Scrollframes are special since they're not positioned, but their
    // scrolledframe might be.  So, we need to check this special case to return
    // the correct containing block (the scrolledframe) in that case.
    // If we're looking for a fixed-pos containing block and the frame is
    // not transformed, skip it.
    if (!frame->IsAbsPosContainingBlock()) {
      continue;
    }
    if (aType == FIXED_POS && !frame->IsFixedPosContainingBlock()) {
      continue;
    }
    nsIFrame* absPosCBCandidate = frame;
    LayoutFrameType type = absPosCBCandidate->Type();
    if (type == LayoutFrameType::FieldSet) {
      absPosCBCandidate =
          static_cast<nsFieldSetFrame*>(absPosCBCandidate)->GetInner();
      if (!absPosCBCandidate) {
        continue;
      }
      type = absPosCBCandidate->Type();
    }
    if (type == LayoutFrameType::Scroll) {
      nsIScrollableFrame* scrollFrame = do_QueryFrame(absPosCBCandidate);
      absPosCBCandidate = scrollFrame->GetScrolledFrame();
      if (!absPosCBCandidate) {
        continue;
      }
      type = absPosCBCandidate->Type();
    }
    // Only first continuations can be containing blocks.
    absPosCBCandidate = absPosCBCandidate->FirstContinuation();
    // Is the frame really an absolute container?
    if (!absPosCBCandidate->IsAbsoluteContainer()) {
      continue;
    }

    // For tables, skip the inner frame and consider the table wrapper frame.
    if (type == LayoutFrameType::Table) {
      continue;
    }
    // For table wrapper frames, we can just return absPosCBCandidate.
    MOZ_ASSERT((nsContainerFrame*)do_QueryFrame(absPosCBCandidate),
               "abs.pos. containing block must be nsContainerFrame sub-class");
    return static_cast<nsContainerFrame*>(absPosCBCandidate);
  }

  MOZ_ASSERT(aType != FIXED_POS, "no ICB in this frame tree?");

  // It is possible for the search for the containing block to fail, because
  // no absolute container can be found in the parent chain.  In those cases,
  // we fall back to the document element's containing block.
  return mDocElementContainingBlock;
}

nsContainerFrame* nsCSSFrameConstructor::GetFloatContainingBlock(
    nsIFrame* aFrame) {
  // Starting with aFrame, look for a frame that is a float containing block.
  // If we hit a frame which prevents its descendants from floating, bail out.
  // The logic here needs to match the logic in MaybePushFloatContainingBlock().
  for (nsIFrame* containingBlock = aFrame;
       containingBlock && !ShouldSuppressFloatingOfDescendants(containingBlock);
       containingBlock = containingBlock->GetParent()) {
    if (containingBlock->IsFloatContainingBlock()) {
      MOZ_ASSERT((nsContainerFrame*)do_QueryFrame(containingBlock),
                 "float containing block must be nsContainerFrame sub-class");
      return static_cast<nsContainerFrame*>(containingBlock);
    }
  }

  // If we didn't find a containing block, then there just isn't
  // one.... return null
  return nullptr;
}

/**
 * This function will get the previous sibling to use for an append operation.
 *
 * It takes a parent frame (must not be null) and the next insertion sibling, if
 * the parent content is display: contents or has ::after content (may be null).
 */
static nsIFrame* FindAppendPrevSibling(nsIFrame* aParentFrame,
                                       nsIFrame* aNextSibling) {
  aParentFrame->DrainSelfOverflowList();

  if (aNextSibling) {
    MOZ_ASSERT(
        aNextSibling->GetParent()->GetContentInsertionFrame() == aParentFrame,
        "Wrong parent");
    return aNextSibling->GetPrevSibling();
  }

  return aParentFrame->PrincipalChildList().LastChild();
}

/**
 * Finds the right parent frame to append content to aParentFrame.
 *
 * Cannot return or receive null.
 */
static nsContainerFrame* ContinuationToAppendTo(
    nsContainerFrame* aParentFrame) {
  MOZ_ASSERT(aParentFrame);

  if (IsFramePartOfIBSplit(aParentFrame)) {
    // If the frame we are manipulating is a ib-split frame (that is, one that's
    // been created as a result of a block-in-inline situation) then we need to
    // append to the last ib-split sibling, not to the frame itself.
    //
    // Always make sure to look at the last continuation of the frame for the
    // {ib} case, even if that continuation is empty.
    //
    // We don't do this for the non-ib-split-frame case, since in the other
    // cases appending to the last nonempty continuation is fine and in fact not
    // doing that can confuse code that doesn't know to pull kids from
    // continuations other than its next one.
    return static_cast<nsContainerFrame*>(
        GetLastIBSplitSibling(aParentFrame)->LastContinuation());
  }

  return nsLayoutUtils::LastContinuationWithChild(aParentFrame);
}

/**
 * This function will get the next sibling for a frame insert operation given
 * the parent and previous sibling.  aPrevSibling may be null.
 */
static nsIFrame* GetInsertNextSibling(nsIFrame* aParentFrame,
                                      nsIFrame* aPrevSibling) {
  if (aPrevSibling) {
    return aPrevSibling->GetNextSibling();
  }

  return aParentFrame->PrincipalChildList().FirstChild();
}

void nsCSSFrameConstructor::AppendFramesToParent(
    nsFrameConstructorState& aState, nsContainerFrame* aParentFrame,
    nsFrameList& aFrameList, nsIFrame* aPrevSibling, bool aIsRecursiveCall) {
  MOZ_ASSERT(
      !IsFramePartOfIBSplit(aParentFrame) || !GetIBSplitSibling(aParentFrame) ||
          !GetIBSplitSibling(aParentFrame)->PrincipalChildList().FirstChild(),
      "aParentFrame has a ib-split sibling with kids?");
  MOZ_ASSERT(!aPrevSibling || aPrevSibling->GetParent() == aParentFrame,
             "Parent and prevsibling don't match");
  MOZ_ASSERT(
      !aParentFrame->HasAnyStateBits(NS_FRAME_HAS_MULTI_COLUMN_ANCESTOR) ||
          !IsFramePartOfIBSplit(aParentFrame),
      "We should have wiped aParentFrame in WipeContainingBlock() "
      "if it's part of an IB split!");

  nsIFrame* nextSibling = ::GetInsertNextSibling(aParentFrame, aPrevSibling);

  NS_ASSERTION(nextSibling || !aParentFrame->GetNextContinuation() ||
                   !aParentFrame->GetNextContinuation()
                        ->PrincipalChildList()
                        .FirstChild() ||
                   aIsRecursiveCall,
               "aParentFrame has later continuations with kids?");
  NS_ASSERTION(
      nextSibling || !IsFramePartOfIBSplit(aParentFrame) ||
          (IsInlineFrame(aParentFrame) && !GetIBSplitSibling(aParentFrame) &&
           !aParentFrame->GetNextContinuation()) ||
          aIsRecursiveCall,
      "aParentFrame is not last?");

  // If we're inserting a list of frames at the end of the trailing inline
  // of an {ib} split, we may need to create additional {ib} siblings to parent
  // them.
  if (!nextSibling && IsFramePartOfIBSplit(aParentFrame)) {
    // When we get here, our frame list might start with a block.  If it does
    // so, and aParentFrame is an inline, and it and all its previous
    // continuations have no siblings, then put the initial blocks from the
    // frame list into the previous block of the {ib} split.  Note that we
    // didn't want to stop at the block part of the split when figuring out
    // initial parent, because that could screw up float parenting; it's easier
    // to do this little fixup here instead.
    if (aFrameList.NotEmpty() && aFrameList.FirstChild()->IsBlockOutside()) {
      // See whether our trailing inline is empty
      nsIFrame* firstContinuation = aParentFrame->FirstContinuation();
      if (firstContinuation->PrincipalChildList().IsEmpty()) {
        // Our trailing inline is empty.  Collect our starting blocks from
        // aFrameList, get the right parent frame for them, and put them in.
        nsFrameList blockKids =
            aFrameList.Split([](nsIFrame* f) { return !f->IsBlockOutside(); });
        NS_ASSERTION(blockKids.NotEmpty(), "No blocks?");

        nsContainerFrame* prevBlock = GetIBSplitPrevSibling(firstContinuation);
        prevBlock =
            static_cast<nsContainerFrame*>(prevBlock->LastContinuation());
        NS_ASSERTION(prevBlock, "Should have previous block here");

        MoveChildrenTo(aParentFrame, prevBlock, blockKids);
      }
    }

    // We want to put some of the frames into this inline frame.
    nsFrameList inlineKids =
        aFrameList.Split([](nsIFrame* f) { return f->IsBlockOutside(); });

    if (!inlineKids.IsEmpty()) {
      AppendFrames(aParentFrame, FrameChildListID::Principal,
                   std::move(inlineKids));
    }

    if (!aFrameList.IsEmpty()) {
      nsFrameList ibSiblings;
      CreateIBSiblings(aState, aParentFrame,
                       aParentFrame->IsAbsPosContainingBlock(), aFrameList,
                       ibSiblings);

      // Make sure to trigger reflow of the inline that used to be our
      // last one and now isn't anymore, since its GetSkipSides() has
      // changed.
      mPresShell->FrameNeedsReflow(aParentFrame,
                                   IntrinsicDirty::FrameAndAncestors,
                                   NS_FRAME_HAS_DIRTY_CHILDREN);

      // Recurse so we create new ib siblings as needed for aParentFrame's
      // parent
      return AppendFramesToParent(aState, aParentFrame->GetParent(), ibSiblings,
                                  aParentFrame, true);
    }
    return;
  }

  // If we're appending a list of frames to the last continuations of a
  // ::-moz-column-content, we may need to create column-span siblings for them.
  if (!nextSibling && IsLastContinuationForColumnContent(aParentFrame)) {
    // Extract any initial non-column-span kids, and append them to
    // ::-moz-column-content's child list.
    nsFrameList initialNonColumnSpanKids =
        aFrameList.Split([](nsIFrame* f) { return f->IsColumnSpan(); });
    AppendFrames(aParentFrame, FrameChildListID::Principal,
                 std::move(initialNonColumnSpanKids));

    if (aFrameList.IsEmpty()) {
      // No more kids to process (there weren't any column-span kids).
      return;
    }

    nsFrameList columnSpanSiblings = CreateColumnSpanSiblings(
        aState, aParentFrame, aFrameList,
        // Column content should never be a absolute/fixed positioned containing
        // block. Pass nullptr as aPositionedFrame.
        nullptr);

    nsContainerFrame* columnSetWrapper = aParentFrame->GetParent();
    while (!columnSetWrapper->IsColumnSetWrapperFrame()) {
      columnSetWrapper = columnSetWrapper->GetParent();
    }
    MOZ_ASSERT(columnSetWrapper,
               "No ColumnSetWrapperFrame ancestor for -moz-column-content?");

    FinishBuildingColumns(aState, columnSetWrapper, aParentFrame,
                          columnSpanSiblings);

    MOZ_ASSERT(columnSpanSiblings.IsEmpty(),
               "The column-span siblings should be moved to the proper place!");
    return;
  }

  // Insert the frames after our aPrevSibling
  InsertFrames(aParentFrame, FrameChildListID::Principal, aPrevSibling,
               std::move(aFrameList));
}

// This gets called to see if the frames corresponding to aSibling and aContent
// should be siblings in the frame tree. Although (1) rows and cols, (2) row
// groups and col groups, (3) row groups and captions, (4) legends and content
// inside fieldsets, (5) popups and other kids of the menu are siblings from a
// content perspective, they are not considered siblings in the frame tree.
bool nsCSSFrameConstructor::IsValidSibling(nsIFrame* aSibling,
                                           nsIContent* aContent,
                                           Maybe<StyleDisplay>& aDisplay) {
  StyleDisplay siblingDisplay = aSibling->GetDisplay();
  if (StyleDisplay::TableColumnGroup == siblingDisplay ||
      StyleDisplay::TableColumn == siblingDisplay ||
      StyleDisplay::TableCaption == siblingDisplay ||
      StyleDisplay::TableHeaderGroup == siblingDisplay ||
      StyleDisplay::TableRowGroup == siblingDisplay ||
      StyleDisplay::TableFooterGroup == siblingDisplay) {
    // if we haven't already, resolve a style to find the display type of
    // aContent.
    if (aDisplay.isNothing()) {
      if (aContent->IsComment() || aContent->IsProcessingInstruction()) {
        // Comments and processing instructions never have frames, so we should
        // not try to generate styles for them.
        return false;
      }
      // FIXME(emilio): This is buggy some times, see bug 1424656.
      RefPtr<ComputedStyle> computedStyle = ResolveComputedStyle(aContent);
      const nsStyleDisplay* display = computedStyle->StyleDisplay();
      aDisplay.emplace(display->mDisplay);
    }

    StyleDisplay display = aDisplay.value();
    // To have decent performance we want to return false in cases in which
    // reordering the two siblings has no effect on display.  To ensure
    // correctness, we MUST return false in cases where the two siblings have
    // the same desired parent type and live on different display lists.
    // Specificaly, columns and column groups should only consider columns and
    // column groups as valid siblings.  Captions should only consider other
    // captions.  All other things should consider each other as valid
    // siblings.  The restriction in the |if| above on siblingDisplay is ok,
    // because for correctness the only part that really needs to happen is to
    // not consider captions, column groups, and row/header/footer groups
    // siblings of each other.  Treating a column or colgroup as a valid
    // sibling of a non-table-related frame will just mean we end up reframing.
    if ((siblingDisplay == StyleDisplay::TableCaption) !=
        (display == StyleDisplay::TableCaption)) {
      // One's a caption and the other is not.  Not valid siblings.
      return false;
    }

    if ((siblingDisplay == StyleDisplay::TableColumnGroup ||
         siblingDisplay == StyleDisplay::TableColumn) !=
        (display == StyleDisplay::TableColumnGroup ||
         display == StyleDisplay::TableColumn)) {
      // One's a column or column group and the other is not.  Not valid
      // siblings.
      return false;
    }
    // Fall through; it's possible that the display type was overridden and
    // a different sort of frame was constructed, so we may need to return false
    // below.
  }

  return true;
}

// FIXME(emilio): If we ever kill IsValidSibling() we can simplify this quite a
// bit (no need to pass aTargetContent or aTargetContentDisplay, and the
// adjust() calls can be responsibility of the caller).
template <nsCSSFrameConstructor::SiblingDirection aDirection>
nsIFrame* nsCSSFrameConstructor::FindSiblingInternal(
    FlattenedChildIterator& aIter, nsIContent* aTargetContent,
    Maybe<StyleDisplay>& aTargetContentDisplay) {
  auto adjust = [&](nsIFrame* aPotentialSiblingFrame) -> nsIFrame* {
    return AdjustSiblingFrame(aPotentialSiblingFrame, aTargetContent,
                              aTargetContentDisplay, aDirection);
  };

  auto nextDomSibling = [](FlattenedChildIterator& aIter) -> nsIContent* {
    return aDirection == SiblingDirection::Forward ? aIter.GetNextChild()
                                                   : aIter.GetPreviousChild();
  };

  auto getInsideMarkerFrame = [](const nsIContent* aContent) -> nsIFrame* {
    auto* marker = nsLayoutUtils::GetMarkerFrame(aContent);
    const bool isInsideMarker =
        marker && marker->GetInFlowParent()->StyleList()->mListStylePosition ==
                      StyleListStylePosition::Inside;
    return isInsideMarker ? marker : nullptr;
  };

  auto getNearPseudo = [&](const nsIContent* aContent) -> nsIFrame* {
    if (aDirection == SiblingDirection::Forward) {
      if (auto* marker = getInsideMarkerFrame(aContent)) {
        return marker;
      }
      return nsLayoutUtils::GetBeforeFrame(aContent);
    }
    return nsLayoutUtils::GetAfterFrame(aContent);
  };

  auto getFarPseudo = [&](const nsIContent* aContent) -> nsIFrame* {
    if (aDirection == SiblingDirection::Forward) {
      return nsLayoutUtils::GetAfterFrame(aContent);
    }
    if (auto* before = nsLayoutUtils::GetBeforeFrame(aContent)) {
      return before;
    }
    return getInsideMarkerFrame(aContent);
  };

  while (nsIContent* sibling = nextDomSibling(aIter)) {
    // NOTE(emilio): It's important to check GetPrimaryFrame() before
    // IsDisplayContents to get the correct insertion point when multiple
    // siblings go from display: non-none to display: contents.
    if (nsIFrame* primaryFrame = sibling->GetPrimaryFrame()) {
      // XXX the GetContent() == sibling check is needed due to bug 135040.
      // Remove it once that's fixed.
      if (primaryFrame->GetContent() == sibling) {
        if (nsIFrame* frame = adjust(primaryFrame)) {
          return frame;
        }
      }
    }

    if (IsDisplayContents(sibling)) {
      if (nsIFrame* frame = adjust(getNearPseudo(sibling))) {
        return frame;
      }

      const bool startFromBeginning = aDirection == SiblingDirection::Forward;
      FlattenedChildIterator iter(sibling, startFromBeginning);
      nsIFrame* sibling = FindSiblingInternal<aDirection>(
          iter, aTargetContent, aTargetContentDisplay);
      if (sibling) {
        return sibling;
      }
    }
  }

  return adjust(getFarPseudo(aIter.Parent()));
}

nsIFrame* nsCSSFrameConstructor::AdjustSiblingFrame(
    nsIFrame* aSibling, nsIContent* aTargetContent,
    Maybe<StyleDisplay>& aTargetContentDisplay, SiblingDirection aDirection) {
  if (!aSibling) {
    return nullptr;
  }

  if (aSibling->IsRenderedLegend()) {
    return nullptr;
  }

  if (aSibling->HasAnyStateBits(NS_FRAME_OUT_OF_FLOW)) {
    aSibling = aSibling->GetPlaceholderFrame();
    MOZ_ASSERT(aSibling);
  }

  MOZ_ASSERT(!aSibling->GetPrevContinuation(), "How?");
  if (aDirection == SiblingDirection::Backward) {
    // The frame may be a ib-split frame (a split inline frame that contains a
    // block).  Get the last part of that split.
    if (IsFramePartOfIBSplit(aSibling)) {
      aSibling = GetLastIBSplitSibling(aSibling);
    }

    // The frame may have a continuation. If so, we want the last
    // non-overflow-container continuation as our previous sibling.
    aSibling = aSibling->GetTailContinuation();
  }

  if (!IsValidSibling(aSibling, aTargetContent, aTargetContentDisplay)) {
    return nullptr;
  }

  return aSibling;
}

nsIFrame* nsCSSFrameConstructor::FindPreviousSibling(
    const FlattenedChildIterator& aIter,
    Maybe<StyleDisplay>& aTargetContentDisplay) {
  return FindSibling<SiblingDirection::Backward>(aIter, aTargetContentDisplay);
}

nsIFrame* nsCSSFrameConstructor::FindNextSibling(
    const FlattenedChildIterator& aIter,
    Maybe<StyleDisplay>& aTargetContentDisplay) {
  return FindSibling<SiblingDirection::Forward>(aIter, aTargetContentDisplay);
}

template <nsCSSFrameConstructor::SiblingDirection aDirection>
nsIFrame* nsCSSFrameConstructor::FindSibling(
    const FlattenedChildIterator& aIter,
    Maybe<StyleDisplay>& aTargetContentDisplay) {
  nsIContent* targetContent = aIter.Get();
  FlattenedChildIterator siblingIter = aIter;
  nsIFrame* sibling = FindSiblingInternal<aDirection>(
      siblingIter, targetContent, aTargetContentDisplay);
  if (sibling) {
    return sibling;
  }

  // Our siblings (if any) do not have a frame to guide us. The frame for the
  // target content should be inserted whereever a frame for the container would
  // be inserted. This is needed when inserting into display: contents nodes.
  const nsIContent* current = aIter.Parent();
  while (IsDisplayContents(current)) {
    const nsIContent* parent = current->GetFlattenedTreeParent();
    MOZ_ASSERT(parent, "No display: contents on the root");

    FlattenedChildIterator iter(parent);
    iter.Seek(current);
    sibling = FindSiblingInternal<aDirection>(iter, targetContent,
                                              aTargetContentDisplay);
    if (sibling) {
      return sibling;
    }

    current = parent;
  }

  return nullptr;
}

// For fieldsets, returns the area frame, if the child is not a legend.
static nsContainerFrame* GetAdjustedParentFrame(nsContainerFrame* aParentFrame,
                                                nsIContent* aChildContent) {
  MOZ_ASSERT(!aParentFrame->IsTableWrapperFrame(), "Shouldn't be happening!");

  nsContainerFrame* newParent = nullptr;
  if (aParentFrame->IsFieldSetFrame()) {
    // If the parent is a fieldSet, use the fieldSet's area frame as the
    // parent unless the new content is a legend.
    if (!aChildContent->IsHTMLElement(nsGkAtoms::legend)) {
      newParent = static_cast<nsFieldSetFrame*>(aParentFrame)->GetInner();
      if (newParent) {
        newParent = newParent->GetContentInsertionFrame();
      }
    }
  }
  return newParent ? newParent : aParentFrame;
}

nsIFrame* nsCSSFrameConstructor::GetInsertionPrevSibling(
    InsertionPoint* aInsertion, nsIContent* aChild, bool* aIsAppend,
    bool* aIsRangeInsertSafe, nsIContent* aStartSkipChild,
    nsIContent* aEndSkipChild) {
  MOZ_ASSERT(aInsertion->mParentFrame, "Must have parent frame to start with");

  *aIsAppend = false;

  // Find the frame that precedes the insertion point.
  FlattenedChildIterator iter(aInsertion->mContainer);
  if (iter.ShadowDOMInvolved() || !aChild->IsRootOfNativeAnonymousSubtree()) {
    // The check for IsRootOfNativeAnonymousSubtree() is because editor is
    // severely broken and calls us directly for native anonymous
    // nodes that it creates.
    if (aStartSkipChild) {
      iter.Seek(aStartSkipChild);
    } else {
      iter.Seek(aChild);
    }
  } else {
    // Prime the iterator for the call to FindPreviousSibling.
    iter.GetNextChild();
    MOZ_ASSERT(aChild->GetProperty(nsGkAtoms::restylableAnonymousNode),
               "Someone passed native anonymous content directly into frame "
               "construction.  Stop doing that!");
  }

  // Note that FindPreviousSibling is passed the iterator by value, so that
  // the later usage of the iterator starts from the same place.
  Maybe<StyleDisplay> childDisplay;
  nsIFrame* prevSibling = FindPreviousSibling(iter, childDisplay);

  // Now, find the geometric parent so that we can handle
  // continuations properly. Use the prev sibling if we have it;
  // otherwise use the next sibling.
  if (prevSibling) {
    aInsertion->mParentFrame =
        prevSibling->GetParent()->GetContentInsertionFrame();
  } else {
    // If there is no previous sibling, then find the frame that follows
    //
    // FIXME(emilio): This is really complex and probably shouldn't be.
    if (aEndSkipChild) {
      iter.Seek(aEndSkipChild);
      iter.GetPreviousChild();
    }
    if (nsIFrame* nextSibling = FindNextSibling(iter, childDisplay)) {
      aInsertion->mParentFrame =
          nextSibling->GetParent()->GetContentInsertionFrame();
    } else {
      // No previous or next sibling, so treat this like an appended frame.
      *aIsAppend = true;

      // Deal with fieldsets.
      aInsertion->mParentFrame =
          ::GetAdjustedParentFrame(aInsertion->mParentFrame, aChild);

      aInsertion->mParentFrame =
          ::ContinuationToAppendTo(aInsertion->mParentFrame);

      prevSibling = ::FindAppendPrevSibling(aInsertion->mParentFrame, nullptr);
    }
  }

  *aIsRangeInsertSafe = childDisplay.isNothing();
  return prevSibling;
}

nsContainerFrame* nsCSSFrameConstructor::GetContentInsertionFrameFor(
    nsIContent* aContent) {
  nsIFrame* frame;
  while (!(frame = aContent->GetPrimaryFrame())) {
    if (!IsDisplayContents(aContent)) {
      return nullptr;
    }

    aContent = aContent->GetFlattenedTreeParent();
    if (!aContent) {
      return nullptr;
    }
  }

  // If the content of the frame is not the desired content then this is not
  // really a frame for the desired content.
  // XXX This check is needed due to bug 135040. Remove it once that's fixed.
  if (frame->GetContent() != aContent) {
    return nullptr;
  }

  nsContainerFrame* insertionFrame = frame->GetContentInsertionFrame();

  NS_ASSERTION(!insertionFrame || insertionFrame == frame || !frame->IsLeaf(),
               "The insertion frame is the primary frame or the primary frame "
               "isn't a leaf");

  return insertionFrame;
}

static bool IsSpecialFramesetChild(nsIContent* aContent) {
  // IMPORTANT: This must match the conditions in nsHTMLFramesetFrame::Init.
  return aContent->IsAnyOfHTMLElements(nsGkAtoms::frameset, nsGkAtoms::frame);
}

static void InvalidateCanvasIfNeeded(PresShell* aPresShell, nsIContent* aNode);

void nsCSSFrameConstructor::AddTextItemIfNeeded(
    nsFrameConstructorState& aState, const ComputedStyle& aParentStyle,
    const InsertionPoint& aInsertion, nsIContent* aPossibleTextContent,
    FrameConstructionItemList& aItems) {
  MOZ_ASSERT(aPossibleTextContent, "Must have node");
  if (!aPossibleTextContent->IsText() ||
      !aPossibleTextContent->HasFlag(NS_CREATE_FRAME_IF_NON_WHITESPACE) ||
      aPossibleTextContent->HasFlag(NODE_NEEDS_FRAME)) {
    // Not text, or not suppressed due to being all-whitespace (if it were being
    // suppressed, it would have the NS_CREATE_FRAME_IF_NON_WHITESPACE flag), or
    // going to be reframed anyway.
    return;
  }
  MOZ_ASSERT(!aPossibleTextContent->GetPrimaryFrame(),
             "Text node has a frame and NS_CREATE_FRAME_IF_NON_WHITESPACE");
  AddFrameConstructionItems(aState, aPossibleTextContent, false, aParentStyle,
                            aInsertion, aItems);
}

void nsCSSFrameConstructor::ReframeTextIfNeeded(nsIContent* aContent) {
  if (!aContent->IsText() ||
      !aContent->HasFlag(NS_CREATE_FRAME_IF_NON_WHITESPACE) ||
      aContent->HasFlag(NODE_NEEDS_FRAME)) {
    // Not text, or not suppressed due to being all-whitespace (if it were being
    // suppressed, it would have the NS_CREATE_FRAME_IF_NON_WHITESPACE flag), or
    // going to be reframed anyway.
    return;
  }
  MOZ_ASSERT(!aContent->GetPrimaryFrame(),
             "Text node has a frame and NS_CREATE_FRAME_IF_NON_WHITESPACE");
  ContentInserted(aContent, InsertionKind::Async);
}

#ifdef DEBUG
void nsCSSFrameConstructor::CheckBitsForLazyFrameConstruction(
    nsIContent* aParent) {
  // If we hit a node with no primary frame, or the NODE_NEEDS_FRAME bit set
  // we want to assert, but leaf frames that process their own children and may
  // ignore anonymous children (eg framesets) make this complicated. So we set
  // these two booleans if we encounter these situations and unset them if we
  // hit a node with a leaf frame.
  //
  // It's fine if one of node without primary frame is in a display:none
  // subtree.
  //
  // Also, it's fine if one of the nodes without primary frame is a display:
  // contents node.
  bool noPrimaryFrame = false;
  bool needsFrameBitSet = false;
  nsIContent* content = aParent;
  while (content && !content->HasFlag(NODE_DESCENDANTS_NEED_FRAMES)) {
    if (content->GetPrimaryFrame() && content->GetPrimaryFrame()->IsLeaf()) {
      noPrimaryFrame = needsFrameBitSet = false;
    }
    if (!noPrimaryFrame && !content->GetPrimaryFrame()) {
      noPrimaryFrame = !IsDisplayContents(content);
    }
    if (!needsFrameBitSet && content->HasFlag(NODE_NEEDS_FRAME)) {
      needsFrameBitSet = true;
    }

    content = content->GetFlattenedTreeParent();
  }
  if (content && content->GetPrimaryFrame() &&
      content->GetPrimaryFrame()->IsLeaf()) {
    noPrimaryFrame = needsFrameBitSet = false;
  }
  MOZ_ASSERT(!noPrimaryFrame,
             "Ancestors of nodes with frames to be "
             "constructed lazily should have frames");
  MOZ_ASSERT(!needsFrameBitSet,
             "Ancestors of nodes with frames to be "
             "constructed lazily should not have NEEDS_FRAME bit set");
}
#endif

// Returns true if this operation can be lazy, false if not.
//
// FIXME(emilio, bug 1410020): This function assumes that the flattened tree
// parent of all the appended children is the same, which, afaict, is not
// necessarily true.
void nsCSSFrameConstructor::ConstructLazily(Operation aOperation,
                                            nsIContent* aChild) {
  MOZ_ASSERT(aChild->GetParent());

  // We can construct lazily; just need to set suitable bits in the content
  // tree.
  Element* parent = aChild->GetFlattenedTreeParentElement();
  if (!parent) {
    // Not part of the flat tree, nothing to do.
    return;
  }

  if (Servo_Element_IsDisplayNone(parent)) {
    // Nothing to do either.
    //
    // FIXME(emilio): This should be an assert, except for weird <frameset>
    // stuff that does its own frame construction. Such an assert would fire in
    // layout/style/crashtests/1411478.html, for example.
    return;
  }

  // Set NODE_NEEDS_FRAME on the new nodes.
  if (aOperation == CONTENTINSERT) {
    NS_ASSERTION(!aChild->GetPrimaryFrame() ||
                     aChild->GetPrimaryFrame()->GetContent() != aChild,
                 // XXX the aChild->GetPrimaryFrame()->GetContent() != aChild
                 // check is needed due to bug 135040. Remove it once that's
                 // fixed.
                 "setting NEEDS_FRAME on a node that already has a frame?");
    aChild->SetFlags(NODE_NEEDS_FRAME);
  } else {  // CONTENTAPPEND
    for (nsIContent* child = aChild; child; child = child->GetNextSibling()) {
      NS_ASSERTION(!child->GetPrimaryFrame() ||
                       child->GetPrimaryFrame()->GetContent() != child,
                   // XXX the child->GetPrimaryFrame()->GetContent() != child
                   // check is needed due to bug 135040. Remove it once that's
                   // fixed.
                   "setting NEEDS_FRAME on a node that already has a frame?");
      child->SetFlags(NODE_NEEDS_FRAME);
    }
  }

  CheckBitsForLazyFrameConstruction(parent);
  parent->NoteDescendantsNeedFramesForServo();
}

void nsCSSFrameConstructor::IssueSingleInsertNofications(
    nsIContent* aStartChild, nsIContent* aEndChild,
    InsertionKind aInsertionKind) {
  for (nsIContent* child = aStartChild; child != aEndChild;
       child = child->GetNextSibling()) {
    // XXX the GetContent() != child check is needed due to bug 135040.
    // Remove it once that's fixed.
    MOZ_ASSERT(!child->GetPrimaryFrame() ||
               child->GetPrimaryFrame()->GetContent() != child);

    // Call ContentRangeInserted with this node.
    ContentRangeInserted(child, child->GetNextSibling(), aInsertionKind);
  }
}

bool nsCSSFrameConstructor::InsertionPoint::IsMultiple() const {
  // Fieldset frames have multiple normal flow child frame lists so handle it
  // the same as if it had multiple content insertion points.
  return mParentFrame && mParentFrame->IsFieldSetFrame();
}

nsCSSFrameConstructor::InsertionPoint
nsCSSFrameConstructor::GetRangeInsertionPoint(nsIContent* aStartChild,
                                              nsIContent* aEndChild,
                                              InsertionKind aInsertionKind) {
  MOZ_ASSERT(aStartChild);

  nsIContent* parent = aStartChild->GetParent();
  if (!parent) {
    IssueSingleInsertNofications(aStartChild, aEndChild, aInsertionKind);
    return {};
  }

  // If the children of the container may be distributed to different insertion
  // points, insert them separately and bail out, letting ContentInserted handle
  // the mess.
  if (parent->GetShadowRoot()) {
    IssueSingleInsertNofications(aStartChild, aEndChild, aInsertionKind);
    return {};
  }

#ifdef DEBUG
  {
    nsIContent* expectedParent = aStartChild->GetFlattenedTreeParent();
    for (nsIContent* child = aStartChild->GetNextSibling(); child;
         child = child->GetNextSibling()) {
      MOZ_ASSERT(child->GetFlattenedTreeParent() == expectedParent);
    }
  }
#endif

  // Now the flattened tree parent of all the siblings is the same, just use the
  // same insertion point and take the fast path, unless it's a multiple
  // insertion point.
  InsertionPoint ip = GetInsertionPoint(aStartChild);
  if (ip.IsMultiple()) {
    IssueSingleInsertNofications(aStartChild, aEndChild, aInsertionKind);
    return {};
  }

  return ip;
}

bool nsCSSFrameConstructor::MaybeRecreateForFrameset(nsIFrame* aParentFrame,
                                                     nsIContent* aStartChild,
                                                     nsIContent* aEndChild) {
  if (aParentFrame->IsFrameSetFrame()) {
    // Check whether we have any kids we care about.
    for (nsIContent* cur = aStartChild; cur != aEndChild;
         cur = cur->GetNextSibling()) {
      if (IsSpecialFramesetChild(cur)) {
        // Just reframe the parent, since framesets are weird like that.
        RecreateFramesForContent(aParentFrame->GetContent(),
                                 InsertionKind::Async);
        return true;
      }
    }
  }
  return false;
}

void nsCSSFrameConstructor::LazilyStyleNewChildRange(nsIContent* aStartChild,
                                                     nsIContent* aEndChild) {
  for (nsIContent* child = aStartChild; child != aEndChild;
       child = child->GetNextSibling()) {
    if (child->IsElement()) {
      child->AsElement()->NoteDirtyForServo();
    }
  }
}

#ifdef DEBUG
static bool IsFlattenedTreeChild(nsIContent* aParent, nsIContent* aChild) {
  FlattenedChildIterator iter(aParent);
  for (nsIContent* node = iter.GetNextChild(); node;
       node = iter.GetNextChild()) {
    if (node == aChild) {
      return true;
    }
  }
  return false;
}
#endif

void nsCSSFrameConstructor::StyleNewChildRange(nsIContent* aStartChild,
                                               nsIContent* aEndChild) {
  ServoStyleSet* styleSet = mPresShell->StyleSet();

  for (nsIContent* child = aStartChild; child != aEndChild;
       child = child->GetNextSibling()) {
    if (!child->IsElement()) {
      continue;
    }

    Element* childElement = child->AsElement();

    // We only come in here from non-lazy frame construction, so the children
    // should be unstyled.
    MOZ_ASSERT(!childElement->HasServoData());

#ifdef DEBUG
    {
      // Furthermore, all of them should have the same flattened tree parent
      // (GetRangeInsertionPoint ensures it). And that parent should be styled,
      // otherwise we would've never found an insertion point at all.
      Element* parent = childElement->GetFlattenedTreeParentElement();
      MOZ_ASSERT(parent);
      MOZ_ASSERT(parent->HasServoData());
      MOZ_ASSERT(
          IsFlattenedTreeChild(parent, child),
          "GetFlattenedTreeParent and ChildIterator don't agree, fix this!");
    }
#endif

    styleSet->StyleNewSubtree(childElement);
  }
}

nsIFrame* nsCSSFrameConstructor::FindNextSiblingForAppend(
    const InsertionPoint& aInsertion) {
  auto SlowPath = [&]() -> nsIFrame* {
    FlattenedChildIterator iter(aInsertion.mContainer,
                                /* aStartAtBeginning = */ false);
    iter.GetPreviousChild();  // Prime the iterator.
    Maybe<StyleDisplay> unused;
    return FindNextSibling(iter, unused);
  };

  if (!IsDisplayContents(aInsertion.mContainer) &&
      !nsLayoutUtils::GetAfterFrame(aInsertion.mContainer)) {
    MOZ_ASSERT(!SlowPath());
    return nullptr;
  }

  return SlowPath();
}

void nsCSSFrameConstructor::ContentAppended(nsIContent* aFirstNewContent,
                                            InsertionKind aInsertionKind) {
  MOZ_ASSERT(aInsertionKind == InsertionKind::Sync ||
             !RestyleManager()->IsInStyleRefresh());

  AUTO_PROFILER_LABEL_HOT("nsCSSFrameConstructor::ContentAppended",
                          LAYOUT_FrameConstruction);
  AUTO_LAYOUT_PHASE_ENTRY_POINT(mPresShell->GetPresContext(), FrameC);

#ifdef DEBUG
  if (gNoisyContentUpdates) {
    printf(
        "nsCSSFrameConstructor::ContentAppended container=%p "
        "first-child=%p lazy=%d\n",
        aFirstNewContent->GetParent(), aFirstNewContent,
        aInsertionKind == InsertionKind::Async);
    if (gReallyNoisyContentUpdates && aFirstNewContent->GetParent()) {
      aFirstNewContent->GetParent()->List(stdout, 0);
    }
  }

  for (nsIContent* child = aFirstNewContent; child;
       child = child->GetNextSibling()) {
    // XXX the GetContent() != child check is needed due to bug 135040.
    // Remove it once that's fixed.
    MOZ_ASSERT(
        !child->GetPrimaryFrame() ||
            child->GetPrimaryFrame()->GetContent() != child,
        "asked to construct a frame for a node that already has a frame");
  }
#endif

  LAYOUT_PHASE_TEMP_EXIT();
  InsertionPoint insertion =
      GetRangeInsertionPoint(aFirstNewContent, nullptr, aInsertionKind);
  nsContainerFrame*& parentFrame = insertion.mParentFrame;
  LAYOUT_PHASE_TEMP_REENTER();
  if (!parentFrame) {
    // We're punting on frame construction because there's no container frame.
    // The Servo-backed style system handles this case like the lazy frame
    // construction case, except when we're already constructing frames, in
    // which case we shouldn't need to do anything else.
    if (aInsertionKind == InsertionKind::Async) {
      LazilyStyleNewChildRange(aFirstNewContent, nullptr);
    }
    return;
  }

  if (aInsertionKind == InsertionKind::Async) {
    ConstructLazily(CONTENTAPPEND, aFirstNewContent);
    LazilyStyleNewChildRange(aFirstNewContent, nullptr);
    return;
  }

  LAYOUT_PHASE_TEMP_EXIT();
  if (MaybeRecreateForFrameset(parentFrame, aFirstNewContent, nullptr)) {
    LAYOUT_PHASE_TEMP_REENTER();
    return;
  }
  LAYOUT_PHASE_TEMP_REENTER();

  if (parentFrame->IsLeaf()) {
    // Nothing to do here; we shouldn't be constructing kids of leaves
    // Clear lazy bits so we don't try to construct again.
    ClearLazyBits(aFirstNewContent, nullptr);
    return;
  }

  LAYOUT_PHASE_TEMP_EXIT();
  if (WipeInsertionParent(parentFrame)) {
    LAYOUT_PHASE_TEMP_REENTER();
    return;
  }
  LAYOUT_PHASE_TEMP_REENTER();

#ifdef DEBUG
  if (gNoisyContentUpdates && IsFramePartOfIBSplit(parentFrame)) {
    printf("nsCSSFrameConstructor::ContentAppended: parentFrame=");
    parentFrame->ListTag(stdout);
    printf(" is ib-split\n");
  }
#endif

  // We should never get here with fieldsets, since they have
  // multiple insertion points.
  MOZ_ASSERT(!parentFrame->IsFieldSetFrame(),
             "Parent frame should not be fieldset!");

  nsIFrame* nextSibling = FindNextSiblingForAppend(insertion);
  if (nextSibling) {
    parentFrame = nextSibling->GetParent()->GetContentInsertionFrame();
  } else {
    parentFrame = ::ContinuationToAppendTo(parentFrame);
  }

  nsContainerFrame* containingBlock = GetFloatContainingBlock(parentFrame);

  // See if the containing block has :first-letter style applied.
  const bool haveFirstLetterStyle =
      containingBlock && HasFirstLetterStyle(containingBlock);

  const bool haveFirstLineStyle =
      containingBlock && ShouldHaveFirstLineStyle(containingBlock->GetContent(),
                                                  containingBlock->Style());

  if (haveFirstLetterStyle) {
    AutoWeakFrame wf(nextSibling);

    // Before we get going, remove the current letter frames
    RemoveLetterFrames(mPresShell, containingBlock);

    // Reget nextSibling, since we may have killed it.
    //
    // FIXME(emilio): This kinda sucks! :(
    if (nextSibling && !wf) {
      nextSibling = FindNextSiblingForAppend(insertion);
      if (nextSibling) {
        parentFrame = nextSibling->GetParent()->GetContentInsertionFrame();
        containingBlock = GetFloatContainingBlock(parentFrame);
      }
    }
  }

  // Create some new frames
  nsFrameConstructorState state(
      mPresShell, GetAbsoluteContainingBlock(parentFrame, FIXED_POS),
      GetAbsoluteContainingBlock(parentFrame, ABS_POS), containingBlock);

  if (mPresShell->GetPresContext()->IsPaginated()) {
    // Because this function can be called outside frame construction, we need
    // to set state.mAutoPageNameValue based on what the parent frame's auto
    // value is.
    // Calling this from outside the frame constructor can violate many of the
    // expectations in AutoFrameConstructionPageName, and unlike during frame
    // construction we already have an auto value from parentFrame, so we do
    // not use AutoFrameConstructionPageName here.
    state.mAutoPageNameValue = parentFrame->GetAutoPageValue();
#ifdef DEBUG
    parentFrame->mWasVisitedByAutoFrameConstructionPageName = true;
#endif
  }

  LayoutFrameType frameType = parentFrame->Type();

  RefPtr<ComputedStyle> parentStyle =
      ResolveComputedStyle(insertion.mContainer);
  FlattenedChildIterator iter(insertion.mContainer);
  const bool haveNoShadowDOM =
      !iter.ShadowDOMInvolved() || !iter.GetNextChild();

  AutoFrameConstructionItemList items(this);
  if (aFirstNewContent->GetPreviousSibling() &&
      GetParentType(frameType) == eTypeBlock && haveNoShadowDOM) {
    // If there's a text node in the normal content list just before the new
    // items, and it has no frame, make a frame construction item for it. If it
    // doesn't need a frame, ConstructFramesFromItemList below won't give it
    // one.  No need to do all this if our parent type is not block, though,
    // since WipeContainingBlock already handles that situation.
    //
    // Because we're appending, we don't need to worry about any text
    // after the appended content; there can only be generated content
    // (and bare text nodes are not generated). Native anonymous content
    // generated by frames never participates in inline layout.
    AddTextItemIfNeeded(state, *parentStyle, insertion,
                        aFirstNewContent->GetPreviousSibling(), items);
  }
  for (nsIContent* child = aFirstNewContent; child;
       child = child->GetNextSibling()) {
    AddFrameConstructionItems(state, child, false, *parentStyle, insertion,
                              items);
  }

  nsIFrame* prevSibling = ::FindAppendPrevSibling(parentFrame, nextSibling);

  // Perform special check for diddling around with the frames in
  // a ib-split inline frame.
  // If we're appending before :after content, then we're not really
  // appending, so let WipeContainingBlock know that.
  LAYOUT_PHASE_TEMP_EXIT();
  if (WipeContainingBlock(state, containingBlock, parentFrame, items, true,
                          prevSibling)) {
    LAYOUT_PHASE_TEMP_REENTER();
    return;
  }
  LAYOUT_PHASE_TEMP_REENTER();

  // If the parent is a block frame, and we're not in a special case
  // where frames can be moved around, determine if the list is for the
  // start or end of the block.
  if (parentFrame->IsBlockFrameOrSubclass() && !haveFirstLetterStyle &&
      !haveFirstLineStyle && !IsFramePartOfIBSplit(parentFrame)) {
    items.SetLineBoundaryAtStart(!prevSibling ||
                                 !prevSibling->IsInlineOutside() ||
                                 prevSibling->IsBrFrame());
    // :after content can't be <br> so no need to check it
    //
    // FIXME(emilio): A display: contents sibling could! Write a test-case and
    // fix.
    items.SetLineBoundaryAtEnd(!nextSibling || !nextSibling->IsInlineOutside());
  }
  // To suppress whitespace-only text frames, we have to verify that
  // our container's DOM child list matches its flattened tree child list.
  items.SetParentHasNoShadowDOM(haveNoShadowDOM);

  nsFrameConstructorSaveState floatSaveState;
  state.MaybePushFloatContainingBlock(parentFrame, floatSaveState);

  nsFrameList frameList;
  ConstructFramesFromItemList(state, items, parentFrame,
                              ParentIsWrapperAnonBox(parentFrame), frameList);

  for (nsIContent* child = aFirstNewContent; child;
       child = child->GetNextSibling()) {
    // Invalidate now instead of before the WipeContainingBlock call, just in
    // case we do wipe; in that case we don't need to do this walk at all.
    // XXXbz does that matter?  Would it make more sense to save some virtual
    // GetChildAt_Deprecated calls instead and do this during construction of
    // our FrameConstructionItemList?
    InvalidateCanvasIfNeeded(mPresShell, child);
  }

  // If the container is a table and a caption was appended, it needs to be put
  // in the table wrapper frame's additional child list.
  nsFrameList captionList;
  if (LayoutFrameType::Table == frameType) {
    // Pull out the captions.  Note that we don't want to do that as we go,
    // because processing a single caption can add a whole bunch of things to
    // the frame items due to pseudoframe processing.  So we'd have to pull
    // captions from a list anyway; might as well do that here.
    // XXXbz this is no longer true; we could pull captions directly out of the
    // FrameConstructionItemList now.
    PullOutCaptionFrames(frameList, captionList);
  }

  if (haveFirstLineStyle && parentFrame == containingBlock) {
    // It's possible that some of the new frames go into a
    // first-line frame. Look at them and see...
    AppendFirstLineFrames(state, containingBlock->GetContent(), containingBlock,
                          frameList);
    // That moved things into line frames as needed, reparenting their
    // styles.  Nothing else needs to be done.
  } else if (parentFrame->Style()->HasPseudoElementData()) {
    // parentFrame might be inside a ::first-line frame.  Check whether it is,
    // and if so fix up our styles.
    CheckForFirstLineInsertion(parentFrame, frameList);
    CheckForFirstLineInsertion(parentFrame, captionList);
  }

  // Notify the parent frame passing it the list of new frames
  // Append the flowed frames to the principal child list; captions
  // need special treatment
  if (captionList.NotEmpty()) {  // append the caption to the table wrapper
    NS_ASSERTION(LayoutFrameType::Table == frameType, "how did that happen?");
    nsContainerFrame* outerTable = parentFrame->GetParent();
    captionList.ApplySetParent(outerTable);
    AppendFrames(outerTable, FrameChildListID::Caption, std::move(captionList));
  }

  LAYOUT_PHASE_TEMP_EXIT();
  if (MaybeRecreateForColumnSpan(state, parentFrame, frameList, prevSibling)) {
    LAYOUT_PHASE_TEMP_REENTER();
    return;
  }
  LAYOUT_PHASE_TEMP_REENTER();

  if (frameList.NotEmpty()) {  // append the in-flow kids
    AppendFramesToParent(state, parentFrame, frameList, prevSibling);
  }

  // Recover first-letter frames
  if (haveFirstLetterStyle) {
    RecoverLetterFrames(containingBlock);
  }

#ifdef DEBUG
  if (gReallyNoisyContentUpdates) {
    printf("nsCSSFrameConstructor::ContentAppended: resulting frame model:\n");
    parentFrame->List(stdout);
  }
#endif

#ifdef ACCESSIBILITY
  if (nsAccessibilityService* accService = GetAccService()) {
    accService->ContentRangeInserted(mPresShell, aFirstNewContent, nullptr);
  }
#endif
}

void nsCSSFrameConstructor::ContentInserted(nsIContent* aChild,
                                            InsertionKind aInsertionKind) {
  ContentRangeInserted(aChild, aChild->GetNextSibling(), aInsertionKind);
}

// ContentRangeInserted handles creating frames for a range of nodes that
// aren't at the end of their childlist. ContentRangeInserted isn't a real
// content notification, but rather it handles regular ContentInserted calls
// for a single node as well as the lazy construction of frames for a range of
// nodes when called from CreateNeededFrames. For a range of nodes to be
// suitable to have its frames constructed all at once they must meet the same
// conditions that ContentAppended imposes (GetRangeInsertionPoint checks
// these), plus more. Namely when finding the insertion prevsibling we must not
// need to consult something specific to any one node in the range, so that the
// insertion prevsibling would be the same for each node in the range. So we
// pass the first node in the range to GetInsertionPrevSibling, and if
// IsValidSibling (the only place GetInsertionPrevSibling might look at the
// passed in node itself) needs to resolve style on the node we record this and
// return that this range needs to be split up and inserted separately. Table
// captions need extra attention as we need to determine where to insert them
// in the caption list, while skipping any nodes in the range being inserted
// (because when we treat the caption frames the other nodes have had their
// frames constructed but not yet inserted into the frame tree).
void nsCSSFrameConstructor::ContentRangeInserted(nsIContent* aStartChild,
                                                 nsIContent* aEndChild,
                                                 InsertionKind aInsertionKind) {
  MOZ_ASSERT(aInsertionKind == InsertionKind::Sync ||
             !RestyleManager()->IsInStyleRefresh());

  AUTO_PROFILER_LABEL_HOT("nsCSSFrameConstructor::ContentRangeInserted",
                          LAYOUT_FrameConstruction);
  AUTO_LAYOUT_PHASE_ENTRY_POINT(mPresShell->GetPresContext(), FrameC);

  MOZ_ASSERT(aStartChild, "must always pass a child");

#ifdef DEBUG
  if (gNoisyContentUpdates) {
    printf(
        "nsCSSFrameConstructor::ContentRangeInserted container=%p "
        "start-child=%p end-child=%p lazy=%d\n",
        aStartChild->GetParent(), aStartChild, aEndChild,
        aInsertionKind == InsertionKind::Async);
    if (gReallyNoisyContentUpdates) {
      if (aStartChild->GetParent()) {
        aStartChild->GetParent()->List(stdout, 0);
      } else {
        aStartChild->List(stdout, 0);
      }
    }
  }

  for (nsIContent* child = aStartChild; child != aEndChild;
       child = child->GetNextSibling()) {
    // XXX the GetContent() != child check is needed due to bug 135040.
    // Remove it once that's fixed.
    NS_ASSERTION(
        !child->GetPrimaryFrame() ||
            child->GetPrimaryFrame()->GetContent() != child,
        "asked to construct a frame for a node that already has a frame");
  }
#endif

  bool isSingleInsert = (aStartChild->GetNextSibling() == aEndChild);
  NS_ASSERTION(isSingleInsert || aInsertionKind == InsertionKind::Sync,
               "range insert shouldn't be lazy");
  NS_ASSERTION(isSingleInsert || aEndChild,
               "range should not include all nodes after aStartChild");

  // If we have a null parent, then this must be the document element being
  // inserted, or some other child of the document in the DOM (might be a PI,
  // say).
  if (!aStartChild->GetParent()) {
    MOZ_ASSERT(isSingleInsert,
               "root node insertion should be a single insertion");
    Element* docElement = mDocument->GetRootElement();
    if (aStartChild != docElement) {
      // Not the root element; just bail out
      return;
    }

    MOZ_ASSERT(!mRootElementFrame, "root element frame already created");
    if (aInsertionKind == InsertionKind::Async) {
      docElement->SetFlags(NODE_NEEDS_FRAME);
      LazilyStyleNewChildRange(docElement, nullptr);
      return;
    }

    // Create frames for the document element and its child elements
    if (ConstructDocElementFrame(docElement)) {
      InvalidateCanvasIfNeeded(mPresShell, aStartChild);
#ifdef DEBUG
      if (gReallyNoisyContentUpdates) {
        printf(
            "nsCSSFrameConstructor::ContentRangeInserted: resulting frame "
            "model:\n");
        mRootElementFrame->List(stdout);
      }
#endif
    }

#ifdef ACCESSIBILITY
    if (nsAccessibilityService* accService = GetAccService()) {
      accService->ContentRangeInserted(mPresShell, aStartChild, aEndChild);
    }
#endif

    return;
  }

  InsertionPoint insertion;
  if (isSingleInsert) {
    // See if we have a Shadow DOM insertion point. If so, then that's our real
    // parent frame; if not, then the frame hasn't been built yet and we just
    // bail.
    insertion = GetInsertionPoint(aStartChild);
  } else {
    // Get our insertion point. If we need to issue single ContentInserteds
    // GetRangeInsertionPoint will take care of that for us.
    LAYOUT_PHASE_TEMP_EXIT();
    insertion = GetRangeInsertionPoint(aStartChild, aEndChild, aInsertionKind);
    LAYOUT_PHASE_TEMP_REENTER();
  }

  if (!insertion.mParentFrame) {
    // We're punting on frame construction because there's no container frame.
    // The Servo-backed style system handles this case like the lazy frame
    // construction case, except when we're already constructing frames, in
    // which case we shouldn't need to do anything else.
    if (aInsertionKind == InsertionKind::Async) {
      LazilyStyleNewChildRange(aStartChild, aEndChild);
    }
    return;
  }

  if (aInsertionKind == InsertionKind::Async) {
    ConstructLazily(CONTENTINSERT, aStartChild);
    LazilyStyleNewChildRange(aStartChild, aEndChild);
    return;
  }

  bool isAppend, isRangeInsertSafe;
  nsIFrame* prevSibling = GetInsertionPrevSibling(
      &insertion, aStartChild, &isAppend, &isRangeInsertSafe);

  // check if range insert is safe
  if (!isSingleInsert && !isRangeInsertSafe) {
    // must fall back to a single ContertInserted for each child in the range
    LAYOUT_PHASE_TEMP_EXIT();
    IssueSingleInsertNofications(aStartChild, aEndChild, InsertionKind::Sync);
    LAYOUT_PHASE_TEMP_REENTER();
    return;
  }

  LayoutFrameType frameType = insertion.mParentFrame->Type();
  LAYOUT_PHASE_TEMP_EXIT();
  if (MaybeRecreateForFrameset(insertion.mParentFrame, aStartChild,
                               aEndChild)) {
    LAYOUT_PHASE_TEMP_REENTER();
    return;
  }
  LAYOUT_PHASE_TEMP_REENTER();

  // We should only get here with fieldsets when doing a single insert, because
  // fieldsets have multiple insertion points.
  NS_ASSERTION(isSingleInsert || frameType != LayoutFrameType::FieldSet,
               "Unexpected parent");
  // Note that this check is insufficient if aStartChild is not a legend with
  // display::contents that contains a legend.  We'll catch that case in
  // WipeContainingBlock. (That code would also catch this case, but handling
  // this early is slightly faster.)
  // XXXmats we should be able to optimize this when the fieldset doesn't
  // currently have a rendered legend.  ContentRangeInserted needs to be fixed
  // to use the inner frame as the content insertion frame in that case.
  if (GetFieldSetFrameFor(insertion.mParentFrame) &&
      aStartChild->NodeInfo()->NameAtom() == nsGkAtoms::legend) {
    // Just reframe the parent, since figuring out whether this
    // should be the new legend and then handling it is too complex.
    // We could do a little better here --- check if the fieldset already
    // has a legend which occurs earlier in its child list than this node,
    // and if so, proceed. But we'd have to extend nsFieldSetFrame
    // to locate this legend in the inserted frames and extract it.
    LAYOUT_PHASE_TEMP_EXIT();
    RecreateFramesForContent(insertion.mParentFrame->GetContent(),
                             InsertionKind::Async);
    LAYOUT_PHASE_TEMP_REENTER();
    return;
  }

  // Don't construct kids of leaves
  if (insertion.mParentFrame->IsLeaf()) {
    // Clear lazy bits so we don't try to construct again.
    ClearLazyBits(aStartChild, aEndChild);
    return;
  }

  LAYOUT_PHASE_TEMP_EXIT();
  if (WipeInsertionParent(insertion.mParentFrame)) {
    LAYOUT_PHASE_TEMP_REENTER();
    return;
  }
  LAYOUT_PHASE_TEMP_REENTER();

  nsFrameConstructorState state(
      mPresShell, GetAbsoluteContainingBlock(insertion.mParentFrame, FIXED_POS),
      GetAbsoluteContainingBlock(insertion.mParentFrame, ABS_POS),
      GetFloatContainingBlock(insertion.mParentFrame),
      do_AddRef(mFrameTreeState));

  // Recover state for the containing block - we need to know if
  // it has :first-letter or :first-line style applied to it. The
  // reason we care is that the internal structure in these cases
  // is not the normal structure and requires custom updating
  // logic.
  nsContainerFrame* containingBlock = state.mFloatedList.mContainingBlock;
  bool haveFirstLetterStyle = false;
  bool haveFirstLineStyle = false;

  // In order to shave off some cycles, we only dig up the
  // containing block haveFirst* flags if the parent frame where
  // the insertion/append is occurring is an inline or block
  // container. For other types of containers this isn't relevant.
  StyleDisplayInside parentDisplayInside =
      insertion.mParentFrame->StyleDisplay()->DisplayInside();

  // Examine the insertion.mParentFrame where the insertion is taking
  // place. If it's a certain kind of container then some special
  // processing is done.
  if (StyleDisplayInside::Flow == parentDisplayInside) {
    // Recover the special style flags for the containing block
    if (containingBlock) {
      haveFirstLetterStyle = HasFirstLetterStyle(containingBlock);
      haveFirstLineStyle = ShouldHaveFirstLineStyle(
          containingBlock->GetContent(), containingBlock->Style());
    }

    if (haveFirstLetterStyle) {
      // If our current insertion.mParentFrame is a Letter frame, use its parent
      // as our new parent hint
      if (insertion.mParentFrame->IsLetterFrame()) {
        // If insertion.mParentFrame is out of flow, then we actually want the
        // parent of the placeholder frame.
        if (insertion.mParentFrame->HasAnyStateBits(NS_FRAME_OUT_OF_FLOW)) {
          nsPlaceholderFrame* placeholderFrame =
              insertion.mParentFrame->GetPlaceholderFrame();
          NS_ASSERTION(placeholderFrame, "No placeholder for out-of-flow?");
          insertion.mParentFrame = placeholderFrame->GetParent();
        } else {
          insertion.mParentFrame = insertion.mParentFrame->GetParent();
        }
      }

      // Remove the old letter frames before doing the insertion
      RemoveLetterFrames(mPresShell, state.mFloatedList.mContainingBlock);

      // Removing the letterframes messes around with the frame tree, removing
      // and creating frames.  We need to reget our prevsibling, parent frame,
      // etc.
      prevSibling = GetInsertionPrevSibling(&insertion, aStartChild, &isAppend,
                                            &isRangeInsertSafe);

      // Need check whether a range insert is still safe.
      if (!isSingleInsert && !isRangeInsertSafe) {
        // Need to recover the letter frames first.
        RecoverLetterFrames(state.mFloatedList.mContainingBlock);

        // must fall back to a single ContertInserted for each child in the
        // range
        LAYOUT_PHASE_TEMP_EXIT();
        IssueSingleInsertNofications(aStartChild, aEndChild,
                                     InsertionKind::Sync);
        LAYOUT_PHASE_TEMP_REENTER();
        return;
      }

      frameType = insertion.mParentFrame->Type();
    }
  }

  // This handles fallback to 'list-style-type' when a 'list-style-image' fails
  // to load.
  if (aStartChild->IsInNativeAnonymousSubtree() &&
      aStartChild->IsHTMLElement(nsGkAtoms::mozgeneratedcontentimage)) {
    MOZ_ASSERT(isSingleInsert);
    MOZ_ASSERT(insertion.mParentFrame->Style()->GetPseudoType() ==
                   PseudoStyleType::marker,
               "we can only handle ::marker fallback for now");
    nsIContent* const nextSibling = aStartChild->GetNextSibling();
    MOZ_ASSERT(nextSibling && nextSibling->IsText(),
               "expected a text node after the list-style-image image");
    DestroyContext context(mPresShell);
    RemoveFrame(context, FrameChildListID::Principal,
                nextSibling->GetPrimaryFrame());
    auto* const container = aStartChild->GetParent()->AsElement();
    nsIContent* firstNewChild = nullptr;
    auto InsertChild = [this, container, nextSibling,
                        &firstNewChild](RefPtr<nsIContent>&& aChild) {
      // We don't strictly have to set NODE_IS_IN_NATIVE_ANONYMOUS_SUBTREE
      // here; it would get set under AppendChildTo.  But AppendChildTo might
      // think that we're going from not being anonymous to being anonymous and
      // do some extra work; setting the flag here avoids that.
      aChild->SetFlags(NODE_IS_IN_NATIVE_ANONYMOUS_SUBTREE);
      container->InsertChildBefore(aChild, nextSibling, false, IgnoreErrors());
      if (auto* childElement = Element::FromNode(aChild)) {
        // If we created any children elements, Servo needs to traverse them,
        // but the root is already set up.
        mPresShell->StyleSet()->StyleNewSubtree(childElement);
      }
      if (!firstNewChild) {
        firstNewChild = aChild;
      }
    };
    CreateGeneratedContentFromListStyleType(
        state, *insertion.mContainer->AsElement(),
        *insertion.mParentFrame->Style(), InsertChild);
    if (!firstNewChild) {
      // No fallback content - we're done.
      return;
    }
    aStartChild = firstNewChild;
    MOZ_ASSERT(firstNewChild->GetNextSibling() == nextSibling,
               "list-style-type should only create one child");
  }

  AutoFrameConstructionItemList items(this);
  RefPtr<ComputedStyle> parentStyle =
      ResolveComputedStyle(insertion.mContainer);
  ParentType parentType = GetParentType(frameType);
  FlattenedChildIterator iter(insertion.mContainer);
  const bool haveNoShadowDOM =
      !iter.ShadowDOMInvolved() || !iter.GetNextChild();
  if (aStartChild->GetPreviousSibling() && parentType == eTypeBlock &&
      haveNoShadowDOM) {
    // If there's a text node in the normal content list just before the
    // new nodes, and it has no frame, make a frame construction item for
    // it, because it might need a frame now.  No need to do this if our
    // parent type is not block, though, since WipeContainingBlock
    // already handles that situation.
    AddTextItemIfNeeded(state, *parentStyle, insertion,
                        aStartChild->GetPreviousSibling(), items);
  }

  if (isSingleInsert) {
    AddFrameConstructionItems(state, aStartChild,
                              aStartChild->IsRootOfNativeAnonymousSubtree(),
                              *parentStyle, insertion, items);
  } else {
    for (nsIContent* child = aStartChild; child != aEndChild;
         child = child->GetNextSibling()) {
      AddFrameConstructionItems(state, child, false, *parentStyle, insertion,
                                items);
    }
  }

  if (aEndChild && parentType == eTypeBlock && haveNoShadowDOM) {
    // If there's a text node in the normal content list just after the
    // new nodes, and it has no frame, make a frame construction item for
    // it, because it might need a frame now.  No need to do this if our
    // parent type is not block, though, since WipeContainingBlock
    // already handles that situation.
    AddTextItemIfNeeded(state, *parentStyle, insertion, aEndChild, items);
  }

  // Perform special check for diddling around with the frames in
  // a special inline frame.
  // If we're appending before :after content, then we're not really
  // appending, so let WipeContainingBlock know that.
  LAYOUT_PHASE_TEMP_EXIT();
  if (WipeContainingBlock(state, containingBlock, insertion.mParentFrame, items,
                          isAppend, prevSibling)) {
    LAYOUT_PHASE_TEMP_REENTER();
    return;
  }
  LAYOUT_PHASE_TEMP_REENTER();

  nsFrameConstructorSaveState floatSaveState;
  state.MaybePushFloatContainingBlock(insertion.mParentFrame, floatSaveState);

  if (state.mPresContext->IsPaginated()) {
    // Because this function can be called outside frame construction, we need
    // to set state.mAutoPageNameValue based on what the parent frame's auto
    // value is.
    // Calling this from outside the frame constructor can violate many of the
    // expectations in AutoFrameConstructionPageName, and unlike during frame
    // construction we already have an auto value from parentFrame, so we do
    // not use AutoFrameConstructionPageName here.
    state.mAutoPageNameValue = insertion.mParentFrame->GetAutoPageValue();
#ifdef DEBUG
    insertion.mParentFrame->mWasVisitedByAutoFrameConstructionPageName = true;
#endif
  }

  // If the container is a table and a caption will be appended, it needs to be
  // put in the table wrapper frame's additional child list.
  // We make no attempt here to set flags to indicate whether the list
  // will be at the start or end of a block. It doesn't seem worthwhile.
  nsFrameList frameList, captionList;
  ConstructFramesFromItemList(state, items, insertion.mParentFrame,
                              ParentIsWrapperAnonBox(insertion.mParentFrame),
                              frameList);

  if (frameList.NotEmpty()) {
    for (nsIContent* child = aStartChild; child != aEndChild;
         child = child->GetNextSibling()) {
      InvalidateCanvasIfNeeded(mPresShell, child);
    }

    if (LayoutFrameType::Table == frameType ||
        LayoutFrameType::TableWrapper == frameType) {
      PullOutCaptionFrames(frameList, captionList);
    }
  }

  if (haveFirstLineStyle && insertion.mParentFrame == containingBlock &&
      isAppend) {
    // It's possible that the new frame goes into a first-line
    // frame. Look at it and see...
    AppendFirstLineFrames(state, containingBlock->GetContent(), containingBlock,
                          frameList);
  } else if (insertion.mParentFrame->Style()->HasPseudoElementData()) {
    CheckForFirstLineInsertion(insertion.mParentFrame, frameList);
    CheckForFirstLineInsertion(insertion.mParentFrame, captionList);
  }

  // We might have captions; put them into the caption list of the
  // table wrapper frame.
  if (captionList.NotEmpty()) {
    NS_ASSERTION(LayoutFrameType::Table == frameType ||
                     LayoutFrameType::TableWrapper == frameType,
                 "parent for caption is not table?");
    // We need to determine where to put the caption items; start with the
    // the parent frame that has already been determined and get the insertion
    // prevsibling of the first caption item.
    bool captionIsAppend;
    nsIFrame* captionPrevSibling = nullptr;

    // aIsRangeInsertSafe is ignored on purpose because it is irrelevant here.
    bool ignored;
    InsertionPoint captionInsertion = insertion;
    if (isSingleInsert) {
      captionPrevSibling = GetInsertionPrevSibling(
          &captionInsertion, aStartChild, &captionIsAppend, &ignored);
    } else {
      nsIContent* firstCaption = captionList.FirstChild()->GetContent();
      // It is very important here that we skip the children in
      // [aStartChild,aEndChild) when looking for a
      // prevsibling.
      captionPrevSibling = GetInsertionPrevSibling(
          &captionInsertion, firstCaption, &captionIsAppend, &ignored,
          aStartChild, aEndChild);
    }

    nsContainerFrame* outerTable =
        captionInsertion.mParentFrame->IsTableFrame()
            ? captionInsertion.mParentFrame->GetParent()
            : captionInsertion.mParentFrame;

    // If the parent is not a table wrapper frame we will try to add frames
    // to a named child list that the parent does not honor and the frames
    // will get lost.
    MOZ_ASSERT(outerTable->IsTableWrapperFrame(),
               "Pseudo frame construction failure; "
               "a caption can be only a child of a table wrapper frame");

    // If the parent of our current prevSibling is different from the frame
    // we'll actually use as the parent, then the calculated insertion
    // point is now invalid (bug 341382).
    if (captionPrevSibling && captionPrevSibling->GetParent() != outerTable) {
      captionPrevSibling = nullptr;
    }

    captionList.ApplySetParent(outerTable);
    if (captionIsAppend) {
      AppendFrames(outerTable, FrameChildListID::Caption,
                   std::move(captionList));
    } else {
      InsertFrames(outerTable, FrameChildListID::Caption, captionPrevSibling,
                   std::move(captionList));
    }
  }

  LAYOUT_PHASE_TEMP_EXIT();
  if (MaybeRecreateForColumnSpan(state, insertion.mParentFrame, frameList,
                                 prevSibling)) {
    LAYOUT_PHASE_TEMP_REENTER();
    return;
  }
  LAYOUT_PHASE_TEMP_REENTER();

  if (frameList.NotEmpty()) {
    // Notify the parent frame
    if (isAppend) {
      AppendFramesToParent(state, insertion.mParentFrame, frameList,
                           prevSibling);
    } else {
      InsertFrames(insertion.mParentFrame, FrameChildListID::Principal,
                   prevSibling, std::move(frameList));
    }
  }

  if (haveFirstLetterStyle) {
    // Recover the letter frames for the containing block when
    // it has first-letter style.
    RecoverLetterFrames(state.mFloatedList.mContainingBlock);
  }

#ifdef DEBUG
  if (gReallyNoisyContentUpdates && insertion.mParentFrame) {
    printf(
        "nsCSSFrameConstructor::ContentRangeInserted: resulting frame "
        "model:\n");
    insertion.mParentFrame->List(stdout);
  }
#endif

#ifdef ACCESSIBILITY
  if (nsAccessibilityService* accService = GetAccService()) {
    accService->ContentRangeInserted(mPresShell, aStartChild, aEndChild);
  }
#endif
}

bool nsCSSFrameConstructor::ContentRemoved(nsIContent* aChild,
                                           nsIContent* aOldNextSibling,
                                           RemoveFlags aFlags) {
  MOZ_ASSERT(aChild);
  MOZ_ASSERT(!aChild->IsRootOfNativeAnonymousSubtree() || !aOldNextSibling,
             "Anonymous roots don't have siblings");
  AUTO_PROFILER_LABEL_HOT("nsCSSFrameConstructor::ContentRemoved",
                          LAYOUT_FrameConstruction);
  AUTO_LAYOUT_PHASE_ENTRY_POINT(mPresShell->GetPresContext(), FrameC);
  nsPresContext* presContext = mPresShell->GetPresContext();
  MOZ_ASSERT(presContext, "Our presShell should have a valid presContext");

  // We want to detect when the viewport override element stored in the
  // prescontext is in the subtree being removed.  Except in fullscreen cases
  // (which are handled in Element::UnbindFromTree and do not get stored on the
  // prescontext), the override element is always either the root element or a
  // <body> child of the root element.  So we can only be removing the stored
  // override element if the thing being removed is either the override element
  // itself or the root element (which can be a parent of the override element).
  if (aChild == presContext->GetViewportScrollStylesOverrideElement() ||
      (aChild->IsElement() && !aChild->GetParent())) {
    // We might be removing the element that we propagated viewport scrollbar
    // styles from.  Recompute those. (This clause covers two of the three
    // possible scrollbar-propagation sources: the <body> [as aChild or a
    // descendant] and the root node. The other possible scrollbar-propagation
    // source is a fullscreen element, and we have code elsewhere to update
    // scrollbars after fullscreen elements are removed -- specifically, it's
    // part of the fullscreen cleanup code called by Element::UnbindFromTree.
    // We don't handle the fullscreen case here, because it doesn't change the
    // scrollbar styles override element stored on the prescontext.)
    Element* newOverrideElement =
        presContext->UpdateViewportScrollStylesOverride();

    // If aChild is the root, then we don't need to do any reframing of
    // newOverrideElement, because we're about to tear down the whole frame tree
    // anyway.  And we need to make sure we don't do any such reframing, because
    // reframing the <body> can trigger a reframe of the <html> and then reenter
    // here.
    //
    // But if aChild is not the root, and if newOverrideElement is not
    // the root and isn't aChild (which it could be if all we're doing
    // here is reframing the current override element), it needs
    // reframing.  In particular, it used to have a scrollframe
    // (because its overflow was not "visible"), but now it will
    // propagate its overflow to the viewport, so it should not need a
    // scrollframe anymore.
    if (aChild->GetParent() && newOverrideElement &&
        newOverrideElement->GetParent() && newOverrideElement != aChild) {
      LAYOUT_PHASE_TEMP_EXIT();
      RecreateFramesForContent(newOverrideElement, InsertionKind::Async);
      LAYOUT_PHASE_TEMP_REENTER();
    }
  }

#ifdef DEBUG
  if (gNoisyContentUpdates) {
    printf(
        "nsCSSFrameConstructor::ContentRemoved container=%p child=%p "
        "old-next-sibling=%p\n",
        aChild->GetParent(), aChild, aOldNextSibling);
    if (gReallyNoisyContentUpdates) {
      aChild->GetParent()->List(stdout, 0);
    }
  }
#endif

  nsIFrame* childFrame = aChild->GetPrimaryFrame();
  if (!childFrame || childFrame->GetContent() != aChild) {
    // XXXbz the GetContent() != aChild check is needed due to bug 135040.
    // Remove it once that's fixed.
    childFrame = nullptr;
  }

  // If we're removing the root, then make sure to remove things starting at
  // the viewport's child instead of the primary frame (which might even be
  // null if the root was display:none, even though the frames above it got
  // created).  Detecting removal of a root is a little exciting; in particular,
  // having no parent is necessary but NOT sufficient.
  //
  // Due to how we process reframes, the content node might not even be in our
  // document by now.  So explicitly check whether the viewport's first kid's
  // content node is aChild.
  //
  // FIXME(emilio): I think the "might not be in our document" bit is impossible
  // now.
  bool isRoot = false;
  if (!aChild->GetParent()) {
    if (nsIFrame* viewport = GetRootFrame()) {
      nsIFrame* firstChild = viewport->PrincipalChildList().FirstChild();
      if (firstChild && firstChild->GetContent() == aChild) {
        isRoot = true;
        childFrame = firstChild;
        NS_ASSERTION(!childFrame->GetNextSibling(), "How did that happen?");
      }
    }
  }

  // We need to be conservative about when to determine whether something has
  // display: contents or not because at this point our actual display may be
  // different.
  //
  // Consider the case of:
  //
  //   <div id="A" style="display: contents"><div id="B"></div></div>
  //
  // If we reconstruct A because its display changed to "none", we still need to
  // cleanup the frame on B, but A's display is now "none", so we can't poke at
  // the style of it.
  //
  // FIXME(emilio, bug 1450366): We can make this faster without adding much
  // complexity for the display: none -> other case, which right now
  // unnecessarily walks the content tree down.
  auto CouldHaveBeenDisplayContents = [aFlags](nsIContent* aContent) -> bool {
    return aFlags == REMOVE_FOR_RECONSTRUCTION || IsDisplayContents(aContent);
  };

  if (!childFrame && CouldHaveBeenDisplayContents(aChild)) {
    // NOTE(emilio): We may iterate through ::before and ::after here and they
    // may be gone after the respective ContentRemoved call. Right now
    // StyleChildrenIterator handles that properly, so it's not an issue.
    StyleChildrenIterator iter(aChild);
    for (nsIContent* c = iter.GetNextChild(); c; c = iter.GetNextChild()) {
      if (c->GetPrimaryFrame() || CouldHaveBeenDisplayContents(c)) {
        LAYOUT_PHASE_TEMP_EXIT();
        bool didReconstruct = ContentRemoved(c, nullptr, aFlags);
        LAYOUT_PHASE_TEMP_REENTER();
        if (didReconstruct) {
          return true;
        }
      }
    }
    return false;
  }

  if (childFrame) {
    if (aFlags == REMOVE_FOR_RECONSTRUCTION) {
      // Before removing the frames associated with the content object,
      // ask them to save their state onto our state object.
      CaptureStateForFramesOf(aChild, mFrameTreeState);
    }

    InvalidateCanvasIfNeeded(mPresShell, aChild);

    // See whether we need to remove more than just childFrame
    LAYOUT_PHASE_TEMP_EXIT();
    if (MaybeRecreateContainerForFrameRemoval(childFrame)) {
      LAYOUT_PHASE_TEMP_REENTER();
      return true;
    }
    LAYOUT_PHASE_TEMP_REENTER();

    // Get the childFrame's parent frame
    nsIFrame* parentFrame = childFrame->GetParent();
    LayoutFrameType parentType = parentFrame->Type();

    if (parentType == LayoutFrameType::FrameSet &&
        IsSpecialFramesetChild(aChild)) {
      // Just reframe the parent, since framesets are weird like that.
      LAYOUT_PHASE_TEMP_EXIT();
      RecreateFramesForContent(parentFrame->GetContent(), InsertionKind::Async);
      LAYOUT_PHASE_TEMP_REENTER();
      return true;
    }

    // If we're a child of MathML, then we should reframe the MathML content.
    // If we're non-MathML, then we would be wrapped in a block so we need to
    // check our grandparent in that case.
    nsIFrame* possibleMathMLAncestor = parentType == LayoutFrameType::Block
                                           ? parentFrame->GetParent()
                                           : parentFrame;
    if (possibleMathMLAncestor->IsMathMLFrame()) {
      LAYOUT_PHASE_TEMP_EXIT();
      RecreateFramesForContent(parentFrame->GetContent(), InsertionKind::Async);
      LAYOUT_PHASE_TEMP_REENTER();
      return true;
    }

#ifdef ACCESSIBILITY
    if (aFlags != REMOVE_FOR_RECONSTRUCTION) {
      if (nsAccessibilityService* accService = GetAccService()) {
        accService->ContentRemoved(mPresShell, aChild);
      }
    }
#endif

    // Examine the containing-block for the removed content and see if
    // :first-letter style applies.
    nsIFrame* inflowChild = childFrame;
    if (childFrame->HasAnyStateBits(NS_FRAME_OUT_OF_FLOW)) {
      inflowChild = childFrame->GetPlaceholderFrame();
      NS_ASSERTION(inflowChild, "No placeholder for out-of-flow?");
    }
    nsContainerFrame* containingBlock =
        GetFloatContainingBlock(inflowChild->GetParent());
    bool haveFLS = containingBlock && HasFirstLetterStyle(containingBlock);
    if (haveFLS) {
      // Trap out to special routine that handles adjusting a blocks
      // frame tree when first-letter style is present.
#ifdef NOISY_FIRST_LETTER
      printf("ContentRemoved: containingBlock=");
      containingBlock->ListTag(stdout);
      printf(" parentFrame=");
      parentFrame->ListTag(stdout);
      printf(" childFrame=");
      childFrame->ListTag(stdout);
      printf("\n");
#endif

      // First update the containing blocks structure by removing the
      // existing letter frames. This makes the subsequent logic
      // simpler.
      RemoveLetterFrames(mPresShell, containingBlock);

      // Recover childFrame and parentFrame
      childFrame = aChild->GetPrimaryFrame();
      if (!childFrame || childFrame->GetContent() != aChild) {
        // XXXbz the GetContent() != aChild check is needed due to bug 135040.
        // Remove it once that's fixed.
        return false;
      }
      parentFrame = childFrame->GetParent();
      parentType = parentFrame->Type();

#ifdef NOISY_FIRST_LETTER
      printf("  ==> revised parentFrame=");
      parentFrame->ListTag(stdout);
      printf(" childFrame=");
      childFrame->ListTag(stdout);
      printf("\n");
#endif
    }

#ifdef DEBUG
    if (gReallyNoisyContentUpdates) {
      printf("nsCSSFrameConstructor::ContentRemoved: childFrame=");
      childFrame->ListTag(stdout);
      putchar('\n');
      parentFrame->List(stdout);
    }
#endif

    // Notify the parent frame that it should delete the frame
    if (childFrame->HasAnyStateBits(NS_FRAME_OUT_OF_FLOW)) {
      childFrame = childFrame->GetPlaceholderFrame();
      NS_ASSERTION(childFrame, "Missing placeholder frame for out of flow.");
      parentFrame = childFrame->GetParent();
    }

    DestroyContext context(mPresShell);
    RemoveFrame(context, nsLayoutUtils::GetChildListNameFor(childFrame),
                childFrame);

    // NOTE(emilio): aChild could be dead here already if it is a ::before or
    // ::after pseudo-element (since in that case it was owned by childFrame,
    // which we just destroyed).

    if (isRoot) {
      mRootElementFrame = nullptr;
      mRootElementStyleFrame = nullptr;
      mDocElementContainingBlock = nullptr;
      mCanvasFrame = nullptr;
      mPageSequenceFrame = nullptr;
    }

    if (haveFLS && mRootElementFrame) {
      RecoverLetterFrames(containingBlock);
    }

    // If we're just reconstructing frames for the element, then the
    // following ContentInserted notification on the element will
    // take care of fixing up any adjacent text nodes.  We don't need
    // to do this if the table parent type of our parent type is not
    // eTypeBlock, though, because in that case the whitespace isn't
    // being suppressed due to us anyway.
    if (aOldNextSibling && aFlags == REMOVE_CONTENT &&
        GetParentType(parentType) == eTypeBlock) {
      MOZ_ASSERT(aChild->GetParentNode(),
                 "How did we have a sibling without a parent?");
      // Adjacent whitespace-only text nodes might have been suppressed if
      // this node does not have inline ends. Create frames for them now
      // if necessary.
      // Reframe any text node just before the node being removed, if there is
      // one, and if it's not the last child or the first child. If a whitespace
      // textframe was being suppressed and it's now the last child or first
      // child then it can stay suppressed since the parent must be a block
      // and hence it's adjacent to a block end.
      // If aOldNextSibling is null, then the text node before the node being
      // removed is the last node, and we don't need to worry about it.
      //
      // FIXME(emilio): This should probably use the lazy frame construction
      // bits if possible instead of reframing it in place.
      nsIContent* prevSibling = aOldNextSibling->GetPreviousSibling();
      if (prevSibling && prevSibling->GetPreviousSibling()) {
        LAYOUT_PHASE_TEMP_EXIT();
        ReframeTextIfNeeded(prevSibling);
        LAYOUT_PHASE_TEMP_REENTER();
      }
      // Reframe any text node just after the node being removed, if there is
      // one, and if it's not the last child or the first child.
      if (aOldNextSibling->GetNextSibling() &&
          aOldNextSibling->GetPreviousSibling()) {
        LAYOUT_PHASE_TEMP_EXIT();
        ReframeTextIfNeeded(aOldNextSibling);
        LAYOUT_PHASE_TEMP_REENTER();
      }
    }

#ifdef DEBUG
    if (gReallyNoisyContentUpdates && parentFrame) {
      printf("nsCSSFrameConstructor::ContentRemoved: resulting frame model:\n");
      parentFrame->List(stdout);
    }
#endif
  }

  return false;
}

/**
 * This method invalidates the canvas when frames are removed or added for a
 * node that might have its background propagated to the canvas, i.e., a
 * document root node or an HTML BODY which is a child of the root node.
 *
 * @param aFrame a frame for a content node about to be removed or a frame that
 *               was just created for a content node that was inserted.
 */
static void InvalidateCanvasIfNeeded(PresShell* aPresShell, nsIContent* aNode) {
  MOZ_ASSERT(aPresShell->GetRootFrame(), "What happened here?");
  MOZ_ASSERT(aPresShell->GetPresContext(), "Say what?");

  //  Note that both in ContentRemoved and ContentInserted the content node
  //  will still have the right parent pointer, so looking at that is ok.

  nsIContent* parent = aNode->GetParent();
  if (parent) {
    // Has a parent; might not be what we want
    nsIContent* grandParent = parent->GetParent();
    if (grandParent) {
      // Has a grandparent, so not what we want
      return;
    }

    // Check whether it's an HTML body
    if (!aNode->IsHTMLElement(nsGkAtoms::body)) {
      return;
    }
  }

  // At this point the node has no parent or it's an HTML <body> child of the
  // root.  We might not need to invalidate in this case (eg we might be in
  // XHTML or something), but chances are we want to.  Play it safe.
  // Invalidate the viewport.

  nsIFrame* rootFrame = aPresShell->GetRootFrame();
  rootFrame->InvalidateFrameSubtree();
}

bool nsCSSFrameConstructor::EnsureFrameForTextNodeIsCreatedAfterFlush(
    CharacterData* aContent) {
  if (!aContent->HasFlag(NS_CREATE_FRAME_IF_NON_WHITESPACE)) {
    return false;
  }

  if (mAlwaysCreateFramesForIgnorableWhitespace) {
    return false;
  }

  // Text frame may have been suppressed. Disable suppression and signal that a
  // flush should be performed. We do this on a document-wide basis so that
  // pages that repeatedly query metrics for collapsed-whitespace text nodes
  // don't trigger pathological behavior.
  mAlwaysCreateFramesForIgnorableWhitespace = true;
  Element* root = mDocument->GetRootElement();
  if (!root) {
    return false;
  }

  RestyleManager()->PostRestyleEvent(root, RestyleHint{0},
                                     nsChangeHint_ReconstructFrame);
  return true;
}

void nsCSSFrameConstructor::CharacterDataChanged(
    nsIContent* aContent, const CharacterDataChangeInfo& aInfo) {
  AUTO_PROFILER_LABEL_HOT("nsCSSFrameConstructor::CharacterDataChanged",
                          LAYOUT_FrameConstruction);
  AUTO_LAYOUT_PHASE_ENTRY_POINT(mPresShell->GetPresContext(), FrameC);

  if ((aContent->HasFlag(NS_CREATE_FRAME_IF_NON_WHITESPACE) &&
       !aContent->TextIsOnlyWhitespace()) ||
      (aContent->HasFlag(NS_REFRAME_IF_WHITESPACE) &&
       aContent->TextIsOnlyWhitespace())) {
#ifdef DEBUG
    nsIFrame* frame = aContent->GetPrimaryFrame();
    NS_ASSERTION(!frame || !frame->IsGeneratedContentFrame(),
                 "Bit should never be set on generated content");
#endif
    LAYOUT_PHASE_TEMP_EXIT();
    RecreateFramesForContent(aContent, InsertionKind::Async);
    LAYOUT_PHASE_TEMP_REENTER();
    return;
  }

  // It's possible the frame whose content changed isn't inserted into the
  // frame hierarchy yet, or that there is no frame that maps the content
  if (nsIFrame* frame = aContent->GetPrimaryFrame()) {
#if 0
    NS_FRAME_LOG(NS_FRAME_TRACE_CALLS,
       ("nsCSSFrameConstructor::CharacterDataChanged: content=%p[%s] subcontent=%p frame=%p",
        aContent, ContentTag(aContent, 0),
        aSubContent, frame));
#endif

    if (frame->HasAnyStateBits(NS_FRAME_IS_IN_SINGLE_CHAR_MI)) {
      LAYOUT_PHASE_TEMP_EXIT();
      RecreateFramesForContent(aContent, InsertionKind::Async);
      LAYOUT_PHASE_TEMP_REENTER();
      return;
    }

    // Special check for text content that is a child of a letter frame.  If
    // this happens, we should remove the letter frame, do whatever we're
    // planning to do with this notification, then put the letter frame back.
    // Note that this is basically what RecreateFramesForContent ends up doing;
    // the reason we dont' want to call that here is that our text content
    // could be native anonymous, in which case RecreateFramesForContent would
    // completely barf on it.  And recreating the non-anonymous ancestor would
    // just lead us to come back into this notification (e.g. if quotes or
    // counters are involved), leading to a loop.
    nsContainerFrame* block = GetFloatContainingBlock(frame);
    bool haveFirstLetterStyle = false;
    if (block) {
      // See if the block has first-letter style applied to it.
      haveFirstLetterStyle = HasFirstLetterStyle(block);
      if (haveFirstLetterStyle) {
        RemoveLetterFrames(mPresShell, block);
        // Reget |frame|, since we might have killed it.
        // Do we really need to call CharacterDataChanged in this case, though?
        frame = aContent->GetPrimaryFrame();
        NS_ASSERTION(frame, "Should have frame here!");
      }
    }

    // Notify the first frame that maps the content. It will generate a reflow
    // command
    frame->CharacterDataChanged(aInfo);

    if (haveFirstLetterStyle) {
      RecoverLetterFrames(block);
    }
  }
}

void nsCSSFrameConstructor::RecalcQuotesAndCounters() {
  nsAutoScriptBlocker scriptBlocker;

  if (mQuotesDirty) {
    mQuotesDirty = false;
    mContainStyleScopeManager.RecalcAllQuotes();
  }

  if (mCountersDirty) {
    mCountersDirty = false;
    mContainStyleScopeManager.RecalcAllCounters();
  }

  NS_ASSERTION(!mQuotesDirty, "Quotes updates will be lost");
  NS_ASSERTION(!mCountersDirty, "Counter updates will be lost");
}

void nsCSSFrameConstructor::NotifyCounterStylesAreDirty() {
  mContainStyleScopeManager.SetAllCountersDirty();
  CountersDirty();
}

void nsCSSFrameConstructor::WillDestroyFrameTree() {
#if defined(DEBUG_dbaron_off)
  mContainStyleScopeManager.DumpCounters();
#endif

  // Prevent frame tree destruction from being O(N^2)
  mContainStyleScopeManager.Clear();
  nsFrameManager::Destroy();
}

// STATIC

// XXXbz I'd really like this method to go away. Once we have inline-block and
// I can just use that for sized broken images, that can happen, maybe.
//
// NOTE(emilio): This needs to match MozAltContent handling.
void nsCSSFrameConstructor::GetAlternateTextFor(const Element& aElement,
                                                nsAString& aAltText) {
  // The "alt" attribute specifies alternate text that is rendered
  // when the image can not be displayed.
  if (aElement.GetAttr(nsGkAtoms::alt, aAltText)) {
    return;
  }

  if (aElement.IsHTMLElement(nsGkAtoms::input)) {
    // If there's no "alt" attribute, and aElement is an input element, then use
    // the value of the "value" attribute.
    if (aElement.GetAttr(nsGkAtoms::value, aAltText)) {
      return;
    }

    // If there's no "value" attribute either, then use the localized string for
    // "Submit" as the alternate text.
    nsContentUtils::GetMaybeLocalizedString(nsContentUtils::eFORMS_PROPERTIES,
                                            "Submit", aElement.OwnerDoc(),
                                            aAltText);
  }
}

nsIFrame* nsCSSFrameConstructor::CreateContinuingOuterTableFrame(
    nsIFrame* aFrame, nsContainerFrame* aParentFrame, nsIContent* aContent,
    ComputedStyle* aComputedStyle) {
  nsTableWrapperFrame* newFrame =
      NS_NewTableWrapperFrame(mPresShell, aComputedStyle);

  newFrame->Init(aContent, aParentFrame, aFrame);

  // Create a continuing inner table frame, and if there's a caption then
  // replicate the caption
  nsFrameList newChildFrames;

  nsIFrame* childFrame = aFrame->PrincipalChildList().FirstChild();
  if (childFrame) {
    nsIFrame* continuingTableFrame =
        CreateContinuingFrame(childFrame, newFrame);
    newChildFrames.AppendFrame(nullptr, continuingTableFrame);

    NS_ASSERTION(!childFrame->GetNextSibling(),
                 "there can be only one inner table frame");
  }

  // Set the table wrapper's initial child list
  newFrame->SetInitialChildList(FrameChildListID::Principal,
                                std::move(newChildFrames));

  return newFrame;
}

nsIFrame* nsCSSFrameConstructor::CreateContinuingTableFrame(
    nsIFrame* aFrame, nsContainerFrame* aParentFrame, nsIContent* aContent,
    ComputedStyle* aComputedStyle) {
  nsTableFrame* newFrame = NS_NewTableFrame(mPresShell, aComputedStyle);

  newFrame->Init(aContent, aParentFrame, aFrame);

  // Replicate any header/footer frames
  nsFrameList childFrames;
  for (nsIFrame* childFrame : aFrame->PrincipalChildList()) {
    // See if it's a header/footer, possibly wrapped in a scroll frame.
    nsTableRowGroupFrame* rowGroupFrame =
        static_cast<nsTableRowGroupFrame*>(childFrame);
    // If the row group was continued, then don't replicate it.
    nsIFrame* rgNextInFlow = rowGroupFrame->GetNextInFlow();
    if (rgNextInFlow) {
      rowGroupFrame->SetRepeatable(false);
    } else if (rowGroupFrame->IsRepeatable()) {
      // Replicate the header/footer frame.
      nsTableRowGroupFrame* headerFooterFrame;
      nsFrameList childList;

      nsFrameConstructorState state(
          mPresShell, GetAbsoluteContainingBlock(newFrame, FIXED_POS),
          GetAbsoluteContainingBlock(newFrame, ABS_POS), nullptr);
      state.mCreatingExtraFrames = true;

      ComputedStyle* const headerFooterComputedStyle = rowGroupFrame->Style();
      headerFooterFrame = static_cast<nsTableRowGroupFrame*>(
          NS_NewTableRowGroupFrame(mPresShell, headerFooterComputedStyle));

      nsIContent* headerFooter = rowGroupFrame->GetContent();
      headerFooterFrame->Init(headerFooter, newFrame, nullptr);

      nsFrameConstructorSaveState absoluteSaveState;
      MakeTablePartAbsoluteContainingBlock(state, absoluteSaveState,
                                           headerFooterFrame);

      nsFrameConstructorSaveState floatSaveState;
      state.MaybePushFloatContainingBlock(headerFooterFrame, floatSaveState);

      ProcessChildren(state, headerFooter, rowGroupFrame->Style(),
                      headerFooterFrame, true, childList, false, nullptr);
      NS_ASSERTION(state.mFloatedList.IsEmpty(), "unexpected floated element");
      headerFooterFrame->SetInitialChildList(FrameChildListID::Principal,
                                             std::move(childList));
      headerFooterFrame->SetRepeatable(true);

      // Table specific initialization
      headerFooterFrame->InitRepeatedFrame(rowGroupFrame);

      // XXX Deal with absolute and fixed frames...
      childFrames.AppendFrame(nullptr, headerFooterFrame);
    }
  }

  // Set the table frame's initial child list
  newFrame->SetInitialChildList(FrameChildListID::Principal,
                                std::move(childFrames));

  return newFrame;
}

nsIFrame* nsCSSFrameConstructor::CreateContinuingFrame(
    nsIFrame* aFrame, nsContainerFrame* aParentFrame, bool aIsFluid) {
  ComputedStyle* computedStyle = aFrame->Style();
  nsIFrame* newFrame = nullptr;
  nsIFrame* nextContinuation = aFrame->GetNextContinuation();
  nsIFrame* nextInFlow = aFrame->GetNextInFlow();

  // Use the frame type to determine what type of frame to create
  LayoutFrameType frameType = aFrame->Type();
  nsIContent* content = aFrame->GetContent();

  if (LayoutFrameType::Text == frameType) {
    newFrame = NS_NewContinuingTextFrame(mPresShell, computedStyle);
    newFrame->Init(content, aParentFrame, aFrame);
  } else if (LayoutFrameType::Inline == frameType) {
    newFrame = NS_NewInlineFrame(mPresShell, computedStyle);
    newFrame->Init(content, aParentFrame, aFrame);
  } else if (LayoutFrameType::Block == frameType) {
    MOZ_ASSERT(!aFrame->IsTableCaption(),
               "no support for fragmenting table captions yet");
    newFrame = NS_NewBlockFrame(mPresShell, computedStyle);
    newFrame->Init(content, aParentFrame, aFrame);
  } else if (LayoutFrameType::ColumnSetWrapper == frameType) {
    newFrame =
        NS_NewColumnSetWrapperFrame(mPresShell, computedStyle, nsFrameState(0));
    newFrame->Init(content, aParentFrame, aFrame);
  } else if (LayoutFrameType::ColumnSet == frameType) {
    MOZ_ASSERT(!aFrame->IsTableCaption(),
               "no support for fragmenting table captions yet");
    newFrame = NS_NewColumnSetFrame(mPresShell, computedStyle, nsFrameState(0));
    newFrame->Init(content, aParentFrame, aFrame);
  } else if (LayoutFrameType::PrintedSheet == frameType) {
    newFrame = ConstructPrintedSheetFrame(mPresShell, aParentFrame, aFrame);
  } else if (LayoutFrameType::Page == frameType) {
    nsCanvasFrame* canvasFrame;  // (unused outparam for ConstructPageFrame)
    newFrame =
        ConstructPageFrame(mPresShell, aParentFrame, aFrame, canvasFrame);
  } else if (LayoutFrameType::TableWrapper == frameType) {
    newFrame = CreateContinuingOuterTableFrame(aFrame, aParentFrame, content,
                                               computedStyle);
  } else if (LayoutFrameType::Table == frameType) {
    newFrame = CreateContinuingTableFrame(aFrame, aParentFrame, content,
                                          computedStyle);
  } else if (LayoutFrameType::TableRowGroup == frameType) {
    newFrame = NS_NewTableRowGroupFrame(mPresShell, computedStyle);
    newFrame->Init(content, aParentFrame, aFrame);
  } else if (LayoutFrameType::TableRow == frameType) {
    nsTableRowFrame* rowFrame = NS_NewTableRowFrame(mPresShell, computedStyle);

    rowFrame->Init(content, aParentFrame, aFrame);

    // Create a continuing frame for each table cell frame
    nsFrameList newChildList;
    nsIFrame* cellFrame = aFrame->PrincipalChildList().FirstChild();
    while (cellFrame) {
      // See if it's a table cell frame
      if (cellFrame->IsTableCellFrame()) {
        nsIFrame* continuingCellFrame =
            CreateContinuingFrame(cellFrame, rowFrame);
        newChildList.AppendFrame(nullptr, continuingCellFrame);
      }
      cellFrame = cellFrame->GetNextSibling();
    }

    rowFrame->SetInitialChildList(FrameChildListID::Principal,
                                  std::move(newChildList));
    newFrame = rowFrame;

  } else if (LayoutFrameType::TableCell == frameType) {
    // Warning: If you change this and add a wrapper frame around table cell
    // frames, make sure Bug 368554 doesn't regress!
    // See IsInAutoWidthTableCellForQuirk() in nsImageFrame.cpp.
    nsTableFrame* tableFrame =
        static_cast<nsTableRowFrame*>(aParentFrame)->GetTableFrame();
    nsTableCellFrame* cellFrame =
        NS_NewTableCellFrame(mPresShell, computedStyle, tableFrame);

    cellFrame->Init(content, aParentFrame, aFrame);

    // Create a continuing area frame
    nsIFrame* blockFrame = aFrame->PrincipalChildList().FirstChild();
    nsIFrame* continuingBlockFrame =
        CreateContinuingFrame(blockFrame, cellFrame);

    SetInitialSingleChild(cellFrame, continuingBlockFrame);
    newFrame = cellFrame;
  } else if (LayoutFrameType::Line == frameType) {
    newFrame = NS_NewFirstLineFrame(mPresShell, computedStyle);
    newFrame->Init(content, aParentFrame, aFrame);
  } else if (LayoutFrameType::Letter == frameType) {
    newFrame = NS_NewFirstLetterFrame(mPresShell, computedStyle);
    newFrame->Init(content, aParentFrame, aFrame);
  } else if (LayoutFrameType::Image == frameType) {
    auto* imageFrame = static_cast<nsImageFrame*>(aFrame);
    newFrame = imageFrame->CreateContinuingFrame(mPresShell, computedStyle);
    newFrame->Init(content, aParentFrame, aFrame);
  } else if (LayoutFrameType::ImageControl == frameType) {
    newFrame = NS_NewImageControlFrame(mPresShell, computedStyle);
    newFrame->Init(content, aParentFrame, aFrame);
  } else if (LayoutFrameType::FieldSet == frameType) {
    newFrame = NS_NewFieldSetFrame(mPresShell, computedStyle);
    newFrame->Init(content, aParentFrame, aFrame);
  } else if (LayoutFrameType::FlexContainer == frameType) {
    newFrame = NS_NewFlexContainerFrame(mPresShell, computedStyle);
    newFrame->Init(content, aParentFrame, aFrame);
  } else if (LayoutFrameType::GridContainer == frameType) {
    newFrame = NS_NewGridContainerFrame(mPresShell, computedStyle);
    newFrame->Init(content, aParentFrame, aFrame);
  } else if (LayoutFrameType::Ruby == frameType) {
    newFrame = NS_NewRubyFrame(mPresShell, computedStyle);
    newFrame->Init(content, aParentFrame, aFrame);
  } else if (LayoutFrameType::RubyBaseContainer == frameType) {
    newFrame = NS_NewRubyBaseContainerFrame(mPresShell, computedStyle);
    newFrame->Init(content, aParentFrame, aFrame);
  } else if (LayoutFrameType::RubyTextContainer == frameType) {
    newFrame = NS_NewRubyTextContainerFrame(mPresShell, computedStyle);
    newFrame->Init(content, aParentFrame, aFrame);
  } else {
    MOZ_CRASH("unexpected frame type");
  }

  // Init() set newFrame to be a fluid continuation of aFrame.
  // If we want a non-fluid continuation, we need to call SetPrevContinuation()
  // to reset NS_FRAME_IS_FLUID_CONTINUATION.
  if (!aIsFluid) {
    newFrame->SetPrevContinuation(aFrame);
  }

  // If a continuing frame needs to carry frame state bits from its previous
  // continuation or parent, set them in nsIFrame::Init(), or in any derived
  // frame class's Init() if the bits are belong to specific group.

  if (nextInFlow) {
    nextInFlow->SetPrevInFlow(newFrame);
    newFrame->SetNextInFlow(nextInFlow);
  } else if (nextContinuation) {
    nextContinuation->SetPrevContinuation(newFrame);
    newFrame->SetNextContinuation(nextContinuation);
  }

  // aFrame cannot be a dynamic reflow root because it has a continuation now.
  aFrame->RemoveStateBits(NS_FRAME_DYNAMIC_REFLOW_ROOT);

  MOZ_ASSERT(!newFrame->GetNextSibling(), "unexpected sibling");
  return newFrame;
}

void nsCSSFrameConstructor::MaybeSetNextPageContentFramePageName(
    const nsIFrame* aFrame) {
  MOZ_ASSERT(aFrame, "Frame should not be null");
  // No parent means the root frame, which isn't what this funciton is for.
  MOZ_ASSERT(aFrame->GetParent(),
             "Frame should be the first child placed on a new page, not the "
             "root frame.");
  if (mNextPageContentFramePageName) {
    return;
  }
  const nsAtom* const autoValue = aFrame->GetParent()->GetAutoPageValue();
  mNextPageContentFramePageName = aFrame->ComputePageValue(autoValue);
}

nsresult nsCSSFrameConstructor::ReplicateFixedFrames(
    nsPageContentFrame* aParentFrame) {
  // Now deal with fixed-pos things....  They should appear on all pages,
  // so we want to move over the placeholders when processing the child
  // of the pageContentFrame.

  nsIFrame* prevPageContentFrame = aParentFrame->GetPrevInFlow();
  if (!prevPageContentFrame) {
    return NS_OK;
  }
  nsContainerFrame* canvasFrame =
      do_QueryFrame(aParentFrame->PrincipalChildList().FirstChild());
  nsIFrame* prevCanvasFrame =
      prevPageContentFrame->PrincipalChildList().FirstChild();
  if (!canvasFrame || !prevCanvasFrame) {
    // document's root element frame missing
    return NS_ERROR_UNEXPECTED;
  }

  nsFrameList fixedPlaceholders;
  nsIFrame* firstFixed =
      prevPageContentFrame->GetChildList(FrameChildListID::Fixed).FirstChild();
  if (!firstFixed) {
    return NS_OK;
  }

  // Don't allow abs-pos descendants of the fixed content to escape the content.
  // This should not normally be possible (because fixed-pos elements should
  // be absolute containers) but fixed-pos tables currently aren't abs-pos
  // containers.
  nsFrameConstructorState state(mPresShell, aParentFrame, nullptr,
                                mRootElementFrame);
  state.mCreatingExtraFrames = true;

  // We can't use an ancestor filter here, because we're not going to
  // be usefully recurring down the tree.  This means that other
  // places in frame construction can't assume a filter is
  // initialized!

  // Iterate across fixed frames and replicate each whose placeholder is a
  // descendant of aFrame. (We don't want to explicitly copy placeholders that
  // are within fixed frames, because that would cause duplicates on the new
  // page - bug 389619)
  for (nsIFrame* fixed = firstFixed; fixed; fixed = fixed->GetNextSibling()) {
    nsIFrame* prevPlaceholder = fixed->GetPlaceholderFrame();
    if (prevPlaceholder && nsLayoutUtils::IsProperAncestorFrame(
                               prevCanvasFrame, prevPlaceholder)) {
      // We want to use the same style as the primary style frame for
      // our content
      nsIContent* content = fixed->GetContent();
      ComputedStyle* computedStyle =
          nsLayoutUtils::GetStyleFrame(content)->Style();
      AutoFrameConstructionItemList items(this);
      AddFrameConstructionItemsInternal(state, content, canvasFrame, true,
                                        computedStyle,
                                        {ItemFlag::AllowPageBreak}, items);
      ConstructFramesFromItemList(state, items, canvasFrame,
                                  /* aParentIsWrapperAnonBox = */ false,
                                  fixedPlaceholders);
    }
  }

  // Add the placeholders to our primary child list.
  // XXXbz this is a little screwed up, since the fixed frames will have
  // broken auto-positioning. Oh, well.
  NS_ASSERTION(!canvasFrame->PrincipalChildList().FirstChild(),
               "leaking frames; doc root continuation must be empty");
  canvasFrame->SetInitialChildList(FrameChildListID::Principal,
                                   std::move(fixedPlaceholders));
  return NS_OK;
}

nsCSSFrameConstructor::InsertionPoint nsCSSFrameConstructor::GetInsertionPoint(
    nsIContent* aChild) {
  MOZ_ASSERT(aChild);
  nsIContent* insertionElement = aChild->GetFlattenedTreeParent();
  if (!insertionElement) {
    // The element doesn't belong in the flattened tree, and thus we don't want
    // to render it.
    return {};
  }

  return {GetContentInsertionFrameFor(insertionElement), insertionElement};
}

// Capture state for the frame tree rooted at the frame associated with the
// content object, aContent
void nsCSSFrameConstructor::CaptureStateForFramesOf(
    nsIContent* aContent, nsILayoutHistoryState* aHistoryState) {
  if (!aHistoryState) {
    return;
  }
  nsIFrame* frame = aContent->GetPrimaryFrame();
  if (frame == mRootElementFrame) {
    frame = mRootElementFrame
                ? GetAbsoluteContainingBlock(mRootElementFrame, FIXED_POS)
                : GetRootFrame();
  }
  for (; frame;
       frame = nsLayoutUtils::GetNextContinuationOrIBSplitSibling(frame)) {
    CaptureFrameState(frame, aHistoryState);
  }
}

static bool IsWhitespaceFrame(nsIFrame* aFrame) {
  MOZ_ASSERT(aFrame, "invalid argument");
  return aFrame->IsTextFrame() && aFrame->GetContent()->TextIsOnlyWhitespace();
}

static nsIFrame* FindFirstNonWhitespaceChild(nsIFrame* aParentFrame) {
  nsIFrame* f = aParentFrame->PrincipalChildList().FirstChild();
  while (f && IsWhitespaceFrame(f)) {
    f = f->GetNextSibling();
  }
  return f;
}

static nsIFrame* FindNextNonWhitespaceSibling(nsIFrame* aFrame) {
  nsIFrame* f = aFrame;
  do {
    f = f->GetNextSibling();
  } while (f && IsWhitespaceFrame(f));
  return f;
}

static nsIFrame* FindPreviousNonWhitespaceSibling(nsIFrame* aFrame) {
  nsIFrame* f = aFrame;
  do {
    f = f->GetPrevSibling();
  } while (f && IsWhitespaceFrame(f));
  return f;
}

bool nsCSSFrameConstructor::MaybeRecreateContainerForFrameRemoval(
    nsIFrame* aFrame) {
#define TRACE(reason)                                                       \
  PROFILER_MARKER("MaybeRecreateContainerForFrameRemoval: " reason, LAYOUT, \
                  {}, Tracing, "Layout")
  MOZ_ASSERT(aFrame, "Must have a frame");
  MOZ_ASSERT(aFrame->GetParent(), "Frame shouldn't be root");
  MOZ_ASSERT(aFrame == aFrame->FirstContinuation(),
             "aFrame not the result of GetPrimaryFrame()?");

  nsIFrame* inFlowFrame = aFrame->HasAnyStateBits(NS_FRAME_OUT_OF_FLOW)
                              ? aFrame->GetPlaceholderFrame()
                              : aFrame;
  MOZ_ASSERT(inFlowFrame, "How did that happen?");
  MOZ_ASSERT(inFlowFrame == inFlowFrame->FirstContinuation(),
             "placeholder for primary frame has previous continuations?");
  nsIFrame* parent = inFlowFrame->GetParent();

  if (inFlowFrame->HasAnyStateBits(NS_FRAME_HAS_MULTI_COLUMN_ANCESTOR)) {
    nsIFrame* grandparent = parent->GetParent();
    MOZ_ASSERT(grandparent);

    bool needsReframe =
        // 1. Removing a column-span may lead to an empty
        // ::-moz-column-span-wrapper.
        inFlowFrame->IsColumnSpan() ||
        // 2. Removing a frame which has any column-span siblings may also
        // lead to an empty ::-moz-column-span-wrapper subtree. The
        // column-span siblings were the frame's children, but later become
        // the frame's siblings after CreateColumnSpanSiblings().
        inFlowFrame->HasColumnSpanSiblings() ||
        // 3. Removing the only child of a ::-moz-column-content, whose
        // ColumnSet grandparent has a previous column-span sibling, requires
        // reframing since we might connect the ColumnSet's next column-span
        // sibling (if there's one). Note that this isn't actually needed if
        // the ColumnSet is at the end of ColumnSetWrapper since we create
        // empty ones at the end anyway, but we're not worried about
        // optimizing that case.
        (parent->Style()->GetPseudoType() == PseudoStyleType::columnContent &&
         // The only child in ::-moz-column-content (might be tall enough to
         // split across columns)
         !inFlowFrame->GetPrevSibling() && !inFlowFrame->GetNextSibling() &&
         // That ::-moz-column-content is the first column.
         !parent->GetPrevInFlow() &&
         // The ColumnSet grandparent has a previous sibling that is a
         // column-span.
         grandparent->GetPrevSibling());

    if (needsReframe) {
      nsContainerFrame* containingBlock =
          GetMultiColumnContainingBlockFor(inFlowFrame);

#ifdef DEBUG
      if (IsFramePartOfIBSplit(inFlowFrame)) {
        nsIFrame* ibContainingBlock = GetIBContainingBlockFor(inFlowFrame);
        MOZ_ASSERT(containingBlock == ibContainingBlock ||
                       nsLayoutUtils::IsProperAncestorFrame(containingBlock,
                                                            ibContainingBlock),
                   "Multi-column containing block should be equal to or be the "
                   "ancestor of the IB containing block!");
      }
#endif

      TRACE("Multi-column");
      RecreateFramesForContent(containingBlock->GetContent(),
                               InsertionKind::Async);
      return true;
    }
  }

  if (IsFramePartOfIBSplit(aFrame)) {
    // The removal functions can't handle removal of an {ib} split directly; we
    // need to rebuild the containing block.
    TRACE("IB split removal");
    ReframeContainingBlock(aFrame);
    return true;
  }

  if (inFlowFrame->IsRenderedLegend()) {
    TRACE("Fieldset / Legend");
    RecreateFramesForContent(parent->GetContent(), InsertionKind::Async);
    return true;
  }

  // Now check for possibly needing to reconstruct due to a pseudo parent
  // For the case of ruby pseudo parent, effectively, only pseudo rb/rt frame
  // need to be checked here, since all other types of parent will be catched
  // by "Check ruby containers" section below.
  if (IsTableOrRubyPseudo(parent)) {
    if (FindFirstNonWhitespaceChild(parent) == inFlowFrame ||
        !FindNextNonWhitespaceSibling(inFlowFrame->LastContinuation()) ||
        // If it is a whitespace, and is the only child of the parent, the
        // pseudo parent was created for the space, and should now be removed.
        (IsWhitespaceFrame(aFrame) &&
         parent->PrincipalChildList().OnlyChild()) ||
        // If we're a table-column-group, then the OnlyChild check above is
        // not going to catch cases when we're the first child.
        (inFlowFrame->IsTableColGroupFrame() &&
         parent->GetChildList(FrameChildListID::ColGroup).FirstChild() ==
             inFlowFrame) ||
        // Similar if we're a table-caption.
        (inFlowFrame->IsTableCaption() &&
         parent->GetChildList(FrameChildListID::Caption).FirstChild() ==
             inFlowFrame)) {
      TRACE("Table or ruby pseudo parent");
      RecreateFramesForContent(parent->GetContent(), InsertionKind::Async);
      return true;
    }
  }

  // Might need to reconstruct things if this frame's nextSibling is a table
  // or ruby pseudo, since removal of this frame might mean that this pseudo
  // needs to get merged with the frame's prevSibling if that's also a table
  // or ruby pseudo.
  nsIFrame* nextSibling =
      FindNextNonWhitespaceSibling(inFlowFrame->LastContinuation());
  NS_ASSERTION(!IsTableOrRubyPseudo(inFlowFrame), "Shouldn't happen here");
  // Effectively, for the ruby pseudo sibling case, only pseudo <ruby> frame
  // need to be checked here, since all other types of such frames will have
  // a ruby container parent, and be catched by "Check ruby containers" below.
  if (nextSibling && IsTableOrRubyPseudo(nextSibling)) {
    nsIFrame* prevSibling = FindPreviousNonWhitespaceSibling(inFlowFrame);
    if (prevSibling && IsTableOrRubyPseudo(prevSibling)) {
      TRACE("Table or ruby pseudo sibling");
      // Good enough to recreate frames for aFrame's parent's content; even if
      // aFrame's parent is a pseudo, that'll be the right content node.
      RecreateFramesForContent(parent->GetContent(), InsertionKind::Async);
      return true;
    }
  }

  // Check ruby containers
  LayoutFrameType parentType = parent->Type();
  if (parentType == LayoutFrameType::Ruby ||
      RubyUtils::IsRubyContainerBox(parentType)) {
    // In ruby containers, pseudo frames may be created from
    // whitespaces or even nothing. There are two cases we actually
    // need to handle here, but hard to check exactly:
    // 1. Status of spaces beside the frame may vary, and related
    //    frames may be constructed or destroyed accordingly.
    // 2. The type of the first child of a ruby frame determines
    //    whether a pseudo ruby base container should exist.
    TRACE("Ruby container");
    RecreateFramesForContent(parent->GetContent(), InsertionKind::Async);
    return true;
  }

  // Might need to reconstruct things if the removed frame's nextSibling is an
  // anonymous flex item.  The removed frame might've been what divided two
  // runs of inline content into two anonymous flex items, which would now
  // need to be merged.
  // NOTE: It's fine that we've advanced nextSibling past whitespace (up above);
  // we're only interested in anonymous flex items here, and those can never
  // be adjacent to whitespace, since they absorb contiguous runs of inline
  // non-replaced content (including whitespace).
  if (nextSibling && IsAnonymousItem(nextSibling)) {
    AssertAnonymousFlexOrGridItemParent(nextSibling, parent);
    TRACE("Anon flex or grid item next sibling");
    // Recreate frames for the flex container (the removed frame's parent)
    RecreateFramesForContent(parent->GetContent(), InsertionKind::Async);
    return true;
  }

  // Might need to reconstruct things if the removed frame's nextSibling is
  // null and its parent is an anonymous flex item. (This might be the last
  // remaining child of that anonymous flex item, which can then go away.)
  if (!nextSibling && IsAnonymousItem(parent)) {
    AssertAnonymousFlexOrGridItemParent(parent, parent->GetParent());
    TRACE("Anon flex or grid item parent");
    // Recreate frames for the flex container (the removed frame's grandparent)
    RecreateFramesForContent(parent->GetParent()->GetContent(),
                             InsertionKind::Async);
    return true;
  }

  // Reconstruct if inflowFrame is parent's only child, and parent is, or has,
  // a non-fluid continuation, i.e. it was split by bidi resolution
  if (!inFlowFrame->GetPrevSibling() && !inFlowFrame->GetNextSibling() &&
      ((parent->GetPrevContinuation() && !parent->GetPrevInFlow()) ||
       (parent->GetNextContinuation() && !parent->GetNextInFlow()))) {
    TRACE("Removing last child of non-fluid split parent");
    RecreateFramesForContent(parent->GetContent(), InsertionKind::Async);
    return true;
  }

  // We might still need to reconstruct things if the parent of inFlowFrame is
  // ib-split, since in that case the removal of aFrame might affect the
  // splitting of its parent.
  if (!IsFramePartOfIBSplit(parent)) {
    return false;
  }

  // If inFlowFrame is not the only in-flow child of |parent|, then removing
  // it will change nothing about the {ib} split.
  if (inFlowFrame != parent->PrincipalChildList().FirstChild() ||
      inFlowFrame->LastContinuation()->GetNextSibling()) {
    return false;
  }

  // If the parent is the first or last part of the {ib} split, then
  // removing one of its kids will have no effect on the splitting.
  // Get the first continuation up front so we don't have to do it twice.
  nsIFrame* parentFirstContinuation = parent->FirstContinuation();
  if (!GetIBSplitSibling(parentFirstContinuation) ||
      !GetIBSplitPrevSibling(parentFirstContinuation)) {
    return false;
  }

  TRACE("IB split parent");
  ReframeContainingBlock(parent);
  return true;
#undef TRACE
}

void nsCSSFrameConstructor::UpdateTableCellSpans(nsIContent* aContent) {
  nsTableCellFrame* cellFrame = do_QueryFrame(aContent->GetPrimaryFrame());

  // It's possible that this warning could fire if some other style change
  // simultaneously changes the 'display' of the element and makes it no
  // longer be a table cell.
  NS_WARNING_ASSERTION(cellFrame, "Hint should only be posted on table cells!");

  if (cellFrame) {
    cellFrame->GetTableFrame()->RowOrColSpanChanged(cellFrame);
  }
}

static nsIContent* GetTopmostMathMLElement(nsIContent* aMathMLContent) {
  MOZ_ASSERT(aMathMLContent->IsMathMLElement());
  MOZ_ASSERT(aMathMLContent->GetPrimaryFrame());
  MOZ_ASSERT(aMathMLContent->GetPrimaryFrame()->IsMathMLFrame());
  nsIContent* root = aMathMLContent;

  for (nsIContent* parent = aMathMLContent->GetFlattenedTreeParent(); parent;
       parent = parent->GetFlattenedTreeParent()) {
    nsIFrame* frame = parent->GetPrimaryFrame();
    if (!frame || !frame->IsMathMLFrame()) {
      break;
    }
    root = parent;
  }

  return root;
}

// We don't know how to re-insert an anonymous subtree root, so recreate the
// closest non-generated ancestor instead, except for a few special cases...
static bool ShouldRecreateContainerForNativeAnonymousContentRoot(
    nsIContent* aContent) {
  if (!aContent->IsRootOfNativeAnonymousSubtree()) {
    return false;
  }
  if (ManualNACPtr::IsManualNAC(aContent)) {
    // Editor NAC, would enter an infinite loop, and we sorta get away with it
    // because it's all abspos.
    return false;
  }
  if (auto* el = Element::FromNode(aContent)) {
    if (auto* classes = el->GetClasses()) {
      if (classes->Contains(nsGkAtoms::mozCustomContentContainer,
                            eCaseMatters)) {
        // Canvas anonymous content (like the custom content container) is also
        // fine, because its only sibling is a tooltip which is also abspos, so
        // relative insertion order doesn't really matter.
        //
        // This is important because the inspector uses it, and we don't want
        // inspecting the page to change behavior heavily (and reframing
        // unfortunately has side-effects sometimes, even though they're bugs).
        return false;
      }
    }
  }

  return true;
}

void nsCSSFrameConstructor::RecreateFramesForContent(
    nsIContent* aContent, InsertionKind aInsertionKind) {
  MOZ_ASSERT(aContent);

  // If there is no document, we don't want to recreate frames for it.  (You
  // shouldn't generally be giving this method content without a document
  // anyway).
  // Rebuilding the frame tree can have bad effects, especially if it's the
  // frame tree for chrome (see bug 157322).
  if (NS_WARN_IF(!aContent->GetComposedDoc())) {
    return;
  }

  // TODO(emilio): We technically can find the right insertion point nowadays
  // using StyleChildrenIterator rather than FlattenedTreeIterator. But we'd
  // need to tweak the setup to insert into replaced elements to filter which
  // anonymous roots can be allowed, and which can't.
  //
  // TODO(emilio, 2022): Is this true? If we have a replaced element we wouldn't
  // have generated e.g., a ::before/::after pseudo-element to begin with (which
  // is what this code is about, so maybe we can just remove this piece of code
  // altogether).
  if (ShouldRecreateContainerForNativeAnonymousContentRoot(aContent)) {
    do {
      aContent = aContent->GetParent();
    } while (ShouldRecreateContainerForNativeAnonymousContentRoot(aContent));
    return RecreateFramesForContent(aContent, InsertionKind::Async);
  }

  nsIFrame* frame = aContent->GetPrimaryFrame();
  if (frame && frame->IsMathMLFrame()) {
    // Reframe the topmost MathML element to prevent exponential blowup
    // (see bug 397518).
    aContent = GetTopmostMathMLElement(aContent);
    frame = aContent->GetPrimaryFrame();
  }

  if (frame) {
    nsIFrame* parent = frame->GetParent();
    nsIContent* parentContent = parent ? parent->GetContent() : nullptr;
    // If the parent frame is a leaf then the subsequent insert will fail to
    // create a frame, so we need to recreate the parent content. This happens
    // with native anonymous content from the editor.
    if (parent && parent->IsLeaf() && parentContent &&
        parentContent != aContent) {
      return RecreateFramesForContent(parentContent, InsertionKind::Async);
    }
  }

  if (frame && MaybeRecreateContainerForFrameRemoval(frame)) {
    return;
  }

  MOZ_ASSERT(aContent->GetParentNode());

  // Remove the frames associated with the content object.
  nsIContent* nextSibling = aContent->IsRootOfNativeAnonymousSubtree()
                                ? nullptr
                                : aContent->GetNextSibling();
  bool didReconstruct =
      ContentRemoved(aContent, nextSibling, REMOVE_FOR_RECONSTRUCTION);

  if (!didReconstruct) {
    if (aInsertionKind == InsertionKind::Async && aContent->IsElement()) {
      // FIXME(emilio, bug 1397239): There's nothing removing the frame state
      // for elements that go away before we come back to the frame
      // constructor.
      //
      // Also, it'd be nice to just use the `ContentRangeInserted` path for
      // both elements and non-elements, but we need to make lazy frame
      // construction to apply to all elements first.
      RestyleManager()->PostRestyleEvent(aContent->AsElement(), RestyleHint{0},
                                         nsChangeHint_ReconstructFrame);
    } else {
      // Now, recreate the frames associated with this content object. If
      // ContentRemoved triggered reconstruction, then we don't need to do this
      // because the frames will already have been built.
      ContentRangeInserted(aContent, aContent->GetNextSibling(),
                           aInsertionKind);
    }
  }
}

bool nsCSSFrameConstructor::DestroyFramesFor(nsIContent* aContent) {
  MOZ_ASSERT(aContent && aContent->GetParentNode());

  nsIContent* nextSibling = aContent->IsRootOfNativeAnonymousSubtree()
                                ? nullptr
                                : aContent->GetNextSibling();

  return ContentRemoved(aContent, nextSibling, REMOVE_FOR_RECONSTRUCTION);
}

//////////////////////////////////////////////////////////////////////

// Block frame construction code

already_AddRefed<ComputedStyle> nsCSSFrameConstructor::GetFirstLetterStyle(
    nsIContent* aContent, ComputedStyle* aComputedStyle) {
  if (aContent) {
    return mPresShell->StyleSet()->ResolvePseudoElementStyle(
        *aContent->AsElement(), PseudoStyleType::firstLetter, nullptr,
        aComputedStyle);
  }
  return nullptr;
}

already_AddRefed<ComputedStyle> nsCSSFrameConstructor::GetFirstLineStyle(
    nsIContent* aContent, ComputedStyle* aComputedStyle) {
  if (aContent) {
    return mPresShell->StyleSet()->ResolvePseudoElementStyle(
        *aContent->AsElement(), PseudoStyleType::firstLine, nullptr,
        aComputedStyle);
  }
  return nullptr;
}

// Predicate to see if a given content (block element) has
// first-letter style applied to it.
bool nsCSSFrameConstructor::ShouldHaveFirstLetterStyle(
    nsIContent* aContent, ComputedStyle* aComputedStyle) {
  return nsLayoutUtils::HasPseudoStyle(aContent, aComputedStyle,
                                       PseudoStyleType::firstLetter,
                                       mPresShell->GetPresContext());
}

bool nsCSSFrameConstructor::HasFirstLetterStyle(nsIFrame* aBlockFrame) {
  MOZ_ASSERT(aBlockFrame, "Need a frame");
  NS_ASSERTION(aBlockFrame->IsBlockFrameOrSubclass(), "Not a block frame?");

  return aBlockFrame->HasAnyStateBits(NS_BLOCK_HAS_FIRST_LETTER_STYLE);
}

bool nsCSSFrameConstructor::ShouldHaveFirstLineStyle(
    nsIContent* aContent, ComputedStyle* aComputedStyle) {
  bool hasFirstLine = nsLayoutUtils::HasPseudoStyle(
      aContent, aComputedStyle, PseudoStyleType::firstLine,
      mPresShell->GetPresContext());
  return hasFirstLine && !aContent->IsHTMLElement(nsGkAtoms::fieldset);
}

void nsCSSFrameConstructor::ShouldHaveSpecialBlockStyle(
    nsIContent* aContent, ComputedStyle* aComputedStyle,
    bool* aHaveFirstLetterStyle, bool* aHaveFirstLineStyle) {
  *aHaveFirstLetterStyle = ShouldHaveFirstLetterStyle(aContent, aComputedStyle);
  *aHaveFirstLineStyle = ShouldHaveFirstLineStyle(aContent, aComputedStyle);
}

/* static */
const nsCSSFrameConstructor::PseudoParentData
    nsCSSFrameConstructor::sPseudoParentData[eParentTypeCount] = {
        // Cell
        {{&nsCSSFrameConstructor::ConstructTableCell,
          FCDATA_IS_TABLE_PART | FCDATA_SKIP_FRAMESET | FCDATA_USE_CHILD_ITEMS |
              FCDATA_IS_WRAPPER_ANON_BOX |
              FCDATA_DESIRED_PARENT_TYPE_TO_BITS(eTypeRow)},
         PseudoStyleType::tableCell},
        // Row
        {{&nsCSSFrameConstructor::ConstructTableRowOrRowGroup,
          FCDATA_IS_TABLE_PART | FCDATA_SKIP_FRAMESET | FCDATA_USE_CHILD_ITEMS |
              FCDATA_IS_WRAPPER_ANON_BOX |
              FCDATA_DESIRED_PARENT_TYPE_TO_BITS(eTypeRowGroup)},
         PseudoStyleType::tableRow},
        // Row group
        {{&nsCSSFrameConstructor::ConstructTableRowOrRowGroup,
          FCDATA_IS_TABLE_PART | FCDATA_SKIP_FRAMESET | FCDATA_USE_CHILD_ITEMS |
              FCDATA_IS_WRAPPER_ANON_BOX |
              FCDATA_DESIRED_PARENT_TYPE_TO_BITS(eTypeTable)},
         PseudoStyleType::tableRowGroup},
        // Column group
        {{ToCreationFunc(NS_NewTableColGroupFrame),
          FCDATA_IS_TABLE_PART | FCDATA_SKIP_FRAMESET |
              FCDATA_DISALLOW_OUT_OF_FLOW | FCDATA_USE_CHILD_ITEMS |
              FCDATA_SKIP_ABSPOS_PUSH |
              // Not FCDATA_IS_WRAPPER_ANON_BOX, because we don't need to
              // restyle these: they have non-inheriting styles.
              FCDATA_DESIRED_PARENT_TYPE_TO_BITS(eTypeTable)},
         PseudoStyleType::tableColGroup},
        // Table
        {{&nsCSSFrameConstructor::ConstructTable,
          FCDATA_SKIP_FRAMESET | FCDATA_USE_CHILD_ITEMS |
              FCDATA_IS_WRAPPER_ANON_BOX},
         PseudoStyleType::table},
        // Ruby
        {{ToCreationFunc(NS_NewRubyFrame),
          FCDATA_IS_LINE_PARTICIPANT | FCDATA_USE_CHILD_ITEMS |
              FCDATA_IS_WRAPPER_ANON_BOX | FCDATA_SKIP_FRAMESET},
         PseudoStyleType::ruby},
        // Ruby Base
        {{ToCreationFunc(NS_NewRubyBaseFrame),
          FCDATA_USE_CHILD_ITEMS | FCDATA_IS_LINE_PARTICIPANT |
              FCDATA_IS_WRAPPER_ANON_BOX |
              FCDATA_DESIRED_PARENT_TYPE_TO_BITS(eTypeRubyBaseContainer) |
              FCDATA_SKIP_FRAMESET},
         PseudoStyleType::rubyBase},
        // Ruby Base Container
        {{ToCreationFunc(NS_NewRubyBaseContainerFrame),
          FCDATA_USE_CHILD_ITEMS | FCDATA_IS_LINE_PARTICIPANT |
              FCDATA_IS_WRAPPER_ANON_BOX |
              FCDATA_DESIRED_PARENT_TYPE_TO_BITS(eTypeRuby) |
              FCDATA_SKIP_FRAMESET},
         PseudoStyleType::rubyBaseContainer},
        // Ruby Text
        {{ToCreationFunc(NS_NewRubyTextFrame),
          FCDATA_USE_CHILD_ITEMS | FCDATA_IS_LINE_PARTICIPANT |
              FCDATA_IS_WRAPPER_ANON_BOX |
              FCDATA_DESIRED_PARENT_TYPE_TO_BITS(eTypeRubyTextContainer) |
              FCDATA_SKIP_FRAMESET},
         PseudoStyleType::rubyText},
        // Ruby Text Container
        {{ToCreationFunc(NS_NewRubyTextContainerFrame),
          FCDATA_USE_CHILD_ITEMS | FCDATA_IS_WRAPPER_ANON_BOX |
              FCDATA_DESIRED_PARENT_TYPE_TO_BITS(eTypeRuby) |
              FCDATA_SKIP_FRAMESET},
         PseudoStyleType::rubyTextContainer}};

void nsCSSFrameConstructor::CreateNeededAnonFlexOrGridItems(
    nsFrameConstructorState& aState, FrameConstructionItemList& aItems,
    nsIFrame* aParentFrame) {
  if (aItems.IsEmpty()) {
    return;
  }

  if (!aParentFrame->IsFlexOrGridContainer()) {
    return;
  }

  const bool isLegacyWebKitBox =
      IsFlexContainerForLegacyWebKitBox(aParentFrame);
  FCItemIterator iter(aItems);
  do {
    // Advance iter past children that don't want to be wrapped
    if (iter.SkipItemsThatDontNeedAnonFlexOrGridItem(aState,
                                                     isLegacyWebKitBox)) {
      // Hit the end of the items without finding any remaining children that
      // need to be wrapped. We're finished!
      return;
    }

    // If our next potentially-wrappable child is whitespace, then see if
    // there's anything wrappable immediately after it. If not, we just drop
    // the whitespace and move on. (We're not supposed to create any anonymous
    // flex/grid items that _only_ contain whitespace).
    // (BUT if this is generated content, then we don't give whitespace nodes
    // any special treatment, because they're probably not really whitespace --
    // they're just temporarily empty, waiting for their generated text.)
    // XXXdholbert If this node's generated text will *actually end up being
    // entirely whitespace*, then we technically should still skip over it, per
    // the CSS grid & flexbox specs. I'm not bothering with that at this point,
    // since it's a pretty extreme edge case.
    if (!aParentFrame->IsGeneratedContentFrame() &&
        iter.item().IsWhitespace(aState)) {
      FCItemIterator afterWhitespaceIter(iter);
      bool hitEnd = afterWhitespaceIter.SkipWhitespace(aState);
      bool nextChildNeedsAnonItem =
          !hitEnd && afterWhitespaceIter.item().NeedsAnonFlexOrGridItem(
                         aState, isLegacyWebKitBox);

      if (!nextChildNeedsAnonItem) {
        // There's nothing after the whitespace that we need to wrap, so we
        // just drop this run of whitespace.
        iter.DeleteItemsTo(this, afterWhitespaceIter);
        if (hitEnd) {
          // Nothing left to do -- we're finished!
          return;
        }
        // else, we have a next child and it does not want to be wrapped.  So,
        // we jump back to the beginning of the loop to skip over that child
        // (and anything else non-wrappable after it)
        MOZ_ASSERT(!iter.IsDone() && !iter.item().NeedsAnonFlexOrGridItem(
                                         aState, isLegacyWebKitBox),
                   "hitEnd and/or nextChildNeedsAnonItem lied");
        continue;
      }
    }

    // Now |iter| points to the first child that needs to be wrapped in an
    // anonymous flex/grid item. Now we see how many children after it also want
    // to be wrapped in an anonymous flex/grid item.
    FCItemIterator endIter(iter);  // iterator to find the end of the group
    endIter.SkipItemsThatNeedAnonFlexOrGridItem(aState, isLegacyWebKitBox);

    NS_ASSERTION(iter != endIter,
                 "Should've had at least one wrappable child to seek past");

    // Now, we create the anonymous flex or grid item to contain the children
    // between |iter| and |endIter|.
    nsIContent* parentContent = aParentFrame->GetContent();
    RefPtr<ComputedStyle> wrapperStyle =
        mPresShell->StyleSet()->ResolveInheritingAnonymousBoxStyle(
            PseudoStyleType::anonymousItem, aParentFrame->Style());

    static constexpr FrameConstructionData sBlockFCData(
        ToCreationFunc(NS_NewBlockFrame), FCDATA_SKIP_FRAMESET |
                                              FCDATA_USE_CHILD_ITEMS |
                                              FCDATA_IS_WRAPPER_ANON_BOX);

    // Use the content of our parent frame
    auto* newItem = new (this) FrameConstructionItem(
        &sBlockFCData, parentContent, wrapperStyle.forget(), true);

    newItem->mIsAllInline =
        newItem->mComputedStyle->StyleDisplay()->IsInlineOutsideStyle();
    newItem->mIsBlock = !newItem->mIsAllInline;

    MOZ_ASSERT(!newItem->mIsAllInline && newItem->mIsBlock,
               "expecting anonymous flex/grid items to be block-level "
               "(this will make a difference when we encounter "
               "'align-items: baseline')");

    // Anonymous flex and grid items induce line boundaries around their
    // contents.
    newItem->mChildItems.SetLineBoundaryAtStart(true);
    newItem->mChildItems.SetLineBoundaryAtEnd(true);
    // The parent of the items in aItems is also the parent of the items
    // in mChildItems
    newItem->mChildItems.SetParentHasNoShadowDOM(aItems.ParentHasNoShadowDOM());

    // Eat up all items between |iter| and |endIter| and put them in our
    // wrapper. This advances |iter| to point to |endIter|.
    iter.AppendItemsToList(this, endIter, newItem->mChildItems);

    iter.InsertItem(newItem);
  } while (!iter.IsDone());
}

/* static */ nsCSSFrameConstructor::RubyWhitespaceType
nsCSSFrameConstructor::ComputeRubyWhitespaceType(StyleDisplay aPrevDisplay,
                                                 StyleDisplay aNextDisplay) {
  MOZ_ASSERT(aPrevDisplay.IsRuby() && aNextDisplay.IsRuby());
  if (aPrevDisplay == aNextDisplay &&
      (aPrevDisplay == StyleDisplay::RubyBase ||
       aPrevDisplay == StyleDisplay::RubyText)) {
    return eRubyInterLeafWhitespace;
  }
  if (aNextDisplay == StyleDisplay::RubyText ||
      aNextDisplay == StyleDisplay::RubyTextContainer) {
    return eRubyInterLevelWhitespace;
  }
  return eRubyInterSegmentWhitespace;
}

/**
 * This function checks the content from |aStartIter| to |aEndIter|,
 * determines whether it contains only whitespace, and if yes,
 * interprets the type of whitespace. This method does not change
 * any of the iters.
 */
/* static */ nsCSSFrameConstructor::RubyWhitespaceType
nsCSSFrameConstructor::InterpretRubyWhitespace(nsFrameConstructorState& aState,
                                               const FCItemIterator& aStartIter,
                                               const FCItemIterator& aEndIter) {
  if (!aStartIter.item().IsWhitespace(aState)) {
    return eRubyNotWhitespace;
  }

  FCItemIterator spaceEndIter(aStartIter);
  spaceEndIter.SkipWhitespace(aState);
  if (spaceEndIter != aEndIter) {
    return eRubyNotWhitespace;
  }

  // Any leading or trailing whitespace in non-pseudo ruby box
  // should have been trimmed, hence there should not be any
  // whitespace at the start or the end.
  MOZ_ASSERT(!aStartIter.AtStart() && !aEndIter.IsDone());
  FCItemIterator prevIter(aStartIter);
  prevIter.Prev();
  return ComputeRubyWhitespaceType(
      prevIter.item().mComputedStyle->StyleDisplay()->mDisplay,
      aEndIter.item().mComputedStyle->StyleDisplay()->mDisplay);
}

/**
 * This function eats up consecutive items which do not want the current
 * parent into either a ruby base box or a ruby text box.  When it
 * returns, |aIter| points to the first item it doesn't wrap.
 */
void nsCSSFrameConstructor::WrapItemsInPseudoRubyLeafBox(
    FCItemIterator& aIter, ComputedStyle* aParentStyle,
    nsIContent* aParentContent) {
  StyleDisplay parentDisplay = aParentStyle->StyleDisplay()->mDisplay;
  ParentType parentType, wrapperType;
  if (parentDisplay == StyleDisplay::RubyTextContainer) {
    parentType = eTypeRubyTextContainer;
    wrapperType = eTypeRubyText;
  } else {
    MOZ_ASSERT(parentDisplay == StyleDisplay::RubyBaseContainer);
    parentType = eTypeRubyBaseContainer;
    wrapperType = eTypeRubyBase;
  }

  MOZ_ASSERT(aIter.item().DesiredParentType() != parentType,
             "Should point to something needs to be wrapped.");

  FCItemIterator endIter(aIter);
  endIter.SkipItemsNotWantingParentType(parentType);

  WrapItemsInPseudoParent(aParentContent, aParentStyle, wrapperType, aIter,
                          endIter);
}

/**
 * This function eats up consecutive items into a ruby level container.
 * It may create zero or one level container. When it returns, |aIter|
 * points to the first item it doesn't wrap.
 */
void nsCSSFrameConstructor::WrapItemsInPseudoRubyLevelContainer(
    nsFrameConstructorState& aState, FCItemIterator& aIter,
    ComputedStyle* aParentStyle, nsIContent* aParentContent) {
  MOZ_ASSERT(aIter.item().DesiredParentType() != eTypeRuby,
             "Pointing to a level container?");

  FrameConstructionItem& firstItem = aIter.item();
  ParentType wrapperType = firstItem.DesiredParentType();
  if (wrapperType != eTypeRubyTextContainer) {
    // If the first item is not ruby text,
    // it should be in a base container.
    wrapperType = eTypeRubyBaseContainer;
  }

  FCItemIterator endIter(aIter);
  do {
    if (endIter.SkipItemsWantingParentType(wrapperType) ||
        // If the skipping above stops at some item which wants a
        // different ruby parent, then we have finished.
        IsRubyParentType(endIter.item().DesiredParentType())) {
      // No more items need to be wrapped in this level container.
      break;
    }

    FCItemIterator contentEndIter(endIter);
    contentEndIter.SkipItemsNotWantingRubyParent();
    // endIter must be on something doesn't want a ruby parent.
    MOZ_ASSERT(contentEndIter != endIter);

    // InterpretRubyWhitespace depends on the fact that any leading or
    // trailing whitespace described in the spec have been trimmed at
    // this point. With this precondition, it is safe not to check
    // whether contentEndIter has been done.
    RubyWhitespaceType whitespaceType =
        InterpretRubyWhitespace(aState, endIter, contentEndIter);
    if (whitespaceType == eRubyInterLevelWhitespace) {
      // Remove inter-level whitespace.
      bool atStart = (aIter == endIter);
      endIter.DeleteItemsTo(this, contentEndIter);
      if (atStart) {
        aIter = endIter;
      }
    } else if (whitespaceType == eRubyInterSegmentWhitespace) {
      // If this level container starts with inter-segment whitespaces,
      // wrap them. Break at contentEndIter. Otherwise, leave it here.
      // Break at endIter. They will be wrapped when we are here again.
      if (aIter == endIter) {
        MOZ_ASSERT(wrapperType == eTypeRubyBaseContainer,
                   "Inter-segment whitespace should be wrapped in rbc");
        endIter = contentEndIter;
      }
      break;
    } else if (wrapperType == eTypeRubyTextContainer &&
               whitespaceType != eRubyInterLeafWhitespace) {
      // Misparented inline content that's not inter-annotation
      // whitespace doesn't belong in a pseudo ruby text container.
      // Break at endIter.
      break;
    } else {
      endIter = contentEndIter;
    }
  } while (!endIter.IsDone());

  // It is possible that everything our parent wants us to wrap is
  // simply an inter-level whitespace, which has been trimmed, or
  // an inter-segment whitespace, which will be wrapped later.
  // In those cases, don't create anything.
  if (aIter != endIter) {
    WrapItemsInPseudoParent(aParentContent, aParentStyle, wrapperType, aIter,
                            endIter);
  }
}

/**
 * This function trims leading and trailing whitespaces
 * in the given item list.
 */
void nsCSSFrameConstructor::TrimLeadingAndTrailingWhitespaces(
    nsFrameConstructorState& aState, FrameConstructionItemList& aItems) {
  FCItemIterator iter(aItems);
  if (!iter.IsDone() && iter.item().IsWhitespace(aState)) {
    FCItemIterator spaceEndIter(iter);
    spaceEndIter.SkipWhitespace(aState);
    iter.DeleteItemsTo(this, spaceEndIter);
  }

  iter.SetToEnd();
  if (!iter.AtStart()) {
    FCItemIterator spaceEndIter(iter);
    do {
      iter.Prev();
      if (iter.AtStart()) {
        // It's fine to not check the first item, because we
        // should have trimmed leading whitespaces above.
        break;
      }
    } while (iter.item().IsWhitespace(aState));
    iter.Next();
    if (iter != spaceEndIter) {
      iter.DeleteItemsTo(this, spaceEndIter);
    }
  }
}

/**
 * This function walks through the child list (aItems) and creates
 * needed pseudo ruby boxes to wrap misparented children.
 */
void nsCSSFrameConstructor::CreateNeededPseudoInternalRubyBoxes(
    nsFrameConstructorState& aState, FrameConstructionItemList& aItems,
    nsIFrame* aParentFrame) {
  const ParentType ourParentType = GetParentType(aParentFrame);
  if (!IsRubyParentType(ourParentType) ||
      aItems.AllWantParentType(ourParentType)) {
    return;
  }

  if (!IsRubyPseudo(aParentFrame) ||
      ourParentType == eTypeRuby /* for 'display:block ruby' */) {
    // Normally, ruby pseudo frames start from and end at some elements,
    // which means they don't have leading and trailing whitespaces at
    // all.  But there are two cases where they do actually have leading
    // or trailing whitespaces:
    // 1. It is an inter-segment whitespace which in an individual ruby
    //    base container.
    // 2. The pseudo frame starts from or ends at consecutive inline
    //    content, which is not pure whitespace, but includes some.
    // In either case, the whitespaces are not the leading or trailing
    // whitespaces defined in the spec, and thus should not be trimmed.
    TrimLeadingAndTrailingWhitespaces(aState, aItems);
  }

  FCItemIterator iter(aItems);
  nsIContent* parentContent = aParentFrame->GetContent();
  ComputedStyle* parentStyle = aParentFrame->Style();
  while (!iter.IsDone()) {
    if (!iter.SkipItemsWantingParentType(ourParentType)) {
      if (ourParentType == eTypeRuby) {
        WrapItemsInPseudoRubyLevelContainer(aState, iter, parentStyle,
                                            parentContent);
      } else {
        WrapItemsInPseudoRubyLeafBox(iter, parentStyle, parentContent);
      }
    }
  }
}

/*
 * This function works as follows: we walk through the child list (aItems) and
 * find items that cannot have aParentFrame as their parent.  We wrap
 * continuous runs of such items into a FrameConstructionItem for a frame that
 * gets them closer to their desired parents.  For example, a run of non-row
 * children of a row-group will get wrapped in a row.  When we later construct
 * the frame for this wrapper (in this case for the row), it'll be the correct
 * parent for the cells in the set of items we wrapped or we'll wrap cells
 * around everything else.  At the end of this method, aItems is guaranteed to
 * contain only items for frames that can be direct kids of aParentFrame.
 */
void nsCSSFrameConstructor::CreateNeededPseudoContainers(
    nsFrameConstructorState& aState, FrameConstructionItemList& aItems,
    nsIFrame* aParentFrame) {
  ParentType ourParentType = GetParentType(aParentFrame);
  if (IsRubyParentType(ourParentType) ||
      aItems.AllWantParentType(ourParentType)) {
    // Nothing to do here
    return;
  }

  FCItemIterator iter(aItems);
  do {
    if (iter.SkipItemsWantingParentType(ourParentType)) {
      // Nothing else to do here; we're finished
      return;
    }

    // Now we're pointing to the first child that wants a different parent
    // type.

    // Now try to figure out what kids we can group together.  We can generally
    // group everything that has a different desired parent type from us.  Two
    // exceptions to this:
    // 1) If our parent type is table, we can't group columns with anything
    //    else other than whitespace.
    // 2) Whitespace that lies between two things we can group which both want
    //    a non-block parent should be dropped, even if we can't group them
    //    with each other and even if the whitespace wants a parent of
    //    ourParentType.  Ends of the list count as things that don't want a
    //    block parent (so that for example we'll drop a whitespace-only list).

    FCItemIterator endIter(iter); /* iterator to find the end of the group */
    ParentType groupingParentType = endIter.item().DesiredParentType();
    if (aItems.AllWantParentType(groupingParentType) &&
        groupingParentType != eTypeBlock) {
      // Just group them all and be done with it.  We need the check for
      // eTypeBlock here to catch the "all the items are whitespace" case
      // described above.
      endIter.SetToEnd();
    } else {
      // Locate the end of the group.

      // Keep track of the type the previous item wanted, in case we have to
      // deal with whitespace.  Start it off with ourParentType, since that's
      // the last thing |iter| would have skipped over.
      ParentType prevParentType = ourParentType;
      do {
        // Walk an iterator past any whitespace that we might be able to drop
        // from the list
        FCItemIterator spaceEndIter(endIter);
        if (prevParentType != eTypeBlock &&
            !aParentFrame->IsGeneratedContentFrame() &&
            spaceEndIter.item().IsWhitespace(aState)) {
          bool trailingSpaces = spaceEndIter.SkipWhitespace(aState);

          // We drop the whitespace in the following cases:
          // 1) If these are not trailing spaces and the next item wants a table
          //    or table-part parent
          // 2) If these are trailing spaces and aParentFrame is a
          //    tabular container according to rule 1.3 of CSS 2.1 Sec 17.2.1.
          //    (Being a tabular container pretty much means ourParentType is
          //    not eTypeBlock besides the eTypeColGroup case, which won't
          //    reach here.)
          if ((!trailingSpaces &&
               IsTableParentType(spaceEndIter.item().DesiredParentType())) ||
              (trailingSpaces && ourParentType != eTypeBlock)) {
            bool updateStart = (iter == endIter);
            endIter.DeleteItemsTo(this, spaceEndIter);
            NS_ASSERTION(trailingSpaces == endIter.IsDone(),
                         "These should match");

            if (updateStart) {
              iter = endIter;
            }

            if (trailingSpaces) {
              break; /* Found group end */
            }

            if (updateStart) {
              // Update groupingParentType, since it might have been eTypeBlock
              // just because of the whitespace.
              groupingParentType = iter.item().DesiredParentType();
            }
          }
        }

        // Now endIter points to a non-whitespace item or a non-droppable
        // whitespace item. In the latter case, if this is the end of the group
        // we'll traverse this whitespace again.  But it'll all just be quick
        // DesiredParentType() checks which will match ourParentType (that's
        // what it means that this is the group end), so it's OK.
        // However, when we are grouping a ruby parent, and endIter points to
        // a non-droppable whitespace, if the next non-whitespace item also
        // wants a ruby parent, the whitespace should also be included into
        // the current ruby container.
        prevParentType = endIter.item().DesiredParentType();
        if (prevParentType == ourParentType &&
            (endIter == spaceEndIter || spaceEndIter.IsDone() ||
             !IsRubyParentType(groupingParentType) ||
             !IsRubyParentType(spaceEndIter.item().DesiredParentType()))) {
          // End the group at endIter.
          break;
        }

        if (ourParentType == eTypeTable &&
            (prevParentType == eTypeColGroup) !=
                (groupingParentType == eTypeColGroup)) {
          // Either we started with columns and now found something else, or
          // vice versa.  In any case, end the grouping.
          break;
        }

        // If we have some whitespace that we were not able to drop and there is
        // an item after the whitespace that is already properly parented, then
        // make sure to include the spaces in our group but stop the group after
        // that.
        if (spaceEndIter != endIter && !spaceEndIter.IsDone() &&
            ourParentType == spaceEndIter.item().DesiredParentType()) {
          endIter = spaceEndIter;
          break;
        }

        // Include the whitespace we didn't drop (if any) in the group.
        endIter = spaceEndIter;
        prevParentType = endIter.item().DesiredParentType();

        endIter.Next();
      } while (!endIter.IsDone());
    }

    if (iter == endIter) {
      // Nothing to wrap here; just skipped some whitespace
      continue;
    }

    // Now group together all the items between iter and endIter.  The right
    // parent type to use depends on ourParentType.
    ParentType wrapperType;
    switch (ourParentType) {
      case eTypeRow:
        // The parent type for a cell is eTypeBlock, since that's what a cell
        // looks like to its kids.
        wrapperType = eTypeBlock;
        break;
      case eTypeRowGroup:
        wrapperType = eTypeRow;
        break;
      case eTypeTable:
        // Either colgroup or rowgroup, depending on what we're grouping.
        wrapperType =
            groupingParentType == eTypeColGroup ? eTypeColGroup : eTypeRowGroup;
        break;
      case eTypeColGroup:
        MOZ_CRASH("Colgroups should be suppresing non-col child items");
      default:
        NS_ASSERTION(ourParentType == eTypeBlock, "Unrecognized parent type");
        if (IsRubyParentType(groupingParentType)) {
          wrapperType = eTypeRuby;
        } else {
          NS_ASSERTION(IsTableParentType(groupingParentType),
                       "groupingParentType should be either Ruby or table");
          wrapperType = eTypeTable;
        }
    }

    ComputedStyle* parentStyle = aParentFrame->Style();
    WrapItemsInPseudoParent(aParentFrame->GetContent(), parentStyle,
                            wrapperType, iter, endIter);

    // Now |iter| points to the item that was the first one we didn't wrap;
    // loop and see whether we need to skip it or wrap it in something
    // different.
  } while (!iter.IsDone());
}

/**
 * This method wraps frame construction item from |aIter| to
 * |aEndIter|. After it returns, aIter points to the first item
 * after the wrapper.
 */
void nsCSSFrameConstructor::WrapItemsInPseudoParent(
    nsIContent* aParentContent, ComputedStyle* aParentStyle,
    ParentType aWrapperType, FCItemIterator& aIter,
    const FCItemIterator& aEndIter) {
  const PseudoParentData& pseudoData = sPseudoParentData[aWrapperType];
  PseudoStyleType pseudoType = pseudoData.mPseudoType;
  auto& parentDisplay = *aParentStyle->StyleDisplay();
  auto parentDisplayInside = parentDisplay.DisplayInside();

  // XXXmats should we use IsInlineInsideStyle() here instead? seems odd to
  // exclude RubyBaseContainer/RubyTextContainer...
  if (pseudoType == PseudoStyleType::table &&
      (parentDisplay.IsInlineFlow() ||
       parentDisplayInside == StyleDisplayInside::RubyBase ||
       parentDisplayInside == StyleDisplayInside::RubyText)) {
    pseudoType = PseudoStyleType::inlineTable;
  }

  RefPtr<ComputedStyle> wrapperStyle;
  if (pseudoData.mFCData.mBits & FCDATA_IS_WRAPPER_ANON_BOX) {
    wrapperStyle = mPresShell->StyleSet()->ResolveInheritingAnonymousBoxStyle(
        pseudoType, aParentStyle);
  } else {
    wrapperStyle =
        mPresShell->StyleSet()->ResolveNonInheritingAnonymousBoxStyle(
            pseudoType);
  }

  // Use the content of our parent frame
  auto* newItem = new (this) FrameConstructionItem(
      &pseudoData.mFCData, aParentContent, wrapperStyle.forget(), true);

  const nsStyleDisplay* disp = newItem->mComputedStyle->StyleDisplay();
  // Here we're cheating a tad... technically, table-internal items should be
  // inline if aParentFrame is inline, but they'll get wrapped in an
  // inline-table in the end, so it'll all work out.  In any case, arguably
  // we don't need to maintain this state at this point... but it's better
  // to, I guess.
  newItem->mIsAllInline = disp->IsInlineOutsideStyle();

  bool isRuby = disp->IsRubyDisplayType();
  if (!isRuby) {
    // Table pseudo frames always induce line boundaries around their
    // contents.
    newItem->mChildItems.SetLineBoundaryAtStart(true);
    newItem->mChildItems.SetLineBoundaryAtEnd(true);
  }
  // The parent of the items in aItems is also the parent of the items
  // in mChildItems
  newItem->mChildItems.SetParentHasNoShadowDOM(
      aIter.List()->ParentHasNoShadowDOM());

  // Eat up all items between |aIter| and |aEndIter| and put them in our
  // wrapper Advances |aIter| to point to |aEndIter|.
  aIter.AppendItemsToList(this, aEndIter, newItem->mChildItems);

  aIter.InsertItem(newItem);
}

void nsCSSFrameConstructor::CreateNeededPseudoSiblings(
    nsFrameConstructorState& aState, FrameConstructionItemList& aItems,
    nsIFrame* aParentFrame) {
  if (aItems.IsEmpty() || GetParentType(aParentFrame) != eTypeRuby) {
    return;
  }

  FCItemIterator iter(aItems);
  StyleDisplay firstDisplay =
      iter.item().mComputedStyle->StyleDisplay()->mDisplay;
  if (firstDisplay == StyleDisplay::RubyBaseContainer) {
    return;
  }
  NS_ASSERTION(firstDisplay == StyleDisplay::RubyTextContainer,
               "Child of ruby frame should either a rbc or a rtc");

  const PseudoParentData& pseudoData =
      sPseudoParentData[eTypeRubyBaseContainer];
  RefPtr<ComputedStyle> pseudoStyle =
      mPresShell->StyleSet()->ResolveInheritingAnonymousBoxStyle(
          pseudoData.mPseudoType, aParentFrame->Style());
  FrameConstructionItem* newItem = new (this) FrameConstructionItem(
      &pseudoData.mFCData,
      // Use the content of the parent frame
      aParentFrame->GetContent(), pseudoStyle.forget(), true);
  newItem->mIsAllInline = true;
  newItem->mChildItems.SetParentHasNoShadowDOM(true);
  iter.InsertItem(newItem);
}

#ifdef DEBUG
/**
 * Returns true iff aFrame should be wrapped in an anonymous flex/grid item,
 * rather than being a direct child of aContainerFrame.
 *
 * NOTE: aContainerFrame must be a flex or grid container - this function is
 * purely for sanity-checking the children of these container types.
 * NOTE: See also NeedsAnonFlexOrGridItem(), for the non-debug version of this
 * logic (which operates a bit earlier, on FCData instead of frames).
 */
static bool FrameWantsToBeInAnonymousItem(const nsIFrame* aContainerFrame,
                                          const nsIFrame* aFrame) {
  MOZ_ASSERT(aContainerFrame->IsFlexOrGridContainer());

  // Any line-participant frames (e.g. text) definitely want to be wrapped in
  // an anonymous flex/grid item.
  if (aFrame->IsLineParticipant()) {
    return true;
  }

  // If the container is a -webkit-{inline-}box container, then placeholders
  // also need to be wrapped, for compatibility.
  if (IsFlexContainerForLegacyWebKitBox(aContainerFrame) &&
      aFrame->IsPlaceholderFrame()) {
    return true;
  }

  return false;
}
#endif

static void VerifyGridFlexContainerChildren(nsIFrame* aParentFrame,
                                            const nsFrameList& aChildren) {
#ifdef DEBUG
  if (!aParentFrame->IsFlexOrGridContainer()) {
    return;
  }

  bool prevChildWasAnonItem = false;
  for (const nsIFrame* child : aChildren) {
    MOZ_ASSERT(!FrameWantsToBeInAnonymousItem(aParentFrame, child),
               "frame wants to be inside an anonymous item, but it isn't");
    if (IsAnonymousItem(child)) {
      AssertAnonymousFlexOrGridItemParent(child, aParentFrame);
      MOZ_ASSERT(!prevChildWasAnonItem, "two anon items in a row");
      nsIFrame* firstWrappedChild = child->PrincipalChildList().FirstChild();
      MOZ_ASSERT(firstWrappedChild, "anonymous item shouldn't be empty");
      prevChildWasAnonItem = true;
    } else {
      prevChildWasAnonItem = false;
    }
  }
#endif
}

static bool FrameHasOnlyPlaceholderPrevSiblings(const nsIFrame* aFrame) {
  // Check for prev siblings, ignoring placeholder frames.
  MOZ_ASSERT(aFrame, "frame must not be null");
  const nsIFrame* prevSibling = aFrame;
  do {
    prevSibling = prevSibling->GetPrevSibling();
  } while (prevSibling && prevSibling->IsPlaceholderFrame());
  return !prevSibling;
}

static bool FrameHasOnlyPlaceholderNextSiblings(const nsIFrame* aFrame) {
  // Check for next siblings, ignoring placeholder frames.
  MOZ_ASSERT(aFrame, "frame must not be null");
  const nsIFrame* nextSibling = aFrame;
  do {
    nextSibling = nextSibling->GetNextSibling();
  } while (nextSibling && nextSibling->IsPlaceholderFrame());
  return !nextSibling;
}

static void SetPageValues(nsIFrame* const aFrame,
                          const nsAtom* const aAutoValue,
                          const nsAtom* const aStartValue,
                          const nsAtom* const aEndValue) {
  MOZ_ASSERT(aAutoValue, "Auto page value should never be null");
  MOZ_ASSERT(aStartValue || aEndValue, "Should not have called with no values");
  nsIFrame::PageValues* pageValues =
      aFrame->GetProperty(nsIFrame::PageValuesProperty());

  if (aStartValue) {
    if (aStartValue == aAutoValue) {
      // If the page value struct already exists, set the start value to null
      // to indicate the auto value.
      if (pageValues) {
        pageValues->mStartPageValue = nullptr;
      }
    } else {
      // The start value is not auto, so we need to store it, creating the
      // page values struct if it does not already exist.
      if (!pageValues) {
        pageValues = new nsIFrame::PageValues();
        aFrame->SetProperty(nsIFrame::PageValuesProperty(), pageValues);
      }
      pageValues->mStartPageValue = aStartValue;
    }
  }
  if (aEndValue) {
    if (aEndValue == aAutoValue) {
      // If the page value struct already exists, set the end value to null
      // to indicate the auto value.
      if (pageValues) {
        pageValues->mEndPageValue = nullptr;
      }
    } else {
      // The end value is not auto, so we need to store it, creating the
      // page values struct if it does not already exist.
      if (!pageValues) {
        pageValues = new nsIFrame::PageValues();
        aFrame->SetProperty(nsIFrame::PageValuesProperty(), pageValues);
      }
      pageValues->mEndPageValue = aEndValue;
    }
  }
}

inline void nsCSSFrameConstructor::ConstructFramesFromItemList(
    nsFrameConstructorState& aState, FrameConstructionItemList& aItems,
    nsContainerFrame* aParentFrame, bool aParentIsWrapperAnonBox,
    nsFrameList& aFrameList) {
#ifdef DEBUG
  if (aParentFrame->StyleContent()->mContent.IsNone() &&
      StaticPrefs::layout_css_element_content_none_enabled()) {
    for (FCItemIterator iter(aItems); !iter.IsDone(); iter.Next()) {
      MOZ_ASSERT(iter.item().mContent->IsInNativeAnonymousSubtree() ||
                 iter.item().mComputedStyle->IsPseudoOrAnonBox());
    }
  }

  // The assertion condition should match the logic in
  // MaybePushFloatContainingBlock().
  MOZ_ASSERT(!(ShouldSuppressFloatingOfDescendants(aParentFrame) ||
               aParentFrame->IsFloatContainingBlock()) ||
                 aState.mFloatCBCandidate == aParentFrame,
             "Our caller or ProcessChildren()'s caller should call "
             "MaybePushFloatContainingBlock() to handle the float containing "
             "block candidate!");
  aState.mFloatCBCandidate = nullptr;
#endif

  // Ensure aParentIsWrapperAnonBox is correct.  We _could_ compute it directly,
  // but it would be a bit slow, which is why we pass it from callers, who have
  // that information offhand in many cases.
  MOZ_ASSERT(ParentIsWrapperAnonBox(aParentFrame) == aParentIsWrapperAnonBox);

  // Note: we explicitly exclude TableColGroupFrame because it doesn't
  // have the FCDATA_IS_WRAPPER_ANON_BOX on pseudos so aParentIsWrapperAnonBox
  // is false for such pseudos (see sPseudoParentData below).
  if (!aParentIsWrapperAnonBox && aState.mHasRenderedLegend &&
      aParentFrame->GetContent()->IsHTMLElement(nsGkAtoms::fieldset) &&
      !aParentFrame->IsTableColGroupFrame()) {
    DebugOnly<bool> found = false;
    for (FCItemIterator iter(aItems); !iter.IsDone(); iter.Next()) {
      if (iter.item().mIsRenderedLegend) {
        // This makes the rendered legend the first frame in the fieldset child
        // list which makes keyboard traversal follow the visual order.
        nsFieldSetFrame* fieldSetFrame = GetFieldSetFrameFor(aParentFrame);
        nsFrameList renderedLegend;
        ConstructFramesFromItem(aState, iter, fieldSetFrame, renderedLegend);
        MOZ_ASSERT(renderedLegend.OnlyChild(),
                   "a rendered legend should have exactly one frame");
        fieldSetFrame->InsertFrames(FrameChildListID::Principal, nullptr,
                                    nullptr, std::move(renderedLegend));
        FCItemIterator next = iter;
        next.Next();
        iter.DeleteItemsTo(this, next);
        found = true;
        break;
      }
    }
    MOZ_ASSERT(found, "should have found our rendered legend");
  }

  CreateNeededPseudoContainers(aState, aItems, aParentFrame);
  CreateNeededAnonFlexOrGridItems(aState, aItems, aParentFrame);
  CreateNeededPseudoInternalRubyBoxes(aState, aItems, aParentFrame);
  CreateNeededPseudoSiblings(aState, aItems, aParentFrame);

  for (FCItemIterator iter(aItems); !iter.IsDone(); iter.Next()) {
    MOZ_ASSERT(!iter.item().mIsRenderedLegend,
               "Only one item can be the rendered legend, "
               "and it should've been handled above");
    NS_ASSERTION(iter.item().DesiredParentType() == GetParentType(aParentFrame),
                 "Needed pseudos didn't get created; expect bad things");
    ConstructFramesFromItem(aState, iter, aParentFrame, aFrameList);
  }

  VerifyGridFlexContainerChildren(aParentFrame, aFrameList);

  // Calculate and propagate page-name values for each frame in the frame list.
  // We do not want to compute and propagate page-name values from frames that
  // are children of any subclasses of block frames, but not actually a block
  // frame. The page-name property does not apply to frames which cannot create
  // class A breakpoints (currently no subclass of BlockFrame can). Because the
  // property does not apply, those children also cannot propagate page-name
  // values.
  // This assumption helps avoid unnecessarily handling page-names for frames
  // such as form controls, which also avoids bug 1819468.
  if (aState.mPresContext->IsPaginated() && aParentFrame->IsBlockFrame()) {
    // Set the start/end page values while iterating the frame list, to walk
    // up the frame tree only once after iterating the frame list.
    // This also avoids extra property lookups on these frames.
    MOZ_ASSERT(aState.mAutoPageNameValue == aParentFrame->GetAutoPageValue(),
               "aState.mAutoPageNameValue should have been equivalent to "
               "the auto value stored on our parent frame.");
    // Even though we store null for page values that equal the "auto" resolved
    // value on frames, we always want startPageValue/endPageValue to be the
    // actual atoms reflecting the start/end values. This is because when we
    // propagate the values up the frame tree, we will need to compare them to
    // the auto value for each ancestor. This value might be different than the
    // auto value for this frame.
    const nsAtom* startPageValue = nullptr;
    const nsAtom* endPageValue = nullptr;
    for (nsIFrame* f : aFrameList) {
      if (f->IsPlaceholderFrame()) {
        continue;
      }
      // Resolve auto against the parent frame's used page name, which has been
      // determined and set on aState.mAutoPageNameValue. If this item is not
      // block-level then we use the value that auto resolves to.
      //
      // This is to achieve the propagation behavior described in the spec:
      //
      // "A start page value and end page value is determined for each box as
      //  the value (if any) propagated from its first or last child box
      //  (respectively), else the used value on the box itself."
      //
      // "A child propagates its own start or end page value if and only if the
      //  page property applies to it."
      //
      // The page property only applies to "boxes that create class A break
      // points". When taken together, this means that non block-level children
      // do not propagate start/end page values, and instead we use "the used
      // value on the box itself", the "box itself" being aParentFrame. This
      // value has been determined and saved as aState.mAutoPageNameValue
      //
      // https://www.w3.org/TR/css-page-3/#using-named-pages
      // https://www.w3.org/TR/css-break-3/#btw-blocks
      const StylePageName& pageName = f->StylePage()->mPage;
      const nsAtom* const pageNameAtom =
          (pageName.IsPageName() && f->IsBlockOutside())
              ? pageName.AsPageName().AsAtom()
              : aState.mAutoPageNameValue;
      nsIFrame::PageValues* pageValues =
          f->GetProperty(nsIFrame::PageValuesProperty());
      // If this frame has any children, it will already have had its page
      // values set at this point. However, if no page values have been set,
      // we must ensure that the appropriate PageValuesProperty value has been
      // set.
      // If the page name is equal to the auto value, then PageValuesProperty
      // should remain null to indicate that the start/end values are both
      // equal to the auto value.
      if (pageNameAtom != aState.mAutoPageNameValue && !pageValues) {
        pageValues = new nsIFrame::PageValues{pageNameAtom, pageNameAtom};
        f->SetProperty(nsIFrame::PageValuesProperty(), pageValues);
      }
      // We don't want to use GetStartPageValue() or GetEndPageValue(), as each
      // requires a property lookup which we can avoid here.
      if (!startPageValue) {
        startPageValue = (pageValues && pageValues->mStartPageValue)
                             ? pageValues->mStartPageValue.get()
                             : aState.mAutoPageNameValue;
      }
      endPageValue = (pageValues && pageValues->mEndPageValue)
                         ? pageValues->mEndPageValue.get()
                         : aState.mAutoPageNameValue;
      MOZ_ASSERT(startPageValue && endPageValue,
                 "Should have found start/end page value");
    }
    MOZ_ASSERT(!startPageValue == !endPageValue,
               "Should have set both or neither page values");
    if (startPageValue) {
      // Walk up the frame tree from our parent frame, propagating start and
      // end page values.
      // As we go, if we find that, for a frame, we are not contributing one of
      // the start/end page values, then our subtree will not contribute this
      // value from that frame onward. startPageValue/endPageValue are set to
      // null to indicate this.
      // Stop iterating when we are not contributing either start or end
      // values, when we hit the root frame (no parent), or when we find a
      // frame that is not a block frame.
      for (nsContainerFrame* ancestorFrame = aParentFrame;
           (startPageValue || endPageValue) && ancestorFrame &&
           ancestorFrame->IsBlockFrame();
           ancestorFrame = ancestorFrame->GetParent()) {
        MOZ_ASSERT(!ancestorFrame->GetPrevInFlow(),
                   "Should not have fragmentation yet");
        MOZ_ASSERT(ancestorFrame->mWasVisitedByAutoFrameConstructionPageName,
                   "Frame should have been visited by "
                   "AutoFrameConstructionPageName");
        {
          // Get what the auto value is, based on this frame's parent.
          // For the root frame, `auto` resolves to the empty atom.
          const nsContainerFrame* const parent = ancestorFrame->GetParent();
          const nsAtom* const parentAuto = MOZ_LIKELY(parent)
                                               ? parent->GetAutoPageValue()
                                               : nsGkAtoms::_empty;
          SetPageValues(ancestorFrame, parentAuto, startPageValue,
                        endPageValue);
        }
        // Once we stop contributing start/end values, we know there is a
        // sibling subtree that contributed that value to our shared parent
        // instead of our starting frame's subtree. This means once
        // startPageValue/endPageValue becomes null, indicating that we are no
        // longer contributing that page value, it should stay null and we no
        // longer need to check for siblings in that direction.
        if (startPageValue &&
            !FrameHasOnlyPlaceholderPrevSiblings(ancestorFrame)) {
          startPageValue = nullptr;
        }
        if (endPageValue &&
            !FrameHasOnlyPlaceholderNextSiblings(ancestorFrame)) {
          endPageValue = nullptr;
        }
      }
    }
  }

  if (aParentIsWrapperAnonBox) {
    for (nsIFrame* f : aFrameList) {
      f->SetParentIsWrapperAnonBox();
    }
  }
}

void nsCSSFrameConstructor::AddFCItemsForAnonymousContent(
    nsFrameConstructorState& aState, nsContainerFrame* aFrame,
    const nsTArray<nsIAnonymousContentCreator::ContentInfo>& aAnonymousItems,
    FrameConstructionItemList& aItemsToConstruct,
    const AutoFrameConstructionPageName&) {
  for (const auto& info : aAnonymousItems) {
    nsIContent* content = info.mContent;
    // Gecko-styled nodes should have no pending restyle flags.
    // Assert some things about this content
    MOZ_ASSERT(!(content->GetFlags() &
                 (NODE_DESCENDANTS_NEED_FRAMES | NODE_NEEDS_FRAME)),
               "Should not be marked as needing frames");
    MOZ_ASSERT(!content->GetPrimaryFrame(), "Should have no existing frame");
    MOZ_ASSERT(!content->IsComment() && !content->IsProcessingInstruction(),
               "Why is someone creating garbage anonymous content");

    // Make sure we eagerly performed the servo cascade when the anonymous
    // nodes were created.
    MOZ_ASSERT(!content->IsElement() || content->AsElement()->HasServoData());

    RefPtr<ComputedStyle> computedStyle = ResolveComputedStyle(content);

    AddFrameConstructionItemsInternal(aState, content, aFrame, true,
                                      computedStyle, {ItemFlag::AllowPageBreak},
                                      aItemsToConstruct);
  }
}

void nsCSSFrameConstructor::ProcessChildren(
    nsFrameConstructorState& aState, nsIContent* aContent,
    ComputedStyle* aComputedStyle, nsContainerFrame* aFrame,
    const bool aCanHaveGeneratedContent, nsFrameList& aFrameList,
    const bool aAllowBlockStyles, nsIFrame* aPossiblyLeafFrame) {
  MOZ_ASSERT(aFrame, "Must have parent frame here");
  MOZ_ASSERT(aFrame->GetContentInsertionFrame() == aFrame,
             "Parent frame in ProcessChildren should be its own "
             "content insertion frame");

  const uint32_t kMaxDepth = 2 * MAX_REFLOW_DEPTH;
  static_assert(kMaxDepth <= UINT16_MAX, "mCurrentDepth type is too narrow");
  AutoRestore<uint16_t> savedDepth(mCurrentDepth);
  if (mCurrentDepth != UINT16_MAX) {
    ++mCurrentDepth;
  }

  if (!aPossiblyLeafFrame) {
    aPossiblyLeafFrame = aFrame;
  }

  // XXXbz ideally, this would do all the pushing of various
  // containing blocks as needed, so callers don't have to do it...

  // Check that our parent frame is a block before allowing ::first-letter/line.
  // E.g. <button style="display:grid"> should not allow it.
  const bool allowFirstPseudos =
      aAllowBlockStyles && aFrame->IsBlockFrameOrSubclass();
  bool haveFirstLetterStyle = false, haveFirstLineStyle = false;
  if (allowFirstPseudos) {
    ShouldHaveSpecialBlockStyle(aContent, aComputedStyle, &haveFirstLetterStyle,
                                &haveFirstLineStyle);
  }

  AutoFrameConstructionItemList itemsToConstruct(this);
  AutoFrameConstructionPageName pageNameTracker(aState, aFrame);

  // If we have first-letter or first-line style then frames can get
  // moved around so don't set these flags.
  if (allowFirstPseudos && !haveFirstLetterStyle && !haveFirstLineStyle) {
    itemsToConstruct.SetLineBoundaryAtStart(true);
    itemsToConstruct.SetLineBoundaryAtEnd(true);
  }

  // Create any anonymous frames we need here.
  AutoTArray<nsIAnonymousContentCreator::ContentInfo, 4> anonymousItems;
  GetAnonymousContent(aContent, aPossiblyLeafFrame, anonymousItems);
#ifdef DEBUG
  for (uint32_t i = 0; i < anonymousItems.Length(); ++i) {
    MOZ_ASSERT(anonymousItems[i].mContent->IsRootOfNativeAnonymousSubtree(),
               "Content should know it's an anonymous subtree");
  }
#endif
  AddFCItemsForAnonymousContent(aState, aFrame, anonymousItems,
                                itemsToConstruct, pageNameTracker);

  nsBlockFrame* listItem = nullptr;
  bool isOutsideMarker = false;
  if (!aPossiblyLeafFrame->IsLeaf()) {
    // :before/:after content should have the same style parent as normal kids.
    //
    // Note that we don't use this style for looking up things like special
    // block styles because in some cases involving table pseudo-frames it has
    // nothing to do with the parent frame's desired behavior.
    auto* styleParentFrame =
        nsIFrame::CorrectStyleParentFrame(aFrame, PseudoStyleType::NotPseudo);
    ComputedStyle* computedStyle = styleParentFrame->Style();

    if (aCanHaveGeneratedContent) {
      if (computedStyle->StyleDisplay()->IsListItem() &&
          (listItem = do_QueryFrame(aFrame)) &&
          !styleParentFrame->IsFieldSetFrame()) {
        isOutsideMarker = computedStyle->StyleList()->mListStylePosition ==
                          StyleListStylePosition::Outside;
        ItemFlags extraFlags;
        if (isOutsideMarker) {
          extraFlags += ItemFlag::IsForOutsideMarker;
        }
        CreateGeneratedContentItem(aState, aFrame, *aContent->AsElement(),
                                   *computedStyle, PseudoStyleType::marker,
                                   itemsToConstruct, extraFlags);
      }
      // Probe for generated content before
      CreateGeneratedContentItem(aState, aFrame, *aContent->AsElement(),
                                 *computedStyle, PseudoStyleType::before,
                                 itemsToConstruct);
    }

    const bool addChildItems = MOZ_LIKELY(mCurrentDepth < kMaxDepth);
    if (!addChildItems) {
      NS_WARNING("ProcessChildren max depth exceeded");
    }

    FlattenedChildIterator iter(aContent);
    const InsertionPoint insertion(aFrame, aContent);
    for (nsIContent* child = iter.GetNextChild(); child;
         child = iter.GetNextChild()) {
      MOZ_ASSERT(insertion.mContainer == GetInsertionPoint(child).mContainer,
                 "GetInsertionPoint should agree with us");
      if (addChildItems) {
        AddFrameConstructionItems(aState, child, iter.ShadowDOMInvolved(),
                                  *computedStyle, insertion, itemsToConstruct);
      } else {
        ClearLazyBits(child, child->GetNextSibling());
      }
    }
    itemsToConstruct.SetParentHasNoShadowDOM(!iter.ShadowDOMInvolved());

    if (aCanHaveGeneratedContent) {
      // Probe for generated content after
      CreateGeneratedContentItem(aState, aFrame, *aContent->AsElement(),
                                 *computedStyle, PseudoStyleType::after,
                                 itemsToConstruct);
    }
  } else {
    ClearLazyBits(aContent->GetFirstChild(), nullptr);
  }

  ConstructFramesFromItemList(aState, itemsToConstruct, aFrame,
                              /* aParentIsWrapperAnonBox = */ false,
                              aFrameList);

  if (listItem) {
    if (auto* markerFrame = nsLayoutUtils::GetMarkerFrame(aContent)) {
      for (auto* childFrame : aFrameList) {
        if (markerFrame == childFrame) {
          if (isOutsideMarker) {
            // SetMarkerFrameForListItem will add childFrame to the
            // FrameChildListID::Bullet
            aFrameList.RemoveFrame(childFrame);
            auto* grandParent = listItem->GetParent()->GetParent();
            if (listItem->Style()->GetPseudoType() ==
                    PseudoStyleType::columnContent &&
                grandParent && grandParent->IsColumnSetWrapperFrame()) {
              listItem = do_QueryFrame(grandParent);
              MOZ_ASSERT(listItem,
                         "ColumnSetWrapperFrame is expected to be "
                         "a nsBlockFrame subclass");
              childFrame->SetParent(listItem);
            }
          }
          listItem->SetMarkerFrameForListItem(childFrame);
          MOZ_ASSERT(listItem->HasAnyStateBits(
                         NS_BLOCK_FRAME_HAS_OUTSIDE_MARKER) == isOutsideMarker);
#ifdef ACCESSIBILITY
          if (nsAccessibilityService* accService = GetAccService()) {
            auto* marker = markerFrame->GetContent();
            accService->ContentRangeInserted(mPresShell, marker, nullptr);
          }
#endif
          break;
        }
      }
    }
  }

  if (haveFirstLetterStyle) {
    WrapFramesInFirstLetterFrame(aFrame, aFrameList);
  }
  if (haveFirstLineStyle) {
    WrapFramesInFirstLineFrame(aState, aContent, aFrame, nullptr, aFrameList);
  }
}

//----------------------------------------------------------------------

// Support for :first-line style

// Special routine to handle placing a list of frames into a block
// frame that has first-line style. The routine ensures that the first
// collection of inline frames end up in a first-line frame.
// NOTE: aState may have containing block information related to a
// different part of the frame tree than where the first line occurs.
// In particular aState may be set up for where ContentInserted or
// ContentAppended is inserting content, which may be some
// non-first-in-flow continuation of the block to which the first-line
// belongs. So this function needs to be careful about how it uses
// aState.
void nsCSSFrameConstructor::WrapFramesInFirstLineFrame(
    nsFrameConstructorState& aState, nsIContent* aBlockContent,
    nsContainerFrame* aBlockFrame, nsFirstLineFrame* aLineFrame,
    nsFrameList& aFrameList) {
  // Extract any initial inline frames from aFrameList so we can put them
  // in the first-line.
  nsFrameList firstLineChildren =
      aFrameList.Split([](nsIFrame* f) { return !f->IsInlineOutside(); });

  if (firstLineChildren.IsEmpty()) {
    // Nothing is supposed to go into the first-line; nothing to do
    return;
  }

  if (!aLineFrame) {
    // Create line frame
    ComputedStyle* parentStyle = nsIFrame::CorrectStyleParentFrame(
                                     aBlockFrame, PseudoStyleType::firstLine)
                                     ->Style();
    RefPtr<ComputedStyle> firstLineStyle =
        GetFirstLineStyle(aBlockContent, parentStyle);

    aLineFrame = NS_NewFirstLineFrame(mPresShell, firstLineStyle);

    // Initialize the line frame
    InitAndRestoreFrame(aState, aBlockContent, aBlockFrame, aLineFrame);

    // The lineFrame will be the block's first child; the rest of the
    // frame list (after lastInlineFrame) will be the second and
    // subsequent children; insert lineFrame into aFrameList.
    aFrameList.InsertFrame(nullptr, nullptr, aLineFrame);

    NS_ASSERTION(aLineFrame->Style() == firstLineStyle,
                 "Bogus style on line frame");
  }

  // Give the inline frames to the lineFrame <b>after</b> reparenting them
  ReparentFrames(this, aLineFrame, firstLineChildren, true);
  if (aLineFrame->PrincipalChildList().IsEmpty() &&
      aLineFrame->HasAnyStateBits(NS_FRAME_FIRST_REFLOW)) {
    aLineFrame->SetInitialChildList(FrameChildListID::Principal,
                                    std::move(firstLineChildren));
  } else {
    AppendFrames(aLineFrame, FrameChildListID::Principal,
                 std::move(firstLineChildren));
  }
}

// Special routine to handle appending a new frame to a block frame's
// child list. Takes care of placing the new frame into the right
// place when first-line style is present.
void nsCSSFrameConstructor::AppendFirstLineFrames(
    nsFrameConstructorState& aState, nsIContent* aBlockContent,
    nsContainerFrame* aBlockFrame, nsFrameList& aFrameList) {
  // It's possible that aBlockFrame needs to have a first-line frame
  // created because it doesn't currently have any children.
  const nsFrameList& blockKids = aBlockFrame->PrincipalChildList();
  if (blockKids.IsEmpty()) {
    WrapFramesInFirstLineFrame(aState, aBlockContent, aBlockFrame, nullptr,
                               aFrameList);
    return;
  }

  // Examine the last block child - if it's a first-line frame then
  // appended frames need special treatment.
  nsIFrame* lastBlockKid = blockKids.LastChild();
  if (!lastBlockKid->IsLineFrame()) {
    // No first-line frame at the end of the list, therefore there is
    // an intervening block between any first-line frame the frames
    // we are appending. Therefore, we don't need any special
    // treatment of the appended frames.
    return;
  }

  nsFirstLineFrame* lineFrame = static_cast<nsFirstLineFrame*>(lastBlockKid);
  WrapFramesInFirstLineFrame(aState, aBlockContent, aBlockFrame, lineFrame,
                             aFrameList);
}

void nsCSSFrameConstructor::CheckForFirstLineInsertion(
    nsIFrame* aParentFrame, nsFrameList& aFrameList) {
  MOZ_ASSERT(aParentFrame->Style()->HasPseudoElementData(),
             "Why were we called?");

  if (aFrameList.IsEmpty()) {
    // Happens often enough, with the caption stuff.  No need to do the ancestor
    // walk here.
    return;
  }

  class RestyleManager* restyleManager = RestyleManager();

  // Check whether there's a ::first-line on the path up from aParentFrame.
  // Note that we can't stop until we've run out of ancestors with
  // pseudo-element data, because the first-letter might be somewhere way up the
  // tree; in particular it might be past our containing block.
  nsIFrame* ancestor = aParentFrame;
  while (ancestor) {
    if (!ancestor->Style()->HasPseudoElementData()) {
      // We know we won't find a ::first-line now.
      return;
    }

    if (!ancestor->IsLineFrame()) {
      ancestor = ancestor->GetParent();
      continue;
    }

    if (!ancestor->Style()->IsPseudoElement()) {
      // This is a continuation lineframe, not the first line; no need to do
      // anything to the styles.
      return;
    }

    // Fix up the styles of aFrameList for ::first-line.
    for (nsIFrame* f : aFrameList) {
      restyleManager->ReparentComputedStyleForFirstLine(f);
    }
    return;
  }
}

//----------------------------------------------------------------------

// First-letter support

// Determine how many characters in the text fragment apply to the
// first letter
static int32_t FirstLetterCount(const nsTextFragment* aFragment) {
  int32_t count = 0;
  int32_t firstLetterLength = 0;

  const uint32_t n = aFragment->GetLength();
  for (uint32_t i = 0; i < n; i++) {
    const char16_t ch = aFragment->CharAt(i);
    // FIXME: take content language into account when deciding whitespace.
    if (dom::IsSpaceCharacter(ch)) {
      if (firstLetterLength) {
        break;
      }
      count++;
      continue;
    }
    // XXX I18n
    if ((ch == '\'') || (ch == '\"')) {
      if (firstLetterLength) {
        break;
      }
      // keep looping
      firstLetterLength = 1;
    } else {
      count++;
      break;
    }
  }

  return count;
}

static bool NeedFirstLetterContinuation(Text* aText) {
  MOZ_ASSERT(aText, "null ptr");
  int32_t flc = FirstLetterCount(&aText->TextFragment());
  int32_t tl = aText->TextDataLength();
  return flc < tl;
}

static bool IsFirstLetterContent(Text* aText) {
  return aText->TextDataLength() && !aText->TextIsOnlyWhitespace();
}

/**
 * Create a letter frame, only make it a floating frame.
 */
nsFirstLetterFrame* nsCSSFrameConstructor::CreateFloatingLetterFrame(
    nsFrameConstructorState& aState, Text* aTextContent, nsIFrame* aTextFrame,
    nsContainerFrame* aParentFrame, ComputedStyle* aParentStyle,
    ComputedStyle* aComputedStyle, nsFrameList& aResult) {
  MOZ_ASSERT(aParentStyle);

  nsFirstLetterFrame* letterFrame =
      NS_NewFloatingFirstLetterFrame(mPresShell, aComputedStyle);
  // We don't want to use a text content for a non-text frame (because we want
  // its primary frame to be a text frame).
  nsIContent* letterContent = aParentFrame->GetContent();
  nsContainerFrame* containingBlock =
      aState.GetGeometricParent(*aComputedStyle->StyleDisplay(), aParentFrame);
  InitAndRestoreFrame(aState, letterContent, containingBlock, letterFrame);

  // Init the text frame to refer to the letter frame.
  //
  // Make sure we get a proper style for it (the one passed in is for the letter
  // frame and will have the float property set on it; the text frame shouldn't
  // have that set).
  ServoStyleSet* styleSet = mPresShell->StyleSet();
  RefPtr<ComputedStyle> textSC =
      styleSet->ResolveStyleForText(aTextContent, aComputedStyle);
  aTextFrame->SetComputedStyleWithoutNotification(textSC);
  InitAndRestoreFrame(aState, aTextContent, letterFrame, aTextFrame);

  // And then give the text frame to the letter frame
  SetInitialSingleChild(letterFrame, aTextFrame);

  // See if we will need to continue the text frame (does it contain
  // more than just the first-letter text or not?) If it does, then we
  // create (in advance) a continuation frame for it.
  nsIFrame* nextTextFrame = nullptr;
  if (NeedFirstLetterContinuation(aTextContent)) {
    // Create continuation
    nextTextFrame = CreateContinuingFrame(aTextFrame, aParentFrame);
    RefPtr<ComputedStyle> newSC =
        styleSet->ResolveStyleForText(aTextContent, aParentStyle);
    nextTextFrame->SetComputedStyle(newSC);
  }

  NS_ASSERTION(aResult.IsEmpty(), "aResult should be an empty nsFrameList!");
  // Put the new float before any of the floats in the block we're doing
  // first-letter for, that is, before any floats whose parent is
  // containingBlock.
  nsIFrame* prevSibling = nullptr;
  for (nsIFrame* f : aState.mFloatedList) {
    if (f->GetParent() == containingBlock) {
      break;
    }
    prevSibling = f;
  }

  aState.AddChild(letterFrame, aResult, letterContent, aParentFrame, false,
                  true, true, prevSibling);

  if (nextTextFrame) {
    aResult.AppendFrame(nullptr, nextTextFrame);
  }

  return letterFrame;
}

/**
 * Create a new letter frame for aTextFrame. The letter frame will be
 * a child of aParentFrame.
 */
void nsCSSFrameConstructor::CreateLetterFrame(
    nsContainerFrame* aBlockFrame, nsContainerFrame* aBlockContinuation,
    Text* aTextContent, nsContainerFrame* aParentFrame, nsFrameList& aResult) {
  NS_ASSERTION(aBlockFrame->IsBlockFrameOrSubclass(), "Not a block frame?");

  // Get a ComputedStyle for the first-letter-frame.
  //
  // Keep this in sync with nsBlockFrame::UpdatePseudoElementStyles.
  nsIFrame* parentFrame = nsIFrame::CorrectStyleParentFrame(
      aParentFrame, PseudoStyleType::firstLetter);

  ComputedStyle* parentComputedStyle = parentFrame->Style();
  ComputedStyle* parentComputedStyleIgnoringFirstLine = parentComputedStyle;
  if (parentFrame->IsLineFrame()) {
    parentComputedStyleIgnoringFirstLine =
        nsIFrame::CorrectStyleParentFrame(aBlockFrame,
                                          PseudoStyleType::firstLetter)
            ->Style();
  }

  // Use content from containing block so that we can actually
  // find a matching style rule.
  nsIContent* blockContent = aBlockFrame->GetContent();

  // Create first-letter style rule, ignoring first line. If we already have a
  // first-line we'll reparent the style below.
  RefPtr<ComputedStyle> sc =
      GetFirstLetterStyle(blockContent, parentComputedStyleIgnoringFirstLine);

  if (sc) {
    if (parentComputedStyleIgnoringFirstLine != parentComputedStyle) {
      sc = mPresShell->StyleSet()->ReparentComputedStyle(
          sc, parentComputedStyle, parentComputedStyle,
          blockContent->AsElement());
    }

    RefPtr<ComputedStyle> textSC =
        mPresShell->StyleSet()->ResolveStyleForText(aTextContent, sc);

    // Create a new text frame (the original one will be discarded)
    // pass a temporary stylecontext, the correct one will be set
    // later.  Start off by unsetting the primary frame for
    // aTextContent, so it's no longer pointing to the to-be-destroyed
    // frame.
    // XXXbz it would be really nice to destroy the old frame _first_,
    // then create the new one, so we could avoid this hack.
    aTextContent->SetPrimaryFrame(nullptr);
    nsIFrame* textFrame = NS_NewTextFrame(mPresShell, textSC);

    NS_ASSERTION(aBlockContinuation == GetFloatContainingBlock(aParentFrame),
                 "Containing block is confused");
    nsFrameConstructorState state(
        mPresShell, GetAbsoluteContainingBlock(aParentFrame, FIXED_POS),
        GetAbsoluteContainingBlock(aParentFrame, ABS_POS), aBlockContinuation);

    // Create the right type of first-letter frame
    const nsStyleDisplay* display = sc->StyleDisplay();
    nsFirstLetterFrame* letterFrame;
    if (display->IsFloatingStyle() && !aParentFrame->IsInSVGTextSubtree()) {
      // Make a floating first-letter frame
      letterFrame = CreateFloatingLetterFrame(state, aTextContent, textFrame,
                                              aParentFrame, parentComputedStyle,
                                              sc, aResult);
    } else {
      // Make an inflow first-letter frame
      letterFrame = NS_NewFirstLetterFrame(mPresShell, sc);

      // Initialize the first-letter-frame.  We don't want to use a text
      // content for a non-text frame (because we want its primary frame to
      // be a text frame).
      nsIContent* letterContent = aParentFrame->GetContent();
      letterFrame->Init(letterContent, aParentFrame, nullptr);

      InitAndRestoreFrame(state, aTextContent, letterFrame, textFrame);

      SetInitialSingleChild(letterFrame, textFrame);
      aResult.Clear();
      aResult.AppendFrame(nullptr, letterFrame);
      NS_ASSERTION(!aBlockFrame->GetPrevContinuation(),
                   "should have the first continuation here");
      aBlockFrame->AddStateBits(NS_BLOCK_HAS_FIRST_LETTER_CHILD);
    }
    MOZ_ASSERT(
        !aBlockFrame->GetPrevContinuation(),
        "Setting up a first-letter frame on a non-first block continuation?");
    auto parent =
        static_cast<nsContainerFrame*>(aParentFrame->FirstContinuation());
    if (MOZ_UNLIKELY(parent->IsLineFrame())) {
      parent = static_cast<nsContainerFrame*>(
          parent->GetParent()->FirstContinuation());
    }
    parent->SetHasFirstLetterChild();
    aBlockFrame->SetProperty(nsContainerFrame::FirstLetterProperty(),
                             letterFrame);
    aTextContent->SetPrimaryFrame(textFrame);
  }
}

void nsCSSFrameConstructor::WrapFramesInFirstLetterFrame(
    nsContainerFrame* aBlockFrame, nsFrameList& aBlockFrames) {
  aBlockFrame->AddStateBits(NS_BLOCK_HAS_FIRST_LETTER_STYLE);

  nsContainerFrame* parentFrame = nullptr;
  nsIFrame* textFrame = nullptr;
  nsIFrame* prevFrame = nullptr;
  nsFrameList letterFrames;
  bool stopLooking = false;
  WrapFramesInFirstLetterFrame(
      aBlockFrame, aBlockFrame, aBlockFrame, aBlockFrames.FirstChild(),
      &parentFrame, &textFrame, &prevFrame, letterFrames, &stopLooking);
  if (!parentFrame) {
    return;
  }
  DestroyContext context(mPresShell);
  if (parentFrame == aBlockFrame) {
    // Take textFrame out of the block's frame list and substitute the
    // letter frame(s) instead.
    aBlockFrames.DestroyFrame(context, textFrame);
    aBlockFrames.InsertFrames(nullptr, prevFrame, std::move(letterFrames));
  } else {
    // Take the old textFrame out of the inline parent's child list
    RemoveFrame(context, FrameChildListID::Principal, textFrame);

    // Insert in the letter frame(s)
    parentFrame->InsertFrames(FrameChildListID::Principal, prevFrame, nullptr,
                              std::move(letterFrames));
  }
}

void nsCSSFrameConstructor::WrapFramesInFirstLetterFrame(
    nsContainerFrame* aBlockFrame, nsContainerFrame* aBlockContinuation,
    nsContainerFrame* aParentFrame, nsIFrame* aParentFrameList,
    nsContainerFrame** aModifiedParent, nsIFrame** aTextFrame,
    nsIFrame** aPrevFrame, nsFrameList& aLetterFrames, bool* aStopLooking) {
  nsIFrame* prevFrame = nullptr;
  nsIFrame* frame = aParentFrameList;

  // This loop attempts to implement "Finding the First Letter":
  // https://drafts.csswg.org/css-pseudo-4/#application-in-css
  // FIXME: we don't handle nested blocks correctly yet though (bug 214004)
  while (frame) {
    nsIFrame* nextFrame = frame->GetNextSibling();

    // Skip all ::markers and placeholders.
    if (frame->Style()->GetPseudoType() == PseudoStyleType::marker ||
        frame->IsPlaceholderFrame()) {
      prevFrame = frame;
      frame = nextFrame;
      continue;
    }
    LayoutFrameType frameType = frame->Type();
    if (LayoutFrameType::Text == frameType) {
      // Wrap up first-letter content in a letter frame
      Text* textContent = frame->GetContent()->AsText();
      if (IsFirstLetterContent(textContent)) {
        // Create letter frame to wrap up the text
        CreateLetterFrame(aBlockFrame, aBlockContinuation, textContent,
                          aParentFrame, aLetterFrames);

        // Provide adjustment information for parent
        *aModifiedParent = aParentFrame;
        *aTextFrame = frame;
        *aPrevFrame = prevFrame;
        *aStopLooking = true;
        return;
      }
    } else if (IsInlineFrame(frame) && frameType != LayoutFrameType::Br) {
      nsIFrame* kids = frame->PrincipalChildList().FirstChild();
      WrapFramesInFirstLetterFrame(aBlockFrame, aBlockContinuation,
                                   static_cast<nsContainerFrame*>(frame), kids,
                                   aModifiedParent, aTextFrame, aPrevFrame,
                                   aLetterFrames, aStopLooking);
      if (*aStopLooking) {
        return;
      }
    } else {
      // This will stop us looking to create more letter frames. For
      // example, maybe the frame-type is "letterFrame" or
      // "placeholderFrame". This keeps us from creating extra letter
      // frames, and also prevents us from creating letter frames when
      // the first real content child of a block is not text (e.g. an
      // image, hr, etc.)
      *aStopLooking = true;
      break;
    }

    prevFrame = frame;
    frame = nextFrame;
  }
}

static nsIFrame* FindFirstLetterFrame(nsIFrame* aFrame,
                                      FrameChildListID aListID) {
  for (nsIFrame* f : aFrame->GetChildList(aListID)) {
    if (f->IsLetterFrame()) {
      return f;
    }
  }
  return nullptr;
}

static void ClearHasFirstLetterChildFrom(nsContainerFrame* aParentFrame) {
  MOZ_ASSERT(aParentFrame);
  auto* parent =
      static_cast<nsContainerFrame*>(aParentFrame->FirstContinuation());
  if (MOZ_UNLIKELY(parent->IsLineFrame())) {
    MOZ_ASSERT(!parent->HasFirstLetterChild());
    parent = static_cast<nsContainerFrame*>(
        parent->GetParent()->FirstContinuation());
  }
  MOZ_ASSERT(parent->HasFirstLetterChild());
  parent->ClearHasFirstLetterChild();
}

void nsCSSFrameConstructor::RemoveFloatingFirstLetterFrames(
    PresShell* aPresShell, nsIFrame* aBlockFrame) {
  // Look for the first letter frame on the FrameChildListID::Float, then
  // FrameChildListID::PushedFloats.
  nsIFrame* floatFrame =
      ::FindFirstLetterFrame(aBlockFrame, FrameChildListID::Float);
  if (!floatFrame) {
    floatFrame =
        ::FindFirstLetterFrame(aBlockFrame, FrameChildListID::PushedFloats);
    if (!floatFrame) {
      return;
    }
  }

  // Take the text frame away from the letter frame (so it isn't
  // destroyed when we destroy the letter frame).
  nsIFrame* textFrame = floatFrame->PrincipalChildList().FirstChild();
  if (!textFrame) {
    return;
  }

  // Discover the placeholder frame for the letter frame
  nsPlaceholderFrame* placeholderFrame = floatFrame->GetPlaceholderFrame();
  if (!placeholderFrame) {
    // Somethings really wrong
    return;
  }
  nsContainerFrame* parentFrame = placeholderFrame->GetParent();
  if (!parentFrame) {
    // Somethings really wrong
    return;
  }

  ClearHasFirstLetterChildFrom(parentFrame);

  // Create a new text frame with the right style that maps all of the content
  // that was previously part of the letter frame (and probably continued
  // elsewhere).
  ComputedStyle* parentSC = parentFrame->Style();
  nsIContent* textContent = textFrame->GetContent();
  if (!textContent) {
    return;
  }
  RefPtr<ComputedStyle> newSC =
      aPresShell->StyleSet()->ResolveStyleForText(textContent, parentSC);
  nsIFrame* newTextFrame = NS_NewTextFrame(aPresShell, newSC);
  newTextFrame->Init(textContent, parentFrame, nullptr);

  // Destroy the old text frame's continuations (the old text frame
  // will be destroyed when its letter frame is destroyed).
  nsIFrame* frameToDelete = textFrame->LastContinuation();
  DestroyContext context(mPresShell);
  while (frameToDelete != textFrame) {
    nsIFrame* nextFrameToDelete = frameToDelete->GetPrevContinuation();
    RemoveFrame(context, FrameChildListID::Principal, frameToDelete);
    frameToDelete = nextFrameToDelete;
  }

  nsIFrame* prevSibling = placeholderFrame->GetPrevSibling();

  // Now that everything is set...
#ifdef NOISY_FIRST_LETTER
  printf(
      "RemoveFloatingFirstLetterFrames: textContent=%p oldTextFrame=%p "
      "newTextFrame=%p\n",
      textContent.get(), textFrame, newTextFrame);
#endif

  // Remove placeholder frame and the float
  RemoveFrame(context, FrameChildListID::Principal, placeholderFrame);

  // Now that the old frames are gone, we can start pointing to our
  // new primary frame.
  textContent->SetPrimaryFrame(newTextFrame);

  // Wallpaper bug 822910.
  bool offsetsNeedFixing = prevSibling && prevSibling->IsTextFrame();
  if (offsetsNeedFixing) {
    prevSibling->AddStateBits(TEXT_OFFSETS_NEED_FIXING);
  }

  // Insert text frame in its place
  InsertFrames(parentFrame, FrameChildListID::Principal, prevSibling,
               nsFrameList(newTextFrame, newTextFrame));

  if (offsetsNeedFixing) {
    prevSibling->RemoveStateBits(TEXT_OFFSETS_NEED_FIXING);
  }
}

void nsCSSFrameConstructor::RemoveFirstLetterFrames(
    PresShell* aPresShell, nsContainerFrame* aFrame,
    nsContainerFrame* aBlockFrame, bool* aStopLooking) {
  nsIFrame* prevSibling = nullptr;
  nsIFrame* kid = aFrame->PrincipalChildList().FirstChild();

  while (kid) {
    if (kid->IsLetterFrame()) {
      ClearHasFirstLetterChildFrom(aFrame);
      nsIFrame* textFrame = kid->PrincipalChildList().FirstChild();
      if (!textFrame) {
        break;
      }

      // Create a new textframe
      ComputedStyle* parentSC = aFrame->Style();
      if (!parentSC) {
        break;
      }
      nsIContent* textContent = textFrame->GetContent();
      if (!textContent) {
        break;
      }
      RefPtr<ComputedStyle> newSC =
          aPresShell->StyleSet()->ResolveStyleForText(textContent, parentSC);
      textFrame = NS_NewTextFrame(aPresShell, newSC);
      textFrame->Init(textContent, aFrame, nullptr);

      DestroyContext context(mPresShell);

      // Next rip out the kid and replace it with the text frame
      RemoveFrame(context, FrameChildListID::Principal, kid);

      // Now that the old frames are gone, we can start pointing to our
      // new primary frame.
      textContent->SetPrimaryFrame(textFrame);

      // Wallpaper bug 822910.
      bool offsetsNeedFixing = prevSibling && prevSibling->IsTextFrame();
      if (offsetsNeedFixing) {
        prevSibling->AddStateBits(TEXT_OFFSETS_NEED_FIXING);
      }

      // Insert text frame in its place
      InsertFrames(aFrame, FrameChildListID::Principal, prevSibling,
                   nsFrameList(textFrame, textFrame));

      if (offsetsNeedFixing) {
        prevSibling->RemoveStateBits(TEXT_OFFSETS_NEED_FIXING);
      }

      *aStopLooking = true;
      NS_ASSERTION(!aBlockFrame->GetPrevContinuation(),
                   "should have the first continuation here");
      aBlockFrame->RemoveStateBits(NS_BLOCK_HAS_FIRST_LETTER_CHILD);
      break;
    }
    if (IsInlineFrame(kid)) {
      nsContainerFrame* kidAsContainerFrame = do_QueryFrame(kid);
      if (kidAsContainerFrame) {
        // Look inside child inline frame for the letter frame.
        RemoveFirstLetterFrames(aPresShell, kidAsContainerFrame, aBlockFrame,
                                aStopLooking);
        if (*aStopLooking) {
          break;
        }
      }
    }
    prevSibling = kid;
    kid = kid->GetNextSibling();
  }
}

void nsCSSFrameConstructor::RemoveLetterFrames(PresShell* aPresShell,
                                               nsContainerFrame* aBlockFrame) {
  aBlockFrame =
      static_cast<nsContainerFrame*>(aBlockFrame->FirstContinuation());
  aBlockFrame->RemoveProperty(nsContainerFrame::FirstLetterProperty());
  nsContainerFrame* continuation = aBlockFrame;

  bool stopLooking = false;
  do {
    RemoveFloatingFirstLetterFrames(aPresShell, continuation);
    RemoveFirstLetterFrames(aPresShell, continuation, aBlockFrame,
                            &stopLooking);
    if (stopLooking) {
      break;
    }
    continuation =
        static_cast<nsContainerFrame*>(continuation->GetNextContinuation());
  } while (continuation);
}

// Fixup the letter frame situation for the given block
void nsCSSFrameConstructor::RecoverLetterFrames(nsContainerFrame* aBlockFrame) {
  aBlockFrame =
      static_cast<nsContainerFrame*>(aBlockFrame->FirstContinuation());
  nsContainerFrame* continuation = aBlockFrame;

  nsContainerFrame* parentFrame = nullptr;
  nsIFrame* textFrame = nullptr;
  nsIFrame* prevFrame = nullptr;
  nsFrameList letterFrames;
  bool stopLooking = false;
  do {
    // XXX shouldn't this bit be set already (bug 408493), assert instead?
    continuation->AddStateBits(NS_BLOCK_HAS_FIRST_LETTER_STYLE);
    WrapFramesInFirstLetterFrame(
        aBlockFrame, continuation, continuation,
        continuation->PrincipalChildList().FirstChild(), &parentFrame,
        &textFrame, &prevFrame, letterFrames, &stopLooking);
    if (stopLooking) {
      break;
    }
    continuation =
        static_cast<nsContainerFrame*>(continuation->GetNextContinuation());
  } while (continuation);

  if (!parentFrame) {
    return;
  }
  // Take the old textFrame out of the parent's child list
  DestroyContext context(mPresShell);
  RemoveFrame(context, FrameChildListID::Principal, textFrame);

  // Insert in the letter frame(s)
  parentFrame->InsertFrames(FrameChildListID::Principal, prevFrame, nullptr,
                            std::move(letterFrames));
}

//----------------------------------------------------------------------

void nsCSSFrameConstructor::ConstructBlock(
    nsFrameConstructorState& aState, nsIContent* aContent,
    nsContainerFrame* aParentFrame, nsContainerFrame* aContentParentFrame,
    ComputedStyle* aComputedStyle, nsContainerFrame** aNewFrame,
    nsFrameList& aFrameList, nsIFrame* aPositionedFrameForAbsPosContainer) {
  // clang-format off
  //
  // If a block frame is in a multi-column subtree, its children may need to
  // be chopped into runs of blocks containing column-spans and runs of
  // blocks containing no column-spans. Each run containing column-spans
  // will be wrapped by an anonymous block. See CreateColumnSpanSiblings() for
  // the implementation.
  //
  // If a block frame is a multi-column container, its children will need to
  // be processed as above. Moreover, it creates a ColumnSetWrapperFrame as
  // its outermost frame, and its children which have no
  // -moz-column-span-wrapper pseudo will be wrapped in ColumnSetFrames. See
  // FinishBuildingColumns() for the implementation.
  //
  // The multi-column subtree maintains the following invariants:
  //
  // 1) All the frames have the frame state bit
  //    NS_FRAME_HAS_MULTI_COLUMN_ANCESTOR set, except for top-level
  //    ColumnSetWrapperFrame and those children in the column-span subtrees.
  //
  // 2) The first and last frame under ColumnSetWrapperFrame are always
  //    ColumnSetFrame.
  //
  // 3) ColumnSetFrames are linked together as continuations.
  //
  // 4) Those column-span wrappers are *not* linked together with themselves nor
  //    with the original block frame. The continuation chain consists of the
  //    original block frame and the original block's continuations wrapping
  //    non-column-spans.
  //
  // For example, this HTML
  //  <div id="x" style="column-count: 2;">
  //    <div style="column-span: all">a</div>
  //    <div id="y">
  //      b
  //      <div style="column-span: all">c</div>
  //      <div style="column-span: all">d</div>
  //      e
  //    </div>
  //  </div>
  //  <div style="column-span: all">f</div>
  //
  //  yields the following frame tree.
  //
  // A) ColumnSetWrapper (original style)
  // B)   ColumnSet (-moz-column-set)   <-- always created by BeginBuildingColumns
  // C)     Block (-moz-column-content)
  // D)   Block (-moz-column-span-wrapper, created by x)
  // E)     Block (div)
  // F)       Text ("a")
  // G)   ColumnSet (-moz-column-set)
  // H)     Block (-moz-column-content, created by x)
  // I)       Block (div, y)
  // J)         Text ("b")
  // K)   Block (-moz-column-span-wrapper, created by x)
  // L)     Block (-moz-column-span-wrapper, created by y)
  // M)       Block (div, new BFC)
  // N)         Text ("c")
  // O)       Block (div, new BFC)
  // P)         Text ("d")
  // Q)   ColumnSet (-moz-column-set)
  // R)     Block (-moz-column-content, created by x)
  // S)       Block (div, y)
  // T)         Text ("e")
  // U) Block (div, new BFC)   <-- not in multi-column hierarchy
  // V)   Text ("f")
  //
  // ColumnSet linkage described in 3): B -> G -> Q
  //
  // Block linkage described in 4): C -> H -> R  and  I -> S
  //
  // clang-format on

  nsBlockFrame* blockFrame = do_QueryFrame(*aNewFrame);
  MOZ_ASSERT(blockFrame && blockFrame->IsBlockFrame(), "not a block frame?");

  // Create column hierarchy if necessary.
  const bool needsColumn =
      aComputedStyle->StyleColumn()->IsColumnContainerStyle();
  if (needsColumn) {
    *aNewFrame = BeginBuildingColumns(aState, aContent, aParentFrame,
                                      blockFrame, aComputedStyle);

    if (aPositionedFrameForAbsPosContainer == blockFrame) {
      aPositionedFrameForAbsPosContainer = *aNewFrame;
    }
  } else {
    // No need to create column hierarchy. Initialize block frame.
    blockFrame->SetComputedStyleWithoutNotification(aComputedStyle);
    InitAndRestoreFrame(aState, aContent, aParentFrame, blockFrame);
  }

  aState.AddChild(*aNewFrame, aFrameList, aContent,
                  aContentParentFrame ? aContentParentFrame : aParentFrame);
  if (!mRootElementFrame) {
    mRootElementFrame = *aNewFrame;
  }

  // We should make the outer frame be the absolute containing block,
  // if one is required. We have to do this because absolute
  // positioning must be computed with respect to the CSS dimensions
  // of the element, which are the dimensions of the outer block. But
  // we can't really do that because only blocks can have absolute
  // children. So use the block and try to compensate with hacks
  // in nsBlockFrame::CalculateContainingBlockSizeForAbsolutes.
  nsFrameConstructorSaveState absoluteSaveState;
  (*aNewFrame)->AddStateBits(NS_FRAME_CAN_HAVE_ABSPOS_CHILDREN);
  if (aPositionedFrameForAbsPosContainer) {
    aState.PushAbsoluteContainingBlock(
        *aNewFrame, aPositionedFrameForAbsPosContainer, absoluteSaveState);
  }

  nsFrameConstructorSaveState floatSaveState;
  aState.MaybePushFloatContainingBlock(blockFrame, floatSaveState);

  if (aParentFrame->HasAnyStateBits(NS_FRAME_HAS_MULTI_COLUMN_ANCESTOR) &&
      !ShouldSuppressColumnSpanDescendants(aParentFrame)) {
    blockFrame->AddStateBits(NS_FRAME_HAS_MULTI_COLUMN_ANCESTOR);
  }

  // Process the child content
  nsFrameList childList;
  ProcessChildren(aState, aContent, aComputedStyle, blockFrame, true, childList,
                  true);

  if (!MayNeedToCreateColumnSpanSiblings(blockFrame, childList)) {
    // No need to create column-span siblings.
    blockFrame->SetInitialChildList(FrameChildListID::Principal,
                                    std::move(childList));
    return;
  }

  // Extract any initial non-column-span kids, and put them in block frame's
  // child list.
  nsFrameList initialNonColumnSpanKids =
      childList.Split([](nsIFrame* f) { return f->IsColumnSpan(); });
  blockFrame->SetInitialChildList(FrameChildListID::Principal,
                                  std::move(initialNonColumnSpanKids));

  if (childList.IsEmpty()) {
    // No more kids to process (there weren't any column-span kids).
    return;
  }

  nsFrameList columnSpanSiblings = CreateColumnSpanSiblings(
      aState, blockFrame, childList,
      // If we're constructing a column container, pass nullptr as
      // aPositionedFrame to forbid reparenting absolute/fixed positioned frames
      // to column contents or column-span wrappers.
      needsColumn ? nullptr : aPositionedFrameForAbsPosContainer);

  if (needsColumn) {
    // We're constructing a column container; need to finish building it.
    FinishBuildingColumns(aState, *aNewFrame, blockFrame, columnSpanSiblings);
  } else {
    // We're constructing a normal block which has column-span children in a
    // column hierarchy such as "x" in the following example.
    //
    // <div style="column-count: 2">
    //   <div id="x">
    //     <div>normal child</div>
    //     <div style="column-span">spanner</div>
    //   </div>
    // </div>
    aFrameList.AppendFrames(nullptr, std::move(columnSpanSiblings));
  }

  MOZ_ASSERT(columnSpanSiblings.IsEmpty(),
             "The column-span siblings should be moved to the proper place!");
}

nsBlockFrame* nsCSSFrameConstructor::BeginBuildingColumns(
    nsFrameConstructorState& aState, nsIContent* aContent,
    nsContainerFrame* aParentFrame, nsContainerFrame* aColumnContent,
    ComputedStyle* aComputedStyle) {
  MOZ_ASSERT(aColumnContent->IsBlockFrame(),
             "aColumnContent should be a block frame.");
  MOZ_ASSERT(aComputedStyle->StyleColumn()->IsColumnContainerStyle(),
             "No need to build a column hierarchy!");

  // The initial column hierarchy looks like this:
  //
  // ColumnSetWrapper (original style)
  //   ColumnSet (-moz-column-set)
  //     Block (-moz-column-content)
  //
  nsBlockFrame* columnSetWrapper = NS_NewColumnSetWrapperFrame(
      mPresShell, aComputedStyle, nsFrameState(NS_FRAME_OWNS_ANON_BOXES));
  InitAndRestoreFrame(aState, aContent, aParentFrame, columnSetWrapper);
  if (aParentFrame->HasAnyStateBits(NS_FRAME_HAS_MULTI_COLUMN_ANCESTOR) &&
      !ShouldSuppressColumnSpanDescendants(aParentFrame)) {
    columnSetWrapper->AddStateBits(NS_FRAME_HAS_MULTI_COLUMN_ANCESTOR);
  }

  AutoFrameConstructionPageName pageNameTracker(aState, columnSetWrapper);
  RefPtr<ComputedStyle> columnSetStyle =
      mPresShell->StyleSet()->ResolveInheritingAnonymousBoxStyle(
          PseudoStyleType::columnSet, aComputedStyle);
  nsContainerFrame* columnSet = NS_NewColumnSetFrame(
      mPresShell, columnSetStyle, nsFrameState(NS_FRAME_OWNS_ANON_BOXES));
  InitAndRestoreFrame(aState, aContent, columnSetWrapper, columnSet);
  columnSet->AddStateBits(NS_FRAME_HAS_MULTI_COLUMN_ANCESTOR);

  RefPtr<ComputedStyle> blockStyle =
      mPresShell->StyleSet()->ResolveInheritingAnonymousBoxStyle(
          PseudoStyleType::columnContent, columnSetStyle);
  aColumnContent->SetComputedStyleWithoutNotification(blockStyle);
  InitAndRestoreFrame(aState, aContent, columnSet, aColumnContent);
  aColumnContent->AddStateBits(NS_FRAME_HAS_MULTI_COLUMN_ANCESTOR);

  // Set up the parent-child chain.
  SetInitialSingleChild(columnSetWrapper, columnSet);
  SetInitialSingleChild(columnSet, aColumnContent);

  return columnSetWrapper;
}

void nsCSSFrameConstructor::FinishBuildingColumns(
    nsFrameConstructorState& aState, nsContainerFrame* aColumnSetWrapper,
    nsContainerFrame* aColumnContent, nsFrameList& aColumnContentSiblings) {
  nsContainerFrame* prevColumnSet = aColumnContent->GetParent();

  MOZ_ASSERT(prevColumnSet->IsColumnSetFrame() &&
                 prevColumnSet->GetParent() == aColumnSetWrapper,
             "Should have established column hierarchy!");

  // Tag the first ColumnSet to have column-span siblings so that the bit can
  // propagate to all the continuations. We don't want the last ColumnSet to
  // have this bit, so we will unset the bit for it at the end of this function.
  prevColumnSet->SetHasColumnSpanSiblings(true);

  nsFrameList finalList;
  while (aColumnContentSiblings.NotEmpty()) {
    nsIFrame* f = aColumnContentSiblings.RemoveFirstChild();
    if (f->IsColumnSpan()) {
      // Do nothing for column-span wrappers. Just move it to the final
      // items.
      finalList.AppendFrame(aColumnSetWrapper, f);
    } else {
      auto* continuingColumnSet = static_cast<nsContainerFrame*>(
          CreateContinuingFrame(prevColumnSet, aColumnSetWrapper, false));
      MOZ_ASSERT(continuingColumnSet->HasColumnSpanSiblings(),
                 "The bit should propagate to the next continuation!");

      f->SetParent(continuingColumnSet);
      SetInitialSingleChild(continuingColumnSet, f);
      finalList.AppendFrame(aColumnSetWrapper, continuingColumnSet);
      prevColumnSet = continuingColumnSet;
    }
  }

  // Unset the bit because the last ColumnSet has no column-span siblings.
  prevColumnSet->SetHasColumnSpanSiblings(false);

  aColumnSetWrapper->AppendFrames(FrameChildListID::Principal,
                                  std::move(finalList));
}

bool nsCSSFrameConstructor::MayNeedToCreateColumnSpanSiblings(
    nsContainerFrame* aBlockFrame, const nsFrameList& aChildList) {
  if (!aBlockFrame->HasAnyStateBits(NS_FRAME_HAS_MULTI_COLUMN_ANCESTOR)) {
    // The block frame isn't in a multi-column block formatting context.
    return false;
  }

  if (ShouldSuppressColumnSpanDescendants(aBlockFrame)) {
    // No need to create column-span siblings for a frame that suppresses them.
    return false;
  }

  if (aChildList.IsEmpty()) {
    // No child needs to be processed.
    return false;
  }

  // Need to actually look into the child list.
  return true;
}

nsFrameList nsCSSFrameConstructor::CreateColumnSpanSiblings(
    nsFrameConstructorState& aState, nsContainerFrame* aInitialBlock,
    nsFrameList& aChildList, nsIFrame* aPositionedFrame) {
  MOZ_ASSERT(aInitialBlock->IsBlockFrameOrSubclass());
  MOZ_ASSERT(!aPositionedFrame || aPositionedFrame->IsAbsPosContainingBlock());

  nsIContent* const content = aInitialBlock->GetContent();
  nsContainerFrame* const parentFrame = aInitialBlock->GetParent();
  const bool isInitialBlockFloatCB = aInitialBlock->IsFloatContainingBlock();

  nsFrameList siblings;
  nsContainerFrame* lastNonColumnSpanWrapper = aInitialBlock;

  // Tag the first non-column-span wrapper to have column-span siblings so that
  // the bit can propagate to all the continuations. We don't want the last
  // wrapper to have this bit, so we will unset the bit for it at the end of
  // this function.
  lastNonColumnSpanWrapper->SetHasColumnSpanSiblings(true);
  do {
    MOZ_ASSERT(aChildList.NotEmpty(), "Why call this if child list is empty?");
    MOZ_ASSERT(aChildList.FirstChild()->IsColumnSpan(),
               "Must have the child starting with column-span!");

    // Grab the consecutive column-span kids, and reparent them into a
    // block frame.
    RefPtr<ComputedStyle> columnSpanWrapperStyle =
        mPresShell->StyleSet()->ResolveNonInheritingAnonymousBoxStyle(
            PseudoStyleType::columnSpanWrapper);
    nsBlockFrame* columnSpanWrapper =
        NS_NewBlockFrame(mPresShell, columnSpanWrapperStyle);
    InitAndRestoreFrame(aState, content, parentFrame, columnSpanWrapper, false);
    columnSpanWrapper->AddStateBits(NS_FRAME_HAS_MULTI_COLUMN_ANCESTOR |
                                    NS_FRAME_CAN_HAVE_ABSPOS_CHILDREN);

    nsFrameList columnSpanKids =
        aChildList.Split([](nsIFrame* f) { return !f->IsColumnSpan(); });
    columnSpanKids.ApplySetParent(columnSpanWrapper);
    columnSpanWrapper->SetInitialChildList(FrameChildListID::Principal,
                                           std::move(columnSpanKids));
    if (aPositionedFrame) {
      aState.ReparentAbsoluteItems(columnSpanWrapper);
    }

    siblings.AppendFrame(nullptr, columnSpanWrapper);

    // Grab the consecutive non-column-span kids, and reparent them into a new
    // continuation of the last non-column-span wrapper frame.
    auto* nonColumnSpanWrapper = static_cast<nsContainerFrame*>(
        CreateContinuingFrame(lastNonColumnSpanWrapper, parentFrame, false));
    nonColumnSpanWrapper->AddStateBits(NS_FRAME_HAS_MULTI_COLUMN_ANCESTOR |
                                       NS_FRAME_CAN_HAVE_ABSPOS_CHILDREN);
    MOZ_ASSERT(nonColumnSpanWrapper->HasColumnSpanSiblings(),
               "The bit should propagate to the next continuation!");

    if (aChildList.NotEmpty()) {
      nsFrameList nonColumnSpanKids =
          aChildList.Split([](nsIFrame* f) { return f->IsColumnSpan(); });

      nonColumnSpanKids.ApplySetParent(nonColumnSpanWrapper);
      nonColumnSpanWrapper->SetInitialChildList(FrameChildListID::Principal,
                                                std::move(nonColumnSpanKids));
      if (aPositionedFrame) {
        aState.ReparentAbsoluteItems(nonColumnSpanWrapper);
      }
      if (isInitialBlockFloatCB) {
        aState.ReparentFloats(nonColumnSpanWrapper);
      }
    }

    siblings.AppendFrame(nullptr, nonColumnSpanWrapper);

    lastNonColumnSpanWrapper = nonColumnSpanWrapper;
  } while (aChildList.NotEmpty());

  // Unset the bit because the last non-column-span wrapper has no column-span
  // siblings.
  lastNonColumnSpanWrapper->SetHasColumnSpanSiblings(false);

  return siblings;
}

bool nsCSSFrameConstructor::MaybeRecreateForColumnSpan(
    nsFrameConstructorState& aState, nsContainerFrame* aParentFrame,
    nsFrameList& aFrameList, nsIFrame* aPrevSibling) {
  if (!aParentFrame->HasAnyStateBits(NS_FRAME_HAS_MULTI_COLUMN_ANCESTOR)) {
    return false;
  }

  if (aFrameList.IsEmpty()) {
    return false;
  }

  MOZ_ASSERT(!IsFramePartOfIBSplit(aParentFrame),
             "We should have wiped aParentFrame in WipeContainingBlock if it's "
             "part of IB split!");

  nsIFrame* nextSibling = ::GetInsertNextSibling(aParentFrame, aPrevSibling);
  if (!nextSibling && IsLastContinuationForColumnContent(aParentFrame)) {
    // We are appending a list of frames to the last continuation of a
    // ::-moz-column-content. This is the case where we can fix the frame tree
    // instead of reframing the containing block. Return false and let
    // AppendFramesToParent() deal with this.
    return false;
  }

  auto HasColumnSpan = [](const nsFrameList& aList) {
    for (nsIFrame* f : aList) {
      if (f->IsColumnSpan()) {
        return true;
      }
    }
    return false;
  };

  if (HasColumnSpan(aFrameList)) {
    // If any frame in the frame list has "column-span:all" style, i.e. a
    // -moz-column-span-wrapper frame, we need to reframe the multi-column
    // containing block.
    //
    // We can only be here if none of the new inserted nsIContent* nodes (via
    // ContentAppended or ContentRangeInserted) have column-span:all style, yet
    // some of them have column-span:all descendants. Sadly, there's no way to
    // detect this by checking FrameConstructionItems in WipeContainingBlock().
    // Otherwise, we would have already wiped the multi-column containing block.
    PROFILER_MARKER("Reframe multi-column after constructing frame list",
                    LAYOUT, {}, Tracing, "Layout");

    // aFrameList can contain placeholder frames. In order to destroy their
    // associated out-of-flow frames properly, we need to manually flush all the
    // out-of-flow frames in aState to their container frames.
    aState.ProcessFrameInsertionsForAllLists();
    DestroyContext context(mPresShell);
    aFrameList.DestroyFrames(context);
    RecreateFramesForContent(
        GetMultiColumnContainingBlockFor(aParentFrame)->GetContent(),
        InsertionKind::Async);
    return true;
  }

  return false;
}

nsIFrame* nsCSSFrameConstructor::ConstructInline(
    nsFrameConstructorState& aState, FrameConstructionItem& aItem,
    nsContainerFrame* aParentFrame, const nsStyleDisplay* aDisplay,
    nsFrameList& aFrameList) {
  // If an inline frame has non-inline kids, then we chop up the child list
  // into runs of blocks and runs of inlines, create anonymous block frames to
  // contain the runs of blocks, inline frames with our style for the runs of
  // inlines, and put all these frames, in order, into aFrameList.
  //
  // When there are column-span blocks in a run of blocks, instead of creating
  // an anonymous block to wrap them, we create multiple anonymous blocks,
  // wrapping runs of non-column-spans and runs of column-spans.
  //
  // We return the the first one.  The whole setup is called an {ib}
  // split; in what follows "frames in the split" refers to the anonymous blocks
  // and inlines that contain our children.
  //
  // {ib} splits maintain the following invariants:
  // 1) All frames in the split have the NS_FRAME_PART_OF_IBSPLIT bit
  //    set.
  //
  // 2) Each frame in the split has the nsIFrame::IBSplitSibling
  //    property pointing to the next frame in the split, except for the last
  //    one, which does not have it set.
  //
  // 3) Each frame in the split has the nsIFrame::IBSplitPrevSibling
  //    property pointing to the previous frame in the split, except for the
  //    first one, which does not have it set.
  //
  // 4) The first and last frame in the split are always inlines.
  //
  // 5) The frames wrapping runs of non-column-spans are linked together as
  //    continuations. The frames wrapping runs of column-spans are *not*
  //    linked with each other nor with other non-column-span wrappers.
  //
  // 6) The first and last frame in the chains of blocks are always wrapping
  //    non-column-spans. Both of them are created even if they're empty.
  //
  // An invariant that is NOT maintained is that the wrappers are actually
  // linked via GetNextSibling linkage.  A simple example is an inline
  // containing an inline that contains a block.  The three parts of the inner
  // inline end up with three different parents.
  //
  // For example, this HTML:
  // <span>
  //   <div>a</div>
  //   <span>
  //     b
  //     <div>c</div>
  //   </span>
  //   d
  //   <div>e</div>
  //   f
  //  </span>
  // Gives the following frame tree:
  //
  // Inline (outer span)
  // Block (anonymous, outer span)
  //   Block (div)
  //     Text("a")
  // Inline (outer span)
  //   Inline (inner span)
  //     Text("b")
  // Block (anonymous, outer span)
  //   Block (anonymous, inner span)
  //     Block (div)
  //       Text("c")
  // Inline (outer span)
  //   Inline (inner span)
  //   Text("d")
  // Block (anonymous, outer span)
  //   Block (div)
  //     Text("e")
  // Inline (outer span)
  //   Text("f")

  nsIContent* const content = aItem.mContent;
  ComputedStyle* const computedStyle = aItem.mComputedStyle;

  nsInlineFrame* newFrame = NS_NewInlineFrame(mPresShell, computedStyle);

  // Initialize the frame
  InitAndRestoreFrame(aState, content, aParentFrame, newFrame);

  // definition cannot be inside next block because the object's destructor is
  // significant. this is part of the fix for bug 42372
  nsFrameConstructorSaveState absoluteSaveState;

  bool isAbsPosCB = newFrame->IsAbsPosContainingBlock();
  newFrame->AddStateBits(NS_FRAME_CAN_HAVE_ABSPOS_CHILDREN);
  if (isAbsPosCB) {
    // Relatively positioned frames becomes a container for child
    // frames that are positioned
    aState.PushAbsoluteContainingBlock(newFrame, newFrame, absoluteSaveState);
  }

  if (aParentFrame->HasAnyStateBits(NS_FRAME_HAS_MULTI_COLUMN_ANCESTOR) &&
      !ShouldSuppressColumnSpanDescendants(aParentFrame)) {
    newFrame->AddStateBits(NS_FRAME_HAS_MULTI_COLUMN_ANCESTOR);
  }

  // Process the child content
  nsFrameList childList;
  ConstructFramesFromItemList(aState, aItem.mChildItems, newFrame,
                              /* aParentIsWrapperAnonBox = */ false, childList);

  nsIFrame* firstBlock = nullptr;
  if (!aItem.mIsAllInline) {
    for (nsIFrame* f : childList) {
      if (f->IsBlockOutside()) {
        firstBlock = f;
        break;
      }
    }
  }

  if (aItem.mIsAllInline || !firstBlock) {
    // This part is easy.  We either already know we have no non-inline kids,
    // or haven't found any when constructing actual frames (the latter can
    // happen only if out-of-flows that we thought had no containing block
    // acquired one when ancestor inline frames and {ib} splits got
    // constructed).  Just put all the kids into the single inline frame and
    // bail.
    newFrame->SetInitialChildList(FrameChildListID::Principal,
                                  std::move(childList));
    aState.AddChild(newFrame, aFrameList, content, aParentFrame);
    return newFrame;
  }

  // This inline frame contains several types of children. Therefore this frame
  // has to be chopped into several pieces, as described above.

  // Grab the first inline's kids
  nsFrameList firstInlineKids = childList.TakeFramesBefore(firstBlock);
  newFrame->SetInitialChildList(FrameChildListID::Principal,
                                std::move(firstInlineKids));

  aFrameList.AppendFrame(nullptr, newFrame);

  newFrame->AddStateBits(NS_FRAME_OWNS_ANON_BOXES);
  CreateIBSiblings(aState, newFrame, isAbsPosCB, childList, aFrameList);

  return newFrame;
}

void nsCSSFrameConstructor::CreateIBSiblings(nsFrameConstructorState& aState,
                                             nsContainerFrame* aInitialInline,
                                             bool aIsAbsPosCB,
                                             nsFrameList& aChildList,
                                             nsFrameList& aSiblings) {
  MOZ_ASSERT(aIsAbsPosCB == aInitialInline->IsAbsPosContainingBlock());

  nsIContent* content = aInitialInline->GetContent();
  ComputedStyle* computedStyle = aInitialInline->Style();
  nsContainerFrame* parentFrame = aInitialInline->GetParent();

  // Resolve the right style for our anonymous blocks.
  //
  // The distinction in styles is needed because of CSS 2.1, section
  // 9.2.1.1, which says:
  //
  //   When such an inline box is affected by relative positioning, any
  //   resulting translation also affects the block-level box contained
  //   in the inline box.
  RefPtr<ComputedStyle> blockSC =
      mPresShell->StyleSet()->ResolveInheritingAnonymousBoxStyle(
          PseudoStyleType::mozBlockInsideInlineWrapper, computedStyle);

  nsContainerFrame* lastNewInline =
      static_cast<nsContainerFrame*>(aInitialInline->FirstContinuation());
  do {
    // On entry to this loop aChildList is not empty and the first frame in it
    // is block-level.
    MOZ_ASSERT(aChildList.NotEmpty(), "Should have child items");
    MOZ_ASSERT(aChildList.FirstChild()->IsBlockOutside(),
               "Must have list starting with block");

    // The initial run of blocks belongs to an anonymous block that we create
    // right now. The anonymous block will be the parent of these block
    // children of the inline.
    nsBlockFrame* blockFrame = NS_NewBlockFrame(mPresShell, blockSC);
    InitAndRestoreFrame(aState, content, parentFrame, blockFrame, false);
    if (aInitialInline->HasAnyStateBits(NS_FRAME_HAS_MULTI_COLUMN_ANCESTOR)) {
      blockFrame->AddStateBits(NS_FRAME_HAS_MULTI_COLUMN_ANCESTOR);
    }

    // Find the first non-block child which defines the end of our block kids
    // and the start of our next inline's kids
    nsFrameList blockKids =
        aChildList.Split([](nsIFrame* f) { return !f->IsBlockOutside(); });

    if (!aInitialInline->HasAnyStateBits(NS_FRAME_HAS_MULTI_COLUMN_ANCESTOR)) {
      MoveChildrenTo(aInitialInline, blockFrame, blockKids);

      SetFrameIsIBSplit(lastNewInline, blockFrame);
      aSiblings.AppendFrame(nullptr, blockFrame);
    } else {
      // Extract any initial non-column-span frames, and put them in
      // blockFrame's child list.
      nsFrameList initialNonColumnSpanKids =
          blockKids.Split([](nsIFrame* f) { return f->IsColumnSpan(); });
      MoveChildrenTo(aInitialInline, blockFrame, initialNonColumnSpanKids);

      SetFrameIsIBSplit(lastNewInline, blockFrame);
      aSiblings.AppendFrame(nullptr, blockFrame);

      if (blockKids.NotEmpty()) {
        // Although SetFrameIsIBSplit() will add NS_FRAME_PART_OF_IBSPLIT for
        // blockFrame later, we manually add the bit earlier here to make all
        // the continuations of blockFrame created in
        // CreateColumnSpanSiblings(), i.e. non-column-span wrappers, have the
        // bit via nsIFrame::Init().
        blockFrame->AddStateBits(NS_FRAME_PART_OF_IBSPLIT);

        nsFrameList columnSpanSiblings =
            CreateColumnSpanSiblings(aState, blockFrame, blockKids,
                                     aIsAbsPosCB ? aInitialInline : nullptr);
        aSiblings.AppendFrames(nullptr, std::move(columnSpanSiblings));
      }
    }

    // Now grab the initial inlines in aChildList and put them into an inline
    // frame.
    nsInlineFrame* inlineFrame = NS_NewInlineFrame(mPresShell, computedStyle);
    InitAndRestoreFrame(aState, content, parentFrame, inlineFrame, false);
    inlineFrame->AddStateBits(NS_FRAME_CAN_HAVE_ABSPOS_CHILDREN);
    if (aInitialInline->HasAnyStateBits(NS_FRAME_HAS_MULTI_COLUMN_ANCESTOR)) {
      inlineFrame->AddStateBits(NS_FRAME_HAS_MULTI_COLUMN_ANCESTOR);
    }

    if (aIsAbsPosCB) {
      inlineFrame->MarkAsAbsoluteContainingBlock();
    }

    if (aChildList.NotEmpty()) {
      nsFrameList inlineKids =
          aChildList.Split([](nsIFrame* f) { return f->IsBlockOutside(); });
      MoveChildrenTo(aInitialInline, inlineFrame, inlineKids);
    }

    SetFrameIsIBSplit(blockFrame, inlineFrame);
    aSiblings.AppendFrame(nullptr, inlineFrame);
    lastNewInline = inlineFrame;
  } while (aChildList.NotEmpty());

  SetFrameIsIBSplit(lastNewInline, nullptr);
}

void nsCSSFrameConstructor::BuildInlineChildItems(
    nsFrameConstructorState& aState, FrameConstructionItem& aParentItem,
    bool aItemIsWithinSVGText, bool aItemAllowsTextPathChild) {
  ComputedStyle* const parentComputedStyle = aParentItem.mComputedStyle;
  nsIContent* const parentContent = aParentItem.mContent;

  if (!aItemIsWithinSVGText) {
    if (parentComputedStyle->StyleDisplay()->IsListItem()) {
      CreateGeneratedContentItem(aState, nullptr, *parentContent->AsElement(),
                                 *parentComputedStyle, PseudoStyleType::marker,
                                 aParentItem.mChildItems);
    }
    // Probe for generated content before
    CreateGeneratedContentItem(aState, nullptr, *parentContent->AsElement(),
                               *parentComputedStyle, PseudoStyleType::before,
                               aParentItem.mChildItems);
  }

  ItemFlags flags;
  if (aItemIsWithinSVGText) {
    flags += ItemFlag::IsWithinSVGText;
  }
  if (aItemAllowsTextPathChild &&
      aParentItem.mContent->IsSVGElement(nsGkAtoms::a)) {
    flags += ItemFlag::AllowTextPathChild;
  }

  FlattenedChildIterator iter(parentContent);
  for (nsIContent* content = iter.GetNextChild(); content;
       content = iter.GetNextChild()) {
    AddFrameConstructionItems(aState, content, iter.ShadowDOMInvolved(),
                              *parentComputedStyle, InsertionPoint(),
                              aParentItem.mChildItems, flags);
  }

  if (!aItemIsWithinSVGText) {
    // Probe for generated content after
    CreateGeneratedContentItem(aState, nullptr, *parentContent->AsElement(),
                               *parentComputedStyle, PseudoStyleType::after,
                               aParentItem.mChildItems);
  }

  aParentItem.mIsAllInline = aParentItem.mChildItems.AreAllItemsInline();
}

// return whether it's ok to append (in the AppendFrames sense) to
// aParentFrame if our nextSibling is aNextSibling.  aParentFrame must
// be an ib-split inline.
static bool IsSafeToAppendToIBSplitInline(nsIFrame* aParentFrame,
                                          nsIFrame* aNextSibling) {
  MOZ_ASSERT(IsInlineFrame(aParentFrame), "Must have an inline parent here");

  do {
    NS_ASSERTION(IsFramePartOfIBSplit(aParentFrame),
                 "How is this not part of an ib-split?");
    if (aNextSibling || aParentFrame->GetNextContinuation() ||
        GetIBSplitSibling(aParentFrame)) {
      return false;
    }

    aNextSibling = aParentFrame->GetNextSibling();
    aParentFrame = aParentFrame->GetParent();
  } while (IsInlineFrame(aParentFrame));

  return true;
}

bool nsCSSFrameConstructor::WipeInsertionParent(nsContainerFrame* aFrame) {
#define TRACE(reason)                                                  \
  PROFILER_MARKER("WipeInsertionParent: " reason, LAYOUT, {}, Tracing, \
                  "Layout");

  const LayoutFrameType frameType = aFrame->Type();

  // FIXME(emilio): This looks terribly inefficient if you insert elements deep
  // in a MathML subtree.
  if (aFrame->IsMathMLFrame()) {
    TRACE("MathML");
    RecreateFramesForContent(aFrame->GetContent(), InsertionKind::Async);
    return true;
  }

  // A ruby-related frame that's getting new children.
  // The situation for ruby is complex, especially when interacting with
  // spaces. It contains these two special cases apart from tables:
  // 1) There are effectively three types of white spaces in ruby frames
  //    we handle differently: leading/tailing/inter-level space,
  //    inter-base/inter-annotation space, and inter-segment space.
  //    These three types of spaces can be converted to each other when
  //    their sibling changes.
  // 2) The first effective child of a ruby frame must always be a ruby
  //    base container. It should be created or destroyed accordingly.
  if (IsRubyPseudo(aFrame) || frameType == LayoutFrameType::Ruby ||
      RubyUtils::IsRubyContainerBox(frameType)) {
    // We want to optimize it better, and avoid reframing as much as
    // possible. But given the cases above, and the fact that a ruby
    // usually won't be very large, it should be fine to reframe it.
    TRACE("Ruby");
    RecreateFramesForContent(aFrame->GetContent(), InsertionKind::Async);
    return true;
  }

  // Reframe the multi-column container whenever elements insert/append
  // into it because we need to reconstruct column-span split.
  if (aFrame->IsColumnSetWrapperFrame()) {
    TRACE("Multi-column");
    RecreateFramesForContent(aFrame->GetContent(), InsertionKind::Async);
    return true;
  }

  return false;

#undef TRACE
}

bool nsCSSFrameConstructor::WipeContainingBlock(
    nsFrameConstructorState& aState, nsIFrame* aContainingBlock,
    nsIFrame* aFrame, FrameConstructionItemList& aItems, bool aIsAppend,
    nsIFrame* aPrevSibling) {
#define TRACE(reason)                                                  \
  PROFILER_MARKER("WipeContainingBlock: " reason, LAYOUT, {}, Tracing, \
                  "Layout");

  if (aItems.IsEmpty()) {
    return false;
  }

  // Before we go and append the frames, we must check for several
  // special situations.

  if (aFrame->GetContent() == mDocument->GetRootElement()) {
    // Situation #1 is when we insert content that becomes the canonical body
    // element, and its used WritingMode is different from the root element's
    // used WritingMode.
    // We need to reframe the root element so that the root element's frames has
    // the correct writing-mode propagated from body element. (See
    // nsCSSFrameConstructor::ConstructDocElementFrame.)
    //
    // Bug 1594297: When inserting a new <body>, we may need to reframe the old
    // <body> which has a "overflow" value other than simple "visible". But it's
    // tricky, see bug 1593752.
    nsIContent* bodyElement = mDocument->GetBodyElement();
    for (FCItemIterator iter(aItems); !iter.IsDone(); iter.Next()) {
      const WritingMode bodyWM(iter.item().mComputedStyle);
      if (iter.item().mContent == bodyElement &&
          bodyWM != aFrame->GetWritingMode()) {
        TRACE("Root");
        RecreateFramesForContent(mDocument->GetRootElement(),
                                 InsertionKind::Async);
        return true;
      }
    }
  }

  nsIFrame* nextSibling = ::GetInsertNextSibling(aFrame, aPrevSibling);

  // Situation #2 is a flex / grid  container frame into which we're inserting
  // new inline non-replaced children, adjacent to an existing anonymous flex or
  // grid item.
  if (aFrame->IsFlexOrGridContainer()) {
    FCItemIterator iter(aItems);

    // Check if we're adding to-be-wrapped content right *after* an existing
    // anonymous flex or grid item (which would need to absorb this content).
    const bool isLegacyWebKitBox = IsFlexContainerForLegacyWebKitBox(aFrame);
    if (aPrevSibling && IsAnonymousItem(aPrevSibling) &&
        iter.item().NeedsAnonFlexOrGridItem(aState, isLegacyWebKitBox)) {
      TRACE("Inserting inline after anon flex or grid item");
      RecreateFramesForContent(aFrame->GetContent(), InsertionKind::Async);
      return true;
    }

    // Check if we're adding to-be-wrapped content right *before* an existing
    // anonymous flex or grid item (which would need to absorb this content).
    if (nextSibling && IsAnonymousItem(nextSibling)) {
      // Jump to the last entry in the list
      iter.SetToEnd();
      iter.Prev();
      if (iter.item().NeedsAnonFlexOrGridItem(aState, isLegacyWebKitBox)) {
        TRACE("Inserting inline before anon flex or grid item");
        RecreateFramesForContent(aFrame->GetContent(), InsertionKind::Async);
        return true;
      }
    }
  }

  // Situation #3 is an anonymous flex or grid item that's getting new children
  // who don't want to be wrapped.
  if (IsAnonymousItem(aFrame)) {
    AssertAnonymousFlexOrGridItemParent(aFrame, aFrame->GetParent());

    // We need to push a null float containing block to be sure that
    // "NeedsAnonFlexOrGridItem" will know we're not honoring floats for this
    // inserted content. (In particular, this is necessary in order for
    // its "GetGeometricParent" call to return the correct result.)
    // We're not honoring floats on this content because it has the
    // _flex/grid container_ as its parent in the content tree.
    nsFrameConstructorSaveState floatSaveState;
    aState.PushFloatContainingBlock(nullptr, floatSaveState);

    FCItemIterator iter(aItems);
    // Skip over things that _do_ need an anonymous flex item, because
    // they're perfectly happy to go here -- they won't cause a reframe.
    nsIFrame* containerFrame = aFrame->GetParent();
    const bool isLegacyWebKitBox =
        IsFlexContainerForLegacyWebKitBox(containerFrame);
    if (!iter.SkipItemsThatNeedAnonFlexOrGridItem(aState, isLegacyWebKitBox)) {
      // We hit something that _doesn't_ need an anonymous flex item!
      // Rebuild the flex container to bust it out.
      TRACE("Inserting non-inlines inside anon flex or grid item");
      RecreateFramesForContent(containerFrame->GetContent(),
                               InsertionKind::Async);
      return true;
    }

    // If we get here, then everything in |aItems| needs to be wrapped in
    // an anonymous flex or grid item.  That's where it's already going - good!
  }

  // Situation #4 is a case when table pseudo-frames don't work out right
  ParentType parentType = GetParentType(aFrame);
  // If all the kids want a parent of the type that aFrame is, then we're all
  // set to go.  Indeed, there won't be any table pseudo-frames created between
  // aFrame and the kids, so those won't need to be merged with any table
  // pseudo-frames that might already be kids of aFrame.  If aFrame itself is a
  // table pseudo-frame, then all the kids in this list would have wanted a
  // frame of that type wrapping them anyway, so putting them inside it is ok.
  if (!aItems.AllWantParentType(parentType)) {
    // Don't give up yet.  If parentType is not eTypeBlock and the parent is
    // not a generated content frame, then try filtering whitespace out of the
    // list.
    if (parentType != eTypeBlock && !aFrame->IsGeneratedContentFrame()) {
      // For leading whitespace followed by a kid that wants our parent type,
      // there are four cases:
      // 1) We have a previous sibling which is not a table pseudo.  That means
      //    that previous sibling wanted a (non-block) parent of the type we're
      //    looking at.  Then the whitespace comes between two table-internal
      //    elements, so should be collapsed out.
      // 2) We have a previous sibling which is a table pseudo.  It might have
      //    kids who want this whitespace, so we need to reframe.
      // 3) We have no previous sibling and our parent frame is not a table
      //    pseudo.  That means that we'll be at the beginning of our actual
      //    non-block-type parent, and the whitespace is OK to collapse out.
      //    If something is ever inserted before us, it'll find our own parent
      //    as its parent and if it's something that would care about the
      //    whitespace it'll want a block parent, so it'll trigger a reframe at
      //    that point.
      // 4) We have no previous sibling and our parent frame is a table pseudo.
      //    Need to reframe.
      // All that is predicated on finding the correct previous sibling.  We
      // might have to walk backwards along continuations from aFrame to do so.
      //
      // It's always OK to drop whitespace between any two items that want a
      // parent of type parentType.
      //
      // For trailing whitespace preceded by a kid that wants our parent type,
      // there are four cases:
      // 1) We have a next sibling which is not a table pseudo.  That means
      //    that next sibling wanted a (non-block) parent of the type we're
      //    looking at.  Then the whitespace comes between two table-internal
      //    elements, so should be collapsed out.
      // 2) We have a next sibling which is a table pseudo.  It might have
      //    kids who want this whitespace, so we need to reframe.
      // 3) We have no next sibling and our parent frame is not a table
      //    pseudo.  That means that we'll be at the end of our actual
      //    non-block-type parent, and the whitespace is OK to collapse out.
      //    If something is ever inserted after us, it'll find our own parent
      //    as its parent and if it's something that would care about the
      //    whitespace it'll want a block parent, so it'll trigger a reframe at
      //    that point.
      // 4) We have no next sibling and our parent frame is a table pseudo.
      //    Need to reframe.
      // All that is predicated on finding the correct next sibling.  We might
      // have to walk forward along continuations from aFrame to do so.  That
      // said, in the case when nextSibling is null at this point and aIsAppend
      // is true, we know we're in case 3.  Furthermore, in that case we don't
      // even have to worry about the table pseudo situation; we know our
      // parent is not a table pseudo there.
      FCItemIterator iter(aItems);
      FCItemIterator start(iter);
      do {
        if (iter.SkipItemsWantingParentType(parentType)) {
          break;
        }

        // iter points to an item that wants a different parent.  If it's not
        // whitespace, we're done; no more point scanning the list.
        if (!iter.item().IsWhitespace(aState)) {
          break;
        }

        if (iter == start) {
          // Leading whitespace.  How to handle this depends on our
          // previous sibling and aFrame.  See the long comment above.
          nsIFrame* prevSibling = aPrevSibling;
          if (!prevSibling) {
            // Try to find one after all
            nsIFrame* parentPrevCont = aFrame->GetPrevContinuation();
            while (parentPrevCont) {
              prevSibling = parentPrevCont->PrincipalChildList().LastChild();
              if (prevSibling) {
                break;
              }
              parentPrevCont = parentPrevCont->GetPrevContinuation();
            }
          };
          if (prevSibling) {
            if (IsTablePseudo(prevSibling)) {
              // need to reframe
              break;
            }
          } else if (IsTablePseudo(aFrame)) {
            // need to reframe
            break;
          }
        }

        FCItemIterator spaceEndIter(iter);
        // Advance spaceEndIter past any whitespace
        bool trailingSpaces = spaceEndIter.SkipWhitespace(aState);

        bool okToDrop;
        if (trailingSpaces) {
          // Trailing whitespace.  How to handle this depeds on aIsAppend, our
          // next sibling and aFrame.  See the long comment above.
          okToDrop = aIsAppend && !nextSibling;
          if (!okToDrop) {
            if (!nextSibling) {
              // Try to find one after all
              nsIFrame* parentNextCont = aFrame->GetNextContinuation();
              while (parentNextCont) {
                nextSibling = parentNextCont->PrincipalChildList().FirstChild();
                if (nextSibling) {
                  break;
                }
                parentNextCont = parentNextCont->GetNextContinuation();
              }
            }

            okToDrop = (nextSibling && !IsTablePseudo(nextSibling)) ||
                       (!nextSibling && !IsTablePseudo(aFrame));
          }
#ifdef DEBUG
          else {
            NS_ASSERTION(!IsTablePseudo(aFrame), "How did that happen?");
          }
#endif
        } else {
          okToDrop = (spaceEndIter.item().DesiredParentType() == parentType);
        }

        if (okToDrop) {
          iter.DeleteItemsTo(this, spaceEndIter);
        } else {
          // We're done: we don't want to drop the whitespace, and it has the
          // wrong parent type.
          break;
        }

        // Now loop, since |iter| points to item right after the whitespace we
        // removed.
      } while (!iter.IsDone());
    }

    // We might be able to figure out some sort of optimizations here, but they
    // would have to depend on having a correct aPrevSibling and a correct next
    // sibling.  For example, we can probably avoid reframing if none of
    // aFrame, aPrevSibling, and next sibling are table pseudo-frames.  But it
    // doesn't seem worth it to worry about that for now, especially since we
    // in fact do not have a reliable aPrevSibling, nor any next sibling, in
    // this method.

    // aItems might have changed, so recheck the parent type thing.  In fact,
    // it might be empty, so recheck that too.
    if (aItems.IsEmpty()) {
      return false;
    }

    if (!aItems.AllWantParentType(parentType)) {
      // Reframing aFrame->GetContent() is good enough, since the content of
      // table pseudo-frames is the ancestor content.
      TRACE("Pseudo-frames going wrong");
      RecreateFramesForContent(aFrame->GetContent(), InsertionKind::Async);
      return true;
    }
  }

  // Situation #5 is a frame in multicol subtree that's getting new children.
  if (aFrame->HasAnyStateBits(NS_FRAME_HAS_MULTI_COLUMN_ANCESTOR)) {
    bool anyColumnSpanItems = false;
    for (FCItemIterator iter(aItems); !iter.IsDone(); iter.Next()) {
      if (iter.item().mComputedStyle->StyleColumn()->IsColumnSpanStyle()) {
        anyColumnSpanItems = true;
        break;
      }
    }

    bool needsReframe =
        // 1. Insert / append any column-span children.
        anyColumnSpanItems ||
        // 2. GetInsertionPrevSibling() modifies insertion parent. If the prev
        // sibling is a column-span, aFrame ends up being the
        // column-span-wrapper.
        aFrame->Style()->GetPseudoType() ==
            PseudoStyleType::columnSpanWrapper ||
        // 3. Append into {ib} split container. There might be room for
        // optimization, but let's reframe for correctness...
        IsFramePartOfIBSplit(aFrame);

    if (needsReframe) {
      TRACE("Multi-column");
      RecreateFramesForContent(
          GetMultiColumnContainingBlockFor(aFrame)->GetContent(),
          InsertionKind::Async);
      return true;
    }

    // If we get here, then we need further check for {ib} split to decide
    // whether to reframe. For example, appending a block into an empty inline
    // that is not part of an {ib} split, but should become an {ib} split.
  }

  // A <fieldset> may need to pick up a new rendered legend from aItems.
  // We currently can't handle this case without recreating frames for
  // the fieldset.
  // XXXmats we should be able to optimize this when the fieldset doesn't
  // currently have a rendered legend.  ContentRangeInserted needs to be fixed
  // to use the inner frame as the content insertion frame in that case.
  if (const auto* fieldset = GetFieldSetFrameFor(aFrame)) {
    // Check if any item is eligible to be a rendered legend.
    for (FCItemIterator iter(aItems); !iter.IsDone(); iter.Next()) {
      const auto& item = iter.item();
      if (!item.mContent->IsHTMLElement(nsGkAtoms::legend)) {
        continue;
      }
      const auto* display = item.mComputedStyle->StyleDisplay();
      if (display->IsFloatingStyle() ||
          display->IsAbsolutelyPositionedStyle()) {
        continue;
      }
      TRACE("Fieldset with rendered legend");
      RecreateFramesForContent(fieldset->GetContent(), InsertionKind::Async);
      return true;
    }
  }

  // Now we have several cases involving {ib} splits.  Put them all in a
  // do/while with breaks to take us to the "go and reconstruct" code.
  do {
    if (IsInlineFrame(aFrame)) {
      if (aItems.AreAllItemsInline()) {
        // We can just put the kids in.
        return false;
      }

      if (!IsFramePartOfIBSplit(aFrame)) {
        // Need to go ahead and reconstruct.
        break;
      }

      // Now we're adding kids including some blocks to an inline part of an
      // {ib} split.  If we plan to call AppendFrames, and don't have a next
      // sibling for the new frames, and our parent is the last continuation of
      // the last part of the {ib} split, and the same is true of all our
      // ancestor inlines (they have no following continuations and they're the
      // last part of their {ib} splits and we'd be adding to the end for all
      // of them), then AppendFrames will handle things for us.  Bail out in
      // that case.
      if (aIsAppend && IsSafeToAppendToIBSplitInline(aFrame, nextSibling)) {
        return false;
      }

      // Need to reconstruct.
      break;
    }

    // Now we know we have a block parent.  If it's not part of an
    // ib-split, we're all set.
    if (!IsFramePartOfIBSplit(aFrame)) {
      return false;
    }

    // We're adding some kids to a block part of an {ib} split.  If all the
    // kids are blocks, we don't need to reconstruct.
    if (aItems.AreAllItemsBlock()) {
      return false;
    }

    // We might have some inline kids for this block.  Just fall out of the
    // loop and reconstruct.
  } while (0);

  // If we don't have a containing block, start with aFrame and look for one.
  if (!aContainingBlock) {
    aContainingBlock = aFrame;
  }

  // To find the right block to reframe, just walk up the tree until we find a
  // frame that is:
  // 1)  Not part of an IB split
  // 2)  Not a pseudo-frame
  // 3)  Not an inline frame
  // We're guaranteed to find one, since ComputedStyle::ApplyStyleFixups
  // enforces that the root is display:none, display:table, or display:block.
  // Note that walking up "too far" is OK in terms of correctness, even if it
  // might be a little inefficient.  This is why we walk out of all
  // pseudo-frames -- telling which ones are or are not OK to walk out of is
  // too hard (and I suspect that we do in fact need to walk out of all of
  // them).
  while (IsFramePartOfIBSplit(aContainingBlock) ||
         aContainingBlock->IsInlineOutside() ||
         aContainingBlock->Style()->IsPseudoOrAnonBox()) {
    aContainingBlock = aContainingBlock->GetParent();
    NS_ASSERTION(aContainingBlock,
                 "Must have non-inline, non-ib-split, non-pseudo frame as "
                 "root (or child of root, for a table root)!");
  }

  // Tell parent of the containing block to reformulate the
  // entire block. This is painful and definitely not optimal
  // but it will *always* get the right answer.

  nsIContent* blockContent = aContainingBlock->GetContent();
  TRACE("IB splits");
  RecreateFramesForContent(blockContent, InsertionKind::Async);
  return true;
#undef TRACE
}

void nsCSSFrameConstructor::ReframeContainingBlock(nsIFrame* aFrame) {
  // XXXbz how exactly would we get here while isReflowing anyway?  Should this
  // whole test be ifdef DEBUG?
  if (mPresShell->IsReflowLocked()) {
    // don't ReframeContainingBlock, this will result in a crash
    // if we remove a tree that's in reflow - see bug 121368 for testcase
    NS_ERROR(
        "Atemptted to nsCSSFrameConstructor::ReframeContainingBlock during a "
        "Reflow!!!");
    return;
  }

  // Get the first "normal" ancestor of the target frame.
  nsIFrame* containingBlock = GetIBContainingBlockFor(aFrame);
  if (containingBlock) {
    // From here we look for the containing block in case the target
    // frame is already a block (which can happen when an inline frame
    // wraps some of its content in an anonymous block; see
    // ConstructInline)

    // NOTE: We used to get the FloatContainingBlock here, but it was often
    // wrong. GetIBContainingBlock works much better and provides the correct
    // container in all cases so GetFloatContainingBlock(aFrame) has been
    // removed

    // And get the containingBlock's content
    if (nsIContent* blockContent = containingBlock->GetContent()) {
#ifdef DEBUG
      if (gNoisyContentUpdates) {
        printf("  ==> blockContent=%p\n", blockContent);
      }
#endif
      RecreateFramesForContent(blockContent, InsertionKind::Async);
      return;
    }
  }

  // If we get here, we're screwed!
  RecreateFramesForContent(mPresShell->GetDocument()->GetRootElement(),
                           InsertionKind::Async);
}

//////////////////////////////////////////////////////////
// nsCSSFrameConstructor::FrameConstructionItem methods //
//////////////////////////////////////////////////////////
bool nsCSSFrameConstructor::FrameConstructionItem::IsWhitespace(
    nsFrameConstructorState& aState) const {
  MOZ_ASSERT(aState.mCreatingExtraFrames || !mContent->GetPrimaryFrame(),
             "How did that happen?");
  if (!mIsText) {
    return false;
  }
  mContent->SetFlags(NS_CREATE_FRAME_IF_NON_WHITESPACE |
                     NS_REFRAME_IF_WHITESPACE);
  return mContent->TextIsOnlyWhitespace();
}

//////////////////////////////////////////////////////////////
// nsCSSFrameConstructor::FrameConstructionItemList methods //
//////////////////////////////////////////////////////////////
void nsCSSFrameConstructor::FrameConstructionItemList::AdjustCountsForItem(
    FrameConstructionItem* aItem, int32_t aDelta) {
  MOZ_ASSERT(aDelta == 1 || aDelta == -1, "Unexpected delta");
  mItemCount += aDelta;
  if (aItem->mIsAllInline) {
    mInlineCount += aDelta;
  }
  if (aItem->mIsBlock) {
    mBlockCount += aDelta;
  }
  mDesiredParentCounts[aItem->DesiredParentType()] += aDelta;
}

////////////////////////////////////////////////////////////////////////
// nsCSSFrameConstructor::FrameConstructionItemList::Iterator methods //
////////////////////////////////////////////////////////////////////////
inline bool nsCSSFrameConstructor::FrameConstructionItemList::Iterator::
    SkipItemsWantingParentType(ParentType aParentType) {
  MOZ_ASSERT(!IsDone(), "Shouldn't be done yet");
  while (item().DesiredParentType() == aParentType) {
    Next();
    if (IsDone()) {
      return true;
    }
  }
  return false;
}

inline bool nsCSSFrameConstructor::FrameConstructionItemList::Iterator::
    SkipItemsNotWantingParentType(ParentType aParentType) {
  MOZ_ASSERT(!IsDone(), "Shouldn't be done yet");
  while (item().DesiredParentType() != aParentType) {
    Next();
    if (IsDone()) {
      return true;
    }
  }
  return false;
}

// Note: we implement -webkit-{inline-}box using nsFlexContainerFrame, but we
// use different rules for what gets wrapped in an anonymous flex item.
bool nsCSSFrameConstructor::FrameConstructionItem::NeedsAnonFlexOrGridItem(
    const nsFrameConstructorState& aState, bool aIsLegacyWebKitBox) {
  if (mFCData->mBits & FCDATA_IS_LINE_PARTICIPANT) {
    // This will be an inline non-replaced box.
    return true;
  }

  if (aIsLegacyWebKitBox) {
    if (mComputedStyle->StyleDisplay()->IsInlineOutsideStyle()) {
      // In an emulated legacy box, all inline-level content gets wrapped in an
      // anonymous flex item.
      return true;
    }
    if (mIsPopup ||
        (!(mFCData->mBits & FCDATA_DISALLOW_OUT_OF_FLOW) &&
         aState.GetGeometricParent(*mComputedStyle->StyleDisplay(), nullptr))) {
      // We're abspos or fixedpos (or a XUL popup), which means we'll spawn a
      // placeholder which (because our container is an emulated legacy box)
      // we'll need to wrap in an anonymous flex item.  So, we just treat
      // _this_ frame as if _it_ needs to be wrapped in an anonymous flex item,
      // and then when we spawn the placeholder, it'll end up in the right
      // spot.
      return true;
    }
  }

  return false;
}

inline bool nsCSSFrameConstructor::FrameConstructionItemList::Iterator::
    SkipItemsThatNeedAnonFlexOrGridItem(const nsFrameConstructorState& aState,
                                        bool aIsLegacyWebKitBox) {
  MOZ_ASSERT(!IsDone(), "Shouldn't be done yet");
  while (item().NeedsAnonFlexOrGridItem(aState, aIsLegacyWebKitBox)) {
    Next();
    if (IsDone()) {
      return true;
    }
  }
  return false;
}

inline bool nsCSSFrameConstructor::FrameConstructionItemList::Iterator::
    SkipItemsThatDontNeedAnonFlexOrGridItem(
        const nsFrameConstructorState& aState, bool aIsLegacyWebKitBox) {
  MOZ_ASSERT(!IsDone(), "Shouldn't be done yet");
  while (!(item().NeedsAnonFlexOrGridItem(aState, aIsLegacyWebKitBox))) {
    Next();
    if (IsDone()) {
      return true;
    }
  }
  return false;
}

inline bool nsCSSFrameConstructor::FrameConstructionItemList::Iterator::
    SkipItemsNotWantingRubyParent() {
  MOZ_ASSERT(!IsDone(), "Shouldn't be done yet");
  while (!IsRubyParentType(item().DesiredParentType())) {
    Next();
    if (IsDone()) {
      return true;
    }
  }
  return false;
}

inline bool
nsCSSFrameConstructor::FrameConstructionItemList::Iterator::SkipWhitespace(
    nsFrameConstructorState& aState) {
  MOZ_ASSERT(!IsDone(), "Shouldn't be done yet");
  MOZ_ASSERT(item().IsWhitespace(aState), "Not pointing to whitespace?");
  do {
    Next();
    if (IsDone()) {
      return true;
    }
  } while (item().IsWhitespace(aState));

  return false;
}

void nsCSSFrameConstructor::FrameConstructionItemList::Iterator::
    AppendItemToList(FrameConstructionItemList& aTargetList) {
  NS_ASSERTION(&aTargetList != &mList, "Unexpected call");
  MOZ_ASSERT(!IsDone(), "should not be done");

  FrameConstructionItem* item = mCurrent;
  Next();
  item->remove();
  aTargetList.mItems.insertBack(item);

  mList.AdjustCountsForItem(item, -1);
  aTargetList.AdjustCountsForItem(item, 1);
}

void nsCSSFrameConstructor::FrameConstructionItemList::Iterator::
    AppendItemsToList(nsCSSFrameConstructor* aFCtor, const Iterator& aEnd,
                      FrameConstructionItemList& aTargetList) {
  NS_ASSERTION(&aTargetList != &mList, "Unexpected call");
  MOZ_ASSERT(&mList == &aEnd.mList, "End iterator for some other list?");

  // We can't just move our guts to the other list if it already has
  // some information or if we're not moving our entire list.
  if (!AtStart() || !aEnd.IsDone() || !aTargetList.IsEmpty()) {
    do {
      AppendItemToList(aTargetList);
    } while (*this != aEnd);
    return;
  }

  // Move our entire list of items into the empty target list.
  aTargetList.mItems = std::move(mList.mItems);

  // Copy over the various counters
  aTargetList.mInlineCount = mList.mInlineCount;
  aTargetList.mBlockCount = mList.mBlockCount;
  aTargetList.mItemCount = mList.mItemCount;
  memcpy(aTargetList.mDesiredParentCounts, mList.mDesiredParentCounts,
         sizeof(aTargetList.mDesiredParentCounts));

  // reset mList
  mList.Reset(aFCtor);

  // Point ourselves to aEnd, as advertised
  SetToEnd();
  MOZ_ASSERT(*this == aEnd, "How did that happen?");
}

void nsCSSFrameConstructor::FrameConstructionItemList::Iterator::InsertItem(
    FrameConstructionItem* aItem) {
  if (IsDone()) {
    mList.mItems.insertBack(aItem);
  } else {
    // Just insert the item before us.  There's no magic here.
    mCurrent->setPrevious(aItem);
  }
  mList.AdjustCountsForItem(aItem, 1);

  MOZ_ASSERT(aItem->getNext() == mCurrent, "How did that happen?");
}

void nsCSSFrameConstructor::FrameConstructionItemList::Iterator::DeleteItemsTo(
    nsCSSFrameConstructor* aFCtor, const Iterator& aEnd) {
  MOZ_ASSERT(&mList == &aEnd.mList, "End iterator for some other list?");
  MOZ_ASSERT(*this != aEnd, "Shouldn't be at aEnd yet");

  do {
    NS_ASSERTION(!IsDone(), "Ran off end of list?");
    FrameConstructionItem* item = mCurrent;
    Next();
    item->remove();
    mList.AdjustCountsForItem(item, -1);
    item->Delete(aFCtor);
  } while (*this != aEnd);
}

void nsCSSFrameConstructor::QuotesDirty() {
  mQuotesDirty = true;
  mPresShell->SetNeedLayoutFlush();
}

void nsCSSFrameConstructor::CountersDirty() {
  mCountersDirty = true;
  mPresShell->SetNeedLayoutFlush();
}

void* nsCSSFrameConstructor::AllocateFCItem() {
  void* item;
  if (mFirstFreeFCItem) {
    item = mFirstFreeFCItem;
    mFirstFreeFCItem = mFirstFreeFCItem->mNext;
  } else {
    item = mFCItemPool.Allocate(sizeof(FrameConstructionItem));
  }
  ++mFCItemsInUse;
  return item;
}

void nsCSSFrameConstructor::FreeFCItem(FrameConstructionItem* aItem) {
  MOZ_ASSERT(mFCItemsInUse != 0);
  if (--mFCItemsInUse == 0) {
    // The arena is now unused - clear it but retain one chunk.
    mFirstFreeFCItem = nullptr;
    mFCItemPool.Clear();
  } else {
    // Prepend it to the list of free items.
    FreeFCItemLink* item = reinterpret_cast<FreeFCItemLink*>(aItem);
    item->mNext = mFirstFreeFCItem;
    mFirstFreeFCItem = item;
  }
}

void nsCSSFrameConstructor::AddSizeOfIncludingThis(
    nsWindowSizes& aSizes) const {
  if (nsIFrame* rootFrame = GetRootFrame()) {
    rootFrame->AddSizeOfExcludingThisForTree(aSizes);
    if (RetainedDisplayListBuilder* builder =
            rootFrame->GetProperty(RetainedDisplayListBuilder::Cached())) {
      builder->AddSizeOfIncludingThis(aSizes);
    }
  }

  // This must be done after measuring from the frame tree, since frame
  // manager will measure sizes of staled computed values and style
  // structs, which only make sense after we know what are being used.
  nsFrameManager::AddSizeOfIncludingThis(aSizes);

  // Measurement of the following members may be added later if DMD finds it
  // is worthwhile:
  // - mFCItemPool
  // - mContainStyleScopeManager
}