Export an 'escapeXml' proc from xmldom
The interface is similar to escapeJson
This commit is contained in:
parent
5623007126
commit
6758fbd06e
2 changed files with 15 additions and 8 deletions
|
|
@ -621,7 +621,7 @@ proc ansiForegroundColorCode*(color: Color): string =
|
||||||
colorsFGCache[color] = result
|
colorsFGCache[color] = result
|
||||||
|
|
||||||
template ansiForegroundColorCode*(color: static[Color]): string =
|
template ansiForegroundColorCode*(color: static[Color]): string =
|
||||||
let rgb = extractRGB(color)
|
const rgb = extractRGB(color)
|
||||||
(static(fmt"{fgPrefix}{rgb.r};{rgb.g};{rgb.b}m"))
|
(static(fmt"{fgPrefix}{rgb.r};{rgb.g};{rgb.b}m"))
|
||||||
|
|
||||||
proc ansiBackgroundColorCode*(color: Color): string =
|
proc ansiBackgroundColorCode*(color: Color): string =
|
||||||
|
|
|
||||||
|
|
@ -1069,17 +1069,15 @@ proc splitData*(textNode: PText, offset: int): PText =
|
||||||
var newNode: PText = textNode.fOwnerDocument.createTextNode(right)
|
var newNode: PText = textNode.fOwnerDocument.createTextNode(right)
|
||||||
return newNode
|
return newNode
|
||||||
|
|
||||||
|
|
||||||
# ProcessingInstruction
|
# ProcessingInstruction
|
||||||
proc target*(pi: PProcessingInstruction): string =
|
proc target*(pi: PProcessingInstruction): string =
|
||||||
## Returns the Processing Instructions target
|
## Returns the Processing Instructions target
|
||||||
|
|
||||||
return pi.fTarget
|
return pi.fTarget
|
||||||
|
|
||||||
|
proc escapeXml*(s: string; result: var string) =
|
||||||
# --Other stuff--
|
## Prepares a string for insertion into a XML document
|
||||||
# Writer
|
## by escaping the XML special characters.
|
||||||
proc addEscaped(s: string): string =
|
|
||||||
result = ""
|
result = ""
|
||||||
for c in items(s):
|
for c in items(s):
|
||||||
case c
|
case c
|
||||||
|
|
@ -1089,11 +1087,20 @@ proc addEscaped(s: string): string =
|
||||||
of '"': result.add(""")
|
of '"': result.add(""")
|
||||||
else: result.add(c)
|
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 =
|
proc nodeToXml(n: PNode, indent: int = 0): string =
|
||||||
result = spaces(indent) & "<" & n.nodeName
|
result = spaces(indent) & "<" & n.nodeName
|
||||||
if not isNil(n.attributes):
|
if not isNil(n.attributes):
|
||||||
for i in items(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:
|
if isNil(n.childNodes) or n.childNodes.len() == 0:
|
||||||
result.add("/>") # No idea why this doesn't need a \n :O
|
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))
|
result.add(nodeToXml(i, indent + 2))
|
||||||
of TextNode:
|
of TextNode:
|
||||||
result.add(spaces(indent * 2))
|
result.add(spaces(indent * 2))
|
||||||
result.add(addEscaped(i.nodeValue))
|
result.add(escapeXml(i.nodeValue))
|
||||||
of CDataSectionNode:
|
of CDataSectionNode:
|
||||||
result.add(spaces(indent * 2))
|
result.add(spaces(indent * 2))
|
||||||
result.add("<![CDATA[" & i.nodeValue & "]]>")
|
result.add("<![CDATA[" & i.nodeValue & "]]>")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue