float-pigment-layout 0.8.2

A light-weight layout engine which supports common web layout algorithms.
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
//! Track Sizing Algorithm Implementation
//!
//! CSS Grid §11.5 (Resolve Intrinsic Track Sizes):
//! https://www.w3.org/TR/css-grid-1/#algo-content
//!
//! CSS Grid §11.7 (Expand Flexible Tracks):
//! https://www.w3.org/TR/css-grid-1/#algo-flex-tracks

use alloc::vec::Vec;
use float_pigment_css::length_num::LengthNum;

use crate::{
    algo::grid::track::GridTracks, types::MinMax, DefLength, LayoutGridAuto, LayoutTrackListItem,
    LayoutTrackSize, LayoutTreeNode, OptionNum, OptionSize, Size,
};

use super::matrix::GridLayoutMatrix;

/// Resolve fr track sizes using the iterative algorithm from CSS Grid §11.7.
///
/// **Definite free space** (§11.7):
/// 1. hypothetical_fr_size = remaining_space / active_flex
/// 2. If any fr track's size < its min-content, freeze it at min-content
/// 3. Repeat until stable
///
/// **Indefinite free space** (§11.7.1 "Find the Size of an fr"):
/// When `available_space` is `None`, fr sizes are derived from items'
/// max-content contributions:
/// 1. For each fr track, compute hypothetical_1fr = max_content / fr_value
/// 2. Take the maximum hypothetical_1fr across all fr tracks
/// 3. Each fr track's size = hypothetical_1fr × fr_value
/// 4. Clamp: if a track's size < min-content, use min-content instead
fn resolve_fr_track_sizes<L: LengthNum + Copy>(
    tracks: &mut [TrackInfo<L>],
    available_space: OptionNum<L>,
) {
    let total_fr: f32 = tracks
        .iter()
        .filter(|t| t.track_type == IntrinsicTrackType::Fr)
        .map(|t| t.fr_value)
        .sum();

    if total_fr <= 0.0 {
        return;
    }

    match available_space.val() {
        Some(available) => {
            // Definite free space: distribute remaining space proportionally
            resolve_fr_definite(tracks, available);
        }
        None => {
            // Indefinite free space (§11.7.1): derive fr size from max-content
            resolve_fr_indefinite(tracks);
        }
    }
}

/// §11.7: Resolve fr tracks with definite free space.
///
/// Iteratively distributes remaining space among fr tracks, freezing
/// tracks at min-content when their hypothetical size is too small.
fn resolve_fr_definite<L: LengthNum + Copy>(tracks: &mut [TrackInfo<L>], available: L) {
    let total_non_fr_size: L = tracks
        .iter()
        .filter(|t| t.track_type != IntrinsicTrackType::Fr)
        .fold(L::zero(), |acc, t| acc + t.base_size.unwrap_or(L::zero()));

    let initial_remaining = if available > total_non_fr_size {
        available - total_non_fr_size
    } else {
        L::zero()
    };

    let mut remaining_space = initial_remaining;
    let mut active_flex: f32 = tracks
        .iter()
        .filter(|t| t.track_type == IntrinsicTrackType::Fr)
        .map(|t| t.fr_value)
        .sum();
    let mut flexible_indices: Vec<usize> = (0..tracks.len())
        .filter(|&i| tracks[i].track_type == IntrinsicTrackType::Fr)
        .collect();
    if flexible_indices.is_empty() {
        return;
    }
    loop {
        if active_flex <= 0.0 {
            break;
        }
        let hypothetical_fr_size = remaining_space.div_f32(active_flex);

        let mut any_frozen = false;
        flexible_indices.retain(|&i| {
            let hypothetical_size = hypothetical_fr_size.mul_f32(tracks[i].fr_value);
            if hypothetical_size < tracks[i].min_content {
                tracks[i].base_size = Some(tracks[i].min_content);
                remaining_space -= tracks[i].min_content;
                active_flex -= tracks[i].fr_value;
                any_frozen = true;
                false
            } else {
                true
            }
        });

        if !any_frozen {
            // All tracks are stable, apply final sizes
            for &i in &flexible_indices {
                let fr_size = hypothetical_fr_size.mul_f32(tracks[i].fr_value);
                tracks[i].base_size = Some(fr_size);
            }
            break;
        }
    }
}

