🐛 Handle empty flex layouts when calculating line positions

Skip per-line stretch division and baseline placement when a flex layout
has no lines. Removing a component's final child then resizing a copy
could divide by zero on the JVM (F169).

The public layout regression fails four assertions before the fix. It
covers row and column layouts with six content alignments. Fresh flex,
swap and variant suites pass 125 tests and 596 assertions, and the shrunk
structure program passes. Pure goldens match all 1149 assertions.

AI-assisted-by: GPT-6
This commit is contained in:
alonso.torres 2026-10-24 09:39:14 +02:00
parent 75190e560c
commit 099af03d7d
2 changed files with 14 additions and 3 deletions

View File

@ -224,12 +224,12 @@
;; When align-items is stretch we need to adjust the main axis size to grow for the full content
stretch-width-fix
(if (and col? (ctl/content-stretch? parent) (not auto-width?))
(if (and (pos? num-lines) col? (ctl/content-stretch? parent) (not auto-width?))
(/ (- layout-width (* layout-gap-col (dec num-lines)) total-max-width) num-lines)
0)
stretch-height-fix
(if (and row? (ctl/content-stretch? parent) (not auto-height?))
(if (and (pos? num-lines) row? (ctl/content-stretch? parent) (not auto-height?))
(/ (- layout-height (* layout-gap-row (dec num-lines)) total-max-height) num-lines)
0)
@ -300,7 +300,8 @@
[total-width total-height] (->> layout-lines (reduce add-lines [0 0]))
base-p (flp/get-base-line parent layout-bounds total-width total-height num-lines)]
base-p (when (pos? num-lines)
(flp/get-base-line parent layout-bounds total-width total-height num-lines))]
(->> layout-lines
(reduce (partial add-starts total-width total-height num-lines) [(transient []) base-p])

View File

@ -34,6 +34,16 @@
[rect]
(grc/rect->points rect))
(t/deftest empty-wrapped-layout-has-no-lines
(doseq [direction [:row :column]
alignment [:stretch :space-around :space-evenly :space-between :center :end]]
(let [frame (make-col-frame :layout-flex-dir direction
:layout-align-content alignment
:layout-padding {:p1 0 :p2 0 :p3 0 :p4 0}
:layout-padding-type :simple)
bounds (rect->bounds (grc/make-rect 0 0 200 200))]
(t/is (= [] (:layout-lines (fld/calc-layout-data frame bounds [] {} {})))))))
;; ---- get-base-line (around? branch) ----
;;
;; Bug: in positions.cljc the col? + around? branch had a mis-parenthesised