diff --git a/karax/vdom.nim b/karax/vdom.nim index d0ee5b5..8a3dfaa 100644 --- a/karax/vdom.nim +++ b/karax/vdom.nim @@ -303,6 +303,12 @@ proc sameAttrs*(a, b: VNode): bool = proc addEventListener*(n: VNode; event: EventKind; handler: EventHandler) = n.events.add((event, handler, nil)) +when kstring is cstring: + proc len(a: kstring): int = + # xxx: maybe move where kstring is defined + # without this, `n.field.len` fails on js (non web) platform + if a == nil: 0 else: a.len + template toStringAttr(field) = if n.field.len > 0: result.add " " & astToStr(field) & " = " & $n.field diff --git a/karax/vstyles.nim b/karax/vstyles.nim index 13f9c3a..a5971a9 100644 --- a/karax/vstyles.nim +++ b/karax/vstyles.nim @@ -235,6 +235,9 @@ proc eq*(a, b: VStyle): bool = return true proc setAttr*(s: VStyle; a, value: kstring) {.noSideEffect.} = + ## inserts (a, value) in sorted order of key `a` + # worst case quadratic complexity (if given styles in reverse order), hopefully + # not a concern assuming small cardinal var i = 0 while i < s.len: if s[i] == a: @@ -243,8 +246,8 @@ proc setAttr*(s: VStyle; a, value: kstring) {.noSideEffect.} = elif s[i] > a: s.add "" s.add "" - # insertion point here: - for j in countdown(s.len-1, i, 2): + # insertion point here, shift all remaining pairs by 2 indexes + for j in countdown(s.len-1, i+3, 2): s[j] = s[j-2] s[j-1] = s[j-3] s[i] = a