1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
use super::baseline::{
GridBaselinePlan, GridBaselineSet, apply_grid_baseline_alignment, grid_baseline_plan,
grid_baseline_sizing_may_need_shims, grid_container_baseline,
grid_taffy_margin_with_baseline_shim, resolve_grid_baseline_participation,
};
use super::item_adjustment::{
GridFinalItemPercentagePlacement, apply_grid_aspect_ratio_item_size_corrections,
apply_grid_deferred_percentage_item_size_corrections, apply_grid_self_alignment_corrections,
};
use super::lanes::GridLanesLayoutContext;
use super::model::{
GridItemArea, GridItemLayout, GridLayout, GridLayoutPurpose, GridTaffyLeaf,
apply_resolved_subgrid_axis_item_geometry,
};
use super::resolved::physical_grid_line_names;
use super::*;
use crate::layout::baseline::{BaselinePair, PhysicalBaselineSets, PhysicalTopBaselineOffset};
impl<'a> LayoutBuilder<'a> {
/// Compute same-page grid item geometry with Spindrift-measured leaf estimates.
///
/// CSS Grid track sizing consumes each item's min-content, max-content, and
/// preferred size contributions. Taffy owns the Grid Level 1 placement and
/// track-sizing algorithm here, while Spindrift supplies leaf measurements from
/// the same inline, block, flex, table, and replaced-element paths used by
/// other layout modes:
/// <https://www.w3.org/TR/css-grid-1/#algo-overview> and
/// <https://www.w3.org/TR/css-sizing-3/#intrinsic>.
///
/// `width` is the grid container's physical CSS content-box width.
pub(super) fn compute_grid_layout(
&mut self,
style: &ComputedStyle,
children: &[GridChild<'_>],
stylesheets: &Stylesheets<'_>,
width: PhysicalContentWidth,
height: Option<PhysicalContentHeight>,
purpose: GridLayoutPurpose,
) -> Option<GridLayout> {
#[cfg(feature = "layout-profile")]
let _layout_profile = crate::layout::layout_profile::grid_layout_scope(
match purpose {
GridLayoutPurpose::FinalLayout | GridLayoutPurpose::FloatBlockSizeMeasurement => {
crate::layout::layout_profile::GridProfilePurpose::Final
}
GridLayoutPurpose::IntrinsicProbe => {
crate::layout::layout_profile::GridProfilePurpose::Intrinsic
}
},
children.len(),
WritingModeAxes::new(style.writing_mode, style.direction).swaps_physical_axes(),
);
// A direct subgrid replay installs this context immediately before
// entering its formatting context. Intrinsic probes may run first,
// but they must only borrow that resolved topology: consuming it
// there would make the subsequent final replay fall back to an
// unresolved standalone grid. The final layout owns the one-shot
// consumption.
// <https://drafts.csswg.org/css-grid-2/#subgrids>
let subgrid_context = match purpose {
GridLayoutPurpose::IntrinsicProbe | GridLayoutPurpose::FloatBlockSizeMeasurement => {
self.resolved_subgrid_context_for_probe()
}
GridLayoutPurpose::FinalLayout => self.take_resolved_subgrid_context(),
};
self.compute_grid_layout_with_margin_trim(
style,
children,
stylesheets,
width,
height,
purpose,
subgrid_context,
true,
)
}
/// Run Grid sizing after deriving any container-owned `margin-trim` used
/// margins from a placement probe. Placement is independent of an item's
/// margins, while track sizing is not, so the probe must not become the
/// final sizing pass.
/// <https://drafts.csswg.org/css-box-4/#margin-trim-grid>.
#[allow(clippy::too_many_arguments)]
fn compute_grid_layout_with_margin_trim(
&mut self,
style: &ComputedStyle,
children: &[GridChild<'_>],
stylesheets: &Stylesheets<'_>,
width: PhysicalContentWidth,
height: Option<PhysicalContentHeight>,
purpose: GridLayoutPurpose,
subgrid_context: Option<ResolvedSubgridContext>,
derive_margin_trim: bool,
) -> Option<GridLayout> {
if derive_margin_trim && grid_has_margin_trim(style) && !children.is_empty() {
let placement_probe = self.compute_grid_layout_with_margin_trim(
style,
children,
stylesheets,
width,
height,
purpose,
subgrid_context.clone(),
false,
)?;
let plan = grid_margin_trim_plan(style, &placement_probe);
if !plan.is_empty() {
let mut trimmed_children = children.to_vec();
for (index, child) in trimmed_children.iter_mut().enumerate() {
plan.apply_to_style(index, &mut child.style);
}
return self.compute_grid_layout_with_margin_trim(
style,
&trimmed_children,
stylesheets,
width,
height,
purpose,
subgrid_context,
false,
);
}
}
let preliminary_pass = self.compute_grid_layout_pass_with_estimates(
style,
children,
stylesheets,
subgrid_context.as_ref(),
&[],
GridLayoutPassConfig {
width,
root_height: height,
item_width_basis: None,
item_height_basis: grid_percentage_basis(
height.map(PhysicalContentHeight::content_box_length),
GridAvailableSizeSource::ContainerBlockSize,
),
item_containing_block_bases: None,
frozen_tracks: GridFrozenTrackTopology::default(),
row_gap_basis: grid_percentage_basis(
height.map(PhysicalContentHeight::content_box_length),
GridAvailableSizeSource::ContainerBlockSize,
),
reported_height: None,
item_placement_overrides: Vec::new(),
baseline_plan: None,
},
)?;
// Grid baseline alignment affects intrinsic track sizes before the
// ordinary track-sizing algorithm runs. Taffy does not expose an
// item-baseline measurement channel, so first obtain its placement
// topology, derive the spec's sizing-only baseline shims from that
// topology, and run the real sizing pass with those shims installed.
// The probe's placement is independent of track sizes; only the
// resulting track contributions change in the second pass.
// <https://drafts.csswg.org/css-grid-2/#algo-track-sizing>
let GridLayoutPassResult {
layout: preliminary_layout,
estimates: preliminary_estimates,
feedback_sensitivities,
} = preliminary_pass;
let baseline_plan =
self.grid_baseline_sizing_plan(style, &preliminary_layout, &preliminary_estimates);
let preliminary_layout = if baseline_plan.is_empty() {
preliminary_layout
} else {
self.compute_grid_layout_pass(
style,
children,
stylesheets,
subgrid_context.as_ref(),
&[],
GridLayoutPassConfig {
width,
root_height: height,
item_width_basis: None,
item_height_basis: grid_percentage_basis(
height.map(PhysicalContentHeight::content_box_length),
GridAvailableSizeSource::ContainerBlockSize,
),
item_containing_block_bases: None,
frozen_tracks: GridFrozenTrackTopology::default(),
row_gap_basis: grid_percentage_basis(
height.map(PhysicalContentHeight::content_box_length),
GridAvailableSizeSource::ContainerBlockSize,
),
reported_height: None,
item_placement_overrides: Vec::new(),
baseline_plan: Some(&baseline_plan),
},
)?
};
let contributions = if purpose.uses_final_track_sizing() && subgrid_context.is_none() {
self.collect_subgrid_contributions(style, children, stylesheets, &preliminary_layout)
} else {
Vec::new()
};
// Descendant contribution proxies take part in parent track sizing,
// but they are not Grid items and must not displace automatic items
// during the proxy pass. Preserve the preliminary placement while
// resolving the shared inherited tracks.
// <https://drafts.csswg.org/css-grid-2/#subgrid-contributions>
let contribution_item_placement_overrides = (!contributions.is_empty())
.then(|| grid_item_placement_overrides(style, &preliminary_layout));
let intrinsic_layout = if contributions.is_empty() {
preliminary_layout
} else {
self.compute_grid_layout_pass(
style,
children,
stylesheets,
subgrid_context.as_ref(),
&contributions,
GridLayoutPassConfig {
width,
root_height: height,
item_width_basis: None,
item_height_basis: grid_percentage_basis(
height.map(PhysicalContentHeight::content_box_length),
GridAvailableSizeSource::ContainerBlockSize,
),
item_containing_block_bases: None,
frozen_tracks: GridFrozenTrackTopology::default(),
row_gap_basis: grid_percentage_basis(
height.map(PhysicalContentHeight::content_box_length),
GridAvailableSizeSource::ContainerBlockSize,
),
reported_height: None,
item_placement_overrides: contribution_item_placement_overrides
.clone()
.unwrap_or_default(),
baseline_plan: Some(&baseline_plan),
},
)?
};
let intrinsic_layout = if style.display.is_grid_lanes() {
intrinsic_layout
} else {
self.resolve_grid_intrinsic_feedback(
style,
children,
stylesheets,
subgrid_context.as_ref(),
&contributions,
width,
height,
&baseline_plan,
&feedback_sensitivities,
intrinsic_layout,
)?
};
if height.is_none()
&& purpose.uses_final_track_sizing()
&& grid_gap_resolves_differently_with_basis(
style.row_gap.clone(),
intrinsic_layout.height,
)
{
return self.compute_grid_layout_pass(
style,
children,
stylesheets,
subgrid_context.as_ref(),
&contributions,
GridLayoutPassConfig {
width,
root_height: Some(intrinsic_layout.height),
item_width_basis: None,
item_height_basis: PercentageBasis::indefinite(),
item_containing_block_bases: None,
frozen_tracks: GridFrozenTrackTopology::default(),
row_gap_basis: grid_percentage_basis(
Some(intrinsic_layout.height.content_box_length()),
GridAvailableSizeSource::ContainerBlockSize,
),
reported_height: Some(intrinsic_layout.height),
item_placement_overrides: contribution_item_placement_overrides
.unwrap_or_default(),
baseline_plan: Some(&baseline_plan),
},
);
}
if style.display.is_grid_lanes() {
return Some(self.apply_grid_lanes_placement(
style,
children,
stylesheets,
GridLanesLayoutContext {
width,
block_percentage_basis:
height.map_or_else(PercentageBasis::indefinite, |height| {
PercentageBasis::definite(layout_pt(height.points()))
}),
subgrid_context: subgrid_context.as_ref(),
},
intrinsic_layout,
));
}
Some(intrinsic_layout)
}
/// Complete CSS Grid's bounded cross-axis intrinsic-contribution feedback
/// sequence. Taffy's public Grid API sizes both axes together, so a
/// correction fixes the previously resolved opposite physical axis and
/// preserves the established item placement.
/// <https://drafts.csswg.org/css-grid-2/#algo-track-sizing>
#[allow(clippy::too_many_arguments)]
fn resolve_grid_intrinsic_feedback(
&mut self,
style: &ComputedStyle,
children: &[GridChild<'_>],
stylesheets: &Stylesheets<'_>,
subgrid_context: Option<&ResolvedSubgridContext>,
contributions: &[SubgridContribution],
width: PhysicalContentWidth,
height: Option<PhysicalContentHeight>,
baseline_plan: &GridBaselinePlan,
feedback_sensitivities: &[GridItemFeedbackSensitivity],
initial_layout: GridLayout,
) -> Option<GridLayout> {
let mut layout = initial_layout;
let probe_plan = GridFeedbackProbePlan::from_sensitivities(feedback_sensitivities);
let mut reusable_estimates = Vec::with_capacity(children.len());
let initial_bases = probe_plan
.needs_inline_comparison
.then(|| grid_item_containing_block_bases(style, &layout));
if let Some(initial_bases) = initial_bases.as_deref() {
#[cfg(feature = "layout-profile")]
let _profile = crate::layout::layout_profile::grid_feedback_sweep_scope(
crate::layout::layout_profile::GridFeedbackSweep::Initial,
children.len(),
);
self.fill_grid_item_estimates_with_bases(
&mut reusable_estimates,
children,
stylesheets,
width,
initial_bases,
);
#[cfg(feature = "layout-profile")]
drop(_profile);
}
// Step 3: rows are now resolved, so an item's inline contribution may
// change (notably through an aspect-ratio descendant). Re-resolve the
// columns once while keeping rows and placement stable.
let mut cyclic_estimates = Vec::with_capacity(children.len());
if probe_plan.needs_container_comparison() {
#[cfg(feature = "layout-profile")]
let _profile = crate::layout::layout_profile::grid_feedback_sweep_scope(
crate::layout::layout_profile::GridFeedbackSweep::Container,
children.len(),
);
self.fill_grid_item_estimates_with_container_bases(
&mut cyclic_estimates,
children,
stylesheets,
width,
height,
);
#[cfg(feature = "layout-profile")]
drop(_profile);
}
if probe_plan.needs_inline_comparison
&& grid_inline_contributions_changed(&cyclic_estimates, &reusable_estimates)
{
#[cfg(feature = "layout-profile")]
crate::layout::layout_profile::record_grid_feedback_inline_correction();
layout = self.compute_grid_layout_pass(
style,
children,
stylesheets,
subgrid_context,
contributions,
GridLayoutPassConfig {
width,
root_height: height,
item_width_basis: None,
item_height_basis: grid_percentage_basis(
height.map(PhysicalContentHeight::content_box_length),
GridAvailableSizeSource::ContainerBlockSize,
),
item_containing_block_bases: initial_bases,
frozen_tracks: GridFrozenTrackTopology {
columns: None,
rows: Some(GridFrozenTrackAxis::from_layout(&layout, GridAxis::Row)),
},
row_gap_basis: grid_percentage_basis(
height.map(PhysicalContentHeight::content_box_length),
GridAvailableSizeSource::ContainerBlockSize,
),
reported_height: Some(layout.height),
item_placement_overrides: grid_item_placement_overrides(style, &layout),
baseline_plan: Some(baseline_plan),
},
)?;
}
// Step 4: the adjusted columns can in turn change a block-axis
// contribution. Perform the symmetric row correction once only.
let column_bases = probe_plan
.needs_block_comparison
.then(|| grid_item_containing_block_bases(style, &layout));
if let Some(column_bases) = column_bases.as_deref() {
#[cfg(feature = "layout-profile")]
let _profile = crate::layout::layout_profile::grid_feedback_sweep_scope(
crate::layout::layout_profile::GridFeedbackSweep::Column,
children.len(),
);
self.fill_grid_item_estimates_with_bases(
&mut reusable_estimates,
children,
stylesheets,
width,
column_bases,
);
#[cfg(feature = "layout-profile")]
drop(_profile);
}
// The initial track layout measured each ordinary grid item against
// the container-wide available space. Compare that original cyclic
// contribution with the estimate constrained by the resolved column
// area; comparing two area-constrained probes would silently skip a
// required row correction for padded nested grids.
// <https://drafts.csswg.org/css-grid-2/#algo-track-sizing>
if probe_plan.needs_block_comparison
&& grid_block_contributions_changed(&cyclic_estimates, &reusable_estimates)
{
#[cfg(feature = "layout-profile")]
crate::layout::layout_profile::record_grid_feedback_block_correction();
layout = self.compute_grid_layout_pass(
style,
children,
stylesheets,
subgrid_context,
contributions,
GridLayoutPassConfig {
width,
root_height: height,
item_width_basis: None,
item_height_basis: grid_percentage_basis(
height.map(PhysicalContentHeight::content_box_length),
GridAvailableSizeSource::ContainerBlockSize,
),
item_containing_block_bases: column_bases,
frozen_tracks: GridFrozenTrackTopology {
columns: Some(GridFrozenTrackAxis::from_layout(&layout, GridAxis::Column)),
rows: None,
},
row_gap_basis: grid_percentage_basis(
height.map(PhysicalContentHeight::content_box_length),
GridAvailableSizeSource::ContainerBlockSize,
),
reported_height: Some(layout.height),
item_placement_overrides: grid_item_placement_overrides(style, &layout),
baseline_plan: Some(baseline_plan),
},
)?;
}
// Grid items are finally laid out against their definite grid areas.
// This supersedes the old fixed-row-only placement retry and covers
// both explicit and content-sized tracks without feeding final item
// percentages back into intrinsic track sizing.
let final_bases = grid_item_containing_block_bases(style, &layout);
let final_layout = self.compute_grid_layout_pass(
style,
children,
stylesheets,
subgrid_context,
contributions,
GridLayoutPassConfig {
width,
root_height: height,
item_width_basis: None,
item_height_basis: grid_percentage_basis(
height.map(PhysicalContentHeight::content_box_length),
GridAvailableSizeSource::ContainerBlockSize,
),
item_containing_block_bases: Some(final_bases),
frozen_tracks: GridFrozenTrackTopology {
columns: Some(GridFrozenTrackAxis::from_layout(&layout, GridAxis::Column)),
rows: Some(GridFrozenTrackAxis::from_layout(&layout, GridAxis::Row)),
},
row_gap_basis: grid_percentage_basis(
height.map(PhysicalContentHeight::content_box_length),
GridAvailableSizeSource::ContainerBlockSize,
),
reported_height: Some(layout.height),
item_placement_overrides: grid_item_placement_overrides(style, &layout),
baseline_plan: Some(baseline_plan),
},
)?;
Some(final_layout)
}
fn fill_grid_item_estimates_with_bases(
&mut self,
output: &mut Vec<GridItemEstimate>,
children: &[GridChild<'_>],
stylesheets: &Stylesheets<'_>,
width: PhysicalContentWidth,
bases: &[GridItemContainingBlockBases],
) {
output.clear();
output.extend(children.iter().zip(bases).map(|(child, bases)| {
self.estimate_grid_item_size_for_parent_track_sizing(
child,
stylesheets,
bases.width.points().unwrap_or(width.points()),
bases.width,
bases.height,
)
}));
}
fn fill_grid_item_estimates_with_container_bases(
&mut self,
output: &mut Vec<GridItemEstimate>,
children: &[GridChild<'_>],
stylesheets: &Stylesheets<'_>,
width: PhysicalContentWidth,
height: Option<PhysicalContentHeight>,
) {
let width_basis = grid_percentage_basis(
Some(width.content_box_length()),
GridAvailableSizeSource::ContainerInlineSize,
);
let height_basis = grid_percentage_basis(
height.map(PhysicalContentHeight::content_box_length),
GridAvailableSizeSource::ContainerBlockSize,
);
output.clear();
output.extend(children.iter().map(|child| {
self.estimate_grid_item_size_for_parent_track_sizing(
child,
stylesheets,
width.points(),
width_basis,
height_basis,
)
}));
}
/// Resolve the baseline sizing data from an already-placed Grid topology.
///
/// Grid placement does not depend on the used track sizes, so the
/// preliminary pass supplies stable row/column membership while this pass
/// supplies the measured item baselines used to construct intrinsic-size
/// shims. The shims are installed only in the following Taffy sizing
/// pass, never in the replay style.
/// <https://drafts.csswg.org/css-grid-2/#algo-track-sizing>
fn grid_baseline_sizing_plan(
&self,
style: &ComputedStyle,
topology: &GridLayout,
estimates: &[GridItemEstimate],
) -> GridBaselinePlan {
if !grid_baseline_sizing_may_need_shims(
style,
&topology.baseline_resolutions,
&topology.items,
) {
return GridBaselinePlan::default();
}
#[cfg(feature = "layout-profile")]
let _profile = crate::layout::layout_profile::grid_baseline_plan_scope(estimates.len());
grid_baseline_plan(
style,
estimates,
&topology.baseline_resolutions,
&topology.rows,
&topology.items,
)
}
/// Run a subgrid contribution probe against its placed border-box
/// geometry. The probe owns the corresponding content-box conversion so
/// descendants are measured against the same used space as final replay.
pub(super) fn compute_grid_layout_for_subgrid_contribution_probe(
&mut self,
style: &ComputedStyle,
children: &[GridChild<'_>],
stylesheets: &Stylesheets<'_>,
placed_item_dimensions: GridItemReplayDimensions,
purpose: GridLayoutPurpose,
) -> Option<GridLayout> {
// This temporary probe owns the context installed by the contribution
// collector. Unlike an intrinsic probe encountered while replaying a
// real subgrid item, it has no later formatting pass that must retain
// the context.
let subgrid_context = self.take_resolved_subgrid_context();
self.compute_grid_layout_with_margin_trim(
style,
children,
stylesheets,
placed_item_dimensions.physical_content_width_for_replay(style),
Some(placed_item_dimensions.physical_content_height_for_replay(style)),
purpose,
subgrid_context,
true,
)
}
/// Compute one Taffy grid layout pass.
///
/// CSS Box Alignment makes Grid cyclic percentage gaps resolve against zero
/// for intrinsic size contributions, but against the grid container content
/// box when laying out contents. Callers can therefore provide a definite
/// `root_height` for final content layout while keeping `item_height_basis`
/// indefinite for grid item percentage block sizes:
/// <https://www.w3.org/TR/css-align-3/#gap-percent> and
/// <https://www.w3.org/TR/css-grid-1/#grid-sizing>.
pub(super) fn compute_grid_layout_pass(
&mut self,
style: &ComputedStyle,
children: &[GridChild<'_>],
stylesheets: &Stylesheets<'_>,
subgrid_context: Option<&ResolvedSubgridContext>,
contributions: &[SubgridContribution],
config: GridLayoutPassConfig<'_>,
) -> Option<GridLayout> {
self.compute_grid_layout_pass_with_estimates(
style,
children,
stylesheets,
subgrid_context,
contributions,
config,
)
.map(|pass| pass.layout)
}
fn compute_grid_layout_pass_with_estimates(
&mut self,
style: &ComputedStyle,
children: &[GridChild<'_>],
stylesheets: &Stylesheets<'_>,
subgrid_context: Option<&ResolvedSubgridContext>,
contributions: &[SubgridContribution],
config: GridLayoutPassConfig<'_>,
) -> Option<GridLayoutPassResult> {
#[cfg(feature = "layout-profile")]
let _profile = crate::layout::layout_profile::grid_track_sizing_scope(children.len());
let content_width = config.width;
let width = content_width.points();
let root_height = config.root_height;
let root_height_points = root_height.map(PhysicalContentHeight::points);
let contained_subgrid_columns = (style.contain.layout || style.contain.paint)
&& matches!(style.grid_template_columns, css::GridTrackList::None);
let contained_subgrid_rows = (style.contain.layout || style.contain.paint)
&& matches!(style.grid_template_rows, css::GridTrackList::None);
let item_height_basis = if contained_subgrid_rows {
GridPercentageBasis::indefinite()
} else {
config.item_height_basis
};
let row_gap_basis = config.row_gap_basis;
// A contained subgrid axis resolves to `none`, leaving its automatic
// implicit tracks to size from intrinsic contributions. Percentages on
// those grid items are cyclic during that sizing phase and therefore
// behave as `auto`; feeding the final stretched grid-area width back
// here would incorrectly make `width: 100%` grow the implicit track.
// <https://drafts.csswg.org/css-grid-2/#subgrid-listing> and
// <https://drafts.csswg.org/css-grid-1/#percentage-sizing>
let item_width_basis = config.item_width_basis.unwrap_or_else(|| {
if contained_subgrid_columns {
GridPercentageBasis::indefinite()
} else {
grid_percentage_basis(
Some(content_width.content_box_length()),
GridAvailableSizeSource::ContainerInlineSize,
)
}
});
let item_available_space = GridPhysicalAvailableSpace {
width_basis: item_width_basis,
height_basis: item_height_basis,
};
// Taffy's grid tracks are physical: columns run along x and rows along
// y. CSS Grid's columns/rows are logical inline/block axes, so vertical
// writing modes must swap the templates, auto tracks, placement lines,
// alignment axes, and gaps at this adapter boundary:
// <https://www.w3.org/TR/css-writing-modes-4/#abstract-box> and
// <https://www.w3.org/TR/css-grid-2/#track-sizing>.
let writing_axes = WritingModeAxes::new(style.writing_mode, style.direction);
let swaps_physical_grid_axes = writing_axes.swaps_physical_axes();
// Taffy reverses its column sequence for horizontal RTL. Vertical
// Grid projection remains within the separately documented incomplete
// writing-mode surface and must not be inferred from coincident track
// coordinates.
let taffy_columns_are_reversed =
!swaps_physical_grid_axes && writing_axes.is_reversed(LogicalAxis::Inline);
let track_percentage_bases =
GridTrackPercentageBases::from_grid_content_box(style, content_width, root_height);
let physical_column_subgrid = subgrid_context
.and_then(|context| context.physical_axis(GridAxis::Column, swaps_physical_grid_axes));
let physical_row_subgrid = subgrid_context
.and_then(|context| context.physical_axis(GridAxis::Row, swaps_physical_grid_axes));
let resolved_item_placements = subgrid_context
.map(|context| context.resolve_item_placements(children, style.grid_auto_flow));
let mut tree: taffy_layout::TaffyTree<GridTaffyLeaf> = taffy_layout::TaffyTree::new();
tree.disable_rounding();
let row_adjustment = taffy_startward_implicit_row_adjustment(
style,
children,
track_percentage_bases.for_axis(GridAxis::Row),
);
let column_adjustment = taffy_startward_implicit_column_adjustment(
style,
children,
track_percentage_bases.for_axis(GridAxis::Column),
);
let mut nodes = Vec::with_capacity(children.len());
let mut estimates = Vec::with_capacity(children.len());
let mut feedback_sensitivities = Vec::with_capacity(children.len());
let mut item_box_metrics = Vec::with_capacity(children.len());
for (index, child) in children.iter().enumerate() {
let item_bases = config
.item_containing_block_bases
.as_ref()
.and_then(|bases| bases.get(index))
.copied();
// A contained subgrid has already resolved this axis to a
// standalone `none` track list. Do not reintroduce the parent
// subgrid area through replay's per-item basis: percentages in
// its implicit tracks are cyclic and behave as auto.
// <https://drafts.csswg.org/css-grid-2/#subgrid-listing>
// <https://drafts.csswg.org/css-sizing-3/#cyclic-percentage>
let child_width_basis = if contained_subgrid_columns {
item_width_basis
} else {
item_bases.map_or(item_width_basis, |bases| bases.width)
};
let child_height_basis = if contained_subgrid_rows {
item_height_basis
} else {
item_bases.map_or(item_height_basis, |bases| bases.height)
};
let child_inline_basis = GridPhysicalAvailableSpace {
width_basis: child_width_basis,
height_basis: child_height_basis,
}
.logical_inline_basis(style);
// During Grid's bounded cross-axis feedback and its final pass,
// an item's resolved grid area is the available physical space
// for intrinsic measurement. The percentage basis alone is not
// enough: nested grids must also receive this dimension as their
// content measurement constraint, so their own padding shrinks
// descendants before they contribute a block size.
// <https://drafts.csswg.org/css-grid-2/#algo-track-sizing>
let child_available_width = child_width_basis.points().unwrap_or(width);
let placement_override = config
.item_placement_overrides
.get(index)
.copied()
.flatten();
let overridden_row = placement_override.and_then(|area| {
taffy_grid_area_line(
area,
if swaps_physical_grid_axes {
GridAxis::Column
} else {
GridAxis::Row
},
)
});
let overridden_column = placement_override.and_then(|area| {
taffy_grid_area_line(
area,
if swaps_physical_grid_axes {
GridAxis::Row
} else {
GridAxis::Column
},
)
});
let resolved_placement = resolved_item_placements
.as_ref()
.map(|placements| placements[index]);
let measurement = self.measure_grid_item_size(
child,
stylesheets,
child_available_width,
child_width_basis,
child_height_basis,
);
let estimate = measurement.estimate;
// A subgrid has no independent intrinsic contribution in an
// inherited axis. Its explicitly placed descendants are inserted
// below as projected proxy leaves after preliminary placement.
// Retain the original estimate for replay/baseline bookkeeping;
// only the Taffy sizing leaf is made empty in that logical axis.
// <https://drafts.csswg.org/css-grid-2/#subgrid-track-sizing>
let sizing_estimate = grid_item_parent_sizing_estimate(estimate, &child.style);
// Grid's intrinsic contributions are logical, while Taffy's
// layout tree is physical. Keep the logical estimate for Grid's
// own track and baseline calculations, and project only the
// automatic measurement inputs supplied to Taffy.
// <https://www.w3.org/TR/css-writing-modes-4/#abstract-box>.
let physical_estimate = sizing_estimate.physical_measurements();
estimates.push(estimate);
feedback_sensitivities.push(measurement.sensitivity);
item_box_metrics.push(used_box_metrics_for_logical_inline_basis(
&child.style,
child_inline_basis.map_source(|_| ()),
));
let node = tree
.new_leaf_with_context(
taffy_layout::Style {
display: taffy_layout::Display::Block,
box_sizing: taffy_bridge::box_sizing(child.style.box_sizing),
direction: taffy_bridge::direction(child.style.used_direction()),
size: taffy_layout::Size {
width: taffy_grid_item_dimension(
child.style.box_values.width.clone(),
child_width_basis,
physical_estimate.min_width,
physical_estimate.content_width,
),
height: taffy_grid_item_dimension(
child.style.box_values.height.value().clone(),
child_height_basis,
physical_estimate.min_height,
physical_estimate.content_height,
),
},
// CSS Grid's track-sizing and item-layout phases both
// need the preferred ratio: a definite grid-area size
// in either axis transfers to the automatic opposite
// axis before alignment and intrinsic contribution
// resolution.
// <https://www.w3.org/TR/css-sizing-4/#aspect-ratio>
aspect_ratio: child
.style
.aspect_ratio
.preferred_ratio_for_non_replaced(false),
min_size: taffy_layout::Size {
width: grid_item_taffy_min_constraint(
child.style.box_values.min_width.clone(),
GridAxis::Column,
style,
&child.style,
physical_estimate.min_width,
physical_estimate.content_width,
),
height: grid_item_taffy_min_constraint(
child.style.box_values.min_height.clone(),
GridAxis::Row,
style,
&child.style,
physical_estimate.min_height,
physical_estimate.content_height,
),
},
max_size: taffy_layout::Size {
width: taffy_grid_item_constraint(
child.style.box_values.max_width.clone(),
GridPercentageBasis::indefinite(),
physical_estimate.min_width,
physical_estimate.content_width,
),
height: taffy_grid_item_constraint(
child.style.box_values.max_height.clone(),
GridPercentageBasis::indefinite(),
physical_estimate.min_height,
physical_estimate.content_height,
),
},
margin: grid_taffy_margin_with_baseline_shim(
&child.style,
child_inline_basis,
config
.baseline_plan
.as_ref()
.and_then(|plan| plan.shim(index)),
),
padding: taffy_bridge::padding(&child.style, child_inline_basis),
border: taffy_bridge::border_edges(used_border_widths(&child.style)),
align_self: if swaps_physical_grid_axes {
taffy_effective_grid_justify_self(&child.style, style)
} else {
taffy_effective_grid_align_self(&child.style, style)
},
justify_self: if swaps_physical_grid_axes {
taffy_effective_grid_align_self(&child.style, style)
} else {
taffy_effective_grid_justify_self(&child.style, style)
},
grid_row: overridden_row.unwrap_or_else(|| {
if swaps_physical_grid_axes {
resolved_placement
.and_then(|placement| placement.columns)
.map(ResolvedSubgridPlacement::taffy_line)
.unwrap_or_else(|| {
physical_row_subgrid.map_or_else(
|| {
taffy_grid_line_with_startward_adjustment(
&child.style.grid_column_start,
&child.style.grid_column_end,
&column_adjustment,
)
},
|axis| {
axis.clamped_taffy_line(
&child.style.grid_column_start,
&child.style.grid_column_end,
)
},
)
})
} else {
resolved_placement
.and_then(|placement| placement.rows)
.map(ResolvedSubgridPlacement::taffy_line)
.unwrap_or_else(|| {
physical_row_subgrid.map_or_else(
|| {
taffy_grid_line_with_startward_adjustment(
&child.style.grid_row_start,
&child.style.grid_row_end,
&row_adjustment,
)
},
|axis| {
axis.clamped_taffy_line(
&child.style.grid_row_start,
&child.style.grid_row_end,
)
},
)
})
}
}),
grid_column: overridden_column.unwrap_or_else(|| {
if swaps_physical_grid_axes {
resolved_placement
.and_then(|placement| placement.rows)
.map(ResolvedSubgridPlacement::taffy_line)
.unwrap_or_else(|| {
physical_column_subgrid.map_or_else(
|| {
taffy_grid_line_with_startward_adjustment(
&child.style.grid_row_start,
&child.style.grid_row_end,
&row_adjustment,
)
},
|axis| {
axis.clamped_taffy_line(
&child.style.grid_row_start,
&child.style.grid_row_end,
)
},
)
})
} else {
resolved_placement
.and_then(|placement| placement.columns)
.map(ResolvedSubgridPlacement::taffy_line)
.unwrap_or_else(|| {
physical_column_subgrid.map_or_else(
|| {
taffy_grid_line_with_startward_adjustment(
&child.style.grid_column_start,
&child.style.grid_column_end,
&column_adjustment,
)
},
|axis| {
axis.clamped_taffy_line(
&child.style.grid_column_start,
&child.style.grid_column_end,
)
},
)
})
}
}),
..Default::default()
},
GridTaffyLeaf::Item(sizing_estimate),
)
.ok()?;
nodes.push(node);
}
let mut contribution_nodes = Vec::with_capacity(contributions.len());
for contribution in contributions {
// The collector retains the descendant's content-box intrinsic
// measurement and its accumulated subgrid edge adjustments
// separately. Taffy owns their margin-inclusive track sizing,
// exactly as it does for ordinary Grid items.
let estimate = contribution.sizing_estimate().physical_measurements();
let node = tree
.new_leaf_with_context(
taffy_layout::Style {
display: taffy_layout::Display::Block,
margin: contribution.taffy_margin(),
grid_row: taffy_layout::Line {
start: taffy_layout::line(
i16::try_from(contribution.area.row_start).ok()?,
),
end: taffy_layout::line(i16::try_from(contribution.area.row_end).ok()?),
},
grid_column: taffy_layout::Line {
start: taffy_layout::line(
i16::try_from(contribution.area.column_start).ok()?,
),
end: taffy_layout::line(
i16::try_from(contribution.area.column_end).ok()?,
),
},
..Default::default()
},
GridTaffyLeaf::Contribution(estimate),
)
.ok()?;
contribution_nodes.push(node);
}
// Taffy's empty grid currently omits otherwise-valid explicit track
// geometry. CSS Grid still sizes an empty grid from its explicit
// tracks and gaps, which is particularly observable when size
// containment removes every real item from the principal sizing pass.
// A zero-contribution probe item materializes those tracks without
// entering Spindrift's returned item list. Auto-fit grids remain genuinely
// empty so their unoccupied repeated tracks can collapse.
// <https://www.w3.org/TR/css-grid-1/#explicit-grids>
// <https://www.w3.org/TR/css-contain-1/#containment-size>
let mut layout_nodes = nodes.clone();
layout_nodes.extend(contribution_nodes);
if layout_nodes.is_empty()
&& !grid_track_list_has_auto_fit(&style.grid_template_columns)
&& !grid_track_list_has_auto_fit(&style.grid_template_rows)
{
let zero = taffy_layout::Dimension::length(0.0);
let zero_constraint = taffy_layout::LengthPercentageAuto::length(0.0);
let placeholder = tree
.new_leaf_with_context(
taffy_layout::Style {
size: taffy_layout::Size {
width: zero,
height: zero,
},
min_size: taffy_layout::Size {
width: zero_constraint,
height: zero_constraint,
},
max_size: taffy_layout::Size {
width: zero_constraint,
height: zero_constraint,
},
..Default::default()
},
GridTaffyLeaf::Contribution(GridItemEstimate::fixed(0.0, 0.0)),
)
.ok()?;
layout_nodes.push(placeholder);
}
let root = tree
.new_with_children(
taffy_layout::Style {
display: taffy_layout::Display::Grid,
box_sizing: taffy_layout::BoxSizing::BorderBox,
direction: taffy_bridge::direction(style.used_direction()),
size: taffy_layout::Size {
width: taffy_layout::Dimension::length(width),
height: root_height_points
.map(taffy_layout::Dimension::length)
.unwrap_or_else(taffy_layout::Dimension::auto),
},
min_size: taffy_layout::Size {
width: taffy_dimension(style.box_values.min_width.clone()),
height: taffy_dimension(style.box_values.min_height.clone()),
},
max_size: taffy_layout::Size {
width: taffy_dimension(style.box_values.max_width.clone()),
height: taffy_dimension(style.box_values.max_height.clone()),
},
grid_template_columns: config
.frozen_tracks
.columns
.as_ref()
.map(|axis| {
let mut sizes = axis.topology.track_sizes();
if !swaps_physical_grid_axes && style.used_direction() == Direction::Rtl
{
sizes.reverse();
}
taffy_fixed_grid_tracks(&sizes)
})
.or_else(|| physical_column_subgrid.map(ResolvedSubgridAxis::taffy_tracks))
.unwrap_or_else(|| {
if swaps_physical_grid_axes {
taffy_grid_template_rows_with_startward_adjustment(
style,
&row_adjustment,
track_percentage_bases.for_axis(GridAxis::Row),
)
} else {
taffy_grid_template_columns_with_startward_adjustment(
style,
&column_adjustment,
track_percentage_bases.for_axis(GridAxis::Column),
)
}
}),
grid_template_rows: config
.frozen_tracks
.rows
.as_ref()
.map(|axis| taffy_fixed_grid_tracks(&axis.topology.track_sizes()))
.or_else(|| physical_row_subgrid.map(ResolvedSubgridAxis::taffy_tracks))
.unwrap_or_else(|| {
if swaps_physical_grid_axes {
taffy_grid_template_columns_with_startward_adjustment(
style,
&column_adjustment,
track_percentage_bases.for_axis(GridAxis::Column),
)
} else {
taffy_grid_template_rows_with_startward_adjustment(
style,
&row_adjustment,
track_percentage_bases.for_axis(GridAxis::Row),
)
}
}),
grid_template_areas: taffy_grid_template_areas_with_startward_adjustment(
&style.grid_template_areas,
&row_adjustment,
&column_adjustment,
),
grid_template_column_names: physical_column_subgrid
.map(|axis| axis.line_names().to_vec())
.unwrap_or_else(|| {
taffy_grid_template_column_names_with_startward_adjustment(
style,
&column_adjustment,
)
}),
grid_template_row_names: physical_row_subgrid
.map(|axis| axis.line_names().to_vec())
.unwrap_or_else(|| {
taffy_grid_template_row_names_with_startward_adjustment(
style,
&row_adjustment,
)
}),
grid_auto_columns: (config.frozen_tracks.columns.is_some()
|| physical_column_subgrid.is_some())
.then(Vec::new)
.unwrap_or_else(|| {
if swaps_physical_grid_axes {
taffy_grid_auto_tracks(&style.grid_auto_rows)
} else {
taffy_grid_auto_tracks(&style.grid_auto_columns)
}
}),
grid_auto_rows: (config.frozen_tracks.rows.is_some()
|| physical_row_subgrid.is_some())
.then(Vec::new)
.unwrap_or_else(|| {
if swaps_physical_grid_axes {
taffy_grid_auto_tracks(&style.grid_auto_columns)
} else {
taffy_grid_auto_tracks(&style.grid_auto_rows)
}
}),
grid_auto_flow: taffy_grid_auto_flow(style.grid_auto_flow),
justify_content: Some(if swaps_physical_grid_axes {
taffy_grid_align_content(style.align_content)
} else {
taffy_grid_justify_content(style.justify_content)
}),
align_content: Some(if swaps_physical_grid_axes {
taffy_grid_justify_content(style.justify_content)
} else {
taffy_grid_align_content(style.align_content)
}),
justify_items: Some(if swaps_physical_grid_axes {
taffy_grid_align_items(style.align_items)
} else {
taffy_grid_justify_items(style.justify_items)
}),
align_items: Some(if swaps_physical_grid_axes {
taffy_grid_justify_items(style.justify_items)
} else {
taffy_grid_align_items(style.align_items)
}),
gap: taffy_layout::Size {
width: physical_column_subgrid.map_or_else(
|| {
taffy_bridge::gap(
if swaps_physical_grid_axes {
style.row_gap.clone()
} else {
style.column_gap.clone()
},
item_width_basis,
)
},
|axis| taffy_layout::LengthPercentage::length(axis.taffy_gap()),
),
height: physical_row_subgrid.map_or_else(
|| {
taffy_bridge::gap(
if swaps_physical_grid_axes {
style.column_gap.clone()
} else {
style.row_gap.clone()
},
row_gap_basis,
)
},
|axis| taffy_layout::LengthPercentage::length(axis.taffy_gap()),
),
},
..Default::default()
},
&layout_nodes,
)
.ok()?;
tree.compute_layout_with_measure(
root,
taffy_layout::Size {
width: taffy_layout::AvailableSpace::Definite(width),
height: root_height_points
.map(taffy_layout::AvailableSpace::Definite)
.unwrap_or(taffy_layout::AvailableSpace::MaxContent),
},
|input, _node_id, node_context, _style| {
let estimate = node_context.map(|context| match context {
GridTaffyLeaf::Item(estimate) | GridTaffyLeaf::Contribution(estimate) => {
estimate
}
});
taffy_grid_measurement(input, estimate)
},
)
.ok()?;
let root_layout = tree.layout(root).ok()?;
let mut grid_item_areas = Vec::new();
let mut columns = GridAxisTopology::default();
let mut rows = GridAxisTopology::default();
let mut taffy_physical_columns = GridAxisTopology::default();
let mut taffy_physical_rows = GridAxisTopology::default();
let mut track_corrections = GridAxisCorrections::default();
match tree.detailed_layout_info(root) {
taffy::tree::DetailedLayoutInfo::Grid(info) => {
// Taffy 0.14 exposes final logical track positions rather
// than the former separate size/gutter arrays. Preserve
// Spindrift's canonical topology by deriving a track extent and
// each interior gutter at this sole backend boundary.
let physical_column_gap = physical_column_subgrid.map_or_else(
|| {
taffy_bridge::resolved_gap(
if swaps_physical_grid_axes {
style.row_gap.clone()
} else {
style.column_gap.clone()
},
item_width_basis,
)
},
|axis| axis.taffy_gap(),
);
let physical_row_gap = physical_row_subgrid.map_or_else(
|| {
taffy_bridge::resolved_gap(
if swaps_physical_grid_axes {
style.column_gap.clone()
} else {
style.row_gap.clone()
},
row_gap_basis,
)
},
|axis| axis.taffy_gap(),
);
let (taffy_column_sizes, taffy_column_gutters) =
taffy_grid_track_layout_from_positions(
&info.columns.positions,
physical_column_gap,
);
let (taffy_row_sizes, taffy_row_gutters) =
taffy_grid_track_layout_from_positions(&info.rows.positions, physical_row_gap);
taffy_physical_columns = physical_grid_topology_from_taffy_positions(
&info.columns.positions,
taffy_columns_are_reversed,
)?;
taffy_physical_rows =
physical_grid_topology_from_taffy_positions(&info.rows.positions, false)?;
grid_item_areas = info
.items
.iter()
.map(|item| GridItemArea {
row_start: item.row_start,
row_end: item.row_end,
column_start: item.column_start,
column_end: item.column_end,
})
.collect();
// A frozen feedback axis carries the corrected auto-fit
// topology from its preceding pass. Its later semantic
// correction rebases the fixed Taffy result against that
// topology, so applying a fresh startward correction here
// would distribute the same collapse twice.
let column_correction = config
.frozen_tracks
.columns
.is_none()
.then(|| {
startward_auto_fit_track_correction(
style,
GridAxis::Column,
&column_adjustment,
&taffy_column_sizes,
&taffy_column_gutters,
&grid_item_areas,
)
})
.flatten();
let row_correction = config
.frozen_tracks
.rows
.is_none()
.then(|| {
startward_auto_fit_track_correction(
style,
GridAxis::Row,
&row_adjustment,
&taffy_row_sizes,
&taffy_row_gutters,
&grid_item_areas,
)
})
.flatten();
let column_sizes = column_correction
.as_ref()
.map(|correction| correction.target.track_sizes())
.unwrap_or_else(|| taffy_column_sizes.clone());
let column_gutters = column_correction
.as_ref()
.map(|correction| correction.target.interior_gutters())
.unwrap_or_else(|| taffy_column_gutters.clone());
let row_sizes = row_correction
.as_ref()
.map(|correction| correction.target.track_sizes())
.unwrap_or_else(|| taffy_row_sizes.clone());
let row_gutters = row_correction
.as_ref()
.map(|correction| correction.target.interior_gutters())
.unwrap_or_else(|| taffy_row_gutters.clone());
let column_collapsed_tracks = auto_fit_collapsed_track_mask(
style,
GridAxis::Column,
&column_adjustment,
column_sizes.len(),
&grid_item_areas,
);
let row_collapsed_tracks = auto_fit_collapsed_track_mask(
style,
GridAxis::Row,
&row_adjustment,
row_sizes.len(),
&grid_item_areas,
);
// Both Taffy's detailed record and the startward correction
// retain synthetic boundary gutters. Canonical Grid topology
// retains only interior gutters, so normalize exactly once at
// this backend boundary before any frozen or paint consumer
// can observe the axis.
let column_track_gutters =
taffy_grid_track_gutters(&column_gutters, column_sizes.len());
let row_track_gutters = taffy_grid_track_gutters(&row_gutters, row_sizes.len());
columns = column_correction.as_ref().map_or_else(
|| {
GridAxisTopology::from_auto_fit_track_layout(
column_sizes,
column_track_gutters,
column_collapsed_tracks,
)
},
|correction| Some(correction.target.clone()),
)?;
rows = row_correction.as_ref().map_or_else(
|| {
GridAxisTopology::from_auto_fit_track_layout(
row_sizes,
row_track_gutters,
row_collapsed_tracks,
)
},
|correction| Some(correction.target.clone()),
)?;
track_corrections = GridAxisCorrections {
columns: column_correction,
rows: row_correction,
};
}
taffy::tree::DetailedLayoutInfo::None => {}
};
// Taffy is used to resolve placement topology, but it has no subgrid
// model. Once placement is known, inherited axes must retain the
// parent-owned track and gutter geometry exactly.
// <https://www.w3.org/TR/css-grid-2/#subgrids>
if let Some(axis) = physical_column_subgrid {
let track_sizes: Vec<f32> = axis
.track_starts()
.iter()
.zip(axis.track_ends())
.map(|(start, end)| (end - start).max(0.0))
.collect();
columns = GridAxisTopology::from_line_offsets(
axis.line_offsets().to_vec(),
track_sizes.clone(),
vec![false; track_sizes.len()],
)?;
}
if let Some(axis) = physical_row_subgrid {
let track_sizes: Vec<f32> = axis
.track_starts()
.iter()
.zip(axis.track_ends())
.map(|(start, end)| (end - start).max(0.0))
.collect();
rows = GridAxisTopology::from_line_offsets(
axis.line_offsets().to_vec(),
track_sizes.clone(),
vec![false; track_sizes.len()],
)?;
}
// Taffy stores columns in backend-logical order even though their
// final rectangles are already physical. Horizontal RTL therefore
// crosses an explicit adapter boundary here: topology, collapse
// provenance, corrections, and item placement ranges all become
// increasing physical left-to-right order together.
if taffy_columns_are_reversed {
let column_track_count = columns.track_count();
columns = columns.reversed();
for area in &mut grid_item_areas {
*area = area
.with_reversed_axis(GridAxis::Column, column_track_count)
.unwrap_or(*area);
}
if let Some(correction) = &mut track_corrections.columns {
correction.source = correction.source.reversed();
correction.target = correction.target.reversed();
}
}
// Taffy's item rectangles are physical and use the detailed track
// positions above. Whenever Spindrift canonicalizes collapsed auto-fit
// geometry, retain that observed topology as the correction source so
// item offsets can be rebased onto the canonical physical topology.
// This also captures Taffy's content-alignment distribution without
// trying to reconstruct it from a single generic line-offset list.
if columns.has_collapsed_auto_fit_tracks() && track_corrections.columns.is_none() {
track_corrections.columns = Some(GridAxisCorrection {
source: CorrectionSource::Aligned(taffy_physical_columns),
target: columns.clone(),
preserved_item_geometry: None,
});
}
if rows.has_collapsed_auto_fit_tracks() && track_corrections.rows.is_none() {
track_corrections.rows = Some(GridAxisCorrection {
source: CorrectionSource::Aligned(taffy_physical_rows),
target: rows.clone(),
preserved_item_geometry: None,
});
}
// Startward implicit expansion already produces an explicit corrected
// track-layout record below. Retain that established source/target
// correction until it can share the same area model; ordinary frozen
// axes use the semantic collapsed topology directly.
let frozen_column_correction = track_corrections
.columns
.is_none()
.then(|| {
config
.frozen_tracks
.columns
.as_ref()
.and_then(|axis| GridAxisCorrection::from_frozen(axis, &columns))
})
.flatten();
let frozen_row_correction = track_corrections
.rows
.is_none()
.then(|| {
config
.frozen_tracks
.rows
.as_ref()
.and_then(|axis| GridAxisCorrection::from_frozen(axis, &rows))
})
.flatten();
if let Some(correction) = &frozen_column_correction {
columns = correction.target.clone();
}
if let Some(correction) = &frozen_row_correction {
rows = correction.target.clone();
}
let column_line_offsets = columns.line_offsets();
let row_line_offsets = rows.line_offsets();
let column_line_names = physical_column_subgrid.map_or_else(
|| physical_grid_line_names(style, GridAxis::Column, column_line_offsets.len()),
|axis| axis.physical_line_names().to_vec(),
);
let row_line_names = physical_row_subgrid.map_or_else(
|| physical_grid_line_names(style, GridAxis::Row, row_line_offsets.len()),
|axis| axis.physical_line_names().to_vec(),
);
// Proxy nodes are appended after real child nodes and intentionally
// have no corresponding `GridItemLayout`: they influence only Taffy's
// track sizing, never replay, baselines, gap decoration, or fragment
// planning.
debug_assert!(grid_item_areas.len() >= nodes.len());
let mut items = nodes
.into_iter()
.enumerate()
.map(|(index, node)| {
let layout = tree.layout(node).ok()?;
Some(
GridItemLayout::new(
GridRect::new(
GridPoint::new(layout.location.x, layout.location.y),
GridSize::new(layout.size.width.max(0.0), layout.size.height.max(0.0)),
),
grid_item_areas.get(index).cloned(),
)
.with_used_box_metrics(item_box_metrics[index]),
)
})
.collect::<Option<Vec<_>>>()?;
debug_assert_eq!(items.len(), children.len());
apply_resolved_subgrid_axis_item_geometry(
physical_column_subgrid,
GridAxis::Column,
&mut items,
);
apply_resolved_subgrid_axis_item_geometry(physical_row_subgrid, GridAxis::Row, &mut items);
apply_frozen_grid_axis_correction(
frozen_column_correction.as_ref(),
style.justify_content,
content_width.points(),
GridAxis::Column,
style,
Some(children),
Some(&estimates),
&mut items,
);
apply_frozen_grid_axis_correction(
frozen_row_correction.as_ref(),
style.align_content,
root_layout.size.height,
GridAxis::Row,
style,
Some(children),
Some(&estimates),
&mut items,
);
let final_grid_height = physical_row_subgrid
.map(ResolvedSubgridAxis::outer_extent)
.unwrap_or(root_layout.size.height);
// Stretch computes a final grid-area rectangle without converting an
// automatic item inline size into an authored definite size. When a
// vertical grid's physical height was indefinite during track sizing,
// preserve that cycle at replay so the item's content uses the same
// percentage behavior that participated in intrinsic sizing.
// <https://drafts.csswg.org/css-grid-2/#grid-item-sizing>
if swaps_physical_grid_axes && !item_height_basis.is_definite() {
for (item, child) in items.iter_mut().zip(children) {
let inline_self = effective_grid_justify_self(&child.style, style).keyword;
if child.style.box_values.height.is_auto()
&& matches!(
inline_self,
SelfAlignmentKeyword::Normal | SelfAlignmentKeyword::Stretch
)
{
item.preserve_cyclic_physical_height_on_replay();
}
}
}
apply_auto_fit_track_corrections(
style,
content_width,
final_grid_height,
&track_corrections,
frozen_column_correction.is_none(),
frozen_row_correction.is_none(),
&mut items,
);
// Taffy 0.14 stores Grid columns in logical order and assigns their
// physical offsets right-to-left for `direction: rtl`. Its item
// rectangles are therefore already the CSS physical rectangles; do
// not mirror them a second time before Spindrift's logical-only replay.
apply_grid_self_alignment_corrections(
style,
children,
content_width,
final_grid_height,
&columns,
&rows,
&mut items,
);
apply_grid_aspect_ratio_item_size_corrections(
style,
children,
content_width,
final_grid_height,
&columns,
&rows,
&mut items,
);
apply_grid_replaced_item_size_corrections(style, children, &estimates, &mut items);
apply_grid_deferred_percentage_item_size_corrections(
GridFinalItemPercentagePlacement {
container_style: style,
container_width: content_width,
container_height: final_grid_height,
columns: &columns,
rows: &rows,
},
children,
&estimates,
&mut items,
);
let baseline_resolutions =
resolve_grid_baseline_participation(style, children, &items, item_available_space);
// Taffy reports each item's border-box location after resolving its
// grid-area margins. Replay suppresses those margins in the child
// style, but must retain the reported border-box origin unchanged.
// <https://www.w3.org/TR/css-grid-1/#grid-item-placement>.
apply_grid_baseline_alignment(
style,
children,
&estimates,
&baseline_resolutions,
&rows,
final_grid_height,
&mut items,
);
let first_baseline = grid_container_baseline(
style,
&estimates,
&baseline_resolutions,
&items,
GridBaselineSet::First,
);
let last_baseline = grid_container_baseline(
style,
&estimates,
&baseline_resolutions,
&items,
GridBaselineSet::Last,
);
let baselines = PhysicalBaselineSets {
vertical: BaselinePair {
first: first_baseline
.map(|baseline| PhysicalTopBaselineOffset::new(layout_pt(baseline))),
last: last_baseline
.map(|baseline| PhysicalTopBaselineOffset::new(layout_pt(baseline))),
},
..PhysicalBaselineSets::default()
};
Some(GridLayoutPassResult {
layout: GridLayout {
height: config.reported_height.unwrap_or_else(|| {
PhysicalContentHeight::new(content_box_pt(final_grid_height))
}),
baselines,
first_baseline,
last_baseline,
items,
baseline_resolutions,
columns,
rows,
content_width: content_width.points(),
content_height: final_grid_height,
column_line_names,
row_line_names,
},
estimates,
feedback_sensitivities,
})
}
}
fn grid_has_margin_trim(style: &ComputedStyle) -> bool {
let trim = style.margin_trim;
trim.block_start || trim.block_end || trim.inline_start || trim.inline_end
}
/// Derive a Grid item's trimmed physical margins from its placed logical area.
///
/// CSS Grid trims every item in the edge track. Taffy reports one-indexed
/// physical row/column line numbers, so translate those back through the
/// container writing mode before comparing the outer tracks. Empty tracks are
/// retained, while only zero-sized unoccupied `auto-fit` tracks are ignored.
/// <https://drafts.csswg.org/css-box-4/#margin-trim-grid>.
fn grid_margin_trim_plan(style: &ComputedStyle, layout: &GridLayout) -> MarginTrimPlan {
let mut plan = MarginTrimPlan::for_item_count(layout.items.len());
let axes = WritingModeAxes::new(style.writing_mode, style.used_direction());
let swaps_axes = axes.swaps_physical_axes();
let physical_spans = layout
.items
.iter()
.filter_map(|item| item.area)
.collect::<Vec<_>>();
let (inline_spans, block_spans, inline_sizes, block_sizes) = if swaps_axes {
(
physical_spans
.iter()
.map(|area| (area.row_start, area.row_end))
.collect::<Vec<_>>(),
physical_spans
.iter()
.map(|area| (area.column_start, area.column_end))
.collect::<Vec<_>>(),
layout.rows.track_sizes(),
layout.columns.track_sizes(),
)
} else {
(
physical_spans
.iter()
.map(|area| (area.column_start, area.column_end))
.collect::<Vec<_>>(),
physical_spans
.iter()
.map(|area| (area.row_start, area.row_end))
.collect::<Vec<_>>(),
layout.columns.track_sizes(),
layout.rows.track_sizes(),
)
};
let (inline_first, inline_last) =
grid_margin_trim_edge_lines(&style.grid_template_columns, &inline_sizes, &inline_spans);
let (block_first, block_last) =
grid_margin_trim_edge_lines(&style.grid_template_rows, &block_sizes, &block_spans);
for (index, item) in layout.items.iter().enumerate() {
let Some(area) = item.area else {
continue;
};
let (inline_start, inline_end, block_start, block_end) = if swaps_axes {
(
area.row_start,
area.row_end,
area.column_start,
area.column_end,
)
} else {
(
area.column_start,
area.column_end,
area.row_start,
area.row_end,
)
};
if style.margin_trim.inline_start && inline_start == inline_first {
plan.trim(index, axes.physical_side(LogicalSide::InlineStart));
}
if style.margin_trim.inline_end && inline_end == inline_last {
plan.trim(index, axes.physical_side(LogicalSide::InlineEnd));
}
if style.margin_trim.block_start && block_start == block_first {
plan.trim(index, axes.physical_side(LogicalSide::BlockStart));
}
if style.margin_trim.block_end && block_end == block_last {
plan.trim(index, axes.physical_side(LogicalSide::BlockEnd));
}
}
plan
}
/// Return the first and last non-collapsed grid lines for one logical axis.
///
/// Outside `auto-fit`, even a zero-sized or empty track remains relevant to
/// margin adjacency. In an `auto-fit` repetition, CSS Grid collapses only
/// empty repeated tracks; an occupied zero-sized track must still be kept.
/// <https://www.w3.org/TR/css-grid-1/#auto-repeat>.
fn grid_margin_trim_edge_lines(
tracks: &css::GridTrackList,
sizes: &[f32],
areas: &[(u16, u16)],
) -> (u16, u16) {
let last_line = u16::try_from(sizes.len().saturating_add(1)).unwrap_or(u16::MAX);
if !grid_track_list_has_auto_fit(tracks) {
return (1, last_line);
}
let occupied = |track_index: usize| {
let line = u16::try_from(track_index.saturating_add(1)).unwrap_or(u16::MAX);
areas
.iter()
.any(|(start, end)| *start <= line && line < *end)
};
let first = sizes
.iter()
.enumerate()
.find(|(index, size)| size.abs() > 0.01 || occupied(*index))
.and_then(|(index, _)| u16::try_from(index.saturating_add(1)).ok())
.unwrap_or(1);
let last = sizes
.iter()
.enumerate()
.rev()
.find(|(index, size)| size.abs() > 0.01 || occupied(*index))
.and_then(|(index, _)| u16::try_from(index.saturating_add(2)).ok())
.unwrap_or(last_line);
(first, last)
}
/// The direct grid item representing a subgrid is empty in an inherited axis;
/// projected descendant proxy leaves supply that axis's track-sizing
/// contribution. `GridItemEstimate` is logical, so this remains independent
/// of the container's physical writing-mode adapter.
fn grid_item_parent_sizing_estimate(
mut estimate: GridItemEstimate,
style: &ComputedStyle,
) -> GridItemEstimate {
if matches!(
style.grid_template_columns,
css::GridTrackList::Subgrid { .. }
) {
estimate.metrics.width = content_box_pt(0.0);
estimate.metrics.min_width = content_box_pt(0.0);
estimate.metrics.content_width = content_box_pt(0.0);
}
if matches!(style.grid_template_rows, css::GridTrackList::Subgrid { .. }) {
estimate.metrics.height = content_box_pt(0.0);
estimate.metrics.min_height = content_box_pt(0.0);
estimate.metrics.content_height = content_box_pt(0.0);
}
estimate
}
/// Convert a Grid item's automatic minimum for Taffy after applying Grid's
/// eligibility conditions. Taffy's generic `auto` minimum is content based,
/// but Grid explicitly zeroes that minimum for a multi-track span containing
/// a flexible track.
/// <https://www.w3.org/TR/css-grid-1/#min-size-auto>
fn grid_item_taffy_min_constraint(
value: css::ComputedLengthPercentageOrAuto,
physical_axis: GridAxis,
container_style: &ComputedStyle,
item_style: &ComputedStyle,
min_content: ContentBoxLength,
max_content: ContentBoxLength,
) -> taffy_layout::LengthPercentageAuto {
if value.is_auto()
&& grid_item_spans_flexible_track_on_physical_axis(
container_style,
item_style,
physical_axis,
)
{
return taffy_layout::LengthPercentageAuto::length(0.0);
}
taffy_grid_item_constraint(
value,
GridPercentageBasis::indefinite(),
min_content,
max_content,
)
}
/// Whether an explicitly counted multi-track span crosses a flexible track.
///
/// The Grid automatic-minimum rule is deliberately based on track sizing
/// functions, not the final numeric track sizes. The full placement engine
/// remains Taffy's responsibility; this early decision is only needed for a
/// span encoded directly in the item style, before Taffy consumes the leaf's
/// minimum contribution.
fn grid_item_spans_flexible_track_on_physical_axis(
container_style: &ComputedStyle,
item_style: &ComputedStyle,
physical_axis: GridAxis,
) -> bool {
let swaps_axes = WritingModeAxes::new(container_style.writing_mode, container_style.direction)
.swaps_physical_axes();
let (start, end, tracks) = match (physical_axis, swaps_axes) {
(GridAxis::Column, false) => (
&item_style.grid_column_start,
&item_style.grid_column_end,
&container_style.grid_template_columns,
),
(GridAxis::Row, false) => (
&item_style.grid_row_start,
&item_style.grid_row_end,
&container_style.grid_template_rows,
),
(GridAxis::Column, true) => (
&item_style.grid_row_start,
&item_style.grid_row_end,
&container_style.grid_template_rows,
),
(GridAxis::Row, true) => (
&item_style.grid_column_start,
&item_style.grid_column_end,
&container_style.grid_template_columns,
),
};
grid_placement_explicit_span(start, end).is_some_and(|span| span > 1)
&& grid_track_list_has_flexible_track(tracks)
}
fn grid_placement_explicit_span(
start: &css::GridPlacement,
end: &css::GridPlacement,
) -> Option<u16> {
match (start, end) {
(css::GridPlacement::Line(_), css::GridPlacement::Span(span))
| (css::GridPlacement::Span(span), css::GridPlacement::Line(_)) => span.count(),
_ => None,
}
}
fn grid_track_list_has_flexible_track(tracks: &css::GridTrackList) -> bool {
let css::GridTrackList::Tracks { components, .. } = tracks else {
return false;
};
components
.iter()
.any(grid_track_component_has_flexible_track)
}
fn grid_track_component_has_flexible_track(component: &css::GridTrackListComponent) -> bool {
match component {
css::GridTrackListComponent::Track(_, track) => {
matches!(track.max, css::GridMaxTrackBreadth::Flex(_))
}
css::GridTrackListComponent::Repeat(_, repeat) => repeat
.tracks
.iter()
.any(grid_track_component_has_flexible_track),
}
}
fn grid_gap_resolves_differently_with_basis(
gap: css::ComputedGap,
container_size: PhysicalContentHeight,
) -> bool {
let css::ComputedGap::LengthPercentage(value) = gap else {
return false;
};
let intrinsic = value.length_max_zero().points();
let used = value
.used_length_with_percentage_basis(PercentageBasis::definite(
container_size.content_box_length(),
))
.map(layout_points)
.unwrap_or(value.length_points())
.max(0.0);
(used - intrinsic).abs() > 0.01
}
pub(super) struct GridLayoutPassConfig<'a> {
pub(super) width: PhysicalContentWidth,
pub(super) root_height: Option<PhysicalContentHeight>,
/// Overrides the physical-width percentage basis for an intrinsic sizing
/// pass. Grid Lanes uses this while probing a column auto-repeat so item
/// percentages cannot feed the container width back into track sizing.
pub(super) item_width_basis: Option<GridPercentageBasis>,
pub(super) item_height_basis: GridPercentageBasis,
/// Definite physical containing-block sizes derived from resolved grid
/// areas. These are used only by the bounded feedback and final-placement
/// passes; the first intrinsic pass retains its cyclic bases.
pub(super) item_containing_block_bases: Option<Vec<GridItemContainingBlockBases>>,
/// Physical track axes held fixed while CSS Grid performs one of its
/// bounded cross-axis intrinsic-contribution corrections.
pub(super) frozen_tracks: GridFrozenTrackTopology,
pub(super) row_gap_basis: GridPercentageBasis,
pub(super) reported_height: Option<PhysicalContentHeight>,
/// Resolved real-item areas retained while sizing-only descendant proxy
/// leaves are included in a second parent track-sizing pass.
pub(super) item_placement_overrides: Vec<Option<GridItemArea>>,
/// Baseline shims derived from the placement topology. These affect only
/// Taffy's intrinsic Grid sizing margins, never the replayed item style.
pub(super) baseline_plan: Option<&'a GridBaselinePlan>,
}
/// The ephemeral data produced by one Grid sizing pass.
///
/// Ordinary callers discard the item estimates with the pass, but baseline
/// shim construction consumes the preliminary pass's exact measurements.
struct GridLayoutPassResult {
layout: GridLayout,
estimates: Vec<GridItemEstimate>,
feedback_sensitivities: Vec<GridItemFeedbackSensitivity>,
}
/// The subset of Grid's bounded feedback sequence that the preliminary item
/// measurements could still affect. The plan is proof-based: an omitted probe
/// is omitted only when every item reported the relevant contribution
/// invariant.
#[derive(Debug, Clone, Copy, Default)]
struct GridFeedbackProbePlan {
needs_inline_comparison: bool,
needs_block_comparison: bool,
}
impl GridFeedbackProbePlan {
fn from_sensitivities(sensitivities: &[GridItemFeedbackSensitivity]) -> Self {
Self {
needs_inline_comparison: sensitivities
.iter()
.any(|sensitivity| sensitivity.inline_contribution_may_depend_on_area),
needs_block_comparison: sensitivities
.iter()
.any(|sensitivity| sensitivity.block_contribution_may_depend_on_area),
}
}
fn needs_container_comparison(self) -> bool {
self.needs_inline_comparison || self.needs_block_comparison
}
}
/// A placed grid item's physical containing-block percentage bases.
///
/// Grid item layout is performed in its grid area, not the grid container;
/// preserving both physical axes prevents an orthogonal item from resolving a
/// block percentage against the container's unrelated dimension.
/// <https://drafts.csswg.org/css-grid-2/#grid-item-sizing>
#[derive(Debug, Clone, Copy)]
pub(super) struct GridItemContainingBlockBases {
width: GridPercentageBasis,
height: GridPercentageBasis,
}
/// Resolved physical tracks retained while correcting the opposite logical
/// axis. Taffy's Grid interface does not expose an axis-only sizing entry
/// point, so the fixed axis is represented as exact fixed track functions.
#[derive(Debug, Clone, Default)]
pub(super) struct GridFrozenTrackTopology {
columns: Option<GridFrozenTrackAxis>,
rows: Option<GridFrozenTrackAxis>,
}
/// The final physical geometry retained while a feedback pass holds an axis
/// fixed. `collapsed_tracks` keeps CSS auto-fit topology that Taffy's fixed
/// length template cannot represent.
#[derive(Debug, Clone)]
pub(super) struct GridFrozenTrackAxis {
topology: GridAxisTopology,
item_geometry: Vec<FrozenGridItemAxisGeometry>,
}
/// One already-resolved physical Grid item axis retained across a bounded
/// opposite-axis feedback pass. The frozen pass must not re-run its
/// self-alignment against Taffy's lossy fixed-track representation.
#[derive(Debug, Clone, Copy)]
struct FrozenGridItemAxisGeometry {
start: f32,
size: f32,
}
impl GridFrozenTrackAxis {
fn from_layout(layout: &GridLayout, axis: GridAxis) -> Self {
Self {
topology: layout.axis_topology(axis).clone(),
item_geometry: layout
.items
.iter()
.map(|item| FrozenGridItemAxisGeometry {
start: item.axis_start(axis),
size: item.axis_size(axis),
})
.collect(),
}
}
fn has_collapsed_tracks(&self) -> bool {
self.topology.has_collapsed_auto_fit_tracks()
}
}
impl GridAxisCorrection {
fn from_frozen(axis: &GridFrozenTrackAxis, source: &GridAxisTopology) -> Option<Self> {
(axis.has_collapsed_tracks() && source.track_count() == axis.topology.track_count()).then(
|| Self {
source: CorrectionSource::Unaligned(source.clone()),
target: axis.topology.clone(),
preserved_item_geometry: Some(axis.item_geometry.clone()),
},
)
}
}
/// Rebase final Taffy item geometry from its fixed-track alignment subject to
/// the CSS auto-fit subject. Taffy cannot mark a fixed zero-length track as
/// collapsed, so it distributes space through every frozen track; CSS Grid
/// distributes only through the non-collapsed tracks.
/// <https://www.w3.org/TR/css-grid-1/#auto-repeat>
#[allow(clippy::too_many_arguments)] // The two feedback axes share this exact placement adapter.
fn apply_frozen_grid_axis_correction(
correction: Option<&GridAxisCorrection>,
content_alignment: css::ContentAlignment,
container_size: f32,
axis: GridAxis,
container_style: &ComputedStyle,
children: Option<&[GridChild<'_>]>,
estimates: Option<&[GridItemEstimate]>,
items: &mut [GridItemLayout],
) {
let Some(correction) = correction else {
return;
};
for (index, item) in items.iter_mut().enumerate() {
if let Some(geometry) = correction
.preserved_item_geometry
.as_deref()
.and_then(|geometry| geometry.get(index))
{
item.set_axis_geometry(axis, geometry.start, geometry.size);
continue;
}
let Some(area) = item.area else {
continue;
};
let (start_line, end_line) = match axis {
GridAxis::Column => (area.column_start, area.column_end),
GridAxis::Row => (area.row_start, area.row_end),
};
let Some((source_start, source_end)) =
correction
.source
.area_bounds(content_alignment, container_size, start_line, end_line)
else {
continue;
};
let Some((target_start, target_end)) = correction.target.aligned_area_bounds(
content_alignment,
container_size,
start_line,
end_line,
) else {
continue;
};
let source_area_size = (source_end - source_start).max(0.0);
let target_area_size = (target_end - target_start).max(0.0);
let size = item.axis_size(axis);
let source_offset = item.axis_start(axis) - source_start;
let source_free_space = source_area_size - size;
let target_free_space = target_area_size - size;
let stretches = children
.zip(estimates)
.and_then(|(children, estimates)| children.get(index).zip(estimates.get(index)))
.map(|(child, estimate)| {
frozen_grid_item_stretches_axis(container_style, child, estimate, axis)
})
// Unit-level topology checks have no item styles. Their source
// geometry intentionally describes an already-stretched item.
.unwrap_or((size - source_area_size).abs() < 0.01);
let (start, size) = if stretches {
(target_start, target_area_size)
} else if source_free_space.abs() >= 0.01 {
(
target_start + source_offset / source_free_space * target_free_space,
size,
)
} else {
// A zero-free-space source cannot reveal a self-alignment
// fraction. Preserve its start-side offset; later Spindrift-owned
// self-alignment corrections handle writing-mode-specific cases.
(target_start + source_offset, size)
};
item.set_axis_geometry(axis, start, size);
}
}
/// Whether the final grid-area sizing step stretches this physical item axis.
/// A frozen pass has already converted collapsed tracks to numeric zeroes, so
/// its reported source area can be wider than the actual CSS area. Determine
/// stretch from the item style instead of inferring it from that source size.
/// <https://www.w3.org/TR/css-grid-1/#grid-item-sizing>
fn frozen_grid_item_stretches_axis(
container_style: &ComputedStyle,
child: &GridChild<'_>,
estimate: &GridItemEstimate,
axis: GridAxis,
) -> bool {
let swaps_axes = WritingModeAxes::new(container_style.writing_mode, container_style.direction)
.swaps_physical_axes();
let (alignment, size_is_auto) = match (axis, swaps_axes) {
(GridAxis::Column, false) => (
effective_grid_justify_self(&child.style, container_style).keyword,
child.style.box_values.width.is_auto(),
),
(GridAxis::Row, false) => (
effective_grid_align_self(&child.style, container_style).keyword,
child.style.box_values.height.value().is_auto(),
),
(GridAxis::Column, true) => (
effective_grid_align_self(&child.style, container_style).keyword,
child.style.box_values.width.is_auto(),
),
(GridAxis::Row, true) => (
effective_grid_justify_self(&child.style, container_style).keyword,
child.style.box_values.height.value().is_auto(),
),
};
if !size_is_auto {
return false;
}
if alignment == SelfAlignmentKeyword::Stretch {
return true;
}
alignment == SelfAlignmentKeyword::Normal
&& estimate.replaced_used_size.is_none()
&& child
.style
.aspect_ratio
.preferred_ratio_for_non_replaced(false)
.is_none()
}
/// Convert Taffy 0.14's final track positions into Spindrift's canonical track
/// extents and interior gutters.
///
/// The positions include content alignment. Spindrift's subsequent static-position
/// and replay paths deliberately apply content distribution from the canonical
/// unaligned topology, so retain Taffy's used track sizes but the pre-alignment
/// CSS gutter at this boundary.
fn taffy_grid_track_layout_from_positions(
positions: &[taffy_layout::Line<f32>],
gap: f32,
) -> (Vec<f32>, Vec<f32>) {
let sizes = positions
.iter()
.map(|track| (track.end - track.start).max(0.0))
.collect();
let gutters = vec![gap.max(0.0); positions.len().saturating_sub(1)];
(sizes, gutters)
}
/// Preserve Taffy's already-physical track bounds while converting its track
/// vector from backend-logical to increasing physical order.
fn physical_grid_topology_from_taffy_positions(
positions: &[taffy_layout::Line<f32>],
reversed: bool,
) -> Option<GridAxisTopology> {
if reversed {
GridAxisTopology::from_track_geometry(
positions
.iter()
.rev()
.map(|track| (track.start, track.end, false)),
)
} else {
GridAxisTopology::from_track_geometry(
positions
.iter()
.map(|track| (track.start, track.end, false)),
)
}
}
/// Convert Taffy's historical alternating grid-track representation to the
/// interior gutters used by Spindrift's line geometry. Taffy 0.14 already yields
/// interior gutters through `taffy_grid_track_layout_from_positions`, while
/// this normalizer retains compatibility with frozen topology corrections.
fn taffy_grid_track_gutters(taffy_gutters: &[f32], track_count: usize) -> Vec<f32> {
let interior_gutter_count = track_count.saturating_sub(1);
if taffy_gutters.len() == track_count.saturating_add(1) {
taffy_gutters
.iter()
.skip(1)
.take(interior_gutter_count)
.copied()
.collect()
} else {
// Preserve compatibility with the older detailed-layout shape, which
// already exposed only interior gutters.
taffy_gutters
.iter()
.take(interior_gutter_count)
.copied()
.collect()
}
}
fn auto_fit_collapsed_track_mask(
style: &ComputedStyle,
axis: GridAxis,
adjustment: &StartwardImplicitTrackAdjustment,
track_count: usize,
item_areas: &[GridItemArea],
) -> Vec<bool> {
let mut collapsed = vec![false; track_count];
let Some(range) = auto_fit_track_range_with_startward_adjustment(style, axis, adjustment)
else {
return collapsed;
};
for track_index in range.filter(|index| *index < track_count) {
collapsed[track_index] = !grid_track_has_item(axis, track_index, item_areas);
}
collapsed
}
/// Derive the physical percentage containing blocks from resolved grid-area
/// boundaries. The line offsets include all crossed effective gutters.
fn grid_item_containing_block_bases(
style: &ComputedStyle,
layout: &GridLayout,
) -> Vec<GridItemContainingBlockBases> {
let swaps_axes =
WritingModeAxes::new(style.writing_mode, style.used_direction()).swaps_physical_axes();
let width_source = if swaps_axes {
GridAvailableSizeSource::GridItemContainingBlockBlock
} else {
GridAvailableSizeSource::GridItemContainingBlockInline
};
let height_source = if swaps_axes {
GridAvailableSizeSource::GridItemContainingBlockInline
} else {
GridAvailableSizeSource::GridItemContainingBlockBlock
};
layout
.items
.iter()
.map(|item| {
let Some(area) = item.area else {
return GridItemContainingBlockBases {
width: GridPercentageBasis::indefinite(),
height: GridPercentageBasis::indefinite(),
};
};
GridItemContainingBlockBases {
width: grid_percentage_basis(
grid_area_axis_extent(&layout.columns, area.column_start, area.column_end)
.map(content_box_pt),
width_source,
),
height: grid_percentage_basis(
grid_area_axis_extent(&layout.rows, area.row_start, area.row_end)
.map(content_box_pt),
height_source,
),
}
})
.collect()
}
fn grid_area_axis_extent(
topology: &GridAxisTopology,
start_line: u16,
end_line: u16,
) -> Option<f32> {
let (start, end) = topology.area_bounds(start_line, end_line)?;
Some((end - start).max(0.0))
}
fn grid_item_placement_overrides(
style: &ComputedStyle,
layout: &GridLayout,
) -> Vec<Option<GridItemArea>> {
let reverse_columns = !WritingModeAxes::new(style.writing_mode, style.used_direction())
.swaps_physical_axes()
&& style.used_direction() == Direction::Rtl;
let column_track_count = layout.columns.track_count();
layout
.items
.iter()
.map(|item| {
item.area.and_then(|area| {
if reverse_columns {
area.with_reversed_axis(GridAxis::Column, column_track_count)
} else {
Some(area)
}
})
})
.collect()
}
/// CSS Grid's feedback decision is based on the relevant logical min-content
/// contribution. The corresponding max-content value is nevertheless passed
/// through the correction once the min-content value changed.
fn grid_inline_contributions_changed(
previous: &[GridItemEstimate],
next: &[GridItemEstimate],
) -> bool {
previous.iter().zip(next).any(|(previous, next)| {
(previous.min_width.points() - next.min_width.points()).abs() > 0.01
})
}
fn grid_block_contributions_changed(
previous: &[GridItemEstimate],
next: &[GridItemEstimate],
) -> bool {
previous.iter().zip(next).any(|(previous, next)| {
(previous.min_height.points() - next.min_height.points()).abs() > 0.01
})
}
/// Convert a preliminary physical Grid area into a fixed Taffy placement for
/// the proxy sizing pass. The area was produced by the preceding placement
/// pass, so its lines are valid Taffy grid lines.
fn taffy_grid_area_line(
area: GridItemArea,
axis: GridAxis,
) -> Option<taffy_layout::Line<taffy_layout::GridPlacement<String>>> {
let (start, end) = match axis {
GridAxis::Column => (area.column_start, area.column_end),
GridAxis::Row => (area.row_start, area.row_end),
};
Some(taffy_layout::Line {
start: taffy_layout::line(i16::try_from(start).ok()?),
end: taffy_layout::line(i16::try_from(end).ok()?),
})
}
/// Freeze a resolved physical track topology for a bounded opposite-axis
/// correction pass.
fn taffy_fixed_grid_tracks(sizes: &[f32]) -> Vec<taffy_layout::GridTemplateComponent<String>> {
sizes
.iter()
.map(|size| {
let size = size.max(0.0);
taffy_layout::GridTemplateComponent::Single(taffy_layout::TrackSizingFunction {
min: taffy_layout::MinTrackSizingFunction::length(size),
max: taffy_layout::MaxTrackSizingFunction::length(size),
})
})
.collect()
}
#[derive(Default)]
struct GridAxisCorrections {
columns: Option<GridAxisCorrection>,
rows: Option<GridAxisCorrection>,
}
struct GridAxisCorrection {
source: CorrectionSource,
target: GridAxisTopology,
preserved_item_geometry: Option<Vec<FrozenGridItemAxisGeometry>>,
}
enum CorrectionSource {
Aligned(GridAxisTopology),
Unaligned(GridAxisTopology),
}
impl CorrectionSource {
fn reversed(&self) -> Self {
match self {
Self::Aligned(topology) => Self::Aligned(topology.reversed()),
Self::Unaligned(topology) => Self::Unaligned(topology.reversed()),
}
}
fn area_bounds(
&self,
content_alignment: css::ContentAlignment,
container_size: f32,
start_line: u16,
end_line: u16,
) -> Option<(f32, f32)> {
match self {
Self::Aligned(topology) => topology.area_bounds(start_line, end_line),
Self::Unaligned(topology) => topology.aligned_area_bounds(
content_alignment,
container_size,
start_line,
end_line,
),
}
}
}
/// Collapse empty frozen `auto-fit` tracks after startward implicit expansion.
///
/// Spindrift freezes a definite auto-repeat count before prepending startward
/// implicit tracks so the authored repeat count is not recomputed from the
/// enlarged Taffy template. CSS Grid still requires empty `auto-fit` repeated
/// tracks to collapse before content alignment, so Spindrift mirrors that part of
/// the used-track geometry after Taffy returns detailed track data:
/// <https://www.w3.org/TR/css-grid-1/#auto-repeat>.
fn startward_auto_fit_track_correction(
style: &ComputedStyle,
axis: GridAxis,
adjustment: &StartwardImplicitTrackAdjustment,
sizes: &[f32],
gutters: &[f32],
item_areas: &[GridItemArea],
) -> Option<GridAxisCorrection> {
if !adjustment.has_startward_tracks() {
return None;
}
let range = auto_fit_track_range_with_startward_adjustment(style, axis, adjustment)?;
if range.end > sizes.len() {
return None;
}
let mut collapsed = vec![false; sizes.len()];
for track_index in range {
if !grid_track_has_item(axis, track_index, item_areas) {
collapsed[track_index] = true;
}
}
if !collapsed.iter().any(|collapsed| *collapsed) {
return None;
}
let mut corrected_sizes = sizes.to_vec();
for (index, size) in corrected_sizes.iter_mut().enumerate() {
if collapsed[index] {
*size = 0.0;
}
}
Some(GridAxisCorrection {
source: CorrectionSource::Unaligned(GridAxisTopology::from_track_layout(
sizes.to_vec(),
gutters.to_vec(),
vec![false; sizes.len()],
)?),
target: GridAxisTopology::from_auto_fit_track_layout(
corrected_sizes,
gutters.to_vec(),
collapsed,
)?,
preserved_item_geometry: None,
})
}
fn grid_track_has_item(axis: GridAxis, track_index: usize, item_areas: &[GridItemArea]) -> bool {
item_areas.iter().any(|area| {
let (start, end) = match axis {
GridAxis::Column => (usize::from(area.column_start), usize::from(area.column_end)),
GridAxis::Row => (usize::from(area.row_start), usize::from(area.row_end)),
};
let start = start.saturating_sub(1);
let end = end.saturating_sub(1);
start <= track_index && track_index < end
})
}
fn apply_auto_fit_track_corrections(
style: &ComputedStyle,
container_width: PhysicalContentWidth,
container_height: f32,
corrections: &GridAxisCorrections,
apply_column_correction: bool,
apply_row_correction: bool,
items: &mut [GridItemLayout],
) {
for item in items {
let Some(area) = item.area else {
continue;
};
if apply_column_correction && let Some(correction) = &corrections.columns {
apply_track_layout_correction_axis(
correction,
style.justify_content,
container_width.points(),
usize::from(area.column_start).saturating_sub(1),
usize::from(area.column_end).saturating_sub(1),
item,
GridAxis::Column,
);
}
if apply_row_correction && let Some(correction) = &corrections.rows {
apply_track_layout_correction_axis(
correction,
style.align_content,
container_height,
usize::from(area.row_start).saturating_sub(1),
usize::from(area.row_end).saturating_sub(1),
item,
GridAxis::Row,
);
}
}
}
fn apply_track_layout_correction_axis(
correction: &GridAxisCorrection,
content_alignment: css::ContentAlignment,
container_size: f32,
start_line: usize,
end_line: usize,
item: &mut GridItemLayout,
axis: GridAxis,
) {
let Ok(start_line) = u16::try_from(start_line + 1) else {
return;
};
let Ok(end_line) = u16::try_from(end_line + 1) else {
return;
};
let original_bounds =
correction
.source
.area_bounds(content_alignment, container_size, start_line, end_line);
let Some((original_start, original_end)) = original_bounds else {
return;
};
let Some((corrected_start, corrected_end)) = correction.target.aligned_area_bounds(
content_alignment,
container_size,
start_line,
end_line,
) else {
return;
};
let original_area_size = (original_end - original_start).max(0.0);
let corrected_area_size = (corrected_end - corrected_start).max(0.0);
let offset_in_area = if (item.axis_size(axis) - original_area_size).abs() >= 0.01
&& matches!(
content_alignment.keyword,
css::ContentAlignmentKeyword::SpaceBetween
| css::ContentAlignmentKeyword::SpaceAround
| css::ContentAlignmentKeyword::SpaceEvenly
) {
// Distributed alignment re-centres a fixed-size item when collapsed
// tracks shorten its source area. Positional alignment preserves the
// Taffy offset, including the negative overflow shift of a span that
// fills the area.
item.axis_start(axis) - original_start + (corrected_area_size - original_area_size) / 2.0
} else {
item.axis_start(axis) - original_start
};
let mut size = item.axis_size(axis);
if (size - original_area_size).abs() < 0.01 {
size = corrected_area_size;
}
item.set_axis_geometry(axis, corrected_start + offset_in_area, size);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn typed_physical_height_drives_percentage_gap_relayout() {
let gap =
css::ComputedGap::LengthPercentage(css::ComputedLengthPercentage::from_percent(0.5));
let height = PhysicalContentHeight::new(content_box_pt(80.0));
assert!(grid_gap_resolves_differently_with_basis(gap, height));
}
fn track(min: css::GridMinTrackBreadth, max: css::GridMaxTrackBreadth) -> css::GridTrackSize {
css::GridTrackSize { min, max }
}
fn grid_tracks(tracks: Vec<css::GridTrackSize>) -> css::GridTrackList {
css::GridTrackList::Tracks {
components: tracks
.into_iter()
.map(|track| css::GridTrackListComponent::Track(Vec::new(), track))
.collect(),
trailing_names: Vec::new(),
}
}
fn feedback_test_layout(area: GridItemArea) -> GridLayout {
GridLayout {
height: PhysicalContentHeight::new(content_box_pt(80.0)),
baselines: PhysicalBaselineSets::default(),
first_baseline: None,
last_baseline: None,
items: vec![GridItemLayout::new(
GridRect::new(GridPoint::new(0.0, 0.0), GridSize::new(0.0, 0.0)),
Some(area),
)],
baseline_resolutions: Vec::new(),
// These topologies deliberately include unequal crossed gutters.
columns: GridAxisTopology::from_line_offsets(
vec![3.0, 13.0, 29.0, 51.0],
vec![10.0, 12.0, 15.0],
vec![false; 3],
)
.unwrap(),
rows: GridAxisTopology::from_line_offsets(
vec![5.0, 19.0, 40.0, 68.0],
vec![14.0, 16.0, 20.0],
vec![false; 3],
)
.unwrap(),
content_width: 48.0,
content_height: 63.0,
column_line_names: Vec::new(),
row_line_names: Vec::new(),
}
}
#[test]
fn grid_area_bases_include_spans_and_crossed_gutters() {
let layout = feedback_test_layout(GridItemArea {
row_start: 2,
row_end: 4,
column_start: 1,
column_end: 3,
});
let bases = grid_item_containing_block_bases(&ComputedStyle::initial(), &layout);
assert_eq!(bases.len(), 1);
assert_eq!(bases[0].width.points(), Some(22.0));
assert_eq!(bases[0].height.points(), Some(41.0));
assert!(matches!(
bases[0].width,
PercentageBasis::Definite {
source: GridAvailableSizeSource::GridItemContainingBlockInline,
..
}
));
assert!(matches!(
bases[0].height,
PercentageBasis::Definite {
source: GridAvailableSizeSource::GridItemContainingBlockBlock,
..
}
));
}
#[test]
fn vertical_grid_area_bases_retain_physical_extents_and_swap_provenance() {
let layout = feedback_test_layout(GridItemArea {
row_start: 2,
row_end: 4,
column_start: 1,
column_end: 3,
});
let mut style = ComputedStyle::initial();
style.writing_mode = WritingMode::VerticalLr;
let bases = grid_item_containing_block_bases(&style, &layout);
assert_eq!(bases[0].width.points(), Some(22.0));
assert_eq!(bases[0].height.points(), Some(41.0));
assert!(matches!(
bases[0].width,
PercentageBasis::Definite {
source: GridAvailableSizeSource::GridItemContainingBlockBlock,
..
}
));
assert!(matches!(
bases[0].height,
PercentageBasis::Definite {
source: GridAvailableSizeSource::GridItemContainingBlockInline,
..
}
));
}
#[test]
fn grid_feedback_corrections_are_tolerance_bounded_and_axis_specific() {
let initial = [GridItemEstimate::fixed(10.0, 20.0)];
let within_tolerance = [GridItemEstimate::fixed(10.005, 20.005)];
let corrected = [GridItemEstimate::fixed(10.02, 20.02)];
assert!(!grid_inline_contributions_changed(
&initial,
&within_tolerance
));
assert!(!grid_block_contributions_changed(
&initial,
&within_tolerance
));
assert!(grid_inline_contributions_changed(&initial, &corrected));
assert!(grid_block_contributions_changed(&initial, &corrected));
}
#[test]
fn frozen_feedback_topology_preserves_placement_for_one_correction_per_axis() {
let layout = feedback_test_layout(GridItemArea {
row_start: 2,
row_end: 3,
column_start: 2,
column_end: 4,
});
let placement = grid_item_placement_overrides(&ComputedStyle::initial(), &layout);
let columns = GridFrozenTrackTopology {
columns: Some(GridFrozenTrackAxis::from_layout(&layout, GridAxis::Column)),
rows: None,
};
let rows = GridFrozenTrackTopology {
columns: None,
rows: Some(GridFrozenTrackAxis::from_layout(&layout, GridAxis::Row)),
};
let preserved_area = placement[0].expect("placed item retains its grid area");
assert_eq!(preserved_area.row_start, 2);
assert_eq!(preserved_area.row_end, 3);
assert_eq!(preserved_area.column_start, 2);
assert_eq!(preserved_area.column_end, 4);
assert_eq!(
columns
.columns
.as_ref()
.map(|axis| axis.topology.track_sizes()),
Some(layout.columns.track_sizes())
);
assert_eq!(
rows.rows.as_ref().map(|axis| axis.topology.track_sizes()),
Some(layout.rows.track_sizes())
);
assert_eq!(
taffy_fixed_grid_tracks(&columns.columns.as_ref().unwrap().topology.track_sizes())
.len(),
3
);
assert_eq!(
taffy_fixed_grid_tracks(&rows.rows.as_ref().unwrap().topology.track_sizes()).len(),
3
);
}
#[test]
fn frozen_auto_fit_rebases_space_around_using_only_active_tracks() {
let correction = GridAxisCorrection::from_frozen(
&GridFrozenTrackAxis {
topology: GridAxisTopology::from_track_layout(
vec![15.0, 15.0, 0.0, 0.0, 15.0, 0.0, 15.0, 0.0, 0.0, 0.0],
vec![0.0; 9],
vec![
false, false, true, true, false, true, false, true, true, true,
],
)
.unwrap(),
item_geometry: Vec::new(),
},
&GridAxisTopology::from_line_offsets(
vec![
0.0, 15.0, 30.0, 30.0, 30.0, 45.0, 45.0, 60.0, 60.0, 60.0, 60.0,
],
vec![15.0, 15.0, 0.0, 0.0, 15.0, 0.0, 15.0, 0.0, 0.0, 0.0],
vec![false; 10],
)
.unwrap(),
)
.expect("collapsed auto-fit tracks need a frozen correction");
let mut items = [
GridItemLayout::new(
GridRect::new(GridPoint::new(4.5, 0.0), GridSize::new(15.0, 15.0)),
Some(GridItemArea {
row_start: 1,
row_end: 2,
column_start: 1,
column_end: 2,
}),
),
GridItemLayout::new(
GridRect::new(GridPoint::new(28.5, 0.0), GridSize::new(15.0, 15.0)),
Some(GridItemArea {
row_start: 1,
row_end: 2,
column_start: 2,
column_end: 3,
}),
),
GridItemLayout::new(
GridRect::new(GridPoint::new(70.5, 0.0), GridSize::new(15.0, 15.0)),
Some(GridItemArea {
row_start: 1,
row_end: 2,
column_start: 5,
column_end: 6,
}),
),
GridItemLayout::new(
GridRect::new(GridPoint::new(103.5, 0.0), GridSize::new(15.0, 15.0)),
Some(GridItemArea {
row_start: 1,
row_end: 2,
column_start: 7,
column_end: 8,
}),
),
];
apply_frozen_grid_axis_correction(
Some(&correction),
css::ContentAlignment::new(css::ContentAlignmentKeyword::SpaceAround),
150.0,
GridAxis::Column,
&ComputedStyle::initial(),
None,
None,
&mut items,
);
assert_eq!(
items.iter().map(GridItemLayout::x).collect::<Vec<_>>(),
vec![11.25, 48.75, 86.25, 123.75]
);
}
#[test]
fn frozen_topology_uses_canonical_auto_fit_gutter_geometry() {
let collapsed_tracks = vec![false, true, false, false, true];
let axis = GridFrozenTrackAxis {
topology: GridAxisTopology::from_auto_fit_track_layout(
vec![0.0, 0.0, 15.0, 15.0, 0.0],
vec![7.0, 8.0, 9.0, 10.0],
collapsed_tracks,
)
.unwrap(),
item_geometry: Vec::new(),
};
assert_eq!(
axis.topology.line_offsets(),
vec![0.0, 7.0, 7.0, 31.0, 46.0, 46.0]
);
}
#[test]
fn taffy_gutter_adapter_discards_boundary_gutters() {
assert_eq!(
taffy_grid_track_gutters(&[0.0, 7.5, 0.0, 7.5, 0.0], 4),
vec![7.5, 0.0, 7.5]
);
assert_eq!(taffy_grid_track_gutters(&[7.5, 0.0], 3), vec![7.5, 0.0]);
}
#[test]
fn taffy_track_positions_preserve_rtl_ordered_track_and_gutter_geometry() {
// Taffy reports physical final positions, including an RTL grid's
// visual reversal. The bridge retains that exact track order and
// derives only the interior gaps needed by Spindrift's logical replay.
let positions = [
taffy_layout::Line {
start: 70.0,
end: 100.0,
},
taffy_layout::Line {
start: 55.0,
end: 65.0,
},
taffy_layout::Line {
start: 30.0,
end: 50.0,
},
];
let (sizes, gutters) = taffy_grid_track_layout_from_positions(&positions, 5.0);
assert_eq!(sizes, vec![30.0, 10.0, 20.0]);
// Content alignment can expand the final physical distance between
// RTL tracks. The topology retains the authored gutter and lets the
// existing logical replay apply alignment once.
assert_eq!(gutters, vec![5.0, 5.0]);
let physical = physical_grid_topology_from_taffy_positions(&positions, true).unwrap();
assert_eq!(physical.track_sizes(), vec![20.0, 10.0, 30.0]);
assert_eq!(physical.interior_gutters(), vec![5.0, 5.0]);
assert_eq!(physical.area_bounds(1, 2), Some((30.0, 50.0)));
assert_eq!(physical.area_bounds(3, 4), Some((70.0, 100.0)));
}
#[test]
fn frozen_topology_distributes_only_non_collapsed_tracks() {
let offsets = vec![0.0, 22.5, 22.5, 45.0, 60.0, 60.0, 60.0, 60.0];
let collapsed = vec![false, true, false, false, true, true, true];
let positions = |keyword| {
[0, 2, 3]
.into_iter()
.map(|line| {
content_aligned_grid_line_offset_with_collapsed_tracks(
css::ContentAlignment::new(keyword),
150.0,
&offsets,
line,
Some(&collapsed),
)
.unwrap()
})
.collect::<Vec<_>>()
};
assert_eq!(
positions(css::ContentAlignmentKeyword::SpaceBetween),
vec![0.0, 67.5, 135.0]
);
assert_eq!(
positions(css::ContentAlignmentKeyword::SpaceAround),
vec![15.0, 67.5, 120.0]
);
assert_eq!(
positions(css::ContentAlignmentKeyword::SpaceEvenly),
vec![22.5, 67.5, 112.5]
);
}
#[test]
fn flexible_track_span_zeros_only_the_automatic_grid_minimum() {
let mut container = ComputedStyle::initial();
container.grid_template_columns = grid_tracks(vec![track(
css::GridMinTrackBreadth::Auto,
css::GridMaxTrackBreadth::Flex(1.0),
)]);
let mut item = ComputedStyle::initial();
item.grid_column_start = css::GridPlacement::Line(css::GridLinePlacement::Number(
std::num::NonZeroI32::new(1).unwrap(),
));
item.grid_column_end = css::GridPlacement::Span(css::GridSpanPlacement::Count(
std::num::NonZeroU16::new(2).unwrap(),
));
assert_eq!(
grid_item_taffy_min_constraint(
css::ComputedLengthPercentageOrAuto::Auto,
GridAxis::Column,
&container,
&item,
content_box_pt(20.0),
content_box_pt(40.0),
),
taffy_layout::LengthPercentageAuto::length(0.0),
);
assert_eq!(
grid_item_taffy_min_constraint(
css::ComputedLengthPercentageOrAuto::LengthPercentage(
css::ComputedLengthPercentage::from_points(12.0),
),
GridAxis::Column,
&container,
&item,
content_box_pt(20.0),
content_box_pt(40.0),
),
taffy_layout::LengthPercentageAuto::length(12.0),
);
}
#[test]
fn feedback_probe_plan_runs_only_the_required_comparisons() {
let invariant = GridItemFeedbackSensitivity {
inline_contribution_may_depend_on_area: false,
block_contribution_may_depend_on_area: false,
};
let inline_only = GridItemFeedbackSensitivity {
inline_contribution_may_depend_on_area: true,
block_contribution_may_depend_on_area: false,
};
let block_only = GridItemFeedbackSensitivity {
inline_contribution_may_depend_on_area: false,
block_contribution_may_depend_on_area: true,
};
let none = GridFeedbackProbePlan::from_sensitivities(&[invariant]);
assert!(!none.needs_inline_comparison);
assert!(!none.needs_block_comparison);
assert!(!none.needs_container_comparison());
let inline = GridFeedbackProbePlan::from_sensitivities(&[invariant, inline_only]);
assert!(inline.needs_inline_comparison);
assert!(!inline.needs_block_comparison);
assert!(inline.needs_container_comparison());
let block = GridFeedbackProbePlan::from_sensitivities(&[invariant, block_only]);
assert!(!block.needs_inline_comparison);
assert!(block.needs_block_comparison);
assert!(block.needs_container_comparison());
}
}