spindrift 0.1.0

A HTML/CSS to PDF renderer, targeting spec compliance and efficiency
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
use super::{
    AlignContent, AlignItems, AlignSelf, AspectRatioCalculationBox, BoxSizing, ComputedStyle,
    ContentAlignmentKeyword, ContentBoxLength, Direction,
    FlexAutomaticMinimumAutomaticPreferredCrossSize, FlexAutomaticMinimumContentSizeSource,
    FlexAutomaticMinimumCrossIntrinsicContributions, FlexAutomaticMinimumInputs,
    FlexAvailablePercentageBasis, FlexAvailableSizeSource, FlexAvailableSpace, FlexCrossSize,
    FlexCrossSizeProperty, FlexCrossSizingPhase, FlexDirection, FlexHypotheticalAutomaticCrossSize,
    FlexItemAvailableSpace, FlexItemBaselineEstimate, FlexItemEstimate, FlexMainSize,
    IntrinsicItemMetrics, JustifyContent, LayoutLength, LogicalAxis, LogicalInlineContentSize,
    LogicalInlinePercentageBasis, NonContentLength, PercentageBasis, PhysicalAxis,
    PhysicalContentHeight, PhysicalContentWidth, RatioOnlyReplacedFlexBaseSize,
    ReplacedPreferredSize, ReplacedPreferredSizeAxes, ReplacedSizeConstraints, ResolvedAspectRatio,
    SelfAlignmentKeyword, SemanticLengthExt, TaffyPoint, TaffyRect, TaffySize, WritingModeAxes,
    constrain_height_with_intrinsic, constrain_width_with_intrinsic, content_box_pt,
    content_box_size_pt, content_box_to_border_box_length, css, flex_main_content_box_length,
    horizontal_border_width, layout_pt, measure_intrinsic_item_leaf, non_content_pt,
    percentage_basis_from_points, resolve_replaced_size_with_aspect_ratio,
    stretch_fit_content_box_size, taffy_layout, used_border_widths,
    used_box_metrics_for_logical_inline_basis, used_content_box_height_or_auto_with_basis,
    used_content_box_width_or_auto_with_basis, used_length_percentage_or_auto,
    used_length_percentage_or_auto_with_basis, used_max_width, used_min_width,
    vertical_border_width,
};
use crate::layout::taffy_bridge;

mod alignment;
pub(super) use alignment::*;

type FlexLogicalInlinePercentageBasis = LogicalInlinePercentageBasis<FlexAvailableSizeSource>;

/// Wraps a raw Taffy layout result in the Taffy coordinate space.
///
/// Taffy returns physical x/y coordinates after Spindrift has mapped CSS flex axes
/// and writing direction into Taffy's row/column model. The returned rect must
/// be converted to container coordinates before storage in flex layout data:
/// <https://www.w3.org/TR/css-flexbox-1/#layout-algorithm>.
pub(super) fn taffy_rect_from_layout(layout: &taffy_layout::Layout) -> TaffyRect {
    TaffyRect::new(
        TaffyPoint::new(layout.location.x, layout.location.y),
        TaffySize::new(layout.size.width, layout.size.height),
    )
}

/// Converts computed CSS margins to Taffy's flex-item margin representation.
///
/// CSS Flexible Box Layout uses margin boxes during flex item sizing and
/// alignment, and CSS Box Model permits negative margins to shift and overlap
/// boxes:
/// <https://www.w3.org/TR/css-flexbox-1/#box-model> and
/// <https://www.w3.org/TR/CSS22/box.html#margin-properties>.
pub(super) fn taffy_margin(
    style: &ComputedStyle,
    containing_style: &ComputedStyle,
    available: FlexAvailableSpace,
) -> taffy_layout::Rect<taffy_layout::LengthPercentageAuto> {
    let percentage_basis = logical_inline_percentage_basis(containing_style, available);
    taffy_bridge::margin(
        style,
        percentage_basis,
        taffy_bridge::TaffyCyclicPercentage::PreservePurePercentage,
    )
}

/// Converts computed CSS padding to Taffy's flex-item padding representation.
///
/// CSS Flexible Box Layout sizes flex items using their box model, and CSS
/// Box Model defines padding edge behavior:
/// <https://www.w3.org/TR/css-flexbox-1/#box-model> and
/// <https://www.w3.org/TR/CSS22/box.html#padding-properties>.
pub(super) fn taffy_padding(
    style: &ComputedStyle,
    containing_style: &ComputedStyle,
    available: FlexAvailableSpace,
) -> taffy_layout::Rect<taffy_layout::LengthPercentage> {
    // CSS Box resolves every physical padding percentage against the
    // containing block's logical inline size. Resolve it at this typed
    // physical/logical adapter boundary instead of letting Taffy apply a
    // physical-width percentage in vertical writing modes:
    // <https://www.w3.org/TR/css-box-3/#padding-physical>.
    let percentage_basis = logical_inline_percentage_basis(containing_style, available);
    taffy_bridge::padding(style, percentage_basis)
}

/// Resolve a flex item's padding for replay and box-model calculations.
///
/// Taffy receives physical edges, but CSS padding percentages retain the
/// flex container's logical inline basis even in vertical writing modes:
/// <https://www.w3.org/TR/css-box-3/#padding-physical>.
pub(super) fn flex_item_used_padding(
    style: &ComputedStyle,
    containing_style: &ComputedStyle,
    available: FlexAvailableSpace,
) -> css::Edges {
    let percentage_basis = logical_inline_percentage_basis(containing_style, available);
    if percentage_basis.is_definite() {
        used_box_metrics_for_logical_inline_basis(style, percentage_basis.map_source(|_| ()))
            .padding
            .to_css_edges()
    } else {
        style.padding
    }
}

/// Resolve a flex item's physical margins using its container's logical inline
/// percentage basis. Auto margins remain zero for size calculations; Taffy
/// retains their auto state separately for alignment.
/// <https://www.w3.org/TR/css-box-3/#margin-physical>.
pub(super) fn flex_item_used_margin(
    style: &ComputedStyle,
    containing_style: &ComputedStyle,
    available: FlexAvailableSpace,
) -> css::Edges {
    let percentage_basis = logical_inline_percentage_basis(containing_style, available);
    if percentage_basis.is_definite() {
        used_box_metrics_for_logical_inline_basis(style, percentage_basis.map_source(|_| ()))
            .margin
            .to_css_edges()
    } else {
        style.margin
    }
}

/// Return the percentage basis for physical box edges in a flex container.
///
/// CSS Box resolves all margin and padding percentages against the containing
/// block's logical inline size, before the Taffy adapter uses physical edges:
/// <https://www.w3.org/TR/css-box-3/#margin-physical>.
fn logical_inline_percentage_basis(
    containing_style: &ComputedStyle,
    available: FlexAvailableSpace,
) -> FlexLogicalInlinePercentageBasis {
    if WritingModeAxes::new(
        containing_style.writing_mode,
        containing_style.used_direction(),
    )
    .swaps_physical_axes()
    {
        available.height_basis
    } else {
        available.width_basis
    }
    .map_value(LogicalInlineContentSize::new)
}

/// Converts used border widths to Taffy's length-only edge representation.
///
/// CSS Flexible Box Layout includes borders in flex item sizing through the
/// CSS box model:
/// <https://www.w3.org/TR/css-flexbox-1/#box-model>.
pub(super) fn taffy_edges(edges: css::Edges) -> taffy_layout::Rect<taffy_layout::LengthPercentage> {
    taffy_bridge::border_edges(edges)
}

/// Converts computed CSS gaps to Taffy's flex gap representation.
///
/// CSS Box Alignment defines `normal` gaps as zero in flex layout and
/// percentage gaps as resolving against the corresponding content-box size.
/// Cyclic percentage gaps contribute zero during intrinsic sizing, so an
/// indefinite basis preserves only the non-percentage length component:
/// <https://www.w3.org/TR/css-align-3/#gap-percent>.
pub(super) fn taffy_gap(
    value: css::ComputedGap,
    percentage_basis: FlexAvailablePercentageBasis,
) -> taffy_layout::LengthPercentage {
    taffy_bridge::gap(value, percentage_basis)
}

/// Converts a flex item's physical size for Taffy at an explicit Flexbox
/// cross-sizing phase.
///
/// CSS Flexbox resolves `auto` main sizes through content sizing for the flex
/// base size, but `align-items: stretch` requires the flex item's cross-size
/// property to remain automatic until flex lines are resolved. When
/// `flex-basis` is not `auto`, it is used in place of the main-size property
/// for flex base sizing, so the authored main-size property must not be
/// supplied to Taffy as a known main-axis size:
/// <https://www.w3.org/TR/css-flexbox-1/#layout-algorithm> and
/// <https://www.w3.org/TR/css-flexbox-1/#algo-stretch> and
/// <https://drafts.csswg.org/css-sizing-4/#stretch-fit-sizing>.
pub(super) fn flex_item_size_dimension(
    value: css::ComputedLengthPercentageOrAuto,
    fallback: ContentBoxLength,
    min_content: ContentBoxLength,
    max_content: ContentBoxLength,
    context: FlexItemSizeDimensionContext,
) -> taffy_layout::Dimension {
    if context
        .flex_direction
        .shares_axis_with(context.dimension_axis)
    {
        if context.flex_basis_overrides_main_size {
            return taffy_layout::Dimension::auto();
        }
        // An unresolved percentage main size is `auto` for flex-basis:auto.
        // The flex-basis adapter below supplies the item's content-based
        // fallback; passing Taffy a zero-length `size` here would instead
        // suppress that fallback and clip the item's normal content.
        // <https://www.w3.org/TR/css-flexbox-1/#flex-basis-property>
        // <https://www.w3.org/TR/css-sizing-3/#percentages>
        if matches!(value, css::ComputedLengthPercentageOrAuto::LengthPercentage(ref value) if value.needs_percentage_basis() && !context.percentage_basis.is_definite())
        {
            return taffy_layout::Dimension::auto();
        }
        match value {
            css::ComputedLengthPercentageOrAuto::Auto => {
                taffy_layout::Dimension::length(fallback.points().max(1.0))
            }
            _ => taffy_intrinsic_dimension_with_basis_and_stretch(
                value,
                context.percentage_basis,
                min_content,
                max_content,
                context.stretch,
            ),
        }
    } else {
        match value {
            css::ComputedLengthPercentageOrAuto::Auto => match context.cross_sizing_phase {
                FlexCrossSizingPhase::Hypothetical => {
                    match context.hypothetical_automatic_cross_size {
                        FlexHypotheticalAutomaticCrossSize::Intrinsic => {
                            taffy_layout::Dimension::auto()
                        }
                        FlexHypotheticalAutomaticCrossSize::FitContent { used_content_size } => {
                            taffy_layout::Dimension::length(used_content_size.points().max(0.0))
                        }
                    }
                }
                FlexCrossSizingPhase::StretchToLine {
                    line_outer_cross_size,
                } => taffy_stretch_fit_dimension(FlexStretchFitContext {
                    available_margin_box_size: Some(layout_pt(line_outer_cross_size.points())),
                    ..context.stretch
                }),
            },
            _ => taffy_intrinsic_dimension_with_basis_and_stretch(
                value,
                context.percentage_basis,
                min_content,
                max_content,
                context.stretch,
            ),
        }
    }
}

#[derive(Debug, Clone, Copy)]
pub(super) struct FlexItemSizeDimensionContext {
    pub(super) flex_direction: FlexDirection,
    pub(super) dimension_axis: FlexDirection,
    pub(super) percentage_basis: FlexAvailablePercentageBasis,
    pub(super) stretch: FlexStretchFitContext,
    pub(super) flex_basis_overrides_main_size: bool,
    /// The only cross-size phase that may influence this physical dimension.
    /// Main-axis callers carry the same context but ignore this value.
    pub(super) cross_sizing_phase: FlexCrossSizingPhase,
    /// Automatic sizing input used exclusively during the hypothetical phase.
    pub(super) hypothetical_automatic_cross_size: FlexHypotheticalAutomaticCrossSize,
}

#[derive(Debug, Clone, Copy)]
pub(super) struct FlexStretchFitContext {
    pub(super) available_margin_box_size: Option<LayoutLength>,
    pub(super) margin_size: LayoutLength,
    pub(super) non_content_size: NonContentLength,
    pub(super) box_sizing: BoxSizing,
}

fn taffy_stretch_fit_dimension(context: FlexStretchFitContext) -> taffy_layout::Dimension {
    let Some(content_size) = resolved_stretch_fit_content_box_size(context) else {
        return taffy_layout::Dimension::auto();
    };
    let size = match context.box_sizing {
        BoxSizing::ContentBox => content_size.points(),
        BoxSizing::BorderBox => {
            content_box_to_border_box_length(content_size, context.non_content_size).points()
        }
    };
    taffy_layout::Dimension::length(size.max(0.0))
}

/// Resolves a definite stretch-fit margin-box slot into the item's content-box
/// size.
///
/// This is shared by normal `stretch` sizing and Flexbox's automatic-minimum
/// transferred-size suggestion, which both need the same box-model conversion:
/// <https://drafts.csswg.org/css-sizing-4/#stretch-fit-sizing> and
/// <https://drafts.csswg.org/css-flexbox-1/#min-size-auto>.
fn resolved_stretch_fit_content_box_size(
    context: FlexStretchFitContext,
) -> Option<ContentBoxLength> {
    context.available_margin_box_size.map(|available| {
        stretch_fit_content_box_size(available, context.margin_size, context.non_content_size)
    })
}

