about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2021-12-10 16:07:54 +0100
committerbptato <nincsnevem662@gmail.com>2021-12-10 16:07:54 +0100
commit91dee7d0d7cfe0f7faa2bf4b364e1db87ec6b70f (patch)
tree3605bd1cf29e8a54f5075710e2119031ad15ef92
parent458f713fd4e506df0b916bb436c2d19fc6557e02 (diff)
downloadchawan-91dee7d0d7cfe0f7faa2bf4b364e1db87ec6b70f.tar.gz
Support inline style attributes
-rw-r--r--src/css/style.nim42
-rw-r--r--src/html/dom.nim10
-rw-r--r--src/html/parser.nim20
3 files changed, 35 insertions, 37 deletions
diff --git a/src/css/style.nim b/src/css/style.nim
index 7a453165..f64cdd45 100644
--- a/src/css/style.nim
+++ b/src/css/style.nim
@@ -27,14 +27,14 @@ func psuccess(s: SelectResult): bool =
 func attrSelectorMatches(elem: Element, sel: Selector): bool =
   case sel.rel
   of ' ': return sel.attr in elem.attributes
-  of '=': return elem.getAttrValue(sel.attr) == sel.value
-  of '~': return sel.value in unicode.split(elem.getAttrValue(sel.attr))
+  of '=': return elem.attr(sel.attr) == sel.value
+  of '~': return sel.value in unicode.split(elem.attr(sel.attr))
   of '|':
-    let val = elem.getAttrValue(sel.attr)
+    let val = elem.attr(sel.attr)
     return val == sel.value or sel.value.startsWith(val & '-')
-  of '^': return elem.getAttrValue(sel.attr).startsWith(sel.value)
-  of '$': return elem.getAttrValue(sel.attr).endsWith(sel.value)
-  of '*': return elem.getAttrValue(sel.attr).contains(sel.value)
+  of '^': return elem.attr(sel.attr).startsWith(sel.value)
+  of '$': return elem.attr(sel.attr).endsWith(sel.value)
+  of '*': return elem.attr(sel.attr).contains(sel.value)
   else: return false
 
 func pseudoSelectorMatches(elem: Element, sel: Selector): bool =
@@ -230,6 +230,15 @@ func calcRules(elem: Element, rules: ParsedStylesheet):
     tosorts[i].sort((x, y) => cmp(x.s,y.s))
     result[i] = tosorts[i].map((x) => x.b)
 
+proc applyItems*(ares: var ApplyResult, elem: Element, decls: seq[CSSParsedItem], pseudo: PseudoElem) =
+  for item in decls:
+    if item of CSSDeclaration:
+      let decl = CSSDeclaration(item)
+      if decl.important:
+        ares.important.add((elem, decl, pseudo))
+      else:
+        ares.normal.add((elem, decl, pseudo))
+
 proc applyRules*(document: Document, pss: ParsedStylesheet, reset: bool = false): ApplyResult =
   var stack: seq[Element]
 
@@ -246,13 +255,7 @@ proc applyRules*(document: Document, pss: ParsedStylesheet, reset: bool = false)
         let rules = rules_pseudo[pseudo]
         for rule in rules:
           let decls = parseCSSListOfDeclarations(rule.value)
-          for item in decls:
-            if item of CSSDeclaration:
-              let decl = CSSDeclaration(item)
-              if decl.important:
-                result.important.add((elem, decl, pseudo))
-              else:
-                result.normal.add((elem, decl, pseudo))
+          result.applyItems(elem, decls, pseudo)
 
     var i = elem.children.len - 1
     while i >= 0:
@@ -302,13 +305,12 @@ proc applyAuthorRules*(document: Document): ApplyResult =
         let rules = rules_pseudo[pseudo]
         for rule in rules:
           let decls = parseCSSListOfDeclarations(rule.value)
-          for item in decls:
-            if item of CSSDeclaration:
-              let decl = CSSDeclaration(item)
-              if decl.important:
-                result.important.add((elem, decl, pseudo))
-              else:
-                result.normal.add((elem, decl, pseudo))
+          result.applyItems(elem, decls, pseudo)
+
+      let style = elem.attr("style")
+      if style.len > 0:
+        let inline_rules = newStringStream(style).parseCSSListOfDeclarations()
+        result.applyItems(elem, inline_rules, PSEUDO_NONE)
 
     var i = elem.children.len - 1
     while i >= 0:
diff --git a/src/html/dom.nim b/src/html/dom.nim
index 1aa6c191..4a63b2d0 100644
--- a/src/html/dom.nim
+++ b/src/html/dom.nim
@@ -62,7 +62,7 @@ type
 
     id*: string
     classList*: seq[string]
-    attributes*: Table[string, Attr]
+    attributes*: Table[string, string]
     cssvalues*: CSSComputedValues
     cssvalues_before*: CSSComputedValues
     cssvalues_after*: CSSComputedValues
@@ -104,7 +104,6 @@ type
   HTMLBRElement* = ref HTMLBRElementObj
   HTMLBRElementObj = object of HTMLElementObj
 
-
 func firstChild(node: Node): Node =
   if node.childNodes.len == 0:
     return nil
@@ -235,8 +234,5 @@ func newAttr*(parent: Element, key: string, value: string): Attr =
   result.name = key
   result.value = value
 
-func getAttrValue*(element: Element, s: string): string =
-  let attr = element.attributes.getOrDefault(s, nil)
-  if attr != nil:
-    return attr.value
-  return ""
+func attr*(element: Element, s: string): string =
+  return element.attributes.getOrDefault(s, "")
diff --git a/src/html/parser.nim b/src/html/parser.nim
index 0102fe17..a3a6885d 100644
--- a/src/html/parser.nim
+++ b/src/html/parser.nim
@@ -225,11 +225,11 @@ proc processDocumentStartElement(state: var HTMLParseState, element: Element, ta
   var add = true
 
   for k, v in tag.attrs:
-    element.attributes[k] = element.newAttr(k, v)
+    element.attributes[k] = v
   
-  element.id = element.getAttrValue("id")
+  element.id = element.attr("id")
   if element.attributes.hasKey("class"):
-    for w in unicode.split(element.attributes["class"].value, Rune(' ')):
+    for w in unicode.split(element.attributes["class"], Rune(' ')):
       element.classList.add(w)
 
   case element.tagType
@@ -240,16 +240,16 @@ proc processDocumentStartElement(state: var HTMLParseState, element: Element, ta
   of TAG_STYLE:
     state.in_style = true
   of TAG_SELECT:
-    HTMLSelectElement(element).name = element.getAttrValue("name")
-    HTMLSelectElement(element).value = element.getAttrValue("value")
+    HTMLSelectElement(element).name = element.attr("name")
+    HTMLSelectElement(element).value = element.attr("value")
   of TAG_INPUT:
-    HTMLInputElement(element).value = element.getAttrValue("value")
-    HTMLInputElement(element).itype = element.getAttrValue("type").inputType()
-    HTMLInputElement(element).size = element.getAttrValue("size").inputSize()
+    HTMLInputElement(element).value = element.attr("value")
+    HTMLInputElement(element).itype = element.attr("type").inputType()
+    HTMLInputElement(element).size = element.attr("size").inputSize()
   of TAG_A:
-    HTMLAnchorElement(element).href = element.getAttrValue("href")
+    HTMLAnchorElement(element).href = element.attr("href")
   of TAG_OPTION:
-    HTMLOptionElement(element).value = element.getAttrValue("href")
+    HTMLOptionElement(element).value = element.attr("href")
   of TAG_HTML:
     add = false
   of TAG_HEAD: