From 35278f391719cccfd94837874460410a8bc82dfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bel=C3=A9n=20Albeza?= Date: Fri, 13 Nov 2026 09:39:43 +0100 Subject: [PATCH 1/3] :bug: Restore the WASM stack pointer after a failed call (#12978) A failed render-wasm call traps without popping its shadow-stack frames. Each failure leaked stack until it overwrote static data, and from then on every call trapped as wasm-critical. h/call now saves the stack pointer before the call and restores it on failure. Closes #12977 Related to #12906 AI-assisted-by: claude-opus-5-5 --- .../src/app/common/render_wasm/helpers.cljc | 43 ++++++++----- .../frontend_tests/render_wasm/call_test.cljs | 62 +++++++++++++++++++ frontend/test/frontend_tests/runner.cljs | 2 + render-wasm/_build_env | 2 +- 4 files changed, 91 insertions(+), 18 deletions(-) create mode 100644 frontend/test/frontend_tests/render_wasm/call_test.cljs diff --git a/common/src/app/common/render_wasm/helpers.cljc b/common/src/app/common/render_wasm/helpers.cljc index 3657ccb026..64ee812a7a 100644 --- a/common/src/app/common/render_wasm/helpers.cljc +++ b/common/src/app/common/render_wasm/helpers.cljc @@ -5,31 +5,40 @@ ;; Copyright (c) KALEIDOS SUBSIDIARY SL (ns app.common.render-wasm.helpers - #?(:cljs (:require-macros [app.common.render-wasm.helpers])) - (:require [app.common.data :as d])) + #?(:cljs (:require-macros [app.common.render-wasm.helpers]))) (def error-code "WASM error code constants (must match render-wasm/src/error.rs and mem.rs)." {0x01 :non-blocking 0x02 :panic}) +#?(:cljs + (defn on-call-error + "Restores the stack pointer after a failed WASM call and throws the + classified error." + [module fn-name stack cause] + (when (some? stack) + ((unchecked-get module "stackRestore") stack)) + (let [read-code-fn (unchecked-get module "_read_error_code") + code-num (when read-code-fn (read-code-fn)) + code (get error-code code-num :wasm-critical) + hint (str "WASM Error (" (name code) ")")] + (throw (ex-info hint {:type :wasm-error :code code :hint hint :fn fn-name} cause))))) + (defmacro call "Calls the WASM function `name`. On failure, throws a `:wasm-error` whose - `:code` comes from `error-code`, or `:wasm-critical` when WASM set none." + `:code` comes from `error-code`, or `:wasm-critical` when WASM set none. + Restores the stack pointer on failure, since a trap leaves its shadow-stack + frames behind." [module name & params] - (let [fn-sym (with-meta (gensym "fn-") {:tag 'function}) - cause-sym (gensym "cause")] - `(let [~fn-sym (cljs.core/unchecked-get ~module ~name)] + (let [fn-sym (with-meta (gensym "fn-") {:tag 'function}) + module-sym (gensym "module") + stack-sym (gensym "stack")] + `(let [~module-sym ~module + ~fn-sym (cljs.core/unchecked-get ~module-sym ~name) + save-fn# (cljs.core/unchecked-get ~module-sym "stackSave") + ~stack-sym (when save-fn# (save-fn#))] (try (~fn-sym ~@params) - (catch :default ~cause-sym - (let [read-code-fn# (cljs.core/unchecked-get ~module "_read_error_code") - code-num# (when read-code-fn# (read-code-fn#)) - code# (get error-code code-num# :wasm-critical) - hint# (str "WASM Error (" (d/name code#) ")") - context# {:type :wasm-error - :code code# - :hint hint# - :fn ~name} - cause# (ex-info hint# context# ~cause-sym)] - (throw cause#))))))) + (catch :default cause# + (on-call-error ~module-sym ~name ~stack-sym cause#)))))) diff --git a/frontend/test/frontend_tests/render_wasm/call_test.cljs b/frontend/test/frontend_tests/render_wasm/call_test.cljs new file mode 100644 index 0000000000..17d4331519 --- /dev/null +++ b/frontend/test/frontend_tests/render_wasm/call_test.cljs @@ -0,0 +1,62 @@ +;; This Source Code Form is subject to the terms of the Mozilla Public +;; License, v. 2.0. If a copy of the MPL was not distributed with this +;; file, You can obtain one at http://mozilla.org/MPL/2.0/. +;; +;; Copyright (c) KALEIDOS SUBSIDIARY SL + +(ns frontend-tests.render-wasm.call-test + "Unit tests for the `app.common.render-wasm.helpers/call` macro." + (:require + [app.common.render-wasm.helpers :as h] + [cljs.test :as t :include-macros true])) + +(defn- fake-module + "Module whose `_fail` export throws like a WASM trap after moving the + shadow stack pointer down 64 bytes, leaking the frame as a trap does." + [error-code] + (let [sp (atom 1000) + restored (atom [])] + {:sp sp + :restored restored + :module #js {"stackSave" (fn [] @sp) + "stackRestore" (fn [v] + (swap! restored conj v) + (reset! sp v)) + "_read_error_code" (fn [] error-code) + "_ok" (fn [x] (* 2 x)) + "_fail" (fn [] + (swap! sp - 64) + (throw (js/Error. "unreachable")))}})) + +(t/deftest failed-call-restores-stack-pointer + (let [{:keys [module sp restored]} (fake-module 0x02)] + (dotimes [_ 3] + (t/is (thrown? js/Error (h/call module "_fail")))) + (t/is (= 1000 @sp)) + (t/is (= [1000 1000 1000] @restored)))) + +(t/deftest failed-call-keeps-error-classification + (let [{:keys [module]} (fake-module 0x01) + data (try + (h/call module "_fail") + nil + (catch :default cause + (ex-data cause)))] + (t/is (= :wasm-error (:type data))) + (t/is (= :non-blocking (:code data))) + (t/is (= "_fail" (:fn data))))) + +(t/deftest successful-call-does-not-restore + (let [{:keys [module restored]} (fake-module 0)] + (t/is (= 42 (h/call module "_ok" 21))) + (t/is (empty? @restored)))) + +(t/deftest module-without-stack-methods-still-reports-errors + (let [module #js {"_read_error_code" (fn [] 0x02) + "_fail" (fn [] (throw (js/Error. "unreachable")))} + data (try + (h/call module "_fail") + nil + (catch :default cause + (ex-data cause)))] + (t/is (= :panic (:code data))))) diff --git a/frontend/test/frontend_tests/runner.cljs b/frontend/test/frontend_tests/runner.cljs index 465fa50050..82ba79e6b1 100644 --- a/frontend/test/frontend_tests/runner.cljs +++ b/frontend/test/frontend_tests/runner.cljs @@ -117,6 +117,7 @@ [frontend-tests.plugins.value-objects-test] [frontend-tests.plugins.wait-for-save-test] [frontend-tests.render-dimensions-test] + [frontend-tests.render-wasm.call-test] [frontend-tests.render-wasm.fonts-test] [frontend-tests.render-wasm.process-objects-test] [frontend-tests.render-wasm.serialization-test] @@ -331,6 +332,7 @@ 'frontend-tests.plugins.utils-test 'frontend-tests.plugins.value-objects-test 'frontend-tests.plugins.wait-for-save-test + 'frontend-tests.render-wasm.call-test 'frontend-tests.render-wasm.fonts-test 'frontend-tests.render-wasm.process-objects-test 'frontend-tests.render-wasm.serialization-test diff --git a/render-wasm/_build_env b/render-wasm/_build_env index c8d1fdd1e5..6dcd24b860 100644 --- a/render-wasm/_build_env +++ b/render-wasm/_build_env @@ -67,7 +67,7 @@ export EMCC_CFLAGS="--no-entry \ -sERROR_ON_UNDEFINED_SYMBOLS=0 \ -sMAX_WEBGL_VERSION=2 \ -sEXPORT_NAME=createRustSkiaModule \ - -sEXPORTED_RUNTIME_METHODS=GL,UTF8ToString,stringToUTF8,HEAPU8,HEAP32,HEAPU32,HEAPF32 \ + -sEXPORTED_RUNTIME_METHODS=GL,UTF8ToString,stringToUTF8,HEAPU8,HEAP32,HEAPU32,HEAPF32,stackSave,stackRestore \ -sENVIRONMENT=web,worker,node \ -sMODULARIZE=1 \ -sFILESYSTEM=0 \ From f807e749dbf986bfcbbc5e590ab3fe73f7f0a76a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bel=C3=A9n=20Albeza?= Date: Wed, 18 Nov 2026 12:29:38 +0100 Subject: [PATCH 2/3] :bug: Fix render-wasm panic when an auto layout has only absolute children An auto flex or grid layout whose children were all absolute shrank to zero, and a rotated shape below it got a matrix that cannot be inverted, which panicked in `_propagate_modifiers`. Skip auto-size when there is no content, as CLJS does. A matrix that still cannot be inverted is now a recoverable error, which shows a toast instead of the error page. Closes #12763 AI-assisted-by: claude-opus-5-5 --- render-wasm/src/shapes/modifiers.rs | 224 ++++++++++++++++++ render-wasm/src/shapes/modifiers/common.rs | 10 + .../src/shapes/modifiers/constraints.rs | 2 +- .../src/shapes/modifiers/flex_layout.rs | 10 +- .../src/shapes/modifiers/grid_layout.rs | 10 +- 5 files changed, 249 insertions(+), 7 deletions(-) diff --git a/render-wasm/src/shapes/modifiers.rs b/render-wasm/src/shapes/modifiers.rs index b79d811459..c46cce21a9 100644 --- a/render-wasm/src/shapes/modifiers.rs +++ b/render-wasm/src/shapes/modifiers.rs @@ -1039,4 +1039,228 @@ mod tests { assert!(is_close_to(bounds.min_x(), 0.0)); } + + #[derive(Clone, Copy)] + enum TestLayout { + None, + Flex, + Grid, + } + + #[derive(Clone, Copy, PartialEq)] + enum Child { + InFlow, + Absolute, + Hidden, + } + + /// Adds a shape with no gaps or padding: auto-sized at the root, fixed below. + fn add_shape( + state: &mut State, + parent: Option, + (left, top, right, bottom): (f32, f32, f32, f32), + layout: TestLayout, + ) -> Uuid { + let id = Uuid::new_v4(); + let sizing = if parent.is_some() { + Sizing::Fix + } else { + Sizing::Auto + }; + let shape = state.shapes.add_shape(id); + if !matches!(layout, TestLayout::None) { + shape.set_shape_type(Type::Frame(Frame::default())); + } + let (align, justify) = (AlignItems::Start, JustifyContent::Start); + let (content, items) = (AlignContent::Start, JustifyItems::Start); + match layout { + TestLayout::Flex => shape.set_flex_layout_data( + FlexDirection::Row, + 0.0, + 0.0, + align, + content, + items, + justify, + WrapType::NoWrap, + 0.0, + 0.0, + 0.0, + 0.0, + ), + TestLayout::Grid => shape.set_grid_layout_data( + GridDirection::Row, + 0.0, + 0.0, + align, + content, + items, + justify, + 0.0, + 0.0, + 0.0, + 0.0, + ), + TestLayout::None => {} + } + shape.set_selrect(left, top, right, bottom); + shape.set_flex_layout_child_data( + 0.0, 0.0, 0.0, 0.0, sizing, sizing, None, None, None, None, None, false, None, + ); + if let Some(parent) = parent { + shape.set_parent(parent); + state.shapes.get_mut(&parent).unwrap().add_child(id); + } + id + } + + fn shape_mut(state: &mut State, id: Uuid) -> &mut Shape { + state.shapes.get_mut(&id).unwrap() + } + + fn set_absolute(state: &mut State, id: Uuid) { + shape_mut(state, id) + .layout_item + .as_mut() + .unwrap() + .is_absolute = true; + } + + fn rotate(state: &mut State, id: Uuid, degrees: f32) { + let (sin, cos) = degrees.to_radians().sin_cos(); + let shape = shape_mut(state, id); + shape.set_rotation(degrees); + shape.set_transform(cos, sin, -sin, cos, 0.0, 0.0); + } + + /// Reflows every layout above `id`, as a committed text resize does. + fn reflow(state: &State, id: Uuid) -> Vec { + let entry = TransformEntry::from_input(id, Matrix::default()); + propagate_modifiers(state, &[entry], PixelPrecision::Disabled).unwrap() + } + + fn size_after(state: &State, result: &[TransformEntry], id: Uuid) -> (f32, f32) { + let mut bounds = state.shapes.get_raw(&id).unwrap().bounds(); + if let Some(entry) = result.iter().find(|e| e.id == id) { + bounds = bounds.transform(&entry.transform); + } + (bounds.width(), bounds.height()) + } + + fn assert_size((width, height): (f32, f32), (expected_w, expected_h): (f32, f32)) { + assert!( + is_close_to(width, expected_w) && is_close_to(height, expected_h), + "got {width}x{height}, expected {expected_w}x{expected_h}" + ); + } + + #[test] + fn test_rotated_shape_under_absolute_child_of_auto_flex_does_not_collapse() { + // Tree from #12763: auto flex > absolute frame > rotated flex row > leaf. + for absolute_layout in [TestLayout::Flex, TestLayout::Grid] { + let mut state = State::new(); + state.shapes.initialize(10); + let root = (-1545.0, -72.0, 232.0, 824.0); + let auto = add_shape(&mut state, None, root, TestLayout::Flex); + let frame = (-1399.0, 9.0, 72.75, 763.0); + let absolute = add_shape(&mut state, Some(auto), frame, absolute_layout); + set_absolute(&mut state, absolute); + let row = (-1379.0, 29.0, -1043.0, 83.0); + let row = add_shape(&mut state, Some(absolute), row, TestLayout::Flex); + let leaf = (-1365.0, 47.0, -1335.0, 64.0); + let leaf = add_shape(&mut state, Some(row), leaf, TestLayout::None); + rotate(&mut state, row, 0.0915); + rotate(&mut state, leaf, 0.0915); + + let result = reflow(&state, leaf); + + for entry in &result { + let (width, height) = size_after(&state, &result, entry.id); + assert!(width > 1.0 && height > 1.0, "{} collapsed", entry.id); + } + } + } + + #[test] + fn test_collapsing_a_rotated_frame_is_a_recoverable_error() { + let mut state = State::new(); + state.shapes.initialize(10); + let frame = (0.0, 0.0, 300.0, 100.0); + let frame = add_shape(&mut state, None, frame, TestLayout::Flex); + let child = add_shape( + &mut state, + Some(frame), + (10.0, 10.0, 50.0, 50.0), + TestLayout::None, + ); + set_absolute(&mut state, child); + rotate(&mut state, frame, 0.0915); + + // Squash the frame to zero width: its rotated corners end on one line. + let entry = TransformEntry::from_input(frame, Matrix::scale((0.0, 1.0))); + let result = propagate_modifiers(&state, &[entry], PixelPrecision::Disabled); + + assert!(matches!( + result, + Err(crate::error::Error::RecoverableError(_)) + )); + } + + /// Size of an auto-size 500x300 frame with one 100x50 child, after reflow. + /// Grids get two fixed 100px columns and one fixed 50px row. + fn auto_frame_size(layout: TestLayout, child: Child) -> (f32, f32) { + let mut state = State::new(); + state.shapes.initialize(10); + let frame = add_shape(&mut state, None, (0.0, 0.0, 500.0, 300.0), layout); + let rect = (10.0, 10.0, 110.0, 60.0); + let child_id = add_shape(&mut state, Some(frame), rect, TestLayout::None); + match child { + Child::Absolute => set_absolute(&mut state, child_id), + Child::Hidden => shape_mut(&mut state, child_id).set_hidden(true), + Child::InFlow => {} + } + if matches!(layout, TestLayout::Grid) { + let column = GridTrack { + track_type: GridTrackType::Fixed, + value: 100.0, + }; + let row = GridTrack { + track_type: GridTrackType::Fixed, + value: 50.0, + }; + let shape = shape_mut(&mut state, frame); + shape.set_grid_columns(vec![column.clone(), column]); + shape.set_grid_rows(vec![row]); + if child != Child::Absolute { + shape.set_grid_cells(vec![GridCell { + row: 1, + row_span: 1, + column: 1, + column_span: 1, + align_self: None, + justify_self: None, + shape: Some(child_id), + }]); + } + } + let result = reflow(&state, frame); + size_after(&state, &result, frame) + } + + #[test] + fn test_auto_flex_size_by_child_kind() { + use Child::*; + assert_size(auto_frame_size(TestLayout::Flex, InFlow), (100.0, 50.0)); + assert_size(auto_frame_size(TestLayout::Flex, Absolute), (500.0, 300.0)); + // Known difference from CLJS, which sizes the frame to hidden children. + assert_size(auto_frame_size(TestLayout::Flex, Hidden), (500.0, 300.0)); + } + + #[test] + fn test_auto_grid_size_by_child_kind() { + use Child::*; + assert_size(auto_frame_size(TestLayout::Grid, InFlow), (200.0, 50.0)); + assert_size(auto_frame_size(TestLayout::Grid, Absolute), (500.0, 300.0)); + assert_size(auto_frame_size(TestLayout::Grid, Hidden), (500.0, 300.0)); + } } diff --git a/render-wasm/src/shapes/modifiers/common.rs b/render-wasm/src/shapes/modifiers/common.rs index 475e8812f7..b622b94672 100644 --- a/render-wasm/src/shapes/modifiers/common.rs +++ b/render-wasm/src/shapes/modifiers/common.rs @@ -2,6 +2,7 @@ use std::collections::HashMap; use crate::math::Bounds; use crate::shapes::Shape; +use crate::state::ShapesPoolRef; use crate::uuid::Uuid; pub trait GetBounds { @@ -13,3 +14,12 @@ impl GetBounds for HashMap { self.get(&shape.id).copied().unwrap_or(shape.bounds()) } } + +/// Whether a layout has a child that takes part in the flow (neither absolute +/// nor hidden). Without one there is no content to auto-size the layout to. +pub fn has_in_flow_children(shape: &Shape, shapes: ShapesPoolRef) -> bool { + shape + .children_ids_iter(true) + .filter_map(|id| shapes.get(id)) + .any(|child| !child.is_absolute() && !child.hidden()) +} diff --git a/render-wasm/src/shapes/modifiers/constraints.rs b/render-wasm/src/shapes/modifiers/constraints.rs index 4f5a9cb228..6b625a0029 100644 --- a/render-wasm/src/shapes/modifiers/constraints.rs +++ b/render-wasm/src/shapes/modifiers/constraints.rs @@ -134,7 +134,7 @@ pub fn propagate_shape_constraints( parent_transform.post_translate(center); parent_transform.pre_translate(-center); - let parent_transform_inv = &parent_transform.invert().ok_or(Error::CriticalError( + let parent_transform_inv = &parent_transform.invert().ok_or(Error::RecoverableError( "Failed to invert parent transform".to_string(), ))?; let origin = parent_transform_inv.map_point(child_bounds_after.nw); diff --git a/render-wasm/src/shapes/modifiers/flex_layout.rs b/render-wasm/src/shapes/modifiers/flex_layout.rs index 5bdd64e488..af0862e785 100644 --- a/render-wasm/src/shapes/modifiers/flex_layout.rs +++ b/render-wasm/src/shapes/modifiers/flex_layout.rs @@ -9,7 +9,7 @@ use crate::uuid::Uuid; use std::collections::{HashMap, VecDeque}; -use super::common::GetBounds; +use super::common::{has_in_flow_children, GetBounds}; const MIN_SIZE: f32 = 0.01; const MAX_SIZE: f32 = f32::INFINITY; @@ -686,7 +686,11 @@ pub fn reflow_flex_layout( } } - if layout_axis.is_auto_across || layout_axis.is_auto_main { + // Like the CLJS layout, keep the size when there is no content to fit: + // auto-sizing to padding only would collapse the frame. + if (layout_axis.is_auto_across || layout_axis.is_auto_main) + && has_in_flow_children(shape, shapes) + { let width = layout_bounds.width(); let height = layout_bounds.height(); @@ -749,7 +753,7 @@ pub fn reflow_flex_layout( let parent_transform = layout_bounds.transform_matrix().unwrap_or_default(); - let parent_transform_inv = &parent_transform.invert().ok_or(Error::CriticalError( + let parent_transform_inv = &parent_transform.invert().ok_or(Error::RecoverableError( "Failed to invert parent transform".to_string(), ))?; let origin = parent_transform_inv.map_point(layout_bounds.nw); diff --git a/render-wasm/src/shapes/modifiers/grid_layout.rs b/render-wasm/src/shapes/modifiers/grid_layout.rs index e28c6dbb46..62d8604e24 100644 --- a/render-wasm/src/shapes/modifiers/grid_layout.rs +++ b/render-wasm/src/shapes/modifiers/grid_layout.rs @@ -10,7 +10,7 @@ use crate::uuid::Uuid; use std::collections::{HashMap, HashSet, VecDeque}; -use super::common::GetBounds; +use super::common::{has_in_flow_children, GetBounds}; const MIN_SIZE: f32 = 0.01; const MAX_SIZE: f32 = f32::INFINITY; @@ -890,7 +890,11 @@ pub fn reflow_grid_layout( } } - if shape.is_layout_horizontal_auto() || shape.is_layout_vertical_auto() { + // Like the CLJS layout, keep the size when there is no content to fit: + // auto-sizing to empty tracks would collapse the frame. + if (shape.is_layout_horizontal_auto() || shape.is_layout_vertical_auto()) + && has_in_flow_children(shape, shapes) + { let width = layout_bounds.width(); let height = layout_bounds.height(); @@ -915,7 +919,7 @@ pub fn reflow_grid_layout( let parent_transform = layout_bounds.transform_matrix().unwrap_or_default(); - let parent_transform_inv = &parent_transform.invert().ok_or(Error::CriticalError( + let parent_transform_inv = &parent_transform.invert().ok_or(Error::RecoverableError( "Failed to invert parent transform".to_string(), ))?; let origin = parent_transform_inv.map_point(layout_bounds.nw); From 76adaf86b13b0b3f32d88a01e0d913f4aee8036b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bel=C3=A9n=20Albeza?= Date: Wed, 18 Nov 2026 12:55:02 +0100 Subject: [PATCH 3/3] :bug: Count hidden children in render-wasm grid auto-size A grid takes its size from its tracks, so, like CLJS, it skips auto-size only when every child is absolute. Flex still skips hidden children, since its line building leaves them out. Refs #12763 AI-assisted-by: claude-opus-5-5 --- render-wasm/src/shapes.rs | 5 +++++ render-wasm/src/shapes/modifiers.rs | 3 ++- render-wasm/src/shapes/modifiers/common.rs | 14 +++++++++++--- render-wasm/src/shapes/modifiers/flex_layout.rs | 6 +++--- render-wasm/src/shapes/modifiers/grid_layout.rs | 8 ++++---- 5 files changed, 25 insertions(+), 11 deletions(-) diff --git a/render-wasm/src/shapes.rs b/render-wasm/src/shapes.rs index c8d218dd9b..97703096ef 100644 --- a/render-wasm/src/shapes.rs +++ b/render-wasm/src/shapes.rs @@ -1785,6 +1785,11 @@ impl Shape { } } + /// Whether flex layout places this shape: neither absolute nor hidden. + pub fn is_in_flow(&self) -> bool { + !self.is_absolute() && !self.hidden() + } + pub fn z_index(&self) -> i32 { match &self.layout_item { Some(LayoutItem { diff --git a/render-wasm/src/shapes/modifiers.rs b/render-wasm/src/shapes/modifiers.rs index c46cce21a9..52e8c488cb 100644 --- a/render-wasm/src/shapes/modifiers.rs +++ b/render-wasm/src/shapes/modifiers.rs @@ -1261,6 +1261,7 @@ mod tests { use Child::*; assert_size(auto_frame_size(TestLayout::Grid, InFlow), (200.0, 50.0)); assert_size(auto_frame_size(TestLayout::Grid, Absolute), (500.0, 300.0)); - assert_size(auto_frame_size(TestLayout::Grid, Hidden), (500.0, 300.0)); + // Grid takes its size from its tracks; hidden children still count. + assert_size(auto_frame_size(TestLayout::Grid, Hidden), (200.0, 50.0)); } } diff --git a/render-wasm/src/shapes/modifiers/common.rs b/render-wasm/src/shapes/modifiers/common.rs index b622b94672..fa12ec5e20 100644 --- a/render-wasm/src/shapes/modifiers/common.rs +++ b/render-wasm/src/shapes/modifiers/common.rs @@ -15,11 +15,19 @@ impl GetBounds for HashMap { } } -/// Whether a layout has a child that takes part in the flow (neither absolute -/// nor hidden). Without one there is no content to auto-size the layout to. +/// Whether a flex layout has a child it places (see `Shape::is_in_flow`). pub fn has_in_flow_children(shape: &Shape, shapes: ShapesPoolRef) -> bool { shape .children_ids_iter(true) .filter_map(|id| shapes.get(id)) - .any(|child| !child.is_absolute() && !child.hidden()) + .any(Shape::is_in_flow) +} + +/// Whether a layout has a child that is not absolute. Hidden children count, +/// as in the CLJS layout. +pub fn has_non_absolute_children(shape: &Shape, shapes: ShapesPoolRef) -> bool { + shape + .children_ids_iter(true) + .filter_map(|id| shapes.get(id)) + .any(|child| !child.is_absolute()) } diff --git a/render-wasm/src/shapes/modifiers/flex_layout.rs b/render-wasm/src/shapes/modifiers/flex_layout.rs index af0862e785..6209a5c4c6 100644 --- a/render-wasm/src/shapes/modifiers/flex_layout.rs +++ b/render-wasm/src/shapes/modifiers/flex_layout.rs @@ -202,7 +202,7 @@ fn initialize_tracks( continue; }; - if child.is_absolute() || child.hidden() { + if !child.is_in_flow() { continue; } @@ -686,8 +686,8 @@ pub fn reflow_flex_layout( } } - // Like the CLJS layout, keep the size when there is no content to fit: - // auto-sizing to padding only would collapse the frame. + // With nothing to place, keep the size instead of collapsing to padding. + // CLJS would also size to hidden children; flex here leaves them out. if (layout_axis.is_auto_across || layout_axis.is_auto_main) && has_in_flow_children(shape, shapes) { diff --git a/render-wasm/src/shapes/modifiers/grid_layout.rs b/render-wasm/src/shapes/modifiers/grid_layout.rs index 62d8604e24..24604f19bb 100644 --- a/render-wasm/src/shapes/modifiers/grid_layout.rs +++ b/render-wasm/src/shapes/modifiers/grid_layout.rs @@ -10,7 +10,7 @@ use crate::uuid::Uuid; use std::collections::{HashMap, HashSet, VecDeque}; -use super::common::{has_in_flow_children, GetBounds}; +use super::common::{has_non_absolute_children, GetBounds}; const MIN_SIZE: f32 = 0.01; const MAX_SIZE: f32 = f32::INFINITY; @@ -890,10 +890,10 @@ pub fn reflow_grid_layout( } } - // Like the CLJS layout, keep the size when there is no content to fit: - // auto-sizing to empty tracks would collapse the frame. + // Like the CLJS layout, skip auto-size when every child is absolute: the + // size comes from the tracks, and hidden children still count. if (shape.is_layout_horizontal_auto() || shape.is_layout_vertical_auto()) - && has_in_flow_children(shape, shapes) + && has_non_absolute_children(shape, shapes) { let width = layout_bounds.width(); let height = layout_bounds.height();