about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2023-07-28 22:48:22 +0200
committerbptato <nincsnevem662@gmail.com>2023-07-28 22:48:22 +0200
commit4ffd835674870434dd820f8778b5eeeee615723c (patch)
treeef252f7da9a2476068e0ec6a667e40501590c6aa /src
parentef308887900bd210ad3f5ae39a730fbb891c78a4 (diff)
downloadchawan-4ffd835674870434dd820f8778b5eeeee615723c.tar.gz
css: very minor optimizations
probably insignificant overall
Diffstat (limited to 'src')
-rw-r--r--src/css/cascade.nim10
-rw-r--r--src/css/sheet.nim24
2 files changed, 15 insertions, 19 deletions
diff --git a/src/css/cascade.nim b/src/css/cascade.nim
index 888191c5..1c711dfb 100644
--- a/src/css/cascade.nim
+++ b/src/css/cascade.nim
@@ -82,20 +82,20 @@ type
 
 proc calcRule(tosorts: var ToSorts, styledNode: StyledNode, rule: CSSRuleDef) =
   for sel in rule.sels:
-    #TODO we shouldn't need backtracking for this...
     if styledNode.selectorsMatch(sel):
       let spec = getSpecificity(sel)
-      tosorts[sel.pseudo].add((spec,rule.decls))
+      tosorts[sel.pseudo].add((spec, rule.decls))
 
 func calcRules(styledNode: StyledNode, sheet: CSSStylesheet): DeclarationList =
   var tosorts: ToSorts
   let elem = Element(styledNode.node)
   for rule in sheet.gen_rules(elem.tagType, elem.id, elem.classList.toks):
     tosorts.calcRule(styledNode, rule)
-
   for i in PseudoElem:
-    tosorts[i].sort(proc(x, y: (int, seq[CSSDeclaration])): int =
-      x[0] - y[0])
+    tosorts[i].sort((proc(x, y: (int, seq[CSSDeclaration])): int =
+      cmp(x[0], y[0])
+    ), order = Ascending)
+    result[i] = newSeqOfCap[CSSDeclaration](tosorts[i].len)
     for item in tosorts[i]:
       result[i].add(item[1])
 
diff --git a/src/css/sheet.nim b/src/css/sheet.nim
index e7a18091..d0d03cb0 100644
--- a/src/css/sheet.nim
+++ b/src/css/sheet.nim
@@ -22,8 +22,8 @@ type
   CSSStylesheet* = ref object
     mq_list*: seq[CSSMediaQueryDef]
     tag_table*: array[TagType, seq[CSSRuleDef]]
-    id_table*: TableRef[string, seq[CSSRuleDef]]
-    class_table*: TableRef[string, seq[CSSRuleDef]]
+    id_table*: Table[string, seq[CSSRuleDef]]
+    class_table*: Table[string, seq[CSSRuleDef]]
     general_list*: seq[CSSRuleDef]
     len*: int
 
@@ -35,8 +35,8 @@ type SelectorHashes = object
 func newStylesheet*(cap: int): CSSStylesheet =
   new(result)
   let bucketsize = cap div 2
-  result.id_table = newTable[string, seq[CSSRuleDef]](bucketsize)
-  result.class_table = newTable[string, seq[CSSRuleDef]](bucketsize)
+  result.id_table = initTable[string, seq[CSSRuleDef]](bucketsize)
+  result.class_table = initTable[string, seq[CSSRuleDef]](bucketsize)
   result.general_list = newSeqOfCap[CSSRuleDef](bucketsize)
 
 proc getSelectorIds(hashes: var SelectorHashes, sel: Selector): bool
@@ -46,9 +46,6 @@ proc getSelectorIds(hashes: var SelectorHashes, sels: CompoundSelector) =
     if hashes.getSelectorIds(sel):
       break
 
-# For now, we match elements against the *last* selector.
-#TODO this is inefficient, so we should eventually get rid of this
-# function
 proc getSelectorIds(hashes: var SelectorHashes, cxsel: ComplexSelector) =
   hashes.getSelectorIds(cxsel[^1])
 
@@ -116,14 +113,13 @@ iterator gen_rules*(sheet: CSSStylesheet, tag: TagType, id: string, classes: seq
   for rule in sheet.tag_table[tag]:
     yield rule
   if id != "":
-    if sheet.id_table.hasKey(id):
-      for rule in sheet.id_table[id]:
+    sheet.id_table.withValue(id, v):
+      for rule in v[]:
+        yield rule
+  for class in classes:
+    sheet.class_table.withValue(class, v):
+      for rule in v[]:
         yield rule
-  if classes.len > 0:
-    for class in classes:
-      if sheet.class_table.hasKey(class):
-        for rule in sheet.class_table[class]:
-          yield rule
   for rule in sheet.general_list:
     yield rule