From 27fb1388b0644da1e733b07042e0cf30eea8d7b9 Mon Sep 17 00:00:00 2001 From: bptato Date: Sun, 6 Apr 2025 19:52:57 +0200 Subject: layout: fix cleared floats not flushing previous exclusions Well, I was trying to reproduce *another* float bug when I accidentally stumbled upon this... The fix is somewhat ugly, but not as bad as I expected. --- src/css/layout.nim | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/css/layout.nim b/src/css/layout.nim index ac64f6be..e2b04e6d 100644 --- a/src/css/layout.nim +++ b/src/css/layout.nim @@ -693,6 +693,8 @@ type # * flush margins and position floats # * check the relevant exclusions and resize the line appropriately init: LineInitState + # float values currently included in unpositionedFloats. + floatsSeen: set[CSSFloat] InlineAtomState = object vertalign: CSSVerticalAlign @@ -1570,6 +1572,13 @@ proc positionRelative(lctx: LayoutContext; space: AvailableSpace; elif box.computed{"bottom"}.canpx(space.h): box.state.offset.y -= box.computed{"bottom"}.px(space.h) +func clearedBy(floats: set[CSSFloat]; clear: CSSClear): bool = + return case clear + of ClearNone: false + of ClearBoth: floats != {} + of ClearInlineStart, ClearLeft: FloatLeft in floats + of ClearInlineEnd, ClearRight: FloatRight in floats + proc layoutFloat(fstate: var FlowState; child: BlockBox) = let lctx = fstate.lctx let sizes = lctx.resolveFloatSizes(fstate.space, child.computed) @@ -1589,17 +1598,19 @@ proc layoutFloat(fstate: var FlowState; child: BlockBox) = fstate.maxChildWidth = max(fstate.maxChildWidth, outerSize.w) fstate.initLine(flag = ilfFloat) var newLine = true - if fstate.lbstate.size.w + outerSize.w <= - fstate.lbstate.availableWidth and + let float = child.computed{"float"} + if not fstate.lbstate.floatsSeen.clearedBy(child.computed{"clear"}) and + fstate.lbstate.size.w + outerSize.w <= fstate.lbstate.availableWidth and (fstate.lbstate.unpositionedFloats.len == 0 or not fstate.lbstate.unpositionedFloats[^1].newLine): # We can still cram floats into the line. - if child.computed{"float"} == FloatLeft: + if float == FloatLeft: fstate.lbstate.size.w += outerSize.w for iastate in fstate.lbstate.iastates.mitems: iastate.offset.x += outerSize.w else: fstate.lbstate.availableWidth -= outerSize.w + fstate.lbstate.floatsSeen.incl(float) newLine = false fstate.lbstate.unpositionedFloats.add(UnpositionedFloat( space: fstate.space, -- cgit