diff options
author | Zahary Karadjov <zahary@gmail.com> | 2018-05-05 17:02:29 +0300 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2018-05-07 09:37:49 +0200 |
commit | 6758fbd06e5cd88b620939b60deb5bfa00345efc (patch) | |
tree | 2c6fd785a38a37ce866bf4dc2291952eb10b467d /lib/pure | |
parent | 56230071263f9884ed14ab25784834a20dd73c0e (diff) | |
download | Nim-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.nim | 2 | ||||
-rw-r--r-- | lib/pure/xmldom.nim | 21 |
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(""") 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 & "]]>") |