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

//! Creates flows and fragments from a DOM tree via a bottom-up, incremental traversal of the DOM.
//!
//! Each step of the traversal considers the node and existing flow, if there is one. If a node is
//! not dirty and an existing flow exists, then the traversal reuses that flow. Otherwise, it
//! proceeds to construct either a flow or a `ConstructionItem`. A construction item is a piece of
//! intermediate data that goes with a DOM node and hasn't found its "home" yet-maybe it's a box,
//! maybe it's an absolute or fixed position thing that hasn't found its containing block yet.
//! Construction items bubble up the tree from children to parents until they find their homes.

use std::collections::LinkedList;
use std::marker::PhantomData;
use std::mem;
use std::sync::atomic::Ordering;
use std::sync::Arc;

use html5ever::{local_name, namespace_url, ns};
use log::debug;
use script_layout_interface::wrapper_traits::{
    PseudoElementType, ThreadSafeLayoutElement, ThreadSafeLayoutNode,
};
use script_layout_interface::{LayoutElementType, LayoutNodeType};
use servo_config::opts;
use servo_url::ServoUrl;
use style::computed_values::caption_side::T as CaptionSide;
use style::computed_values::display::T as Display;
use style::computed_values::empty_cells::T as EmptyCells;
use style::computed_values::float::T as Float;
use style::computed_values::list_style_position::T as ListStylePosition;
use style::computed_values::position::T as Position;
use style::context::SharedStyleContext;
use style::dom::TElement;
use style::logical_geometry::Direction;
use style::properties::ComputedValues;
use style::selector_parser::{PseudoElement, RestyleDamage};
use style::servo::restyle_damage::ServoRestyleDamage;
use style::values::computed::Image;
use style::values::generics::counters::ContentItem;
use style::LocalName;

use crate::block::BlockFlow;
use crate::context::LayoutContext;
use crate::data::{InnerLayoutData, LayoutDataFlags};
use crate::display_list::items::OpaqueNode;
use crate::flex::FlexFlow;
use crate::floats::FloatKind;
use crate::flow::{
    AbsoluteDescendants, Flow, FlowClass, FlowFlags, GetBaseFlow, ImmutableFlowUtils,
    MutableFlowUtils, MutableOwnedFlowUtils,
};
use crate::flow_ref::FlowRef;
use crate::fragment::{
    CanvasFragmentInfo, Fragment, FragmentFlags, GeneratedContentInfo, IframeFragmentInfo,
    ImageFragmentInfo, InlineAbsoluteFragmentInfo, InlineAbsoluteHypotheticalFragmentInfo,
    InlineBlockFragmentInfo, MediaFragmentInfo, SpecificFragmentInfo, SvgFragmentInfo,
    TableColumnFragmentInfo, UnscannedTextFragmentInfo, WhitespaceStrippingResult,
};
use crate::inline::{InlineFlow, InlineFragmentNodeFlags, InlineFragmentNodeInfo};
use crate::linked_list::prepend_from;
use crate::list_item::{ListItemFlow, ListStyleTypeContent};
use crate::multicol::{MulticolColumnFlow, MulticolFlow};
use crate::table::TableFlow;
use crate::table_caption::TableCaptionFlow;
use crate::table_cell::TableCellFlow;
use crate::table_colgroup::TableColGroupFlow;
use crate::table_row::TableRowFlow;
use crate::table_rowgroup::TableRowGroupFlow;
use crate::table_wrapper::TableWrapperFlow;
use crate::text::TextRunScanner;
use crate::traversal::PostorderNodeMutTraversal;
use crate::wrapper::{TextContent, ThreadSafeLayoutNodeHelpers};
use crate::{parallel, ServoArc};

/// The results of flow construction for a DOM node.
#[derive(Clone, Default)]
pub enum ConstructionResult {
    /// This node contributes nothing at all (`display: none`). Alternately, this is what newly
    /// created nodes have their `ConstructionResult` set to.
    #[default]
    None,

    /// This node contributed a flow at the proper position in the tree.
    /// Nothing more needs to be done for this node. It has bubbled up fixed
    /// and absolute descendant flows that have a containing block above it.
    Flow(FlowRef, AbsoluteDescendants),

    /// This node contributed some object or objects that will be needed to construct a proper flow
    /// later up the tree, but these objects have not yet found their home.
    ConstructionItem(ConstructionItem),
}

impl ConstructionResult {
    pub fn get(&mut self) -> ConstructionResult {
        // FIXME(pcwalton): Stop doing this with inline fragments. Cloning fragments is very
        // inefficient!
        (*self).clone()
    }

    pub fn debug_id(&self) -> usize {
        match *self {
            ConstructionResult::None => 0,
            ConstructionResult::ConstructionItem(_) => 0,
            ConstructionResult::Flow(ref flow_ref, _) => flow_ref.base().debug_id(),
        }
    }
}

/// Represents the output of flow construction for a DOM node that has not yet resulted in a
/// complete flow. Construction items bubble up the tree until they find a `Flow` to be attached
/// to.
#[derive(Clone)]
pub enum ConstructionItem {
    /// Inline fragments and associated {ib} splits that have not yet found flows.
    InlineFragments(InlineFragmentsConstructionResult),
    /// Potentially ignorable whitespace.
    ///
    /// FIXME(emilio): How could whitespace have any PseudoElementType other
    /// than Normal?
    Whitespace(
        OpaqueNode,
        PseudoElementType,
        ServoArc<ComputedValues>,
        RestyleDamage,
    ),
    /// TableColumn Fragment
    TableColumnFragment(Fragment),
}

/// Represents inline fragments and {ib} splits that are bubbling up from an inline.
#[derive(Clone)]
pub struct InlineFragmentsConstructionResult {
    /// Any {ib} splits that we're bubbling up.
    pub splits: LinkedList<InlineBlockSplit>,

    /// Any fragments that succeed the {ib} splits.
    pub fragments: IntermediateInlineFragments,
}

/// Represents an {ib} split that has not yet found the containing block that it belongs to. This
/// is somewhat tricky. An example may be helpful. For this DOM fragment:
///
/// ```html
///     <span>
///     A
///     <div>B</div>
///     C
///     </span>
/// ```
///
/// The resulting `ConstructionItem` for the outer `span` will be:
///
/// ```rust,ignore
///     ConstructionItem::InlineFragments(
///         InlineFragmentsConstructionResult {
///             splits: linked_list![
///                 InlineBlockSplit {
///                     predecessors: IntermediateInlineFragments {
///                         fragments: linked_list![A],
///                         absolute_descendents: AbsoluteDescendents {
///                             descendant_links: vec![]
///                         },
///                     },
///                     flow: B,
///                 }
///             ],
///             fragments: linked_list![C],
///         },
///     )
/// ```
#[derive(Clone)]
pub struct InlineBlockSplit {
    /// The inline fragments that precede the flow.
    pub predecessors: IntermediateInlineFragments,

    /// The flow that caused this {ib} split.
    pub flow: FlowRef,
}

impl InlineBlockSplit {
    /// Flushes the given accumulator to the new split and makes a new accumulator to hold any
    /// subsequent fragments.
    fn new<'dom, ConcreteThreadSafeLayoutNode>(
        fragment_accumulator: &mut InlineFragmentsAccumulator,
        node: &ConcreteThreadSafeLayoutNode,
        style_context: &SharedStyleContext,
        flow: FlowRef,
    ) -> InlineBlockSplit
    where
        ConcreteThreadSafeLayoutNode: ThreadSafeLayoutNode<'dom>,
    {
        fragment_accumulator.enclosing_node.as_mut().expect(
            "enclosing_node is None; Are {ib} splits being generated outside of an inline node?"
        ).flags.remove(InlineFragmentNodeFlags::LAST_FRAGMENT_OF_ELEMENT);

        let split = InlineBlockSplit {
            predecessors: mem::replace(
                fragment_accumulator,
                InlineFragmentsAccumulator::from_inline_node(node, style_context),
            )
            .get_intermediate_inline_fragments::<ConcreteThreadSafeLayoutNode>(style_context),
            flow,
        };

        fragment_accumulator
            .enclosing_node
            .as_mut()
            .unwrap()
            .flags
            .remove(InlineFragmentNodeFlags::FIRST_FRAGMENT_OF_ELEMENT);

        split
    }
}

/// Holds inline fragments and absolute descendants.
#[derive(Clone)]
pub struct IntermediateInlineFragments {
    /// The list of fragments.
    pub fragments: LinkedList<Fragment>,

    /// The list of absolute descendants of those inline fragments.
    pub absolute_descendants: AbsoluteDescendants,
}

impl IntermediateInlineFragments {
    fn new() -> IntermediateInlineFragments {
        IntermediateInlineFragments {
            fragments: LinkedList::new(),
            absolute_descendants: AbsoluteDescendants::new(),
        }
    }

    fn is_empty(&self) -> bool {
        self.fragments.is_empty() && self.absolute_descendants.is_empty()
    }

    fn push_all(&mut self, mut other: IntermediateInlineFragments) {
        self.fragments.append(&mut other.fragments);
        self.absolute_descendants
            .push_descendants(other.absolute_descendants);
    }
}

/// Holds inline fragments that we're gathering for children of an inline node.
struct InlineFragmentsAccumulator {
    /// The list of fragments.
    fragments: IntermediateInlineFragments,

    /// Information about the inline box directly enclosing the fragments being gathered, if any.
    ///
    /// `inline::InlineFragmentNodeInfo` also stores flags indicating whether a fragment is the
    /// first and/or last of the corresponding inline box. This `InlineFragmentsAccumulator` may
    /// represent only one side of an {ib} split, so we store these flags as if it represented only
    /// one fragment. `to_intermediate_inline_fragments` later splits this hypothetical fragment
    /// into pieces, leaving the `FIRST_FRAGMENT_OF_ELEMENT` and `LAST_FRAGMENT_OF_ELEMENT` flags,
    /// if present, on the first and last fragments of the output.
    enclosing_node: Option<InlineFragmentNodeInfo>,

    /// Restyle damage to use for fragments created in this node.
    restyle_damage: RestyleDamage,

    /// Bidi control characters to insert before and after these fragments.
    bidi_control_chars: Option<(&'static str, &'static str)>,
}

impl InlineFragmentsAccumulator {
    fn new() -> InlineFragmentsAccumulator {
        InlineFragmentsAccumulator {
            fragments: IntermediateInlineFragments::new(),
            enclosing_node: None,
            bidi_control_chars: None,
            restyle_damage: RestyleDamage::empty(),
        }
    }

    fn from_inline_node<'dom>(
        node: &impl ThreadSafeLayoutNode<'dom>,
        style_context: &SharedStyleContext,
    ) -> InlineFragmentsAccumulator {
        InlineFragmentsAccumulator {
            fragments: IntermediateInlineFragments::new(),
            enclosing_node: Some(InlineFragmentNodeInfo {
                address: node.opaque(),
                pseudo: node.get_pseudo_element_type(),
                style: node.style(style_context),
                selected_style: node.selected_style(),
                flags: InlineFragmentNodeFlags::FIRST_FRAGMENT_OF_ELEMENT |
                    InlineFragmentNodeFlags::LAST_FRAGMENT_OF_ELEMENT,
            }),
            bidi_control_chars: None,
            restyle_damage: node.restyle_damage(),
        }
    }

    fn push(&mut self, fragment: Fragment) {
        self.fragments.fragments.push_back(fragment)
    }

    fn push_all(&mut self, mut fragments: IntermediateInlineFragments) {
        self.fragments.fragments.append(&mut fragments.fragments);
        self.fragments
            .absolute_descendants
            .push_descendants(fragments.absolute_descendants);
    }

    fn get_intermediate_inline_fragments<'dom, N>(
        self,
        context: &SharedStyleContext,
    ) -> IntermediateInlineFragments
    where
        N: ThreadSafeLayoutNode<'dom>,
    {
        let InlineFragmentsAccumulator {
            mut fragments,
            enclosing_node,
            bidi_control_chars,
            restyle_damage,
        } = self;
        if let Some(mut enclosing_node) = enclosing_node {
            let fragment_count = fragments.fragments.len();
            for (index, fragment) in fragments.fragments.iter_mut().enumerate() {
                let mut enclosing_node = enclosing_node.clone();
                if index != 0 {
                    enclosing_node
                        .flags
                        .remove(InlineFragmentNodeFlags::FIRST_FRAGMENT_OF_ELEMENT)
                }
                if index != fragment_count - 1 {
                    enclosing_node
                        .flags
                        .remove(InlineFragmentNodeFlags::LAST_FRAGMENT_OF_ELEMENT)
                }
                fragment.add_inline_context_style(enclosing_node);
            }

            // Control characters are later discarded in transform_text, so they don't affect the
            // is_first/is_last styles above.
            enclosing_node.flags.remove(
                InlineFragmentNodeFlags::FIRST_FRAGMENT_OF_ELEMENT |
                    InlineFragmentNodeFlags::LAST_FRAGMENT_OF_ELEMENT,
            );

            if let Some((start, end)) = bidi_control_chars {
                fragments
                    .fragments
                    .push_front(control_chars_to_fragment::<N::ConcreteElement>(
                        &enclosing_node,
                        context,
                        start,
                        restyle_damage,
                    ));
                fragments
                    .fragments
                    .push_back(control_chars_to_fragment::<N::ConcreteElement>(
                        &enclosing_node,
                        context,
                        end,
                        restyle_damage,
                    ));
            }
        }
        fragments
    }
}

/// An object that knows how to create flows.
pub struct FlowConstructor<'a, N> {
    /// The layout context.
    pub layout_context: &'a LayoutContext<'a>,
    /// Satisfy the compiler about the unused parameters, which we use to improve the ergonomics of
    /// the ensuing impl {} by removing the need to parameterize all the methods individually.
    phantom2: PhantomData<N>,
}

