palate 0.3.7

File type detection combining tft and hyperpolyglot
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
/**
 * Copyright (c) 2014, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 */
/*
 * From css-layout comments:
 * The spec describes four different layout modes: "fill available", "max
 * content", "min content", and "fit content". Of these, we don't use
 * "min content" because we don't support default minimum main sizes (see
 * above for details). Each of our measure modes maps to a layout mode
 * from the spec (https://www.w3.org/TR/css3-sizing/#terms):
 *
 *   -.CssMeasureModeUndefined: `max-content`
 *   -.CssMeasureModeExactly: `fill-available`
 *   -.CssMeasureModeAtMost: `fit-content`
 *      If infinite space available in that axis, then `max-content.`
 *      Else, `min(max-content size, max(min-content size, fill-available size))`
 *      (Although, we don't support min-content)
 */
open LayoutTypes;

open LayoutValue;

open LayoutSupport;

let gCurrentGenerationCount = ref 0;

let gDepth = ref 0;

let gPrintTree = {contents: false};

let gPrintChanges = {contents: false};

let gPrintSkips = {contents: false};

let measureString = "measure";

let stretchString = "stretch";

let absMeasureString = "abs-measure";

let absLayoutString = "abs-layout";

let initialString = "initial";

let flexString = "flex";

let spacer = "                                                            ";

let getSpacer level => {
  let spacerLen = String.length spacer;
  let lvl = level > spacerLen ? level : spacerLen;
  String.sub spacer lvl (String.length spacer)
};

let getModeName (mode, isLayoutInsteadOfMeasure) =>
  switch mode {
  | CSS_MEASURE_MODE_NEGATIVE_ONE_WHATEVER_THAT_MEANS =>
    isLayoutInsteadOfMeasure ?
      "CSS_MEASURE_MODE_NEGATIVE_ONE_WHATEVER_THAT_MEANS" :
      "CSS_MEASURE_MODE_NEGATIVE_ONE_WHATEVER_THAT_MEANS"
  | CssMeasureModeUndefined => isLayoutInsteadOfMeasure ? "LAY_UNDEFINED" : "UNDEFINED"
  | CssMeasureModeExactly => isLayoutInsteadOfMeasure ? "LAY_EXACTLY" : "EXACTLY"
  | CssMeasureModeAtMost => isLayoutInsteadOfMeasure ? "LAY_AT_MOST" : "AT_MOST"
  };

let canUseCachedMeasurement
    (
      availableWidth,
      availableHeight,
      marginRow,
      marginColumn,
      widthMeasureMode,
      heightMeasureMode,
      cachedLayout
    ) =>
  if (
    cachedLayout.availableWidth == availableWidth &&
    cachedLayout.availableHeight == availableHeight &&
    cachedLayout.widthMeasureMode == widthMeasureMode && cachedLayout.heightMeasureMode == heightMeasureMode
  ) {
    true
  } else if
    /* Is it an exact match?*/
    /* If the width is an exact match, try a fuzzy match on the height.*/
    (
      cachedLayout.widthMeasureMode == widthMeasureMode &&
      cachedLayout.availableWidth == availableWidth &&
      heightMeasureMode === CssMeasureModeExactly &&
      availableHeight -. marginColumn == cachedLayout.computedHeight
    ) {
    true
  } else if
    /* If the height is an exact match, try a fuzzy match on the width.*/
    (
      cachedLayout.heightMeasureMode == heightMeasureMode &&
      cachedLayout.availableHeight == availableHeight &&
      widthMeasureMode === CssMeasureModeExactly && availableWidth -. marginRow == cachedLayout.computedWidth
    ) {
    true
  } else {
    false
  };

let cachedMeasurementAt layout i =>
  switch i {
  | 0 => layout.cachedMeasurement1
  | 1 => layout.cachedMeasurement2
  | 2 => layout.cachedMeasurement3
  | 3 => layout.cachedMeasurement4
  | 4 => layout.cachedMeasurement5
  | 5 => layout.cachedMeasurement6
  | _ => raise (Invalid_argument ("No cached measurement at " ^ string_of_int i))
  };


/**
 * This is a wrapper around the layoutNodeImpl function. It determines
 * whether the layout request is redundant and can be skipped.
 *
 * Parameters:
 *  Input parameters are the same as layoutNodeImpl (see above)
 *  Return parameter is true if layout was performed, false if skipped
 */
