about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2025-02-08 20:17:35 +0100
committerbptato <nincsnevem662@gmail.com>2025-02-08 20:17:35 +0100
commit2c136265658450ea91cb81ee6b3a85198a0c4844 (patch)
tree71331c1fa14e71542391dc0a6501f329e5a6d39e /src
parent971e49f1eb26fd6a0c980c64f199b7356f489340 (diff)
downloadchawan-2c136265658450ea91cb81ee6b3a85198a0c4844.tar.gz
box: abstract over tree traversal
Diffstat (limited to 'src')
-rw-r--r--src/css/box.nim16
-rw-r--r--src/server/buffer.nim20
2 files changed, 20 insertions, 16 deletions
diff --git a/src/css/box.nim b/src/css/box.nim
index 7bcf0f3b..dca3ade2 100644
--- a/src/css/box.nim
+++ b/src/css/box.nim
@@ -108,6 +108,22 @@ type
     of ibtBox:
       box*: BlockBox
 
+iterator children*(box: CSSBox): lent CSSBox {.inline.} =
+  if box of BlockBox:
+    let box = BlockBox(box)
+    for child in box.children:
+      yield child
+  else:
+    let ibox = InlineBox(box)
+    case ibox.t
+    of ibtParent:
+      for child in ibox.children:
+        yield child
+    of ibtBox:
+      yield CSSBox(ibox.box)
+    else:
+      discard
+
 # We store runs in state as a private field, so that we can both check
 # if the box type is correct and reset them on relayout by zeroing out
 # state.
diff --git a/src/server/buffer.nim b/src/server/buffer.nim
index e34d88f6..dca85806 100644
--- a/src/server/buffer.nim
+++ b/src/server/buffer.nim
@@ -666,22 +666,10 @@ type GotoAnchorResult* = object
   focus*: ReadLineResult
 
 proc findAnchor(box: CSSBox; anchor: Element): Offset =
-  if box of InlineBox:
-    let ibox = InlineBox(box)
-    if ibox.t == ibtBox:
-      let off = ibox.box.findAnchor(anchor)
-      if off.y >= 0:
-        return off
-    elif ibox.t == ibtParent:
-      for child in ibox.children:
-        let off = child.findAnchor(anchor)
-        if off.y >= 0:
-          return off
-  else:
-    for child in BlockBox(box).children:
-      let off = child.findAnchor(anchor)
-      if off.y >= 0:
-        return off
+  for child in box.children:
+    let off = child.findAnchor(anchor)
+    if off.y >= 0:
+      return off
   if box.element == anchor:
     return box.render.offset
   return offset(-1, -1)