summary refs log tree commit diff stats
path: root/lib/pure
diff options
context:
space:
mode:
authorZahary Karadjov <zahary@gmail.com>2018-05-05 17:02:29 +0300
committerAndreas Rumpf <rumpf_a@web.de>2018-05-07 09:37:49 +0200
commit6758fbd06e5cd88b620939b60deb5bfa00345efc (patch)
tree2c6fd785a38a37ce866bf4dc2291952eb10b467d /lib/pure
parent56230071263f9884ed14ab25784834a20dd73c0e (diff)
downloadNim-6758fbd06e5cd88b620939b60deb5bfa00345efc.tar.gz
Export an 'escapeXml' proc from xmldom
The interface is similar to escapeJson
Diffstat (limited to 'lib/pure')
-rw-r--r--lib/pure/terminal.nim2
-rw-r--r--lib/pure/xmldom.nim21
2 files changed, 15 insertions, 8 deletions
diff --git a/lib/pure/terminal.nim b/lib/pure/terminal.nim
index 8b5dcf0c5..fcca4d5d7 100644
--- a/lib/pure/terminal.nim
+++ b/lib/pure/terminal.nim
@@ -621,7 +621,7 @@ proc ansiForegroundColorCode*(color: Color): string =
       colorsFGCache[color] = result
 
 template ansiForegroundColorCode*(color: static[Color]): string =
-  let rgb = extractRGB(color)
+  const rgb = extractRGB(color)
   (static(fmt"{fgPrefix}{rgb.r};{rgb.g};{rgb.b}m"))
 
 proc ansiBackgroundColorCode*(color: Color): string =
diff --git a/lib/pure/xmldom.nim b/lib/pure/xmldom.nim
index c38d36026..8cd47aa39 100644
--- a/lib/pure/xmldom.nim
+++ b/lib/pure/xmldom.nim
@@ -1069,17 +1069,15 @@ proc splitData*(textNode: PText, offset: int): PText =
     var newNode: PText = textNode.fOwnerDocument.createTextNode(right)
     return newNode
 
-
 # ProcessingInstruction
 proc target*(pi: PProcessingInstruction): string =
   ## Returns the Processing Instructions target
 
   return pi.fTarget
 
-
-# --Other stuff--
-# Writer
-proc addEscaped(s: string): string =
+proc escapeXml*(s: string; result: var string) =
+  ## Prepares a string for insertion into a XML document
+  ## by escaping the XML special characters.
   result = ""
   for c in items(s):
     case c
@@ -1089,11 +1087,20 @@ proc addEscaped(s: string): string =
     of '"': result.add("&quot;")
     else: result.add(c)
 
+proc escapeXml*(s: string): string =
+  ## Prepares a string for insertion into a XML document
+  ## by escaping the XML special characters.
+  result = newStringOfCap(s.len + s.len shr 4)
+  escapeXml(s, result)
+
+# --Other stuff--
+# Writer
+
 proc nodeToXml(n: PNode, indent: int = 0): string =
   result = spaces(indent) & "<" & n.nodeName
   if not isNil(n.attributes):
     for i in items(n.attributes):
-      result.add(" " & i.name & "=\"" & addEscaped(i.value) & "\"")
+      result.add(" " & i.name & "=\"" & escapeXml(i.value) & "\"")
 
   if isNil(n.childNodes) or n.childNodes.len() == 0:
     result.add("/>") # No idea why this doesn't need a \n :O
@@ -1106,7 +1113,7 @@ proc nodeToXml(n: PNode, indent: int = 0): string =
         result.add(nodeToXml(i, indent + 2))
       of TextNode:
         result.add(spaces(indent * 2))
-        result.add(addEscaped(i.nodeValue))
+        result.add(escapeXml(i.nodeValue))
       of CDataSectionNode:
         result.add(spaces(indent * 2))
         result.add("<![CDATA[" & i.nodeValue & "]]>")
=2c584cdb3d71f0cd811e3774c49db34735040032'>^
43bddf62d ^
d3394be55 ^
526f9da46 ^
43bddf62d ^
526f9da46 ^



43bddf62d ^
526f9da46 ^
43bddf62d ^
526f9da46 ^

43bddf62d ^
526f9da46 ^
d3394be55 ^
526f9da46 ^





43bddf62d ^
526f9da46 ^
d3394be55 ^
43bddf62d ^
526f9da46 ^












d3394be55 ^
526f9da46 ^





43bddf62d ^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89