/// §11.7.1: Resolve fr tracks with indefinite free space.
///
/// Per W3C CSS Grid §11.7.1 "Find the Size of an fr":
/// When free space is indefinite, the fr size is derived from items'
/// max-content contributions:
/// 1. For each fr track, compute hypothetical_1fr = max_content / fr_value
/// 2. The unified 1fr size = max(all hypothetical_1fr values)
/// 3. Each fr track's base_size = unified_1fr × fr_value
/// 4. Clamp: if base_size < min_content, use min_content
fn resolve_fr_indefinite<L: LengthNum + Copy>(tracks: &mut [TrackInfo<L>]) {
    // Step 1-2: Find the largest hypothetical 1fr size across all fr tracks
    let mut hypothetical_1fr = L::zero();
    for track in tracks.iter() {
        if track.track_type == IntrinsicTrackType::Fr && track.fr_value > 0.0 {
            let candidate = track.max_content.div_f32(track.fr_value);
            if candidate > hypothetical_1fr {
                hypothetical_1fr = candidate;
            }
        }
    }

    // Step 3-4: Apply the unified 1fr size to each fr track, clamped to min-content
    for track in tracks.iter_mut() {
        if track.track_type == IntrinsicTrackType::Fr {
            let fr_size = hypothetical_1fr.mul_f32(track.fr_value);
            track.base_size = Some(fr_size.max(track.min_content));
        }
    }
}

/// The type of track sizing function for intrinsic sizing (§11.5).
///
/// CSS Grid §7.2: Track Sizing Functions
/// <https://www.w3.org/TR/css-grid-1/#track-sizing>
///
/// This determines how the track participates in intrinsic sizing:
/// - Fixed: size is predetermined, no intrinsic sizing needed
/// - Auto: min = min-content, max = max-content (§11.5)
/// - MinContent: min = min-content, max = min-content (§11.5)
/// - MaxContent: min = min-content, max = max-content (§11.5)
/// - Fr: handled separately in §11.7
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) enum IntrinsicTrackType {
    /// A fixed-size track (length or percentage). No intrinsic sizing needed.
    Fixed,
    /// An `auto` track: min = min-content, max = max-content.
    Auto,
    /// A `min-content` track: min = min-content, max = min-content.
    MinContent,
    /// A `max-content` track: min = min-content, max = max-content.
    MaxContent,
    /// An `fr` track: handled separately in §11.7.
    Fr,
}

pub(crate) struct TrackInfo<L: LengthNum> {
    /// The track's computed base size (§11.4).
    pub(crate) base_size: Option<L>,
    /// The track's growth limit (§11.4).
    /// `None` represents infinity.
    pub(crate) growth_limit: Option<L>,
    pub(crate) fr_value: f32,
    pub(crate) min_content: L,
    pub(crate) max_content: L,
    /// The type of track for intrinsic sizing purposes (§11.5).
    pub(crate) track_type: IntrinsicTrackType,
}

impl<L: LengthNum + Copy> TrackInfo<L> {
    fn new() -> Self {
        Self {
            base_size: None,
            growth_limit: None,
            fr_value: 0.0,
            min_content: L::zero(),
            max_content: L::zero(),
            track_type: IntrinsicTrackType::Auto,
        }
    }
}

/// Determine IntrinsicTrackType from a LayoutTrackSize.
///
/// CSS Grid §7.2: Track Sizing Functions
/// <https://www.w3.org/TR/css-grid-1/#track-sizing>
pub(crate) fn classify_track_type<L: LengthNum, C: PartialEq + Clone>(
    track_size: &LayoutTrackSize<L, C>,
) -> IntrinsicTrackType {
    match track_size {
        LayoutTrackSize::Length(DefLength::Points(_) | DefLength::Percent(_)) => {
            IntrinsicTrackType::Fixed
        }
        LayoutTrackSize::Length(_) => IntrinsicTrackType::Auto,
        LayoutTrackSize::Fr(_) => IntrinsicTrackType::Fr,
        LayoutTrackSize::MinContent => IntrinsicTrackType::MinContent,
        LayoutTrackSize::MaxContent => IntrinsicTrackType::MaxContent,
    }
}

impl IntrinsicTrackType {
    /// Whether this track needs a min-content layout pass.
    ///
    /// All non-fixed tracks need min-content: auto, fr, min-content, max-content.
    /// Fixed tracks (length/percentage) do not.
    #[inline]
    pub(crate) fn needs_min_content(self) -> bool {
        self != IntrinsicTrackType::Fixed
    }

