Skip to content

Commit

Permalink
fixes for recent Nim devel version, closes ba0f3/uibuilder.nim#3
Browse files Browse the repository at this point in the history
  • Loading branch information
ba0f3 committed Sep 10, 2018
1 parent d39ab81 commit 00afd4e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 23 deletions.
36 changes: 14 additions & 22 deletions src/xml.nim
Original file line number Diff line number Diff line change
Expand Up @@ -163,56 +163,48 @@ proc newNode(name: string, text = ""): XmlNode =
new(result)
result.name = name
result.text = text
result.attributes = newStringTable(modeCaseInsensitive)
result.children = @[]

proc child*(node: XmlNode, name: string): XmlNode =
## finds the first element of `node` with name `name`
## returns `nil` on failure
if not node.children.isNil:
for n in node.children:
if n.name == name:
result = n
break
for n in node.children:
if n.name == name:
result = n
break

proc `$`*(node: XmlNode): string =
result = "<"
result.add(node.name)
if not node.attributes.isNil:
for k, v in node.attributes.pairs:
result.add(fmt" {k}=""{v}""")
if node.text.len == 0 and node.children.isNil:

for k, v in node.attributes.pairs:
result.add(fmt" {k}=""{v}""")
if node.text.len == 0 and node.children.len == 0:
result.add(" />")
return
elif node.text.len > 0:
result.add(">" & node.text)
else:
result.add(">")

if not node.children.isNil:
for child in node.children:
result.add($child)
for child in node.children:
result.add($child)
result.add("</" & node.name & ">")


proc addChild*(node, child: XmlNode) =
if node.children.isNil:
node.children = @[]
node.children.add(child)

proc hasAttr*(node: XmlNode, name: string): bool =
## returns `true` if `node` has attribute `name`
if node.attributes.isNil:
result = false
else:
result = node.attributes.hasKey(name)
result = node.attributes.hasKey(name)

proc attr*(node: XmlNode, name: string): string =
## returns value of attribute `name`, returns "" on failure
if not node.attributes.isNil:
result = node.attributes.getOrDefault(name)
result = node.attributes.getOrDefault(name)

proc setAttr(node: XmlNode, name, value: string) =
if node.attributes.isNil:
node.attributes = newStringTable(modeCaseInsensitive)
node.attributes[name] = value

proc parseNode(tokens: seq[XmlToken], start = 0): (XmlNode, int) =
Expand Down
2 changes: 1 addition & 1 deletion src/xml/selector.nim
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ proc searchCombined(parent: XmlNode, selectors: seq[Selector], found: var seq[Xm
matches = @[]

for j in starts:
if parent.children.isNil:
if parent.children.len == 0:
continue
for k in j..parent.children.len-1:
var child = parent.children[k]
Expand Down

0 comments on commit 00afd4e

Please sign in to comment.