package toffee

  1. Overview
  2. Docs

Source file toffee_compute_block.ml

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
(* Block layout computation algorithm *)

module Style = Toffee_style
module Tree = Toffee_tree
open Toffee_geometry
open Style
open Tree

(* Helper module for Option<float> operations used in layout *)
module Helpers = struct
  let maybe_clamp value min max =
    match (min, max) with
    | Some min_val, Some max_val -> Float.max min_val (Float.min max_val value)
    | Some min_val, None -> Float.max min_val value
    | None, Some max_val -> Float.min max_val value
    | None, None -> value
end

(* Whether a block container stretches a child with an auto width to its own
   width: not a table, nor a replaced element, whose auto width is its natural
   width (CSS 2 10.3.4). *)
let is_stretched (style : Style.t) : bool =
  not (Style.item_is_table style || Style.item_is_replaced style)

(* Per-child data that is accumulated and modified over the course of the layout
   algorithm *)
type block_item = {
  node_id : Node_id.t; (* The identifier for the associated node *)
  order : int;
      (* The "source order" of the item. This is the index of the item within
         the children iterator, and controls the order in which the nodes are
         placed *)
  is_stretched : bool;
      (* Whether an auto width stretches to the container's: not for tables and
         replaced elements *)
  size : float option size; (* The base size of this item *)
  min_size : float option size; (* The minimum allowable size of this item *)
  max_size : float option size; (* The maximum allowable size of this item *)
  overflow : overflow point; (* The overflow style of the item *)
  scrollbar_width : float;
      (* The width of the item's scrollbars (if it has scrollbars) *)
  position : position; (* The position style of the item *)
  inset : length_percentage_auto rect; (* The final offset of this item *)
  margin : length_percentage_auto rect; (* The margin of this item *)
  padding : Rectf.t; (* The padding of this item *)
  border : Rectf.t; (* The border of this item *)
  padding_border_sum : Sizef.t;
      (* The sum of padding and border for this item *)
  mutable static_position : Pointf.t;
      (* The computed "static position" of this item. The static position is the
         position taking into account padding, border, margins, and
         scrollbar_gutters but not inset *)
  mutable can_be_collapsed_through : bool;
      (* Whether margins can be collapsed through this item *)
  mutable final_layout : Layout.t option;
      (* The layout of an in-flow item, which the container commits to the
         tree from its final layout alone *)
}

(* Generate a list of block items from the children of a node *)
let generate_item_list (type t)
    (module Tree : Tree.LAYOUT_PARTIAL_TREE with type t = t) (tree : t)
    (node : Node_id.t) (node_inner_size : float option size) : block_item list =
  (* Get calc resolver from tree *)
  let calc = Tree.resolve_calc_value tree in

  let child_count = Tree.child_count tree node in
  let items = ref [] in
  let order = ref 0 in
  for i = 0 to child_count - 1 do
    let child_node_id = Tree.get_child_id tree node i in
    let child_style = Tree.get_core_container_style tree child_node_id in
    if Style.box_generation_mode child_style <> Box_generation_mode.None then (
      let aspect_ratio = Style.aspect_ratio child_style in
      let padding =
        Style.padding child_style
        |> Length_percentage.resolve_rect_or_zero node_inner_size.width calc
      in
      let border =
        Style.border child_style
        |> Length_percentage.resolve_rect_or_zero node_inner_size.width calc
      in
      let pb_sum = Rectf.sum_axes (Rectf.add padding border) in
      let box_sizing_adjustment =
        if Style.box_sizing child_style = Box_sizing.Content_box then pb_sum
        else Sizef.zero
      in

      let item =
        {
          node_id = child_node_id;
          order = !order;
          is_stretched = is_stretched child_style;
          size =
            (let size_dimensions = Style.size child_style in
             let resolved_size : float option size =
               {
                 width =
                   Dimension.maybe_resolve
                     (Size.get Inline size_dimensions)
                     node_inner_size.width calc;
                 height =
                   Dimension.maybe_resolve
                     (Size.get Block size_dimensions)
                     node_inner_size.height calc;
               }
             in
             resolved_size
             |> Size.apply_aspect_ratio aspect_ratio
             |> Sizef.maybe_add box_sizing_adjustment);
          min_size =
            (let min_size_dimensions = Style.min_size child_style in
             let resolved_min_size : float option size =
               {
                 width =
                   Dimension.maybe_resolve
                     (Size.get Inline min_size_dimensions)
                     node_inner_size.width calc;
                 height =
                   Dimension.maybe_resolve
                     (Size.get Block min_size_dimensions)
                     node_inner_size.height calc;
               }
             in
             resolved_min_size
             |> Size.apply_aspect_ratio aspect_ratio
             |> Sizef.maybe_add box_sizing_adjustment);
          max_size =
            (let max_size_dimensions = Style.max_size child_style in
             let resolved_max_size : float option size =
               {
                 width =
                   Dimension.maybe_resolve
                     (Size.get Inline max_size_dimensions)
                     node_inner_size.width calc;
                 height =
                   Dimension.maybe_resolve
                     (Size.get Block max_size_dimensions)
                     node_inner_size.height calc;
               }
             in
             resolved_max_size
             |> Size.apply_aspect_ratio aspect_ratio
             |> Sizef.maybe_add box_sizing_adjustment);
          overflow = Style.overflow child_style;
          scrollbar_width = Style.scrollbar_width child_style;
          position = Style.position child_style;
          inset = Style.inset child_style;
          margin = Style.margin child_style;
          padding;
          border;
          padding_border_sum = pb_sum;
          (* Fields to be computed later (for now we initialise with dummy
             values) *)
          static_position = Pointf.zero;
          can_be_collapsed_through = false;
          final_layout = None;
        }
      in
      items := item :: !items;
      order := !order + 1)
  done;
  List.rev !items

(* Compute the content-based width when the container width is not known *)
let determine_content_based_container_width (type t)
    (module Tree : Tree.LAYOUT_PARTIAL_TREE with type t = t) (tree : t)
    (items : block_item list) (available_width : Available_space.t) : float =
  let available_space =
    Size.{ width = available_width; height = Available_space.min_content }
  in

  let max_child_width = ref 0.0 in
  List.iter
    (fun item ->
      if item.position <> Position.Absolute then
        let known_dimensions =
          Size.clamp_option item.min_size item.max_size item.size
        in

        let calc = Tree.resolve_calc_value tree in
        let parent_width = Available_space.to_option available_space.width in
        let item_x_margin_sum =
          Length_percentage_auto.resolve_or_zero item.margin.left parent_width
            calc
          +. Length_percentage_auto.resolve_or_zero item.margin.right
               parent_width calc
        in
        let width =
          match known_dimensions.width with
          | Some w -> w
          | None ->
              let available_space_for_child =
                Size.
                  {
                    width =
                      Available_space.sub_or_zero available_space.width
                        (Some item_x_margin_sum);
                    height = available_space.height;
                  }
              in
              let size_and_baselines =
                Tree.compute_child_layout tree item.node_id
                  (Layout_input.make ~run_mode:Run_mode.Compute_size
                     ~sizing_mode:Sizing_mode.Inherent_size
                     ~axis:Requested_axis.Horizontal ~known_dimensions
                     ~parent_size:Size.none
                     ~available_space:available_space_for_child
                     ~vertical_margins_are_collapsible:
                       { start = true; end_ = true })
              in
              (Layout_output.size size_and_baselines).width
        in
        (* A child contributes its margin box, whether its width is known or
           measured *)
        let width =
          Float.max width item.padding_border_sum.width +. item_x_margin_sum
        in
        max_child_width := Float.max !max_child_width width)
    items;

  !max_child_width

(* Perform final layout on in-flow children *)
let perform_final_layout_on_in_flow_children (type t)
    (module Tree : Tree.LAYOUT_PARTIAL_TREE with type t = t) (tree : t)
    (items : block_item list) (container_outer_width : float)
    (container_percentage_resolution_height : float option)
    (content_box_inset : Rectf.t) (resolved_content_box_inset : Rectf.t)
    (text_align : Text_align.t) (justify_items : Align_items.t option)
    (own_margins_collapse_with_children : bool line)
    (overflow_acc : Toffee_compute_helpers.overflow_acc)
    (overflow_flags : Toffee_compute_helpers.overflow_flags)
    (overflow_origin : Rectf.t) (overflow_padding : Rectf.t) :
    float * Collapsible_margin_set.t * Collapsible_margin_set.t =
  (* Resolve container_inner_width for sizing child nodes using initial
     content_box_inset *)
  let container_inner_width =
    container_outer_width -. Rectf.horizontal_axis_sum content_box_inset
  in
  (* The children's percentage heights and vertical insets resolve against
     the container's content box height, when it is definite. *)
  let container_percentage_resolution_height =
    Option.map
      (fun height ->
        height -. Rectf.vertical_axis_sum resolved_content_box_inset)
      container_percentage_resolution_height
  in
  let parent_size =
    Size.
      {
        width = Some container_inner_width;
        height = container_percentage_resolution_height;
      }
  in
  (* Vertical available space in block flow is indefinite, not a min-content
     constraint: Max_content is how toffee says indefinite. Under Min_content
     every descendant grid believed it was sized under a min-content
     constraint, in which maximizing the tracks has no free space, so auto rows
     holding only scroll containers (whose automatic minimum size is 0)
     collapsed to zero height. Browsers size such rows to their content. *)
  let available_space =
    Size.
      {
        width = Available_space.of_float container_inner_width;
        height = Available_space.max_content;
      }
  in

  let committed_y_offset = ref resolved_content_box_inset.top in
  let y_offset_for_absolute = ref resolved_content_box_inset.top in
  let first_child_top_margin_set = ref Collapsible_margin_set.zero in
  let active_collapsible_margin_set = ref Collapsible_margin_set.zero in
  let is_collapsing_with_first_margin_set = ref true in

  let calc = Tree.resolve_calc_value tree in

  List.iter
    (fun item ->
      if item.position = Position.Absolute then
        (* For absolute items, only set static position *)
        item.static_position <-
          Pointf.
            { x = resolved_content_box_inset.left; y = !y_offset_for_absolute }
      else
        (* Handle in-flow items *)
        let item_margin =
          item.margin
          |> Rect.map (fun margin ->
              Length_percentage_auto.resolve_to_option_with_calc margin
                container_inner_width calc)
        in
        let item_non_auto_margin =
          Rectf.
            {
              left = Option.value item_margin.left ~default:0.0;
              right = Option.value item_margin.right ~default:0.0;
              top = Option.value item_margin.top ~default:0.0;
              bottom = Option.value item_margin.bottom ~default:0.0;
            }
        in
        let item_non_auto_x_margin_sum =
          Rectf.horizontal_axis_sum item_non_auto_margin
        in

        (* justify-self, or the container's justify-items, on a block-level
           box (CSS Box Alignment 3, 6.1; Taffy #1250). Normal and stretch lay
           it out by the default rules, except that stretch also stretches a
           table or replaced box; any other value sizes an auto width as
           fit-content and aligns the margin box *)
        let justify_self =
          match
            Style.justify_self (Tree.get_core_container_style tree item.node_id)
          with
          | Some _ as justify_self -> justify_self
          | None -> justify_items
        in
        let aligned =
          match justify_self with
          | None | Some Align_items.Stretch -> false
          | Some _ -> true
        in
        let known_dimensions =
          if not (item.is_stretched || justify_self = Some Align_items.Stretch)
          then Size.none
          else
            item.size
            |> Size.map_width (fun width ->
                match width with
                | Some w ->
                    Some
                      (Helpers.maybe_clamp w item.min_size.width
                         item.max_size.width)
                | None ->
                    let stretched_width =
                      container_inner_width -. item_non_auto_x_margin_sum
                    in
                    let width =
                      if not aligned then stretched_width
                      else
                        (* fit-content: the width the box takes in the
                           stretched width *)
                        (Layout_output.size
                           (Tree.compute_child_layout tree item.node_id
                              (Layout_input.make ~run_mode:Run_mode.Compute_size
                                 ~sizing_mode:Sizing_mode.Inherent_size
                                 ~axis:Requested_axis.Horizontal
                                 ~known_dimensions:Size.none ~parent_size
                                 ~available_space:
                                   Size.
                                     {
                                       width =
                                         Available_space.of_float
                                           stretched_width;
                                       height = Available_space.max_content;
                                     }
                                 ~vertical_margins_are_collapsible:
                                   Line.both_true)))
                          .width
                    in
                    Some
                      (Helpers.maybe_clamp width item.min_size.width
                         item.max_size.width))
            |> Size.clamp_option item.min_size item.max_size
        in

        let item_layout =
          Tree.compute_child_layout tree item.node_id
            (Layout_input.make ~run_mode:Run_mode.Perform_layout
               ~sizing_mode:Sizing_mode.Inherent_size ~axis:Requested_axis.Both
               ~known_dimensions ~parent_size
               ~available_space:
                 Size.
                   {
                     width =
                       Available_space.sub_or_zero available_space.width
                         (Some item_non_auto_x_margin_sum);
                     height = available_space.height;
                   }
               ~vertical_margins_are_collapsible:Line.both_true)
        in
        let final_size = Layout_output.size item_layout in

        let top_margin_set =
          Collapsible_margin_set.collapse_with_margin
            (Layout_output.top_margin item_layout)
            (Option.value item_margin.top ~default:0.0)
        in
        let bottom_margin_set =
          Collapsible_margin_set.collapse_with_margin
            (Layout_output.bottom_margin item_layout)
            (Option.value item_margin.bottom ~default:0.0)
        in

        (* Expand auto margins to fill available space *)
        (* Note: Vertical auto-margins for relatively positioned block items simply resolve to 0. *)
        (* See: https://www.w3.org/TR/CSS21/visudet.html#abs-non-replaced-width *)
        let free_x_space =
          Float.max 0.0
            (container_inner_width -. final_size.width
           -. item_non_auto_x_margin_sum)
        in
        let x_axis_auto_margin_size =
          let auto_margin_count =
            (if Option.is_none item_margin.left then 1 else 0)
            + if Option.is_none item_margin.right then 1 else 0
          in
          if auto_margin_count > 0 then
            free_x_space /. float_of_int auto_margin_count
          else 0.0
        in
        let resolved_margin =
          Rectf.
            {
              left =
                Option.value item_margin.left ~default:x_axis_auto_margin_size;
              right =
                Option.value item_margin.right ~default:x_axis_auto_margin_size;
              top = Collapsible_margin_set.resolve top_margin_set;
              bottom = Collapsible_margin_set.resolve bottom_margin_set;
            }
        in

        (* Resolve item inset *)
        let inset =
          Rect.zip_size
            Size.
              {
                width = Some container_inner_width;
                height = container_percentage_resolution_height;
              }
            (fun p s -> Length_percentage_auto.maybe_resolve p s calc)
            item.inset
        in
        let inset_offset =
          Pointf.
            {
              x =
                (match inset.left with
                | Some l -> l
                | None -> (
                    match inset.right with Some r -> -.r | None -> 0.0));
              y =
                (match inset.top with
                | Some t -> t
                | None -> (
                    match inset.bottom with Some b -> -.b | None -> 0.0));
            }
        in

        let y_margin_offset =
          if
            !is_collapsing_with_first_margin_set
            && own_margins_collapse_with_children.start
          then 0.0
          else
            Collapsible_margin_set.resolve
              (Collapsible_margin_set.collapse_with_margin
                 !active_collapsible_margin_set
                 resolved_margin.top)
        in

        item.can_be_collapsed_through <-
          Layout_output.margins_can_collapse_through item_layout;
        item.static_position <-
          Pointf.
            {
              x = resolved_content_box_inset.left;
              y =
                !committed_y_offset
                +. Collapsible_margin_set.resolve !active_collapsible_margin_set;
            };
        let location =
          Pointf.
            {
              x =
                resolved_content_box_inset.left +. inset_offset.x
                +. resolved_margin.left;
              y = !committed_y_offset +. inset_offset.y +. y_margin_offset;
            }
        in

        (* Apply alignment *)
        let item_outer_width =
          final_size.width +. Rectf.horizontal_axis_sum resolved_margin
        in
        let location =
          if
            aligned
            && Option.is_some item_margin.left
            && Option.is_some item_margin.right
          then
            (* justify-self takes precedence over text-align (stretch too, as
               in Chromium), auto margins over justify-self *)
            let free_x_space = container_inner_width -. item_outer_width in
            match justify_self with
            | Some Align_items.Center ->
                Pointf.{ location with x = location.x +. (free_x_space /. 2.0) }
            | Some (Align_items.End | Align_items.Flex_end) ->
                Pointf.{ location with x = location.x +. free_x_space }
            | _ -> location
          else if
            justify_self = None && item_outer_width < container_inner_width
          then
            match text_align with
            | Text_align.Auto -> location
            | Text_align.Legacy_left -> location (* Left aligned by default *)
            | Text_align.Legacy_right ->
                Pointf.
                  {
                    location with
                    x = location.x +. (container_inner_width -. item_outer_width);
                  }
            | Text_align.Legacy_center ->
                Pointf.
                  {
                    location with
                    x =
                      location.x
                      +. ((container_inner_width -. item_outer_width) /. 2.0);
                  }
          else location
        in

        let scrollbar_size =
          Sizef.
            {
              width =
                (if item.overflow.y = Overflow.Scroll then item.scrollbar_width
                 else 0.0);
              height =
                (if item.overflow.x = Overflow.Scroll then item.scrollbar_width
                 else 0.0);
            }
        in

        item.final_layout <-
          Some
            (Layout.make ~order:item.order ~size:final_size
               ~scrollable_overflow:
                 (Layout_output.scrollable_overflow item_layout)
               ~scrollbar_size ~location ~padding:item.padding
               ~border:item.border ~margin:resolved_margin);

        (* Add the item to the container's scrollable overflow *)
        Toffee_compute_helpers.add_overflow_contribution overflow_acc
          ~flags:overflow_flags ~origin:overflow_origin ~client:Sizef.zero
          ~padding:overflow_padding ~location ~size:final_size
          ~overflow_area:(Layout_output.scrollable_overflow item_layout)
          ~border:item.border ~overflow:item.overflow ~margin:resolved_margin;

        (* Update first_child_top_margin_set *)
        if !is_collapsing_with_first_margin_set then
          if item.can_be_collapsed_through then
            first_child_top_margin_set :=
              !first_child_top_margin_set
              |> Collapsible_margin_set.collapse_with_set top_margin_set
              |> Collapsible_margin_set.collapse_with_set bottom_margin_set
          else (
            first_child_top_margin_set :=
              Collapsible_margin_set.collapse_with_set
                !first_child_top_margin_set
                top_margin_set;
            is_collapsing_with_first_margin_set := false);

        (* Update active_collapsible_margin_set *)
        if item.can_be_collapsed_through then (
          active_collapsible_margin_set :=
            !active_collapsible_margin_set
            |> Collapsible_margin_set.collapse_with_set top_margin_set
            |> Collapsible_margin_set.collapse_with_set bottom_margin_set;
          y_offset_for_absolute :=
            !committed_y_offset +. final_size.height +. y_margin_offset)
        else (
          committed_y_offset :=
            !committed_y_offset +. final_size.height +. y_margin_offset;
          active_collapsible_margin_set := bottom_margin_set;
          y_offset_for_absolute :=
            !committed_y_offset
            +. Collapsible_margin_set.resolve !active_collapsible_margin_set))
    items;

  let last_child_bottom_margin_set = !active_collapsible_margin_set in
  let bottom_y_margin_offset =
    if own_margins_collapse_with_children.end_ then 0.0
    else Collapsible_margin_set.resolve last_child_bottom_margin_set
  in

  committed_y_offset :=
    !committed_y_offset +. resolved_content_box_inset.bottom
    +. bottom_y_margin_offset;
  let content_height = Float.max 0.0 !committed_y_offset in
  (content_height, !first_child_top_margin_set, last_child_bottom_margin_set)

(* Perform absolute layout on absolutely positioned children *)
let perform_absolute_layout_on_absolute_children (type t)
    (module Tree : Tree.LAYOUT_PARTIAL_TREE with type t = t) (tree : t)
    (items : block_item list) (area_size : Sizef.t) (area_offset : Pointf.t)
    (outer_size : Sizef.t) (content_box_inset : Rectf.t)
    (overflow_acc : Toffee_compute_helpers.overflow_acc)
    (overflow_flags : Toffee_compute_helpers.overflow_flags)
    (overflow_origin : Rectf.t) : unit =
  let area_width = area_size.width in
  let area_height = area_size.height in

  let calc = Tree.resolve_calc_value tree in

  List.iter
    (fun item ->
      if item.position = Position.Absolute then
        let child_style = Tree.get_core_container_style tree item.node_id in

        (* Skip items that are display:none or are not position:absolute *)
        if
          Style.box_generation_mode child_style <> Box_generation_mode.None
          && Style.position child_style = Position.Absolute
        then (
          let aspect_ratio = Style.aspect_ratio child_style in
          let margin =
            Style.margin child_style
            |> Rect.map (fun margin ->
                Length_percentage_auto.resolve_to_option_with_calc margin
                  area_width calc)
          in
          let padding =
            Style.padding child_style
            |> Length_percentage.resolve_rect_or_zero (Some area_width) calc
          in
          let border =
            Style.border child_style
            |> Length_percentage.resolve_rect_or_zero (Some area_width) calc
          in
          let padding_border_sum = Rectf.sum_axes (Rectf.add padding border) in
          let box_sizing_adjustment =
            if Style.box_sizing child_style = Box_sizing.Content_box then
              padding_border_sum
            else Sizef.zero
          in

          (* Resolve inset *)
          let inset = Style.inset child_style in
          let left =
            Length_percentage_auto.maybe_resolve inset.left (Some area_width)
              calc
          in
          let right =
            Length_percentage_auto.maybe_resolve inset.right (Some area_width)
              calc
          in
          let top =
            Length_percentage_auto.maybe_resolve inset.top (Some area_height)
              calc
          in
          let bottom =
            Length_percentage_auto.maybe_resolve inset.bottom (Some area_height)
              calc
          in

          (* Compute known dimensions from min/max/inherent size styles *)
          let style_size =
            Style.size child_style |> fun dims ->
            Size.
              {
                width =
                  Dimension.maybe_resolve (Size.get Inline dims)
                    (Some area_width) calc;
                height =
                  Dimension.maybe_resolve (Size.get Block dims)
                    (Some area_height) calc;
              }
            |> Size.apply_aspect_ratio aspect_ratio
            |> Sizef.maybe_add box_sizing_adjustment
          in
          let resolved_min_size =
            Style.min_size child_style |> fun dims ->
            Size.
              {
                width =
                  Dimension.maybe_resolve (Size.get Inline dims)
                    (Some area_width) calc;
                height =
                  Dimension.maybe_resolve (Size.get Block dims)
                    (Some area_height) calc;
              }
            |> Size.apply_aspect_ratio aspect_ratio
            |> Sizef.maybe_add box_sizing_adjustment
          in
          let min_size =
            resolved_min_size |> fun size ->
            Size.choose_first size (Sizef.some padding_border_sum)
            |> Sizef.maybe_max padding_border_sum
          in
          let max_size =
            Style.max_size child_style |> fun dims ->
            Size.
              {
                width =
                  Dimension.maybe_resolve (Size.get Inline dims)
                    (Some area_width) calc;
                height =
                  Dimension.maybe_resolve (Size.get Block dims)
                    (Some area_height) calc;
              }
            |> Size.apply_aspect_ratio aspect_ratio
            |> Sizef.maybe_add box_sizing_adjustment
          in
          (* If both min and max in a given axis are set and max <= min then
             this determines the size in that axis *)
          let min_max_definite_size =
            Size.map2
              (fun min max ->
                match (min, max) with
                | Some min_v, Some max_v when max_v <= min_v -> Some min_v
                | _ -> None)
              min_size max_size
          in
          let known_dimensions =
            ref
              (Size.choose_first min_max_definite_size
                 (Size.clamp_option min_size max_size style_size))
          in

          (* Fill in width from left/right and reapply aspect ratio if: - Width
             is not already known - Item has both left and right inset
             properties set *)
          (match (!known_dimensions.width, left, right) with
          | None, Some l, Some r ->
              let new_width_raw =
                area_width
                -. Option.value margin.left ~default:0.0
                -. Option.value margin.right ~default:0.0
                -. l -. r
              in
              known_dimensions :=
                {
                  !known_dimensions with
                  width = Some (Float.max new_width_raw 0.0);
                };
              known_dimensions :=
                !known_dimensions
                |> Size.apply_aspect_ratio aspect_ratio
                |> Size.clamp_option min_size max_size
                |> Size.choose_first min_max_definite_size
          | _ -> ());

          (* Fill in height from top/bottom and reapply aspect ratio if: -
             Height is not already known - Item has both top and bottom inset
             properties set *)
          (match (!known_dimensions.height, top, bottom) with
          | None, Some t, Some b ->
              let new_height_raw =
                area_height
                -. Option.value margin.top ~default:0.0
                -. Option.value margin.bottom ~default:0.0
                -. t -. b
              in
              known_dimensions :=
                {
                  !known_dimensions with
                  height = Some (Float.max new_height_raw 0.0);
                };
              known_dimensions :=
                !known_dimensions
                |> Size.apply_aspect_ratio aspect_ratio
                |> Size.clamp_option min_size max_size
                |> Size.choose_first min_max_definite_size
          | _ -> ());

          let measured_size =
            Tree.compute_child_layout tree item.node_id
              (Layout_input.make ~run_mode:Run_mode.Compute_size
                 ~sizing_mode:Sizing_mode.Content_size ~axis:Requested_axis.Both
                 ~known_dimensions:!known_dimensions
                 ~parent_size:(Sizef.some area_size)
                 ~available_space:
                   Size.
                     {
                       width =
                         Available_space.of_float
                           (Helpers.maybe_clamp area_width min_size.width
                              max_size.width);
                       height =
                         Available_space.of_float
                           (Helpers.maybe_clamp area_height min_size.height
                              max_size.height);
                     }
                 ~vertical_margins_are_collapsible:Line.both_false)
            |> Layout_output.size
          in
          let final_size =
            Sizef.unwrap_or measured_size !known_dimensions |> fun s ->
            let s_opt = Sizef.some s in
            Size.choose_first min_max_definite_size
              (Size.clamp_option min_size max_size s_opt)
            |> fun s_opt -> Sizef.unwrap_or s s_opt
          in

          let layout_output =
            Tree.compute_child_layout tree item.node_id
              (Layout_input.make ~run_mode:Run_mode.Perform_layout
                 ~sizing_mode:Sizing_mode.Content_size ~axis:Requested_axis.Both
                 ~known_dimensions:(Sizef.some final_size)
                 ~parent_size:(Sizef.some area_size)
                 ~available_space:
                   Size.
                     {
                       width =
                         Available_space.of_float
                           (Helpers.maybe_clamp area_width min_size.width
                              max_size.width);
                       height =
                         Available_space.of_float
                           (Helpers.maybe_clamp area_height min_size.height
                              max_size.height);
                     }
                 ~vertical_margins_are_collapsible:Line.both_false)
          in

          (* Auto margins (CSS Position 3, abspos margins): zero unless both
             insets of their axis are set; then they share the free space
             between the insets, and in the inline axis a negative free space
             falls to the end margin alone *)
          let resolve_auto_margins start_inset end_inset start_margin end_margin
              cb_size size ~inline =
            match (start_inset, end_inset) with
            | Some start_inset, Some end_inset -> (
                let free_space = cb_size -. start_inset -. end_inset -. size in
                match (start_margin, end_margin) with
                | Some start, Some end_ -> (start, end_)
                | Some start, None -> (start, free_space -. start)
                | None, Some end_ -> (free_space -. end_, end_)
                | None, None ->
                    if free_space < 0.0 && inline then (0.0, free_space)
                    else (free_space /. 2.0, free_space /. 2.0))
            | _ ->
                ( Option.value start_margin ~default:0.0,
                  Option.value end_margin ~default:0.0 )
          in
          let resolved_margin =
            let left_margin, right_margin =
              resolve_auto_margins left right margin.left margin.right
                area_size.width final_size.width ~inline:true
            and top_margin, bottom_margin =
              resolve_auto_margins top bottom margin.top margin.bottom
                area_size.height final_size.height ~inline:false
            in
            Rectf.
              {
                left = left_margin;
                right = right_margin;
                top = top_margin;
                bottom = bottom_margin;
              }
          in

          let location =
            Pointf.
              {
                x =
                  (match left with
                  | Some l -> l +. resolved_margin.left +. area_offset.x
                  | None -> (
                      match right with
                      | Some r ->
                          area_size.width -. final_size.width -. r
                          -. resolved_margin.right +. area_offset.x
                      | None -> (
                          (* justify-self aligns the margin box within the
                             containing block's content width, as in
                             Chromium *)
                          item.static_position.x +. resolved_margin.left
                          +.
                          match Style.justify_self child_style with
                          | Some
                              (( Align_items.Center | Align_items.End
                               | Align_items.Flex_end ) as justify) ->
                              let free =
                                outer_size.width -. content_box_inset.left
                                -. content_box_inset.right -. final_size.width
                                -. resolved_margin.left -. resolved_margin.right
                              in
                              if justify = Align_items.Center then free /. 2.0
                              else free
                          | _ -> 0.0)));
                y =
                  (match top with
                  | Some t -> t +. resolved_margin.top +. area_offset.y
                  | None -> (
                      match bottom with
                      | Some b ->
                          area_size.height -. final_size.height -. b
                          -. resolved_margin.bottom +. area_offset.y
                      | None -> (
                          (* align-self aligns the margin box around its
                             static position, an area of no height, as in
                             Chromium *)
                          item.static_position.y +. resolved_margin.top
                          -.
                          let outer_height =
                            final_size.height +. resolved_margin.top
                            +. resolved_margin.bottom
                          in
                          match Style.align_self child_style with
                          | Some Align_items.Center -> outer_height /. 2.0
                          | Some (Align_items.End | Align_items.Flex_end) ->
                              outer_height
                          | _ -> 0.0)));
              }
          in

          (* Note: axis intentionally switched here as scrollbars take up space
             in the opposite axis to the axis in which scrolling is enabled. *)
          let scrollbar_size =
            Sizef.
              {
                width =
                  (if item.overflow.y = Overflow.Scroll then
                     item.scrollbar_width
                   else 0.0);
                height =
                  (if item.overflow.x = Overflow.Scroll then
                     item.scrollbar_width
                   else 0.0);
              }
          in

          Tree.set_unrounded_layout tree item.node_id
            (Layout.make ~order:item.order ~size:final_size
               ~scrollable_overflow:
                 (Layout_output.scrollable_overflow layout_output)
               ~scrollbar_size ~location ~padding ~border
               ~margin:resolved_margin);

          (* Add the item to the container's scrollable overflow *)
          Toffee_compute_helpers.add_overflow_contribution overflow_acc
            ~flags:overflow_flags ~origin:overflow_origin ~client:Sizef.zero
            ~padding:Rectf.zero ~location ~size:final_size
            ~overflow_area:(Layout_output.scrollable_overflow layout_output)
            ~border ~overflow:item.overflow ~margin:resolved_margin))
    items

(* Inner computation function - continues after initial setup *)
let compute_inner (type t)
    (module Tree : Tree.LAYOUT_PARTIAL_TREE with type t = t) (tree : t)
    (node_id : Node_id.t) (inputs : Layout_input.t) : Layout_output.t =
  let known_dimensions = Layout_input.known_dimensions inputs in
  let parent_size = Layout_input.parent_size inputs in
  let available_space = Layout_input.available_space inputs in
  let run_mode = Layout_input.run_mode inputs in
  let vertical_margins_are_collapsible =
    Layout_input.vertical_margins_are_collapsible inputs
  in

  let style = Tree.get_core_container_style tree node_id in
  let raw_padding = Style.padding style in
  let raw_border = Style.border style in
  let raw_margin = Style.margin style in
  let aspect_ratio = Style.aspect_ratio style in
  let calc = Tree.resolve_calc_value tree in
  let padding =
    raw_padding
    |> Style.Length_percentage.resolve_rect_or_zero parent_size.width calc
  in
  let border =
    raw_border
    |> Style.Length_percentage.resolve_rect_or_zero parent_size.width calc
  in

  (* Scrollbar gutters are reserved when the `overflow` property is set to
     `Overflow::Scroll`. However, the axis are switched (transposed) because a
     node that scrolls vertically needs *horizontal* space to be reserved for a
     scrollbar *)
  let scrollbar_gutter =
    let overflow = Style.overflow style in
    let scrollbar_width = Style.scrollbar_width style in
    (* TODO: make side configurable based on the `direction` property *)
    Rectf.
      {
        top = 0.0;
        left = 0.0;
        right = (if overflow.y = Overflow.Scroll then scrollbar_width else 0.0);
        bottom = (if overflow.x = Overflow.Scroll then scrollbar_width else 0.0);
      }
  in
  let padding_border = Rectf.add padding border in
  let padding_border_size = Rectf.sum_axes padding_border in
  let content_box_inset = Rectf.add padding_border scrollbar_gutter in

  let box_sizing_adjustment =
    if Style.box_sizing style = Box_sizing.Content_box then padding_border_size
    else Sizef.zero
  in
  let size =
    Style.size style |> fun dims ->
    Size.
      {
        width = Style.Dimension.maybe_resolve dims.width parent_size.width calc;
        height =
          Style.Dimension.maybe_resolve dims.height parent_size.height calc;
      }
    |> Size.apply_aspect_ratio aspect_ratio
    |> Sizef.maybe_add box_sizing_adjustment
  in
  let min_size =
    Style.min_size style |> fun dims ->
    Size.
      {
        width = Style.Dimension.maybe_resolve dims.width parent_size.width calc;
        height =
          Style.Dimension.maybe_resolve dims.height parent_size.height calc;
      }
    |> Size.apply_aspect_ratio aspect_ratio
    |> Sizef.maybe_add box_sizing_adjustment
  in
  let max_size =
    Style.max_size style |> fun dims ->
    Size.
      {
        width = Style.Dimension.maybe_resolve dims.width parent_size.width calc;
        height =
          Style.Dimension.maybe_resolve dims.height parent_size.height calc;
      }
    |> Size.apply_aspect_ratio aspect_ratio
    |> Sizef.maybe_add box_sizing_adjustment
  in
  (* A definite size in one axis transfers through aspect-ratio to the other
     (css-sizing-4). A block parent knows an axis only when it constrains it,
     as the stretched width at final layout does, and leaves it unknown while
     measuring the intrinsic size, so the transfer applies to real constraints
     only. Only an unknown axis takes the derived size, clamped: a known size
     stays as the parent resolved it. *)
  let ratio_known_dimensions =
    Size.choose_first known_dimensions
      (known_dimensions
      |> Size.apply_aspect_ratio aspect_ratio
      |> Size.clamp_option min_size max_size)
  in
  (* A height the ratio gives is a definite basis for the children's
     percentages, but with an automatic minimum height the box still grows
     to its content (css-sizing-4, the automatic minimum size of a box with
     a ratio), unless it is a scroll container: it then floors the height
     the content gives rather than fixing it. NaN where it does not apply. *)
  let ratio_height_floor =
    match (known_dimensions.height, ratio_known_dimensions.height) with
    | None, Some height
      when Option.is_none min_size.height
           &&
           let overflow = Style.overflow style in
           not
             (Overflow.is_container overflow.x
             || Overflow.is_container overflow.y) ->
        height
    | _ -> Float.nan
  in
  let known_dimensions =
    if Float.is_nan ratio_height_floor then ratio_known_dimensions
    else { ratio_known_dimensions with height = None }
  in
  (* A known height that is not definite, as the content height a flex
     container gives an item it does not stretch, is no basis for the
     children's percentages *)
  let percentage_basis =
    if
      (Layout_input.known_dimensions_are_definite inputs).height
      || Option.is_none ratio_known_dimensions.height
    then ratio_known_dimensions
    else { ratio_known_dimensions with height = None }
  in
  let container_content_box_size =
    Sizef.maybe_sub (Rectf.sum_axes content_box_inset) percentage_basis
  in

  (* Determine margin collapsing behaviour *)
  let own_margins_collapse_with_children =
    Line.
      {
        start =
          vertical_margins_are_collapsible.start
          && (not (Overflow.is_container (Style.overflow style).x))
          && (not (Overflow.is_container (Style.overflow style).y))
          && Style.position style = Position.Relative
          && padding.top = 0.0 && border.top = 0.0;
        end_ =
          vertical_margins_are_collapsible.end_
          && (not (Overflow.is_container (Style.overflow style).x))
          && (not (Overflow.is_container (Style.overflow style).y))
          && Style.position style = Position.Relative
          && padding.bottom = 0.0 && border.bottom = 0.0
          && Option.is_none size.height;
      }
  in
  let has_styles_preventing_being_collapsed_through =
    (match Style.display style with Display.Block -> false | _ -> true)
    || Overflow.is_container (Style.overflow style).x
    || Overflow.is_container (Style.overflow style).y
    || Style.position style = Position.Absolute
    || padding.top > 0.0 || padding.bottom > 0.0 || border.top > 0.0
    || border.bottom > 0.0
    || (match size.height with Some h when h > 0.0 -> true | _ -> false)
    || match min_size.height with Some h when h > 0.0 -> true | _ -> false
  in

  let text_align = text_align style in

  (* 1. Generate items *)
  let items =
    generate_item_list (module Tree) tree node_id container_content_box_size
  in

  (* 2. Compute container width *)
  let container_outer_width =
    match known_dimensions.width with
    | Some width -> width
    | None ->
        let available_width =
          Available_space.sub_or_zero available_space.width
            (Some (Rectf.horizontal_axis_sum content_box_inset))
        in
        let intrinsic_width =
          determine_content_based_container_width
            (module Tree)
            tree items available_width
          +. Rectf.horizontal_axis_sum content_box_inset
        in
        intrinsic_width |> fun w ->
        (match (min_size.width, max_size.width) with
          | Some min, Some max -> Float.max min (Float.min max w)
          | Some min, None -> Float.max min w
          | None, Some max -> Float.min max w
          | None, None -> w)
        |> Float.max padding_border_size.width
  in

  (* Short-circuit if computing size and both dimensions known *)
  match (run_mode, known_dimensions.height) with
  | Run_mode.Compute_size, Some container_outer_height ->
      Layout_output.from_outer_size
        Sizef.{ width = container_outer_width; height = container_outer_height }
  (* Nor is the height computed when only the width is requested. *)
  | Run_mode.Compute_size, None
    when Layout_input.axis inputs = Requested_axis.Horizontal ->
      Layout_output.from_outer_size
        Sizef.{ width = container_outer_width; height = 0.0 }
  | _ ->
      (* The height against which the children's percentages resolve: the
         container's own height when it is known. Its min-height alone is not
         a basis: percentages of an indefinite height behave as auto (CSS 2
         10.5). Under Content_size the container's height and min-height are
         ignored, so they are no basis either: the content size of a flex item
         must not depend on its specified height. *)
      let container_percentage_resolution_height =
        match (percentage_basis.height, size.height, min_size.height) with
        | (Some _ as height), _, _ -> height
        | None, _, _
          when Layout_input.sizing_mode inputs <> Sizing_mode.Inherent_size ->
            None
        | None, Some height, Some min -> Some (Float.max height min)
        | None, height, _ -> height
      in
      (* 3. Perform final item layout and return content height *)
      (* Percentage padding and border resolve against the containing block's
         width, not the box's own: they differ when the box's width does not
         stretch. Without a containing block, at the root, the box's own width
         is the basis. *)
      let percentage_resolution_width =
        Option.value parent_size.width ~default:container_outer_width
      in
      let resolved_padding =
        raw_padding
        |> Style.Length_percentage.resolve_rect_or_zero
             (Some percentage_resolution_width) calc
      in
      let resolved_border =
        raw_border
        |> Style.Length_percentage.resolve_rect_or_zero
             (Some percentage_resolution_width) calc
      in
      let resolved_content_box_inset =
        Rectf.add (Rectf.add resolved_padding resolved_border) scrollbar_gutter
      in
      let is_scroll_container =
        let overflow = Style.overflow style in
        Overflow.is_container overflow.x || Overflow.is_container overflow.y
      in
      (* The container's scrollable overflow, gathered in its padding-box
         frame. A block container's scroll origins are never reversed. *)
      let overflow_acc = Toffee_compute_helpers.overflow_acc () in
      let overflow_flags =
        Toffee_compute_helpers.overflow_flags ~is_scroll_container
          ~reversed_x:false ~reversed_y:false ~margins:Full_margins ()
      in
      let ( intrinsic_outer_height,
            first_child_top_margin_set,
            last_child_bottom_margin_set ) =
        perform_final_layout_on_in_flow_children
          (module Tree)
          tree items container_outer_width
          container_percentage_resolution_height content_box_inset
          resolved_content_box_inset text_align
          (Style.justify_items style)
          own_margins_collapse_with_children overflow_acc overflow_flags
          resolved_border resolved_padding
      in
      let container_outer_height =
        known_dimensions.height
        |> Option.value
             ~default:
               ( (if Float.is_nan ratio_height_floor then intrinsic_outer_height
                  else Float.max intrinsic_outer_height ratio_height_floor)
               |> fun h -> Helpers.maybe_clamp h min_size.height max_size.height
               )
        |> Float.max padding_border_size.height
      in
      let final_outer_size =
        Sizef.{ width = container_outer_width; height = container_outer_height }
      in

      (* Short-circuit if computing size *)
      if run_mode = Run_mode.Compute_size then
        Layout_output.from_outer_size final_outer_size
      else (
        (* Commit the in-flow children's layouts. *)
        List.iter
          (fun item ->
            match item.final_layout with
            | Some layout -> Tree.set_unrounded_layout tree item.node_id layout
            | None -> ())
          items;
        (* 4. Layout absolutely positioned children *)
        let absolute_position_inset =
          Rectf.add resolved_border scrollbar_gutter
        in
        let absolute_position_area =
          Sizef.sub final_outer_size (Rectf.sum_axes absolute_position_inset)
        in
        let absolute_position_offset =
          Pointf.
            {
              x = absolute_position_inset.left;
              y = absolute_position_inset.top;
            }
        in
        perform_absolute_layout_on_absolute_children
          (module Tree)
          tree items absolute_position_area absolute_position_offset
          final_outer_size resolved_content_box_inset overflow_acc
          (Toffee_compute_helpers.with_margins overflow_flags No_margins)
          resolved_border;

        (* 5. Perform hidden layout on hidden children *)
        let len = Tree.child_count tree node_id in
        for order = 0 to len - 1 do
          let child = Tree.get_child_id tree node_id order in
          let child_style = Tree.get_core_container_style tree child in
          if Style.box_generation_mode child_style = Box_generation_mode.None
          then
            (* Note: Rust uses perform_child_layout which internally uses
               RunMode::PerformLayout, not a special hidden layout mode *)
            Tree.compute_child_layout tree child
              (Layout_input.make ~run_mode:Run_mode.Perform_layout
                 ~sizing_mode:Sizing_mode.Inherent_size
                 ~axis:Requested_axis.Both ~known_dimensions:Size.none
                 ~parent_size:Size.none
                 ~available_space:
                   Size.
                     {
                       width = Available_space.max_content;
                       height = Available_space.max_content;
                     }
                 ~vertical_margins_are_collapsible:Line.both_false)
            |> ignore
        done;

        (* 7. Determine whether this node can be collapsed through *)
        let all_in_flow_children_can_be_collapsed_through =
          List.for_all
            (fun item ->
              item.position = Position.Absolute || item.can_be_collapsed_through)
            items
        in
        let can_be_collapsed_through =
          (not has_styles_preventing_being_collapsed_through)
          && all_in_flow_children_can_be_collapsed_through
        in

        (* 8. The container's scrollable overflow: a scroll container's inline
           padding is part of it, even without content *)
        if is_scroll_container then
          Toffee_compute_helpers.union overflow_acc ~left:0.0
            ~right:(resolved_padding.left +. resolved_padding.right)
            ~top:0.0 ~bottom:0.0;
        let scrollable_overflow =
          Toffee_compute_helpers.finalize_overflow overflow_acc
            ~flags:overflow_flags ~size:final_outer_size ~border:resolved_border
            ~client:
              (if is_scroll_container then
                 Sizef.sub final_outer_size
                   (Rectf.sum_axes absolute_position_inset)
               else Sizef.zero)
        in

        (* 9. Return final outputs *)
        let top_margin =
          if own_margins_collapse_with_children.start then
            first_child_top_margin_set
          else
            let margin_top =
              Style.Length_percentage_auto.resolve_or_zero raw_margin.top
                parent_size.width calc
            in
            Collapsible_margin_set.from_margin margin_top
        in
        let bottom_margin =
          if own_margins_collapse_with_children.end_ then
            last_child_bottom_margin_set
          else
            let margin_bottom =
              Style.Length_percentage_auto.resolve_or_zero raw_margin.bottom
                parent_size.width calc
            in
            Collapsible_margin_set.from_margin margin_bottom
        in
        Layout_output.make ~size:final_outer_size ~scrollable_overflow
          ~first_baselines:Point.none ~top_margin ~bottom_margin
          ~margins_can_collapse_through:can_be_collapsed_through)

(* Computes the layout of a block container according to the block layout
   algorithm *)
let compute_block_layout (type t)
    (module Tree : Tree.LAYOUT_PARTIAL_TREE with type t = t) (tree : t)
    (node_id : Node_id.t) (inputs : Layout_input.t) : Layout_output.t =
  let known_dimensions = Layout_input.known_dimensions inputs in
  let parent_size = Layout_input.parent_size inputs in
  let available_space = Layout_input.available_space inputs in
  let run_mode = Layout_input.run_mode inputs in
  let sizing_mode = Layout_input.sizing_mode inputs in

  let style = Tree.get_core_container_style tree node_id in
  let calc = Tree.resolve_calc_value tree in

  (* Pull these out earlier to avoid borrowing issues *)
  let aspect_ratio = Style.aspect_ratio style in
  let padding =
    Style.padding style
    |> Length_percentage.resolve_rect_or_zero parent_size.width calc
  in
  let border =
    Style.border style
    |> Length_percentage.resolve_rect_or_zero parent_size.width calc
  in
  let padding_border_size = Rectf.sum_axes (Rectf.add padding border) in
  let box_sizing_adjustment =
    if Style.box_sizing style = Box_sizing.Content_box then padding_border_size
    else Sizef.zero
  in

  let min_size =
    Style.min_size style |> fun dims ->
    Size.
      {
        width =
          Dimension.maybe_resolve (Size.get Inline dims) parent_size.width calc;
        height =
          Dimension.maybe_resolve (Size.get Block dims) parent_size.height calc;
      }
    |> Size.apply_aspect_ratio aspect_ratio
    |> Sizef.maybe_add box_sizing_adjustment
  in
  let max_size =
    Style.max_size style |> fun dims ->
    Size.
      {
        width =
          Dimension.maybe_resolve (Size.get Inline dims) parent_size.width calc;
        height =
          Dimension.maybe_resolve (Size.get Block dims) parent_size.height calc;
      }
    |> Size.apply_aspect_ratio aspect_ratio
    |> Sizef.maybe_add box_sizing_adjustment
  in
  let clamped_style_size =
    if sizing_mode = Sizing_mode.Inherent_size then
      Style.size style |> fun dims ->
      Size.
        {
          width =
            Dimension.maybe_resolve (Size.get Inline dims) parent_size.width
              calc;
          height =
            Dimension.maybe_resolve (Size.get Block dims) parent_size.height
              calc;
        }
      |> Size.apply_aspect_ratio aspect_ratio
      |> Sizef.maybe_add box_sizing_adjustment
      |> Size.clamp_option min_size max_size
    else Size.none
  in

  (* If both min and max in a given axis are set and max <= min then this
     determines the size in that axis *)
  let min_max_definite_size =
    Size.map2
      (fun min max ->
        match (min, max) with
        | Some min_v, Some max_v when max_v <= min_v -> Some min_v
        | _ -> None)
      min_size max_size
  in

  let styled_based_known_dimensions =
    known_dimensions |> fun dims ->
    Size.choose_first dims min_max_definite_size |> fun dims ->
    Size.choose_first dims clamped_style_size
    |> Sizef.maybe_max padding_border_size
  in

  (* Short-circuit layout if the container's size is fully determined by the
     container's size and the run mode is ComputeSize (and thus the container's
     size is all that we're interested in) *)
  if run_mode = Run_mode.Compute_size then
    match styled_based_known_dimensions with
    | { width = Some width; height = Some height } ->
        Layout_output.from_outer_size Sizef.{ width; height }
    (* Nor when the width is known and only the width is requested. *)
    | { width = Some width; height = None }
      when Layout_input.axis inputs = Requested_axis.Horizontal ->
        Layout_output.from_outer_size Sizef.{ width; height = 0.0 }
    | _ ->
        (* Continue with compute_inner even in Compute_size mode if size not
           fully determined *)
        compute_inner
          (module Tree)
          tree node_id
          (Layout_input.make ~run_mode ~sizing_mode
             ~axis:(Layout_input.axis inputs)
             ~known_dimensions:styled_based_known_dimensions ~parent_size
             ~available_space
             ~vertical_margins_are_collapsible:
               (Layout_input.vertical_margins_are_collapsible inputs))
  else
    (* TODO: debug_log "BLOCK" *)
    compute_inner
      (module Tree)
      tree node_id
      (Layout_input.make ~run_mode ~sizing_mode ~axis:(Layout_input.axis inputs)
         ~known_dimensions:styled_based_known_dimensions ~parent_size
         ~available_space
         ~vertical_margins_are_collapsible:
           (Layout_input.vertical_margins_are_collapsible inputs))