about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/config/config.nim9
-rw-r--r--src/css/box.nim1
-rw-r--r--src/css/style.nim67
-rw-r--r--src/html/dom.nim21
-rw-r--r--src/io/buffer.nim9
-rw-r--r--src/main.nim4
-rw-r--r--src/types/enums.nim3
-rw-r--r--src/utils/eprint.nim11
8 files changed, 85 insertions, 40 deletions
diff --git a/src/config/config.nim b/src/config/config.nim
index 5a49bb34..dab5347f 100644
--- a/src/config/config.nim
+++ b/src/config/config.nim
@@ -21,7 +21,7 @@ type
     ACTION_SCROLL_DOWN, ACTION_SCROLL_UP, ACTION_SCROLL_LEFT, ACTION_SCROLL_RIGHT,
     ACTION_CLICK,
     ACTION_CHANGE_LOCATION,
-    ACTION_RELOAD, ACTION_RESHAPE, ACTION_REDRAW,
+    ACTION_RELOAD, ACTION_RESHAPE, ACTION_REDRAW, ACTION_TOGGLE_SOURCE,
     ACTION_CURSOR_FIRST_LINE, ACTION_CURSOR_LAST_LINE,
     ACTION_CURSOR_TOP, ACTION_CURSOR_MIDDLE, ACTION_CURSOR_BOTTOM,
     ACTION_CENTER_LINE, ACTION_LINE_INFO,
@@ -49,7 +49,6 @@ func getConfig(s: StaticConfig): Config =
 
 func getRealKey(key: string): string =
   var realk: string
-  var currchar: char
   var control = 0
   var meta = 0
   var skip = false
@@ -59,12 +58,10 @@ func getRealKey(key: string): string =
     elif skip:
       realk &= c
       skip = false
-    elif c == 'M':
+    elif c == 'M' and meta == 0:
       inc meta
-      currchar = c
-    elif c == 'C':
+    elif c == 'C' and control == 0:
       inc control
-      currchar = c
     elif c == '-' and control == 1:
       inc control
     elif c == '-' and meta == 1:
diff --git a/src/css/box.nim b/src/css/box.nim
index c2974215..3b74380a 100644
--- a/src/css/box.nim
+++ b/src/css/box.nim
@@ -1,6 +1,5 @@
 import unicode
 
-import types/enums
 import utils/twtstr
 
 type
diff --git a/src/css/style.nim b/src/css/style.nim
index c1820b12..dd44eacc 100644
--- a/src/css/style.nim
+++ b/src/css/style.nim
@@ -43,24 +43,39 @@ type
   CSSColor* = tuple[r: uint8, g: uint8, b: uint8, a: uint8]
   
   CSSComputedValue* = object of RootObj
-    case t*: CSSRuleType
-    of RULE_ALL: discard
-    of RULE_COLOR:
+    t*: CSSRuleType
+    case v*: CSSValueType
+    of VALUE_COLOR:
       color*: CSSColor
-    of RULE_MARGIN, RULE_MARGIN_TOP, RULE_MARGIN_LEFT, RULE_MARGIN_RIGHT,
-       RULE_MARGIN_BOTTOM:
+    of VALUE_LENGTH:
       length*: CSSLength
-    of RULE_FONT_STYLE:
-      fontStyle*: CSSFontStyle
-    of RULE_DISPLAY:
+    of VALUE_FONT_STYLE:
+      fontstyle*: CSSFontStyle
+    of VALUE_DISPLAY:
       display*: DisplayType
-    of RULE_CONTENT:
+    of VALUE_CONTENT:
       content*: seq[Rune]
+    of VALUE_NONE: discard
 
   CSSSpecifiedValue* = object of CSSComputedValue
     hasGlobalValue: bool
     globalValue: CSSGlobalValueType
 
+const ValueTypes = {
+  RULE_ALL: VALUE_NONE,
+  RULE_COLOR: VALUE_COLOR,
+  RULE_MARGIN: VALUE_LENGTH,
+  RULE_MARGIN_TOP: VALUE_LENGTH,
+  RULE_MARGIN_BOTTOM: VALUE_LENGTH,
+  RULE_MARGIN_LEFT: VALUE_LENGTH,
+  RULE_MARGIN_RIGHT: VALUE_LENGTH,
+  RULE_FONT_STYLE: VALUE_FONT_STYLE,
+  RULE_DISPLAY: VALUE_DISPLAY,
+  RULE_CONTENT: VALUE_CONTENT,
+}.toTable()
+
+func getValueType*(rule: CSSRuleType): CSSValueType =
+  return ValueTypes[rule]
 
 func cells(l: CSSLength): int =
   case l.unit
@@ -249,33 +264,41 @@ func cssFontStyle(d: CSSDeclaration): CSSFontStyle =
 func getSpecifiedValue*(d: CSSDeclaration): CSSSpecifiedValue =
   case $d.name
   of "color":
-    return CSSSpecifiedValue(t: RULE_COLOR, color: cssColor(d))
+    return CSSSpecifiedValue(t: RULE_COLOR, v: VALUE_COLOR, color: cssColor(d))
   of "margin":
-    return CSSSpecifiedValue(t: RULE_MARGIN, length: cssLength(d))
+    return CSSSpecifiedValue(t: RULE_MARGIN, v: VALUE_LENGTH, length: cssLength(d))
   of "margin-top":
-    return CSSSpecifiedValue(t: RULE_MARGIN_TOP, length: cssLength(d))
+    return CSSSpecifiedValue(t: RULE_MARGIN_TOP, v: VALUE_LENGTH, length: cssLength(d))
   of "margin-left":
-    return CSSSpecifiedValue(t: RULE_MARGIN_LEFT, length: cssLength(d))
+    return CSSSpecifiedValue(t: RULE_MARGIN_LEFT, v: VALUE_LENGTH, length: cssLength(d))
   of "margin-bottom":
-    return CSSSpecifiedValue(t: RULE_MARGIN_BOTTOM, length: cssLength(d))
+    return CSSSpecifiedValue(t: RULE_MARGIN_BOTTOM, v: VALUE_LENGTH, length: cssLength(d))
   of "margin-right":
-    return CSSSpecifiedValue(t: RULE_MARGIN_RIGHT, length: cssLength(d))
+    return CSSSpecifiedValue(t: RULE_MARGIN_RIGHT, v: VALUE_LENGTH, length: cssLength(d))
   of "font-style":
-    return CSSSpecifiedValue(t: RULE_FONT_STYLE, fontStyle: cssFontStyle(d))
+    return CSSSpecifiedValue(t: RULE_FONT_STYLE, v: VALUE_FONT_STYLE, fontStyle: cssFontStyle(d))
   of "display":
-    return CSSSpecifiedValue(t: RULE_DISPLAY, display: cssDisplay(d))
+    return CSSSpecifiedValue(t: RULE_DISPLAY, v: VALUE_DISPLAY, display: cssDisplay(d))
   of "content":
-    return CSSSpecifiedValue(t: RULE_CONTENT, content: cssString(d))
+    return CSSSpecifiedValue(t: RULE_CONTENT, v: VALUE_CONTENT, content: cssString(d))
 
 func getComputedValue*(rule: CSSSpecifiedValue): CSSComputedValue =
   let inherit = rule.hasGlobalValue and (rule.globalValue == VALUE_INHERIT)
   let initial = rule.hasGlobalValue and (rule.globalValue == VALUE_INHERIT)
   let unset = rule.hasGlobalValue and (rule.globalValue == VALUE_INHERIT)
   let revert = rule.hasGlobalValue and (rule.globalValue == VALUE_INHERIT)
-  return rule
-  #case rule.t
-  #of RULE_COLOR:
-  #  return CSSComputedValue(t: rule.t, 
+  case rule.v
+  of VALUE_COLOR:
+    return CSSComputedValue(t: rule.t, v: VALUE_COLOR, color: rule.color)
+  of VALUE_LENGTH:
+    return CSSComputedValue(t: rule.t, v: VALUE_LENGTH, length: rule.length)
+  of VALUE_DISPLAY:
+    return CSSComputedValue(t: rule.t, v: VALUE_DISPLAY, display: rule.display)
+  of VALUE_FONT_STYLE:
+    return CSSComputedValue(t: rule.t, v: VALUE_FONT_STYLE, fontstyle: rule.fontstyle)
+  of VALUE_CONTENT:
+    return CSSComputedValue(t: rule.t, v: VALUE_CONTENT, content: rule.content)
+  of VALUE_NONE: return CSSComputedValue(t: rule.t, v: VALUE_NONE)
 
 func getValue*(vals: seq[CSSComputedValue], rule: CSSRuleType): CSSComputedValue =
   for val in vals:
