Skip to content

Commit 7493daf

Browse files
committed
Fix depth and parent functions
1 parent ff0ee25 commit 7493daf

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

src/XML.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ attributes(o) = o.attributes
257257
value(o) = o.value
258258
children(o::T) where {T} = isnothing(o.children) ? () : o.children
259259

260-
depth(o) = 1
260+
depth(o) = missing
261261
parent(o) = missing
262262
next(o) = missing
263263
prev(o) = missing
@@ -357,7 +357,7 @@ write(x; kw...) = (io = IOBuffer(); write(io, x; kw...); String(take!(io)))
357357

358358
write(filename::AbstractString, x; kw...) = open(io -> write(io, x; kw...), filename, "w")
359359

360-
function write(io::IO, x; indentsize::Int=2, depth::Int=depth(x))
360+
function write(io::IO, x; indentsize::Int=2, depth::Int=1)
361361
indent = ' ' ^ indentsize
362362
nodetype = XML.nodetype(x)
363363
tag = XML.tag(x)

src/raw.jl

+10-1
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,23 @@ function children(o::Raw)
208208
end
209209
end
210210

211+
"""
212+
depth(node) --> Int
213+
214+
Return the depth of the node. Will be `0` for `Document` nodes. Not defined for `XML.Node`.
215+
"""
216+
function depth(o::Raw)
217+
o.depth
218+
end
219+
211220
"""
212221
parent(node) --> typeof(node), Nothing
213222
214223
Return the parent of the node. Will be `nothing` for `Document` nodes. Not defined for `XML.Node`.
215224
"""
216225
function parent(o::Raw)
217226
depth = o.depth
218-
depth === 1 && return nothing
227+
depth === 0 && return nothing
219228
p = prev(o)
220229
while p.depth >= depth
221230
p = prev(p)

test/runtests.jl

+12
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,18 @@ end
134134
end
135135
end
136136

137+
@testset "depth and parent" begin
138+
@test XML.depth(data) == 0
139+
@test isnothing(XML.parent(data))
140+
@test XML.depth(doc[1]) == 1
141+
@test XML.parent(doc[1]) == data
142+
@test XML.depth(doc[2]) == 1
143+
@test XML.depth(doc[3]) == 2
144+
@test XML.parent(doc[3]) == doc[2]
145+
@test XML.depth(doc[end]) == 1
146+
@test XML.parent(doc[end]) == data
147+
end
148+
137149
@testset "tag/attributes/value" begin
138150
x = doc[1] # <?xml version="1.0"?>
139151
@test XML.tag(x) === nothing

0 commit comments

Comments
 (0)