🐛 Ignore 0fr tracks when a grid hugs its content (#13256)

With auto width or height, the fr unit is the largest size / value of
the flex tracks. A 0fr track divided by zero, so the fr was infinite,
the track size became NaN and so did the position of every child. The
backend rejected the shape and the save. Skip tracks with no fr value
when computing that minimum, in common (SVG renderer) and in
render-wasm (WebGL renderer). A 0fr track keeps the size of its
content, as in CSS.

Closes #13255

AI-assisted-by: claude-opus-5-5
This commit is contained in:
Eva Marco 2026-11-26 12:03:20 +01:00 committed by GitHub
parent bb79d2f207
commit 09b53a4932
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 80 additions and 2 deletions

View File

@ -392,7 +392,10 @@
(if (empty? tracks)
min-fr
(let [{:keys [size type value]} (first tracks)
min-fr (if (= type :flex) (max min-fr (/ size value)) min-fr)]
;; A 0fr track takes no free space, so it sets no minimum
min-fr (if (and (= type :flex) (pos? value))
(max min-fr (/ size value))
min-fr)]
(recur (rest tracks) (double min-fr))))))
(defn calc-layout-data

View File

@ -433,3 +433,27 @@
(t/deftest grid-calculate-params-clamps-overflowing-child-padding
(let [params (grid-params-for-children {:x -10 :y -5 :width 150 :height 150})]
(t/is (= {:p1 0 :p2 0 :p3 0 :p4 0} (:layout-padding params)))))
;; ---------------------------------------------------------------------------
;; FR tracks with value 0 in a board that hugs its content
;; ---------------------------------------------------------------------------
;;
;; With auto width, the fr unit comes from `min-fr-value`, which divided the
;; size of each flex track by its value. A `0fr` track gave an infinite fr,
;; then a NaN track size and NaN positions for every child, so the backend
;; rejected the save (#12463).
(t/deftest min-fr-value-ignores-zero-value-flex-tracks
(let [tracks [{:type :flex :value 0.0 :size 24.0}
{:type :flex :value 1.0 :size 10.0}]]
(t/is (mth/close? 10.0 (gld/min-fr-value tracks)))))
(t/deftest grid-auto-width-with-zero-fr-column-keeps-finite-tracks
(let [frame (make-grid-frame :layout-grid-columns [{:type :flex :value 1.0}
{:type :flex :value 0.0}
{:type :flex :value 1.0}]
:layout-item-h-sizing :auto)
result (gld/calc-layout-data frame (bounds-for frame) [] {} {})]
(doseq [{:keys [size start-p]} (:column-tracks result)]
(t/is (mth/finite? size))
(t/is (mth/finite? (:x start-p))))))

View File

@ -341,7 +341,10 @@ fn set_fr_value(
for t in tracks.iter() {
if t.track_type == GridTrackType::Flex {
tot_frs += t.value;
cur_fr_value = f32::max(t.size / t.value, cur_fr_value);
// A 0fr track takes no free space, so it sets no minimum
if t.value > 0.0 {
cur_fr_value = f32::max(t.size / t.value, cur_fr_value);
}
} else {
tot_size_nofr += t.size;
}
@ -937,3 +940,51 @@ pub fn reflow_grid_layout(
Ok(result)
}
#[cfg(test)]
mod tests {
use super::*;
fn flex_track(value: f32, size: f32) -> TrackData {
TrackData {
track_type: GridTrackType::Flex,
value,
size,
max_size: MAX_SIZE,
anchor_start: Point::default(),
anchor_end: Point::default(),
}
}
fn layout_data() -> LayoutData {
LayoutData {
align_items: AlignItems::Start,
align_content: AlignContent::Start,
justify_items: JustifyItems::Start,
justify_content: JustifyContent::Start,
padding_top: 0.0,
padding_right: 0.0,
padding_bottom: 0.0,
padding_left: 0.0,
row_gap: 0.0,
column_gap: 0.0,
}
}
// A 0fr track in a board that hugs its content gave an infinite fr
// unit, then NaN track sizes and NaN positions for every child (#12463).
#[test]
fn auto_layout_with_zero_fr_track_keeps_finite_sizes() {
let mut tracks = vec![
flex_track(1.0, 24.0),
flex_track(0.0, 24.0),
flex_track(1.0, 24.0),
];
set_fr_value(true, &layout_data(), &mut tracks, MIN_SIZE, true);
assert!(tracks.iter().all(|t| t.size.is_finite()));
assert_eq!(tracks[0].size, 24.0);
assert_eq!(tracks[1].size, 24.0);
}
}