about summary refs log tree commit diff stats
path: root/src/html/dom.nim
diff options
context:
space:
mode:
Diffstat (limited to 'src/html/dom.nim')
-rw-r--r--src/html/dom.nim41
1 files changed, 34 insertions, 7 deletions
diff --git a/src/html/dom.nim b/src/html/dom.nim
index e5aee036..96bbd8a1 100644
--- a/src/html/dom.nim
+++ b/src/html/dom.nim
@@ -11,10 +11,12 @@ import algorithm
 import ../css/style
 import ../css/cssparser
 import ../css/selector
+import ../css/box
 
 import ../types/enums
 
 import ../utils/twtstr
+import ../utils/eprint
 
 const css = staticRead"../../res/default.css"
 let stylesheet = parseCSS(newStringStream(css))
@@ -44,7 +46,6 @@ type
     width*: int
     height*: int
     hidden*: bool
-    box*: CSSBox
 
   Attr* = ref AttrObj
   AttrObj = object of NodeObj
@@ -91,6 +92,7 @@ type
     attributes*: Table[string, Attr]
     style*: CSS2Properties
     cssvalues*: seq[CSSComputedValue]
+    box*: CSSBox
 
   HTMLElement* = ref HTMLElementObj
   HTMLElementObj = object of ElementObj
@@ -477,13 +479,38 @@ proc applyRules*(document: Document, rules: CSSStylesheet): seq[tuple[e:Element,
     for child in elem.children:
       stack.add(child)
 
-proc generateBox*(elem: Element) =
-  if elem.style.display == DISPLAY_INLINE:
-    discard
-
+proc generateBox*(elem: Element, x: int, y: int, w: int, h: int) =
+  let display = elem.cssvalues.getValue(RULE_DISPLAY)
+  if display.t == RULE_DISPLAY:
+    if display.display == DISPLAY_NONE:
+      return
+  var box = new(CSSBlockBox)
+  box.innerEdge.x1 = x
+  box.innerEdge.y1 = y
+
+  var lx = x
+  var rx = x
+  var ry = y
   for child in elem.childNodes:
     if child.nodeType == ELEMENT_NODE:
-      Element(child).generateBox()
+      let elem = Element(child)
+      elem.generateBox(rx, ry, w, h)
+      box.innerEdge.x2 += elem.box.size().w
+      box.innerEdge.y2 += elem.box.size().h
+      rx = x
+      lx = x
+      ry += elem.box.size().h
+      box.children.add(elem.box)
+    elif child.nodeType == TEXT_NODE:
+      let text = Text(child)
+      let runes = text.data.toRunes()
+      let boxes = boxesForText(runes, w, h, lx, rx, ry)
+      for child in boxes:
+        box.children.add(child)
+        box.innerEdge.y2 += child.size().h
+        ry += child.size().h
+      lx = boxes[^1].innerEdge.x2
+  elem.box = box
 
 proc applyDefaultStylesheet*(document: Document) =
   let important = document.applyRules(stylesheet)
@@ -491,4 +518,4 @@ proc applyDefaultStylesheet*(document: Document) =
     rule.e.style.applyProperty(rule.d)
     rule.e.cssvalues.add(getComputedValue(rule.d))
 
-  document.root.generateBox()
+  document.root.generateBox(0, 0, 80, 24)