Skip to content

Commit 0d31333

Browse files
committed
Add Base.copy(::Node) and update docs and tests
1 parent 36b0644 commit 0d31333

File tree

3 files changed

+52
-7
lines changed

3 files changed

+52
-7
lines changed

README.md

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,18 +99,49 @@ node["key"] = value
9999
node["key"]
100100
```
101101

102-
- `Node` is an immutable type. However, you can easily create a copy with one or more field values changed by using the `Node(::Node; kw...)` constructor where `kw` are the fields you want to change. For example:
102+
- `Node` is an immutable type. However, you can easily create a simple copy using `copy(::Node)` or
103+
copy with one or more changes by using the `Node(::Node, atts...; kw...)` constructor where `atts`
104+
are new children to add and `kw` are the node attributes you want to add or change. For example:
103105

104106
```julia
105-
node = XML.Element("tag", XML.Text("child"))
107+
julia> node = XML.Element("tag", XML.Text("child"))
108+
Node Element <tag> (1 child)
106109

107-
simple_value(node)
108-
# "child"
110+
julia> simple_value(node)
111+
"child"
109112

110-
node2 = Node(node, children=XML.Text("changed"))
113+
julia> node2 = Node(node, new_att="I'm new")
114+
Node Element <tag new_att="I'm new"> (1 child)
111115

112-
simple_value(node2)
113-
# "changed"
116+
julia> node3 = Node(node2, XML.Element("new_child", child_att="I'm new too"))
117+
Node Element <tag new_att="I'm new"> (2 children)
118+
119+
julia> println(XML.write(node))
120+
<tag>child</tag>
121+
122+
julia> println(XML.write(node2))
123+
<tag new_att="I'm new">child</tag>
124+
125+
julia> println(XML.write(node3))
126+
<tag new_att="I'm new">
127+
child
128+
<new_child child_att="I'm new too"/>
129+
</tag>
130+
131+
julia> node4=copy(node3)
132+
Node Element <tag new_att="I'm new"> (2 children)
133+
134+
julia> println(XML.write(node4))
135+
<tag new_att="I'm new">
136+
child
137+
<new_child child_att="I'm new too"/>
138+
</tag>
139+
140+
julia> node4==node3
141+
true
142+
143+
julia> node4===node3
144+
false
114145
```
115146

116147
### Writing `Element` `Node`s with `XML.h`

src/XML.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,10 @@ simple_value(o) = is_simple(o) ? value(only(o)) : error("`XML.simple_value` is o
276276

277277
Base.@deprecate_binding simplevalue simple_value
278278

279+
#-----------------------------------------------------------------------------# copy node
280+
281+
Base.copy(o::AbstractXMLNode) = Node(o.nodetype, o.tag, o.attributes, o.value, isnothing(o.children) ? nothing : [Base.copy(x) for x in o.children])
282+
279283
#-----------------------------------------------------------------------------# nodes_equal
280284
function nodes_equal(a, b)
281285
out = XML.tag(a) == XML.tag(b)

test/runtests.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,16 @@ end
174174
end
175175
end
176176

177+
#-----------------------------------------------------------------------------# copy
178+
@testset "Copy node" begin
179+
for path in all_files
180+
node = read(path, Node)
181+
node2 = copy(node)
182+
@test node == node2
183+
@test node2==deepcopy(node)
184+
end
185+
end
186+
177187
#-----------------------------------------------------------------------------# roundtrip
178188
@testset "read/write/read roundtrip" begin
179189
for path in all_files

0 commit comments

Comments
 (0)