mirror of
https://github.com/nim-lang/Nim.git
synced 2026-02-15 07:43:26 +00:00
Merge pull request #4717 from nigredo-tori/xml-escaping
xmltree: separate escaping for attributes
This commit is contained in:
@@ -226,6 +226,18 @@ proc noWhitespace(n: XmlNode): bool =
|
||||
|
||||
proc add*(result: var string, n: XmlNode, indent = 0, indWidth = 2) =
|
||||
## 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
|
||||
case n.k
|
||||
of xnElement:
|
||||
@@ -236,7 +248,7 @@ proc add*(result: var string, n: XmlNode, indent = 0, indWidth = 2) =
|
||||
result.add(' ')
|
||||
result.add(key)
|
||||
result.add("=\"")
|
||||
result.addEscaped(val)
|
||||
result.addEscapedAttr(val)
|
||||
result.add('"')
|
||||
if n.len > 0:
|
||||
result.add('>')
|
||||
@@ -381,6 +393,5 @@ proc findAll*(n: XmlNode, tag: string): seq[XmlNode] =
|
||||
findAll(n, tag, result)
|
||||
|
||||
when isMainModule:
|
||||
let link = "http://nim-lang.org"
|
||||
assert """<a href="""" & escape(link) & """">Nim rules.</a>""" ==
|
||||
assert """<a href="http://nim-lang.org">Nim rules.</a>""" ==
|
||||
$(<>a(href="http://nim-lang.org", newText("Nim rules.")))
|
||||
|
||||
Reference in New Issue
Block a user