about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2023-01-01 22:05:06 +0100
committerbptato <nincsnevem662@gmail.com>2023-01-01 22:05:06 +0100
commitd4d45f4148b47757bab5f2c4d2aa20fd2afc7d25 (patch)
tree2a0ee6fa8002c5e49d56b2aa88215c95d17004d1 /src
parentbfac3a3e843fcb3a6f2d5dd3cc2e45967e8b4f1b (diff)
downloadchawan-d4d45f4148b47757bab5f2c4d2aa20fd2afc7d25.tar.gz
selectorparser: support :link, :visited
I say "support", but :visited is never matched.
Diffstat (limited to 'src')
-rw-r--r--src/css/cascade.nim7
-rw-r--r--src/css/match.nim4
-rw-r--r--src/css/selectorparser.nim7
-rw-r--r--src/css/values.nim11
4 files changed, 22 insertions, 7 deletions
diff --git a/src/css/cascade.nim b/src/css/cascade.nim
index 7822bca2..acf2ddf4 100644
--- a/src/css/cascade.nim
+++ b/src/css/cascade.nim
@@ -185,7 +185,8 @@ proc applyDeclarations(styledNode: StyledNode, parent: CSSComputedValues, ua, us
   styledNode.computed = builder.buildComputedValues()
 
 # Either returns a new styled node or nil.
-proc applyDeclarations(pseudo: PseudoElem, styledParent: StyledNode, ua, user: DeclarationList, author: seq[DeclarationList]): StyledNode =
+proc applyDeclarations(pseudo: PseudoElem, styledParent: StyledNode, ua,
+                       user: DeclarationList, author: seq[DeclarationList]): StyledNode =
   var builder = newComputedValueBuilder(styledParent.computed)
 
   builder.addValues(ua[pseudo], ORIGIN_USER_AGENT)
@@ -268,9 +269,7 @@ proc applyRules(document: Document, ua, user: CSSStylesheet, cachedTree: StyledN
         styledParent.children.add(styledChild)
     else:
       # From here on, computed values of this node's children are invalid
-      # (because of inheritance).
-      # That said, we could do this way more efficiently... TODO.
-      # (For example, we could replace the parent's computed values in-place.)
+      # because of property inheritance.
       cachedChild = nil
       if pseudo != PSEUDO_NONE:
         let (ua, user, authordecls) = styledParent.calcRules(ua, user, author)
diff --git a/src/css/match.nim b/src/css/match.nim
index 2d69943b..b17c690d 100644
--- a/src/css/match.nim
+++ b/src/css/match.nim
@@ -100,6 +100,10 @@ func pseudoSelectorMatches[T: Element|StyledNode](elem: T, sel: Selector, felem:
     return selem.selectorsMatch(sel.pseudo.fsels, felem)
   of PSEUDO_LANG:
     return sel.pseudo.s == "en" #TODO languages?
+  of PSEUDO_LINK:
+    return elem.tagType in {TAG_A, TAG_AREA} and elem.attrb("href")
+  of PSEUDO_VISITED:
+    return false
 
 func combinatorSelectorMatches[T: Element|StyledNode](elem: T, sel: Selector, felem: T): bool =
   let selem = elem
diff --git a/src/css/selectorparser.nim b/src/css/selectorparser.nim
index 90214a13..6b719ba6 100644
--- a/src/css/selectorparser.nim
+++ b/src/css/selectorparser.nim
@@ -24,7 +24,8 @@ type
   PseudoClass* = enum
     PSEUDO_FIRST_CHILD, PSEUDO_LAST_CHILD, PSEUDO_ONLY_CHILD, PSEUDO_HOVER,
     PSEUDO_ROOT, PSEUDO_NTH_CHILD, PSEUDO_NTH_LAST_CHILD, PSEUDO_CHECKED,
-    PSEUDO_FOCUS, PSEUDO_IS, PSEUDO_NOT, PSEUDO_WHERE, PSEUDO_LANG
+    PSEUDO_FOCUS, PSEUDO_IS, PSEUDO_NOT, PSEUDO_WHERE, PSEUDO_LANG,
+    PSEUDO_LINK, PSEUDO_VISITED
 
   CombinatorType* = enum
     DESCENDANT_COMBINATOR, CHILD_COMBINATOR, NEXT_SIBLING_COMBINATOR,
@@ -275,6 +276,10 @@ proc parseSelectorToken(state: var SelectorParser, csstoken: CSSToken) =
         add_pseudo_class PSEUDO_CHECKED
       of "focus":
         add_pseudo_class PSEUDO_FOCUS
+      of "link":
+        add_pseudo_class PSEUDO_LINK
+      of "visited":
+        add_pseudo_class PSEUDO_VISITED
     of QUERY_PSELEM:
       case csstoken.value
       of "before":
diff --git a/src/css/values.nim b/src/css/values.nim
index 58e00d74..89a9f7d1 100644
--- a/src/css/values.nim
+++ b/src/css/values.nim
@@ -330,6 +330,13 @@ func propertyType(s: string): CSSPropertyType =
 func valueType(prop: CSSPropertyType): CSSValueType =
   return ValueTypes[prop]
 
+func `$`*(val: CSSComputedValue): string =
+  result = ($val.t).toLowerAscii().split('_')[1..^1].join('-') & ": "
+  case val.v
+  of VALUE_COLOR:
+    result &= $val.color
+  else: discard
+
 macro `{}`*(vals: CSSComputedValues, s: string): untyped =
   let t = propertyType($s)
   let vs = $valueType(t)
@@ -1232,10 +1239,10 @@ func inheritProperties*(parent: CSSComputedValues): CSSComputedValues =
     else:
       result[prop] = getDefault(prop)
 
-func copyProperties*(parent: CSSComputedValues): CSSComputedValues =
+func copyProperties*(props: CSSComputedValues): CSSComputedValues =
   new(result)
   for prop in CSSPropertyType:
-    result[prop] = parent[prop]
+    result[prop] = props[prop]
 
 func rootProperties*(): CSSComputedValues =
   new(result)