aboutsummaryrefslogtreecommitdiffstats
path: root/src/html
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2021-08-06 23:24:40 +0200
committerbptato <nincsnevem662@gmail.com>2021-08-06 23:24:40 +0200
commitab963e4efe0871a6bdedf2f56fcfb9ed15636d12 (patch)
tree78397ac1ca492e4be08af07d02a0d609896e8ecc /src/html
parent559d2ca622e15c5781303940bc0c87e9aed1e5d1 (diff)
downloadchawan-ab963e4efe0871a6bdedf2f56fcfb9ed15636d12.tar.gz
HTML display with highly broken box model
Diffstat (limited to 'src/html')
-rw-r--r--src/html/dom.nim41
-rw-r--r--src/html/htmlparser.nim2
2 files changed, 35 insertions, 8 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)
diff --git a/src/html/htmlparser.nim b/src/html/htmlparser.nim
index 2a652304..c060d666 100644
--- a/src/html/htmlparser.nim
+++ b/src/html/htmlparser.nim
@@ -378,7 +378,7 @@ proc processDocumentPart(state: var HTMLParseState, buf: string) =
#TODO for doctype
while p < max and buf[p] != '>':
inc p
- at = p
+ at = p + 1
continue
if not state.in_comment: