Skip to content

Commit 7a5f85c

Browse files
committed
unescape!
1 parent 7d452d1 commit 7a5f85c

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,6 @@ Platform Info:
199199

200200
# Possible Gotchas
201201

202-
XML.jl doesn't escape special characters (`<`, `>`, `&`, `"`, and `'` ) for you, but it provides `XML.escape(::String)` and `XML.unescape(::String)` utility functions.
202+
- XML.jl doesn't automatically escape special characters (`<`, `>`, `&`, `"`, and `'` ) for you.
203+
- Use the `XML.escape(::String)` and `XML.unescape(::String)` utility functions to convert strings back and forth.
204+
- You can also use `XML.escape!(::Node)` which will escape the strings of all child `Text` nodes.

src/XML.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,15 @@ function escape!(o::Node, warn::Bool=true)
166166
map!(x -> escape!(x, false), o.children, o.children)
167167
o
168168
end
169+
function unescape!(o::Node, warn::Bool=true)
170+
if o.nodetype == Text
171+
warn && @warn "unescape!() called on a Text Node creates a new node."
172+
return Text(unescape(o.value))
173+
end
174+
isnothing(o.children) && return o
175+
map!(x -> unescape!(x, false), o.children, o.children)
176+
o
177+
end
169178

170179

171180
Base.read(filename::AbstractString, ::Type{Node}) = Node(read(filename, Raw))

test/runtests.jl

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,22 @@ all_files = [
1919
"example.kml" => example_kml
2020
]
2121

22-
#-----------------------------------------------------------------------------# utils
23-
@testset "utils" begin
22+
#-----------------------------------------------------------------------------# escaping/unescaping
23+
@testset "escaping/unescaping" begin
2424
s = "This > string < has & some \" special ' characters"
2525
@test escape(s) == "This &gt; string &lt; has &amp; some &quot; special &apos; characters"
2626
@test escape(escape(s)) == escape(s)
2727
@test s == unescape(escape(s))
2828
@test s == unescape(unescape(escape(s)))
29+
30+
n = Element("tag", Text(s))
31+
@test XML.simplevalue(n) == s
32+
33+
XML.escape!(n)
34+
@test XML.simplevalue(n) == escape(s)
35+
36+
XML.unescape!(n)
37+
@test XML.simplevalue(n) == s
2938
end
3039

3140
#-----------------------------------------------------------------------------# Raw

0 commit comments

Comments
 (0)