impl<'a, 'dom, ConcreteThreadSafeLayoutNode> FlowConstructor<'a, ConcreteThreadSafeLayoutNode>
where
    ConcreteThreadSafeLayoutNode: ThreadSafeLayoutNode<'dom>,
{
    /// Creates a new flow constructor.
    pub fn new(layout_context: &'a LayoutContext<'a>) -> Self {
        FlowConstructor {
            layout_context,
            phantom2: PhantomData,
        }
    }

    #[inline]
    fn style_context(&self) -> &SharedStyleContext {
        self.layout_context.shared_context()
    }

    #[inline]
    fn set_flow_construction_result(
        &self,
        node: &ConcreteThreadSafeLayoutNode,
        result: ConstructionResult,
    ) {
        node.set_flow_construction_result(result);
    }

    /// Builds the fragment for the given block or subclass thereof.
    fn build_fragment_for_block(&self, node: &ConcreteThreadSafeLayoutNode) -> Fragment {
        let specific_fragment_info = match node.type_id() {
            Some(LayoutNodeType::Element(LayoutElementType::HTMLIFrameElement)) => {
                SpecificFragmentInfo::Iframe(IframeFragmentInfo::new(node))
            },
            Some(LayoutNodeType::Element(LayoutElementType::HTMLImageElement)) => {
                let image_info = Box::new(ImageFragmentInfo::new(
                    node.image_url(),
                    node.image_density(),
                    node,
                    self.layout_context,
                ));
                SpecificFragmentInfo::Image(image_info)
            },
            Some(LayoutNodeType::Element(LayoutElementType::HTMLMediaElement)) => {
                let data = node.media_data().unwrap();
                SpecificFragmentInfo::Media(Box::new(MediaFragmentInfo::new(data)))
            },
            Some(LayoutNodeType::Element(LayoutElementType::HTMLObjectElement)) => {
                let elem = node.as_element().unwrap();
                let type_and_data = (
                    elem.get_attr(&ns!(), &local_name!("type")),
                    elem.get_attr(&ns!(), &local_name!("data")),
                );
                let object_data = match type_and_data {
                    (None, Some(uri)) if is_image_data(uri) => ServoUrl::parse(uri).ok(),
                    _ => None,
                };
                let image_info = Box::new(ImageFragmentInfo::new(
                    object_data,
                    None,
                    node,
                    self.layout_context,
                ));
                SpecificFragmentInfo::Image(image_info)
            },
            Some(LayoutNodeType::Element(LayoutElementType::HTMLTableElement)) => {
                SpecificFragmentInfo::TableWrapper
            },
            Some(LayoutNodeType::Element(LayoutElementType::HTMLTableColElement)) => {
                SpecificFragmentInfo::TableColumn(TableColumnFragmentInfo::new(node))
            },
            Some(LayoutNodeType::Element(LayoutElementType::HTMLTableCellElement)) => {
                SpecificFragmentInfo::TableCell
            },
            Some(LayoutNodeType::Element(LayoutElementType::HTMLTableRowElement)) |
            Some(LayoutNodeType::Element(LayoutElementType::HTMLTableSectionElement)) => {
                SpecificFragmentInfo::TableRow
            },
            Some(LayoutNodeType::Element(LayoutElementType::HTMLCanvasElement)) => {
                let data = node.canvas_data().unwrap();
                SpecificFragmentInfo::Canvas(Box::new(CanvasFragmentInfo::new(data)))
            },
            Some(LayoutNodeType::Element(LayoutElementType::SVGSVGElement)) => {
                let data = node.svg_data().unwrap();
                SpecificFragmentInfo::Svg(Box::new(SvgFragmentInfo::new(data)))
            },
            _ => {
                // This includes pseudo-elements.
                SpecificFragmentInfo::Generic
            },
        };

        Fragment::new(node, specific_fragment_info, self.layout_context)
    }

    /// Creates an inline flow from a set of inline fragments, then adds it as a child of the given
    /// flow or pushes it onto the given flow list.
    ///
    /// `#[inline(always)]` because this is performance critical and LLVM will not inline it
    /// otherwise.
    #[inline(always)]
    fn flush_inline_fragments_to_flow(
        &mut self,
        fragment_accumulator: InlineFragmentsAccumulator,
        flow: &mut FlowRef,
        absolute_descendants: &mut AbsoluteDescendants,
        legalizer: &mut Legalizer,
        node: &ConcreteThreadSafeLayoutNode,
    ) {
        let mut fragments = fragment_accumulator
            .get_intermediate_inline_fragments::<ConcreteThreadSafeLayoutNode>(
                self.style_context(),
            );
        if fragments.is_empty() {
            return;
        };

        strip_ignorable_whitespace_from_start(&mut fragments.fragments);
        strip_ignorable_whitespace_from_end(&mut fragments.fragments);
        if fragments.fragments.is_empty() {
            absolute_descendants.push_descendants(fragments.absolute_descendants);
            return;
        }

        // Build a list of all the inline-block fragments before fragments is moved.
        let mut inline_block_flows = vec![];
        for fragment in &fragments.fragments {
            match fragment.specific {
                SpecificFragmentInfo::InlineBlock(ref info) => {
                    inline_block_flows.push(info.flow_ref.clone())
                },
                SpecificFragmentInfo::InlineAbsoluteHypothetical(ref info) => {
                    inline_block_flows.push(info.flow_ref.clone())
                },
                SpecificFragmentInfo::InlineAbsolute(ref info) => {
                    inline_block_flows.push(info.flow_ref.clone())
                },
                _ => {},
            }
        }

        // We must scan for runs before computing minimum ascent and descent because scanning
        // for runs might collapse so much whitespace away that only hypothetical fragments
        // remain. In that case the inline flow will compute its ascent and descent to be zero.
        let scanned_fragments = TextRunScanner::new().scan_for_runs(
            &self.layout_context.font_context,
            mem::take(&mut fragments.fragments),
        );
        let mut inline_flow_ref = FlowRef::new(Arc::new(InlineFlow::from_fragments(
            scanned_fragments,
            node.style(self.style_context()).writing_mode,
        )));

        // Add all the inline-block fragments as children of the inline flow.
        for inline_block_flow in &inline_block_flows {
            inline_flow_ref.add_new_child(inline_block_flow.clone());
        }

        // Set up absolute descendants as necessary.
        //
        // The inline flow itself may need to become the containing block for absolute descendants
        // in order to handle cases like:
        //
        //      <div>
        //          <span style="position: relative">
        //              <span style="position: absolute; ..."></span>
        //          </span>
        //      </div>
        //
        // See the comment above `flow::AbsoluteDescendantInfo` for more information.
        inline_flow_ref.take_applicable_absolute_descendants(&mut fragments.absolute_descendants);
        absolute_descendants.push_descendants(fragments.absolute_descendants);

        {
            // FIXME(#6503): Use Arc::get_mut().unwrap() here.
            let inline_flow = FlowRef::deref_mut(&mut inline_flow_ref).as_mut_inline();
            inline_flow.minimum_line_metrics = inline_flow.minimum_line_metrics(
                &self.layout_context.font_context,
                &node.style(self.style_context()),
            );
        }

        inline_flow_ref.finish();
        legalizer.add_child::<ConcreteThreadSafeLayoutNode::ConcreteElement>(
            self.style_context(),
            flow,
            inline_flow_ref,
        )
    }

    fn build_block_flow_using_construction_result_of_child(
        &mut self,
        flow: &mut FlowRef,
        node: &ConcreteThreadSafeLayoutNode,
        kid: ConcreteThreadSafeLayoutNode,
        inline_fragment_accumulator: &mut InlineFragmentsAccumulator,
        abs_descendants: &mut AbsoluteDescendants,
        legalizer: &mut Legalizer,
    ) {
        match kid.get_construction_result() {
            ConstructionResult::None => {},
            ConstructionResult::Flow(kid_flow, kid_abs_descendants) => {
                // If kid_flow is TableCaptionFlow, kid_flow should be added under
                // TableWrapperFlow.
                if flow.is_table() && kid_flow.is_table_caption() {
                    let construction_result =
                        ConstructionResult::Flow(kid_flow, AbsoluteDescendants::new());
                    self.set_flow_construction_result(&kid, construction_result)
                } else {
                    if !kid_flow
                        .base()
                        .flags
                        .contains(FlowFlags::IS_ABSOLUTELY_POSITIONED)
                    {
                        // Flush any inline fragments that we were gathering up. This allows us to
                        // handle {ib} splits.
                        let old_inline_fragment_accumulator = mem::replace(
                            inline_fragment_accumulator,
                            InlineFragmentsAccumulator::new(),
                        );
                        self.flush_inline_fragments_to_flow(
                            old_inline_fragment_accumulator,
                            flow,
                            abs_descendants,
                            legalizer,
                            node,
                        );
                    }
                    legalizer.add_child::<ConcreteThreadSafeLayoutNode::ConcreteElement>(
                        self.style_context(),
                        flow,
                        kid_flow,
                    )
                }
                abs_descendants.push_descendants(kid_abs_descendants);
            },
            ConstructionResult::ConstructionItem(ConstructionItem::InlineFragments(
                InlineFragmentsConstructionResult {
                    splits,
                    fragments: successor_fragments,
                },
            )) => {
                // Add any {ib} splits.
                for split in splits {
                    // Pull apart the {ib} split object and push its predecessor fragments
                    // onto the list.
                    let InlineBlockSplit {
                        predecessors,
                        flow: kid_flow,
                    } = split;
                    inline_fragment_accumulator.push_all(predecessors);

                    // Flush any inline fragments that we were gathering up.
                    debug!(
                        "flushing {} inline box(es) to flow A",
                        inline_fragment_accumulator.fragments.fragments.len()
                    );
                    let old_inline_fragment_accumulator = mem::replace(
                        inline_fragment_accumulator,
                        InlineFragmentsAccumulator::new(),
                    );
                    let absolute_descendants =
                        &mut inline_fragment_accumulator.fragments.absolute_descendants;
                    self.flush_inline_fragments_to_flow(
                        old_inline_fragment_accumulator,
                        flow,
                        absolute_descendants,
                        legalizer,
                        node,
                    );

                    // Push the flow generated by the {ib} split onto our list of flows.
                    legalizer.add_child::<ConcreteThreadSafeLayoutNode::ConcreteElement>(
                        self.style_context(),
                        flow,
                        kid_flow,
                    )
                }

                // Add the fragments to the list we're maintaining.
                inline_fragment_accumulator.push_all(successor_fragments);
            },
            ConstructionResult::ConstructionItem(ConstructionItem::Whitespace(
                whitespace_node,
                whitespace_pseudo,
                whitespace_style,
                whitespace_damage,
            )) => {
                // Add whitespace results. They will be stripped out later on when
                // between block elements, and retained when between inline elements.
                let fragment_info = SpecificFragmentInfo::UnscannedText(Box::new(
                    UnscannedTextFragmentInfo::new(Box::<str>::from(" "), None),
                ));
                let fragment = Fragment::from_opaque_node_and_style(
                    whitespace_node,
                    whitespace_pseudo,
                    whitespace_style,
                    node.selected_style(),
                    whitespace_damage,
                    fragment_info,
                );
                inline_fragment_accumulator
                    .fragments
                    .fragments
                    .push_back(fragment);
            },
            ConstructionResult::ConstructionItem(ConstructionItem::TableColumnFragment(_)) => {
                // TODO: Implement anonymous table objects for missing parents
                // CSS 2.1 § 17.2.1, step 3-2
            },
        }
    }

    /// Constructs a block flow, beginning with the given `initial_fragments` if present and then
    /// appending the construction results of children to the child list of the block flow. {ib}
    /// splits and absolutely-positioned descendants are handled correctly.
    fn build_flow_for_block_starting_with_fragments(
        &mut self,
        mut flow: FlowRef,
        node: &ConcreteThreadSafeLayoutNode,
        initial_fragments: IntermediateInlineFragments,
    ) -> ConstructionResult {
        // Gather up fragments for the inline flows we might need to create.
        let mut inline_fragment_accumulator = InlineFragmentsAccumulator::new();

        inline_fragment_accumulator
            .fragments
            .push_all(initial_fragments);

        // List of absolute descendants, in tree order.
        let mut abs_descendants = AbsoluteDescendants::new();
        let mut legalizer = Legalizer::new();
        let is_media_element_with_widget = node.type_id() ==
            Some(LayoutNodeType::Element(LayoutElementType::HTMLMediaElement)) &&
            node.as_element().unwrap().is_shadow_host();
        if !node.is_replaced_content() || is_media_element_with_widget {
            for kid in node.children() {
                if kid.get_pseudo_element_type() != PseudoElementType::Normal {
                    if node.is_replaced_content() {
                        // Replaced elements don't have pseudo-elements per spec.
                        continue;
                    }
                    self.process(&kid);
                }

                self.build_block_flow_using_construction_result_of_child(
                    &mut flow,
                    node,
                    kid,
                    &mut inline_fragment_accumulator,
                    &mut abs_descendants,
                    &mut legalizer,
                );
            }
        }

        // Perform a final flush of any inline fragments that we were gathering up to handle {ib}
        // splits, after stripping ignorable whitespace.
        self.flush_inline_fragments_to_flow(
            inline_fragment_accumulator,
            &mut flow,
            &mut abs_descendants,
            &mut legalizer,
            node,
        );

        // The flow is done.
        legalizer.finish(&mut flow);
        flow.finish();

        // Set up the absolute descendants.
        if flow.is_absolute_containing_block() {
            // This is the containing block for all the absolute descendants.
            flow.set_absolute_descendants(abs_descendants);

            abs_descendants = AbsoluteDescendants::new();
            if flow
                .base()
                .flags
                .contains(FlowFlags::IS_ABSOLUTELY_POSITIONED)
            {
                // This is now the only absolute flow in the subtree which hasn't yet
                // reached its CB.
                abs_descendants.push(flow.clone());
            }
        }
        ConstructionResult::Flow(flow, abs_descendants)
    }

    /// Constructs a flow for the given block node and its children. This method creates an
    /// initial fragment as appropriate and then dispatches to
    /// `build_flow_for_block_starting_with_fragments`. Currently the following kinds of flows get
    /// initial content:
    ///
    /// * Generated content gets the initial content specified by the `content` attribute of the
    ///   CSS.
    /// * `<input>` and `<textarea>` elements get their content.
    ///
    /// FIXME(pcwalton): It is not clear to me that there isn't a cleaner way to handle
    /// `<textarea>`.
    fn build_flow_for_block_like(
        &mut self,
        flow: FlowRef,
        node: &ConcreteThreadSafeLayoutNode,
    ) -> ConstructionResult {
        let mut fragments = IntermediateInlineFragments::new();
        let node_is_input_or_text_area = node.type_id() ==
            Some(LayoutNodeType::Element(LayoutElementType::HTMLInputElement)) ||
            node.type_id() ==
                Some(LayoutNodeType::Element(
                    LayoutElementType::HTMLTextAreaElement,
                ));
        if node.get_pseudo_element_type().is_replaced_content() || node_is_input_or_text_area {
            // A TextArea's text contents are displayed through the input text
            // box, so don't construct them.
            if node.type_id() ==
                Some(LayoutNodeType::Element(
                    LayoutElementType::HTMLTextAreaElement,
                ))
            {
                for kid in node.children() {
                    self.set_flow_construction_result(&kid, ConstructionResult::None)
                }
            }

            let context = self.style_context();
            let mut style = node.style(context);
            style = context
                .stylist
                .style_for_anonymous::<ConcreteThreadSafeLayoutNode::ConcreteElement>(
                    &context.guards,
                    &PseudoElement::ServoLegacyText,
                    &style,
                );
            if node_is_input_or_text_area {
                style = context
                    .stylist
                    .style_for_anonymous::<ConcreteThreadSafeLayoutNode::ConcreteElement>(
                        &context.guards,
                        &PseudoElement::ServoLegacyInputText,
                        &style,
                    )
            }

            self.create_fragments_for_node_text_content(&mut fragments, node, &style)
        }
        self.build_flow_for_block_starting_with_fragments(flow, node, fragments)
    }

    /// Pushes fragments appropriate for the content of the given node onto the given list.
    fn create_fragments_for_node_text_content(
        &self,
        fragments: &mut IntermediateInlineFragments,
        node: &ConcreteThreadSafeLayoutNode,
        style: &ServoArc<ComputedValues>,
    ) {
        // Fast path: If there is no text content, return immediately.
        let text_content = node.text_content();
        if text_content.is_empty() {
            return;
        }

        let style = (*style).clone();
        let selected_style = node.selected_style();

        match text_content {
            TextContent::Text(string) => {
                let info = Box::new(UnscannedTextFragmentInfo::new(string, node.selection()));
                let specific_fragment_info = SpecificFragmentInfo::UnscannedText(info);
                fragments
                    .fragments
                    .push_back(Fragment::from_opaque_node_and_style(
                        node.opaque(),
                        node.get_pseudo_element_type(),
                        style,
                        selected_style,
                        node.restyle_damage(),
                        specific_fragment_info,
                    ))
            },
            TextContent::GeneratedContent(content_items) => {
                for content_item in content_items.into_iter() {
                    let specific_fragment_info = match content_item {
                        ContentItem::String(string) => {
                            let info =
                                Box::new(UnscannedTextFragmentInfo::new(string.into(), None));
                            SpecificFragmentInfo::UnscannedText(info)
                        },
                        ContentItem::Attr(attr) => {
                            let element = node.as_element().expect("Expected an element");
                            let attr_val = element
                                .get_attr(&attr.namespace_url, &LocalName::from(&*attr.attribute));
                            let string = attr_val.map_or("".to_string(), |s| s.to_string());
                            let info =
                                Box::new(UnscannedTextFragmentInfo::new(string.into(), None));
                            SpecificFragmentInfo::UnscannedText(info)
                        },
                        content_item => {
                            let content_item =
                                Box::new(GeneratedContentInfo::ContentItem(content_item));
                            SpecificFragmentInfo::GeneratedContent(content_item)
                        },
                    };
                    fragments
                        .fragments
                        .push_back(Fragment::from_opaque_node_and_style(
                            node.opaque(),
                            node.get_pseudo_element_type(),
                            style.clone(),
                            selected_style.clone(),
                            node.restyle_damage(),
                            specific_fragment_info,
                        ))
                }
            },
        }
    }

    /// Builds a flow for a node with `display: block`. This yields a `BlockFlow` with possibly
    /// other `BlockFlow`s or `InlineFlow`s underneath it, depending on whether {ib} splits needed
    /// to happen.
    fn build_flow_for_block(
        &mut self,
        node: &ConcreteThreadSafeLayoutNode,
        float_kind: Option<FloatKind>,
    ) -> ConstructionResult {
        if node.style(self.style_context()).is_multicol() {
            return self.build_flow_for_multicol(node, float_kind);
        }

        let fragment = self.build_fragment_for_block(node);
        let flow = FlowRef::new(Arc::new(BlockFlow::from_fragment_and_float_kind(
            fragment, float_kind,
        )));
        self.build_flow_for_block_like(flow, node)
    }

    /// Bubbles up {ib} splits.
    fn accumulate_inline_block_splits(
        &mut self,
        splits: LinkedList<InlineBlockSplit>,
        node: &ConcreteThreadSafeLayoutNode,
        fragment_accumulator: &mut InlineFragmentsAccumulator,
        opt_inline_block_splits: &mut LinkedList<InlineBlockSplit>,
    ) {
        for split in splits {
            let InlineBlockSplit {
                predecessors,
                flow: kid_flow,
            } = split;
            fragment_accumulator.push_all(predecessors);

            opt_inline_block_splits.push_back(InlineBlockSplit::new(
                fragment_accumulator,
                node,
                self.style_context(),
                kid_flow,
            ));
        }
    }

    /// Concatenates the fragments of kids, adding in our own borders/padding/margins if necessary.
    /// Returns the `InlineFragmentsConstructionResult`, if any. There will be no
    /// `InlineFragmentsConstructionResult` if this node consisted entirely of ignorable
    /// whitespace.
    fn build_fragments_for_nonreplaced_inline_content(
        &mut self,
        node: &ConcreteThreadSafeLayoutNode,
    ) -> ConstructionResult {
        let mut opt_inline_block_splits: LinkedList<InlineBlockSplit> = LinkedList::new();
        let mut fragment_accumulator =
            InlineFragmentsAccumulator::from_inline_node(node, self.style_context());
        fragment_accumulator.bidi_control_chars =
            bidi_control_chars(&node.style(self.style_context()));

        let mut abs_descendants = AbsoluteDescendants::new();

        // Concatenate all the fragments of our kids, creating {ib} splits as necessary.
        let mut is_empty = true;
        for kid in node.children() {
            is_empty = false;
            if kid.get_pseudo_element_type() != PseudoElementType::Normal {
                self.process(&kid);
            }
            match kid.get_construction_result() {
                ConstructionResult::None => {},
                ConstructionResult::Flow(flow, kid_abs_descendants) => {
                    if !flow
                        .base()
                        .flags
                        .contains(FlowFlags::IS_ABSOLUTELY_POSITIONED)
                    {
                        opt_inline_block_splits.push_back(InlineBlockSplit::new(
                            &mut fragment_accumulator,
                            node,
                            self.style_context(),
                            flow,
                        ));
                        abs_descendants.push_descendants(kid_abs_descendants);
                    } else {
                        // Push the absolutely-positioned kid as an inline containing block.
                        let kid_node = flow.as_block().fragment.node;
                        let kid_pseudo = flow.as_block().fragment.pseudo;
                        let kid_style = flow.as_block().fragment.style.clone();
                        let kid_selected_style = flow.as_block().fragment.selected_style.clone();
                        let kid_restyle_damage = flow.as_block().fragment.restyle_damage;
                        let fragment_info = SpecificFragmentInfo::InlineAbsolute(
                            InlineAbsoluteFragmentInfo::new(flow),
                        );
                        fragment_accumulator.push(Fragment::from_opaque_node_and_style(
                            kid_node,
                            kid_pseudo,
                            kid_style,
                            kid_selected_style,
                            kid_restyle_damage,
                            fragment_info,
                        ));
                        fragment_accumulator
                            .fragments
                            .absolute_descendants
                            .push_descendants(kid_abs_descendants);
                    }
                },
                ConstructionResult::ConstructionItem(ConstructionItem::InlineFragments(
                    InlineFragmentsConstructionResult {
                        splits,
                        fragments: successors,
                    },
                )) => {
                    // Bubble up {ib} splits.
                    self.accumulate_inline_block_splits(
                        splits,
                        node,
                        &mut fragment_accumulator,
                        &mut opt_inline_block_splits,
                    );

                    // Push residual fragments.
                    fragment_accumulator.push_all(successors);
                },
                ConstructionResult::ConstructionItem(ConstructionItem::Whitespace(
                    whitespace_node,
                    whitespace_pseudo,
                    whitespace_style,
                    whitespace_damage,
                )) => {
                    // Instantiate the whitespace fragment.
                    let fragment_info = SpecificFragmentInfo::UnscannedText(Box::new(
                        UnscannedTextFragmentInfo::new(Box::<str>::from(" "), None),
                    ));
                    let fragment = Fragment::from_opaque_node_and_style(
                        whitespace_node,
                        whitespace_pseudo,
                        whitespace_style,
                        node.selected_style(),
                        whitespace_damage,
                        fragment_info,
                    );
                    fragment_accumulator.fragments.fragments.push_back(fragment)
                },
                ConstructionResult::ConstructionItem(ConstructionItem::TableColumnFragment(_)) => {
                    // TODO: Implement anonymous table objects for missing parents
                    // CSS 2.1 § 17.2.1, step 3-2
                },
            }
        }

        let node_style = node.style(self.style_context());
        if is_empty && has_padding_or_border(&node_style) {
            // An empty inline box needs at least one fragment to draw its background and borders.
            let info = SpecificFragmentInfo::UnscannedText(Box::new(
                UnscannedTextFragmentInfo::new(Box::<str>::from(""), None),
            ));
            let fragment = Fragment::from_opaque_node_and_style(
                node.opaque(),
                node.get_pseudo_element_type(),
                node_style.clone(),
                node.selected_style(),
                node.restyle_damage(),
                info,
            );
            fragment_accumulator.fragments.fragments.push_back(fragment)
        }

        // Finally, make a new construction result.
        if !opt_inline_block_splits.is_empty() ||
            !fragment_accumulator.fragments.is_empty() ||
            !abs_descendants.is_empty()
        {
            fragment_accumulator
                .fragments
                .absolute_descendants
                .push_descendants(abs_descendants);

            // If the node is positioned, then it's the containing block for all absolutely-
            // positioned descendants.
            if node_style.get_box().position != Position::Static {
                fragment_accumulator
                    .fragments
                    .absolute_descendants
                    .mark_as_having_reached_containing_block();
            }

            let construction_item =
                ConstructionItem::InlineFragments(InlineFragmentsConstructionResult {
                    splits: opt_inline_block_splits,
                    fragments: fragment_accumulator
                        .get_intermediate_inline_fragments::<ConcreteThreadSafeLayoutNode>(
                            self.style_context(),
                        ),
                });
            ConstructionResult::ConstructionItem(construction_item)
        } else {
            ConstructionResult::None
        }
    }

    /// Creates an `InlineFragmentsConstructionResult` for replaced content. Replaced content
    /// doesn't render its children, so this just nukes a child's fragments and creates a
    /// `Fragment`.
    fn build_fragments_for_replaced_inline_content(
        &mut self,
        node: &ConcreteThreadSafeLayoutNode,
    ) -> ConstructionResult {
        for kid in node.children() {
            self.set_flow_construction_result(&kid, ConstructionResult::None)
        }

        let context = self.style_context();
        let style = node.style(context);
        // If this node is ignorable whitespace, bail out now.
        if node.is_ignorable_whitespace(context) {
            return ConstructionResult::ConstructionItem(ConstructionItem::Whitespace(
                node.opaque(),
                node.get_pseudo_element_type(),
                context
                    .stylist
                    .style_for_anonymous::<ConcreteThreadSafeLayoutNode::ConcreteElement>(
                        &context.guards,
                        &PseudoElement::ServoLegacyText,
                        &style,
                    ),
                node.restyle_damage(),
            ));
        }

        // If this is generated content, then we need to initialize the accumulator with the
        // fragment corresponding to that content. Otherwise, just initialize with the ordinary
        // fragment that needs to be generated for this inline node.
        let mut fragments = IntermediateInlineFragments::new();
        match (node.get_pseudo_element_type(), node.type_id()) {
            (_, Some(LayoutNodeType::Text)) => {
                let text_style = context
                    .stylist
                    .style_for_anonymous::<ConcreteThreadSafeLayoutNode::ConcreteElement>(
                        &context.guards,
                        &PseudoElement::ServoLegacyText,
                        &style,
                    );
                self.create_fragments_for_node_text_content(&mut fragments, node, &text_style)
            },
            (PseudoElementType::Normal, _) => {
                fragments
                    .fragments
                    .push_back(self.build_fragment_for_block(node));
            },
            (_, _) => self.create_fragments_for_node_text_content(&mut fragments, node, &style),
        }

        let construction_item =
            ConstructionItem::InlineFragments(InlineFragmentsConstructionResult {
                splits: LinkedList::new(),
                fragments,
            });
        ConstructionResult::ConstructionItem(construction_item)
    }

    /// Build the fragment for an inline-block or inline-flex, based on the `display` flag
    fn build_fragment_for_inline_block_or_inline_flex(
        &mut self,
        node: &ConcreteThreadSafeLayoutNode,
        display: Display,
    ) -> ConstructionResult {
        let block_flow_result = match display {
            Display::InlineBlock => self.build_flow_for_block(node, None),
            Display::InlineFlex => self.build_flow_for_flex(node, None),
            _ => panic!("The flag should be inline-block or inline-flex"),
        };
        let (block_flow, abs_descendants) = match block_flow_result {
            ConstructionResult::Flow(block_flow, abs_descendants) => (block_flow, abs_descendants),
            _ => unreachable!(),
        };

        let context = self.style_context();
        let style = node.style(context);
        let style = context
            .stylist
            .style_for_anonymous::<ConcreteThreadSafeLayoutNode::ConcreteElement>(
                &context.guards,
                &PseudoElement::ServoLegacyInlineBlockWrapper,
                &style,
            );
        let fragment_info =
            SpecificFragmentInfo::InlineBlock(InlineBlockFragmentInfo::new(block_flow));
        let fragment = Fragment::from_opaque_node_and_style(
            node.opaque(),
            node.get_pseudo_element_type(),
            style,
            node.selected_style(),
            node.restyle_damage(),
            fragment_info,
        );

        let mut fragment_accumulator = InlineFragmentsAccumulator::new();
        fragment_accumulator.fragments.fragments.push_back(fragment);
        fragment_accumulator
            .fragments
            .absolute_descendants
            .push_descendants(abs_descendants);

        let construction_item =
            ConstructionItem::InlineFragments(InlineFragmentsConstructionResult {
                splits: LinkedList::new(),
                fragments: fragment_accumulator
                    .get_intermediate_inline_fragments::<ConcreteThreadSafeLayoutNode>(context),
            });
        ConstructionResult::ConstructionItem(construction_item)
    }

    /// This is an annoying case, because the computed `display` value is `block`, but the
    /// hypothetical box is inline.
    fn build_fragment_for_absolutely_positioned_inline(
        &mut self,
        node: &ConcreteThreadSafeLayoutNode,
    ) -> ConstructionResult {
        let block_flow_result = self.build_flow_for_block(node, None);
        let (block_flow, abs_descendants) = match block_flow_result {
            ConstructionResult::Flow(block_flow, abs_descendants) => (block_flow, abs_descendants),
            _ => unreachable!(),
        };

        let fragment_info = SpecificFragmentInfo::InlineAbsoluteHypothetical(
            InlineAbsoluteHypotheticalFragmentInfo::new(block_flow),
        );
        let style_context = self.style_context();
        let style = node.style(style_context);
        let style = style_context
            .stylist
            .style_for_anonymous::<ConcreteThreadSafeLayoutNode::ConcreteElement>(
                &style_context.guards,
                &PseudoElement::ServoLegacyInlineAbsolute,
                &style,
            );
        let fragment = Fragment::from_opaque_node_and_style(
            node.opaque(),
            PseudoElementType::Normal,
            style,
            node.selected_style(),
            node.restyle_damage(),
            fragment_info,
        );

        let mut fragment_accumulator =
            InlineFragmentsAccumulator::from_inline_node(node, self.style_context());
        fragment_accumulator.fragments.fragments.push_back(fragment);
        fragment_accumulator
            .fragments
            .absolute_descendants
            .push_descendants(abs_descendants);

        let construction_item =
            ConstructionItem::InlineFragments(InlineFragmentsConstructionResult {
                splits: LinkedList::new(),
                fragments: fragment_accumulator
                    .get_intermediate_inline_fragments::<ConcreteThreadSafeLayoutNode>(
                        style_context,
                    ),
            });
        ConstructionResult::ConstructionItem(construction_item)
    }

    /// Builds one or more fragments for a node with `display: inline`. This yields an
    /// `InlineFragmentsConstructionResult`.
    fn build_fragments_for_inline(
        &mut self,
        node: &ConcreteThreadSafeLayoutNode,
    ) -> ConstructionResult {
        // Is this node replaced content?
        if !node.is_replaced_content() {
            // Go to a path that concatenates our kids' fragments.
            self.build_fragments_for_nonreplaced_inline_content(node)
        } else {
            // Otherwise, just nuke our kids' fragments, create our fragment if any, and be done
            // with it.
            self.build_fragments_for_replaced_inline_content(node)
        }
    }

    /// Places any table captions found under the given table wrapper, if the value of their
    /// `caption-side` property is equal to the given `side`.
    fn place_table_caption_under_table_wrapper_on_side(
        &mut self,
        table_wrapper_flow: &mut FlowRef,
        node: &ConcreteThreadSafeLayoutNode,
        side: CaptionSide,
    ) {
        // Only flows that are table captions are matched here.
        for kid in node.children() {
            match kid.get_construction_result() {
                ConstructionResult::Flow(kid_flow, _) => {
                    if kid_flow.is_table_caption() &&
                        kid_flow
                            .as_block()
                            .fragment
                            .style()
                            .get_inherited_table()
                            .caption_side ==
                            side
                    {
                        table_wrapper_flow.add_new_child(kid_flow);
                    }
                },
                ConstructionResult::None | ConstructionResult::ConstructionItem(_) => {},
            }
        }
    }

    /// Builds a flow for a node with `column-count` or `column-width` non-`auto`.
    /// This yields a `MulticolFlow` with a single `MulticolColumnFlow` underneath it.
    fn build_flow_for_multicol(
        &mut self,
        node: &ConcreteThreadSafeLayoutNode,
        float_kind: Option<FloatKind>,
    ) -> ConstructionResult {
        let fragment = Fragment::new(node, SpecificFragmentInfo::Multicol, self.layout_context);
        let mut flow = FlowRef::new(Arc::new(MulticolFlow::from_fragment(fragment, float_kind)));

        let column_fragment = Fragment::new(
            node,
            SpecificFragmentInfo::MulticolColumn,
            self.layout_context,
        );
        let column_flow =
            FlowRef::new(Arc::new(MulticolColumnFlow::from_fragment(column_fragment)));

        // First populate the column flow with its children.
        let construction_result = self.build_flow_for_block_like(column_flow, node);

        let mut abs_descendants = AbsoluteDescendants::new();

        if let ConstructionResult::Flow(column_flow, column_abs_descendants) = construction_result {
            flow.add_new_child(column_flow);
            abs_descendants.push_descendants(column_abs_descendants);
        }

        // The flow is done.
        flow.finish();
        if flow.is_absolute_containing_block() {
            // This is the containing block for all the absolute descendants.
            flow.set_absolute_descendants(abs_descendants);

            abs_descendants = AbsoluteDescendants::new();

            if flow
                .base()
                .flags
                .contains(FlowFlags::IS_ABSOLUTELY_POSITIONED)
            {
                // This is now the only absolute flow in the subtree which hasn't yet
                // reached its containing block.
                abs_descendants.push(flow.clone());
            }
        }

        ConstructionResult::Flow(flow, abs_descendants)
    }

    /// Builds a flow for a node with `display: table`. This yields a `TableWrapperFlow` with
    /// possibly other `TableCaptionFlow`s or `TableFlow`s underneath it.
    fn build_flow_for_table(
        &mut self,
        node: &ConcreteThreadSafeLayoutNode,
        float_value: Float,
    ) -> ConstructionResult {
        let mut legalizer = Legalizer::new();

        let table_style;
        let wrapper_style;
        {
            let context = self.style_context();
            table_style = node.style(context);
            wrapper_style = context
                .stylist
                .style_for_anonymous::<ConcreteThreadSafeLayoutNode::ConcreteElement>(
                    &context.guards,
                    &PseudoElement::ServoLegacyTableWrapper,
                    &table_style,
                );
        }
        let wrapper_fragment = Fragment::from_opaque_node_and_style(
            node.opaque(),
            PseudoElementType::Normal,
            wrapper_style,
            node.selected_style(),
            node.restyle_damage(),
            SpecificFragmentInfo::TableWrapper,
        );
        let wrapper_float_kind = FloatKind::from_property(float_value);
        let mut wrapper_flow = FlowRef::new(Arc::new(
            TableWrapperFlow::from_fragment_and_float_kind(wrapper_fragment, wrapper_float_kind),
        ));

        let table_fragment = Fragment::new(node, SpecificFragmentInfo::Table, self.layout_context);
        let table_flow = FlowRef::new(Arc::new(TableFlow::from_fragment(table_fragment)));

        // First populate the table flow with its children.
        let construction_result = self.build_flow_for_block_like(table_flow, node);

        let mut abs_descendants = AbsoluteDescendants::new();

        // The order of the caption and the table are not necessarily the same order as in the DOM
        // tree. All caption blocks are placed before or after the table flow, depending on the
        // value of `caption-side`.
        self.place_table_caption_under_table_wrapper_on_side(
            &mut wrapper_flow,
            node,
            CaptionSide::Top,
        );

        if let ConstructionResult::Flow(table_flow, table_abs_descendants) = construction_result {
            legalizer.add_child::<ConcreteThreadSafeLayoutNode::ConcreteElement>(
                self.style_context(),
                &mut wrapper_flow,
                table_flow,
            );
            abs_descendants.push_descendants(table_abs_descendants);
        }

        // If the value of `caption-side` is `bottom`, place it now.
        self.place_table_caption_under_table_wrapper_on_side(
            &mut wrapper_flow,
            node,
            CaptionSide::Bottom,
        );

        // The flow is done.
        legalizer.finish(&mut wrapper_flow);
        wrapper_flow.finish();

        if wrapper_flow.is_absolute_containing_block() {
            // This is the containing block for all the absolute descendants.
            wrapper_flow.set_absolute_descendants(abs_descendants);

            abs_descendants = AbsoluteDescendants::new();

            if wrapper_flow
                .base()
                .flags
                .contains(FlowFlags::IS_ABSOLUTELY_POSITIONED)
            {
                // This is now the only absolute flow in the subtree which hasn't yet
                // reached its containing block.
                abs_descendants.push(wrapper_flow.clone());
            }
        }

        ConstructionResult::Flow(wrapper_flow, abs_descendants)
    }

    /// Builds a flow for a node with `display: table-caption`. This yields a `TableCaptionFlow`
    /// with possibly other `BlockFlow`s or `InlineFlow`s underneath it.
    fn build_flow_for_table_caption(
        &mut self,
        node: &ConcreteThreadSafeLayoutNode,
    ) -> ConstructionResult {
        let fragment = self.build_fragment_for_block(node);
        let flow = FlowRef::new(Arc::new(TableCaptionFlow::from_fragment(fragment)));
        self.build_flow_for_block_like(flow, node)
    }

    /// Builds a flow for a node with `display: table-row-group`. This yields a `TableRowGroupFlow`
    /// with possibly other `TableRowFlow`s underneath it.
    fn build_flow_for_table_rowgroup(
        &mut self,
        node: &ConcreteThreadSafeLayoutNode,
    ) -> ConstructionResult {
        let fragment = Fragment::new(node, SpecificFragmentInfo::TableRow, self.layout_context);
        let flow = FlowRef::new(Arc::new(TableRowGroupFlow::from_fragment(fragment)));
        self.build_flow_for_block_like(flow, node)
    }

    /// Builds a flow for a node with `display: table-row`. This yields a `TableRowFlow` with
    /// possibly other `TableCellFlow`s underneath it.
    fn build_flow_for_table_row(
        &mut self,
        node: &ConcreteThreadSafeLayoutNode,
    ) -> ConstructionResult {
        let fragment = Fragment::new(node, SpecificFragmentInfo::TableRow, self.layout_context);
        let flow = FlowRef::new(Arc::new(TableRowFlow::from_fragment(fragment)));
        self.build_flow_for_block_like(flow, node)
    }

    /// Builds a flow for a node with `display: table-cell`. This yields a `TableCellFlow` with
    /// possibly other `BlockFlow`s or `InlineFlow`s underneath it.
    fn build_flow_for_table_cell(
        &mut self,
        node: &ConcreteThreadSafeLayoutNode,
    ) -> ConstructionResult {
        let fragment = Fragment::new(node, SpecificFragmentInfo::TableCell, self.layout_context);

        // Determine if the table cell should be hidden. Per CSS 2.1 § 17.6.1.1, this will be true
        // if the cell has any in-flow elements (even empty ones!) and has `empty-cells` set to
        // `hide`.
        let hide = node
            .style(self.style_context())
            .get_inherited_table()
            .empty_cells ==
            EmptyCells::Hide &&
            node.children().all(|kid| {
                let position = kid.style(self.style_context()).get_box().position;
                !kid.is_content() || position == Position::Absolute || position == Position::Fixed
            });

        let flow = FlowRef::new(Arc::new(
            TableCellFlow::from_node_fragment_and_visibility_flag(node, fragment, !hide),
        ));
        self.build_flow_for_block_like(flow, node)
    }

    /// Builds a flow for a node with `display: list-item`. This yields a `ListItemFlow` with
    /// possibly other `BlockFlow`s or `InlineFlow`s underneath it.
    fn build_flow_for_list_item(
        &mut self,
        node: &ConcreteThreadSafeLayoutNode,
        flotation: Float,
    ) -> ConstructionResult {
        let flotation = FloatKind::from_property(flotation);
        let marker_fragments = match node.style(self.style_context()).get_list().list_style_image {
            Image::Url(ref url_value) => {
                let image_info = Box::new(ImageFragmentInfo::new(
                    url_value.url().cloned().map(Into::into),
                    None,
                    node,
                    self.layout_context,
                ));
                vec![Fragment::new(
                    node,
                    SpecificFragmentInfo::Image(image_info),
                    self.layout_context,
                )]
            },
            // XXX: Non-None image types unimplemented.
            Image::ImageSet(..) |
            Image::Gradient(..) |
            Image::PaintWorklet(..) |
            Image::CrossFade(..) |
            Image::None => match ListStyleTypeContent::from_list_style_type(
                node.style(self.style_context()).get_list().list_style_type,
            ) {
                ListStyleTypeContent::None => Vec::new(),
                ListStyleTypeContent::StaticText(ch) => {
                    let text = format!("{}\u{a0}", ch);
                    let mut unscanned_marker_fragments = LinkedList::new();
                    unscanned_marker_fragments.push_back(Fragment::new(
                        node,
                        SpecificFragmentInfo::UnscannedText(Box::new(
                            UnscannedTextFragmentInfo::new(Box::<str>::from(text), None),
                        )),
                        self.layout_context,
                    ));
                    let marker_fragments = TextRunScanner::new().scan_for_runs(
                        &self.layout_context.font_context,
                        unscanned_marker_fragments,
                    );
                    marker_fragments.fragments
                },
                ListStyleTypeContent::GeneratedContent(info) => vec![Fragment::new(
                    node,
                    SpecificFragmentInfo::GeneratedContent(info),
                    self.layout_context,
                )],
            },
        };

        // If the list marker is outside, it becomes the special "outside fragment" that list item
        // flows have. If it's inside, it's just a plain old fragment. Note that this means that
        // we adopt Gecko's behavior rather than WebKit's when the marker causes an {ib} split,
        // which has caused some malaise (Bugzilla #36854) but CSS 2.1 § 12.5.1 lets me do it, so
        // there.
        let mut initial_fragments = IntermediateInlineFragments::new();
        let main_fragment = self.build_fragment_for_block(node);
        let flow = match node
            .style(self.style_context())
            .get_list()
            .list_style_position
        {
            ListStylePosition::Outside => Arc::new(ListItemFlow::from_fragments_and_flotation(
                main_fragment,
                marker_fragments,
                flotation,
            )),
            ListStylePosition::Inside => {
                for marker_fragment in marker_fragments {
                    initial_fragments.fragments.push_back(marker_fragment)
                }
                Arc::new(ListItemFlow::from_fragments_and_flotation(
                    main_fragment,
                    vec![],
                    flotation,
                ))
            },
        };

        self.build_flow_for_block_starting_with_fragments(
            FlowRef::new(flow),
            node,
            initial_fragments,
        )
    }

    /// Creates a fragment for a node with `display: table-column`.
    fn build_fragments_for_table_column(
        &mut self,
        node: &ConcreteThreadSafeLayoutNode,
    ) -> ConstructionResult {
        // CSS 2.1 § 17.2.1. Treat all child fragments of a `table-column` as `display: none`.
        for kid in node.children() {
            self.set_flow_construction_result(&kid, ConstructionResult::None)
        }

        let specific = SpecificFragmentInfo::TableColumn(TableColumnFragmentInfo::new(node));
        let construction_item = ConstructionItem::TableColumnFragment(Fragment::new(
            node,
            specific,
            self.layout_context,
        ));
        ConstructionResult::ConstructionItem(construction_item)
    }

    /// Builds a flow for a node with `display: table-column-group`.
    /// This yields a `TableColGroupFlow`.
    fn build_flow_for_table_colgroup(
        &mut self,
        node: &ConcreteThreadSafeLayoutNode,
    ) -> ConstructionResult {
        let fragment = Fragment::new(
            node,
            SpecificFragmentInfo::TableColumn(TableColumnFragmentInfo::new(node)),
            self.layout_context,
        );
        let mut col_fragments = vec![];
        for kid in node.children() {
            // CSS 2.1 § 17.2.1. Treat all non-column child fragments of `table-column-group`
            // as `display: none`.
            if let ConstructionResult::ConstructionItem(ConstructionItem::TableColumnFragment(
                fragment,
            )) = kid.get_construction_result()
            {
                col_fragments.push(fragment)
            }
        }
        if col_fragments.is_empty() {
            debug!("add SpecificFragmentInfo::TableColumn for empty colgroup");
            let specific = SpecificFragmentInfo::TableColumn(TableColumnFragmentInfo::new(node));
            col_fragments.push(Fragment::new(node, specific, self.layout_context));
        }
        let mut flow = FlowRef::new(Arc::new(TableColGroupFlow::from_fragments(
            fragment,
            col_fragments,
        )));
        flow.finish();

        ConstructionResult::Flow(flow, AbsoluteDescendants::new())
    }

    /// Builds a flow for a node with 'display: flex'.
    fn build_flow_for_flex(
        &mut self,
        node: &ConcreteThreadSafeLayoutNode,
        float_kind: Option<FloatKind>,
    ) -> ConstructionResult {
        let fragment = self.build_fragment_for_block(node);
        let flow = FlowRef::new(Arc::new(FlexFlow::from_fragment(fragment, float_kind)));
        self.build_flow_for_block_like(flow, node)
    }

    /// Attempts to perform incremental repair to account for recent changes to this node. This
    /// can fail and return false, indicating that flows will need to be reconstructed.
    ///
    /// TODO(pcwalton): Add some more fast paths, like toggling `display: none`, adding block kids
    /// to block parents with no {ib} splits, adding out-of-flow kids, etc.
    pub fn repair_if_possible(&mut self, node: &ConcreteThreadSafeLayoutNode) -> bool {
        // We can skip reconstructing the flow if we don't have to reconstruct and none of our kids
        // did either.
        //
        // We visit the kids first and reset their HAS_NEWLY_CONSTRUCTED_FLOW flags after checking
        // them.  NOTE: Make sure not to bail out early before resetting all the flags!
        let mut need_to_reconstruct = false;

        // If the node has display: none, it's possible that we haven't even
        // styled the children once, so we need to bailout early here.
        if node.style(self.style_context()).get_box().clone_display() == Display::None {
            return false;
        }

        for kid in node.children() {
            if kid
                .flags()
                .contains(LayoutDataFlags::HAS_NEWLY_CONSTRUCTED_FLOW)
            {
                kid.remove_flags(LayoutDataFlags::HAS_NEWLY_CONSTRUCTED_FLOW);
                need_to_reconstruct = true
            }
        }

        if need_to_reconstruct {
            return false;
        }

        if node
            .restyle_damage()
            .contains(ServoRestyleDamage::RECONSTRUCT_FLOW)
        {
            return false;
        }

        let mut set_has_newly_constructed_flow_flag = false;
        let result = {
            let style = node.style(self.style_context());

            if style.can_be_fragmented() || style.is_multicol() {
                return false;
            }

            let damage = node.restyle_damage();
            let mut data = node.mutate_layout_data().unwrap();

            match *node.construction_result_mut(&mut data) {
                ConstructionResult::None => true,
                ConstructionResult::Flow(ref mut flow, _) => {
                    // The node's flow is of the same type and has the same set of children and can
                    // therefore be repaired by simply propagating damage and style to the flow.
                    if !flow.is_block_flow() {
                        return false;
                    }

                    let flow = FlowRef::deref_mut(flow);
                    flow.mut_base().restyle_damage.insert(damage);
                    flow.repair_style_and_bubble_inline_sizes(&style);
                    true
                },
                ConstructionResult::ConstructionItem(ConstructionItem::InlineFragments(
                    ref mut inline_fragments_construction_result,
                )) => {
                    if !inline_fragments_construction_result.splits.is_empty() {
                        return false;
                    }

                    for fragment in inline_fragments_construction_result
                        .fragments
                        .fragments
                        .iter_mut()
                    {
                        // Only mutate the styles of fragments that represent the dirty node (including
                        // pseudo-element).
                        if fragment.node != node.opaque() {
                            continue;
                        }
                        if fragment.pseudo != node.get_pseudo_element_type() {
                            continue;
                        }

                        match fragment.specific {
                            SpecificFragmentInfo::InlineBlock(ref mut inline_block_fragment) => {
                                let flow_ref =
                                    FlowRef::deref_mut(&mut inline_block_fragment.flow_ref);
                                flow_ref.mut_base().restyle_damage.insert(damage);
                                // FIXME(pcwalton): Fragment restyle damage too?
                                flow_ref.repair_style_and_bubble_inline_sizes(&style);
                            },
                            SpecificFragmentInfo::InlineAbsoluteHypothetical(
                                ref mut inline_absolute_hypothetical_fragment,
                            ) => {
                                let flow_ref = FlowRef::deref_mut(
                                    &mut inline_absolute_hypothetical_fragment.flow_ref,
                                );
                                flow_ref.mut_base().restyle_damage.insert(damage);
                                // FIXME(pcwalton): Fragment restyle damage too?
                                flow_ref.repair_style_and_bubble_inline_sizes(&style);
                            },
                            SpecificFragmentInfo::InlineAbsolute(
                                ref mut inline_absolute_fragment,
                            ) => {
                                let flow_ref =
                                    FlowRef::deref_mut(&mut inline_absolute_fragment.flow_ref);
                                flow_ref.mut_base().restyle_damage.insert(damage);
                                // FIXME(pcwalton): Fragment restyle damage too?
                                flow_ref.repair_style_and_bubble_inline_sizes(&style);
                            },
                            SpecificFragmentInfo::ScannedText(_) => {
                                // Text fragments in ConstructionResult haven't been scanned yet
                                unreachable!()
                            },
                            SpecificFragmentInfo::GeneratedContent(_) |
                            SpecificFragmentInfo::UnscannedText(_) => {
                                // We can't repair this unscanned text; we need to update the
                                // scanned text fragments.
                                //
                                // TODO: Add code to find and repair the ScannedText fragments?
                                return false;
                            },
                            _ => {
                                fragment.repair_style(&style);
                                set_has_newly_constructed_flow_flag = true;
                            },
                        }
                    }
                    true
                },
                ConstructionResult::ConstructionItem(_) => false,
            }
        };
        if set_has_newly_constructed_flow_flag {
            node.insert_flags(LayoutDataFlags::HAS_NEWLY_CONSTRUCTED_FLOW);
        }
        result
    }
}

