Skip to content

Commit 143c2b2

Browse files
committed
smarter escaping
1 parent 47b89e3 commit 143c2b2

File tree

1 file changed

+1
-15
lines changed

1 file changed

+1
-15
lines changed

src/XML.jl

+1-15
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,9 @@ export
1212
parent, depth, next, prev
1313

1414
#-----------------------------------------------------------------------------# escape/unescape
15-
# TODO: be smarter :
16-
# 1. to avoid escaping entities e.g. `&entity;`
17-
# 2. to avoid re-escaping already escaped entities e.g. `&`
1815
const escape_chars = ('&' => "&amp;", '<' => "&lt;", '>' => "&gt;", "'" => "&apos;", '"' => "&quot;")
19-
2016
unescape(x::AbstractString) = replace(x, reverse.(escape_chars)...)
21-
22-
# requires special handling of `&` to avoid double-escaping
23-
function escape(x::String)
24-
s = replace(x,
25-
r"&(?!(?:amp|lt|gt|quot|apos);)" => "&amp;",
26-
'<' => "&lt;", '>' => "&gt;",
27-
'"' => "&quot;",
28-
''' => "&apos;"
29-
)
30-
return s
31-
end
17+
escape(x::String) = replace(x, r"&(?=\s)" => "&amp;", escape_chars[2:end]...)
3218

3319
#-----------------------------------------------------------------------------# NodeType
3420
"""

0 commit comments

Comments
 (0)