    /// Whether this track needs a max-content layout pass.
    ///
    /// §11.5 Step 4: Tracks with intrinsic max sizing functions
    /// (auto, max-content) need max-content contributions.
    /// §11.7.1: Fr tracks also need max-content for indefinite free space.
    #[inline]
    pub(crate) fn needs_max_content(self) -> bool {
        matches!(
            self,
            IntrinsicTrackType::Auto | IntrinsicTrackType::MaxContent | IntrinsicTrackType::Fr
        )
    }
}

/// Classify a track at a given index, handling both explicit and implicit tracks.
///
/// For explicit tracks, classifies from the track list.
/// For implicit tracks (§7.6), classifies from grid-auto-rows/columns.
pub(crate) fn classify_track_at_index<L: LengthNum, C: PartialEq + Clone>(
    index: usize,
    explicit_track_list: &[&LayoutTrackListItem<L, C>],
    grid_auto_tracks: &LayoutGridAuto<L, C>,
) -> IntrinsicTrackType {
    if let Some(track_item) = explicit_track_list.get(index) {
        match track_item {
            LayoutTrackListItem::TrackSize(track_size) => classify_track_type(track_size),
            _ => IntrinsicTrackType::Fixed,
        }
    } else {
        let implicit_index = index - explicit_track_list.len();
        classify_track_type(&grid_auto_tracks.get(implicit_index))
    }
}

/// Initialize track info for one axis.
///
/// CSS Grid §7.6: Implicit tracks use grid-auto-rows/columns.
/// For each track position, determines its type (Fixed/Auto/MinContent/MaxContent/Fr)
/// from either the explicit track list or implicit auto tracks.
fn init_track_infos<L: LengthNum + Copy, C: PartialEq + Clone>(
    track_count: usize,
    explicit_track_list: &[&LayoutTrackListItem<L, C>],
    grid_auto_tracks: &LayoutGridAuto<L, C>,
) -> Vec<TrackInfo<L>> {
    let explicit_count = explicit_track_list.len();
    (0..track_count)
        .map(|i| {
            let mut info = TrackInfo::new();
            if let Some(LayoutTrackListItem::TrackSize(track_size)) = explicit_track_list.get(i) {
                info.track_type = classify_track_type(track_size);
                if let LayoutTrackSize::Fr(fr_value) = track_size {
                    info.fr_value = *fr_value;
                }
            } else if i >= explicit_count {
                // Implicit track (§7.6)
                let implicit_index = i - explicit_count;
                let implicit_track_size = grid_auto_tracks.get(implicit_index);
                info.track_type = classify_track_type(&implicit_track_size);
                if let LayoutTrackSize::Fr(fr_value) = implicit_track_size {
                    info.fr_value = fr_value;
                }
            }
            info
        })
        .collect()
}

