about summary refs log tree commit diff stats
path: root/src/layout
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2023-01-08 16:00:26 +0100
committerbptato <nincsnevem662@gmail.com>2023-01-08 16:19:17 +0100
commit4e40c70ebc19807a0bd121b2376ff147adae765c (patch)
treece6fcab0bc9c35d42cf69141725adcf2cd60c20f /src/layout
parent7bed16fa67df23f1bd1156e709a4b959b274b2ca (diff)
downloadchawan-4e40c70ebc19807a0bd121b2376ff147adae765c.tar.gz
css/cascade, layout/engine: add table align
Also fix margin-left/right: auto, to some extent.
Diffstat (limited to 'src/layout')
-rw-r--r--src/layout/engine.nim66
1 files changed, 32 insertions, 34 deletions
diff --git a/src/layout/engine.nim b/src/layout/engine.nim
index f116e440..47c293b6 100644
--- a/src/layout/engine.nim
+++ b/src/layout/engine.nim
@@ -8,7 +8,6 @@ import css/stylednode
 import css/values
 import io/window
 import layout/box
-import types/color
 import utils/twtstr
 
 # Build phase
@@ -810,28 +809,10 @@ proc positionBlocks(box: BlockBox) =
   box.height += box.padding_top
 
   x += box.padding_left
-  let needsMove = box.computed{"text-align"} in {TEXT_ALIGN_CHA_LEFT, TEXT_ALIGN_CHA_CENTER, TEXT_ALIGN_CHA_RIGHT}
 
   template apply_child(child: BlockBox) =
     child.offset.y = y
     child.offset.x = x
-    if not needsMove and spec and child.contentWidth < box.contentWidth:
-      let margin_left = child.computed{"margin-left"}
-      let margin_right = child.computed{"margin-right"}
-      if margin_left.auto and margin_right.auto:
-        child.margin_left += box.contentWidth div 2
-        child.margin_left -= child.contentWidth div 2
-        child.margin_right += box.contentWidth div 2
-        child.margin_right -= child.contentWidth div 2
-      elif margin_left.auto:
-        child.margin_left += box.contentWidth
-        child.margin_left -= child.contentWidth
-      elif margin_right.auto:
-        child.margin_right += box.contentWidth
-        child.margin_right -= child.contentWidth
-    child.offset.x += child.margin_left
-    if box.computed{"position"} == POSITION_RELATIVE:
-      box.positionRelative(child)
     y += child.height
     box.height += child.height
     if not spec:
@@ -887,25 +868,42 @@ proc positionBlocks(box: BlockBox) =
   margin_todo.append(box.margin_bottom)
   box.margin_bottom = margin_todo.sum()
 
-  if needsMove:
-    # Re-position the children.
-    # The x offset for values in shrink mode depends on the parent box's
-    # width, so we can not just do this in the first pass.
-    let width = if box.shrink:
-      min(box.width, box.contentWidth)
-    else:
-      max(box.width, box.contentWidth)
+  # Re-position the children.
+  # The x offset for values in shrink mode depends on the parent box's
+  # width, so we can not just do this in the first pass.
+  let width = if box.shrink:
+    min(box.width, box.contentWidth)
+  else:
+    max(box.width, box.contentWidth)
+  for child in box.nested:
     case box.computed{"text-align"}
     of TEXT_ALIGN_CHA_CENTER:
-      for child in box.nested:
-        child.offset.x += width div 2
-        child.offset.x -= child.width div 2
+      child.offset.x += width div 2
+      child.offset.x -= child.width div 2
     of TEXT_ALIGN_CHA_LEFT: discard
     of TEXT_ALIGN_CHA_RIGHT:
-      for child in box.nested:
-        child.offset.x += width
-        child.offset.x -= child.width
-    else: discard
+      child.offset.x += width
+      child.offset.x -= child.width
+    elif child.contentWidth < box.contentWidth:
+      let margin_left = child.computed{"margin-left"}
+      let margin_right = child.computed{"margin-right"}
+      if margin_left.auto and margin_right.auto:
+        child.margin_left += width div 2
+        child.margin_left -= child.width div 2
+        child.margin_right += width div 2
+        child.margin_right -= child.width div 2
+      elif margin_left.auto:
+        child.margin_left += width
+        child.margin_left -= child.width
+      elif margin_right.auto:
+        child.margin_right += width
+        child.margin_right -= child.width
+      if not spec:
+        let marginWidth = child.width + child.margin_left + child.margin_right
+        box.width = min(box.maxContentWidth, max(marginWidth, box.width))
+    child.offset.x += child.margin_left
+    if box.computed{"position"} == POSITION_RELATIVE:
+      box.positionRelative(child)
 
   box.height += box.padding_bottom