bugfixes: re; pegs module

This commit is contained in:
Andreas Rumpf 2010-02-28 18:14:27 +01:00
commit ddb7185482
13 changed files with 97 additions and 72 deletions

View file

@ -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("&lt;")
of '>': result.add("&gt;")
of '&': result.add("&amp;")
of '"': result.add("&quot;")
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))
result.add(nodeToXml(doc.documentElement))