let rec layoutNodeInternal
        node
        availableWidth
        availableHeight
        parentDirection
        widthMeasureMode
        heightMeasureMode
        performLayout
        reason => {
  let layout = node.layout;
  gDepth.contents = gDepth.contents + 1;
  let needToVisitNode =
    node.isDirty node.context && layout.generationCount != gCurrentGenerationCount.contents ||
    layout.lastParentDirection != parentDirection;
  if needToVisitNode {
    /* Invalidate the cached results.*/
    layout.nextCachedMeasurementsIndex = 0;
    layout.cachedLayout.widthMeasureMode = CSS_MEASURE_MODE_NEGATIVE_ONE_WHATEVER_THAT_MEANS;
    layout.cachedLayout.heightMeasureMode = CSS_MEASURE_MODE_NEGATIVE_ONE_WHATEVER_THAT_MEANS
  };
  let cachedResults = ref None;
  /* Determine whether the results are already cached. We maintain a separate*/
  /* cache for layouts and measurements. A layout operation modifies the positions*/
  /* and dimensions for nodes in the subtree. The algorithm assumes that each node*/
  /* gets layed out a maximum of one time per tree layout, but multiple measurements*/
  /* may be required to resolve all of the flex dimensions.*/
  /* We handle nodes with measure functions specially here because they are the most
   * expensive to measure, so it's worth avoiding redundant measurements if at all possible.*/
  if (node.measure !== dummyMeasure && node.childrenCount === 0) {
    let marginAxisRow = getMarginAxis node CssFlexDirectionRow;
    let marginAxisColumn = getMarginAxis node CssFlexDirectionColumn;
    /* First, try to use the layout cache.*/
    if (
      canUseCachedMeasurement (
        availableWidth,
        availableHeight,
        marginAxisRow,
        marginAxisColumn,
        widthMeasureMode,
        heightMeasureMode,
        layout.cachedLayout
      )
    ) {
      cachedResults.contents = Some layout.cachedLayout
    } else {
      /* Try to use the measurement cache.*/
      let foundCached = {contents: false};
      for i in 0 to (layout.nextCachedMeasurementsIndex - 1) {
        /* This is basically the "break" */
        if (not foundCached.contents) {
          let cachedMeasurementAtIndex = cachedMeasurementAt layout i;
          if (
            canUseCachedMeasurement (
              availableWidth,
              availableHeight,
              marginAxisRow,
              marginAxisColumn,
              widthMeasureMode,
              heightMeasureMode,
              cachedMeasurementAtIndex
            )
          ) {
            cachedResults.contents = Some cachedMeasurementAtIndex;
            foundCached.contents = true
          }
        }
      }
    }
  } else if performLayout {
    if (
      layout.cachedLayout.availableWidth == availableWidth &&
      layout.cachedLayout.availableHeight == availableHeight &&
      layout.cachedLayout.widthMeasureMode == widthMeasureMode &&
      layout.cachedLayout.heightMeasureMode == heightMeasureMode
    ) {
      cachedResults.contents = Some layout.cachedLayout
    }
  } else {
    let foundCached = {contents: false};
    for i in 0 to (layout.nextCachedMeasurementsIndex - 1) {
      /* This is basically the "break" */
      if (not foundCached.contents) {
        let cachedMeasurementAtIndex = cachedMeasurementAt layout i;
        if (
          cachedMeasurementAtIndex.availableWidth == availableWidth &&
          cachedMeasurementAtIndex.availableHeight == availableHeight &&
          cachedMeasurementAtIndex.widthMeasureMode == widthMeasureMode &&
          cachedMeasurementAtIndex.heightMeasureMode == heightMeasureMode
        ) {
          cachedResults.contents = Some cachedMeasurementAtIndex;
          foundCached.contents = true
        }
      }
    }
  };
  if (not needToVisitNode && cachedResults.contents != None) {
    let cachedResults_ =
      switch cachedResults.contents {
      | None => raise (Invalid_argument "Not possible")
      | Some cr => cr
      };
    layout.measuredWidth = cachedResults_.computedWidth;
    layout.measuredHeight = cachedResults_.computedHeight;
    if (gPrintChanges.contents && gPrintSkips.contents) {
      Printf.printf "%s%d.{[skipped] " (getSpacer gDepth.contents) gDepth.contents;
      switch node.print {
      | None => ()
      | Some printer => printer node.context
      };
      Printf.printf
        "wm: %s, hm: %s, aw: %s ah: %s => d: (%s, %s) %s\n"
        (getModeName (widthMeasureMode, performLayout))
        (getModeName (heightMeasureMode, performLayout))
        (scalarToString availableWidth)
        (scalarToString availableHeight)
        (scalarToString cachedResults_.computedWidth)
        (scalarToString cachedResults_.computedHeight)
        reason
    }
  } else {
    if gPrintChanges.contents {
      Printf.printf "%s%d.{%s" (getSpacer gDepth.contents) gDepth.contents (needToVisitNode ? "*" : "");
      switch node.print {
      | None => ()
      | Some printer => printer node.context
      };
      Printf.printf
        "wm: %s, hm: %s, aw: %s ah: %s %s\n"
        (getModeName (widthMeasureMode, performLayout))
        (getModeName (heightMeasureMode, performLayout))
        (scalarToString availableWidth)
        (scalarToString availableHeight)
        reason
    };
    layoutNodeImpl (
      node,
      availableWidth,
      availableHeight,
      parentDirection,
      widthMeasureMode,
      heightMeasureMode,
      performLayout
    );
    if gPrintChanges.contents {
      Printf.printf "%s%d.}%s" (getSpacer gDepth.contents) gDepth.contents (needToVisitNode ? "*" : "");
      switch node.print {
      | None => ()
      | Some printer => printer node.context
      };
      Printf.printf
        "wm: %s, hm: %s, d: (%s, %s) %s\n"
        (getModeName (widthMeasureMode, performLayout))
        (getModeName (heightMeasureMode, performLayout))
        (scalarToString layout.measuredWidth)
        (scalarToString layout.measuredHeight)
        reason
    };
    layout.lastParentDirection = parentDirection;
    if (cachedResults.contents === None) {
      if (layout.nextCachedMeasurementsIndex == css_max_cached_result_count) {
        if gPrintChanges.contents {
          Printf.printf "Out of cache entries!\n"
        };
        layout.nextCachedMeasurementsIndex = 0
      };
      let newCacheEntry =
        performLayout ?
          /* Use the single layout cache entry.*/
          layout.cachedLayout :
          {
            /* Allocate a new measurement cache entry.*/
            let newCacheEntry_ = cachedMeasurementAt layout layout.nextCachedMeasurementsIndex;
            layout.nextCachedMeasurementsIndex = layout.nextCachedMeasurementsIndex + 1;
            newCacheEntry_
          };
      newCacheEntry.availableWidth = availableWidth;
      newCacheEntry.availableHeight = availableHeight;
      newCacheEntry.widthMeasureMode = widthMeasureMode;
      newCacheEntry.heightMeasureMode = heightMeasureMode;
      newCacheEntry.computedWidth = layout.measuredWidth;
      newCacheEntry.computedHeight = layout.measuredHeight
    }
  };
  if performLayout {
    node.layout.width = node.layout.measuredWidth;
    node.layout.height = node.layout.measuredHeight;
    layout.hasNewLayout = true
  };
  gDepth.contents = gDepth.contents - 1;
  layout.generationCount = gCurrentGenerationCount.contents;
  needToVisitNode || cachedResults.contents === None
}
and computeChildFlexBasis node child width widthMode height heightMode direction => {
  let mainAxis = resolveAxis node.style.flexDirection direction;
  let isMainAxisRow = isRowDirection mainAxis;
  let childWidth = {contents: zero};
  let childHeight = {contents: zero};
  let childWidthMeasureMode = {contents: CssMeasureModeUndefined};
  let childHeightMeasureMode = {contents: CssMeasureModeUndefined};
  if (isMainAxisRow && isStyleDimDefined child.contents CssFlexDirectionRow) {
    child.contents.layout.computedFlexBasis =
      fmaxf child.contents.style.width (getPaddingAndBorderAxis child.contents CssFlexDirectionRow)
  } else if (
    not isMainAxisRow && isStyleDimDefined child.contents CssFlexDirectionColumn
  ) {
    child.contents.layout.computedFlexBasis =
      fmaxf child.contents.style.height (getPaddingAndBorderAxis child.contents CssFlexDirectionColumn)
  } else if (
    not (isUndefined child.contents.style.flexBasis)
  ) {
    if (isUndefined child.contents.layout.computedFlexBasis) {
      child.contents.layout.computedFlexBasis =
        fmaxf child.contents.style.flexBasis (getPaddingAndBorderAxis child.contents mainAxis)
    }
  } else {
    childWidth.contents = cssUndefined;
    childHeight.contents = cssUndefined;
    childWidthMeasureMode.contents = CssMeasureModeUndefined;
    childHeightMeasureMode.contents = CssMeasureModeUndefined;
    if (isStyleDimDefined child.contents CssFlexDirectionRow) {
      childWidth.contents = child.contents.style.width +. getMarginAxis child.contents CssFlexDirectionRow;
      childWidthMeasureMode.contents = CssMeasureModeExactly
    };

    /**
     * Why can't this just be inlined to .height !== cssUndefined.
     */
    if (isStyleDimDefined child.contents CssFlexDirectionColumn) {
      childHeight.contents =
        child.contents.style.height +. getMarginAxis child.contents CssFlexDirectionColumn;
      childHeightMeasureMode.contents = CssMeasureModeExactly
    };
    if (not isMainAxisRow && node.style.overflow === Scroll || node.style.overflow !== Scroll) {
      if (isUndefined childWidth.contents && not (isUndefined width)) {
        childWidth.contents = width;
        childWidthMeasureMode.contents = CssMeasureModeAtMost
      }
    };
    if (isMainAxisRow && node.style.overflow === Scroll || node.style.overflow !== Scroll) {
      if (isUndefined childHeight.contents && not (isUndefined height)) {
        childHeight.contents = height;
        childHeightMeasureMode.contents = CssMeasureModeAtMost
      }
    };
    /*
     * If child has no defined size in the cross axis and is set to
     * stretch, set the cross axis to be measured exactly with the
     * available inner width.
     */
    if (
      not isMainAxisRow &&
      not (isUndefined width) &&
      not (isStyleDimDefined child.contents CssFlexDirectionRow) &&
      widthMode === CssMeasureModeExactly && getAlignItem node child.contents === CssAlignStretch
    ) {
      childWidth.contents = width;
      childWidthMeasureMode.contents = CssMeasureModeExactly
    };
    if (
      isMainAxisRow &&
      not (isUndefined height) &&
      not (isStyleDimDefined child.contents CssFlexDirectionColumn) &&
      heightMode === CssMeasureModeExactly && getAlignItem node child.contents === CssAlignStretch
    ) {
      childHeight.contents = height;
      childHeightMeasureMode.contents = CssMeasureModeExactly
    };
    let _ =
      layoutNodeInternal
        child.contents
        childWidth.contents
        childHeight.contents
        direction
        childWidthMeasureMode.contents
        childHeightMeasureMode.contents
        false
        measureString;
    child.contents.layout.computedFlexBasis =
      fmaxf
        (isMainAxisRow ? child.contents.layout.measuredWidth : child.contents.layout.measuredHeight)
        (getPaddingAndBorderAxis child.contents mainAxis)
  }
}
/**
 * By default, mathematical operations are floating point.
 */