impl<'a, 'dom, ConcreteThreadSafeLayoutNode>
    PostorderNodeMutTraversal<'dom, ConcreteThreadSafeLayoutNode>
    for FlowConstructor<'a, ConcreteThreadSafeLayoutNode>
where
    ConcreteThreadSafeLayoutNode: ThreadSafeLayoutNode<'dom>,
{
    // Construct Flow based on 'display', 'position', and 'float' values.
    //
    // CSS 2.1 Section 9.7
    //
    // TODO: This should actually consult the table in that section to get the
    // final computed value for 'display'.
    fn process(&mut self, node: &ConcreteThreadSafeLayoutNode) {
        node.insert_flags(LayoutDataFlags::HAS_NEWLY_CONSTRUCTED_FLOW);

        let style = node.style(self.style_context());

        // Bail out if this node is display: none. The style system guarantees
        // that we don't arrive here for children of those.
        if style.get_box().display.is_none() {
            self.set_flow_construction_result(node, ConstructionResult::None);
            return;
        }

        // Get the `display` property for this node, and determine whether this node is floated.
        let (display, float, positioning) = match node.type_id() {
            None => {
                // Pseudo-element.
                (
                    style.get_box().display,
                    style.get_box().float,
                    style.get_box().position,
                )
            },
            Some(LayoutNodeType::Element(_)) => {
                let original_display = style.get_box().original_display;
                // FIXME(emilio, #19771): This munged_display business is pretty
                // wrong. After we fix this we should be able to unify the
                // pseudo-element path too.
                let munged_display = match original_display {
                    Display::Inline | Display::InlineBlock => original_display,
                    _ => style.get_box().display,
                };
                (
                    munged_display,
                    style.get_box().float,
                    style.get_box().position,
                )
            },
            Some(LayoutNodeType::Text) => (Display::Inline, Float::None, Position::Static),
        };

        debug!(
            "building flow for node: {:?} {:?} {:?} {:?}",
            display,
            float,
            positioning,
            node.type_id()
        );

        // FIXME(emilio): This should look at display-outside and
        // display-inside, but there's so much stuff that goes through the
        // generic "block" codepath (wrongly).
        //
        // Switch on display and floatedness.
        match (display, float, positioning) {
            // `display: none` contributes no flow construction result.
            (Display::None, _, _) => {
                self.set_flow_construction_result(node, ConstructionResult::None);
            },

            // Table items contribute table flow construction results.
            (Display::Table, float_value, _) => {
                let construction_result = self.build_flow_for_table(node, float_value);
                self.set_flow_construction_result(node, construction_result)
            },

            // Absolutely positioned elements will have computed value of
            // `float` as 'none' and `display` as per the table.
            // Only match here for block items. If an item is absolutely
            // positioned, but inline we shouldn't try to construct a block
            // flow here - instead, let it match the inline case
            // below.
            (Display::Block, _, Position::Absolute) | (Display::Block, _, Position::Fixed) => {
                let construction_result = self.build_flow_for_block(node, None);
                self.set_flow_construction_result(node, construction_result)
            },

            // Inline items that are absolutely-positioned contribute inline fragment construction
            // results with a hypothetical fragment.
            (Display::Inline, _, Position::Absolute) |
            (Display::Inline, _, Position::Fixed) |
            (Display::InlineBlock, _, Position::Absolute) |
            (Display::InlineBlock, _, Position::Fixed) => {
                let construction_result =
                    self.build_fragment_for_absolutely_positioned_inline(node);
                self.set_flow_construction_result(node, construction_result)
            },

            // Inline items contribute inline fragment construction results.
            //
            // FIXME(pcwalton, #3307): This is not sufficient to handle floated generated content.
            (Display::Inline, Float::None, _) => {
                let construction_result = self.build_fragments_for_inline(node);
                self.set_flow_construction_result(node, construction_result)
            },

            // Inline-block items contribute inline fragment construction results.
            (Display::InlineBlock, Float::None, _) => {
                let construction_result =
                    self.build_fragment_for_inline_block_or_inline_flex(node, Display::InlineBlock);
                self.set_flow_construction_result(node, construction_result)
            },

            // Table items contribute table flow construction results.
            (Display::TableCaption, _, _) => {
                let construction_result = self.build_flow_for_table_caption(node);
                self.set_flow_construction_result(node, construction_result)
            },

            // Table items contribute table flow construction results.
            (Display::TableColumnGroup, _, _) => {
                let construction_result = self.build_flow_for_table_colgroup(node);
                self.set_flow_construction_result(node, construction_result)
            },

            // Table items contribute table flow construction results.
            (Display::TableColumn, _, _) => {
                let construction_result = self.build_fragments_for_table_column(node);
                self.set_flow_construction_result(node, construction_result)
            },

            // Table items contribute table flow construction results.
            (Display::TableRowGroup, _, _) |
            (Display::TableHeaderGroup, _, _) |
            (Display::TableFooterGroup, _, _) => {
                let construction_result = self.build_flow_for_table_rowgroup(node);
                self.set_flow_construction_result(node, construction_result)
            },

            // Table items contribute table flow construction results.
            (Display::TableRow, _, _) => {
                let construction_result = self.build_flow_for_table_row(node);
                self.set_flow_construction_result(node, construction_result)
            },

            // Table items contribute table flow construction results.
            (Display::TableCell, _, _) => {
                let construction_result = self.build_flow_for_table_cell(node);
                self.set_flow_construction_result(node, construction_result)
            },

            // Flex items contribute flex flow construction results.
            (Display::Flex, float_value, _) => {
                let float_kind = FloatKind::from_property(float_value);
                let construction_result = self.build_flow_for_flex(node, float_kind);
                self.set_flow_construction_result(node, construction_result)
            },

            (Display::InlineFlex, _, _) => {
                let construction_result =
                    self.build_fragment_for_inline_block_or_inline_flex(node, Display::InlineFlex);
                self.set_flow_construction_result(node, construction_result)
            },

            // Block flows that are not floated contribute block flow construction results.
            //
            // TODO(pcwalton): Make this only trigger for blocks and handle the other `display`
            // properties separately.
            (_, float_value, _) => {
                let float_kind = FloatKind::from_property(float_value);
                // List items contribute their own special flows.
                let construction_result = if display.is_list_item() {
                    self.build_flow_for_list_item(node, float_value)
                } else {
                    self.build_flow_for_block(node, float_kind)
                };
                self.set_flow_construction_result(node, construction_result)
            },
        }
    }
}

/// A utility trait with some useful methods for node queries.
trait NodeUtils {
    /// Returns true if this node doesn't render its kids and false otherwise.
    fn is_replaced_content(&self) -> bool;

    fn construction_result_mut(self, layout_data: &mut InnerLayoutData) -> &mut ConstructionResult;

    /// Sets the construction result of a flow.
    fn set_flow_construction_result(self, result: ConstructionResult);

    /// Returns the construction result for this node.
    fn get_construction_result(self) -> ConstructionResult;
}

impl<'dom, ConcreteThreadSafeLayoutNode> NodeUtils for ConcreteThreadSafeLayoutNode
where
    ConcreteThreadSafeLayoutNode: ThreadSafeLayoutNode<'dom>,
{
    fn is_replaced_content(&self) -> bool {
        match self.type_id() {
            Some(LayoutNodeType::Text) |
            Some(LayoutNodeType::Element(LayoutElementType::HTMLImageElement)) |
            Some(LayoutNodeType::Element(LayoutElementType::HTMLMediaElement)) |
            Some(LayoutNodeType::Element(LayoutElementType::HTMLIFrameElement)) |
            Some(LayoutNodeType::Element(LayoutElementType::HTMLCanvasElement)) |
            Some(LayoutNodeType::Element(LayoutElementType::SVGSVGElement)) => true,
            Some(LayoutNodeType::Element(LayoutElementType::HTMLObjectElement)) => {
                let elem = self.as_element().unwrap();
                let type_and_data = (
                    elem.get_attr(&ns!(), &local_name!("type")),
                    elem.get_attr(&ns!(), &local_name!("data")),
                );
                match type_and_data {
                    (None, Some(uri)) => is_image_data(uri),
                    _ => false,
                }
            },
            Some(LayoutNodeType::Element(_)) => false,
            None => self.get_pseudo_element_type().is_replaced_content(),
        }
    }

    fn construction_result_mut(self, data: &mut InnerLayoutData) -> &mut ConstructionResult {
        match self.get_pseudo_element_type() {
            PseudoElementType::Before => &mut data.before_flow_construction_result,
            PseudoElementType::After => &mut data.after_flow_construction_result,
            PseudoElementType::DetailsSummary => &mut data.details_summary_flow_construction_result,
            PseudoElementType::DetailsContent => &mut data.details_content_flow_construction_result,
            PseudoElementType::Normal => &mut data.flow_construction_result,
        }
    }

    #[inline(always)]
    fn set_flow_construction_result(self, result: ConstructionResult) {
        let mut layout_data = self.mutate_layout_data().unwrap();
        let dst = self.construction_result_mut(&mut layout_data);
        *dst = result;
    }

    #[inline(always)]
    fn get_construction_result(self) -> ConstructionResult {
        let mut layout_data = self.mutate_layout_data().unwrap();
        self.construction_result_mut(&mut layout_data).get()
    }
}

impl FlowRef {
    /// Adds a new flow as a child of this flow. Fails if this flow is marked as a leaf.
    fn add_new_child(&mut self, mut new_child: FlowRef) {
        {
            let kid_base = FlowRef::deref_mut(&mut new_child).mut_base();
            kid_base.parallel.parent = parallel::mut_owned_flow_to_unsafe_flow(self);
        }

        let base = FlowRef::deref_mut(self).mut_base();
        base.children.push_back(new_child);
        let _ = base.parallel.children_count.fetch_add(1, Ordering::Relaxed);
    }

    /// Finishes a flow. Once a flow is finished, no more child flows or fragments may be added to
    /// it. This will normally run the bubble-inline-sizes (minimum and preferred -- i.e. intrinsic
    /// -- inline-size) calculation, unless the global `bubble_inline-sizes_separately` flag is on.
    ///
    /// All flows must be finished at some point, or they will not have their intrinsic inline-sizes
    /// properly computed. (This is not, however, a memory safety problem.)
    fn finish(&mut self) {
        if !opts::get().debug.bubble_inline_sizes_separately {
            FlowRef::deref_mut(self).bubble_inline_sizes();
            FlowRef::deref_mut(self)
                .mut_base()
                .restyle_damage
                .remove(ServoRestyleDamage::BUBBLE_ISIZES);
        }
    }
}

