xmltree: separate escaping for attributes
This commit is contained in:
parent
147c257720
commit
8c9e2d7a43
1 changed files with 14 additions and 3 deletions
|
|
@ -226,6 +226,18 @@ proc noWhitespace(n: XmlNode): bool =
|
||||||
|
|
||||||
proc add*(result: var string, n: XmlNode, indent = 0, indWidth = 2) =
|
proc add*(result: var string, n: XmlNode, indent = 0, indWidth = 2) =
|
||||||
## adds the textual representation of `n` to `result`.
|
## adds the textual representation of `n` to `result`.
|
||||||
|
|
||||||
|
proc addEscapedAttr(result: var string, s: string) =
|
||||||
|
# `addEscaped` alternative with less escaped characters.
|
||||||
|
# Only to be used for escaping attribute values enclosed in double quotes!
|
||||||
|
for c in items(s):
|
||||||
|
case c
|
||||||
|
of '<': result.add("<")
|
||||||
|
of '>': result.add(">")
|
||||||
|
of '&': result.add("&")
|
||||||
|
of '"': result.add(""")
|
||||||
|
else: result.add(c)
|
||||||
|
|
||||||
if n == nil: return
|
if n == nil: return
|
||||||
case n.k
|
case n.k
|
||||||
of xnElement:
|
of xnElement:
|
||||||
|
|
@ -236,7 +248,7 @@ proc add*(result: var string, n: XmlNode, indent = 0, indWidth = 2) =
|
||||||
result.add(' ')
|
result.add(' ')
|
||||||
result.add(key)
|
result.add(key)
|
||||||
result.add("=\"")
|
result.add("=\"")
|
||||||
result.addEscaped(val)
|
result.addEscapedAttr(val)
|
||||||
result.add('"')
|
result.add('"')
|
||||||
if n.len > 0:
|
if n.len > 0:
|
||||||
result.add('>')
|
result.add('>')
|
||||||
|
|
@ -381,6 +393,5 @@ proc findAll*(n: XmlNode, tag: string): seq[XmlNode] =
|
||||||
findAll(n, tag, result)
|
findAll(n, tag, result)
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
let link = "http://nim-lang.org"
|
assert """<a href="http://nim-lang.org">Nim rules.</a>""" ==
|
||||||
assert """<a href="""" & escape(link) & """">Nim rules.</a>""" ==
|
|
||||||
$(<>a(href="http://nim-lang.org", newText("Nim rules.")))
|
$(<>a(href="http://nim-lang.org", newText("Nim rules.")))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue