summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/Layout/GridFormattingContext.cpp
blob: b1f6cd894d092aeb6afca2aaadf4672f6a1f012c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
/*
 * Copyright (c) 2022, Martin Falisse <mfalisse@outlook.com>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibWeb/DOM/Node.h>
#include <LibWeb/Layout/Box.h>
#include <LibWeb/Layout/GridFormattingContext.h>

namespace Web::Layout {

GridFormattingContext::GridFormattingContext(LayoutState& state, BlockContainer const& block_container, FormattingContext* parent)
    : BlockFormattingContext(state, block_container, parent)
{
}

GridFormattingContext::~GridFormattingContext() = default;

float GridFormattingContext::resolve_definite_track_size(CSS::GridSize const& grid_size, AvailableSpace const& available_space, Box const& box)
{
    VERIFY(grid_size.is_definite());
    switch (grid_size.type()) {
    case CSS::GridSize::Type::Length:
        if (grid_size.length().is_auto())
            break;
        return grid_size.length().to_px(box);
    case CSS::GridSize::Type::Percentage:
        if (available_space.width.is_definite())
            return grid_size.percentage().as_fraction() * available_space.width.to_px().value();
        break;
    default:
        VERIFY_NOT_REACHED();
    }
    return 0;
}

size_t GridFormattingContext::count_of_gap_columns()
{
    size_t count = 0;
    for (auto& grid_column : m_grid_columns) {
        if (grid_column.is_gap)
            count++;
    }
    return count;
}

size_t GridFormattingContext::count_of_gap_rows()
{
    size_t count = 0;
    for (auto& grid_row : m_grid_rows) {
        if (grid_row.is_gap)
            count++;
    }
    return count;
}

float GridFormattingContext::resolve_size(CSS::Size const& size, AvailableSize const& available_size, Box const& box)
{
    if (size.is_length() && size.length().is_calculated()) {
        if (size.length().calculated_style_value()->contains_percentage()) {
            if (!available_size.is_definite())
                return 0;
            auto& calc_value = *size.length().calculated_style_value();
            return calc_value.resolve_length_percentage(box, CSS::Length::make_px(available_size.to_px())).value_or(CSS::Length::make_auto()).to_px(box);
        }
        return size.length().to_px(box);
    }
    if (size.is_length()) {
        return size.length().to_px(box);
    }
    if (size.is_percentage()) {
        if (!available_size.is_definite())
            return 0;
        return available_size.to_px().value() * size.percentage().as_fraction();
    }
    return 0;
}

int GridFormattingContext::get_count_of_tracks(Vector<CSS::ExplicitGridTrack> const& track_list, AvailableSpace const& available_space, Box const& box)
{
    auto track_count = 0;
    for (auto const& explicit_grid_track : track_list) {
        if (explicit_grid_track.is_repeat() && explicit_grid_track.repeat().is_default())
            track_count += explicit_grid_track.repeat().repeat_count() * explicit_grid_track.repeat().grid_track_size_list().track_list().size();
        else
            track_count += 1;
    }

    if (track_list.size() == 1
        && track_list.first().is_repeat()
        && (track_list.first().repeat().is_auto_fill() || track_list.first().repeat().is_auto_fit())) {
        track_count = count_of_repeated_auto_fill_or_fit_tracks(track_list, available_space, box);
    }

    return track_count;
}

int GridFormattingContext::count_of_repeated_auto_fill_or_fit_tracks(Vector<CSS::ExplicitGridTrack> const& track_list, AvailableSpace const& available_space, Box const& box)
{
    // https://www.w3.org/TR/css-grid-2/#auto-repeat
    // 7.2.3.2. Repeat-to-fill: auto-fill and auto-fit repetitions
    // On a subgridded axis, the auto-fill keyword is only valid once per <line-name-list>, and repeats
    // enough times for the name list to match the subgrid’s specified grid span (falling back to 0 if
    // the span is already fulfilled).

    // Otherwise on a standalone axis, when auto-fill is given as the repetition number
    // If the grid container has a definite size or max size in the relevant axis, then the number of
    // repetitions is the largest possible positive integer that does not cause the grid to overflow the
    // content box of its grid container

    auto sum_of_grid_track_sizes = 0;
    // (treating each track as its max track sizing function if that is definite or its minimum track sizing
    // function otherwise, flooring the max track sizing function by the min track sizing function if both
    // are definite, and taking gap into account)
    // FIXME: take gap into account
    for (auto& explicit_grid_track : track_list.first().repeat().grid_track_size_list().track_list()) {
        auto track_sizing_function = explicit_grid_track;
        if (track_sizing_function.is_minmax()) {
            if (track_sizing_function.minmax().max_grid_size().is_definite() && !track_sizing_function.minmax().min_grid_size().is_definite())
                sum_of_grid_track_sizes += resolve_definite_track_size(track_sizing_function.minmax().max_grid_size(), available_space, box);
            else if (track_sizing_function.minmax().min_grid_size().is_definite() && !track_sizing_function.minmax().max_grid_size().is_definite())
                sum_of_grid_track_sizes += resolve_definite_track_size(track_sizing_function.minmax().min_grid_size(), available_space, box);
            else if (track_sizing_function.minmax().min_grid_size().is_definite() && track_sizing_function.minmax().max_grid_size().is_definite())
                sum_of_grid_track_sizes += min(resolve_definite_track_size(track_sizing_function.minmax().min_grid_size(), available_space, box), resolve_definite_track_size(track_sizing_function.minmax().max_grid_size(), available_space, box));
        } else {
            sum_of_grid_track_sizes += min(resolve_definite_track_size(track_sizing_function.grid_size(), available_space, box), resolve_definite_track_size(track_sizing_function.grid_size(), available_space, box));
        }
    }
    return max(1, static_cast<int>(get_free_space_x(available_space) / sum_of_grid_track_sizes));

    // For the purpose of finding the number of auto-repeated tracks in a standalone axis, the UA must
    // floor the track size to a UA-specified value to avoid division by zero. It is suggested that this
    // floor be 1px.
}

void GridFormattingContext::place_item_with_row_and_column_position(Box const& box, Box const& child_box)
{
    int row_start = child_box.computed_values().grid_row_start().raw_value();
    int row_end = child_box.computed_values().grid_row_end().raw_value();
    int column_start = child_box.computed_values().grid_column_start().raw_value();
    int column_end = child_box.computed_values().grid_column_end().raw_value();

    // https://www.w3.org/TR/css-grid-2/#line-placement
    // 8.3. Line-based Placement: the grid-row-start, grid-column-start, grid-row-end, and grid-column-end properties

    // https://www.w3.org/TR/css-grid-2/#grid-placement-slot
    // First attempt to match the grid area’s edge to a named grid area: if there is a grid line whose
    // line name is <custom-ident>-start (for grid-*-start) / <custom-ident>-end (for grid-*-end),
    // contributes the first such line to the grid item’s placement.

    // Otherwise, treat this as if the integer 1 had been specified along with the <custom-ident>.

    // https://www.w3.org/TR/css-grid-2/#grid-placement-int
    // Contributes the Nth grid line to the grid item’s placement. If a negative integer is given, it
    // instead counts in reverse, starting from the end edge of the explicit grid.
    if (row_end < 0)
        row_end = m_occupation_grid.row_count() + row_end + 2;
    if (column_end < 0)
        column_end = m_occupation_grid.column_count() + column_end + 2;

    // If a name is given as a <custom-ident>, only lines with that name are counted. If not enough
    // lines with that name exist, all implicit grid lines are assumed to have that name for the purpose
    // of finding this position.

    // https://www.w3.org/TR/css-grid-2/#grid-placement-span-int
    // Contributes a grid span to the grid item’s placement such that the corresponding edge of the grid
    // item’s grid area is N lines from its opposite edge in the corresponding direction. For example,
    // grid-column-end: span 2 indicates the second grid line in the endward direction from the
    // grid-column-start line.
    int row_span = 1;
    int column_span = 1;
    if (child_box.computed_values().grid_row_start().is_position() && child_box.computed_values().grid_row_end().is_span())
        row_span = child_box.computed_values().grid_row_end().raw_value();
    if (child_box.computed_values().grid_column_start().is_position() && child_box.computed_values().grid_column_end().is_span())
        column_span = child_box.computed_values().grid_column_end().raw_value();
    if (child_box.computed_values().grid_row_end().is_position() && child_box.computed_values().grid_row_start().is_span()) {
        row_span = child_box.computed_values().grid_row_start().raw_value();
        row_start = row_end - row_span;
    }
    if (child_box.computed_values().grid_column_end().is_position() && child_box.computed_values().grid_column_start().is_span()) {
        column_span = child_box.computed_values().grid_column_start().raw_value();
        column_start = column_end - column_span;
    }

    // If a name is given as a <custom-ident>, only lines with that name are counted. If not enough
    // lines with that name exist, all implicit grid lines on the side of the explicit grid
    // corresponding to the search direction are assumed to have that name for the purpose of counting
    // this span.

    // https://drafts.csswg.org/css-grid/#grid-placement-auto
    // auto
    // The property contributes nothing to the grid item’s placement, indicating auto-placement or a
    // default span of one. (See § 8 Placing Grid Items, above.)

    // https://www.w3.org/TR/css-grid-2/#common-uses-named-lines
    // 8.1.3. Named Lines and Spans
    // Instead of counting lines by number, lines can be referenced by their line name:
    if (child_box.computed_values().grid_column_start().has_line_name()) {
        auto found_flag_and_index = get_line_index_by_line_name(child_box.computed_values().grid_column_start().line_name(), box.computed_values().grid_template_columns());
        if (found_flag_and_index > -1)
            column_start = 1 + found_flag_and_index;
        else
            column_start = 1; // FIXME
    }
    if (child_box.computed_values().grid_column_end().has_line_name()) {
        auto found_flag_and_index = get_line_index_by_line_name(child_box.computed_values().grid_column_end().line_name(), box.computed_values().grid_template_columns());
        if (found_flag_and_index > -1) {
            column_end = 1 + found_flag_and_index;
            if (!child_box.computed_values().grid_column_start().is_position())
                column_start = column_end - column_span;
        } else {
            column_end = 2;   // FIXME
            column_start = 1; // FIXME
        }
    }
    if (child_box.computed_values().grid_row_start().has_line_name()) {
        auto found_flag_and_index = get_line_index_by_line_name(child_box.computed_values().grid_row_start().line_name(), box.computed_values().grid_template_rows());
        if (found_flag_and_index > -1)
            row_start = 1 + found_flag_and_index;
        else
            row_start = 1; // FIXME
    }
    if (child_box.computed_values().grid_row_end().has_line_name()) {
        auto found_flag_and_index = get_line_index_by_line_name(child_box.computed_values().grid_row_end().line_name(), box.computed_values().grid_template_rows());
        if (found_flag_and_index > -1) {
            row_end = 1 + found_flag_and_index;
            if (!child_box.computed_values().grid_row_start().is_position())
                row_start = row_end - row_span;
        } else {
            row_end = 2;   // FIXME
            row_start = 1; // FIXME
        }
    }

    // If there are multiple lines of the same name, they effectively establish a named set of grid
    // lines, which can be exclusively indexed by filtering the placement by name:

    // https://drafts.csswg.org/css-grid/#grid-placement-errors
    // 8.3.1. Grid Placement Conflict Handling
    // If the placement for a grid item contains two lines, and the start line is further end-ward than
    // the end line, swap the two lines. If the start line is equal to the end line, remove the end
    // line.
    if (child_box.computed_values().grid_row_start().is_position() && child_box.computed_values().grid_row_end().is_position()) {
        if (row_start > row_end)
            swap(row_start, row_end);
        if (row_start != row_end)
            row_span = row_end - row_start;
    }
    if (child_box.computed_values().grid_column_start().is_position() && child_box.computed_values().grid_column_end().is_position()) {
        if (column_start > column_end)
            swap(column_start, column_end);
        if (column_start != column_end)
            column_span = column_end - column_start;
    }

    // If the placement contains two spans, remove the one contributed by the end grid-placement
    // property.
    if (child_box.computed_values().grid_row_start().is_span() && child_box.computed_values().grid_row_end().is_span())
        row_span = child_box.computed_values().grid_row_start().raw_value();
    if (child_box.computed_values().grid_column_start().is_span() && child_box.computed_values().grid_column_end().is_span())
        column_span = child_box.computed_values().grid_column_start().raw_value();

    // FIXME: If the placement contains only a span for a named line, replace it with a span of 1.

    row_start -= 1;
    column_start -= 1;
    m_positioned_boxes.append({ child_box, row_start, row_span, column_start, column_span });

    m_occupation_grid.maybe_add_row(row_start + 1);
    m_occupation_grid.maybe_add_column(column_start + 1);
    m_occupation_grid.set_occupied(column_start, column_start + column_span, row_start, row_start + row_span);
}

void GridFormattingContext::place_item_with_row_position(Box const& box, Box const& child_box)
{
    int row_start = child_box.computed_values().grid_row_start().raw_value();
    int row_end = child_box.computed_values().grid_row_end().raw_value();

    // https://www.w3.org/TR/css-grid-2/#line-placement
    // 8.3. Line-based Placement: the grid-row-start, grid-column-start, grid-row-end, and grid-column-end properties

    // https://www.w3.org/TR/css-grid-2/#grid-placement-slot
    // First attempt to match the grid area’s edge to a named grid area: if there is a grid line whose
    // line name is <custom-ident>-start (for grid-*-start) / <custom-ident>-end (for grid-*-end),
    // contributes the first such line to the grid item’s placement.

    // Otherwise, treat this as if the integer 1 had been specified along with the <custom-ident>.

    // https://www.w3.org/TR/css-grid-2/#grid-placement-int
    // Contributes the Nth grid line to the grid item’s placement. If a negative integer is given, it
    // instead counts in reverse, starting from the end edge of the explicit grid.
    if (row_end < 0)
        row_end = m_occupation_grid.row_count() + row_end + 2;

    // If a name is given as a <custom-ident>, only lines with that name are counted. If not enough
    // lines with that name exist, all implicit grid lines are assumed to have that name for the purpose
    // of finding this position.

    // https://www.w3.org/TR/css-grid-2/#grid-placement-span-int
    // Contributes a grid span to the grid item’s placement such that the corresponding edge of the grid
    // item’s grid area is N lines from its opposite edge in the corresponding direction. For example,
    // grid-column-end: span 2 indicates the second grid line in the endward direction from the
    // grid-column-start line.
    int row_span = 1;
    if (child_box.computed_values().grid_row_start().is_position() && child_box.computed_values().grid_row_end().is_span())
        row_span = child_box.computed_values().grid_row_end().raw_value();
    if (child_box.computed_values().grid_row_end().is_position() && child_box.computed_values().grid_row_start().is_span()) {
        row_span = child_box.computed_values().grid_row_start().raw_value();
        row_start = row_end - row_span;
        // FIXME: Remove me once have implemented spans overflowing into negative indexes, e.g., grid-row: span 2 / 1
        if (row_start < 0)
            row_start = 1;
    }

    // If a name is given as a <custom-ident>, only lines with that name are counted. If not enough
    // lines with that name exist, all implicit grid lines on the side of the explicit grid
    // corresponding to the search direction are assumed to have that name for the purpose of counting
    // this span.

    // https://drafts.csswg.org/css-grid/#grid-placement-auto
    // auto
    // The property contributes nothing to the grid item’s placement, indicating auto-placement or a
    // default span of one. (See § 8 Placing Grid Items, above.)

    // https://www.w3.org/TR/css-grid-2/#common-uses-named-lines
    // 8.1.3. Named Lines and Spans
    // Instead of counting lines by number, lines can be referenced by their line name:
    if (child_box.computed_values().grid_row_start().has_line_name()) {
        auto found_flag_and_index = get_line_index_by_line_name(child_box.computed_values().grid_row_start().line_name(), box.computed_values().grid_template_rows());
        if (found_flag_and_index > -1)
            row_start = 1 + found_flag_and_index;
        else
            row_start = 1; // FIXME
    }
    if (child_box.computed_values().grid_row_end().has_line_name()) {
        auto found_flag_and_index = get_line_index_by_line_name(child_box.computed_values().grid_row_end().line_name(), box.computed_values().grid_template_rows());
        if (found_flag_and_index > -1) {
            row_end = 1 + found_flag_and_index;
            if (!child_box.computed_values().grid_row_start().is_position())
                row_start = row_end - row_span;
        } else {
            row_start = 1; // FIXME
            row_end = 2;   // FIXME
        }
    }

    // If there are multiple lines of the same name, they effectively establish a named set of grid
    // lines, which can be exclusively indexed by filtering the placement by name:

    // https://drafts.csswg.org/css-grid/#grid-placement-errors
    // 8.3.1. Grid Placement Conflict Handling
    // If the placement for a grid item contains two lines, and the start line is further end-ward than
    // the end line, swap the two lines. If the start line is equal to the end line, remove the end
    // line.
    if (child_box.computed_values().grid_row_start().is_position() && child_box.computed_values().grid_row_end().is_position()) {
        if (row_start > row_end)
            swap(row_start, row_end);
        if (row_start != row_end)
            row_span = row_end - row_start;
    }
    // FIXME: Have yet to find the spec for this.
    if (!child_box.computed_values().grid_row_start().is_position() && child_box.computed_values().grid_row_end().is_position() && row_end == 1)
        row_start = 1;

    // If the placement contains two spans, remove the one contributed by the end grid-placement
    // property.
    if (child_box.computed_values().grid_row_start().is_span() && child_box.computed_values().grid_row_end().is_span())
        row_span = child_box.computed_values().grid_row_start().raw_value();

    // FIXME: If the placement contains only a span for a named line, replace it with a span of 1.

    row_start -= 1;
    m_occupation_grid.maybe_add_row(row_start + row_span);

    int column_start = 0;
    auto column_span = child_box.computed_values().grid_column_start().is_span() ? child_box.computed_values().grid_column_start().raw_value() : 1;
    // https://drafts.csswg.org/css-grid/#auto-placement-algo
    // 8.5. Grid Item Placement Algorithm
    // 3.3. If the largest column span among all the items without a definite column position is larger
    // than the width of the implicit grid, add columns to the end of the implicit grid to accommodate
    // that column span.
    m_occupation_grid.maybe_add_column(column_span);
    bool found_available_column = false;
    for (int column_index = column_start; column_index < m_occupation_grid.column_count(); column_index++) {
        if (!m_occupation_grid.is_occupied(column_index, row_start)) {
            found_available_column = true;
            column_start = column_index;
            break;
        }
    }
    if (!found_available_column) {
        column_start = m_occupation_grid.column_count();
        m_occupation_grid.maybe_add_column(column_start + column_span);
    }
    m_occupation_grid.set_occupied(column_start, column_start + column_span, row_start, row_start + row_span);

    m_positioned_boxes.append({ child_box, row_start, row_span, column_start, column_span });
}

void GridFormattingContext::place_item_with_column_position(Box const& box, Box const& child_box, int& auto_placement_cursor_x, int& auto_placement_cursor_y)
{
    int column_start = child_box.computed_values().grid_column_start().raw_value();
    int column_end = child_box.computed_values().grid_column_end().raw_value();

    // https://www.w3.org/TR/css-grid-2/#line-placement
    // 8.3. Line-based Placement: the grid-row-start, grid-column-start, grid-row-end, and grid-column-end properties

    // https://www.w3.org/TR/css-grid-2/#grid-placement-slot
    // First attempt to match the grid area’s edge to a named grid area: if there is a grid line whose
    // line name is <custom-ident>-start (for grid-*-start) / <custom-ident>-end (for grid-*-end),
    // contributes the first such line to the grid item’s placement.

    // Otherwise, treat this as if the integer 1 had been specified along with the <custom-ident>.

    // https://www.w3.org/TR/css-grid-2/#grid-placement-int
    // Contributes the Nth grid line to the grid item’s placement. If a negative integer is given, it
    // instead counts in reverse, starting from the end edge of the explicit grid.
    if (column_end < 0)
        column_end = m_occupation_grid.column_count() + column_end + 2;

    // If a name is given as a <custom-ident>, only lines with that name are counted. If not enough
    // lines with that name exist, all implicit grid lines are assumed to have that name for the purpose
    // of finding this position.

    // https://www.w3.org/TR/css-grid-2/#grid-placement-span-int
    // Contributes a grid span to the grid item’s placement such that the corresponding edge of the grid
    // item’s grid area is N lines from its opposite edge in the corresponding direction. For example,
    // grid-column-end: span 2 indicates the second grid line in the endward direction from the
    // grid-column-start line.
    int column_span = 1;
    auto row_span = child_box.computed_values().grid_row_start().is_span() ? child_box.computed_values().grid_row_start().raw_value() : 1;
    if (child_box.computed_values().grid_column_start().is_position() && child_box.computed_values().grid_column_end().is_span())
        column_span = child_box.computed_values().grid_column_end().raw_value();
    if (child_box.computed_values().grid_column_end().is_position() && child_box.computed_values().grid_column_start().is_span()) {
        column_span = child_box.computed_values().grid_column_start().raw_value();
        column_start = column_end - column_span;
        // FIXME: Remove me once have implemented spans overflowing into negative indexes, e.g., grid-column: span 2 / 1
        if (column_start < 0)
            column_start = 1;
    }
    // FIXME: Have yet to find the spec for this.
    if (!child_box.computed_values().grid_column_start().is_position() && child_box.computed_values().grid_column_end().is_position() && column_end == 1)
        column_start = 1;

    // If a name is given as a <custom-ident>, only lines with that name are counted. If not enough
    // lines with that name exist, all implicit grid lines on the side of the explicit grid
    // corresponding to the search direction are assumed to have that name for the purpose of counting
    // this span.

    // https://drafts.csswg.org/css-grid/#grid-placement-auto
    // auto
    // The property contributes nothing to the grid item’s placement, indicating auto-placement or a
    // default span of one. (See § 8 Placing Grid Items, above.)

    // https://www.w3.org/TR/css-grid-2/#common-uses-named-lines
    // 8.1.3. Named Lines and Spans
    // Instead of counting lines by number, lines can be referenced by their line name:
    if (child_box.computed_values().grid_column_start().has_line_name()) {
        auto found_flag_and_index = get_line_index_by_line_name(child_box.computed_values().grid_column_start().line_name(), box.computed_values().grid_template_columns());
        if (found_flag_and_index > -1)
            column_start = 1 + found_flag_and_index;
        else
            column_start = 1; // FIXME
    }
    if (child_box.computed_values().grid_column_end().has_line_name()) {
        auto found_flag_and_index = get_line_index_by_line_name(child_box.computed_values().grid_column_end().line_name(), box.computed_values().grid_template_columns());
        if (found_flag_and_index > -1) {
            column_end = 1 + found_flag_and_index;
            if (!child_box.computed_values().grid_column_start().is_position())
                column_start = column_end - column_span;
        } else {
            column_end = 2;   // FIXME
            column_start = 1; // FIXME
        }
    }

    // If there are multiple lines of the same name, they effectively establish a named set of grid
    // lines, which can be exclusively indexed by filtering the placement by name:

    // https://drafts.csswg.org/css-grid/#grid-placement-errors
    // 8.3.1. Grid Placement Conflict Handling
    // If the placement for a grid item contains two lines, and the start line is further end-ward than
    // the end line, swap the two lines. If the start line is equal to the end line, remove the end
    // line.
    if (child_box.computed_values().grid_column_start().is_position() && child_box.computed_values().grid_column_end().is_position()) {
        if (column_start > column_end)
            swap(column_start, column_end);
        if (column_start != column_end)
            column_span = column_end - column_start;
    }

    // If the placement contains two spans, remove the one contributed by the end grid-placement
    // property.
    if (child_box.computed_values().grid_column_start().is_span() && child_box.computed_values().grid_column_end().is_span())
        column_span = child_box.computed_values().grid_column_start().raw_value();

    // FIXME: If the placement contains only a span for a named line, replace it with a span of 1.

    column_start -= 1;

    // 4.1.1.1. Set the column position of the cursor to the grid item's column-start line. If this is
    // less than the previous column position of the cursor, increment the row position by 1.
    if (column_start < auto_placement_cursor_x)
        auto_placement_cursor_y++;
    auto_placement_cursor_x = column_start;

    m_occupation_grid.maybe_add_column(auto_placement_cursor_x + 1);
    m_occupation_grid.maybe_add_row(auto_placement_cursor_y + 1);

    // 4.1.1.2. Increment the cursor's row position until a value is found where the grid item does not
    // overlap any occupied grid cells (creating new rows in the implicit grid as necessary).
    while (true) {
        if (!m_occupation_grid.is_occupied(column_start, auto_placement_cursor_y)) {
            break;
        }
        auto_placement_cursor_y++;
        m_occupation_grid.maybe_add_row(auto_placement_cursor_y + row_span);
    }
    // 4.1.1.3. Set the item's row-start line to the cursor's row position, and set the item's row-end
    // line according to its span from that position.
    m_occupation_grid.set_occupied(column_start, column_start + column_span, auto_placement_cursor_y, auto_placement_cursor_y + row_span);

    m_positioned_boxes.append({ child_box, auto_placement_cursor_y, row_span, column_start, column_span });
}

void GridFormattingContext::place_item_with_no_declared_position(Box const& child_box, int& auto_placement_cursor_x, int& auto_placement_cursor_y)
{
    // 4.1.2.1. Increment the column position of the auto-placement cursor until either this item's grid
    // area does not overlap any occupied grid cells, or the cursor's column position, plus the item's
    // column span, overflow the number of columns in the implicit grid, as determined earlier in this
    // algorithm.
    auto column_start = 0;
    auto column_span = 1;
    if (child_box.computed_values().grid_column_start().is_span())
        column_span = child_box.computed_values().grid_column_start().raw_value();
    else if (child_box.computed_values().grid_column_end().is_span())
        column_span = child_box.computed_values().grid_column_end().raw_value();
    // https://drafts.csswg.org/css-grid/#auto-placement-algo
    // 8.5. Grid Item Placement Algorithm
    // 3.3. If the largest column span among all the items without a definite column position is larger
    // than the width of the implicit grid, add columns to the end of the implicit grid to accommodate
    // that column span.
    m_occupation_grid.maybe_add_column(column_span);
    auto row_start = 0;
    auto row_span = 1;
    if (child_box.computed_values().grid_row_start().is_span())
        row_span = child_box.computed_values().grid_row_start().raw_value();
    else if (child_box.computed_values().grid_row_end().is_span())
        row_span = child_box.computed_values().grid_row_end().raw_value();
    auto found_unoccupied_area = false;
    for (int row_index = auto_placement_cursor_y; row_index < m_occupation_grid.row_count(); row_index++) {
        for (int column_index = auto_placement_cursor_x; column_index < m_occupation_grid.column_count(); column_index++) {
            if (column_span + column_index <= m_occupation_grid.column_count()) {
                auto found_all_available = true;
                for (int span_index = 0; span_index < column_span; span_index++) {
                    if (m_occupation_grid.is_occupied(column_index + span_index, row_index))
                        found_all_available = false;
                }
                if (found_all_available) {
                    found_unoccupied_area = true;
                    column_start = column_index;
                    row_start = row_index;
                    goto finish;
                }
            }
        }
        auto_placement_cursor_x = 0;
        auto_placement_cursor_y++;
    }
finish:

    // 4.1.2.2. If a non-overlapping position was found in the previous step, set the item's row-start
    // and column-start lines to the cursor's position. Otherwise, increment the auto-placement cursor's
    // row position (creating new rows in the implicit grid as necessary), set its column position to the
    // start-most column line in the implicit grid, and return to the previous step.
    if (!found_unoccupied_area) {
        row_start = m_occupation_grid.row_count();
        m_occupation_grid.maybe_add_row(m_occupation_grid.row_count() + 1);
    }

    m_occupation_grid.set_occupied(column_start, column_start + column_span, row_start, row_start + row_span);
    m_positioned_boxes.append({ child_box, row_start, row_span, column_start, column_span });
}

void GridFormattingContext::initialize_grid_tracks(Box const& box, AvailableSpace const& available_space, int column_count, int row_count)
{
    for (auto const& track_in_list : box.computed_values().grid_template_columns().track_list()) {
        auto repeat_count = (track_in_list.is_repeat() && track_in_list.repeat().is_default()) ? track_in_list.repeat().repeat_count() : 1;
        if (track_in_list.is_repeat()) {
            if (track_in_list.repeat().is_auto_fill() || track_in_list.repeat().is_auto_fit())
                repeat_count = column_count;
        }
        for (auto _ = 0; _ < repeat_count; _++) {
            switch (track_in_list.type()) {
            case CSS::ExplicitGridTrack::Type::MinMax:
                m_grid_columns.append(TemporaryTrack(track_in_list.minmax().min_grid_size(), track_in_list.minmax().max_grid_size()));
                break;
            case CSS::ExplicitGridTrack::Type::Repeat:
                for (auto& explicit_grid_track : track_in_list.repeat().grid_track_size_list().track_list()) {
                    auto track_sizing_function = explicit_grid_track;
                    if (track_sizing_function.is_minmax())
                        m_grid_columns.append(TemporaryTrack(track_sizing_function.minmax().min_grid_size(), track_sizing_function.minmax().max_grid_size()));
                    else
                        m_grid_columns.append(TemporaryTrack(track_sizing_function.grid_size()));
                }
                break;
            case CSS::ExplicitGridTrack::Type::Default:
                m_grid_columns.append(TemporaryTrack(track_in_list.grid_size()));
                break;
            default:
                VERIFY_NOT_REACHED();
            }
        }
    }
    for (auto const& track_in_list : box.computed_values().grid_template_rows().track_list()) {
        auto repeat_count = (track_in_list.is_repeat() && track_in_list.repeat().is_default()) ? track_in_list.repeat().repeat_count() : 1;
        if (track_in_list.is_repeat()) {
            if (track_in_list.repeat().is_auto_fill() || track_in_list.repeat().is_auto_fit())
                repeat_count = row_count;
        }
        for (auto _ = 0; _ < repeat_count; _++) {
            switch (track_in_list.type()) {
            case CSS::ExplicitGridTrack::Type::MinMax:
                m_grid_rows.append(TemporaryTrack(track_in_list.minmax().min_grid_size(), track_in_list.minmax().max_grid_size()));
                break;
            case CSS::ExplicitGridTrack::Type::Repeat:
                for (auto& explicit_grid_track : track_in_list.repeat().grid_track_size_list().track_list()) {
                    auto track_sizing_function = explicit_grid_track;
                    if (track_sizing_function.is_minmax())
                        m_grid_rows.append(TemporaryTrack(track_sizing_function.minmax().min_grid_size(), track_sizing_function.minmax().max_grid_size()));
                    else
                        m_grid_rows.append(TemporaryTrack(track_sizing_function.grid_size()));
                }
                break;
            case CSS::ExplicitGridTrack::Type::Default:
                m_grid_rows.append(TemporaryTrack(track_in_list.grid_size()));
                break;
            default:
                VERIFY_NOT_REACHED();
            }
        }
    }

    for (int column_index = m_grid_columns.size(); column_index < m_occupation_grid.column_count(); column_index++)
        m_grid_columns.append(TemporaryTrack());
    for (int row_index = m_grid_rows.size(); row_index < m_occupation_grid.row_count(); row_index++)
        m_grid_rows.append(TemporaryTrack());

    // https://www.w3.org/TR/css-grid-2/#gutters
    // 11.1. Gutters: the row-gap, column-gap, and gap properties
    // For the purpose of track sizing, each gutter is treated as an extra, empty, fixed-size track of
    // the specified size, which is spanned by any grid items that span across its corresponding grid
    // line.
    if (!box.computed_values().column_gap().is_auto()) {
        for (int column_index = 1; column_index < (m_occupation_grid.column_count() * 2) - 1; column_index += 2)
            m_grid_columns.insert(column_index, TemporaryTrack(resolve_size(box.computed_values().column_gap(), available_space.width, box), true));
    }
    if (!box.computed_values().row_gap().is_auto()) {
        for (int row_index = 1; row_index < (m_occupation_grid.row_count() * 2) - 1; row_index += 2)
            m_grid_rows.insert(row_index, TemporaryTrack(resolve_size(box.computed_values().row_gap(), available_space.height, box), true));
    }
}

void GridFormattingContext::calculate_sizes_of_columns(Box const& box, AvailableSpace const& available_space)
{
    // https://www.w3.org/TR/css-grid-2/#algo-init
    // 12.4. Initialize Track Sizes
    // Initialize each track’s base size and growth limit.
    for (auto& grid_column : m_grid_columns) {
        if (grid_column.is_gap)
            continue;
        // For each track, if the track’s min track sizing function is:
        switch (grid_column.min_track_sizing_function.type()) {
        // - A fixed sizing function
        // Resolve to an absolute length and use that size as the track’s initial base size.
        case CSS::GridSize::Type::Length:
            if (!grid_column.min_track_sizing_function.length().is_auto())
                grid_column.base_size = grid_column.min_track_sizing_function.length().to_px(box);
            break;
        case CSS::GridSize::Type::Percentage:
            if (available_space.width.is_definite())
                grid_column.base_size = grid_column.min_track_sizing_function.percentage().as_fraction() * available_space.width.to_px().value();
            break;
        // - An intrinsic sizing function
        // Use an initial base size of zero.
        case CSS::GridSize::Type::FlexibleLength:
            break;
        default:
            VERIFY_NOT_REACHED();
        }

        // For each track, if the track’s max track sizing function is:
        switch (grid_column.max_track_sizing_function.type()) {
        // - A fixed sizing function
        // Resolve to an absolute length and use that size as the track’s initial growth limit.
        case CSS::GridSize::Type::Length:
            if (!grid_column.max_track_sizing_function.length().is_auto())
                grid_column.growth_limit = grid_column.max_track_sizing_function.length().to_px(box);
            else
                // - An intrinsic sizing function
                // Use an initial growth limit of infinity.
                grid_column.growth_limit = -1;
            break;
        case CSS::GridSize::Type::Percentage:
            if (available_space.width.is_definite())
                grid_column.growth_limit = grid_column.max_track_sizing_function.percentage().as_fraction() * available_space.width.to_px().value();
            break;
        // - A flexible sizing function
        // Use an initial growth limit of infinity.
        case CSS::GridSize::Type::FlexibleLength:
            grid_column.growth_limit = -1;
            break;
        default:
            VERIFY_NOT_REACHED();
        }

        // In all cases, if the growth limit is less than the base size, increase the growth limit to match
        // the base size.
        if (grid_column.growth_limit != -1 && grid_column.growth_limit < grid_column.base_size)
            grid_column.growth_limit = grid_column.base_size;
    }

    // https://www.w3.org/TR/css-grid-2/#algo-content
    // 12.5. Resolve Intrinsic Track Sizes
    // This step resolves intrinsic track sizing functions to absolute lengths. First it resolves those
    // sizes based on items that are contained wholly within a single track. Then it gradually adds in
    // the space requirements of items that span multiple tracks, evenly distributing the extra space
    // across those tracks insofar as possible.

    // FIXME: 1. Shim baseline-aligned items so their intrinsic size contributions reflect their baseline
    // alignment. For the items in each baseline-sharing group, add a “shim” (effectively, additional
    // margin) on the start/end side (for first/last-baseline alignment) of each item so that, when
    // start/end-aligned together their baselines align as specified.

    // Consider these “shims” as part of the items’ intrinsic size contribution for the purpose of track
    // sizing, below. If an item uses multiple intrinsic size contributions, it can have different shims
    // for each one.

    // 2. Size tracks to fit non-spanning items: For each track with an intrinsic track sizing function and
    // not a flexible sizing function, consider the items in it with a span of 1:
    int index = 0;
    for (auto& grid_column : m_grid_columns) {
        if (grid_column.is_gap)
            continue;
        if (!grid_column.min_track_sizing_function.is_intrinsic_track_sizing()) {
            ++index;
            continue;
        }

        Vector<Box const&> boxes_of_column;
        for (auto& positioned_box : m_positioned_boxes) {
            if (positioned_box.column == index && positioned_box.column_span == 1)
                boxes_of_column.append(positioned_box.box);
        }

        // - For min-content minimums:
        // If the track has a min-content min track sizing function, set its base size to the maximum of the
        // items’ min-content contributions, floored at zero.
        // FIXME: Not implemented yet min-content.

        // - For max-content minimums:
        // If the track has a max-content min track sizing function, set its base size to the maximum of the
        // items’ max-content contributions, floored at zero.
        // FIXME: Not implemented yet max-content.

        // - For auto minimums:
        // If the track has an auto min track sizing function and the grid container is being sized under a
        // min-/max-content constraint, set the track’s base size to the maximum of its items’ limited
        // min-/max-content contributions (respectively), floored at zero. The limited min-/max-content
        // contribution of an item is (for this purpose) its min-/max-content contribution (accordingly),
        // limited by the max track sizing function (which could be the argument to a fit-content() track
        // sizing function) if that is fixed and ultimately floored by its minimum contribution (defined
        // below).
        // FIXME: Not implemented yet min-/max-content.

        // Otherwise, set the track’s base size to the maximum of its items’ minimum contributions, floored
        // at zero. The minimum contribution of an item is the smallest outer size it can have.
        // Specifically, if the item’s computed preferred size behaves as auto or depends on the size of its
        // containing block in the relevant axis, its minimum contribution is the outer size that would
        // result from assuming the item’s used minimum size as its preferred size; else the item’s minimum
        // contribution is its min-content contribution. Because the minimum contribution often depends on
        // the size of the item’s content, it is considered a type of intrinsic size contribution.
        float grid_column_width = 0;
        for (auto& box_of_column : boxes_of_column)
            grid_column_width = max(grid_column_width, calculate_min_content_width(box_of_column).value());
        grid_column.base_size = grid_column_width;

        // - For min-content maximums:
        // If the track has a min-content max track sizing function, set its growth limit to the maximum of
        // the items’ min-content contributions.
        // FIXME: Not implemented yet min-content maximums.

        // - For max-content maximums:
        // If the track has a max-content max track sizing function, set its growth limit to the maximum of
        // the items’ max-content contributions. For fit-content() maximums, furthermore clamp this growth
        // limit by the fit-content() argument.
        // FIXME: Not implemented yet max-content maximums.

        // In all cases, if a track’s growth limit is now less than its base size, increase the growth limit
        // to match the base size.
        if (grid_column.growth_limit != -1 && grid_column.growth_limit < grid_column.base_size)
            grid_column.growth_limit = grid_column.base_size;

        ++index;
    }

    // https://www.w3.org/TR/css-grid-2/#auto-repeat
    // The auto-fit keyword behaves the same as auto-fill, except that after grid item placement any
    // empty repeated tracks are collapsed. An empty track is one with no in-flow grid items placed into
    // or spanning across it. (This can result in all tracks being collapsed, if they’re all empty.)
    if (box.computed_values().grid_template_columns().track_list().size() == 1
        && box.computed_values().grid_template_columns().track_list().first().is_repeat()
        && box.computed_values().grid_template_columns().track_list().first().repeat().is_auto_fit()) {
        for (size_t idx = 0; idx < m_grid_columns.size(); idx++) {
            auto column_to_check = box.computed_values().column_gap().is_auto() ? idx : idx / 2;
            if (m_occupation_grid.is_occupied(column_to_check, 0))
                continue;
            if (!box.computed_values().column_gap().is_auto() && idx % 2 != 0)
                continue;

            // A collapsed track is treated as having a fixed track sizing function of 0px
            m_grid_columns[idx].base_size = 0;
            m_grid_columns[idx].growth_limit = 0;

            // FIXME: And the gutters on either side of it—including any space allotted through distributed
            // alignment—collapse.
        }
    }

    // 3. Increase sizes to accommodate spanning items crossing content-sized tracks: Next, consider the
    // items with a span of 2 that do not span a track with a flexible sizing function.
    // FIXME: Content-sized tracks not implemented (min-content, etc.)

    // 3.1. For intrinsic minimums: First increase the base size of tracks with an intrinsic min track sizing
    // function by distributing extra space as needed to accommodate these items’ minimum contributions.

    // If the grid container is being sized under a min- or max-content constraint, use the items’
    // limited min-content contributions in place of their minimum contributions here. (For an item
    // spanning multiple tracks, the upper limit used to calculate its limited min-/max-content
    // contribution is the sum of the fixed max track sizing functions of any tracks it spans, and is
    // applied if it only spans such tracks.)

    // 3.2. For content-based minimums: Next continue to increase the base size of tracks with a min track
    // sizing function of min-content or max-content by distributing extra space as needed to account
    // for these items' min-content contributions.

    // 3.3. For max-content minimums: Next, if the grid container is being sized under a max-content
    // constraint, continue to increase the base size of tracks with a min track sizing function of auto
    // or max-content by distributing extra space as needed to account for these items' limited
    // max-content contributions.

    // In all cases, continue to increase the base size of tracks with a min track sizing function of
    // max-content by distributing extra space as needed to account for these items' max-content
    // contributions.

    // 3.4. If at this point any track’s growth limit is now less than its base size, increase its growth
    // limit to match its base size.

    // 3.5. For intrinsic maximums: Next increase the growth limit of tracks with an intrinsic max track
    // sizing function by distributing extra space as needed to account for these items' min-content
    // contributions. Mark any tracks whose growth limit changed from infinite to finite in this step as
    // infinitely growable for the next step.

    // 3.6. For max-content maximums: Lastly continue to increase the growth limit of tracks with a max track
    // sizing function of max-content by distributing extra space as needed to account for these items'
    // max-content contributions. However, limit the growth of any fit-content() tracks by their
    // fit-content() argument.

    // Repeat incrementally for items with greater spans until all items have been considered.

    // FIXME: 4. Increase sizes to accommodate spanning items crossing flexible tracks: Next, repeat the previous
    // step instead considering (together, rather than grouped by span size) all items that do span a
    // track with a flexible sizing function while

    // - distributing space only to flexible tracks (i.e. treating all other tracks as having a fixed
    // sizing function)

    // - if the sum of the flexible sizing functions of all flexible tracks spanned by the item is greater
    // than zero, distributing space to such tracks according to the ratios of their flexible sizing
    // functions rather than distributing space equally

    // FIXME: 5. If any track still has an infinite growth limit (because, for example, it had no items placed in
    // it or it is a flexible track), set its growth limit to its base size.

    // https://www.w3.org/TR/css-grid-2/#extra-space
    // 12.5.1. Distributing Extra Space Across Spanned Tracks
    // To distribute extra space by increasing the affected sizes of a set of tracks as required by a
    // set of intrinsic size contributions,
    float sum_of_track_sizes = 0;
    for (auto& it : m_grid_columns)
        sum_of_track_sizes += it.base_size;

    // 1. Maintain separately for each affected base size or growth limit a planned increase, initially
    // set to 0. (This prevents the size increases from becoming order-dependent.)

    // 2. For each considered item,

    // 2.1. Find the space to distribute: Subtract the corresponding size (base size or growth limit) of
    // every spanned track from the item’s size contribution to find the item’s remaining size
    // contribution. (For infinite growth limits, substitute the track’s base size.) This is the space
    // to distribute. Floor it at zero.

    // For base sizes, the limit is its growth limit. For growth limits, the limit is infinity if it is
    // marked as infinitely growable, and equal to the growth limit otherwise. If the affected size was
    // a growth limit and the track is not marked infinitely growable, then each item-incurred increase
    // will be zero.
    // extra-space = max(0, size-contribution - ∑track-sizes)
    for (auto& grid_column : m_grid_columns) {
        if (grid_column.is_gap)
            continue;
        grid_column.space_to_distribute = max(0, (grid_column.growth_limit == -1 ? grid_column.base_size : grid_column.growth_limit) - grid_column.base_size);
    }

    auto remaining_free_space = available_space.width.is_definite() ? available_space.width.to_px().value() - sum_of_track_sizes : 0;
    // 2.2. Distribute space up to limits: Find the item-incurred increase for each spanned track with an
    // affected size by: distributing the space equally among such tracks, freezing a track’s
    // item-incurred increase as its affected size + item-incurred increase reaches its limit (and
    // continuing to grow the unfrozen tracks as needed).
    auto count_of_unfrozen_tracks = 0;
    for (auto& grid_column : m_grid_columns) {
        if (grid_column.space_to_distribute > 0)
            count_of_unfrozen_tracks++;
    }
    while (remaining_free_space > 0) {
        if (count_of_unfrozen_tracks == 0)
            break;
        auto free_space_to_distribute_per_track = remaining_free_space / count_of_unfrozen_tracks;

        for (auto& grid_column : m_grid_columns) {
            if (grid_column.space_to_distribute == 0)
                continue;
            // 2.4. For each affected track, if the track’s item-incurred increase is larger than the track’s planned
            // increase set the track’s planned increase to that value.
            if (grid_column.space_to_distribute <= free_space_to_distribute_per_track) {
                grid_column.planned_increase += grid_column.space_to_distribute;
                remaining_free_space -= grid_column.space_to_distribute;
                grid_column.space_to_distribute = 0;
            } else {
                grid_column.space_to_distribute -= free_space_to_distribute_per_track;
                grid_column.planned_increase += free_space_to_distribute_per_track;
                remaining_free_space -= free_space_to_distribute_per_track;
            }
        }

        count_of_unfrozen_tracks = 0;
        for (auto& grid_column : m_grid_columns) {
            if (grid_column.space_to_distribute > 0)
                count_of_unfrozen_tracks++;
        }
        if (remaining_free_space == 0)
            break;
    }

    // 2.3. Distribute space beyond limits: If space remains after all tracks are frozen, unfreeze and
    // continue to distribute space to the item-incurred increase of…

    // - when accommodating minimum contributions or accommodating min-content contributions: any affected
    // track that happens to also have an intrinsic max track sizing function; if there are no such
    // tracks, then all affected tracks.

    // - when accommodating max-content contributions: any affected track that happens to also have a
    // max-content max track sizing function; if there are no such tracks, then all affected tracks.

    // - when handling any intrinsic growth limit: all affected tracks.

    // For this purpose, the max track sizing function of a fit-content() track is treated as
    // max-content until it reaches the limit specified as the fit-content() argument, after which it is
    // treated as having a fixed sizing function of that argument.

    // This step prioritizes the distribution of space for accommodating space required by the
    // tracks’ min track sizing functions beyond their current growth limits based on the types of their
    // max track sizing functions.

    // 3. Update the tracks' affected sizes by adding in the planned increase so that the next round of
    // space distribution will account for the increase. (If the affected size is an infinite growth
    // limit, set it to the track’s base size plus the planned increase.)
    for (auto& grid_column : m_grid_columns)
        grid_column.base_size += grid_column.planned_increase;

    // https://www.w3.org/TR/css-grid-2/#algo-grow-tracks
    // 12.6. Maximize Tracks

    // If the free space is positive, distribute it equally to the base sizes of all tracks, freezing
    // tracks as they reach their growth limits (and continuing to grow the unfrozen tracks as needed).
    auto free_space = get_free_space_x(available_space);
    while (free_space > 0) {
        auto free_space_to_distribute_per_track = free_space / (m_grid_columns.size() - count_of_gap_columns());
        for (auto& grid_column : m_grid_columns) {
            if (grid_column.is_gap)
                continue;
            if (grid_column.growth_limit != -1)
                grid_column.base_size = min(grid_column.growth_limit, grid_column.base_size + free_space_to_distribute_per_track);
            else
                grid_column.base_size = grid_column.base_size + free_space_to_distribute_per_track;
        }
        if (get_free_space_x(available_space) == free_space)
            break;
        free_space = get_free_space_x(available_space);
    }

    // For the purpose of this step: if sizing the grid container under a max-content constraint, the
    // free space is infinite; if sizing under a min-content constraint, the free space is zero.

    // If this would cause the grid to be larger than the grid container’s inner size as limited by its
    // max-width/height, then redo this step, treating the available grid space as equal to the grid
    // container’s inner size when it’s sized to its max-width/height.

    // https://drafts.csswg.org/css-grid/#algo-flex-tracks
    // 12.7. Expand Flexible Tracks
    // This step sizes flexible tracks using the largest value it can assign to an fr without exceeding
    // the available space.

    // First, find the grid’s used flex fraction:
    auto column_flex_factor_sum = 0;
    for (auto& grid_column : m_grid_columns) {
        if (grid_column.min_track_sizing_function.is_flexible_length())
            column_flex_factor_sum++;
    }
    // See 12.7.1.
    // Let flex factor sum be the sum of the flex factors of the flexible tracks. If this value is less
    // than 1, set it to 1 instead.
    if (column_flex_factor_sum < 1)
        column_flex_factor_sum = 1;

    // See 12.7.1.
    float sized_column_widths = 0;
    for (auto& grid_column : m_grid_columns) {
        if (!grid_column.min_track_sizing_function.is_flexible_length())
            sized_column_widths += grid_column.base_size;
    }
    // Let leftover space be the space to fill minus the base sizes of the non-flexible grid tracks.
    double free_horizontal_space = available_space.width.is_definite() ? available_space.width.to_px().value() - sized_column_widths : 0;

    // If the free space is zero or if sizing the grid container under a min-content constraint:
    // The used flex fraction is zero.
    // FIXME: Add min-content constraint check.

    // Otherwise, if the free space is a definite length:
    // The used flex fraction is the result of finding the size of an fr using all of the grid tracks
    // and a space to fill of the available grid space.
    if (free_horizontal_space > 0) {
        for (auto& grid_column : m_grid_columns) {
            if (grid_column.min_track_sizing_function.is_flexible_length()) {
                // See 12.7.1.
                // Let the hypothetical fr size be the leftover space divided by the flex factor sum.
                auto hypothetical_fr_size = static_cast<double>(1.0 / column_flex_factor_sum) * free_horizontal_space;
                // For each flexible track, if the product of the used flex fraction and the track’s flex factor is
                // greater than the track’s base size, set its base size to that product.
                grid_column.base_size = max(grid_column.base_size, hypothetical_fr_size);
            }
        }
    }

    // Otherwise, if the free space is an indefinite length:
    // FIXME: No tracks will have indefinite length as per current implementation.

    // The used flex fraction is the maximum of:
    // For each flexible track, if the flexible track’s flex factor is greater than one, the result of
    // dividing the track’s base size by its flex factor; otherwise, the track’s base size.

    // For each grid item that crosses a flexible track, the result of finding the size of an fr using
    // all the grid tracks that the item crosses and a space to fill of the item’s max-content
    // contribution.

    // If using this flex fraction would cause the grid to be smaller than the grid container’s
    // min-width/height (or larger than the grid container’s max-width/height), then redo this step,
    // treating the free space as definite and the available grid space as equal to the grid container’s
    // inner size when it’s sized to its min-width/height (max-width/height).

    // For each flexible track, if the product of the used flex fraction and the track’s flex factor is
    // greater than the track’s base size, set its base size to that product.

    // https://drafts.csswg.org/css-grid/#algo-find-fr-size
    // 12.7.1. Find the Size of an fr

    // This algorithm finds the largest size that an fr unit can be without exceeding the target size.
    // It must be called with a set of grid tracks and some quantity of space to fill.

    // 1. Let leftover space be the space to fill minus the base sizes of the non-flexible grid tracks.

    // 2. Let flex factor sum be the sum of the flex factors of the flexible tracks. If this value is less
    // than 1, set it to 1 instead.

    // 3. Let the hypothetical fr size be the leftover space divided by the flex factor sum.

    // FIXME: 4. If the product of the hypothetical fr size and a flexible track’s flex factor is less than the
    // track’s base size, restart this algorithm treating all such tracks as inflexible.

    // 5. Return the hypothetical fr size.

    // https://drafts.csswg.org/css-grid/#algo-stretch
    // 12.8. Stretch auto Tracks

    // When the content-distribution property of the grid container is normal or stretch in this axis,
    // this step expands tracks that have an auto max track sizing function by dividing any remaining
    // positive, definite free space equally amongst them. If the free space is indefinite, but the grid
    // container has a definite min-width/height, use that size to calculate the free space for this
    // step instead.
    float used_horizontal_space = 0;
    for (auto& grid_column : m_grid_columns) {
        if (!(grid_column.max_track_sizing_function.is_length() && grid_column.max_track_sizing_function.length().is_auto()))
            used_horizontal_space += grid_column.base_size;
    }

    float remaining_horizontal_space = available_space.width.is_definite() ? available_space.width.to_px().value() - used_horizontal_space : 0;
    auto count_of_auto_max_column_tracks = 0;
    for (auto& grid_column : m_grid_columns) {
        if (grid_column.max_track_sizing_function.is_length() && grid_column.max_track_sizing_function.length().is_auto())
            count_of_auto_max_column_tracks++;
    }
    for (auto& grid_column : m_grid_columns) {
        if (grid_column.max_track_sizing_function.is_length() && grid_column.max_track_sizing_function.length().is_auto())
            grid_column.base_size = max(grid_column.base_size, remaining_horizontal_space / count_of_auto_max_column_tracks);
    }

    // If calculating the layout of a grid item in this step depends on the available space in the block
    // axis, assume the available space that it would have if any row with a definite max track sizing
    // function had that size and all other rows were infinite. If both the grid container and all
    // tracks have definite sizes, also apply align-content to find the final effective size of any gaps
    // spanned by such items; otherwise ignore the effects of track alignment in this estimation.
}

void GridFormattingContext::calculate_sizes_of_rows(Box const& box)
{
    // https://www.w3.org/TR/css-grid-2/#algo-init
    // 12.4. Initialize Track Sizes
    // Initialize each track’s base size and growth limit.
    auto& box_state = m_state.get_mutable(box);
    for (auto& grid_row : m_grid_rows) {
        if (grid_row.is_gap)
            continue;
        // For each track, if the track’s min track sizing function is:
        switch (grid_row.min_track_sizing_function.type()) {
        // - A fixed sizing function
        // Resolve to an absolute length and use that size as the track’s initial base size.
        case CSS::GridSize::Type::Length:
            if (!grid_row.min_track_sizing_function.length().is_auto())
                grid_row.base_size = grid_row.min_track_sizing_function.length().to_px(box);
            break;
        case CSS::GridSize::Type::Percentage:
            grid_row.base_size = grid_row.min_track_sizing_function.percentage().as_fraction() * box_state.content_height();
            break;
        // - An intrinsic sizing function
        // Use an initial base size of zero.
        case CSS::GridSize::Type::FlexibleLength:
            break;
        default:
            VERIFY_NOT_REACHED();
        }

        // For each track, if the track’s max track sizing function is:
        switch (grid_row.max_track_sizing_function.type()) {
        // - A fixed sizing function
        // Resolve to an absolute length and use that size as the track’s initial growth limit.
        case CSS::GridSize::Type::Length:
            if (!grid_row.max_track_sizing_function.length().is_auto())
                grid_row.growth_limit = grid_row.max_track_sizing_function.length().to_px(box);
            else
                // - An intrinsic sizing function
                // Use an initial growth limit of infinity.
                grid_row.growth_limit = -1;
            break;
        case CSS::GridSize::Type::Percentage:
            grid_row.growth_limit = grid_row.max_track_sizing_function.percentage().as_fraction() * box_state.content_height();
            break;
        // - A flexible sizing function
        // Use an initial growth limit of infinity.
        case CSS::GridSize::Type::FlexibleLength:
            grid_row.growth_limit = -1;
            break;
        default:
            VERIFY_NOT_REACHED();
        }

        // In all cases, if the growth limit is less than the base size, increase the growth limit to match
        // the base size.
        if (grid_row.growth_limit != -1 && grid_row.growth_limit < grid_row.base_size)
            grid_row.growth_limit = grid_row.base_size;
    }

    // https://www.w3.org/TR/css-grid-2/#algo-content
    // 12.5. Resolve Intrinsic Track Sizes
    // This step resolves intrinsic track sizing functions to absolute lengths. First it resolves those
    // sizes based on items that are contained wholly within a single track. Then it gradually adds in
    // the space requirements of items that span multiple tracks, evenly distributing the extra space
    // across those tracks insofar as possible.

    // FIXME: 1. Shim baseline-aligned items so their intrinsic size contributions reflect their baseline
    // alignment. For the items in each baseline-sharing group, add a “shim” (effectively, additional
    // margin) on the start/end side (for first/last-baseline alignment) of each item so that, when
    // start/end-aligned together their baselines align as specified.

    // Consider these “shims” as part of the items’ intrinsic size contribution for the purpose of track
    // sizing, below. If an item uses multiple intrinsic size contributions, it can have different shims
    // for each one.

    // 2. Size tracks to fit non-spanning items: For each track with an intrinsic track sizing function and
    // not a flexible sizing function, consider the items in it with a span of 1:
    auto index = 0;
    for (auto& grid_row : m_grid_rows) {
        if (grid_row.is_gap)
            continue;
        if (!grid_row.min_track_sizing_function.is_intrinsic_track_sizing()) {
            ++index;
            continue;
        }

        Vector<PositionedBox&> positioned_boxes_of_row;
        for (auto& positioned_box : m_positioned_boxes) {
            if (positioned_box.row == index && positioned_box.row_span == 1)
                positioned_boxes_of_row.append(positioned_box);
        }

        // - For min-content minimums:
        // If the track has a min-content min track sizing function, set its base size to the maximum of the
        // items’ min-content contributions, floored at zero.
        // FIXME: Not implemented yet min-content.

        // - For max-content minimums:
        // If the track has a max-content min track sizing function, set its base size to the maximum of the
        // items’ max-content contributions, floored at zero.
        // FIXME: Not implemented yet max-content.

        // - For auto minimums:
        // If the track has an auto min track sizing function and the grid container is being sized under a
        // min-/max-content constraint, set the track’s base size to the maximum of its items’ limited
        // min-/max-content contributions (respectively), floored at zero. The limited min-/max-content
        // contribution of an item is (for this purpose) its min-/max-content contribution (accordingly),
        // limited by the max track sizing function (which could be the argument to a fit-content() track
        // sizing function) if that is fixed and ultimately floored by its minimum contribution (defined
        // below).
        // FIXME: Not implemented yet min-/max-content.

        // Otherwise, set the track’s base size to the maximum of its items’ minimum contributions, floored
        // at zero. The minimum contribution of an item is the smallest outer size it can have.
        // Specifically, if the item’s computed preferred size behaves as auto or depends on the size of its
        // containing block in the relevant axis, its minimum contribution is the outer size that would
        // result from assuming the item’s used minimum size as its preferred size; else the item’s minimum
        // contribution is its min-content contribution. Because the minimum contribution often depends on
        // the size of the item’s content, it is considered a type of intrinsic size contribution.
        float grid_row_height = 0;
        for (auto& positioned_box : positioned_boxes_of_row)
            grid_row_height = max(grid_row_height, calculate_min_content_height(positioned_box.box, AvailableSize::make_definite(m_grid_columns[positioned_box.column].base_size)).value());
        grid_row.base_size = grid_row_height;

        // - For min-content maximums:
        // If the track has a min-content max track sizing function, set its growth limit to the maximum of
        // the items’ min-content contributions.
        // FIXME: Not implemented yet min-content maximums.

        // - For max-content maximums:
        // If the track has a max-content max track sizing function, set its growth limit to the maximum of
        // the items’ max-content contributions. For fit-content() maximums, furthermore clamp this growth
        // limit by the fit-content() argument.
        // FIXME: Not implemented yet max-content maximums.

        // In all cases, if a track’s growth limit is now less than its base size, increase the growth limit
        // to match the base size.
        if (grid_row.growth_limit != -1 && grid_row.growth_limit < grid_row.base_size)
            grid_row.growth_limit = grid_row.base_size;
        ++index;
    }

    // https://www.w3.org/TR/css-grid-2/#auto-repeat
    // The auto-fit keyword behaves the same as auto-fill, except that after grid item placement any
    // empty repeated tracks are collapsed. An empty track is one with no in-flow grid items placed into
    // or spanning across it. (This can result in all tracks being collapsed, if they’re all empty.)

    // 3. Increase sizes to accommodate spanning items crossing content-sized tracks: Next, consider the
    // items with a span of 2 that do not span a track with a flexible sizing function.
    // FIXME: Content-sized tracks not implemented (min-content, etc.)

    // 3.1. For intrinsic minimums: First increase the base size of tracks with an intrinsic min track sizing
    // function by distributing extra space as needed to accommodate these items’ minimum contributions.

    // If the grid container is being sized under a min- or max-content constraint, use the items’
    // limited min-content contributions in place of their minimum contributions here. (For an item
    // spanning multiple tracks, the upper limit used to calculate its limited min-/max-content
    // contribution is the sum of the fixed max track sizing functions of any tracks it spans, and is
    // applied if it only spans such tracks.)

    // 3.2. For content-based minimums: Next continue to increase the base size of tracks with a min track
    // sizing function of min-content or max-content by distributing extra space as needed to account
    // for these items' min-content contributions.

    // 3.3. For max-content minimums: Next, if the grid container is being sized under a max-content
    // constraint, continue to increase the base size of tracks with a min track sizing function of auto
    // or max-content by distributing extra space as needed to account for these items' limited
    // max-content contributions.

    // In all cases, continue to increase the base size of tracks with a min track sizing function of
    // max-content by distributing extra space as needed to account for these items' max-content
    // contributions.

    // 3.4. If at this point any track’s growth limit is now less than its base size, increase its growth
    // limit to match its base size.

    // 3.5. For intrinsic maximums: Next increase the growth limit of tracks with an intrinsic max track
    // sizing function by distributing extra space as needed to account for these items' min-content
    // contributions. Mark any tracks whose growth limit changed from infinite to finite in this step as
    // infinitely growable for the next step.

    // 3.6. For max-content maximums: Lastly continue to increase the growth limit of tracks with a max track
    // sizing function of max-content by distributing extra space as needed to account for these items'
    // max-content contributions. However, limit the growth of any fit-content() tracks by their
    // fit-content() argument.

    // Repeat incrementally for items with greater spans until all items have been considered.

    // FIXME: 4. Increase sizes to accommodate spanning items crossing flexible tracks: Next, repeat the previous
    // step instead considering (together, rather than grouped by span size) all items that do span a
    // track with a flexible sizing function while

    // - distributing space only to flexible tracks (i.e. treating all other tracks as having a fixed
    // sizing function)

    // - if the sum of the flexible sizing functions of all flexible tracks spanned by the item is greater
    // than zero, distributing space to such tracks according to the ratios of their flexible sizing
    // functions rather than distributing space equally

    // FIXME: 5. If any track still has an infinite growth limit (because, for example, it had no items placed in
    // it or it is a flexible track), set its growth limit to its base size.

    // https://www.w3.org/TR/css-grid-2/#extra-space
    // 12.5.1. Distributing Extra Space Across Spanned Tracks
    // To distribute extra space by increasing the affected sizes of a set of tracks as required by a
    // set of intrinsic size contributions,

    // 1. Maintain separately for each affected base size or growth limit a planned increase, initially
    // set to 0. (This prevents the size increases from becoming order-dependent.)

    // 2. For each considered item,

    // 2.1. Find the space to distribute: Subtract the corresponding size (base size or growth limit) of
    // every spanned track from the item’s size contribution to find the item’s remaining size
    // contribution. (For infinite growth limits, substitute the track’s base size.) This is the space
    // to distribute. Floor it at zero.

    // For base sizes, the limit is its growth limit. For growth limits, the limit is infinity if it is
    // marked as infinitely growable, and equal to the growth limit otherwise. If the affected size was
    // a growth limit and the track is not marked infinitely growable, then each item-incurred increase
    // will be zero.
    // extra-space = max(0, size-contribution - ∑track-sizes)

    // 2.2. Distribute space up to limits: Find the item-incurred increase for each spanned track with an
    // affected size by: distributing the space equally among such tracks, freezing a track’s
    // item-incurred increase as its affected size + item-incurred increase reaches its limit (and
    // continuing to grow the unfrozen tracks as needed).

    // 2.3. Distribute space beyond limits: If space remains after all tracks are frozen, unfreeze and
    // continue to distribute space to the item-incurred increase of…

    // - when accommodating minimum contributions or accommodating min-content contributions: any affected
    // track that happens to also have an intrinsic max track sizing function; if there are no such
    // tracks, then all affected tracks.

    // - when accommodating max-content contributions: any affected track that happens to also have a
    // max-content max track sizing function; if there are no such tracks, then all affected tracks.

    // - when handling any intrinsic growth limit: all affected tracks.

    // For this purpose, the max track sizing function of a fit-content() track is treated as
    // max-content until it reaches the limit specified as the fit-content() argument, after which it is
    // treated as having a fixed sizing function of that argument.

    // This step prioritizes the distribution of space for accommodating space required by the
    // tracks’ min track sizing functions beyond their current growth limits based on the types of their
    // max track sizing functions.

    // 3. Update the tracks' affected sizes by adding in the planned increase so that the next round of
    // space distribution will account for the increase. (If the affected size is an infinite growth
    // limit, set it to the track’s base size plus the planned increase.)
    // FIXME: Do for rows.

    // https://www.w3.org/TR/css-grid-2/#algo-grow-tracks
    // 12.6. Maximize Tracks

    // If the free space is positive, distribute it equally to the base sizes of all tracks, freezing
    // tracks as they reach their growth limits (and continuing to grow the unfrozen tracks as needed).

    auto free_space = get_free_space_y(box);
    while (free_space > 0) {
        auto free_space_to_distribute_per_track = free_space / (m_grid_rows.size() - count_of_gap_rows());
        for (auto& grid_row : m_grid_rows) {
            if (grid_row.is_gap)
                continue;
            grid_row.base_size = min(grid_row.growth_limit, grid_row.base_size + free_space_to_distribute_per_track);
        }
        if (get_free_space_y(box) == free_space)
            break;
        free_space = get_free_space_y(box);
    }
    if (free_space == -1) {
        for (auto& grid_row : m_grid_rows) {
            if (grid_row.is_gap)
                continue;
            if (grid_row.growth_limit != -1)
                grid_row.base_size = grid_row.growth_limit;
        }
    }

    // For the purpose of this step: if sizing the grid container under a max-content constraint, the
    // free space is infinite; if sizing under a min-content constraint, the free space is zero.

    // If this would cause the grid to be larger than the grid container’s inner size as limited by its
    // max-width/height, then redo this step, treating the available grid space as equal to the grid
    // container’s inner size when it’s sized to its max-width/height.

    // https://drafts.csswg.org/css-grid/#algo-flex-tracks
    // 12.7. Expand Flexible Tracks
    // This step sizes flexible tracks using the largest value it can assign to an fr without exceeding
    // the available space.

    // First, find the grid’s used flex fraction:
    auto row_flex_factor_sum = 0;
    for (auto& grid_row : m_grid_rows) {
        if (grid_row.min_track_sizing_function.is_flexible_length())
            row_flex_factor_sum++;
    }
    // See 12.7.1.
    // Let flex factor sum be the sum of the flex factors of the flexible tracks. If this value is less
    // than 1, set it to 1 instead.
    if (row_flex_factor_sum < 1)
        row_flex_factor_sum = 1;

    // See 12.7.1.
    float sized_row_heights = 0;
    for (auto& grid_row : m_grid_rows) {
        if (!grid_row.min_track_sizing_function.is_flexible_length())
            sized_row_heights += grid_row.base_size;
    }
    // Let leftover space be the space to fill minus the base sizes of the non-flexible grid tracks.
    double free_vertical_space = box_state.content_height() - sized_row_heights;

    // If the free space is zero or if sizing the grid container under a min-content constraint:
    // The used flex fraction is zero.
    // FIXME: Add min-content constraint check.

    // Otherwise, if the free space is a definite length:
    // The used flex fraction is the result of finding the size of an fr using all of the grid tracks
    // and a space to fill of the available grid space.
    if (free_vertical_space > 0) {
        for (auto& grid_row : m_grid_rows) {
            if (grid_row.min_track_sizing_function.is_flexible_length()) {
                // See 12.7.1.
                // Let the hypothetical fr size be the leftover space divided by the flex factor sum.
                auto hypothetical_fr_size = static_cast<double>(1.0 / row_flex_factor_sum) * free_vertical_space;
                // For each flexible track, if the product of the used flex fraction and the track’s flex factor is
                // greater than the track’s base size, set its base size to that product.
                grid_row.base_size = max(grid_row.base_size, hypothetical_fr_size);
            }
        }
    }

    // Otherwise, if the free space is an indefinite length:
    // FIXME: No tracks will have indefinite length as per current implementation.

    // The used flex fraction is the maximum of:
    // For each flexible track, if the flexible track’s flex factor is greater than one, the result of
    // dividing the track’s base size by its flex factor; otherwise, the track’s base size.

    // For each grid item that crosses a flexible track, the result of finding the size of an fr using
    // all the grid tracks that the item crosses and a space to fill of the item’s max-content
    // contribution.

    // If using this flex fraction would cause the grid to be smaller than the grid container’s
    // min-width/height (or larger than the grid container’s max-width/height), then redo this step,
    // treating the free space as definite and the available grid space as equal to the grid container’s
    // inner size when it’s sized to its min-width/height (max-width/height).

    // For each flexible track, if the product of the used flex fraction and the track’s flex factor is
    // greater than the track’s base size, set its base size to that product.

    // https://drafts.csswg.org/css-grid/#algo-find-fr-size
    // 12.7.1. Find the Size of an fr

    // This algorithm finds the largest size that an fr unit can be without exceeding the target size.
    // It must be called with a set of grid tracks and some quantity of space to fill.

    // 1. Let leftover space be the space to fill minus the base sizes of the non-flexible grid tracks.

    // 2. Let flex factor sum be the sum of the flex factors of the flexible tracks. If this value is less
    // than 1, set it to 1 instead.

    // 3. Let the hypothetical fr size be the leftover space divided by the flex factor sum.

    // FIXME: 4. If the product of the hypothetical fr size and a flexible track’s flex factor is less than the
    // track’s base size, restart this algorithm treating all such tracks as inflexible.

    // 5. Return the hypothetical fr size.

    // https://drafts.csswg.org/css-grid/#algo-stretch
    // 12.8. Stretch auto Tracks

    // When the content-distribution property of the grid container is normal or stretch in this axis,
    // this step expands tracks that have an auto max track sizing function by dividing any remaining
    // positive, definite free space equally amongst them. If the free space is indefinite, but the grid
    // container has a definite min-width/height, use that size to calculate the free space for this
    // step instead.
    float used_vertical_space = 0;
    for (auto& grid_row : m_grid_rows) {
        if (!(grid_row.max_track_sizing_function.is_length() && grid_row.max_track_sizing_function.length().is_auto()))
            used_vertical_space += grid_row.base_size;
    }

    float remaining_vertical_space = box_state.content_height() - used_vertical_space;
    auto count_of_auto_max_row_tracks = 0;
    for (auto& grid_row : m_grid_rows) {
        if (grid_row.max_track_sizing_function.is_length() && grid_row.max_track_sizing_function.length().is_auto())
            count_of_auto_max_row_tracks++;
    }
    for (auto& grid_row : m_grid_rows) {
        if (grid_row.max_track_sizing_function.is_length() && grid_row.max_track_sizing_function.length().is_auto())
            grid_row.base_size = max(grid_row.base_size, remaining_vertical_space / count_of_auto_max_row_tracks);
    }
}

void GridFormattingContext::run(Box const& box, LayoutMode, AvailableSpace const& available_space)
{
    auto grid_template_columns = box.computed_values().grid_template_columns();
    auto grid_template_rows = box.computed_values().grid_template_rows();

    // https://drafts.csswg.org/css-grid/#overview-placement
    // 2.2. Placing Items
    // The contents of the grid container are organized into individual grid items (analogous to
    // flex items), which are then assigned to predefined areas in the grid. They can be explicitly
    // placed using coordinates through the grid-placement properties or implicitly placed into
    // empty areas using auto-placement.
    box.for_each_child_of_type<Box>([&](Box& child_box) {
        if (can_skip_is_anonymous_text_run(child_box))
            return IterationDecision::Continue;
        m_boxes_to_place.append(child_box);
        return IterationDecision::Continue;
    });

    auto column_count = get_count_of_tracks(grid_template_columns.track_list(), available_space, box);
    auto row_count = get_count_of_tracks(grid_template_rows.track_list(), available_space, box);

    m_occupation_grid = OccupationGrid(column_count, row_count);

    // https://drafts.csswg.org/css-grid/#auto-placement-algo
    // 8.5. Grid Item Placement Algorithm

    // FIXME: 0. Generate anonymous grid items

    // 1. Position anything that's not auto-positioned.
    for (size_t i = 0; i < m_boxes_to_place.size(); i++) {
        auto const& child_box = m_boxes_to_place[i];
        if (is_auto_positioned_row(child_box.computed_values().grid_row_start(), child_box.computed_values().grid_row_end())
            || is_auto_positioned_column(child_box.computed_values().grid_column_start(), child_box.computed_values().grid_column_end()))
            continue;
        place_item_with_row_and_column_position(box, child_box);
        m_boxes_to_place.remove(i);
        i--;
    }

    // 2. Process the items locked to a given row.
    // FIXME: Do "dense" packing
    for (size_t i = 0; i < m_boxes_to_place.size(); i++) {
        auto const& child_box = m_boxes_to_place[i];
        if (is_auto_positioned_row(child_box.computed_values().grid_row_start(), child_box.computed_values().grid_row_end()))
            continue;
        place_item_with_row_position(box, child_box);
        m_boxes_to_place.remove(i);
        i--;
    }

    // 3. Determine the columns in the implicit grid.
    // NOTE: "implicit grid" here is the same as the m_occupation_grid

    // 3.1. Start with the columns from the explicit grid.
    // NOTE: Done in step 1.

    // 3.2. Among all the items with a definite column position (explicitly positioned items, items
    // positioned in the previous step, and items not yet positioned but with a definite column) add
    // columns to the beginning and end of the implicit grid as necessary to accommodate those items.
    // NOTE: "Explicitly positioned items" and "items positioned in the previous step" done in step 1
    // and 2, respectively. Adding columns for "items not yet positioned but with a definite column"
    // will be done in step 4.

    // 4. Position the remaining grid items.
    // For each grid item that hasn't been positioned by the previous steps, in order-modified document
    // order:
    auto auto_placement_cursor_x = 0;
    auto auto_placement_cursor_y = 0;
    for (size_t i = 0; i < m_boxes_to_place.size(); i++) {
        auto const& child_box = m_boxes_to_place[i];
        // 4.1. For sparse packing:
        // FIXME: no distinction made. See #4.2

        // 4.1.1. If the item has a definite column position:
        if (!is_auto_positioned_column(child_box.computed_values().grid_column_start(), child_box.computed_values().grid_column_end()))
            place_item_with_column_position(box, child_box, auto_placement_cursor_x, auto_placement_cursor_y);

        // 4.1.2. If the item has an automatic grid position in both axes:
        else
            place_item_with_no_declared_position(child_box, auto_placement_cursor_x, auto_placement_cursor_y);

        m_boxes_to_place.remove(i);
        i--;

        // FIXME: 4.2. For dense packing:
    }

    // https://drafts.csswg.org/css-grid/#overview-sizing
    // 2.3. Sizing the Grid
    // Once the grid items have been placed, the sizes of the grid tracks (rows and columns) are
    // calculated, accounting for the sizes of their contents and/or available space as specified in
    // the grid definition.

    // https://www.w3.org/TR/css-grid-2/#layout-algorithm
    // 12. Grid Sizing
    // This section defines the grid sizing algorithm, which determines the size of all grid tracks and,
    // by extension, the entire grid.

    // Each track has specified minimum and maximum sizing functions (which may be the same). Each
    // sizing function is either:

    // - A fixed sizing function (<length> or resolvable <percentage>).
    // - An intrinsic sizing function (min-content, max-content, auto, fit-content()).
    // - A flexible sizing function (<flex>).

    // The grid sizing algorithm defines how to resolve these sizing constraints into used track sizes.
    initialize_grid_tracks(box, available_space, column_count, row_count);

    // https://www.w3.org/TR/css-grid-2/#algo-overview
    // 12.1. Grid Sizing Algorithm

    // 1. First, the track sizing algorithm is used to resolve the sizes of the grid columns.
    // In this process, any grid item which is subgridded in the grid container’s inline axis is treated
    // as empty and its grid items (the grandchildren) are treated as direct children of the grid
    // container (their grandparent). This introspection is recursive.

    // Items which are subgridded only in the block axis, and whose grid container size in the inline
    // axis depends on the size of its contents are also introspected: since the size of the item in
    // this dimension can be dependent on the sizing of its subgridded tracks in the other, the size
    // contribution of any such item to this grid’s column sizing (see Resolve Intrinsic Track Sizes) is
    // taken under the provision of having determined its track sizing only up to the same point in the
    // Grid Sizing Algorithm as this itself. E.g. for the first pass through this step, the item will
    // have its tracks sized only through this first step; if a second pass of this step is triggered
    // then the item will have completed a first pass through steps 1-3 as well as the second pass of
    // this step prior to returning its size for consideration in this grid’s column sizing. Again, this
    // introspection is recursive.

    // https://www.w3.org/TR/css-grid-2/#algo-track-sizing
    // 12.3. Track Sizing Algorithm

    // The remainder of this section is the track sizing algorithm, which calculates from the min and
    // max track sizing functions the used track size. Each track has a base size, a <length> which
    // grows throughout the algorithm and which will eventually be the track’s final size, and a growth
    // limit, a <length> which provides a desired maximum size for the base size. There are 5 steps:

    // 1. Initialize Track Sizes
    // 2. Resolve Intrinsic Track Sizes
    // 3. Maximize Tracks
    // 4. Expand Flexible Tracks
    // 5. Expand Stretched auto Tracks

    calculate_sizes_of_columns(box, available_space);

    // https://www.w3.org/TR/css-grid-2/#algo-overview
    // 12.1. Grid Sizing Algorithm
    // 2. Next, the track sizing algorithm resolves the sizes of the grid rows.
    // In this process, any grid item which is subgridded in the grid container’s block axis is treated
    // as empty and its grid items (the grandchildren) are treated as direct children of the grid
    // container (their grandparent). This introspection is recursive.

    // As with sizing columns, items which are subgridded only in the inline axis, and whose grid
    // container size in the block axis depends on the size of its contents are also introspected. (As
    // with sizing columns, the size contribution to this grid’s row sizing is taken under the provision
    // of having determined its track sizing only up to this corresponding point in the algorithm; and
    // again, this introspection is recursive.)

    // To find the inline-axis available space for any items whose block-axis size contributions require
    // it, use the grid column sizes calculated in the previous step. If the grid container’s inline
    // size is definite, also apply justify-content to account for the effective column gap sizes.

    // https://www.w3.org/TR/css-grid-2/#algo-track-sizing
    // 12.3. Track Sizing Algorithm

    // The remainder of this section is the track sizing algorithm, which calculates from the min and
    // max track sizing functions the used track size. Each track has a base size, a <length> which
    // grows throughout the algorithm and which will eventually be the track’s final size, and a growth
    // limit, a <length> which provides a desired maximum size for the base size. There are 5 steps:

    // 1. Initialize Track Sizes
    // 2. Resolve Intrinsic Track Sizes
    // 3. Maximize Tracks
    // 4. Expand Flexible Tracks
    // 5. Expand Stretched auto Tracks

    calculate_sizes_of_rows(box);

    // https://www.w3.org/TR/css-grid-2/#algo-overview
    // 12.1. Grid Sizing Algorithm
    // 3. Then, if the min-content contribution of any grid item has changed based on the row sizes and
    // alignment calculated in step 2, re-resolve the sizes of the grid columns with the new min-content
    // and max-content contributions (once only).

    // To find the block-axis available space for any items whose inline-axis size contributions require
    // it, use the grid row sizes calculated in the previous step. If the grid container’s block size is
    // definite, also apply align-content to account for the effective row gap sizes

    // 4. Next, if the min-content contribution of any grid item has changed based on the column sizes and
    // alignment calculated in step 3, re-resolve the sizes of the grid rows with the new min-content
    // and max-content contributions (once only).

    // To find the inline-axis available space for any items whose block-axis size contributions require
    // it, use the grid column sizes calculated in the previous step. If the grid container’s inline
    // size is definite, also apply justify-content to account for the effective column gap sizes.

    // 5. Finally, the grid container is sized using the resulting size of the grid as its content size,
    // and the tracks are aligned within the grid container according to the align-content and
    // justify-content properties.

    // Once the size of each grid area is thus established, the grid items are laid out into their
    // respective containing blocks. The grid area’s width and height are considered definite for this
    // purpose.

    auto layout_box = [&](int row_start, int row_end, int column_start, int column_end, Box const& child_box) -> void {
        auto& child_box_state = m_state.get_mutable(child_box);
        float x_start = 0;
        float x_end = 0;
        float y_start = 0;
        float y_end = 0;
        for (int i = 0; i < column_start; i++)
            x_start += m_grid_columns[i].base_size;
        for (int i = 0; i < column_end; i++)
            x_end += m_grid_columns[i].base_size;
        for (int i = 0; i < row_start; i++)
            y_start += m_grid_rows[i].base_size;
        for (int i = 0; i < row_end; i++)
            y_end += m_grid_rows[i].base_size;
        child_box_state.set_content_width(x_end - x_start);
        child_box_state.set_content_height(y_end - y_start);
        child_box_state.offset = { x_start, y_start };

        auto available_space_for_children = AvailableSpace(AvailableSize::make_definite(child_box_state.content_width()), AvailableSize::make_definite(child_box_state.content_height()));
        if (auto independent_formatting_context = layout_inside(child_box, LayoutMode::Normal, available_space_for_children))
            independent_formatting_context->parent_context_did_dimension_child_root_box();
    };

    for (auto& positioned_box : m_positioned_boxes) {
        auto resolved_row_start = box.computed_values().row_gap().is_auto() ? positioned_box.row : positioned_box.row * 2;
        auto resolved_row_span = box.computed_values().row_gap().is_auto() ? positioned_box.row_span : positioned_box.row_span * 2;
        if (!box.computed_values().row_gap().is_auto() && resolved_row_start == 0)
            resolved_row_span -= 1;
        if (resolved_row_start + resolved_row_span > static_cast<int>(m_grid_rows.size()))
            resolved_row_span = m_grid_rows.size() - resolved_row_start;

        auto resolved_column_start = box.computed_values().column_gap().is_auto() ? positioned_box.column : positioned_box.column * 2;
        auto resolved_column_span = box.computed_values().column_gap().is_auto() ? positioned_box.column_span : positioned_box.column_span * 2;
        if (!box.computed_values().column_gap().is_auto() && resolved_column_start == 0)
            resolved_column_span -= 1;
        if (resolved_column_start + resolved_column_span > static_cast<int>(m_grid_columns.size()))
            resolved_column_span = m_grid_columns.size() - resolved_column_start;

        layout_box(
            resolved_row_start,
            resolved_row_start + resolved_row_span,
            resolved_column_start,
            resolved_column_start + resolved_column_span,
            positioned_box.box);
    }

    float total_y = 0;
    for (auto& grid_row : m_grid_rows)
        total_y += grid_row.base_size;
    m_automatic_content_height = total_y;
}

CSSPixels GridFormattingContext::automatic_content_height() const
{
    return m_automatic_content_height;
}

bool GridFormattingContext::is_auto_positioned_row(CSS::GridTrackPlacement const& grid_row_start, CSS::GridTrackPlacement const& grid_row_end) const
{
    return is_auto_positioned_track(grid_row_start, grid_row_end);
}

bool GridFormattingContext::is_auto_positioned_column(CSS::GridTrackPlacement const& grid_column_start, CSS::GridTrackPlacement const& grid_column_end) const
{
    return is_auto_positioned_track(grid_column_start, grid_column_end);
}

bool GridFormattingContext::is_auto_positioned_track(CSS::GridTrackPlacement const& grid_track_start, CSS::GridTrackPlacement const& grid_track_end) const
{
    return grid_track_start.is_auto_positioned() && grid_track_end.is_auto_positioned();
}

float GridFormattingContext::get_free_space_x(AvailableSpace const& available_space)
{
    // https://www.w3.org/TR/css-grid-2/#algo-terms
    // free space: Equal to the available grid space minus the sum of the base sizes of all the grid
    // tracks (including gutters), floored at zero. If available grid space is indefinite, the free
    // space is indefinite as well.
    // FIXME: do indefinite space
    if (!available_space.width.is_definite())
        return 0;
    auto sum_base_sizes = 0;
    for (auto& grid_column : m_grid_columns)
        sum_base_sizes += grid_column.base_size;
    return max(0, available_space.width.to_px().value() - sum_base_sizes);
}

float GridFormattingContext::get_free_space_y(Box const& box)
{
    // https://www.w3.org/TR/css-grid-2/#algo-terms
    // free space: Equal to the available grid space minus the sum of the base sizes of all the grid
    // tracks (including gutters), floored at zero. If available grid space is indefinite, the free
    // space is indefinite as well.
    auto sum_base_sizes = 0;
    for (auto& grid_row : m_grid_rows)
        sum_base_sizes += grid_row.base_size;
    auto& box_state = m_state.get_mutable(box);
    if (box_state.has_definite_height())
        return max(0, absolute_content_rect(box, m_state).height() - sum_base_sizes);
    return -1;
}

int GridFormattingContext::get_line_index_by_line_name(DeprecatedString const& needle, CSS::GridTrackSizeList grid_track_size_list)
{
    if (grid_track_size_list.track_list().size() == 0)
        return -1;

    auto repeated_tracks_count = 0;
    for (size_t x = 0; x < grid_track_size_list.track_list().size(); x++) {
        if (grid_track_size_list.track_list()[x].is_repeat()) {
            // FIXME: Calculate amount of columns/rows if auto-fill/fit
            if (!grid_track_size_list.track_list()[x].repeat().is_default())
                return -1;
            auto repeat = grid_track_size_list.track_list()[x].repeat().grid_track_size_list();
            for (size_t y = 0; y < repeat.track_list().size(); y++) {
                for (size_t z = 0; z < repeat.line_names()[y].size(); z++) {
                    if (repeat.line_names()[y][z] == needle)
                        return x + repeated_tracks_count;
                    repeated_tracks_count++;
                }
            }
        } else {
            for (size_t y = 0; y < grid_track_size_list.line_names()[x].size(); y++) {
                if (grid_track_size_list.line_names()[x][y] == needle)
                    return x + repeated_tracks_count;
            }
        }
    }
    for (size_t y = 0; y < grid_track_size_list.line_names()[grid_track_size_list.track_list().size()].size(); y++) {
        if (grid_track_size_list.line_names()[grid_track_size_list.track_list().size()][y] == needle)
            return grid_track_size_list.track_list().size() + repeated_tracks_count;
    }
    return -1;
}

OccupationGrid::OccupationGrid(int column_count, int row_count)
{
    Vector<bool> occupation_grid_row;
    for (int column_index = 0; column_index < max(column_count, 1); column_index++)
        occupation_grid_row.append(false);
    for (int row_index = 0; row_index < max(row_count, 1); row_index++)
        m_occupation_grid.append(occupation_grid_row);
}

OccupationGrid::OccupationGrid()
{
}

void OccupationGrid::maybe_add_column(int needed_number_of_columns)
{
    if (needed_number_of_columns <= column_count())
        return;
    auto column_count_before_modification = column_count();
    for (auto& occupation_grid_row : m_occupation_grid)
        for (int idx = 0; idx < needed_number_of_columns - column_count_before_modification; idx++)
            occupation_grid_row.append(false);
}

void OccupationGrid::maybe_add_row(int needed_number_of_rows)
{
    if (needed_number_of_rows <= row_count())
        return;

    Vector<bool> new_occupation_grid_row;
    for (int idx = 0; idx < column_count(); idx++)
        new_occupation_grid_row.append(false);

    for (int idx = 0; idx < needed_number_of_rows - row_count(); idx++)
        m_occupation_grid.append(new_occupation_grid_row);
}

void OccupationGrid::set_occupied(int column_start, int column_end, int row_start, int row_end)
{
    for (int row_index = 0; row_index < row_count(); row_index++) {
        if (row_index >= row_start && row_index < row_end) {
            for (int column_index = 0; column_index < column_count(); column_index++) {
                if (column_index >= column_start && column_index < column_end)
                    set_occupied(column_index, row_index);
            }
        }
    }
}

void OccupationGrid::set_occupied(int column_index, int row_index)
{
    m_occupation_grid[row_index][column_index] = true;
}

bool OccupationGrid::is_occupied(int column_index, int row_index)
{
    return m_occupation_grid[row_index][column_index];
}

}