/// Update a single track's intrinsic sizes based on an item's contribution.
///
/// CSS Grid §11.5: Resolve Intrinsic Track Sizes
/// <https://www.w3.org/TR/css-grid-1/#algo-content>
///
/// - §11.5 Step 2: For tracks with intrinsic min sizing function,
///   increase base_size to the item's min-content contribution.
/// - §11.5 Step 4: For tracks with intrinsic max sizing function,
///   increase growth_limit to the item's max-content contribution.
///
/// For fixed tracks: use the explicit track size (§11.4).
/// For fr tracks: only min_content is updated (used as freeze threshold §11.7).
fn update_track_intrinsic_sizes<L: LengthNum + Copy>(
    track: &mut TrackInfo<L>,
    outer_min_content: L,
    effective_min_content: L,
    outer_max_content: L,
    fixed_track_size: OptionNum<L>,
) {
    // Always update min_content for all tracks (used as fr freeze threshold §11.7)
    track.min_content = track.min_content.max(outer_min_content);
    // Always update max_content for all tracks (used for §11.7.1 indefinite fr sizing)
    track.max_content = track.max_content.max(outer_max_content);

    if track.track_type == IntrinsicTrackType::Fr {
        return;
    }

    match track.track_type {
        IntrinsicTrackType::Fixed => {
            // Fixed track: use the specified size (§11.4)
            if fixed_track_size.is_some() {
                let size = fixed_track_size.val().unwrap();
                track.base_size = Some(track.base_size.map(|s| s.max(size)).unwrap_or(size));
                // Fixed track: growth_limit = base_size (§11.4)
                track.growth_limit = track.base_size;
            }
        }
        IntrinsicTrackType::Auto => {
            // §11.5 Step 2: auto min -> increase base_size to min-content contribution.
            track.base_size = Some(
                track
                    .base_size
                    .map_or(effective_min_content, |s| s.max(effective_min_content)),
            );
            // §11.4: auto max -> growth_limit = infinity (None).
            // §11.5 Step 4: increase(infinity, max-content) = infinity.
            // This allows §11.6 Maximize to distribute free space.
            track.growth_limit = None;
        }
        IntrinsicTrackType::MinContent => {
            // §11.5 Step 2: base_size = min-content contribution
            track.base_size = Some(
                track
                    .base_size
                    .map(|s| s.max(effective_min_content))
                    .unwrap_or(effective_min_content),
            );
            // §11.5 Step 4: min-content max -> growth_limit = min-content
            track.growth_limit = Some(
                track
                    .growth_limit
                    .map(|s| s.max(effective_min_content))
                    .unwrap_or(effective_min_content),
            );
        }
        IntrinsicTrackType::MaxContent => {
            // §11.5 Step 2: base_size = min-content contribution
            track.base_size = Some(
                track
                    .base_size
                    .map(|s| s.max(effective_min_content))
                    .unwrap_or(effective_min_content),
            );
            // §11.5 Step 4: max-content max -> growth_limit = max-content
            track.growth_limit = Some(
                track
                    .growth_limit
                    .map(|s| s.max(outer_max_content))
                    .unwrap_or(outer_max_content),
            );
        }
        // Fr is handled by the early return above
        IntrinsicTrackType::Fr => {}
    }
}

/// Resolve the fixed track size for an implicit or explicit track.
///
/// For explicit tracks, uses the item's pre-resolved track size.
/// For implicit tracks (§7.6), resolves from grid-auto-rows/columns.
fn resolve_fixed_track_size<T: LayoutTreeNode>(
    track_index: usize,
    explicit_count: usize,
    item_track_size: OptionNum<T::Length>,
    grid_auto_tracks: &LayoutGridAuto<T::Length, T::LengthCustom>,
    available_space: OptionNum<T::Length>,
    node: &T,
) -> OptionNum<T::Length> {
    if track_index < explicit_count {
        item_track_size
    } else {
        let implicit_index = track_index - explicit_count;
        match grid_auto_tracks.get(implicit_index) {
            LayoutTrackSize::Length(def_len) => def_len.resolve(available_space, node),
            _ => OptionNum::none(),
        }
    }
}

