Skip to content

Commit

Permalink
add xmltree.clear, fixes nim-lang#3797 (nim-lang#10711)
Browse files Browse the repository at this point in the history
* add `xmltree.clear`, fixes nim-lang#3797
* use `setLen`
  • Loading branch information
narimiran authored and Araq committed Feb 20, 2019
1 parent c025786 commit 6a5c747
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions lib/pure/xmltree.nim
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,35 @@ proc `[]`* (n: var XmlNode, i: int): var XmlNode {.inline.} =
assert n.k == xnElement
result = n.s[i]

proc clear*(n: var XmlNode) =
## Recursively clear all children of an XmlNode.
runnableExamples:
from strutils import unindent

var g = newElement("myTag")
g.add newText("some text")
g.add newComment("this is comment")

var h = newElement("secondTag")
h.add newEntity("some entity")

let att = {"key1": "first value", "key2": "second value"}.toXmlAttributes
var k = newXmlTree("treeTag", [g, h], att)

assert ($k).unindent ==
"""<treeTag key2="second value" key1="first value">
<myTag>some text<!-- this is comment --></myTag>
<secondTag>&some entity;</secondTag>
</treeTag>""".unindent
clear(k)
assert $k == """<treeTag key2="second value" key1="first value" />"""

for i in 0 ..< n.len:
clear(n[i])
if n.k == xnElement:
n.s.setLen(0)


iterator items*(n: XmlNode): XmlNode {.inline.} =
## Iterates over any child of `n`.
##
Expand Down

0 comments on commit 6a5c747

Please sign in to comment.