/// Strips ignorable whitespace from the start of a list of fragments.
pub fn strip_ignorable_whitespace_from_start(this: &mut LinkedList<Fragment>) {
    if this.is_empty() {
        return; // Fast path.
    }

    let mut leading_fragments_consisting_of_solely_bidi_control_characters = LinkedList::new();
    while !this.is_empty() {
        match this
            .front_mut()
            .as_mut()
            .unwrap()
            .strip_leading_whitespace_if_necessary()
        {
            WhitespaceStrippingResult::RetainFragment => break,
            WhitespaceStrippingResult::FragmentContainedOnlyBidiControlCharacters => {
                leading_fragments_consisting_of_solely_bidi_control_characters
                    .push_back(this.pop_front().unwrap())
            },
            WhitespaceStrippingResult::FragmentContainedOnlyWhitespace => {
                let removed_fragment = this.pop_front().unwrap();
                if let Some(ref mut remaining_fragment) = this.front_mut() {
                    remaining_fragment.meld_with_prev_inline_fragment(&removed_fragment);
                }
            },
        }
    }
    prepend_from(
        this,
        &mut leading_fragments_consisting_of_solely_bidi_control_characters,
    );
}

/// Strips ignorable whitespace from the end of a list of fragments.
pub fn strip_ignorable_whitespace_from_end(this: &mut LinkedList<Fragment>) {
    if this.is_empty() {
        return;
    }

    let mut trailing_fragments_consisting_of_solely_bidi_control_characters = LinkedList::new();
    while !this.is_empty() {
        match this
            .back_mut()
            .as_mut()
            .unwrap()
            .strip_trailing_whitespace_if_necessary()
        {
            WhitespaceStrippingResult::RetainFragment => break,
            WhitespaceStrippingResult::FragmentContainedOnlyBidiControlCharacters => {
                trailing_fragments_consisting_of_solely_bidi_control_characters
                    .push_front(this.pop_back().unwrap())
            },
            WhitespaceStrippingResult::FragmentContainedOnlyWhitespace => {
                let removed_fragment = this.pop_back().unwrap();
                if let Some(ref mut remaining_fragment) = this.back_mut() {
                    remaining_fragment.meld_with_next_inline_fragment(&removed_fragment);
                }
            },
        }
    }
    this.append(&mut trailing_fragments_consisting_of_solely_bidi_control_characters);
}