/// Converts a CSS size to Taffy, resolving mixed length-percentages when possible.
///
/// CSS Values allows `<length-percentage>` math such as `calc(50% + 10pt)`.
/// Taffy's style interface cannot carry Spindrift's mixed CSS math representation
/// or percentage-definiteness semantics, so flex layout resolves mixed values
/// at this bridge when the relevant flex container axis is definite. CSS Sizing
/// leaves nonzero percentages unresolved when their basis is indefinite, so
/// those values stay automatic for Taffy and use intrinsic measurement instead:
/// <https://www.w3.org/TR/css-values-4/#mixed-percentages>,
/// <https://www.w3.org/TR/css-sizing-3/#percentage-sizing>, and
/// <https://www.w3.org/TR/css-flexbox-1/#layout-algorithm>.
pub(super) fn taffy_optional_dimension_with_basis(
    value: css::ComputedLengthPercentageOrAuto,
    percentage_basis: FlexAvailablePercentageBasis,
) -> taffy_layout::Dimension {
    taffy_flex_optional_dimension(
        value,
        FlexTaffyPercentagePolicy::ResolveAgainstDefiniteBasis(percentage_basis),
    )
}

/// Selects whether a flex size may remain a symbolic Taffy percentage.
///
/// Flex item's used size resolution needs a definite containing-block basis,
/// while the flex container's own optional maximum constraint may preserve a
/// pure percentage for Taffy's later resolution. These are separate CSS
/// sizing phases, not interchangeable representations:
/// <https://www.w3.org/TR/css-sizing-3/#percentage-sizing> and
/// <https://www.w3.org/TR/css-flexbox-1/#layout-algorithm>.
#[derive(Debug, Clone, Copy)]
enum FlexTaffyPercentagePolicy {
    PreservePurePercentage,
    ResolveAgainstDefiniteBasis(FlexAvailablePercentageBasis),
}

/// Convert a Flex optional size through the policy selected by its CSS phase.
///
/// The value grammar is shared between the symbolic container constraint and
/// the definite-basis item sizing path. The policy governs only the
/// `<length-percentage>` arm; Flex's higher-level flex-basis, stretch-fit, and
/// automatic minimum-size logic remains with its callers.
fn taffy_flex_optional_dimension(
    value: css::ComputedLengthPercentageOrAuto,
    percentage_policy: FlexTaffyPercentagePolicy,
) -> taffy_layout::Dimension {
    match value {
        css::ComputedLengthPercentageOrAuto::LengthPercentage(value) => {
            taffy_flex_length_percentage_dimension(value, percentage_policy)
        }
        css::ComputedLengthPercentageOrAuto::Auto
        | css::ComputedLengthPercentageOrAuto::Stretch
        | css::ComputedLengthPercentageOrAuto::MinContent
        | css::ComputedLengthPercentageOrAuto::MaxContent
        | css::ComputedLengthPercentageOrAuto::FitContent(_)
        | css::ComputedLengthPercentageOrAuto::CalcSize(_) => taffy_layout::Dimension::auto(),
    }
}

fn taffy_flex_length_percentage_dimension(
    value: css::ComputedLengthPercentage,
    percentage_policy: FlexTaffyPercentagePolicy,
) -> taffy_layout::Dimension {
    let FlexTaffyPercentagePolicy::ResolveAgainstDefiniteBasis(percentage_basis) =
        percentage_policy
    else {
        return taffy_dimension_from_length_percentage(value);
    };
    let percentage_basis = percentage_basis.points();
    if let Some(basis) = percentage_basis
        && let Some(resolved) = value
            .used_length_with_percentage_basis(PercentageBasis::definite(layout_pt(basis.max(0.0))))
    {
        return taffy_layout::Dimension::length(resolved.points());
    }
    if value.needs_percentage_basis() && percentage_basis.is_none() {
        return taffy_layout::Dimension::auto();
    }
    if !value.is_definitely_absolute()
        && value
            .used_length_with_percentage_basis(PercentageBasis::<ContentBoxLength>::indefinite())
            .is_none()
    {
        return taffy_layout::Dimension::auto();
    }
    taffy_dimension_from_length_percentage(value)
}

/// Converts a CSS size constraint to Taffy when intrinsic contributions are known.
///
/// CSS Sizing defines `min-content`, `max-content`, and `fit-content()` as
/// intrinsic size keywords. Flex layout has already estimated each flex item's
/// intrinsic contributions before building the Taffy tree, so min/max
/// constraints can be resolved here instead of being dropped:
/// <https://www.w3.org/TR/css-sizing-3/#intrinsic-sizes> and
/// <https://www.w3.org/TR/css-flexbox-1/#layout-algorithm>.
pub(super) fn taffy_intrinsic_dimension_with_basis(
    value: css::ComputedLengthPercentageOrAuto,
    percentage_basis: FlexAvailablePercentageBasis,
    min_content: ContentBoxLength,
    max_content: ContentBoxLength,
) -> taffy_layout::Dimension {
    taffy_intrinsic_dimension_with_basis_and_stretch(
        value,
        percentage_basis,
        min_content,
        max_content,
        FlexStretchFitContext {
            available_margin_box_size: None,
            margin_size: layout_pt(0.0),
            non_content_size: non_content_pt(0.0),
            box_sizing: BoxSizing::ContentBox,
        },
    )
}

pub(super) fn taffy_intrinsic_dimension_with_basis_and_stretch(
    value: css::ComputedLengthPercentageOrAuto,
    percentage_basis: FlexAvailablePercentageBasis,
    min_content: ContentBoxLength,
    max_content: ContentBoxLength,
    stretch: FlexStretchFitContext,
) -> taffy_layout::Dimension {
    let min_content = min_content.points().max(0.0);
    let max_content = max_content.points().max(min_content);
    match value {
        css::ComputedLengthPercentageOrAuto::Auto => taffy_layout::Dimension::auto(),
        css::ComputedLengthPercentageOrAuto::Stretch => taffy_stretch_fit_dimension(stretch),
        css::ComputedLengthPercentageOrAuto::LengthPercentage(_) => {
            taffy_optional_dimension_with_basis(value, percentage_basis)
        }
        css::ComputedLengthPercentageOrAuto::MinContent => {
            taffy_layout::Dimension::length(min_content.max(0.0))
        }
        css::ComputedLengthPercentageOrAuto::MaxContent => {
            taffy_layout::Dimension::length(max_content.max(min_content).max(0.0))
        }
        css::ComputedLengthPercentageOrAuto::FitContent(limit) => {
            let stretch = limit
                .and_then(|value| {
                    if value.is_definitely_absolute() {
                        Some(value.length_max_zero().points())
                    } else {
                        percentage_basis.points().and_then(|basis| {
                            value
                                .used_length_with_percentage_basis(PercentageBasis::definite(
                                    layout_pt(basis.max(0.0)),
                                ))
                                .map(|length| length.points())
                        })
                    }
                })
                .unwrap_or_else(|| max_content.max(min_content).max(0.0));
            taffy_layout::Dimension::length(
                max_content
                    .max(min_content)
                    .min(stretch.max(min_content))
                    .max(0.0),
            )
        }
        css::ComputedLengthPercentageOrAuto::CalcSize(value) => {
            let percentage_basis = percentage_basis.points().unwrap_or(0.0);
            let stretch_size = stretch
                .available_margin_box_size
                .map(|value| value.points())
                .unwrap_or(percentage_basis)
                .max(0.0);
            let fit_content = max_content.min(min_content.max(stretch_size));
            taffy_layout::Dimension::length(
                value
                    .used_value(
                        max_content,
                        min_content,
                        max_content,
                        fit_content,
                        stretch_size,
                        PercentageBasis::definite(layout_pt(percentage_basis)),
                    )
                    .max(layout_pt(0.0))
                    .points(),
            )
        }
    }
}

/// Measures a leaf flex item for Taffy's layout algorithm from intrinsic estimates.
///
/// CSS Flexbox lays out each flex item to determine its flex base size and
/// hypothetical cross size, then later may override known dimensions during
/// line sizing and stretch alignment:
/// <https://www.w3.org/TR/css-flexbox-1/#layout-algorithm> and
/// <https://www.w3.org/TR/css-flexbox-1/#algo-cross-item>.
pub(super) fn measure_flex_item(
    known_dimensions: taffy_layout::Size<Option<f32>>,
    _available_space: taffy_layout::Size<taffy_layout::AvailableSpace>,
    estimate: Option<&mut FlexItemEstimate>,
) -> taffy_layout::Size<f32> {
    let estimate = estimate.copied().unwrap_or_else(|| {
        FlexItemEstimate::new(
            IntrinsicItemMetrics::zero(),
            FlexItemBaselineEstimate::default(),
        )
    });
    // Taffy receives generic scalar metrics. This is the explicit Flex
    // baseline transport boundary; all Flex recursion retains typed pairs.
    let metrics = estimate.legacy_metrics();
    measure_intrinsic_item_leaf(
        known_dimensions,
        metrics.preferred_aspect_ratio,
        taffy_layout::Size {
            width: metrics.width.points(),
            height: metrics.height.points(),
        },
    )
}

/// Adapt Spindrift's typed flex-item estimate to Taffy 0.14's leaf-layout
/// callback. Only physical top-edge baselines are supplied; vertical-writing
/// and last-baseline reconciliation remain Spindrift-owned.
pub(super) fn taffy_flex_measurement(
    input: ::taffy::tree::LayoutInput,
    estimate: Option<&mut FlexItemEstimate>,
) -> ::taffy::tree::LayoutOutput {
    let baselines = estimate
        .as_deref()
        .map(|estimate| (*estimate).legacy_metrics())
        .map(|metrics| (metrics.first_baseline, metrics.last_baseline))
        .unwrap_or((None, None));
    let size = measure_flex_item(input.known_dimensions, input.available_space, estimate);
    taffy_bridge::measured_leaf_output(size, baselines.0, baselines.1)
}

/// Converts a CSS optional size to Taffy's `Dimension`.
///
/// CSS Values defines the `<length-percentage> | auto` shape used by flex item
/// width, height, and flex-basis:
/// <https://www.w3.org/TR/css-values-4/#mixed-percentages>.
pub(super) fn taffy_optional_dimension(
    value: css::ComputedLengthPercentageOrAuto,
) -> taffy_layout::LengthPercentageAuto {
    taffy_bridge::min_max_constraint(taffy_flex_optional_dimension(
        value,
        FlexTaffyPercentagePolicy::PreservePurePercentage,
    ))
}

fn taffy_dimension_from_length_percentage(
    value: css::ComputedLengthPercentage,
) -> taffy_layout::Dimension {
    if let Some(percent) = value
        .pure_percentage_coefficient()
        .filter(|percent| *percent != 0.0)
    {
        taffy_layout::Dimension::percent(percent.max(0.0))
    } else {
        taffy_layout::Dimension::length(value.length_max_zero().points())
    }
}

/// Converts a CSS min-size value for a flex container root.
///
/// CSS Sizing defines the initial `min-width`/`min-height` as `auto`; for a
/// flex container's own used size, that automatic minimum does not become the
/// flex item automatic minimum from Flexbox 4.5, so the root minimum is zero
/// unless the author supplies a definite length/percentage:
/// <https://www.w3.org/TR/css-sizing-3/#min-size-properties> and
/// <https://www.w3.org/TR/css-flexbox-1/#min-size-auto>.
pub(super) fn taffy_min_dimension(
    value: css::ComputedLengthPercentageOrAuto,
    percentage_basis: FlexAvailablePercentageBasis,
) -> taffy_layout::LengthPercentageAuto {
    used_length_percentage_or_auto(value, percentage_basis)
        .map(|length| taffy_layout::LengthPercentageAuto::length(length.points()))
        .unwrap_or_else(|| taffy_layout::LengthPercentageAuto::length(0.0))
}

/// Resolve a table flex item's authored length/percentage minimum without
/// discarding the table grid's intrinsic minimum.
///
/// CSS Tables makes the used table minimum at least its min-content width,
/// while a definite (including percentage-resolved) authored `min-width` can
/// raise that floor.  Taffy has no `max(length, percent)` dimension, so this
/// adapter resolves the author value at the Flex percentage-basis boundary.
/// An indefinite percentage basis leaves the intrinsic table floor in place.
/// <https://drafts.csswg.org/css-tables-3/#computing-the-table-width>
/// <https://www.w3.org/TR/css-sizing-3/#min-size-properties>
pub(super) fn table_length_percentage_min_dimension(
    value: css::ComputedLengthPercentageOrAuto,
    percentage_basis: FlexAvailablePercentageBasis,
    table_min_content: ContentBoxLength,
) -> taffy_layout::LengthPercentageAuto {
    debug_assert!(matches!(
        value,
        css::ComputedLengthPercentageOrAuto::LengthPercentage(_)
    ));
    let table_min_content = table_min_content.max(content_box_pt(0.0));
    let table_min_layout = layout_pt(table_min_content.points());
    let authored = used_length_percentage_or_auto(value, percentage_basis)
        .unwrap_or(table_min_layout)
        .max(table_min_layout);
    taffy_layout::LengthPercentageAuto::length(authored.points())
}

/// Computes Taffy's automatic minimum size for a flex item.
///
/// CSS Flexbox section 4.5 defines the automatic minimum size of flex items as
/// a content-based minimum size. For flex items with a preferred aspect ratio,
/// that content-based minimum combines the content size suggestion and the
/// transferred size suggestion from CSS Sizing:
/// <https://www.w3.org/TR/css-flexbox-1/#min-size-auto>,
/// <https://www.w3.org/TR/css-flexbox-1/#content-based-minimum-size>, and
/// <https://www.w3.org/TR/css-sizing-4/#aspect-ratio>.
pub(super) fn flex_min_size_dimension(
    specified: css::ComputedLengthPercentageOrAuto,
    estimated_min_content: ContentBoxLength,
    estimated_max_content: ContentBoxLength,
    context: FlexMinSizeDimensionContext<'_>,
) -> taffy_layout::LengthPercentageAuto {
    let Some(automatic_minimum) = resolve_automatic_flex_minimum(specified.clone(), context) else {
        return taffy_bridge::min_max_constraint(taffy_intrinsic_dimension_with_basis_and_stretch(
            specified,
            context.percentage_basis,
            estimated_min_content,
            estimated_max_content,
            context.stretch,
        ));
    };
    taffy_layout::LengthPercentageAuto::length(automatic_minimum.used_content_box.points())
}

/// The resolved content-based automatic minimum of one flex-item axis.
///
/// This record is deliberately shared by the Taffy adapter and Spindrift's
/// post-layout safeguard.  CSS Flexbox defines a single automatic-minimum
/// decision; keeping its suggestions and selected content-box result together
/// prevents either consumer from silently selecting a different minimum.
/// <https://www.w3.org/TR/css-flexbox-1/#min-size-auto>
#[derive(Debug, Clone, Copy)]
pub(super) struct AutomaticFlexMinimum {
    pub(super) content_size_suggestion: ContentBoxLength,
    pub(super) transferred_size_suggestion: Option<ContentBoxLength>,
    pub(super) specified_size_suggestion: Option<ContentBoxLength>,
    pub(super) used_content_box: ContentBoxLength,
}

impl AutomaticFlexMinimum {
    pub(super) fn from_suggestions(
        content_size_suggestion: ContentBoxLength,
        transferred_size_suggestion: Option<ContentBoxLength>,
        specified_size_suggestion: Option<ContentBoxLength>,
        is_replaced: bool,
    ) -> Self {
        let mut used_content_box = content_size_suggestion.max(content_box_pt(0.0));
        if let Some(transferred) = transferred_size_suggestion {
            let transferred = transferred.max(content_box_pt(0.0));
            used_content_box = if is_replaced {
                used_content_box.min(transferred)
            } else {
                used_content_box.max(transferred)
            };
        }
        if let Some(specified) = specified_size_suggestion {
            used_content_box = used_content_box.min(specified.max(content_box_pt(0.0)));
        }
        Self {
            content_size_suggestion,
            transferred_size_suggestion,
            specified_size_suggestion,
            used_content_box,
        }
    }

    pub(super) fn debug_assert_consistent(self, is_replaced: bool) {
        debug_assert!(self.used_content_box >= content_box_pt(0.0));
        if let Some(specified) = self.specified_size_suggestion {
            debug_assert!(self.used_content_box <= specified.max(content_box_pt(0.0)));
        }
        if let Some(transferred) = self.transferred_size_suggestion {
            let transferred = transferred.max(content_box_pt(0.0));
            if is_replaced {
                debug_assert!(self.used_content_box <= transferred);
            } else {
                debug_assert!(
                    self.used_content_box >= self.content_size_suggestion.max(content_box_pt(0.0))
                );
            }
        }
    }
}

/// Resolve Flexbox's automatic main-axis minimum, if this axis uses one.
///
/// A cross-axis automatic minimum remains `auto` for Taffy, and a scrollable
/// main-axis item has a zero automatic minimum.  The returned value is always
/// a non-negative content-box length; callers perform their explicit
/// content-to-border-box conversion at their own backend boundary.
/// <https://www.w3.org/TR/css-flexbox-1/#min-size-auto>
pub(super) fn resolve_automatic_flex_minimum(
    specified: css::ComputedLengthPercentageOrAuto,
    context: FlexMinSizeDimensionContext<'_>,
) -> Option<AutomaticFlexMinimum> {
    let minimum_kind =
        flex_main_axis_content_based_minimum_kind(&specified, context.style, context.direction)?;
    if !context.is_main_axis {
        return None;
    }
    if minimum_kind == FlexContentBasedMinimumKind::CssAutomatic && context.overflow.is_scrollable()
    {
        return Some(AutomaticFlexMinimum {
            content_size_suggestion: content_box_pt(0.0),
            transferred_size_suggestion: None,
            specified_size_suggestion: None,
            used_content_box: content_box_pt(0.0),
        });
    }
    let inputs = context.automatic_minimum_inputs?;
    // CSS Flexbox 4.5: non-scrollable flex items use the content-based
    // minimum size in the main axis, capped by a definite preferred main
    // size. Cross-axis auto minimums remain automatic.
    // The pass-scoped input preserves whether this is a genuine intrinsic
    // contribution or a ratio-only replaced item. `calc-size(auto, …)` must
    // still peel back an estimate's raised floor before substituting `auto`.
    // <https://drafts.csswg.org/css-values-5/#calc-size>.
    let intrinsic_content_size_suggestion = if specified.calc_size_with_auto_basis().is_some() {
        inputs
            .content_size_source
            .content_size_suggestion()
            .min(inputs.max_content_size)
    } else {
        inputs.content_size_source.content_size_suggestion()
    };
    let authored_stretch_fit_cross_size = authored_cross_stretch_fit_content_box_size(
        context.style,
        context.direction,
        context.cross_stretch,
    );
    let transferred_size_suggestion = automatic_minimum_transferred_size_suggestion(
        context.style,
        context.direction,
        inputs.preferred_aspect_ratio,
        FlexCrossSizeSuggestionContext {
            available_cross_size: context.available_cross_size,
            authored_stretch_fit_cross_size,
            stretched_cross_size: context.stretched_cross_size,
            automatic_preferred_cross_size: inputs
                .automatic_preferred_cross_size
                .content_box_size(),
            intrinsic: FlexCrossIntrinsicContributions {
                min_content: inputs.cross_intrinsic.min_content,
                max_content: inputs.cross_intrinsic.max_content,
            },
        },
    )
    .map(|suggestion| {
        inputs
            .aspect_ratio_sizing
            .map(|sizing| {
                if context.direction.is_row_axis() {
                    sizing.constraints.constrain_width(suggestion)
                } else {
                    sizing.constraints.constrain_height(suggestion)
                }
            })
            .unwrap_or(suggestion)
    });
    // A replaced item's automatic preferred main size is itself derived from
    // its definite preferred cross size and ratio. In particular, an authored
    // cross-axis `stretch` is not merely an additional minimum constraint on
    // the intrinsic object: it establishes the replaced item's used content
    // contribution before Flexbox compares content and transferred
    // suggestions. Otherwise the replaced-element `min()` rule would retain
    // the natural object size and defeat a larger stretch-fit cross size.
    // <https://drafts.csswg.org/css-sizing-4/#stretch-fit-sizing> and
    // <https://drafts.csswg.org/css-flexbox-1/#min-size-auto>.
    let content_size_suggestion = if inputs.is_replaced && authored_stretch_fit_cross_size.is_some()
    {
        transferred_size_suggestion.unwrap_or(intrinsic_content_size_suggestion)
    } else {
        intrinsic_content_size_suggestion
    };
    let selection = AutomaticFlexMinimum::from_suggestions(
        content_size_suggestion,
        transferred_size_suggestion,
        inputs.definite_preferred_content_size,
        inputs.is_replaced,
    );
    selection.debug_assert_consistent(inputs.is_replaced);
    let automatic_minimum = selection.used_content_box.points();
    let automatic_minimum = specified
        .calc_size_with_auto_basis()
        .map(|value| {
            value
                .used_value(
                    automatic_minimum,
                    content_size_suggestion.points(),
                    inputs.max_content_size.points(),
                    automatic_minimum,
                    context
                        .stretch
                        .available_margin_box_size
                        .map(SemanticLengthExt::points)
                        .unwrap_or(0.0),
                    PercentageBasis::definite(layout_pt(
                        context.percentage_basis.points().unwrap_or(0.0),
                    )),
                )
                .points()
        })
        .unwrap_or(automatic_minimum);
    Some(AutomaticFlexMinimum {
        content_size_suggestion,
        transferred_size_suggestion,
        specified_size_suggestion: inputs.definite_preferred_content_size,
        used_content_box: content_box_pt(automatic_minimum.max(0.0)),
    })
}

#[derive(Debug, Clone, Copy)]
pub(super) struct FlexMinSizeDimensionContext<'a> {
    pub(super) style: &'a ComputedStyle,
    pub(super) direction: FlexDirection,
    pub(super) automatic_minimum_inputs: Option<FlexAutomaticMinimumInputs>,
    pub(super) available_cross_size: Option<FlexCrossSize>,
    /// The cross-axis stretch-fit context for an authored `width` or `height`
    /// whose value is `stretch`. This is distinct from `stretched_cross_size`, which records
    /// Flexbox self-alignment stretch after line sizing.
    pub(super) cross_stretch: FlexStretchFitContext,
    pub(super) stretched_cross_size: Option<FlexCrossSize>,
    pub(super) is_main_axis: bool,
    pub(super) overflow: css::Overflow,
    pub(super) percentage_basis: FlexAvailablePercentageBasis,
    pub(super) stretch: FlexStretchFitContext,
}

/// Why a flex item's main-axis minimum needs the content-based floor.
///
/// `min-content` in an item's logical block axis is automatic sizing under
/// CSS Sizing. It therefore selects the same content-based suggestion as an
/// automatic flex minimum, but remains an authored intrinsic constraint: the
/// scroll-container exception applies only to an actual `auto` minimum.
/// Keeping those origins distinct prevents a scrollable `min-height:
/// min-content` item from being silently converted to zero.
/// <https://www.w3.org/TR/css-sizing-3/#valdef-width-min-content> and
/// <https://www.w3.org/TR/css-flexbox-1/#min-size-auto>.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum FlexContentBasedMinimumKind {
    CssAutomatic,
    BlockAxisMinContent,
}

/// Select a main-axis minimum that needs Flexbox's content-based suggestion.
///
/// This is intentionally defined at the physical-main/logical-block boundary:
/// a vertical writing-mode item can have a horizontal block axis, so the
/// property name alone is not enough to classify `min-content`.
pub(super) fn flex_main_axis_content_based_minimum_kind(
    value: &css::ComputedLengthPercentageOrAuto,
    style: &ComputedStyle,
    physical_direction: FlexDirection,
) -> Option<FlexContentBasedMinimumKind> {
    if value.is_auto() || value.calc_size_with_auto_basis().is_some() {
        return Some(FlexContentBasedMinimumKind::CssAutomatic);
    }
    if matches!(value, css::ComputedLengthPercentageOrAuto::MinContent)
        && flex_main_axis_is_item_block_axis(style, physical_direction)
    {
        return Some(FlexContentBasedMinimumKind::BlockAxisMinContent);
    }
    None
}

/// Whether the container's physical flex main axis is the item's logical
/// block axis.
fn flex_main_axis_is_item_block_axis(
    style: &ComputedStyle,
    physical_direction: FlexDirection,
) -> bool {
    match WritingModeAxes::new(style.writing_mode, style.used_direction())
        .physical_axis(LogicalAxis::Block)
    {
        PhysicalAxis::Horizontal => physical_direction.is_row_axis(),
        PhysicalAxis::Vertical => physical_direction.is_column_axis(),
    }
}

/// Returns the overflow value for the flex item's main axis.
///
/// CSS Flexbox resolves automatic minimum sizes on the flex main axis, and CSS
/// Overflow exposes independent inline/block overflow controls through
/// `overflow-x` and `overflow-y`:
/// <https://www.w3.org/TR/css-flexbox-1/#min-size-auto> and
/// <https://www.w3.org/TR/css-overflow-3/#overflow-properties>.
pub(super) fn flex_item_main_axis_overflow(
    style: &ComputedStyle,
    direction: FlexDirection,
) -> css::Overflow {
    if direction.is_row_axis() {
        style.overflow_x
    } else {
        style.overflow_y
    }
}

/// Computes the Taffy `flex-basis` dimension from CSS flex and main-size values.
///
/// CSS Flexbox defines `flex-basis:auto` as retrieving the main-size property
/// and falling back to content sizing. Percentages resolve against the flex
/// container's inner main size, and if that size is indefinite the used value
/// is content:
/// <https://www.w3.org/TR/css-flexbox-1/#flex-basis-property>.
pub(super) fn resolve_taffy_flex_basis(
    style: &ComputedStyle,
    estimate: &FlexItemEstimate,
    context: FlexBasisContext,
) -> ResolvedFlexBasis {
    match &style.flex_basis {
        css::ComputedFlexBasis::LengthPercentage(length) => {
            let main_size_basis = context.main_size_basis.points();
            if length.contains_percentage() && main_size_basis.is_none() {
                return ResolvedFlexBasis::normal_flow_content(taffy_layout::Dimension::length(
                    flex_auto_content_basis(
                        style,
                        if context.direction.is_row_axis() {
                            estimate.content_width
                        } else {
                            estimate.content_height
                        },
                        context.direction,
                    )
                    .points(),
                ));
            }
            return ResolvedFlexBasis::definite_flex_basis(taffy_optional_dimension_with_basis(
                css::ComputedLengthPercentageOrAuto::LengthPercentage(length.value.clone()),
                context.main_size_basis,
            ));
        }
        css::ComputedFlexBasis::Content | css::ComputedFlexBasis::MaxContent => {
            if let Some(size) = context.ratio_only_replaced_base_size {
                return ResolvedFlexBasis::normal_flow_content(taffy_layout::Dimension::length(
                    flex_auto_content_basis(
                        style,
                        size.main_content_size(context.direction),
                        context.direction,
                    )
                    .points(),
                ));
            }
            let fallback = if context.direction.is_row_axis() {
                estimate.content_width
            } else {
                estimate.content_height
            };
            return aspect_ratio_transferred_flex_basis(
                style,
                estimate,
                context.direction,
                context.available_cross_size,
                context.stretched_cross_size,
                context.preferred_aspect_ratio,
            )
            .map(|basis| {
                ResolvedFlexBasis::aspect_ratio_transfer(taffy_layout::Dimension::length(
                    basis.points(),
                ))
            })
            .unwrap_or_else(|| {
                ResolvedFlexBasis::normal_flow_content(taffy_layout::Dimension::length(
                    flex_auto_content_basis(style, fallback, context.direction).points(),
                ))
            });
        }
        css::ComputedFlexBasis::MinContent => {
            return ResolvedFlexBasis::normal_flow_content(taffy_layout::Dimension::length(
                flex_auto_content_basis(
                    style,
                    if context.direction.is_row_axis() {
                        estimate.min_width
                    } else {
                        estimate.min_height
                    },
                    context.direction,
                )
                .points(),
            ));
        }
        css::ComputedFlexBasis::FitContent(limit) => {
            let min_content = if context.direction.is_row_axis() {
                estimate.min_width
            } else {
                estimate.min_height
            };
            let max_content = if context.direction.is_row_axis() {
                estimate.content_width
            } else {
                estimate.content_height
            };
            let limit = limit
                .clone()
                .and_then(|limit| {
                    if limit.is_definitely_absolute() {
                        Some(content_box_pt(limit.length_max_zero().points()))
                    } else {
                        context.main_size_basis.points().and_then(|basis| {
                            limit
                                .used_length_with_percentage_basis(PercentageBasis::definite(
                                    layout_pt(basis),
                                ))
                                .map(|length| content_box_pt(length.points()))
                        })
                    }
                })
                .unwrap_or_else(|| flex_main_content_box_length(context.available_main_size));
            return ResolvedFlexBasis::normal_flow_content(taffy_layout::Dimension::length(
                flex_auto_content_basis(
                    style,
                    fit_content_basis(min_content, max_content, limit),
                    context.direction,
                )
                .points(),
            ));
        }
        css::ComputedFlexBasis::Auto => {}
    }

    // CSS Flexbox 7.2.3: `flex-basis:auto` retrieves the main-size property,
    // and if that is also auto the used flex basis is `content`. CSS Flexbox
    // 9.2 transfers a preferred aspect ratio through a definite cross size
    // before falling back to content sizing.
    if context.direction.is_row_axis() {
        if !style.box_values.width.is_auto() {
            let dimension = taffy_flex_basis_from_main_size(
                style,
                style.box_values.width.clone(),
                estimate,
                context.main_size_basis,
                FlexDirection::Row,
            );
            if main_size_property_needs_content_fallback(
                &style.box_values.width,
                context.main_size_basis,
            ) {
                ResolvedFlexBasis::normal_flow_content(dimension)
            } else {
                ResolvedFlexBasis::main_size_property(dimension)
            }
        } else if let Some(size) = context.ratio_only_replaced_base_size {
            ResolvedFlexBasis::normal_flow_content(taffy_layout::Dimension::length(
                flex_auto_content_basis(
                    style,
                    size.main_content_size(FlexDirection::Row),
                    FlexDirection::Row,
                )
                .points(),
            ))
        } else if let Some(transferred) = aspect_ratio_transferred_flex_basis(
            style,
            estimate,
            context.direction,
            context.available_cross_size,
            context.stretched_cross_size,
            context.preferred_aspect_ratio,
        ) {
            ResolvedFlexBasis::aspect_ratio_transfer(taffy_layout::Dimension::length(
                transferred.points(),
            ))
        } else {
            ResolvedFlexBasis::normal_flow_content(taffy_layout::Dimension::length(
                flex_auto_content_basis(style, estimate.content_width, FlexDirection::Row).points(),
            ))
        }
    } else if !style.box_values.height.is_auto() {
        let dimension = taffy_flex_basis_from_main_size(
            style,
            style.box_values.height.value().clone(),
            estimate,
            context.main_size_basis,
            FlexDirection::Column,
        );
        if main_size_property_needs_content_fallback(
            style.box_values.height.value(),
            context.main_size_basis,
        ) {
            ResolvedFlexBasis::normal_flow_content(dimension)
        } else {
            ResolvedFlexBasis::main_size_property(dimension)
        }
    } else if let Some(size) = context.ratio_only_replaced_base_size {
        ResolvedFlexBasis::normal_flow_content(taffy_layout::Dimension::length(
            flex_auto_content_basis(
                style,
                size.main_content_size(FlexDirection::Column),
                FlexDirection::Column,
            )
            .points(),
        ))
    } else if let Some(transferred) = aspect_ratio_transferred_flex_basis(
        style,
        estimate,
        context.direction,
        context.available_cross_size,
        context.stretched_cross_size,
        context.preferred_aspect_ratio,
    ) {
        ResolvedFlexBasis::aspect_ratio_transfer(taffy_layout::Dimension::length(
            transferred.points(),
        ))
    } else {
        ResolvedFlexBasis::normal_flow_content(taffy_layout::Dimension::length(
            flex_auto_content_basis(style, estimate.content_height, FlexDirection::Column).points(),
        ))
    }
}

fn main_size_property_needs_content_fallback(
    main_size: &css::ComputedLengthPercentageOrAuto,
    main_size_basis: FlexAvailablePercentageBasis,
) -> bool {
    matches!(main_size, css::ComputedLengthPercentageOrAuto::LengthPercentage(value)
        if value.needs_percentage_basis() && !main_size_basis.is_definite())
}

#[derive(Debug, Clone, Copy)]
pub(super) struct FlexBasisContext {
    pub(super) direction: FlexDirection,
    pub(super) available_main_size: FlexMainSize,
    pub(super) available_cross_size: Option<FlexCrossSize>,
    pub(super) stretched_cross_size: Option<FlexCrossSize>,
    pub(super) main_size_basis: FlexAvailablePercentageBasis,
    pub(super) preferred_aspect_ratio: Option<f32>,
    pub(super) ratio_only_replaced_base_size: Option<RatioOnlyReplacedFlexBaseSize>,
}

/// The CSS rule that supplied a resolved flex basis.
///
/// This remains separate from Taffy's scalar basis through final flex
/// geometry. A later normal-flow measurement may replace only a genuinely
/// content-derived provisional main span; neither an aspect-ratio transfer nor
/// a retrieved main-size property may be mistaken for that case.
/// <https://www.w3.org/TR/css-flexbox-1/#flex-basis-property>
/// <https://www.w3.org/TR/css-flexbox-1/#algo-main-item>
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum FlexMainSizeProvenance {
    NormalFlowContent,
    AspectRatioTransfer,
    MainSizeProperty,
    DefiniteFlexBasis,
}

impl FlexMainSizeProvenance {
    pub(super) fn permits_final_normal_flow_block_span(self) -> bool {
        matches!(self, Self::NormalFlowContent)
    }

    /// Whether Flexbox 9.8 treats the resulting post-flexing main size as
    /// definite for descendant percentage sizing.
    /// <https://www.w3.org/TR/css-flexbox-1/#definite-sizes>
    pub(super) fn is_definite(self) -> bool {
        matches!(
            self,
            Self::AspectRatioTransfer | Self::MainSizeProperty | Self::DefiniteFlexBasis
        )
    }
}

/// A Taffy basis and the CSS sizing rule that produced it.
#[derive(Debug, Clone, Copy)]
pub(super) struct ResolvedFlexBasis {
    pub(super) dimension: taffy_layout::Dimension,
    pub(super) provenance: FlexMainSizeProvenance,
}

impl ResolvedFlexBasis {
    fn normal_flow_content(dimension: taffy_layout::Dimension) -> Self {
        Self {
            dimension,
            provenance: FlexMainSizeProvenance::NormalFlowContent,
        }
    }

    fn aspect_ratio_transfer(dimension: taffy_layout::Dimension) -> Self {
        Self {
            dimension,
            provenance: FlexMainSizeProvenance::AspectRatioTransfer,
        }
    }

    fn main_size_property(dimension: taffy_layout::Dimension) -> Self {
        Self {
            dimension,
            provenance: FlexMainSizeProvenance::MainSizeProperty,
        }
    }

    fn definite_flex_basis(dimension: taffy_layout::Dimension) -> Self {
        Self {
            dimension,
            provenance: FlexMainSizeProvenance::DefiniteFlexBasis,
        }
    }

    /// Reports definiteness independently of the scalar Taffy dimension.
    ///
    /// A basis transferred through a preferred aspect ratio is definite when
    /// its determining cross size is definite, just like an authored definite
    /// basis or a definite main-size property:
    /// <https://www.w3.org/TR/css-flexbox-1/#definite-sizes>.
    pub(super) fn is_definite(self) -> bool {
        self.provenance.is_definite()
    }
}

/// Computes the flex base-size transfer from a definite cross size.
///
/// Flexbox section 9.2 lets a flex item with a preferred aspect ratio use a
/// definite cross size to resolve its flex base size before falling back to
/// content sizing:
/// <https://www.w3.org/TR/css-flexbox-1/#algo-main-item> and
/// <https://www.w3.org/TR/css-sizing-4/#aspect-ratio>.
fn aspect_ratio_transferred_flex_basis(
    style: &ComputedStyle,
    estimate: &FlexItemEstimate,
    direction: FlexDirection,
    available_cross_size: Option<FlexCrossSize>,
    stretched_cross_size: Option<FlexCrossSize>,
    preferred_aspect_ratio: Option<f32>,
) -> Option<LayoutLength> {
    let transferred = aspect_ratio_transferred_content_main_size_with_cross_constraints(
        style,
        direction,
        preferred_aspect_ratio,
        FlexCrossSizeSuggestionContext {
            available_cross_size,
            authored_stretch_fit_cross_size: None,
            stretched_cross_size,
            automatic_preferred_cross_size: None,
            intrinsic: FlexCrossIntrinsicContributions {
                min_content: if direction.is_row_axis() {
                    estimate.min_height
                } else {
                    estimate.min_width
                },
                max_content: if direction.is_row_axis() {
                    estimate.content_height
                } else {
                    estimate.content_width
                },
            },
        },
    )
    .map(|size| {
        estimate
            .aspect_ratio_sizing
            .map(|sizing| {
                if direction.is_row_axis() {
                    sizing.constraints.constrain_width(size)
                } else {
                    sizing.constraints.constrain_height(size)
                }
            })
            .unwrap_or(size)
    });
    let transferred = transferred.or_else(|| {
        let sizing = estimate.aspect_ratio_sizing?;
        if !style.box_values.width.is_auto() || !style.box_values.height.is_auto() {
            return None;
        }
        // Flexbox 9.2 Part E lays an auto/auto ratio item into its
        // fit-content cross size while ignoring min/max constraints in the
        // main axis. Apply only the authored cross-axis constraints here;
        // using the fully transferred result would reflect `min-height` into
        // a column item's fit-content width and inflate its flex base.
        // <https://www.w3.org/TR/css-flexbox-1/#algo-main-item>
        Some(if direction.is_row_axis() {
            let cross = sizing
                .authored_height_constraints
                .constrain(sizing.intrinsic_height);
            sizing.ratio.width_from_height(cross)
        } else {
            let cross = sizing
                .authored_width_constraints
                .constrain(sizing.intrinsic_width);
            sizing.ratio.height_from_width(cross)
        })
    })?;
    Some(flex_aspect_ratio_basis_from_content_box(
        style,
        transferred,
        direction,
    ))
}

/// Intrinsic cross-axis contributions used while resolving a transferred
/// aspect-ratio size.
#[derive(Debug, Clone, Copy)]
pub(super) struct FlexCrossIntrinsicContributions {
    pub(super) min_content: ContentBoxLength,
    pub(super) max_content: ContentBoxLength,
}

/// Cross-axis inputs that can establish a Flexbox ratio transfer.
///
/// These suggestions are one conceptual unit: they all describe the item's
/// used cross-size contribution before it transfers through the preferred
/// aspect ratio. Keeping them together prevents a caller from accidentally
/// pairing intrinsic contributions with an unrelated percentage basis.
#[derive(Debug, Clone, Copy)]
pub(super) struct FlexCrossSizeSuggestionContext {
    pub(super) available_cross_size: Option<FlexCrossSize>,
    pub(super) authored_stretch_fit_cross_size: Option<ContentBoxLength>,
    pub(super) stretched_cross_size: Option<FlexCrossSize>,
    pub(super) automatic_preferred_cross_size: Option<ContentBoxLength>,
    pub(super) intrinsic: FlexCrossIntrinsicContributions,
}

/// Computes a transferred main size after cross-axis min/max constraints.
///
/// Flexbox uses the item's used definite cross size for aspect-ratio transfer,
/// so min/max constraints apply before the transfer. Intrinsic constraint
/// keywords use the already-measured cross-axis contributions:
/// <https://www.w3.org/TR/css-flexbox-1/#algo-main-item> and
/// <https://www.w3.org/TR/css-flexbox-1/#min-size-auto>.
pub(super) fn aspect_ratio_transferred_content_main_size_with_cross_constraints(
    style: &ComputedStyle,
    direction: FlexDirection,
    preferred_aspect_ratio: Option<f32>,
    cross: FlexCrossSizeSuggestionContext,
) -> Option<ContentBoxLength> {
    let ratio = preferred_aspect_ratio?;
    let cross_non_content = if direction.is_row_axis() {
        non_content_pt(style.padding.top + style.padding.bottom + vertical_border_width(style))
    } else {
        non_content_pt(style.padding.left + style.padding.right + horizontal_border_width(style))
    };
    let specified_cross_size = if direction.is_row_axis() {
        used_content_box_height_or_auto_with_basis(
            style,
            percentage_basis_from_points(cross.available_cross_size.map(FlexCrossSize::points)),
            cross_non_content,
        )
    } else {
        used_content_box_width_or_auto_with_basis(
            style,
            percentage_basis_from_points(cross.available_cross_size.map(FlexCrossSize::points)),
            cross_non_content,
        )
    };
    let cross_content_size = specified_cross_size
        .or(cross.authored_stretch_fit_cross_size)
        .or_else(|| {
            cross
                .stretched_cross_size
                .map(|size| content_box_pt((size.points() - cross_non_content.points()).max(0.0)))
        })
        .or(cross.automatic_preferred_cross_size)?;

    // The definite cross size used for aspect-ratio transfer is a used size:
    // it is constrained by the item's cross-axis min/max properties before it
    // becomes a flex base size.  Keep the conversion in content-box space so
    // `box-sizing` is applied consistently to the preferred and constraint
    // sizes. CSS Flexbox 9.2 resolves the resulting cross size before deriving
    // the flex base size through the preferred aspect ratio:
    // <https://www.w3.org/TR/css-flexbox-1/#algo-main-item>.
    let cross_percentage_basis =
        percentage_basis_from_points(cross.available_cross_size.map(FlexCrossSize::points));
    let constrained_cross = if direction.is_row_axis() {
        constrain_height_with_intrinsic(
            style,
            cross_content_size,
            cross.intrinsic.min_content,
            cross.intrinsic.max_content,
            cross_percentage_basis,
            non_content_pt(style.padding.top + style.padding.bottom + vertical_border_width(style)),
        )
    } else {
        constrain_width_with_intrinsic(
            style,
            cross_content_size,
            cross.intrinsic.min_content,
            cross.intrinsic.max_content,
            cross_percentage_basis,
            non_content_pt(
                style.padding.left + style.padding.right + horizontal_border_width(style),
            ),
        )
    };
    Some(flex_aspect_ratio_transferred_content_main_size(
        style,
        constrained_cross,
        direction,
        ratio,
    ))
}

/// Computes the transferred suggestion used by Flexbox's automatic minimum.
///
/// A definite preferred cross size supplies the normal transferred
/// suggestion. When that size is automatic, the automatic-minimum algorithm
/// still applies a definite cross-axis minimum before transferring the
/// preferred ratio. This is distinct from flex-base sizing: it must not make
/// an automatic cross size definite for the main flex basis.
/// <https://www.w3.org/TR/css-flexbox-1/#min-size-auto> and
/// <https://www.w3.org/TR/css-sizing-4/#aspect-ratio>.
pub(super) fn automatic_minimum_transferred_size_suggestion(
    style: &ComputedStyle,
    direction: FlexDirection,
    preferred_aspect_ratio: Option<f32>,
    cross: FlexCrossSizeSuggestionContext,
) -> Option<ContentBoxLength> {
    if let Some(transferred) = aspect_ratio_transferred_content_main_size_with_cross_constraints(
        style,
        direction,
        preferred_aspect_ratio,
        cross,
    ) {
        return Some(transferred);
    }

    let ratio = preferred_aspect_ratio?;
    let percentage_basis =
        percentage_basis_from_points(cross.available_cross_size.map(FlexCrossSize::points));
    let constrained_minimum_cross = if direction.is_row_axis() {
        constrain_height_with_intrinsic(
            style,
            content_box_pt(0.0),
            cross.intrinsic.min_content,
            cross.intrinsic.max_content,
            percentage_basis,
            non_content_pt(style.padding.top + style.padding.bottom + vertical_border_width(style)),
        )
    } else {
        constrain_width_with_intrinsic(
            style,
            content_box_pt(0.0),
            cross.intrinsic.min_content,
            cross.intrinsic.max_content,
            percentage_basis,
            non_content_pt(
                style.padding.left + style.padding.right + horizontal_border_width(style),
            ),
        )
    };
    (constrained_minimum_cross.points() > 0.0).then(|| {
        flex_aspect_ratio_transferred_content_main_size(
            style,
            constrained_minimum_cross,
            direction,
            ratio,
        )
    })
}

/// Resolves an authored cross-axis `stretch` preferred size for Flexbox's
/// transferred-size suggestion.
///
/// This is intentionally separate from self-alignment stretch: the latter is
/// established after flex-line sizing, whereas CSS Sizing's `stretch` value
/// is itself a definite preferred cross size whenever its stretch-fit slot is
/// definite.
/// <https://drafts.csswg.org/css-sizing-4/#stretch-fit-sizing> and
/// <https://drafts.csswg.org/css-flexbox-1/#min-size-auto>.
fn authored_cross_stretch_fit_content_box_size(
    style: &ComputedStyle,
    direction: FlexDirection,
    stretch: FlexStretchFitContext,
) -> Option<ContentBoxLength> {
    let cross_size = if direction.is_row_axis() {
        &style.box_values.height
    } else {
        &style.box_values.width
    };
    matches!(cross_size, css::ComputedLengthPercentageOrAuto::Stretch)
        .then(|| resolved_stretch_fit_content_box_size(stretch))
        .flatten()
}

/// Return a replaced item's CSS automatic preferred size on Flexbox's
/// physical cross axis when the corresponding CSS size property is `auto`.
///
/// The estimate records CSS Images' default object size separately from an
/// authored preferred size and from flex stretch. It is definite only for the
/// transferred-size suggestion; it must not turn `auto` into a general flex
/// basis override.
/// <https://www.w3.org/TR/css-images-3/#default-sizing>
/// <https://www.w3.org/TR/css-flexbox-1/#transferred-size-suggestion>
pub(super) fn automatic_preferred_cross_content_size(
    style: &ComputedStyle,
    estimate: &FlexItemEstimate,
    direction: FlexDirection,
) -> Option<ContentBoxLength> {
    automatic_preferred_content_size_on_axis(
        style,
        estimate,
        if direction.is_row_axis() {
            FlexDirection::Column
        } else {
            FlexDirection::Row
        },
    )
}

/// Select the stable inputs to one flex pass's automatic main-axis minimum.
///
/// The dynamic cross slot (available or stretched) is intentionally supplied
/// by each consumer later, but content, CSS Images fallback, and authored
/// preferred-size roles are selected exactly once here. This prevents a
/// post-layout correction from reclassifying an automatic preferred object
/// size as intrinsic content.
/// <https://www.w3.org/TR/css-flexbox-1/#min-size-auto>
pub(super) fn flex_automatic_minimum_inputs(
    style: &ComputedStyle,
    estimate: &FlexItemEstimate,
    direction: FlexDirection,
    content_size_source: FlexAutomaticMinimumContentSizeSource,
    preferred_aspect_ratio: Option<f32>,
    is_replaced: bool,
    available: FlexAvailableSpace,
) -> FlexAutomaticMinimumInputs {
    let (max_content_size, automatic_preferred_cross_size, cross_intrinsic) =
        if direction.is_row_axis() {
            (
                estimate.content_width,
                automatic_preferred_cross_content_size(style, estimate, FlexDirection::Row),
                FlexAutomaticMinimumCrossIntrinsicContributions {
                    min_content: estimate.min_height,
                    max_content: estimate.content_height,
                },
            )
        } else {
            (
                estimate.content_height,
                automatic_preferred_cross_content_size(style, estimate, FlexDirection::Column),
                FlexAutomaticMinimumCrossIntrinsicContributions {
                    min_content: estimate.min_width,
                    max_content: estimate.content_width,
                },
            )
        };
    let definite_preferred_content_size = if style.display.is_table() {
        None
    } else if direction.is_row_axis() {
        used_content_box_width_or_auto_with_basis(
            style,
            available.width_basis,
            non_content_pt(
                style.padding.left + style.padding.right + horizontal_border_width(style),
            ),
        )
    } else {
        used_content_box_height_or_auto_with_basis(
            style,
            available.height_basis,
            non_content_pt(style.padding.top + style.padding.bottom + vertical_border_width(style)),
        )
    };
    FlexAutomaticMinimumInputs {
        content_size_source,
        max_content_size,
        automatic_preferred_cross_size: automatic_preferred_cross_size.map_or(
            FlexAutomaticMinimumAutomaticPreferredCrossSize::None,
            FlexAutomaticMinimumAutomaticPreferredCrossSize::CssImagesDefaultObjectSize,
        ),
        cross_intrinsic,
        preferred_aspect_ratio,
        aspect_ratio_sizing: estimate.aspect_ratio_sizing,
        is_replaced,
        definite_preferred_content_size,
    }
}

/// Store the one pass-scoped automatic-main-minimum input record used by all
/// Flexbox consumers.
///
/// Intrinsic contribution sizing, Taffy's flexible-length pass, and Spindrift's
/// final-layout safeguard must select the same content-size source and
/// preferred aspect ratio. The content probe is supplied separately because
/// it suppresses the preferred main size while retaining every other style.
/// <https://www.w3.org/TR/css-flexbox-1/#min-size-auto>.
pub(super) fn set_flex_item_automatic_main_minimum_inputs(
    estimate: &mut FlexItemEstimate,
    style: &ComputedStyle,
    direction: FlexDirection,
    automatic_main_min_content: Option<ContentBoxLength>,
    preferred_aspect_ratio: Option<f32>,
    is_replaced: bool,
    available: FlexAvailableSpace,
) {
    let content_size_source = if is_replaced && estimate.automatic_preferred_physical_size.is_some()
    {
        // CSS Images' default object size remains a transferred-size source.
        // A viewBox-only replaced item has no intrinsic axis, so it must not
        // also become a min-content contribution.
        FlexAutomaticMinimumContentSizeSource::RatioOnlyReplaced
    } else {
        FlexAutomaticMinimumContentSizeSource::Intrinsic(automatic_main_min_content.unwrap_or_else(
            || {
                if direction.is_row_axis() {
                    estimate.min_width
                } else {
                    estimate.min_height
                }
            },
        ))
    };
    estimate.set_automatic_main_minimum_inputs(flex_automatic_minimum_inputs(
        style,
        estimate,
        direction,
        content_size_source,
        preferred_aspect_ratio,
        is_replaced,
        available,
    ));
}

/// Classify the computed CSS size property selected by the container's
/// physical cross axis. CSS logical properties have already been projected
/// into these physical width/height values by the cascade.
pub(super) fn flex_item_cross_size_property(
    style: &ComputedStyle,
    direction: FlexDirection,
) -> FlexCrossSizeProperty {
    let is_auto = if direction.is_row_axis() {
        style.box_values.height.is_auto()
    } else {
        style.box_values.width.is_auto()
    };
    if is_auto {
        FlexCrossSizeProperty::Auto
    } else {
        FlexCrossSizeProperty::NonAuto
    }
}

/// Whether this item may use automatic cross-size reconciliation in Spindrift's
/// current replaced-element adapter. The CSS-property classification remains
/// separate: an automatic preferred replaced size does not make its computed
/// cross-size property non-auto.
pub(super) fn flex_item_cross_size_is_auto(
    style: &ComputedStyle,
    direction: FlexDirection,
) -> bool {
    flex_item_cross_size_property(style, direction).is_auto()
}

/// Resolve the temporary physical size used for a ratio-only replaced item's
/// content-derived flex base size.
///
/// CSS Images retains the default object size as an automatic preferred size,
/// but Flexbox first sizes a content-based item into its available space. For
/// a viewBox-only SVG this means capping its logical inline size by the
/// margin-, border-, and padding-adjusted inline space and deriving the other
/// physical axis through the preferred aspect ratio.
/// <https://www.w3.org/TR/css-flexbox-1/#algo-main-item>
/// <https://www.w3.org/TR/css-images-3/#default-sizing>
pub(super) fn ratio_only_replaced_flex_base_size(
    style: &ComputedStyle,
    estimate: &FlexItemEstimate,
    available: FlexItemAvailableSpace,
    margin: css::Edges,
    padding: css::Edges,
    borders: css::Edges,
    preferred_aspect_ratio: Option<f32>,
) -> Option<RatioOnlyReplacedFlexBaseSize> {
    if !style.box_values.width.is_auto() || !style.box_values.height.is_auto() {
        return None;
    }
    let automatic = estimate.automatic_preferred_physical_size?;
    let ratio = preferred_aspect_ratio.filter(|ratio| ratio.is_finite() && *ratio > 0.0)?;
    let axes = WritingModeAxes::new(style.writing_mode, style.used_direction());
    let (available_inline, default_inline, inline_margin, inline_non_content) =
        if axes.swaps_physical_axes() {
            (
                available
                    .height
                    .map(PhysicalContentHeight::content_box_length)?,
                automatic.height.content_box_length(),
                margin.top + margin.bottom,
                padding.top + padding.bottom + borders.top + borders.bottom,
            )
        } else {
            (
                available.width.content_box_length(),
                automatic.width.content_box_length(),
                margin.left + margin.right,
                padding.left + padding.right + borders.left + borders.right,
            )
        };
    let inline = default_inline.min(content_box_pt(
        (available_inline.points() - inline_margin - inline_non_content).max(0.0),
    ));
    let (width, height) = if axes.swaps_physical_axes() {
        (inline.points() * ratio, inline.points())
    } else {
        (inline.points(), inline.points() / ratio)
    };
    let constrained = resolve_replaced_size_with_aspect_ratio(
        content_box_size_pt(width, height),
        ratio,
        ReplacedPreferredSizeAxes {
            width: ReplacedPreferredSize::Automatic,
            height: ReplacedPreferredSize::Automatic,
        },
        ReplacedSizeConstraints {
            min_width: used_min_width(style, available.width_basis)
                .map(|width| width.max(content_box_pt(0.0))),
            max_width: used_max_width(style, available.width_basis)
                .map(|width| width.max(content_box_pt(0.0))),
            min_height: used_length_percentage_or_auto_with_basis(
                style.box_values.min_height.clone(),
                available.height_basis,
            )
            .map(|height| content_box_pt(height.points().max(0.0))),
            max_height: used_length_percentage_or_auto_with_basis(
                style.box_values.max_height.clone(),
                available.height_basis,
            )
            .map(|height| content_box_pt(height.points().max(0.0))),
        },
    );
    Some(RatioOnlyReplacedFlexBaseSize::new(
        PhysicalContentWidth::new(content_box_pt(constrained.width)),
        PhysicalContentHeight::new(content_box_pt(constrained.height)),
    ))
}

/// Return a replaced item's CSS automatic preferred size on one physical axis.
pub(super) fn automatic_preferred_content_size_on_axis(
    style: &ComputedStyle,
    estimate: &FlexItemEstimate,
    axis: FlexDirection,
) -> Option<ContentBoxLength> {
    let size = estimate.automatic_preferred_physical_size?;
    if axis.is_row_axis() {
        style
            .box_values
            .width
            .is_auto()
            .then(|| size.width.content_box_length())
    } else {
        style
            .box_values
            .height
            .is_auto()
            .then(|| size.height.content_box_length())
    }
}

fn taffy_flex_basis_from_main_size(
    style: &ComputedStyle,
    value: css::ComputedLengthPercentageOrAuto,
    estimate: &FlexItemEstimate,
    main_size_basis: FlexAvailablePercentageBasis,
    direction: FlexDirection,
) -> taffy_layout::Dimension {
    let (min_content, max_content) = if direction.is_row_axis() {
        (estimate.min_width, estimate.content_width)
    } else {
        (estimate.min_height, estimate.content_height)
    };
    if matches!(value, css::ComputedLengthPercentageOrAuto::LengthPercentage(ref value) if value.needs_percentage_basis() && !main_size_basis.is_definite())
    {
        return taffy_layout::Dimension::length(
            flex_auto_content_basis(style, max_content, direction).points(),
        );
    }

    taffy_intrinsic_dimension_with_basis(value, main_size_basis, min_content, max_content)
}

/// Computes the intrinsic `fit-content` size clamp for `flex-basis`.
///
/// CSS Sizing defines fit-content as
/// `min(max-content, max(min-content, stretch-or-argument))`; Flexbox accepts
/// that width grammar for `flex-basis`:
/// <https://www.w3.org/TR/css-sizing-3/#fit-content-size> and
/// <https://www.w3.org/TR/css-flexbox-1/#flex-basis-property>.
fn fit_content_basis(
    min_content: ContentBoxLength,
    max_content: ContentBoxLength,
    limit: ContentBoxLength,
) -> ContentBoxLength {
    content_box_pt(
        max_content
            .points()
            .max(0.0)
            .min(min_content.points().max(0.0).max(limit.points().max(0.0))),
    )
}

/// Transfer a flex item's cross-axis content size through its preferred ratio.
///
/// The result remains in content-box space for the flex sizing algorithm. A
/// bare ratio on a border-box item instead operates on border-box dimensions,
/// so convert into and then out of that coordinate space at this boundary.
/// `auto <ratio>` always remains content-box based.
/// <https://drafts.csswg.org/css-sizing-4/#aspect-ratio> and
/// <https://www.w3.org/TR/css-flexbox-1/#algo-main-item>.
pub(in crate::layout::flex) fn flex_aspect_ratio_transferred_content_main_size(
    style: &ComputedStyle,
    cross_content_size: ContentBoxLength,
    direction: FlexDirection,
    ratio: f32,
) -> ContentBoxLength {
    let border_widths = used_border_widths(style);
    let horizontal_non_content = non_content_pt(
        style.padding.left + style.padding.right + border_widths.left + border_widths.right,
    );
    let vertical_non_content = non_content_pt(
        style.padding.top + style.padding.bottom + border_widths.top + border_widths.bottom,
    );
    let calculation_box = if style.aspect_ratio.uses_content_box_for_non_replaced()
        || style.box_sizing == BoxSizing::ContentBox
    {
        AspectRatioCalculationBox::ContentBox
    } else {
        AspectRatioCalculationBox::BorderBox
    };
    let resolved = ResolvedAspectRatio::new(
        ratio,
        calculation_box,
        horizontal_non_content,
        vertical_non_content,
    )
    .expect("preferred aspect ratios are positive and finite");
    if direction.is_row_axis() {
        resolved.width_from_height(cross_content_size)
    } else {
        resolved.height_from_width(cross_content_size)
    }
}

fn main_axis_extras(style: &ComputedStyle, direction: FlexDirection) -> NonContentLength {
    let border_widths = used_border_widths(style);
    non_content_pt(if direction.is_row_axis() {
        style.padding.left + style.padding.right + border_widths.left + border_widths.right
    } else {
        style.padding.top + style.padding.bottom + border_widths.top + border_widths.bottom
    })
}

/// Computes the content-derived basis used when `flex-basis:auto` has no main size.
///
/// CSS Flexbox defines content-based flex basis resolution for `auto`:
/// <https://www.w3.org/TR/css-flexbox-1/#flex-basis-property>.
pub(super) fn flex_auto_content_basis(
    style: &ComputedStyle,
    length: ContentBoxLength,
    direction: FlexDirection,
) -> LayoutLength {
    flex_auto_content_basis_from_content_box(style, length, direction)
}

fn flex_auto_content_basis_from_content_box(
    style: &ComputedStyle,
    length: ContentBoxLength,
    direction: FlexDirection,
) -> LayoutLength {
    // The intrinsic estimate is already the item's content-box contribution.
    // In particular, white-space processing belongs to text measurement, not
    // flex-basis conversion: adding a line-height or rounding here changes a
    // `flex-basis:auto` item even when its measured max-content width is
    // definite.
    let length = content_box_pt(length.points().max(0.0));
    if style.box_sizing == BoxSizing::BorderBox {
        crate::units::IntoLayoutLength::into_layout_length(content_box_to_border_box_length(
            length,
            main_axis_extras(style, direction),
        ))
    } else {
        crate::units::IntoLayoutLength::into_layout_length(length)
    }
}

