Skip to content

Commit 96fb8c8

Browse files
committed
Fix AbsText vector
1 parent 4b3e680 commit 96fb8c8

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

src/xmlutils/findnode.jl

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,11 @@ function parse_textindices(indicesstr::String)
243243
else
244244
indicesExpr = Meta.parse(indicesstr)
245245
indicesExpr.head == :vect || error("give indices as a vetor e.g. [2:3], [2, 3] ,[:]")
246-
indices = eval(indexExpr)
246+
indices = eval(indicesExpr)
247+
# TODO find a better way
248+
if typeof(indices) <: Vector{UnitRange{Int64}}
249+
indices = indices[1]
250+
end
247251
end
248252
return indices
249253
end
@@ -270,14 +274,17 @@ function findalltext(indices::Colon, node::Node)
270274
end
271275

272276
function findalltext(indices::AbstractVector, node::Node)
273-
out = Node[]
274-
iText = 0
277+
out = Vector{Node}(undef, length(indices))
278+
iTextLocation = 1
279+
outIndex = 1
275280
for child in eachnode(node)
276281
if istext(child)
277-
iText +=1
278-
if iText in indices
279-
push!(out, child)
282+
if iTextLocation in indices
283+
out[outIndex] = child
280284
end
285+
outIndex += 1
286+
else
287+
iTextLocation +=1
281288
end
282289
end
283290
if !isempty(out)

test/xmlutils.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,26 @@ using AcuteML, Test
9999
updatecontent!(2, "", n, AbsText)
100100
@test 2 == findcontent(Int64, "", n, AbsText)
101101

102+
n = createnode(AbsNormal, "p")
103+
addnode!(n, "n1", "1", AbsNormal)
104+
addnode!(n, "n2", "2", AbsNormal)
105+
addnode!(n, "n3", "3", AbsNormal)
106+
addnode!(n, "n4", "4", AbsNormal)
107+
addnode!(n, "[2:3]", ["txt2", "txt3"], AbsText) # Adds text inside n1 and n2
108+
@test ["txt2", "txt3"] == findcontent(Vector{String}, "[2:3]", n, AbsText)
109+
updatecontent!(["txt2-u", "txt3-u"] , "[2:3]", n, AbsText)
110+
@test ["txt2-u", "txt3-u"] == findcontent(Vector{String}, "[2:3]", n, AbsText)
111+
# TODO will throw error if text nodes don't exist
112+
@test_broken updatecontent!(["txt1-uu", "txt2-uu"] , "[1:2]", n, AbsText)
113+
@test_broken ["txt1-uu", "txt2-uu"] == findcontent(Vector{String}, "[1:2]", n, AbsText)
114+
@test_broken ["txt1-uu", "txt2-uu", "txt3-u"] == findcontent(Vector{String}, "[1:2]", n, AbsText)
115+
116+
n = createnode(AbsNormal, "a")
117+
addnode!(n, "1", 1, AbsText)
118+
@test 1 == findcontent(Int64, "1", n, AbsText)
119+
updatecontent!(2, "", n, AbsText)
120+
@test 2 == findcontent(Int64, "", n, AbsText)
121+
102122

103123
@testset "Node link" begin
104124
# Linking two nodes

0 commit comments

Comments
 (0)