/// Compute track sizes based on item content.
///
/// Implements the core parts of the Track Sizing Algorithm:
/// - CSS Grid §11.5 (Resolve Intrinsic Track Sizes):
///   <https://www.w3.org/TR/css-grid-1/#algo-content>
/// - CSS Grid §11.7 (Expand Flexible Tracks):
///   <https://www.w3.org/TR/css-grid-1/#algo-flex-tracks>
///
/// Phase 1: Resolve intrinsic track sizes (§11.5)
///   Step 2 - For tracks with intrinsic min sizing function (auto, min-content,
///   max-content): set base_size = item's min-content contribution
///   Step 4 - For tracks with intrinsic max sizing function:
///   set growth_limit = item's max-content contribution
///   (auto/max-content -> max-content; min-content -> min-content)
///
///   For fixed tracks: use the specified size for both base_size and growth_limit
///   For fr tracks: collect min-content as freeze threshold
///
/// Phase 2: Iterative fr algorithm (§11.7)
/// 1. Calculate hypothetical_fr_size = remaining_space / total_flex
/// 2. If any fr track's size < its min-content, freeze it at min-content
/// 3. Repeat until stable
///
/// Returns `(column_tracks, row_tracks)` ready for §11.6/§11.8 processing.
#[allow(clippy::too_many_arguments)]
pub(crate) fn compute_track_sizes<T: LayoutTreeNode>(
    grid_layout_matrix: &GridLayoutMatrix<T>,
    column_track_list: &[&LayoutTrackListItem<T::Length, T::LengthCustom>],
    row_track_list: &[&LayoutTrackListItem<T::Length, T::LengthCustom>],
    available_grid_space: OptionSize<T::Length>,
    grid_auto_columns: &LayoutGridAuto<T::Length, T::LengthCustom>,
    grid_auto_rows: &LayoutGridAuto<T::Length, T::LengthCustom>,
) -> (GridTracks<T>, GridTracks<T>) {
    let explicit_column_count = column_track_list.len();
    let explicit_row_count = row_track_list.len();

    // Initialize track info for both axes (§7.6)
    let mut columns = init_track_infos(
        grid_layout_matrix.column_count(),
        column_track_list,
        grid_auto_columns,
    );
    let mut rows = init_track_infos(
        grid_layout_matrix.row_count(),
        row_track_list,
        grid_auto_rows,
    );

    // ═══════════════════════════════════════════════════════════════════════
    // Phase 1: Resolve intrinsic track sizes (§11.5)
    // CSS Grid §11.5: Resolve Intrinsic Track Sizes
    // https://www.w3.org/TR/css-grid-1/#algo-content
    //
    // §11.5 Step 2: For tracks with an intrinsic min track sizing function,
    //   increase base_size to the item's min-content contribution.
    //
    // §11.5 Step 4: For tracks with an intrinsic max track sizing function,
    //   increase growth_limit to the item's max-content contribution.
    //   - auto / max-content -> growth_limit = max-content contribution
    //   - min-content -> growth_limit = min-content contribution
    //
    // For fixed tracks: use the explicit track size (§11.4)
    // For fr tracks: collect min-content for freeze threshold (§11.7)
    // ═══════════════════════════════════════════════════════════════════════

    for item in grid_layout_matrix.items() {
        let row = item.row();
        let column = item.column();

        let css_width = item.css_size.width;
        let css_height = item.css_size.height;
        let min_content_size = item.min_content_size().copied().unwrap_or(Size::zero());

        // §11.5 Step 2: min-content contribution for base_size
        // If item has a CSS size, use it; otherwise use min-content
        let min_content_width = if css_width.is_some() {
            css_width.val().unwrap().max(min_content_size.width)
        } else {
            min_content_size.width
        };
        let outer_min_content_width = min_content_width + item.margin.horizontal();

        let min_content_height = if css_height.is_some() {
            css_height.val().unwrap().max(min_content_size.height)
        } else {
            min_content_size.height
        };
        let outer_min_content_height = min_content_height + item.margin.vertical();

        // §11.5 Step 4: max-content contribution for growth_limit
        //
        // For columns: use max_content_size (unconstrained layout) to get the
        // true max-content width. This is the size the item would take with
        // infinite available space.
        //
        // For rows: use computed_size (constrained by resolved column width).
        // Per §11.5, row contributions are computed using the item's resolved
        // column size, so the constrained layout result is correct.
        let max_content_size = item.max_content_size().copied();
        let max_content_width = if css_width.is_some() {
            css_width.val().unwrap()
        } else {
            max_content_size.map_or(item.computed_size().width, |s| s.width)
        };
        let outer_max_content_width = max_content_width + item.margin.horizontal();

        let max_content_height = if css_height.is_some() {
            css_height.val().unwrap()
        } else {
            item.computed_size().height
        };
        let outer_max_content_height = max_content_height + item.margin.vertical();

        // Intrinsic sizing invariants from CSS Sizing:
        // min-content contribution must not exceed max-content contribution.
        let effective_min_content_width = outer_min_content_width.min(outer_max_content_width);
        let effective_min_content_height = outer_min_content_height.min(outer_max_content_height);

        // Resolve fixed track sizes for implicit/explicit tracks
        let col_fixed_size = resolve_fixed_track_size::<T>(
            column,
            explicit_column_count,
            item.track_inline_size(),
            grid_auto_columns,
            available_grid_space.width,
            item.node,
        );
        let row_fixed_size = resolve_fixed_track_size::<T>(
            row,
            explicit_row_count,
            item.track_block_size(),
            grid_auto_rows,
            available_grid_space.height,
            item.node,
        );

        // Update column track
        update_track_intrinsic_sizes(
            &mut columns[column],
            outer_min_content_width,
            effective_min_content_width,
            outer_max_content_width,
            col_fixed_size,
        );

        // Update row track
        update_track_intrinsic_sizes(
            &mut rows[row],
            outer_min_content_height,
            effective_min_content_height,
            outer_max_content_height,
            row_fixed_size,
        );
    }

    // ═══════════════════════════════════════════════════════════════════════
    // Phase 2: Calculate fr track sizes with iterative algorithm (§11.7)
    // CSS Grid §11.7: Expand Flexible Tracks
    // https://www.w3.org/TR/css-grid-1/#algo-flex-tracks
    //
    // The algorithm iteratively calculates fr sizes:
    // 1. hypothetical_fr_size = leftover_space / total_flex
    // 2. If any fr track's size < its min-content, freeze it at min-content
    // 3. Repeat until stable
    // ═══════════════════════════════════════════════════════════════════════

    resolve_fr_track_sizes(&mut columns, available_grid_space.width);
    resolve_fr_track_sizes(&mut rows, available_grid_space.height);

    (
        GridTracks::from_track_info(&columns),
        GridTracks::from_track_info(&rows),
    )
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Helper: create a non-fr TrackInfo with a fixed base_size.
    fn fixed_track(base: f32) -> TrackInfo<f32> {
        TrackInfo {
            base_size: Some(base),
            growth_limit: Some(base),
            fr_value: 0.0,
            min_content: 0.0,
            max_content: 0.0,
            track_type: IntrinsicTrackType::Fixed,
        }
    }

    /// Helper: create an fr TrackInfo.
    fn fr_track(fr: f32, min_content: f32) -> TrackInfo<f32> {
        TrackInfo {
            base_size: None,
            growth_limit: None,
            fr_value: fr,
            min_content,
            max_content: 0.0,
            track_type: IntrinsicTrackType::Fr,
        }
    }

    /// Helper: create an fr TrackInfo with max_content (for indefinite tests).
    fn fr_track_with_max(fr: f32, min_content: f32, max_content: f32) -> TrackInfo<f32> {
        TrackInfo {
            base_size: None,
            growth_limit: None,
            fr_value: fr,
            min_content,
            max_content,
            track_type: IntrinsicTrackType::Fr,
        }
    }

    // ═══════════════════════════════════════════════════════════════════════
    // §11.7: Expand Flexible Tracks (resolve_fr_track_sizes)
    // <https://www.w3.org/TR/css-grid-1/#algo-flex-tracks>
    // ═══════════════════════════════════════════════════════════════════════

    #[test]
    fn fr_single_track_takes_all_remaining_space() {
        // CSS Grid §11.7: A single 1fr track should consume all remaining
        // space after non-flexible tracks are subtracted.
        //
        // Layout: grid-template-columns: 100px 1fr
        // Available: 400px -> remaining = 400 - 100 = 300px
        let mut tracks = vec![fixed_track(100.0), fr_track(1.0, 0.0)];
        resolve_fr_track_sizes(&mut tracks, OptionNum::some(400.0));
        assert_eq!(tracks[0].base_size, Some(100.0));
        assert_eq!(tracks[1].base_size, Some(300.0));
    }

    #[test]
    fn fr_multiple_tracks_distribute_proportionally() {
        // CSS Grid §11.7: Multiple fr tracks share remaining space
        // proportionally to their flex factor.
        //
        // Layout: grid-template-columns: 1fr 2fr 1fr
        // Available: 400px -> each fr unit = 400 / 4 = 100px
        let mut tracks = vec![fr_track(1.0, 0.0), fr_track(2.0, 0.0), fr_track(1.0, 0.0)];
        resolve_fr_track_sizes(&mut tracks, OptionNum::some(400.0));
        assert_eq!(tracks[0].base_size, Some(100.0));
        assert_eq!(tracks[1].base_size, Some(200.0));
        assert_eq!(tracks[2].base_size, Some(100.0));
    }

    #[test]
    fn fr_with_fixed_tracks_subtracts_fixed_first() {
        // CSS Grid §11.7: Fixed tracks are subtracted from available space
        // before distributing to fr tracks.
        //
        // Layout: grid-template-columns: 50px 1fr 50px 2fr
        // Available: 350px -> remaining = 350 - 50 - 50 = 250px
        // fr unit = 250 / 3 = 83.333...
        let mut tracks = vec![
            fixed_track(50.0),
            fr_track(1.0, 0.0),
            fixed_track(50.0),
            fr_track(2.0, 0.0),
        ];
        resolve_fr_track_sizes(&mut tracks, OptionNum::some(350.0));
        assert_eq!(tracks[0].base_size, Some(50.0)); // fixed
        let fr_unit = 250.0 / 3.0;
        assert!((tracks[1].base_size.unwrap() - fr_unit).abs() < 0.01);
        assert_eq!(tracks[2].base_size, Some(50.0)); // fixed
        assert!((tracks[3].base_size.unwrap() - fr_unit * 2.0).abs() < 0.01);
    }

    #[test]
    fn fr_freeze_at_min_content() {
        // CSS Grid §11.7 iterative freeze: If a hypothetical fr size would
        // make a track smaller than its min-content, that track is frozen at
        // min-content and the remaining space is redistributed.
        //
        // Layout: grid-template-columns: 1fr 1fr
        // Available: 100px, track[0].min_content = 80px
        // Round 1: hypothetical = 100/2 = 50 < 80 -> freeze track[0] at 80
        // Round 2: remaining = 100 - 80 = 20 -> track[1] = 20
        let mut tracks = vec![fr_track(1.0, 80.0), fr_track(1.0, 0.0)];
        resolve_fr_track_sizes(&mut tracks, OptionNum::some(100.0));
        assert_eq!(tracks[0].base_size, Some(80.0));
        assert_eq!(tracks[1].base_size, Some(20.0));
    }

    #[test]
    fn fr_all_frozen_at_min_content() {
        // CSS Grid §11.7: When all fr tracks are frozen because their
        // hypothetical size < min-content, each gets its min-content.
        //
        // Layout: grid-template-columns: 1fr 1fr
        // Available: 100px, min-content = 60 each
        // Round 1: hypothetical = 100/2 = 50 < 60 -> both frozen
        let mut tracks = vec![fr_track(1.0, 60.0), fr_track(1.0, 60.0)];
        resolve_fr_track_sizes(&mut tracks, OptionNum::some(100.0));
        assert_eq!(tracks[0].base_size, Some(60.0));
        assert_eq!(tracks[1].base_size, Some(60.0));
    }

    #[test]
    fn fr_no_available_space_returns_zero() {
        // CSS Grid §11.7: When available space is zero, fr tracks get zero.
        let mut tracks = vec![fr_track(1.0, 0.0), fr_track(2.0, 0.0)];
        resolve_fr_track_sizes(&mut tracks, OptionNum::some(0.0));
        assert_eq!(tracks[0].base_size, Some(0.0));
        assert_eq!(tracks[1].base_size, Some(0.0));
    }

    #[test]
    fn fr_indefinite_container_uses_max_content() {
        // CSS Grid §11.7.1: When free space is indefinite, fr sizes are
        // derived from items' max-content contributions.
        //
        // Layout: grid-template-columns: 1fr 2fr
        // item-a (1fr): max-content = 100px → hypothetical 1fr = 100/1 = 100
        // item-b (2fr): max-content = 200px → hypothetical 1fr = 200/2 = 100
        // unified 1fr = max(100, 100) = 100
        // Result: 1fr = 100px, 2fr = 200px
        let mut tracks = vec![
            fr_track_with_max(1.0, 0.0, 100.0),
            fr_track_with_max(2.0, 0.0, 200.0),
        ];
        resolve_fr_track_sizes(&mut tracks, OptionNum::none());
        assert_eq!(tracks[0].base_size, Some(100.0));
        assert_eq!(tracks[1].base_size, Some(200.0));
    }

    #[test]
    fn fr_indefinite_picks_largest_hypothetical() {
        // §11.7.1: The unified 1fr = max of all (max-content / fr_value).
        //
        // Layout: grid-template-columns: 1fr 2fr
        // item-a (1fr): max-content = 150px → hypothetical 1fr = 150/1 = 150
        // item-b (2fr): max-content = 200px → hypothetical 1fr = 200/2 = 100
        // unified 1fr = max(150, 100) = 150
        // Result: 1fr = 150px, 2fr = 300px
        let mut tracks = vec![
            fr_track_with_max(1.0, 0.0, 150.0),
            fr_track_with_max(2.0, 0.0, 200.0),
        ];
        resolve_fr_track_sizes(&mut tracks, OptionNum::none());
        assert_eq!(tracks[0].base_size, Some(150.0));
        assert_eq!(tracks[1].base_size, Some(300.0));
    }

    #[test]
    fn fr_indefinite_clamps_to_min_content() {
        // §11.7.1: fr size should not be smaller than min-content.
        //
        // Layout: grid-template-columns: 1fr 1fr
        // item-a (1fr): max-content = 50px, min-content = 80px
        // item-b (1fr): max-content = 50px, min-content = 0px
        // hypothetical 1fr = max(50/1, 50/1) = 50
        // track-a: max(50, 80) = 80 (clamped to min-content)
        // track-b: max(50, 0) = 50
        let mut tracks = vec![
            fr_track_with_max(1.0, 80.0, 50.0),
            fr_track_with_max(1.0, 0.0, 50.0),
        ];
        resolve_fr_track_sizes(&mut tracks, OptionNum::none());
        assert_eq!(tracks[0].base_size, Some(80.0));
        assert_eq!(tracks[1].base_size, Some(50.0));
    }

    #[test]
    fn fr_indefinite_zero_max_content() {
        // §11.7.1: When all max-content = 0, fr tracks get zero
        // (or min-content if min-content > 0).
        let mut tracks = vec![
            fr_track_with_max(1.0, 0.0, 0.0),
            fr_track_with_max(2.0, 0.0, 0.0),
        ];
        resolve_fr_track_sizes(&mut tracks, OptionNum::none());
        assert_eq!(tracks[0].base_size, Some(0.0));
        assert_eq!(tracks[1].base_size, Some(0.0));
    }

    #[test]
    fn fr_zero_total_fr_is_noop() {
        // Edge case: total_fr = 0 should be a no-op.
        let mut tracks = vec![fixed_track(100.0)];
        resolve_fr_track_sizes(&mut tracks, OptionNum::some(400.0));
        assert_eq!(tracks[0].base_size, Some(100.0)); // unchanged
    }

    #[test]
    fn fr_fixed_consumes_all_space_fr_gets_zero() {
        // CSS Grid §11.7: When non-fr tracks consume all available space,
        // remaining = 0, fr tracks get zero.
        //
        // Layout: grid-template-columns: 300px 1fr
        // Available: 300px -> remaining = 0
        let mut tracks = vec![fixed_track(300.0), fr_track(1.0, 0.0)];
        resolve_fr_track_sizes(&mut tracks, OptionNum::some(300.0));
        assert_eq!(tracks[0].base_size, Some(300.0));
        assert_eq!(tracks[1].base_size, Some(0.0));
    }

    // ═══════════════════════════════════════════════════════════════════════
    // IntrinsicTrackType classification
    // <https://www.w3.org/TR/css-grid-1/#track-sizing>
    // ═══════════════════════════════════════════════════════════════════════

    #[test]
    fn classify_track_type_fixed_points() {
        // §7.2: Fixed track sizing function (e.g. 100px)
        let ts: LayoutTrackSize<f32, i32> = LayoutTrackSize::Length(DefLength::Points(100.0));
        assert_eq!(
            classify_track_type::<f32, i32>(&ts),
            IntrinsicTrackType::Fixed
        );
    }

    #[test]
    fn classify_track_type_fixed_percent() {
        // §7.2: Percentage is also a fixed sizing function
        let ts: LayoutTrackSize<f32, i32> = LayoutTrackSize::Length(DefLength::Percent(0.5));
        assert_eq!(
            classify_track_type::<f32, i32>(&ts),
            IntrinsicTrackType::Fixed
        );
    }

    #[test]
    fn classify_track_type_auto() {
        // §7.2: Auto maps to min-content/max-content intrinsic sizing
        let ts: LayoutTrackSize<f32, i32> = LayoutTrackSize::Length(DefLength::Auto);
        assert_eq!(
            classify_track_type::<f32, i32>(&ts),
            IntrinsicTrackType::Auto
        );
    }

    #[test]
    fn classify_track_type_fr() {
        // §7.2: Flexible sizing function (fr unit)
        let ts: LayoutTrackSize<f32, i32> = LayoutTrackSize::Fr(1.0);
        assert_eq!(classify_track_type::<f32, i32>(&ts), IntrinsicTrackType::Fr);
    }

    #[test]
    fn classify_track_type_min_content() {
        // §7.2: min-content intrinsic sizing function
        let ts: LayoutTrackSize<f32, i32> = LayoutTrackSize::MinContent;
        assert_eq!(
            classify_track_type::<f32, i32>(&ts),
            IntrinsicTrackType::MinContent
        );
    }

    #[test]
    fn classify_track_type_max_content() {
        // §7.2: max-content intrinsic sizing function
        let ts: LayoutTrackSize<f32, i32> = LayoutTrackSize::MaxContent;
        assert_eq!(
            classify_track_type::<f32, i32>(&ts),
            IntrinsicTrackType::MaxContent
        );
    }
}