diff options
-rw-r--r-- | lib/pure/xmltree.nim | 26 | ||||
-rw-r--r-- | tests/stdlib/txmltree.nim | 18 |
2 files changed, 35 insertions, 9 deletions
diff --git a/lib/pure/xmltree.nim b/lib/pure/xmltree.nim index 3c6eb14e3..5385705cd 100644 --- a/lib/pure/xmltree.nim +++ b/lib/pure/xmltree.nim @@ -91,13 +91,25 @@ proc rawTag*(n: XmlNode): string {.inline.} = shallowCopy(result, n.fTag) proc innerText*(n: XmlNode): string = - ## gets the inner text of `n`. `n` has to be an ``xnElement`` node. Only - ## ``xnText`` and ``xnEntity`` nodes are considered part of `n`'s inner text, - ## other child nodes are silently ignored. - result = "" - assert n.k == xnElement - for i in 0 .. n.s.len-1: - if n.s[i].k in {xnText, xnEntity}: result.add(n.s[i].fText) + ## gets the inner text of `n`: + ## + ## - If `n` is `xnText` or `xnEntity`, returns its content. + ## - If `n` is `xnElement`, runs recursively on each child node and + ## concatenates the results. + ## - Otherwise returns an empty string. + var res = "" + proc worker(n: XmlNode) = + case n.k + of { xnText, xnEntity }: + res.add(n.fText) + of xnElement: + for sub in n.s: + worker(sub) + else: + discard + + worker(n) + res proc tag*(n: XmlNode): string {.inline.} = ## gets the tag name of `n`. `n` has to be an ``xnElement`` node. diff --git a/tests/stdlib/txmltree.nim b/tests/stdlib/txmltree.nim index bfe2dc94a..a849859e3 100644 --- a/tests/stdlib/txmltree.nim +++ b/tests/stdlib/txmltree.nim @@ -1,6 +1,11 @@ discard """ file: "txmltree.nim" - output: "true" + output: '''true +true +true +true +true +''' """ import xmltree, strtabs @@ -9,5 +14,14 @@ var x = <>a(href="nim.de", newText("www.nim-test.de")) echo($x == "<a href=\"nim.de\">www.nim-test.de</a>") +echo(newText("foo").innerText == "foo") +echo(newEntity("bar").innerText == "bar") +echo(newComment("baz").innerText == "") - +let y = newXmlTree("x", [ + newText("foo"), + newXmlTree("y", [ + newText("bar") + ]) +]) +echo(y.innerText == "foobar") |