/// Convert an aspect-ratio-derived content size into a flex main-size basis.
///
/// A bare `aspect-ratio` works in the box selected by `box-sizing`, but
/// `auto <ratio>` always works in the content box. Taffy applies the item's
/// `box-sizing` when resolving its flex basis, so retain a content-box basis
/// for `content-box` items and convert only `border-box` items.
/// <https://drafts.csswg.org/css-sizing-4/#aspect-ratio>
fn flex_aspect_ratio_basis_from_content_box(
    style: &ComputedStyle,
    length: ContentBoxLength,
    direction: FlexDirection,
) -> LayoutLength {
    let length = content_box_pt(length.points().max(0.0));
    if style.box_sizing == BoxSizing::BorderBox {
        crate::units::IntoLayoutLength::into_layout_length(content_box_to_border_box_length(
            length,
            main_axis_extras(style, direction),
        ))
    } else {
        crate::units::IntoLayoutLength::into_layout_length(length)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::layout::flex::{
        FlexAspectRatioSizing, FlexAutomaticPreferredPhysicalSize,
        flex_available_percentage_basis_from_points,
    };
    use crate::layout::{AspectRatioAxisConstraints, ResolvedAspectRatioConstraints, WritingMode};

    #[test]
    fn automatic_content_basis_preserves_measured_width_with_preserved_whitespace() {
        let mut style = ComputedStyle::initial();
        style.white_space = css::WhiteSpace::Pre;

        assert_eq!(
            flex_auto_content_basis(&style, content_box_pt(18.65625), FlexDirection::Row).points(),
            18.65625
        );
    }

    #[test]
    fn block_axis_min_content_preserves_its_authored_minimum_kind() {
        let horizontal = ComputedStyle::initial();
        assert_eq!(
            flex_main_axis_content_based_minimum_kind(
                &css::ComputedLengthPercentageOrAuto::MinContent,
                &horizontal,
                FlexDirection::Column,
            ),
            Some(FlexContentBasedMinimumKind::BlockAxisMinContent),
        );
        assert_eq!(
            flex_main_axis_content_based_minimum_kind(
                &css::ComputedLengthPercentageOrAuto::MinContent,
                &horizontal,
                FlexDirection::Row,
            ),
            None,
        );
        assert_eq!(
            flex_main_axis_content_based_minimum_kind(
                &css::ComputedLengthPercentageOrAuto::Auto,
                &horizontal,
                FlexDirection::Column,
            ),
            Some(FlexContentBasedMinimumKind::CssAutomatic),
        );
    }

    #[test]
    fn flex_margin_adapter_preserves_negative_lengths_and_percentages() {
        let mut style = ComputedStyle::initial();
        style.box_values.margin.left = css::ComputedLengthPercentageOrAuto::LengthPercentage(
            css::ComputedLengthPercentage::from_points(-50.0),
        );
        let length = taffy_bridge::margin(
            &style,
            PercentageBasis::<LogicalInlineContentSize>::indefinite(),
            taffy_bridge::TaffyCyclicPercentage::PreservePurePercentage,
        )
        .left;
        assert_eq!(length.resolve_to_option(200.0, |_, _| 0.0), Some(-50.0));

        style.box_values.margin.left = css::ComputedLengthPercentageOrAuto::LengthPercentage(
            css::ComputedLengthPercentage::from_percent(-0.25),
        );
        let percentage = taffy_bridge::margin(
            &style,
            PercentageBasis::<LogicalInlineContentSize>::indefinite(),
            taffy_bridge::TaffyCyclicPercentage::PreservePurePercentage,
        )
        .left;
        assert_eq!(percentage.resolve_to_option(200.0, |_, _| 0.0), Some(-50.0));

        style.box_values.margin.left = css::ComputedLengthPercentageOrAuto::Auto;
        let auto = taffy_bridge::margin(
            &style,
            PercentageBasis::<LogicalInlineContentSize>::indefinite(),
            taffy_bridge::TaffyCyclicPercentage::PreservePurePercentage,
        )
        .left;
        assert!(auto.is_auto());
    }

    #[test]
    fn flex_size_adapter_still_clamps_negative_lengths_and_percentages() {
        assert_eq!(
            taffy_dimension_from_length_percentage(css::ComputedLengthPercentage::from_points(
                -50.0
            )),
            taffy_layout::Dimension::length(0.0)
        );
        assert_eq!(
            taffy_dimension_from_length_percentage(css::ComputedLengthPercentage::from_percent(
                -0.25
            )),
            taffy_layout::Dimension::percent(0.0)
        );
    }

    #[test]
    fn flex_optional_dimension_policies_preserve_or_resolve_pure_percentages() {
        let percent = css::ComputedLengthPercentageOrAuto::LengthPercentage(
            css::ComputedLengthPercentage::from_percent(0.5),
        );
        let definite_basis = flex_available_percentage_basis_from_points(
            Some(80.0),
            FlexAvailableSizeSource::ContainingBlock,
        );
        let indefinite_basis = PercentageBasis::indefinite();

        assert_eq!(
            taffy_optional_dimension(percent.clone()),
            taffy_layout::LengthPercentageAuto::percent(0.5),
        );
        assert_eq!(
            taffy_optional_dimension_with_basis(percent.clone(), definite_basis),
            taffy_layout::Dimension::length(40.0),
        );
        assert!(taffy_optional_dimension_with_basis(percent, indefinite_basis).is_auto());
    }

    #[test]
    fn flex_optional_dimension_policies_share_fixed_lengths() {
        let fixed = css::ComputedLengthPercentageOrAuto::LengthPercentage(
            css::ComputedLengthPercentage::from_points(12.0),
        );
        let indefinite_basis = PercentageBasis::indefinite();

        assert_eq!(
            taffy_optional_dimension(fixed.clone()),
            taffy_layout::LengthPercentageAuto::length(12.0),
        );
        assert_eq!(
            taffy_optional_dimension_with_basis(fixed, indefinite_basis),
            taffy_layout::Dimension::length(12.0),
        );
    }

    #[test]
    fn flex_size_adapter_does_not_treat_unresolved_metric_math_as_a_fixed_length() {
        let value = css::ComputedLengthPercentageOrAuto::LengthPercentage(
            css::ComputedLengthPercentage::sum(
                css::ComputedLengthPercentage::from_points(12.0),
                css::ComputedLengthPercentage::from_em(1.0),
            ),
        );

        assert!(
            taffy_optional_dimension_with_basis(value, PercentageBasis::indefinite()).is_auto()
        );
    }

    #[test]
    fn unresolved_percentage_main_size_remains_auto_for_content_flex_basis() {
        let value = css::ComputedLengthPercentageOrAuto::LengthPercentage(
            css::ComputedLengthPercentage::from_percent(0.01),
        );
        let context = FlexItemSizeDimensionContext {
            flex_direction: FlexDirection::Column,
            dimension_axis: FlexDirection::Column,
            percentage_basis: PercentageBasis::indefinite(),
            stretch: FlexStretchFitContext {
                available_margin_box_size: None,
                margin_size: layout_pt(0.0),
                non_content_size: non_content_pt(0.0),
                box_sizing: BoxSizing::ContentBox,
            },
            flex_basis_overrides_main_size: false,
            cross_sizing_phase: FlexCrossSizingPhase::Hypothetical,
            hypothetical_automatic_cross_size: FlexHypotheticalAutomaticCrossSize::Intrinsic,
        };

        assert!(
            flex_item_size_dimension(
                value,
                content_box_pt(10.0),
                content_box_pt(10.0),
                content_box_pt(20.0),
                context,
            )
            .is_auto()
        );
    }

    #[test]
    fn cross_size_property_and_adapter_phase_keep_orthogonal_auto_distinct_from_inline_size() {
        let mut style = ComputedStyle::initial();
        // `inline-size` on a vertical item is projected by cascade to its
        // physical height. In a horizontal row that is the Flex cross-size
        // property and must therefore be non-auto; physical width remains
        // automatic for a column container's cross axis.
        style.writing_mode = WritingMode::VerticalRl;
        *style.box_values.height = css::ComputedLengthPercentageOrAuto::LengthPercentage(
            css::ComputedLengthPercentage::from_points(6.0),
        );
        assert_eq!(
            flex_item_cross_size_property(&style, FlexDirection::Row),
            FlexCrossSizeProperty::NonAuto
        );
        assert_eq!(
            flex_item_cross_size_property(&style, FlexDirection::Column),
            FlexCrossSizeProperty::Auto
        );

        let context = FlexItemSizeDimensionContext {
            flex_direction: FlexDirection::Row,
            dimension_axis: FlexDirection::Column,
            percentage_basis: PercentageBasis::indefinite(),
            stretch: FlexStretchFitContext {
                available_margin_box_size: None,
                margin_size: layout_pt(0.0),
                non_content_size: non_content_pt(0.0),
                box_sizing: BoxSizing::ContentBox,
            },
            flex_basis_overrides_main_size: false,
            cross_sizing_phase: FlexCrossSizingPhase::Hypothetical,
            hypothetical_automatic_cross_size: FlexHypotheticalAutomaticCrossSize::Intrinsic,
        };
        assert!(
            flex_item_size_dimension(
                css::ComputedLengthPercentageOrAuto::Auto,
                content_box_pt(10.0),
                content_box_pt(10.0),
                content_box_pt(20.0),
                context,
            )
            .is_auto()
        );

        let fit_content = FlexItemSizeDimensionContext {
            hypothetical_automatic_cross_size: FlexHypotheticalAutomaticCrossSize::FitContent {
                used_content_size: content_box_pt(36.0),
            },
            ..context
        };
        assert_eq!(
            flex_item_size_dimension(
                css::ComputedLengthPercentageOrAuto::Auto,
                content_box_pt(10.0),
                content_box_pt(10.0),
                content_box_pt(20.0),
                fit_content,
            ),
            taffy_layout::Dimension::length(36.0)
        );

        let stretched = FlexItemSizeDimensionContext {
            cross_sizing_phase: FlexCrossSizingPhase::StretchToLine {
                line_outer_cross_size: FlexCrossSize::new(44.0),
            },
            ..context
        };
        assert_eq!(
            flex_item_size_dimension(
                css::ComputedLengthPercentageOrAuto::Auto,
                content_box_pt(10.0),
                content_box_pt(10.0),
                content_box_pt(20.0),
                stretched,
            ),
            taffy_layout::Dimension::length(44.0)
        );
    }

    #[test]
    fn flex_container_min_dimension_uses_the_supplied_physical_basis() {
        let fifty_percent = css::ComputedLengthPercentageOrAuto::LengthPercentage(
            css::ComputedLengthPercentage::from_percent(0.5),
        );
        let width_basis = flex_available_percentage_basis_from_points(
            Some(200.0),
            FlexAvailableSizeSource::ContainingBlock,
        );
        let height_basis = flex_available_percentage_basis_from_points(
            Some(80.0),
            FlexAvailableSizeSource::ContainingBlock,
        );

        assert_eq!(
            taffy_min_dimension(fifty_percent.clone(), width_basis),
            taffy_layout::LengthPercentageAuto::length(100.0)
        );
        assert_eq!(
            taffy_min_dimension(fifty_percent, height_basis),
            taffy_layout::LengthPercentageAuto::length(40.0)
        );
    }

    #[test]
    fn flex_gap_resolves_percentages_only_with_definite_basis() {
        let percent_gap =
            css::ComputedGap::LengthPercentage(css::ComputedLengthPercentage::from_percent(0.5));
        let mixed = css::ComputedLengthPercentage::from_affine(layout_pt(4.0), 0.5, true);
        let mixed_gap = css::ComputedGap::LengthPercentage(mixed);
        let indefinite_basis = PercentageBasis::indefinite();
        let definite_basis = flex_available_percentage_basis_from_points(
            Some(40.0),
            FlexAvailableSizeSource::ContainingBlock,
        );

        assert_eq!(
            taffy_gap(percent_gap.clone(), indefinite_basis),
            taffy_layout::LengthPercentage::length(0.0)
        );
        assert_eq!(
            taffy_gap(percent_gap, definite_basis),
            taffy_layout::LengthPercentage::length(20.0)
        );
        assert_eq!(
            taffy_gap(mixed_gap.clone(), indefinite_basis),
            taffy_layout::LengthPercentage::length(4.0)
        );
        assert_eq!(
            taffy_gap(mixed_gap, definite_basis),
            taffy_layout::LengthPercentage::length(24.0)
        );
    }

    fn test_flex_basis_context() -> FlexBasisContext {
        FlexBasisContext {
            direction: FlexDirection::Row,
            available_main_size: FlexMainSize::new(200.0),
            available_cross_size: None,
            stretched_cross_size: None,
            main_size_basis: PercentageBasis::definite_from(
                content_box_pt(200.0),
                FlexAvailableSizeSource::ContainingBlock,
            ),
            preferred_aspect_ratio: None,
            ratio_only_replaced_base_size: None,
        }
    }

    fn test_flex_estimate() -> FlexItemEstimate {
        FlexItemEstimate::new(
            IntrinsicItemMetrics {
                width: content_box_pt(60.0),
                height: content_box_pt(30.0),
                min_width: content_box_pt(20.0),
                min_height: content_box_pt(10.0),
                content_width: content_box_pt(80.0),
                content_height: content_box_pt(40.0),
                preferred_aspect_ratio: None,
                first_baseline: None,
                last_baseline: None,
            },
            FlexItemBaselineEstimate::default(),
        )
    }

    fn ratio_only_replaced_estimate(width: f32, height: f32) -> FlexItemEstimate {
        let mut estimate = test_flex_estimate();
        estimate.set_automatic_preferred_physical_size(FlexAutomaticPreferredPhysicalSize {
            width: PhysicalContentWidth::new(content_box_pt(width)),
            height: PhysicalContentHeight::new(content_box_pt(height)),
        });
        estimate
    }

    fn ratio_only_available_space(width: f32, height: Option<f32>) -> FlexItemAvailableSpace {
        FlexItemAvailableSpace {
            width: PhysicalContentWidth::new(content_box_pt(width)),
            width_basis: PercentageBasis::definite_from(
                content_box_pt(width),
                FlexAvailableSizeSource::ContainingBlock,
            ),
            height: height.map(|height| PhysicalContentHeight::new(content_box_pt(height))),
            height_basis: height.map_or_else(PercentageBasis::indefinite, |height| {
                PercentageBasis::definite_from(
                    content_box_pt(height),
                    FlexAvailableSizeSource::ContainingBlock,
                )
            }),
            stretched_width: None,
            stretched_height: None,
        }
    }

    #[test]
    fn ratio_only_replaced_flex_base_uses_margin_adjusted_inline_space() {
        let style = ComputedStyle::initial();
        let estimate = ratio_only_replaced_estimate(225.0, 225.0);
        let base = ratio_only_replaced_flex_base_size(
            &style,
            &estimate,
            ratio_only_available_space(112.5, None),
            css::Edges {
                right: 37.5,
                ..css::Edges::ZERO
            },
            css::Edges::ZERO,
            css::Edges::ZERO,
            Some(1.0),
        )
        .expect("a ratio-only replaced item has a temporary flex base size");

        assert_eq!(base.main_content_size(FlexDirection::Row).points(), 75.0);
        assert_eq!(base.main_content_size(FlexDirection::Column).points(), 75.0);
        assert_eq!(base.cross_content_size(FlexDirection::Row).points(), 75.0);
        assert!(flex_item_cross_size_is_auto(&style, FlexDirection::Column));
        assert_eq!(
            automatic_preferred_cross_content_size(&style, &estimate, FlexDirection::Column)
                .expect("the CSS Images fallback remains available to automatic minimum sizing")
                .points(),
            225.0,
        );

        let resolved = resolve_taffy_flex_basis(
            &style,
            &estimate,
            FlexBasisContext {
                ratio_only_replaced_base_size: Some(base),
                ..test_flex_basis_context()
            },
        );
        assert_eq!(resolved.dimension, taffy_layout::Dimension::length(75.0));
        assert_eq!(
            resolved.provenance,
            FlexMainSizeProvenance::NormalFlowContent
        );

        let column = resolve_taffy_flex_basis(
            &style,
            &estimate,
            FlexBasisContext {
                direction: FlexDirection::Column,
                ratio_only_replaced_base_size: Some(base),
                ..test_flex_basis_context()
            },
        );
        assert_eq!(column.dimension, taffy_layout::Dimension::length(75.0));
    }

    #[test]
    fn ratio_only_replaced_flex_base_projects_vertical_inline_space() {
        let mut style = ComputedStyle::initial();
        style.writing_mode = WritingMode::VerticalRl;
        let estimate = ratio_only_replaced_estimate(225.0, 112.5);
        let base = ratio_only_replaced_flex_base_size(
            &style,
            &estimate,
            ratio_only_available_space(400.0, Some(80.0)),
            css::Edges {
                top: 20.0,
                ..css::Edges::ZERO
            },
            css::Edges::ZERO,
            css::Edges::ZERO,
            Some(2.0),
        )
        .expect("a vertical ratio-only replaced item has a temporary flex base size");

        assert_eq!(base.main_content_size(FlexDirection::Row).points(), 120.0);
        assert_eq!(base.main_content_size(FlexDirection::Column).points(), 60.0);
        assert_eq!(base.cross_content_size(FlexDirection::Row).points(), 60.0);
    }

    #[test]
    fn flex_item_measurement_extracts_typed_content_box_lengths() {
        let mut estimate = test_flex_estimate();

        let measured = measure_flex_item(
            taffy_layout::Size {
                width: None,
                height: None,
            },
            taffy_layout::Size {
                width: taffy_layout::AvailableSpace::Definite(200.0),
                height: taffy_layout::AvailableSpace::Definite(200.0),
            },
            Some(&mut estimate),
        );

        assert_eq!(measured.width, 60.0);
        assert_eq!(measured.height, 30.0);
    }

    #[test]
    fn flex_basis_uses_typed_content_and_min_content_estimates() {
        let estimate = test_flex_estimate();
        let mut style = ComputedStyle::initial();

        style.flex_basis = css::ComputedFlexBasis::Content;
        assert_eq!(
            resolve_taffy_flex_basis(&style, &estimate, test_flex_basis_context()).dimension,
            taffy_layout::Dimension::length(80.0)
        );

        style.flex_basis = css::ComputedFlexBasis::MinContent;
        assert_eq!(
            resolve_taffy_flex_basis(&style, &estimate, test_flex_basis_context()).dimension,
            taffy_layout::Dimension::length(20.0)
        );

        style.flex_basis = css::ComputedFlexBasis::MaxContent;
        assert_eq!(
            resolve_taffy_flex_basis(&style, &estimate, test_flex_basis_context()).dimension,
            taffy_layout::Dimension::length(80.0)
        );
    }

    #[test]
    fn flex_basis_indefinite_percentage_falls_back_to_typed_content_estimate() {
        let estimate = test_flex_estimate();
        let mut style = ComputedStyle::initial();
        style.flex_basis = css::ComputedFlexBasis::LengthPercentage(
            css::ComputedFlexBasisLength::new(css::ComputedLengthPercentage::from_percent(0.5)),
        );
        let context = FlexBasisContext {
            main_size_basis: PercentageBasis::indefinite(),
            ..test_flex_basis_context()
        };

        let resolved = resolve_taffy_flex_basis(&style, &estimate, context);
        assert_eq!(resolved.dimension, taffy_layout::Dimension::length(80.0));
        assert_eq!(
            resolved.provenance,
            FlexMainSizeProvenance::NormalFlowContent,
            "an indefinite percentage flex basis, including flex: 1's 0%, uses content sizing",
        );
    }

    #[test]
    fn resolved_flex_basis_retains_the_main_size_provenance() {
        let estimate = test_flex_estimate();
        let mut content = ComputedStyle::initial();
        content.flex_basis = css::ComputedFlexBasis::Content;
        assert_eq!(
            resolve_taffy_flex_basis(&content, &estimate, test_flex_basis_context()).provenance,
            FlexMainSizeProvenance::NormalFlowContent,
        );

        let mut main_size_property = ComputedStyle::initial();
        main_size_property.box_values.width = css::ComputedLengthPercentageOrAuto::LengthPercentage(
            css::ComputedLengthPercentage::from_points(60.0),
        );
        assert_eq!(
            resolve_taffy_flex_basis(&main_size_property, &estimate, test_flex_basis_context(),)
                .provenance,
            FlexMainSizeProvenance::MainSizeProperty,
        );

        let mut definite_basis = ComputedStyle::initial();
        definite_basis.flex_basis = css::ComputedFlexBasis::LengthPercentage(
            css::ComputedFlexBasisLength::new(css::ComputedLengthPercentage::from_points(60.0)),
        );
        assert_eq!(
            resolve_taffy_flex_basis(&definite_basis, &estimate, test_flex_basis_context())
                .provenance,
            FlexMainSizeProvenance::DefiniteFlexBasis,
        );

        let mut transferred = ComputedStyle::initial();
        transferred.flex_basis = css::ComputedFlexBasis::Content;
        transferred.box_values.width = css::ComputedLengthPercentageOrAuto::LengthPercentage(
            css::ComputedLengthPercentage::from_points(80.0),
        );
        let context = FlexBasisContext {
            direction: FlexDirection::Column,
            available_cross_size: Some(FlexCrossSize::new(80.0)),
            preferred_aspect_ratio: Some(1.0),
            ..test_flex_basis_context()
        };
        assert_eq!(
            resolve_taffy_flex_basis(&transferred, &estimate, context).provenance,
            FlexMainSizeProvenance::AspectRatioTransfer,
        );
        assert!(resolve_taffy_flex_basis(&transferred, &estimate, context).is_definite());
    }

    #[test]
    fn auto_auto_ratio_column_basis_uses_fit_content_cross_and_ignores_main_minimum() {
        let mut style = ComputedStyle::initial();
        style.aspect_ratio = css::AspectRatio::from_ratio(1.0).unwrap();
        let ratio = ResolvedAspectRatio::new(
            1.0,
            AspectRatioCalculationBox::ContentBox,
            non_content_pt(0.0),
            non_content_pt(0.0),
        )
        .unwrap();
        let authored_width_constraints = AspectRatioAxisConstraints {
            minimum: Some(content_box_pt(100.0)),
            ..Default::default()
        };
        let authored_height_constraints = AspectRatioAxisConstraints {
            minimum: Some(content_box_pt(200.0)),
            ..Default::default()
        };
        let mut estimate = FlexItemEstimate::fixed(
            PhysicalContentWidth::new(content_box_pt(100.0)),
            PhysicalContentHeight::new(content_box_pt(100.0)),
        );
        estimate.set_aspect_ratio_sizing(FlexAspectRatioSizing {
            ratio,
            authored_width_constraints,
            authored_height_constraints,
            constraints: ResolvedAspectRatioConstraints::resolve(
                ratio,
                authored_width_constraints,
                authored_height_constraints,
            ),
            intrinsic_width: content_box_pt(1.0),
            intrinsic_height: content_box_pt(0.0),
        });
        let context = FlexBasisContext {
            direction: FlexDirection::Column,
            preferred_aspect_ratio: Some(1.0),
            ..test_flex_basis_context()
        };

        let resolved = resolve_taffy_flex_basis(&style, &estimate, context);

        assert_eq!(resolved.dimension, taffy_layout::Dimension::length(100.0));
        assert_eq!(
            resolved.provenance,
            FlexMainSizeProvenance::AspectRatioTransfer
        );
    }

    #[test]
    fn automatic_minimum_transfers_a_definite_cross_minimum_through_ratio() {
        let mut style = ComputedStyle::initial();
        style.box_values.min_height = css::ComputedLengthPercentageOrAuto::LengthPercentage(
            css::ComputedLengthPercentage::from_points(30.0),
        );

        let transferred = automatic_minimum_transferred_size_suggestion(
            &style,
            FlexDirection::Row,
            Some(1.0),
            FlexCrossSizeSuggestionContext {
                available_cross_size: None,
                authored_stretch_fit_cross_size: None,
                stretched_cross_size: None,
                automatic_preferred_cross_size: None,
                intrinsic: FlexCrossIntrinsicContributions {
                    min_content: content_box_pt(10.0),
                    max_content: content_box_pt(100.0),
                },
            },
        )
        .expect("a definite cross minimum supplies a transferred suggestion");

        assert_eq!(transferred.points(), 30.0);
    }

    #[test]
    fn automatic_preferred_replaced_cross_size_transfers_through_max_constraints() {
        let mut column_style = ComputedStyle::initial();
        column_style.box_values.max_width = css::ComputedLengthPercentageOrAuto::LengthPercentage(
            css::ComputedLengthPercentage::from_points(100.0),
        );
        let column_transferred = automatic_minimum_transferred_size_suggestion(
            &column_style,
            FlexDirection::Column,
            Some(1.0),
            FlexCrossSizeSuggestionContext {
                available_cross_size: None,
                authored_stretch_fit_cross_size: None,
                stretched_cross_size: None,
                automatic_preferred_cross_size: Some(content_box_pt(225.0)),
                intrinsic: FlexCrossIntrinsicContributions {
                    min_content: content_box_pt(0.0),
                    max_content: content_box_pt(225.0),
                },
            },
        )
        .expect("automatic width transfers through max-width");
        assert_eq!(column_transferred.points(), 100.0);

        let mut row_style = ComputedStyle::initial();
        row_style.box_values.max_height = css::ComputedLengthPercentageOrAuto::LengthPercentage(
            css::ComputedLengthPercentage::from_points(100.0),
        );
        let row_transferred = automatic_minimum_transferred_size_suggestion(
            &row_style,
            FlexDirection::Row,
            Some(1.0),
            FlexCrossSizeSuggestionContext {
                available_cross_size: None,
                authored_stretch_fit_cross_size: None,
                stretched_cross_size: None,
                automatic_preferred_cross_size: Some(content_box_pt(225.0)),
                intrinsic: FlexCrossIntrinsicContributions {
                    min_content: content_box_pt(0.0),
                    max_content: content_box_pt(225.0),
                },
            },
        )
        .expect("automatic height transfers through max-height");
        assert_eq!(row_transferred.points(), 100.0);
    }

    #[test]
    fn ratio_only_automatic_minimum_keeps_css_images_fallback_transferred_only() {
        let style = ComputedStyle::initial();
        let inputs = FlexAutomaticMinimumInputs {
            content_size_source: FlexAutomaticMinimumContentSizeSource::RatioOnlyReplaced,
            max_content_size: content_box_pt(225.0),
            automatic_preferred_cross_size:
                FlexAutomaticMinimumAutomaticPreferredCrossSize::CssImagesDefaultObjectSize(
                    content_box_pt(225.0),
                ),
            cross_intrinsic: FlexAutomaticMinimumCrossIntrinsicContributions {
                min_content: content_box_pt(0.0),
                max_content: content_box_pt(225.0),
            },
            preferred_aspect_ratio: Some(1.0),
            aspect_ratio_sizing: None,
            is_replaced: true,
            definite_preferred_content_size: None,
        };
        let resolved = resolve_automatic_flex_minimum(
            css::ComputedLengthPercentageOrAuto::Auto,
            FlexMinSizeDimensionContext {
                style: &style,
                direction: FlexDirection::Row,
                automatic_minimum_inputs: Some(inputs),
                available_cross_size: None,
                cross_stretch: FlexStretchFitContext {
                    available_margin_box_size: None,
                    margin_size: layout_pt(0.0),
                    non_content_size: non_content_pt(0.0),
                    box_sizing: BoxSizing::ContentBox,
                },
                stretched_cross_size: None,
                is_main_axis: true,
                overflow: flex_item_main_axis_overflow(&style, FlexDirection::Row),
                percentage_basis: PercentageBasis::indefinite(),
                stretch: FlexStretchFitContext {
                    available_margin_box_size: None,
                    margin_size: layout_pt(0.0),
                    non_content_size: non_content_pt(0.0),
                    box_sizing: BoxSizing::ContentBox,
                },
            },
        )
        .expect("a main-axis automatic minimum resolves");

        assert_eq!(resolved.content_size_suggestion.points(), 0.0);
        assert_eq!(
            resolved
                .transferred_size_suggestion
                .expect("the CSS Images fallback transfers through the ratio")
                .points(),
            225.0
        );
        assert_eq!(resolved.used_content_box.points(), 0.0);
    }

    #[test]
    fn intrinsic_replaced_automatic_minimum_retains_its_content_suggestion() {
        let style = ComputedStyle::initial();
        let inputs = FlexAutomaticMinimumInputs {
            content_size_source: FlexAutomaticMinimumContentSizeSource::Intrinsic(content_box_pt(
                50.0,
            )),
            max_content_size: content_box_pt(100.0),
            automatic_preferred_cross_size: FlexAutomaticMinimumAutomaticPreferredCrossSize::None,
            cross_intrinsic: FlexAutomaticMinimumCrossIntrinsicContributions {
                min_content: content_box_pt(0.0),
                max_content: content_box_pt(0.0),
            },
            preferred_aspect_ratio: None,
            aspect_ratio_sizing: None,
            is_replaced: true,
            definite_preferred_content_size: None,
        };
        let resolved = resolve_automatic_flex_minimum(
            css::ComputedLengthPercentageOrAuto::Auto,
            FlexMinSizeDimensionContext {
                style: &style,
                direction: FlexDirection::Row,
                automatic_minimum_inputs: Some(inputs),
                available_cross_size: None,
                cross_stretch: FlexStretchFitContext {
                    available_margin_box_size: None,
                    margin_size: layout_pt(0.0),
                    non_content_size: non_content_pt(0.0),
                    box_sizing: BoxSizing::ContentBox,
                },
                stretched_cross_size: None,
                is_main_axis: true,
                overflow: flex_item_main_axis_overflow(&style, FlexDirection::Row),
                percentage_basis: PercentageBasis::indefinite(),
                stretch: FlexStretchFitContext {
                    available_margin_box_size: None,
                    margin_size: layout_pt(0.0),
                    non_content_size: non_content_pt(0.0),
                    box_sizing: BoxSizing::ContentBox,
                },
            },
        )
        .expect("a main-axis automatic minimum resolves");

        assert_eq!(resolved.used_content_box.points(), 50.0);
    }

    #[test]
    fn scrollable_block_axis_min_content_does_not_take_auto_minimum_zero() {
        let mut style = ComputedStyle::initial();
        style.overflow_y = css::Overflow::Auto;
        let inputs = FlexAutomaticMinimumInputs {
            content_size_source: FlexAutomaticMinimumContentSizeSource::Intrinsic(content_box_pt(
                50.0,
            )),
            max_content_size: content_box_pt(100.0),
            automatic_preferred_cross_size: FlexAutomaticMinimumAutomaticPreferredCrossSize::None,
            cross_intrinsic: FlexAutomaticMinimumCrossIntrinsicContributions {
                min_content: content_box_pt(0.0),
                max_content: content_box_pt(0.0),
            },
            preferred_aspect_ratio: None,
            aspect_ratio_sizing: None,
            is_replaced: true,
            definite_preferred_content_size: None,
        };
        let context = FlexMinSizeDimensionContext {
            style: &style,
            direction: FlexDirection::Column,
            automatic_minimum_inputs: Some(inputs),
            available_cross_size: None,
            cross_stretch: FlexStretchFitContext {
                available_margin_box_size: None,
                margin_size: layout_pt(0.0),
                non_content_size: non_content_pt(0.0),
                box_sizing: BoxSizing::ContentBox,
            },
            stretched_cross_size: None,
            is_main_axis: true,
            overflow: flex_item_main_axis_overflow(&style, FlexDirection::Column),
            percentage_basis: PercentageBasis::indefinite(),
            stretch: FlexStretchFitContext {
                available_margin_box_size: None,
                margin_size: layout_pt(0.0),
                non_content_size: non_content_pt(0.0),
                box_sizing: BoxSizing::ContentBox,
            },
        };

        let auto =
            resolve_automatic_flex_minimum(css::ComputedLengthPercentageOrAuto::Auto, context)
                .expect("a scrollable automatic minimum resolves");
        let min_content = resolve_automatic_flex_minimum(
            css::ComputedLengthPercentageOrAuto::MinContent,
            context,
        )
        .expect("a block-axis min-content minimum resolves");

        assert_eq!(auto.used_content_box.points(), 0.0);
        assert_eq!(min_content.used_content_box.points(), 50.0);
    }

    #[test]
    fn content_box_aspect_ratio_transfer_keeps_content_box_flex_basis() {
        let mut style = ComputedStyle::initial();
        style.box_sizing = BoxSizing::ContentBox;
        style.box_values.width = css::ComputedLengthPercentageOrAuto::LengthPercentage(
            css::ComputedLengthPercentage::from_points(150.0),
        );
        // Taffy applies content-box sizing itself, so these edges do not
        // belong in the adapter's flex-basis value.
        style.padding.top = 75.0;
        style.padding.bottom = 75.0;

        let basis = aspect_ratio_transferred_flex_basis(
            &style,
            &test_flex_estimate(),
            FlexDirection::Column,
            Some(FlexCrossSize::new(300.0)),
            None,
            Some(1.0),
        )
        .expect("definite cross size should transfer through aspect ratio");

        assert_eq!(basis.points(), 150.0);
    }

    #[test]
    fn border_box_aspect_ratio_transfer_adds_extras_once() {
        let mut style = ComputedStyle::initial();
        style.box_sizing = BoxSizing::BorderBox;
        style.box_values.width = css::ComputedLengthPercentageOrAuto::LengthPercentage(
            css::ComputedLengthPercentage::from_points(300.0),
        );
        style.padding.left = 75.0;
        style.padding.right = 75.0;
        style.padding.top = 75.0;
        style.padding.bottom = 75.0;

        let basis = aspect_ratio_transferred_flex_basis(
            &style,
            &test_flex_estimate(),
            FlexDirection::Column,
            Some(FlexCrossSize::new(300.0)),
            None,
            Some(1.0),
        )
        .expect("definite cross size should transfer through aspect ratio");

        assert_eq!(basis.points(), 300.0);
    }

    #[test]
    fn aspect_ratio_flex_basis_uses_max_constrained_cross_size() {
        let mut style = ComputedStyle::initial();
        style.box_values.width = css::ComputedLengthPercentageOrAuto::LengthPercentage(
            css::ComputedLengthPercentage::from_points(500.0),
        );
        style.box_values.max_width = css::ComputedLengthPercentageOrAuto::LengthPercentage(
            css::ComputedLengthPercentage::from_percent(1.0),
        );
        let estimate = FlexItemEstimate::fixed(
            PhysicalContentWidth::new(content_box_pt(100.0)),
            PhysicalContentHeight::new(content_box_pt(100.0)),
        );

        let basis = aspect_ratio_transferred_flex_basis(
            &style,
            &estimate,
            FlexDirection::Column,
            Some(FlexCrossSize::new(100.0)),
            None,
            Some(1.0),
        )
        .expect("definite max-constrained cross size should transfer through aspect ratio");

        assert_eq!(basis.points(), 100.0);
    }
}