about summary refs log tree commit diff stats
path: root/src/css
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-05-27 23:32:55 +0200
committerbptato <nincsnevem662@gmail.com>2024-05-27 23:35:07 +0200
commit6340e6a2c041abb9273241897c485aabe87ff5dd (patch)
treebdaa339338bc382eb7a56bb47536c930749e81f7 /src/css
parenta8f3891507cf21320f78f2d4a97731ad4462c676 (diff)
downloadchawan-6340e6a2c041abb9273241897c485aabe87ff5dd.tar.gz
stylednode: move invalidation data to DOM
this way, we do not refer to nodes of previous cascade passes
Diffstat (limited to 'src/css')
-rw-r--r--src/css/match.nim6
-rw-r--r--src/css/stylednode.nim35
2 files changed, 16 insertions, 25 deletions
diff --git a/src/css/match.nim b/src/css/match.nim
index e621b332..52a8ab7f 100644
--- a/src/css/match.nim
+++ b/src/css/match.nim
@@ -83,7 +83,7 @@ func pseudoSelectorMatches[T: Element|StyledNode](elem: T; sel: Selector;
     return elem.parentNode.firstElementChild == elem and
       elem.parentNode.lastElementChild == elem
   of pcHover:
-    when selem is StyledNode: felem.addDependency(selem, dtHover)
+    when selem is StyledNode: felem.addDependency(elem, dtHover)
     return elem.hover
   of pcRoot: return elem == elem.document.html
   of pcNthChild:
@@ -133,14 +133,14 @@ func pseudoSelectorMatches[T: Element|StyledNode](elem: T; sel: Selector;
         inc i
     return false
   of pcChecked:
-    when selem is StyledNode: felem.addDependency(selem, dtChecked)
+    when selem is StyledNode: felem.addDependency(elem, dtChecked)
     if elem.tagType == TAG_INPUT:
       return HTMLInputElement(elem).checked
     elif elem.tagType == TAG_OPTION:
       return HTMLOptionElement(elem).selected
     return false
   of pcFocus:
-    when selem is StyledNode: felem.addDependency(selem, dtFocus)
+    when selem is StyledNode: felem.addDependency(elem, dtFocus)
     return elem.document.focus == elem
   of pcNot:
     return not selem.selectorsMatch(sel.pseudo.fsels, felem)
diff --git a/src/css/stylednode.nim b/src/css/stylednode.nim
index 218f330d..4d14123d 100644
--- a/src/css/stylednode.nim
+++ b/src/css/stylednode.nim
@@ -39,15 +39,7 @@ type
   StyledType* = enum
     stElement, stText, stReplacement
 
-  DependencyType* = enum
-    dtHover, dtChecked, dtFocus
-
-  DependencyInfo* = object
-    # All nodes we depend on, for each dependency type d.
-    nodes*: array[DependencyType, seq[StyledNode]]
-    # Previous value. The owner StyledNode is marked as invalid when one of
-    # these no longer matches the DOM value.
-    prev: array[DependencyType, bool]
+  DependencyInfo = array[DependencyType, seq[Element]]
 
   StyledNode* = ref object
     parent*: StyledNode
@@ -59,6 +51,7 @@ type
     of stElement:
       computed*: CSSComputedValues
       children*: seq[StyledNode]
+      # All elements we depend on, for each dependency type d.
       depends*: DependencyInfo
     of stReplacement:
       # replaced elements: quotes, or (TODO) markers, images
@@ -126,33 +119,31 @@ func isValid*(styledNode: StyledNode): bool =
   if styledNode.node != nil and Element(styledNode.node).invalid:
     return false
   for d in DependencyType:
-    for child in styledNode.depends.nodes[d]:
-      assert child.node != nil
-      let elem = Element(child.node)
+    for elem in styledNode.depends[d]:
       case d
       of dtHover:
-        if child.depends.prev[d] != elem.hover:
+        if elem.prev[d] != elem.hover:
           return false
       of dtChecked:
-        if child.depends.prev[d] != elem.checked:
+        if elem.prev[d] != elem.checked:
           return false
       of dtFocus:
         let focus = elem.document.focus == elem
-        if child.depends.prev[d] != focus:
+        if elem.prev[d] != focus:
           return false
   return true
 
 proc applyDependValues*(styledNode: StyledNode) =
   let elem = Element(styledNode.node)
-  styledNode.depends.prev[dtHover] = elem.hover
-  styledNode.depends.prev[dtChecked] = elem.checked
+  elem.prev[dtHover] = elem.hover
+  elem.prev[dtChecked] = elem.checked
   let focus = elem.document.focus == elem
-  styledNode.depends.prev[dtFocus] = focus
+  elem.prev[dtFocus] = focus
   elem.invalid = false
 
-proc addDependency*(styledNode, dep: StyledNode; t: DependencyType) =
-  if dep notin styledNode.depends.nodes[t]:
-    styledNode.depends.nodes[t].add(dep)
+proc addDependency*(styledNode: StyledNode; dep: Element; t: DependencyType) =
+  if dep notin styledNode.depends[t]:
+    styledNode.depends[t].add(dep)
 
 func newStyledElement*(parent: StyledNode; element: Element;
     computed: CSSComputedValues; reg: DependencyInfo): StyledNode =
@@ -172,7 +163,7 @@ func newStyledElement*(element: Element): StyledNode =
   return StyledNode(t: stElement, node: element)
 
 func newStyledElement*(parent: StyledNode; pseudo: PseudoElem;
-    computed: CSSComputedValues; reg: sink DependencyInfo): StyledNode =
+    computed: CSSComputedValues; reg: DependencyInfo): StyledNode =
   return StyledNode(
     t: stElement,
     computed: computed,