summary refs log tree commit diff stats
path: root/lib/pure/xmltree.nim
diff options
context:
space:
mode:
authorrumpf_a@web.de <>2010-02-20 19:21:38 +0100
committerrumpf_a@web.de <>2010-02-20 19:21:38 +0100
commit6bc16904edd3738ab97573b9eeb3a6a7cce9574c (patch)
treea24577d18f693a0b5497ad78b54c4d20cb711fc6 /lib/pure/xmltree.nim
parent64da2f16813bbf03b8a2117d7c4abffd1adf525f (diff)
downloadNim-6bc16904edd3738ab97573b9eeb3a6a7cce9574c.tar.gz
bugfixes for unicode; xmlparser; htmlparser; scanner
Diffstat (limited to 'lib/pure/xmltree.nim')
-rw-r--r--lib/pure/xmltree.nim24
1 files changed, 20 insertions, 4 deletions
diff --git a/lib/pure/xmltree.nim b/lib/pure/xmltree.nim
index 2b0977874..7b77fe156 100644
--- a/lib/pure/xmltree.nim
+++ b/lib/pure/xmltree.nim
@@ -153,8 +153,15 @@ proc addIndent(result: var string, indent: int) =
   result.add("\n")
   for i in 1..indent: result.add(' ')
   
+proc noWhitespace(n: PXmlNode): bool =
+  #for i in 1..n.len-1:
+  #  if n[i].kind != n[0].kind: return true
+  for i in 0..n.len-1:
+    if n[i].kind in {xnText, xnEntity}: return true
+  
 proc add*(result: var string, n: PXmlNode, indent = 0, indWidth = 2) = 
   ## adds the textual representation of `n` to `result`.
+  if n == nil: return
   case n.k
   of xnElement:
     result.add('<')
@@ -168,10 +175,19 @@ proc add*(result: var string, n: PXmlNode, indent = 0, indWidth = 2) =
         result.add('"')
     if n.len > 0:
       result.add('>')
-      for i in 0..n.len-1:
-        result.addIndent(indent+indWidth)
-        result.add(n[i], indent+indWidth, indWidth)
-      result.addIndent(indent)
+      if n.len > 1:
+        if noWhitespace(n):
+          # for mixed leaves, we cannot output whitespace for readability,
+          # because this would be wrong. For example: ``a<b>b</b>`` is
+          # different from ``a <b>b</b>``.
+          for i in 0..n.len-1: result.add(n[i], indent+indWidth, indWidth)
+        else: 
+          for i in 0..n.len-1:
+            result.addIndent(indent+indWidth)
+            result.add(n[i], indent+indWidth, indWidth)
+          result.addIndent(indent)
+      else:
+        result.add(n[0], indent+indWidth, indWidth)
       result.add("</")
       result.add(n.fTag)
       result.add(">")