/// If the 'unicode-bidi' property has a value other than 'normal', return the bidi control codes
/// to inject before and after the text content of the element.
fn bidi_control_chars(style: &ServoArc<ComputedValues>) -> Option<(&'static str, &'static str)> {
    use style::computed_values::direction::T::*;
    use style::computed_values::unicode_bidi::T::*;

    let unicode_bidi = style.get_text().unicode_bidi;
    let direction = style.get_inherited_box().direction;

    // See the table in http://dev.w3.org/csswg/css-writing-modes/#unicode-bidi
    match (unicode_bidi, direction) {
        (Normal, _) => None,
        (Embed, Ltr) => Some(("\u{202A}", "\u{202C}")),
        (Embed, Rtl) => Some(("\u{202B}", "\u{202C}")),
        (Isolate, Ltr) => Some(("\u{2066}", "\u{2069}")),
        (Isolate, Rtl) => Some(("\u{2067}", "\u{2069}")),
        (BidiOverride, Ltr) => Some(("\u{202D}", "\u{202C}")),
        (BidiOverride, Rtl) => Some(("\u{202E}", "\u{202C}")),
        (IsolateOverride, Ltr) => Some(("\u{2068}\u{202D}", "\u{202C}\u{2069}")),
        (IsolateOverride, Rtl) => Some(("\u{2068}\u{202E}", "\u{202C}\u{2069}")),
        (Plaintext, _) => Some(("\u{2068}", "\u{2069}")),
    }
}

fn control_chars_to_fragment<E>(
    node: &InlineFragmentNodeInfo,
    context: &SharedStyleContext,
    text: &str,
    restyle_damage: RestyleDamage,
) -> Fragment
where
    E: TElement,
{
    let info = SpecificFragmentInfo::UnscannedText(Box::new(UnscannedTextFragmentInfo::new(
        Box::<str>::from(text),
        None,
    )));
    let text_style = context.stylist.style_for_anonymous::<E>(
        &context.guards,
        &PseudoElement::ServoLegacyText,
        &node.style,
    );

    Fragment::from_opaque_node_and_style(
        node.address,
        node.pseudo,
        text_style,
        node.selected_style.clone(),
        restyle_damage,
        info,
    )
}

/// Returns true if this node has non-zero padding or border.
fn has_padding_or_border(values: &ComputedValues) -> bool {
    let padding = values.get_padding();
    let border = values.get_border();

    use style::Zero;
    !padding.padding_top.is_definitely_zero() ||
        !padding.padding_right.is_definitely_zero() ||
        !padding.padding_bottom.is_definitely_zero() ||
        !padding.padding_left.is_definitely_zero() ||
        !border.border_top_width.is_zero() ||
        !border.border_right_width.is_zero() ||
        !border.border_bottom_width.is_zero() ||
        !border.border_left_width.is_zero()
}

/// Maintains a stack of anonymous boxes needed to ensure that the flow tree is *legal*. The tree
/// is legal if it follows the rules in CSS 2.1 § 17.2.1.
///
/// As an example, the legalizer makes sure that table row flows contain only table cells. If the
/// flow constructor attempts to place, say, a block flow directly underneath the table row, the
/// legalizer generates an anonymous table cell in between to hold the block.
///
/// Generally, the flow constructor should use `Legalizer::add_child()` instead of calling
/// `Flow::add_new_child()` directly. This ensures that the flow tree remains legal at all times
/// and centralizes the anonymous flow generation logic in one place.
struct Legalizer {
    /// A stack of anonymous flows that have yet to be finalized (i.e. that still could acquire new
    /// children).
    stack: Vec<FlowRef>,
}

impl Legalizer {
    /// Creates a new legalizer.
    fn new() -> Legalizer {
        Legalizer { stack: vec![] }
    }

    /// Makes the `child` flow a new child of `parent`. Anonymous flows are automatically inserted
    /// to keep the tree legal.
    fn add_child<E>(
        &mut self,
        context: &SharedStyleContext,
        parent: &mut FlowRef,
        mut child: FlowRef,
    ) where
        E: TElement,
    {
        while !self.stack.is_empty() {
            if self.try_to_add_child::<E>(context, parent, &mut child) {
                return;
            }
            self.flush_top_of_stack(parent)
        }

        while !self.try_to_add_child::<E>(context, parent, &mut child) {
            self.push_next_anonymous_flow::<E>(context, parent)
        }
    }

    /// Flushes all flows we've been gathering up.
    fn finish(mut self, parent: &mut FlowRef) {
        while !self.stack.is_empty() {
            self.flush_top_of_stack(parent)
        }
    }

    /// Attempts to make `child` a child of `parent`. On success, this returns true. If this would
    /// make the tree illegal, this method does nothing and returns false.
    ///
    /// This method attempts to create anonymous blocks in between `parent` and `child` if and only
    /// if those blocks will only ever have `child` as their sole child. At present, this is only
    /// true for anonymous block children of flex flows.
    fn try_to_add_child<E>(
        &mut self,
        context: &SharedStyleContext,
        parent: &mut FlowRef,
        child: &mut FlowRef,
    ) -> bool
    where
        E: TElement,
    {
        let parent = self.stack.last_mut().unwrap_or(parent);
        let (parent_class, child_class) = (parent.class(), child.class());
        match (parent_class, child_class) {
            (FlowClass::TableWrapper, FlowClass::Table) |
            (FlowClass::Table, FlowClass::TableColGroup) |
            (FlowClass::Table, FlowClass::TableRowGroup) |
            (FlowClass::Table, FlowClass::TableRow) |
            (FlowClass::Table, FlowClass::TableCaption) |
            (FlowClass::TableRowGroup, FlowClass::TableRow) |
            (FlowClass::TableRow, FlowClass::TableCell) => {
                parent.add_new_child((*child).clone());
                true
            },

            (FlowClass::TableWrapper, _) |
            (FlowClass::Table, _) |
            (FlowClass::TableRowGroup, _) |
            (FlowClass::TableRow, _) |
            (_, FlowClass::Table) |
            (_, FlowClass::TableColGroup) |
            (_, FlowClass::TableRowGroup) |
            (_, FlowClass::TableRow) |
            (_, FlowClass::TableCaption) |
            (_, FlowClass::TableCell) => false,

            (FlowClass::Flex, FlowClass::Inline) => {
                FlowRef::deref_mut(child)
                    .mut_base()
                    .flags
                    .insert(FlowFlags::MARGINS_CANNOT_COLLAPSE);
                let mut block_wrapper = Legalizer::create_anonymous_flow::<E, _>(
                    context,
                    parent,
                    &[PseudoElement::ServoLegacyAnonymousBlock],
                    SpecificFragmentInfo::Generic,
                    BlockFlow::from_fragment,
                );

                {
                    let flag = if parent.as_flex().main_mode() == Direction::Inline {
                        FragmentFlags::IS_INLINE_FLEX_ITEM
                    } else {
                        FragmentFlags::IS_BLOCK_FLEX_ITEM
                    };
                    let block = FlowRef::deref_mut(&mut block_wrapper).as_mut_block();
                    block.base.flags.insert(FlowFlags::MARGINS_CANNOT_COLLAPSE);
                    block.fragment.flags.insert(flag);
                }
                block_wrapper.add_new_child((*child).clone());
                block_wrapper.finish();
                parent.add_new_child(block_wrapper);
                true
            },

            (FlowClass::Flex, _) => {
                {
                    let flag = if parent.as_flex().main_mode() == Direction::Inline {
                        FragmentFlags::IS_INLINE_FLEX_ITEM
                    } else {
                        FragmentFlags::IS_BLOCK_FLEX_ITEM
                    };
                    let block = FlowRef::deref_mut(child).as_mut_block();
                    block.base.flags.insert(FlowFlags::MARGINS_CANNOT_COLLAPSE);
                    block.fragment.flags.insert(flag);
                }
                parent.add_new_child((*child).clone());
                true
            },

            _ => {
                parent.add_new_child((*child).clone());
                true
            },
        }
    }

    /// Finalizes the flow on the top of the stack.
    fn flush_top_of_stack(&mut self, parent: &mut FlowRef) {
        let mut child = self.stack.pop().expect("flush_top_of_stack(): stack empty");
        child.finish();
        self.stack.last_mut().unwrap_or(parent).add_new_child(child)
    }

    /// Adds the anonymous flow that would be necessary to make an illegal child of `parent` legal
    /// to the stack.
    fn push_next_anonymous_flow<E>(&mut self, context: &SharedStyleContext, parent: &FlowRef)
    where
        E: TElement,
    {
        let parent_class = self.stack.last().unwrap_or(parent).class();
        match parent_class {
            FlowClass::TableRow => self.push_new_anonymous_flow::<E, _>(
                context,
                parent,
                &[PseudoElement::ServoAnonymousTableCell],
                SpecificFragmentInfo::TableCell,
                TableCellFlow::from_fragment,
            ),
            FlowClass::Table | FlowClass::TableRowGroup => self.push_new_anonymous_flow::<E, _>(
                context,
                parent,
                &[PseudoElement::ServoAnonymousTableRow],
                SpecificFragmentInfo::TableRow,
                TableRowFlow::from_fragment,
            ),
            FlowClass::TableWrapper => self.push_new_anonymous_flow::<E, _>(
                context,
                parent,
                &[PseudoElement::ServoLegacyAnonymousTable],
                SpecificFragmentInfo::Table,
                TableFlow::from_fragment,
            ),
            _ => self.push_new_anonymous_flow::<E, _>(
                context,
                parent,
                &[
                    PseudoElement::ServoLegacyTableWrapper,
                    PseudoElement::ServoLegacyAnonymousTableWrapper,
                ],
                SpecificFragmentInfo::TableWrapper,
                TableWrapperFlow::from_fragment,
            ),
        }
    }

    /// Creates an anonymous flow and pushes it onto the stack.
    fn push_new_anonymous_flow<E, F>(
        &mut self,
        context: &SharedStyleContext,
        reference: &FlowRef,
        pseudos: &[PseudoElement],
        specific_fragment_info: SpecificFragmentInfo,
        constructor: fn(Fragment) -> F,
    ) where
        E: TElement,
        F: Flow,
    {
        let new_flow = Self::create_anonymous_flow::<E, _>(
            context,
            reference,
            pseudos,
            specific_fragment_info,
            constructor,
        );
        self.stack.push(new_flow)
    }

    /// Creates a new anonymous flow. The new flow is identical to `reference` except with all
    /// styles applying to every pseudo-element in `pseudos` applied.
    ///
    /// This method invokes the supplied constructor function on the given specific fragment info
    /// in order to actually generate the flow.
    fn create_anonymous_flow<E, F>(
        context: &SharedStyleContext,
        reference: &FlowRef,
        pseudos: &[PseudoElement],
        specific_fragment_info: SpecificFragmentInfo,
        constructor: fn(Fragment) -> F,
    ) -> FlowRef
    where
        E: TElement,
        F: Flow,
    {
        let reference_block = reference.as_block();
        let mut new_style = reference_block.fragment.style.clone();
        for pseudo in pseudos {
            new_style =
                context
                    .stylist
                    .style_for_anonymous::<E>(&context.guards, pseudo, &new_style);
        }
        let fragment = reference_block
            .fragment
            .create_similar_anonymous_fragment(new_style, specific_fragment_info);
        FlowRef::new(Arc::new(constructor(fragment)))
    }
}

pub fn is_image_data(uri: &str) -> bool {
    static TYPES: &[&str] = &["data:image/png", "data:image/gif", "data:image/jpeg"];
    TYPES.iter().any(|&type_| uri.starts_with(type_))
}