diff options
Diffstat (limited to 'lib/pure/xmldom.nim')
-rwxr-xr-x | lib/pure/xmldom.nim | 16 |
1 files changed, 13 insertions, 3 deletions
diff --git a/lib/pure/xmldom.nim b/lib/pure/xmldom.nim index babf60108..b7ee165f5 100755 --- a/lib/pure/xmldom.nim +++ b/lib/pure/xmldom.nim @@ -1044,10 +1044,20 @@ proc target*(PI: PProcessingInstruction): string = # --Other stuff-- # Writer +proc addEscaped(s: string): string = + result = "" + for c in items(s): + case c + of '<': result.add("<") + of '>': result.add(">") + of '&': result.add("&") + of '"': result.add(""") + else: result.add(c) + proc nodeToXml(n: PNode, indent: int = 0): string = result = repeatChar(indent, ' ') & "<" & n.nodeName for i in items(n.Attributes): - result.add(" " & i.name & "=\"" & i.value & "\"") + result.add(" " & i.name & "=\"" & addEscaped(i.value) & "\"") if n.childNodes.len() == 0: result.add("/>") # No idea why this doesn't need a \n :O @@ -1060,7 +1070,7 @@ proc nodeToXml(n: PNode, indent: int = 0): string = result.add(nodeToXml(i, indent + 2)) of TextNode: result.add(repeatChar(indent * 2, ' ')) - result.add(i.nodeValue) + result.add(addEscaped(i.nodeValue)) of CDataSectionNode: result.add(repeatChar(indent * 2, ' ')) result.add("<![CDATA[" & i.nodeValue & "]]>") @@ -1080,4 +1090,4 @@ proc nodeToXml(n: PNode, indent: int = 0): string = proc `$`*(doc: PDocument): string = ## Converts a PDocument object into a string representation of it's XML result = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" - result.add(nodeToXml(doc.documentElement)) \ No newline at end of file + result.add(nodeToXml(doc.documentElement)) |