diff --git a/src/html/dom.nim b/src/html/dom.nim
index 2279725f..c7d3a650 100644
--- a/src/html/dom.nim
+++ b/src/html/dom.nim
@@ -468,10 +468,12 @@ proc applyRules*(document: Document, rules: CSSStylesheet): seq[tuple[e:Element,
       let decls = parseCSSListOfDeclarations(oblock.value)
       for item in decls:
         if item of CSSDeclaration:
-          if ((CSSDeclaration)item).important:
-            result.add((elem, CSSDeclaration(item)))
+          let decl = CSSDeclaration(item)
+          if decl.important:
+            result.add((elem, decl))
           else:
-            elem.style.applyProperty(CSSDeclaration(item))
+            elem.style.applyProperty(decl)
+            elem.cssvalues.add(getComputedValue(decl))
 
     for child in elem.children:
       stack.add(child)
@@ -492,12 +494,13 @@ proc generateBox*(elem: Element, x: int, y: int, w: int, h: int) =
     if child.nodeType == ELEMENT_NODE:
       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)
+      if elem.box != nil:
+        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()
diff --git a/src/io/buffer.nim b/src/io/buffer.nim
index f3fbbd38..a33c88ff 100644
--- a/src/io/buffer.nim
+++ b/src/io/buffer.nim
@@ -73,6 +73,8 @@ type
     displaycontrols*: bool
     redraw*: bool
     location*: Uri
+    source*: string #TODO
+    showsource*: bool
 
 func newBuffer*(attrs: TermAttributes): Buffer =
   new(result)
@@ -783,6 +785,13 @@ proc inputLoop(attrs: TermAttributes, buffer: Buffer): bool =
       reshape = true
       redraw = true
     of ACTION_REDRAW: redraw = true
+    of ACTION_TOGGLE_SOURCE:
+      buffer.showsource = not buffer.showsource
+      if buffer.showsource:
+        buffer.renderPlainText(buffer.source)
+      else:
+        buffer.renderDocument()
+      redraw = true
     else: discard
     stdout.hideCursor()
 
diff --git a/src/main.nim b/src/main.nim
index 6f940b50..4ce11f79 100644
--- a/src/main.nim
+++ b/src/main.nim
@@ -42,10 +42,10 @@ proc main*() =
   let buffer = newBuffer(attrs)
   let uri = parseUri(paramStr(1))
   buffers.add(buffer)
-  buffer.document = parseHtml(getPageUri(uri))
+  buffer.source = getPageUri(uri).readAll() #TODO buffer.renderPlainText(getPageUri(uri).readAll())
+  buffer.document = parseHtml(newStringStream(buffer.source))
   buffer.setLocation(uri)
   buffer.document.applyDefaultStylesheet()
-  #buffer.renderPlainText(getPageUri(uri).readAll())
   buffer.renderDocument()
   var lastUri = uri
   while displayPage(attrs, buffer):
diff --git a/src/types/enums.nim b/src/types/enums.nim
index eac71dd5..e257e579 100644
--- a/src/types/enums.nim
+++ b/src/types/enums.nim
@@ -78,6 +78,9 @@ type
   CSSGlobalValueType* = enum
     VALUE_INITIAL, VALUE_INHERIT, VALUE_REVERT, VALUE_UNSET
 
+  CSSValueType* = enum
+    VALUE_NONE, VALUE_LENGTH, VALUE_COLOR, VALUE_CONTENT, VALUE_DISPLAY, VALUE_FONT_STYLE
+
   DrawInstructionType* = enum
     DRAW_TEXT, DRAW_GOTO, DRAW_FGCOLOR, DRAW_BGCOLOR, DRAW_STYLE, DRAW_RESET
 
diff --git a/src/utils/eprint.nim b/src/utils/eprint.nim
index 128d34d9..d13fcf91 100644
--- a/src/utils/eprint.nim
+++ b/src/utils/eprint.nim
@@ -10,6 +10,17 @@ template eprint*(s: varargs[string, `$`]) = {.cast(noSideEffect).}:
     stderr.write(x)
   stderr.write('\n')
 
+template eecho*(s: varargs[string, `$`]) = {.cast(noSideEffect).}:
+  var a = false
+  var o = ""
+  for x in s:
+    if not a:
+      a = true
+    else:
+      o &= ' '
+    o &= x
+  echo o
+
 template print*(s: varargs[string, `$`]) =
   for x in s:
     stdout.write(x)