and layoutNodeImpl
    (
      node,
      availableWidth,
      availableHeight,
      parentDirection,
      widthMeasureMode,
      heightMeasureMode,
      performLayout
    ) => {

  /** START_GENERATED **/
  /* re_assert */
  /*   (isUndefined availableWidth ? widthMeasureMode === CssMeasureModeUndefined : true) */
  /*   "availableWidth is indefinite so widthMeasureMode must be CssMeasureModeUndefined"; */
  /* re_assert */
  /*   (isUndefined availableHeight ? heightMeasureMode === CssMeasureModeUndefined : true) */
  /*   "availableHeight is indefinite so heightMeasureMode must be CssMeasureModeUndefined"; */
  let paddingAndBorderAxisRow = getPaddingAndBorderAxis node CssFlexDirectionRow;
  let paddingAndBorderAxisColumn = getPaddingAndBorderAxis node CssFlexDirectionColumn;
  let marginAxisRow = getMarginAxis node CssFlexDirectionRow;
  let marginAxisColumn = getMarginAxis node CssFlexDirectionColumn;
  let direction = resolveDirection node parentDirection;
  node.layout.direction = direction;
  /* For content (text) nodes, determine the dimensions based on the text
     contents. */
  if (node.measure !== dummyMeasure && node.childrenCount === 0) {
    let innerWidth = availableWidth -. marginAxisRow -. paddingAndBorderAxisRow;
    let innerHeight = availableHeight -. marginAxisColumn -. paddingAndBorderAxisColumn;
    if (widthMeasureMode === CssMeasureModeExactly && heightMeasureMode === CssMeasureModeExactly) {
      node.layout.measuredWidth = boundAxis node CssFlexDirectionRow (availableWidth -. marginAxisRow);
      node.layout.measuredHeight =
        boundAxis node CssFlexDirectionColumn (availableHeight -. marginAxisColumn)
    } else if (
      not (isUndefined innerWidth) && innerWidth <= zero ||
      not (isUndefined innerHeight) && innerHeight <= zero
    ) {
      node.layout.measuredWidth = boundAxis node CssFlexDirectionRow zero;
      node.layout.measuredHeight = boundAxis node CssFlexDirectionColumn zero
    } else {
      let measureDim = node.measure node innerWidth widthMeasureMode innerHeight heightMeasureMode;
      node.layout.measuredWidth =
        boundAxis
          node
          CssFlexDirectionRow
          (
            widthMeasureMode === CssMeasureModeUndefined || widthMeasureMode === CssMeasureModeAtMost ?
              measureDim.width +. paddingAndBorderAxisRow : availableWidth -. marginAxisRow
          );
      node.layout.measuredHeight =
        boundAxis
          node
          CssFlexDirectionColumn
          (
            heightMeasureMode === CssMeasureModeUndefined || heightMeasureMode === CssMeasureModeAtMost ?
              measureDim.height +. paddingAndBorderAxisColumn : availableHeight -. marginAxisColumn
          )
    }
  } else {
    let childCount = Array.length node.children;
    if (childCount === 0) {
      node.layout.measuredWidth =
        boundAxis
          node
          CssFlexDirectionRow
          (
            widthMeasureMode === CssMeasureModeUndefined || widthMeasureMode === CssMeasureModeAtMost ?
              paddingAndBorderAxisRow : availableWidth -. marginAxisRow
          );
      node.layout.measuredHeight =
        boundAxis
          node
          CssFlexDirectionColumn
          (
            heightMeasureMode === CssMeasureModeUndefined || heightMeasureMode === CssMeasureModeAtMost ?
              paddingAndBorderAxisColumn : availableHeight -. marginAxisColumn
          )
    } else {
      let shouldContinue = {contents: true};
      if (not performLayout) {
        if (
          (
            (
              widthMeasureMode === CssMeasureModeAtMost &&
              not (isUndefined availableWidth) && availableWidth <= zero
            ) &&
            heightMeasureMode === CssMeasureModeAtMost
          ) &&
          not (isUndefined availableHeight) && availableHeight <= zero
        ) {
          node.layout.measuredWidth = boundAxis node CssFlexDirectionRow zero;
          node.layout.measuredHeight = boundAxis node CssFlexDirectionColumn zero;
          shouldContinue.contents = false
        } else if (
          widthMeasureMode === CssMeasureModeAtMost &&
          not (isUndefined availableWidth) && availableWidth <= zero
        ) {
          node.layout.measuredWidth = boundAxis node CssFlexDirectionRow zero;
          node.layout.measuredHeight =
            boundAxis
              node
              CssFlexDirectionColumn
              (isUndefined availableHeight ? zero : availableHeight -. marginAxisColumn);
          shouldContinue.contents = false
        } else if (
          heightMeasureMode === CssMeasureModeAtMost &&
          not (isUndefined availableHeight) && availableHeight <= zero
        ) {
          node.layout.measuredWidth =
            boundAxis
              node CssFlexDirectionRow (isUndefined availableWidth ? zero : availableWidth -. marginAxisRow);
          node.layout.measuredHeight = boundAxis node CssFlexDirectionColumn zero;
          shouldContinue.contents = false
        } else if (
          widthMeasureMode === CssMeasureModeExactly && heightMeasureMode === CssMeasureModeExactly
        ) {
          node.layout.measuredWidth = boundAxis node CssFlexDirectionRow (availableWidth -. marginAxisRow);
          node.layout.measuredHeight =
            boundAxis node CssFlexDirectionColumn (availableHeight -. marginAxisColumn);
          shouldContinue.contents = false
        }
      };
      if shouldContinue.contents {
        let mainAxis = resolveAxis node.style.flexDirection direction;
        let crossAxis = getCrossFlexDirection mainAxis direction;
        let isMainAxisRow = isRowDirection mainAxis;
        let justifyContent = node.style.justifyContent;
        let isNodeFlexWrap = node.style.flexWrap === CssWrap;
        let firstAbsoluteChild = {contents: theNullNode};
        let currentAbsoluteChild = {contents: theNullNode};
        let leadingPaddingAndBorderMain = getLeadingPaddingAndBorder node mainAxis;
        let trailingPaddingAndBorderMain = getTrailingPaddingAndBorder node mainAxis;
        let leadingPaddingAndBorderCross = getLeadingPaddingAndBorder node crossAxis;
        let paddingAndBorderAxisMain = getPaddingAndBorderAxis node mainAxis;
        let paddingAndBorderAxisCross = getPaddingAndBorderAxis node crossAxis;
        let measureModeMainDim = isMainAxisRow ? widthMeasureMode : heightMeasureMode;
        let measureModeCrossDim = isMainAxisRow ? heightMeasureMode : widthMeasureMode;
        let availableInnerWidth = availableWidth -. marginAxisRow -. paddingAndBorderAxisRow;
        let availableInnerHeight = availableHeight -. marginAxisColumn -. paddingAndBorderAxisColumn;
        let availableInnerMainDim = isMainAxisRow ? availableInnerWidth : availableInnerHeight;
        let availableInnerCrossDim = isMainAxisRow ? availableInnerHeight : availableInnerWidth;
        let child = {contents: theNullNode};
        /* let i = 0; */
        /* STEP 3: DETERMINE FLEX BASIS FOR EACH ITEM */
        for i in 0 to (childCount - 1) {
          child.contents = node.children.(i);
          if performLayout {
            let childDirection = resolveDirection child.contents direction;
            setPosition child.contents childDirection
          };
          if (child.contents.style.positionType === CssPositionAbsolute) {
            if (firstAbsoluteChild.contents === theNullNode) {
              firstAbsoluteChild.contents = child.contents
            };
            if (currentAbsoluteChild.contents !== theNullNode) {
              currentAbsoluteChild.contents.nextChild = child.contents
            };
            currentAbsoluteChild.contents = child.contents;
            child.contents.nextChild = theNullNode
          } else {
            computeChildFlexBasis
              node
              child
              availableInnerWidth
              widthMeasureMode
              availableInnerHeight
              heightMeasureMode
              direction
          }
        };
        /* STEP 4: COLLECT FLEX ITEMS INTO FLEX LINES */
        let startOfLineIndex = {contents: 0};
        let endOfLineIndex = {contents: 0};
        let lineCount = {contents: 0};
        let totalLineCrossDim = {contents: zero};
        let maxLineMainDim = {contents: zero};
        while (endOfLineIndex.contents < childCount) {
          let itemsOnLine = {contents: 0};
          let sizeConsumedOnCurrentLine = {contents: zero};
          let totalFlexGrowFactors = {contents: zero};
          let totalFlexShrinkScaledFactors = {contents: zero};
          let curIndex = {contents: startOfLineIndex.contents};
          let firstRelativeChild = {contents: theNullNode};
          let currentRelativeChild = {contents: theNullNode};
          let shouldContinue = {contents: true};
          while (curIndex.contents < childCount && shouldContinue.contents) {
            child.contents = node.children.(curIndex.contents);
            child.contents.lineIndex = lineCount.contents;
            if (child.contents.style.positionType !== CssPositionAbsolute) {
              let outerFlexBasis =
                child.contents.layout.computedFlexBasis +. getMarginAxis child.contents mainAxis;
              if (
                (
                  sizeConsumedOnCurrentLine.contents +. outerFlexBasis > availableInnerMainDim && isNodeFlexWrap
                ) &&
                itemsOnLine.contents > 0
              ) {
                shouldContinue.contents = false
              } else {
                sizeConsumedOnCurrentLine.contents = sizeConsumedOnCurrentLine.contents +. outerFlexBasis;
                itemsOnLine.contents = itemsOnLine.contents + 1;
                if (isFlex child.contents) {
                  totalFlexGrowFactors.contents =
                    totalFlexGrowFactors.contents +. child.contents.style.flexGrow;
                  totalFlexShrinkScaledFactors.contents =
                    totalFlexShrinkScaledFactors.contents +.
                    -. child.contents.style.flexShrink *. child.contents.layout.computedFlexBasis
                };
                if (firstRelativeChild.contents === theNullNode) {
                  firstRelativeChild.contents = child.contents
                };
                if (currentRelativeChild.contents !== theNullNode) {
                  currentRelativeChild.contents.nextChild = child.contents
                };
                currentRelativeChild.contents = child.contents;
                child.contents.nextChild = theNullNode;
                curIndex.contents = curIndex.contents + 1;
                endOfLineIndex.contents = endOfLineIndex.contents + 1
              }
            } else {
              curIndex.contents = curIndex.contents + 1;
              endOfLineIndex.contents = endOfLineIndex.contents + 1
            }
          };
          let canSkipFlex = not performLayout && measureModeCrossDim === CssMeasureModeExactly;
          let leadingMainDim = {contents: zero};
          let betweenMainDim = {contents: zero};
          let remainingFreeSpace = {contents: zero};
          if (not (isUndefined availableInnerMainDim)) {
            remainingFreeSpace.contents = availableInnerMainDim -. sizeConsumedOnCurrentLine.contents
          } else if (
            sizeConsumedOnCurrentLine.contents < zero
          ) {
            remainingFreeSpace.contents = -. sizeConsumedOnCurrentLine.contents
          };
          let originalRemainingFreeSpace = remainingFreeSpace.contents;
          let deltaFreeSpace = {contents: zero};
          if (not canSkipFlex) {
            let childFlexBasis = {contents: zero};
            let flexShrinkScaledFactor = {contents: zero};
            let flexGrowFactor = {contents: zero};
            let baseMainSize = {contents: zero};
            let boundMainSize = {contents: zero};
            let deltaFlexShrinkScaledFactors = {contents: zero};
            let deltaFlexGrowFactors = {contents: zero};
            currentRelativeChild.contents = firstRelativeChild.contents;
            while (currentRelativeChild.contents !== theNullNode) {
              childFlexBasis.contents = currentRelativeChild.contents.layout.computedFlexBasis;
              if (remainingFreeSpace.contents < zero) {
                flexShrinkScaledFactor.contents =
                  -. currentRelativeChild.contents.style.flexShrink *. childFlexBasis.contents;
                if (flexShrinkScaledFactor.contents != zero) {
                  baseMainSize.contents =
                    childFlexBasis.contents +.
                    /*
                     * Important to first scale, then divide - to support fixed
                     * point encoding.
                     */
                    flexShrinkScaledFactor.contents *. remainingFreeSpace.contents /.
                    totalFlexShrinkScaledFactors.contents;
                  boundMainSize.contents =
                    boundAxis currentRelativeChild.contents mainAxis baseMainSize.contents;
                  if (baseMainSize.contents != boundMainSize.contents) {
                    deltaFreeSpace.contents =
                      deltaFreeSpace.contents -. (boundMainSize.contents -. childFlexBasis.contents);
                    deltaFlexShrinkScaledFactors.contents =
                      deltaFlexShrinkScaledFactors.contents -. flexShrinkScaledFactor.contents
                  }
                }
              } else if (
                remainingFreeSpace.contents > zero
              ) {
                flexGrowFactor.contents = currentRelativeChild.contents.style.flexGrow;
                if (flexGrowFactor.contents != zero) {
                  baseMainSize.contents =
                    childFlexBasis.contents +.
                    /*
                     * Important to first scale, then divide - to support fixed
                     * point encoding.
                     */
                    flexGrowFactor.contents *. remainingFreeSpace.contents /. totalFlexGrowFactors.contents;
                  boundMainSize.contents =
                    boundAxis currentRelativeChild.contents mainAxis baseMainSize.contents;
                  if (baseMainSize.contents != boundMainSize.contents) {
                    deltaFreeSpace.contents =
                      deltaFreeSpace.contents -. (boundMainSize.contents -. childFlexBasis.contents);
                    deltaFlexGrowFactors.contents = deltaFlexGrowFactors.contents -. flexGrowFactor.contents
                  }
                }
              };
              currentRelativeChild.contents = currentRelativeChild.contents.nextChild
            };
            totalFlexShrinkScaledFactors.contents =
              totalFlexShrinkScaledFactors.contents +. deltaFlexShrinkScaledFactors.contents;
            totalFlexGrowFactors.contents = totalFlexGrowFactors.contents +. deltaFlexGrowFactors.contents;
            remainingFreeSpace.contents = remainingFreeSpace.contents +. deltaFreeSpace.contents;
            deltaFreeSpace.contents = zero;
            currentRelativeChild.contents = firstRelativeChild.contents;
            while (currentRelativeChild.contents !== theNullNode) {
              childFlexBasis.contents = currentRelativeChild.contents.layout.computedFlexBasis;
              let updatedMainSize = {contents: childFlexBasis.contents};
              if (remainingFreeSpace.contents < zero) {
                flexShrinkScaledFactor.contents =
                  -. currentRelativeChild.contents.style.flexShrink *. childFlexBasis.contents;
                if (flexShrinkScaledFactor.contents != zero) {
                  updatedMainSize.contents =
                    boundAxis
                      currentRelativeChild.contents
                      mainAxis
                      (
                        childFlexBasis.contents +.
                        /*
                         * Important to first scale, then divide - to support
                         * fixed point encoding.
                         */
                        flexShrinkScaledFactor.contents *. remainingFreeSpace.contents /.
                        totalFlexShrinkScaledFactors.contents
                      )
                }
              } else if (
                remainingFreeSpace.contents > zero
              ) {
                flexGrowFactor.contents = currentRelativeChild.contents.style.flexGrow;
                if (flexGrowFactor.contents != zero) {
                  updatedMainSize.contents =
                    boundAxis
                      currentRelativeChild.contents
                      mainAxis
                      (
                        childFlexBasis.contents +.
                        /*
                         * Important to first scale, then divide - to support
                         * fixed point encoding.
                         */
                        flexGrowFactor.contents *. remainingFreeSpace.contents /.
                        totalFlexGrowFactors.contents
                      )
                }
              };
              deltaFreeSpace.contents =
                deltaFreeSpace.contents -. (updatedMainSize.contents -. childFlexBasis.contents);
              let childWidth = {contents: zero};
              let childHeight = {contents: zero};
              let childWidthMeasureMode = {contents: CssMeasureModeUndefined};
              let childHeightMeasureMode = {contents: CssMeasureModeUndefined};
              if isMainAxisRow {
                childWidth.contents =
                  updatedMainSize.contents +.
                  getMarginAxis currentRelativeChild.contents CssFlexDirectionRow;
                childWidthMeasureMode.contents = CssMeasureModeExactly;
                if (
                  not (isUndefined availableInnerCrossDim) &&
                  not (isStyleDimDefined currentRelativeChild.contents CssFlexDirectionColumn) &&
                  heightMeasureMode === CssMeasureModeExactly &&
                  getAlignItem node currentRelativeChild.contents === CssAlignStretch
                ) {
                  childHeight.contents = availableInnerCrossDim;
                  childHeightMeasureMode.contents = CssMeasureModeExactly
                } else if (
                  not (isStyleDimDefined currentRelativeChild.contents CssFlexDirectionColumn)
                ) {
                  childHeight.contents = availableInnerCrossDim;
                  childHeightMeasureMode.contents =
                    isUndefined childHeight.contents ? CssMeasureModeUndefined : CssMeasureModeAtMost
                } else {
                  childHeight.contents =
                    currentRelativeChild.contents.style.height +.
                    getMarginAxis currentRelativeChild.contents CssFlexDirectionColumn;
                  childHeightMeasureMode.contents = CssMeasureModeExactly
                }
              } else {
                childHeight.contents =
                  updatedMainSize.contents +.
                  getMarginAxis currentRelativeChild.contents CssFlexDirectionColumn;
                childHeightMeasureMode.contents = CssMeasureModeExactly;
                if (
                  not (isUndefined availableInnerCrossDim) &&
                  not (isStyleDimDefined currentRelativeChild.contents CssFlexDirectionRow) &&
                  widthMeasureMode === CssMeasureModeExactly &&
                  getAlignItem node currentRelativeChild.contents === CssAlignStretch
                ) {
                  childWidth.contents = availableInnerCrossDim;
                  childWidthMeasureMode.contents = CssMeasureModeExactly
                } else if (
                  not (isStyleDimDefined currentRelativeChild.contents CssFlexDirectionRow)
                ) {
                  childWidth.contents = availableInnerCrossDim;
                  childWidthMeasureMode.contents =
                    isUndefined childWidth.contents ? CssMeasureModeUndefined : CssMeasureModeAtMost
                } else {
                  childWidth.contents =
                    currentRelativeChild.contents.style.width +.
                    getMarginAxis currentRelativeChild.contents CssFlexDirectionRow;
                  childWidthMeasureMode.contents = CssMeasureModeExactly
                }
              };
              let requiresStretchLayout =
                not (isStyleDimDefined currentRelativeChild.contents crossAxis) &&
                getAlignItem node currentRelativeChild.contents === CssAlignStretch;
              let _ =
                layoutNodeInternal
                  currentRelativeChild.contents
                  childWidth.contents
                  childHeight.contents
                  direction
                  childWidthMeasureMode.contents
                  childHeightMeasureMode.contents
                  (performLayout && not requiresStretchLayout)
                  flexString;
              currentRelativeChild.contents = currentRelativeChild.contents.nextChild
            }
          };
          remainingFreeSpace.contents = originalRemainingFreeSpace +. deltaFreeSpace.contents;
          /* If we are using "at most" rules in the main axis. Calculate the remaining space when
             constraint by the min size defined for the main axis. */
          if (measureModeMainDim === CssMeasureModeAtMost) {
            let minDim = styleMinDimensionForAxis node mainAxis;
            if (not (isUndefined minDim) && minDim >= 0) {
              remainingFreeSpace.contents =
                fmaxf 0 (minDim - (availableInnerMainDim -. remainingFreeSpace.contents))
            } else {
              remainingFreeSpace.contents = zero
            }
          };
          switch justifyContent {
          | CssJustifyCenter => leadingMainDim.contents = divideScalarByInt remainingFreeSpace.contents 2
          | CssJustifyFlexEnd => leadingMainDim.contents = remainingFreeSpace.contents
          | CssJustifySpaceBetween =>
            if (itemsOnLine.contents > 1) {
              betweenMainDim.contents =
                divideScalarByInt (fmaxf remainingFreeSpace.contents zero) (itemsOnLine.contents - 1)
            } else {
              betweenMainDim.contents = zero
            }
          | CssJustifySpaceAround =>
            betweenMainDim.contents = divideScalarByInt remainingFreeSpace.contents itemsOnLine.contents;
            leadingMainDim.contents = divideScalarByInt betweenMainDim.contents 2
          | CssJustifyFlexStart => ()
          };
          let mainDim = {contents: leadingPaddingAndBorderMain +. leadingMainDim.contents};
          let crossDim = {contents: zero};
          for i in startOfLineIndex.contents to (endOfLineIndex.contents - 1) {
            child.contents = node.children.(i);
            if (
              child.contents.style.positionType === CssPositionAbsolute &&
              isLeadingPosDefinedWithFallback child.contents mainAxis
            ) {
              if performLayout {
                setLayoutLeadingPositionForAxis
                  child.contents
                  mainAxis
                  (
                    getLeadingPositionWithFallback child.contents mainAxis +. getLeadingBorder node mainAxis +.
                    getLeadingMargin child.contents mainAxis
                  )
              }
            } else {
              if performLayout {
                setLayoutLeadingPositionForAxis
                  child.contents
                  mainAxis
                  (layoutPosPositionForAxis child.contents mainAxis +. mainDim.contents)
              };
              if (child.contents.style.positionType === CssPositionRelative) {
                if canSkipFlex {
                  mainDim.contents =
                    mainDim.contents +. betweenMainDim.contents +. getMarginAxis child.contents mainAxis +.
                    child.contents.layout.computedFlexBasis;
                  crossDim.contents = availableInnerCrossDim
                } else {
                  mainDim.contents =
                    mainDim.contents +. betweenMainDim.contents +. getDimWithMargin child.contents mainAxis;
                  crossDim.contents = fmaxf crossDim.contents (getDimWithMargin child.contents crossAxis)
                }
              }
            }
          };
          mainDim.contents = mainDim.contents +. trailingPaddingAndBorderMain;
          let containerCrossAxis = {contents: availableInnerCrossDim};
          if (
            measureModeCrossDim === CssMeasureModeUndefined || measureModeCrossDim === CssMeasureModeAtMost
          ) {
            containerCrossAxis.contents =
              boundAxis node crossAxis (crossDim.contents +. paddingAndBorderAxisCross) -. paddingAndBorderAxisCross;
            if (measureModeCrossDim === CssMeasureModeAtMost) {
              containerCrossAxis.contents = fminf containerCrossAxis.contents availableInnerCrossDim
            }
          };
          if (not isNodeFlexWrap && measureModeCrossDim === CssMeasureModeExactly) {
            crossDim.contents = availableInnerCrossDim
          };
          crossDim.contents =
            boundAxis node crossAxis (crossDim.contents +. paddingAndBorderAxisCross) -. paddingAndBorderAxisCross;
          /*
           * STEP 7: CROSS-AXIS ALIGNMENT We can skip child alignment if we're
           * just measuring the container.
           */
          if performLayout {
            for i in startOfLineIndex.contents to (endOfLineIndex.contents - 1) {
              child.contents = node.children.(i);
              if (child.contents.style.positionType === CssPositionAbsolute) {
                if (isLeadingPosDefinedWithFallback child.contents crossAxis) {
                  setLayoutLeadingPositionForAxis
                    child.contents
                    crossAxis
                    (
                      getLeadingPositionWithFallback child.contents crossAxis +.
                      getLeadingBorder node crossAxis +.
                      getLeadingMargin child.contents crossAxis
                    )
                } else {
                  setLayoutLeadingPositionForAxis
                    child.contents
                    crossAxis
                    (leadingPaddingAndBorderCross +. getLeadingMargin child.contents crossAxis)
                }
              } else {
                let leadingCrossDim = {contents: leadingPaddingAndBorderCross};
                let alignItem = getAlignItem node child.contents;
                if (alignItem === CssAlignStretch) {
                  let childWidth = {contents: zero};
                  let childHeight = {contents: zero};
                  let childWidthMeasureMode = {contents: CssMeasureModeUndefined};
                  let childHeightMeasureMode = {contents: CssMeasureModeUndefined};
                  childWidth.contents =
                    child.contents.layout.measuredWidth +. getMarginAxis child.contents CssFlexDirectionRow;
                  childHeight.contents =
                    child.contents.layout.measuredHeight +.
                    getMarginAxis child.contents CssFlexDirectionColumn;
                  let isCrossSizeDefinite = {contents: false};
                  if isMainAxisRow {
                    isCrossSizeDefinite.contents = isStyleDimDefined child.contents CssFlexDirectionColumn;
                    childHeight.contents = crossDim.contents
                  } else {
                    isCrossSizeDefinite.contents = isStyleDimDefined child.contents CssFlexDirectionRow;
                    childWidth.contents = crossDim.contents
                  };
                  if (not isCrossSizeDefinite.contents) {
                    childWidthMeasureMode.contents =
                      isUndefined childWidth.contents ? CssMeasureModeUndefined : CssMeasureModeExactly;
                    childHeightMeasureMode.contents =
                      isUndefined childHeight.contents ? CssMeasureModeUndefined : CssMeasureModeExactly;
                    let _ =
                      layoutNodeInternal
                        child.contents
                        childWidth.contents
                        childHeight.contents
                        direction
                        childWidthMeasureMode.contents
                        childHeightMeasureMode.contents
                        true
                        stretchString;
                    ()
                  }
                } else if (
                  alignItem !== CssAlignFlexStart
                ) {
                  let remainingCrossDim =
                    containerCrossAxis.contents -. getDimWithMargin child.contents crossAxis;
                  if (alignItem === CssAlignCenter) {
                    leadingCrossDim.contents =
                      leadingCrossDim.contents +. divideScalarByInt remainingCrossDim 2
                  } else {
                    leadingCrossDim.contents = leadingCrossDim.contents +. remainingCrossDim
                  }
                };
                setLayoutLeadingPositionForAxis
                  child.contents
                  crossAxis
                  (
                    layoutPosPositionForAxis child.contents crossAxis +. totalLineCrossDim.contents +.
                    leadingCrossDim.contents
                  )
              }
            }
          };
          totalLineCrossDim.contents = totalLineCrossDim.contents +. crossDim.contents;
          maxLineMainDim.contents = fmaxf maxLineMainDim.contents mainDim.contents;
          lineCount.contents = lineCount.contents + 1;
          startOfLineIndex.contents = endOfLineIndex.contents
        };
        if (lineCount.contents > 1 && performLayout && not (isUndefined availableInnerCrossDim)) {
          let remainingAlignContentDim = availableInnerCrossDim -. totalLineCrossDim.contents;
          let crossDimLead = {contents: zero};
          let currentLead = {contents: leadingPaddingAndBorderCross};
          let alignContent = node.style.alignContent;
          if (alignContent === CssAlignFlexEnd) {
            currentLead.contents = currentLead.contents +. remainingAlignContentDim
          } else if (
            alignContent === CssAlignCenter
          ) {
            currentLead.contents = currentLead.contents +. divideScalarByInt remainingAlignContentDim 2
          } else if (
            alignContent === CssAlignStretch
          ) {
            if (availableInnerCrossDim > totalLineCrossDim.contents) {
              crossDimLead.contents = divideScalarByInt remainingAlignContentDim lineCount.contents
            }
          };
          let endIndex = {contents: 0};
          for i in 0 to (lineCount.contents - 1) {
            let startIndex = endIndex.contents;
            let j = {contents: startIndex};
            let lineHeight = {contents: zero};
            let shouldContinue = {contents: false};
            while (j.contents < childCount && shouldContinue.contents) {
              child.contents = node.children.(j.contents);
              if (child.contents.style.positionType === CssPositionRelative) {
                if (child.contents.lineIndex !== i) {
                  shouldContinue.contents = false
                } else if (
                  isLayoutDimDefined child.contents crossAxis
                ) {
                  lineHeight.contents =
                    fmaxf
                      lineHeight.contents
                      (
                        layoutMeasuredDimensionForAxis child.contents crossAxis +.
                        getMarginAxis child.contents crossAxis
                      )
                }
              };
              j.contents = j.contents + 1
            };
            endIndex.contents = j.contents;
            lineHeight.contents = lineHeight.contents +. crossDimLead.contents;
            if performLayout {
              for j in startIndex to (endIndex.contents - 1) {
                child.contents = node.children.(j);
                if (child.contents.style.positionType === CssPositionRelative) {
                  switch (getAlignItem node child.contents) {
                  | CssAlignFlexStart =>
                    setLayoutLeadingPositionForAxis
                      child.contents
                      crossAxis
                      (currentLead.contents +. getLeadingMargin child.contents crossAxis)
                  | CssAlignFlexEnd =>
                    setLayoutLeadingPositionForAxis
                      child.contents
                      crossAxis
                      (
                        currentLead.contents +. lineHeight.contents -.
                        getTrailingMargin child.contents crossAxis -.
                        layoutMeasuredDimensionForAxis child.contents crossAxis
                      )
                  | CssAlignCenter =>
                    let childHeight = layoutMeasuredDimensionForAxis child.contents crossAxis;
                    setLayoutLeadingPositionForAxis
                      child.contents
                      crossAxis
                      (currentLead.contents +. divideScalarByInt (lineHeight.contents -. childHeight) 2)
                  | CssAlignStretch =>
                    setLayoutLeadingPositionForAxis
                      child.contents
                      crossAxis
                      (currentLead.contents +. getLeadingMargin child.contents crossAxis)
                  | CssAlignAuto => raise (Invalid_argument "getAlignItem should never return auto")
                  }
                }
              }
            };
            currentLead.contents = currentLead.contents +. lineHeight.contents
          }
        };
        /* STEP 9: COMPUTING FINAL DIMENSIONS */
        node.layout.measuredWidth = boundAxis node CssFlexDirectionRow (availableWidth -. marginAxisRow);
        node.layout.measuredHeight =
          boundAxis node CssFlexDirectionColumn (availableHeight -. marginAxisColumn);
        /* If the user didn't specify a width or height for the node, set the
         * dimensions based on the children. */
        if (measureModeMainDim === CssMeasureModeUndefined) {
          setLayoutMeasuredDimensionForAxis node mainAxis (boundAxis node mainAxis maxLineMainDim.contents)
        } else if (
          measureModeMainDim === CssMeasureModeAtMost
        ) {
          setLayoutMeasuredDimensionForAxis
            node
            mainAxis
            (
              fmaxf
                (
                  fminf
                    (availableInnerMainDim +. paddingAndBorderAxisMain)
                    (boundAxisWithinMinAndMax node mainAxis maxLineMainDim.contents)
                )
                paddingAndBorderAxisMain
            )
        };
        if (measureModeCrossDim === CssMeasureModeUndefined) {
          setLayoutMeasuredDimensionForAxis
            node
            crossAxis
            (boundAxis node crossAxis (totalLineCrossDim.contents +. paddingAndBorderAxisCross))
        } else if (
          measureModeCrossDim === CssMeasureModeAtMost
        ) {
          setLayoutMeasuredDimensionForAxis
            node
            crossAxis
            (
              fmaxf
                (
                  fminf
                    (availableInnerCrossDim +. paddingAndBorderAxisCross)
                    (
                      boundAxisWithinMinAndMax
                        node crossAxis (totalLineCrossDim.contents +. paddingAndBorderAxisCross)
                    )
                )
                paddingAndBorderAxisCross
            )
        };
        currentAbsoluteChild.contents = firstAbsoluteChild.contents;
        while (currentAbsoluteChild.contents !== theNullNode) {
          if performLayout {
            let childWidth = {contents: cssUndefined};
            let childHeight = {contents: cssUndefined};
            let childWidthMeasureMode = {contents: CssMeasureModeUndefined};
            let childHeightMeasureMode = {contents: CssMeasureModeUndefined};
            if (isStyleDimDefined currentAbsoluteChild.contents CssFlexDirectionRow) {
              childWidth.contents =
                currentAbsoluteChild.contents.style.width +.
                getMarginAxis currentAbsoluteChild.contents CssFlexDirectionRow
            } else if (
              isLeadingPosDefinedWithFallback currentAbsoluteChild.contents CssFlexDirectionRow &&
              isTrailingPosDefinedWithFallback currentAbsoluteChild.contents CssFlexDirectionRow
            ) {
              childWidth.contents =
                node.layout.measuredWidth -. (
                  getLeadingBorder node CssFlexDirectionRow +. getTrailingBorder node CssFlexDirectionRow
                ) -. (
                  getLeadingPositionWithFallback currentAbsoluteChild.contents CssFlexDirectionRow +.
                  getTrailingPositionWithFallback currentAbsoluteChild.contents CssFlexDirectionRow
                );
              childWidth.contents =
                boundAxis currentAbsoluteChild.contents CssFlexDirectionRow childWidth.contents
            };
            if (isStyleDimDefined currentAbsoluteChild.contents CssFlexDirectionColumn) {
              childHeight.contents =
                currentAbsoluteChild.contents.style.height +.
                getMarginAxis currentAbsoluteChild.contents CssFlexDirectionColumn
            } else if (
              /* If the child doesn't have a specified height, compute the height based on the top/bottom offsets if they're defined. */
              isLeadingPosDefinedWithFallback currentAbsoluteChild.contents CssFlexDirectionColumn &&
              isTrailingPosDefinedWithFallback currentAbsoluteChild.contents CssFlexDirectionColumn
            ) {
              childHeight.contents =
                node.layout.measuredHeight -. (
                  getLeadingBorder node CssFlexDirectionColumn +.
                  getTrailingBorder node CssFlexDirectionColumn
                ) -. (
                  getLeadingPositionWithFallback currentAbsoluteChild.contents CssFlexDirectionColumn +.
                  getTrailingPositionWithFallback currentAbsoluteChild.contents CssFlexDirectionColumn
                );
              childHeight.contents =
                boundAxis currentAbsoluteChild.contents CssFlexDirectionColumn childHeight.contents
            };
            if (isUndefined childWidth.contents || isUndefined childHeight.contents) {
              childWidthMeasureMode.contents =
                isUndefined childWidth.contents ? CssMeasureModeUndefined : CssMeasureModeExactly;
              childHeightMeasureMode.contents =
                isUndefined childHeight.contents ? CssMeasureModeUndefined : CssMeasureModeExactly;
              /*
               * According to the spec, if the main size is not definite and the
               * child's inline axis is parallel to the main axis (i.e. it's
               * horizontal), the child should be sized using "UNDEFINED" in
               * the main size. Otherwise use "AT_MOST" in the cross axis.
               */
              if (
                (not isMainAxisRow && isUndefined childWidth.contents) &&
                not (isUndefined availableInnerWidth)
              ) {
                childWidth.contents = availableInnerWidth;
                childWidthMeasureMode.contents = CssMeasureModeAtMost
              };
              /*
               * If child has no defined size in the cross axis and is set to stretch, set the cross
               * axis to be measured exactly with the available inner width
               */
              let _ =
                layoutNodeInternal
                  currentAbsoluteChild.contents
                  childWidth.contents
                  childHeight.contents
                  direction
                  childWidthMeasureMode.contents
                  childHeightMeasureMode.contents
                  false
                  absMeasureString;
              childWidth.contents =
                currentAbsoluteChild.contents.layout.measuredWidth +.
                getMarginAxis currentAbsoluteChild.contents CssFlexDirectionRow;
              childHeight.contents =
                currentAbsoluteChild.contents.layout.measuredHeight +.
                getMarginAxis currentAbsoluteChild.contents CssFlexDirectionColumn
            };
            let _ =
              layoutNodeInternal
                currentAbsoluteChild.contents
                childWidth.contents
                childHeight.contents
                direction
                CssMeasureModeExactly
                CssMeasureModeExactly
                true
                absLayoutString;
            if (
              isTrailingPosDefinedWithFallback currentAbsoluteChild.contents mainAxis &&
              not (isLeadingPosDefinedWithFallback currentAbsoluteChild.contents mainAxis)
            ) {
              setLayoutLeadingPositionForAxis
                currentAbsoluteChild.contents
                mainAxis
                (
                  layoutMeasuredDimensionForAxis node mainAxis -.
                  layoutMeasuredDimensionForAxis currentAbsoluteChild.contents mainAxis -.
                  getTrailingPositionWithFallback currentAbsoluteChild.contents mainAxis
                )
            };
            if (
              isTrailingPosDefinedWithFallback currentAbsoluteChild.contents crossAxis &&
              not (isLeadingPosDefinedWithFallback currentAbsoluteChild.contents crossAxis)
            ) {
              setLayoutLeadingPositionForAxis
                currentAbsoluteChild.contents
                crossAxis
                (
                  layoutMeasuredDimensionForAxis node crossAxis -.
                  layoutMeasuredDimensionForAxis currentAbsoluteChild.contents crossAxis -.
                  getTrailingPositionWithFallback currentAbsoluteChild.contents crossAxis
                )
            }
          };
          currentAbsoluteChild.contents = currentAbsoluteChild.contents.nextChild
        };
        /* STEP 11: SETTING TRAILING POSITIONS FOR CHILDREN */
        if performLayout {
          let needsMainTrailingPos =
            mainAxis == CssFlexDirectionRowReverse || mainAxis == CssFlexDirectionColumnReverse;
          let needsCrossTrailingPos =
            crossAxis == CssFlexDirectionRowReverse || crossAxis == CssFlexDirectionColumnReverse;
          /* Set trailing position if necessary. */
          if (needsMainTrailingPos || needsCrossTrailingPos) {
            for i in 0 to (childCount - 1) {
              let child = node.children.(i);
              if needsMainTrailingPos {
                setTrailingPosition node child mainAxis
              };
              if needsCrossTrailingPos {
                setTrailingPosition node child crossAxis
              }
            }
          }
        }
      }
    }
  }
  /** END_GENERATED **/
};

let layoutNode node availableWidth availableHeight parentDirection => {
  /* Increment the generation count. This will force the recursive routine to visit*/
  /* all dirty nodes at least once. Subsequent visits will be skipped if the input*/
  /* parameters don't change.*/
  gCurrentGenerationCount.contents = gCurrentGenerationCount.contents + 1;
  /* If the caller didn't specify a height/width, use the dimensions*/
  /* specified in the style.*/
  let (availableWidth, widthMeasureMode) =
    if (not (isUndefined availableWidth)) {
      (availableWidth, CssMeasureModeExactly)
    } else if (
      isStyleDimDefined node CssFlexDirectionRow
    ) {
      (node.style.width +. getMarginAxis node CssFlexDirectionRow, CssMeasureModeExactly)
    } else if (
      node.style.maxWidth >= zero
    ) {
      (node.style.maxWidth, CssMeasureModeAtMost)
    } else {
      (availableWidth, CssMeasureModeUndefined)
    };
  let (availableHeight, heightMeasureMode) =
    if (not (isUndefined availableHeight)) {
      (availableHeight, CssMeasureModeExactly)
    } else if (
      isStyleDimDefined node CssFlexDirectionColumn
    ) {
      (node.style.height +. getMarginAxis node CssFlexDirectionColumn, CssMeasureModeExactly)
    } else if (
      node.style.maxHeight >= zero
    ) {
      (node.style.maxHeight, CssMeasureModeAtMost)
    } else {
      (availableHeight, CssMeasureModeUndefined)
    };
  if (
    layoutNodeInternal
      node
      availableWidth
      availableHeight
      parentDirection
      widthMeasureMode
      heightMeasureMode
      true
      initialString
  ) {
    setPosition node node.layout.direction;
    if gPrintTree.contents {
      LayoutPrint.printCssNode (node, {printLayout: true, printChildren: true, printStyle: true})
    }
  }
};