Karax: prepare for the Nim release where 'nil' is not a thing for strings anymore
This commit is contained in:
parent
52026d1687
commit
b05c037bb0
2 changed files with 11 additions and 10 deletions
|
|
@ -208,7 +208,7 @@ template markDirty*(c: VComponent) =
|
||||||
inc c.version
|
inc c.version
|
||||||
|
|
||||||
proc setAttr*(n: VNode; key: kstring; val: kstring = "") =
|
proc setAttr*(n: VNode; key: kstring; val: kstring = "") =
|
||||||
if n.attrs.isNil:
|
if n.attrs.len == 0:
|
||||||
n.attrs = @[key, val]
|
n.attrs = @[key, val]
|
||||||
else:
|
else:
|
||||||
for i in countup(0, n.attrs.len-2, 2):
|
for i in countup(0, n.attrs.len-2, 2):
|
||||||
|
|
@ -291,7 +291,7 @@ proc addEventListener*(n: VNode; event: EventKind; handler: EventHandler) =
|
||||||
n.events.add((event, handler, nil))
|
n.events.add((event, handler, nil))
|
||||||
|
|
||||||
template toStringAttr(field) =
|
template toStringAttr(field) =
|
||||||
if n.field != nil:
|
if n.field.len > 0:
|
||||||
result.add " " & astToStr(field) & " = " & $n.field
|
result.add " " & astToStr(field) & " = " & $n.field
|
||||||
|
|
||||||
proc toString*(n: VNode; result: var string; indent: int) =
|
proc toString*(n: VNode; result: var string; indent: int) =
|
||||||
|
|
@ -306,7 +306,7 @@ proc toString*(n: VNode; result: var string; indent: int) =
|
||||||
if n.kind == VNodeKind.text:
|
if n.kind == VNodeKind.text:
|
||||||
result.add n.text
|
result.add n.text
|
||||||
else:
|
else:
|
||||||
if n.text != nil:
|
if n.text.len > 0:
|
||||||
result.add " value = "
|
result.add " value = "
|
||||||
result.add n.text
|
result.add n.text
|
||||||
for child in items(n):
|
for child in items(n):
|
||||||
|
|
@ -379,11 +379,11 @@ proc add*(result: var string, n: VNode, indent = 0, indWidth = 2) =
|
||||||
let kind = $n.kind
|
let kind = $n.kind
|
||||||
result.add('<')
|
result.add('<')
|
||||||
result.add(kind)
|
result.add(kind)
|
||||||
if n.id != nil:
|
if n.id.len > 0:
|
||||||
result.add " id=\""
|
result.add " id=\""
|
||||||
result.addEscapedAttr(n.id)
|
result.addEscapedAttr(n.id)
|
||||||
result.add('"')
|
result.add('"')
|
||||||
if n.class != nil:
|
if n.class.len > 0:
|
||||||
result.add " class=\""
|
result.add " class=\""
|
||||||
result.addEscapedAttr(n.class)
|
result.addEscapedAttr(n.class)
|
||||||
result.add('"')
|
result.add('"')
|
||||||
|
|
|
||||||
|
|
@ -241,8 +241,8 @@ proc setAttr(s: VStyle; a, value: kstring) {.noSideEffect.} =
|
||||||
s[i+1] = value
|
s[i+1] = value
|
||||||
return
|
return
|
||||||
elif s[i] > a:
|
elif s[i] > a:
|
||||||
s.add nil
|
s.add ""
|
||||||
s.add nil
|
s.add ""
|
||||||
# insertion point here:
|
# insertion point here:
|
||||||
for j in countdown(s.len-1, i, 2):
|
for j in countdown(s.len-1, i, 2):
|
||||||
s[j] = s[j-2]
|
s[j] = s[j-2]
|
||||||
|
|
@ -255,18 +255,19 @@ proc setAttr(s: VStyle; a, value: kstring) {.noSideEffect.} =
|
||||||
s.add value
|
s.add value
|
||||||
|
|
||||||
proc setAttr*(s: VStyle; attr: StyleAttr, value: kstring) {.noSideEffect.} =
|
proc setAttr*(s: VStyle; attr: StyleAttr, value: kstring) {.noSideEffect.} =
|
||||||
|
when kstring is cstring:
|
||||||
assert value != nil, "value must not be nil"
|
assert value != nil, "value must not be nil"
|
||||||
setAttr(s, toStyleAttrName[attr], value)
|
setAttr(s, toStyleAttrName[attr], value)
|
||||||
|
|
||||||
proc getAttr*(s: VStyle; attr: StyleAttr): kstring {.noSideEffect.} =
|
proc getAttr*(s: VStyle; attr: StyleAttr): kstring {.noSideEffect.} =
|
||||||
## returns 'nil' if the attribute has not been set.
|
## returns "" if the attribute has not been set.
|
||||||
var i = 0
|
var i = 0
|
||||||
let a = toStyleAttrName[attr]
|
let a = toStyleAttrName[attr]
|
||||||
while i < s.len:
|
while i < s.len:
|
||||||
if s[i] == a:
|
if s[i] == a:
|
||||||
return s[i+1]
|
return s[i+1]
|
||||||
elif s[i] > a:
|
elif s[i] > a:
|
||||||
return nil
|
return ""
|
||||||
inc i, 2
|
inc i, 2
|
||||||
|
|
||||||
proc style*(pairs: varargs[(StyleAttr, kstring)]): VStyle {.noSideEffect.} =
|
proc style*(pairs: varargs[(StyleAttr, kstring)]): VStyle {.noSideEffect.} =
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue