summary refs log tree commit diff stats
path: root/lib/pure/xmltree.nim
diff options
context:
space:
mode:
authorDmitry Polienko <dmitry@eldis.ru>2016-09-05 15:43:44 +0700
committerDmitry Polienko <dmitry@eldis.ru>2016-09-05 15:43:44 +0700
commit8c9e2d7a432fe1dc88267a4033bcb71170020e76 (patch)
tree395b20930490a06b580c68782c541c88cc120f15 /lib/pure/xmltree.nim
parent147c2577200306317b5ba52335293a07b6111444 (diff)
downloadNim-8c9e2d7a432fe1dc88267a4033bcb71170020e76.tar.gz
xmltree: separate escaping for attributes
Diffstat (limited to 'lib/pure/xmltree.nim')
-rw-r--r--lib/pure/xmltree.nim17
1 files changed, 14 insertions, 3 deletions
diff --git a/lib/pure/xmltree.nim b/lib/pure/xmltree.nim
index a9fc8998a..3c6eb14e3 100644
--- a/lib/pure/xmltree.nim
+++ b/lib/pure/xmltree.nim
@@ -226,6 +226,18 @@ proc noWhitespace(n: XmlNode): bool =
 
 proc add*(result: var string, n: XmlNode, indent = 0, indWidth = 2) =
   ## adds the textual representation of `n` to `result`.
+
+  proc addEscapedAttr(result: var string, s: string) =
+    # `addEscaped` alternative with less escaped characters.
+    # Only to be used for escaping attribute values enclosed in double quotes!
+    for c in items(s):
+      case c
+      of '<': result.add("&lt;")
+      of '>': result.add("&gt;")
+      of '&': result.add("&amp;")
+      of '"': result.add("&quot;")
+      else: result.add(c)
+
   if n == nil: return
   case n.k
   of xnElement:
@@ -236,7 +248,7 @@ proc add*(result: var string, n: XmlNode, indent = 0, indWidth = 2) =
         result.add(' ')
         result.add(key)
         result.add("=\"")
-        result.addEscaped(val)
+        result.addEscapedAttr(val)
         result.add('"')
     if n.len > 0:
       result.add('>')
@@ -381,6 +393,5 @@ proc findAll*(n: XmlNode, tag: string): seq[XmlNode] =
   findAll(n, tag, result)
 
 when isMainModule:
-  let link = "http://nim-lang.org"
-  assert """<a href="""" & escape(link) & """">Nim rules.</a>""" ==
+  assert """<a href="http://nim-lang.org">Nim rules.</a>""" ==
     $(<>a(href="http://nim-lang.org", newText("Nim rules.")))