summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2016-12-01 11:20:27 +0100
committerGitHub <noreply@github.com>2016-12-01 11:20:27 +0100
commit0f700d22778d8d24a3a97c2bcac8885d4bd29cfa (patch)
tree69a0c014a0772337acebb4df4fcb14f887d8769d /lib
parent7c83daee4283c6b3282407316632a07f4fbae05d (diff)
parentff69656f80a6494eecad3bd41e9c1d8e68e7f5b7 (diff)
downloadNim-0f700d22778d8d24a3a97c2bcac8885d4bd29cfa.tar.gz
Merge pull request #5074 from nigredo-tori/fix-5073
Make xmltree.innerText recursive, define for all node kinds
Diffstat (limited to 'lib')
-rw-r--r--lib/pure/xmltree.nim23
1 files changed, 17 insertions, 6 deletions
diff --git a/lib/pure/xmltree.nim b/lib/pure/xmltree.nim
index 3c6eb14e3..7cfb62157 100644
--- a/lib/pure/xmltree.nim
+++ b/lib/pure/xmltree.nim
@@ -91,13 +91,24 @@ 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.
+  ## 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.
+  proc worker(res: var string, n: XmlNode) =
+    case n.k
+    of xnText, xnEntity:
+      res.add(n.fText)
+    of xnElement:
+      for sub in n.s:
+        worker(res, sub)
+    else:
+      discard
+
   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)
+  worker(result, n)
 
 proc tag*(n: XmlNode): string {.inline.} =
   ## gets the tag name of `n`. `n` has to be an ``xnElement`` node.