Skip to content

Commit 35f6ed8

Browse files
committed
After hours tidy-up of superfluous code!
1 parent bf1a5cd commit 35f6ed8

File tree

1 file changed

+16
-30
lines changed

1 file changed

+16
-30
lines changed

src/raw.jl

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@
2828
x === RawDocument ? Document :
2929
nothing
3030

31-
struct XMLSpaceContext
32-
preserve_space::Vector{Bool} # Stack to track xml:space state
33-
end
34-
XMLSpaceContext() = XMLSpaceContext([false]) # Default is not preserving
31+
#struct XMLSpaceContext
32+
# preserve_space::Vector{Bool} # Stack to track xml:space state
33+
#end
34+
#XMLSpaceContext() = XMLSpaceContext([false]) # Default is not preserving
3535

3636
#-----------------------------------------------------------------------------# Raw
3737
"""
@@ -69,9 +69,9 @@ struct Raw
6969
pos::Int
7070
len::Int
7171
data::Vector{UInt8}
72-
ctx::XMLSpaceContext
72+
ctx::Vector{Bool} # Context for xml:space (Vector so mutable)
7373
end
74-
Raw(data::Vector{UInt8}, ctx=XMLSpaceContext()) = Raw(RawDocument, 0, 0, 0, data, ctx)
74+
Raw(data::Vector{UInt8}, ctx=[false]) = Raw(RawDocument, 0, 0, 0, data, ctx)
7575

7676

7777
Base.read(filename::String, ::Type{Raw}) = isfile(filename) ?
@@ -169,25 +169,17 @@ function attributes(o::Raw)
169169
i = name_start(o.data, i)
170170
i = name_stop(o.data, i)
171171
out=get_attributes(o.data, i + 1, o.pos + o.len)
172-
if !isnothing(out) && haskey(out, "xml:space")
172+
if o.type === RawElementOpen && !isnothing(out) && haskey(out, "xml:space")
173173
# If xml:space attribute is present, we need to preserve whitespace
174174
if out["xml:space"] == "preserve"
175-
push!(o.ctx.preserve_space, true)
175+
o.ctx[1]= true
176176
elseif out["xml:space"] == "default"
177-
push!(o.ctx.preserve_space, false)
177+
o.ctx[1] = false
178178
else
179179
error("Invalid value for xml:space attribute: $(out["xml:space"]). Must be 'preserve' or 'default'.")
180180
end
181181
end
182182
out
183-
184-
elseif o.type === RawText
185-
if length(o.ctx.preserve_space)>0
186-
push!(o.ctx.preserve_space, o.ctx.preserve_space[end])
187-
else
188-
push!(o.ctx.preserve_space, false)
189-
end
190-
nothing
191183
elseif o.type === RawDeclaration
192184
get_attributes(o.data, o.pos + 6, o.pos + o.len)
193185
else
@@ -225,11 +217,7 @@ function children(o::Raw)
225217
out = Raw[]
226218
for item in xml_nodes(o)
227219
if item.depth == depth + 1
228-
if length(item.ctx.preserve_space) > 0
229-
item.ctx.preserve_space[1] = o.ctx.preserve_space[end] # inherit the context
230-
else
231-
push!(item.ctx.preserve_space, false)
232-
end
220+
item.ctx[1] = o.ctx[1] # inherit the context
233221
o.type==RawElementOpen && attributes(item)
234222
push!(out, item)
235223
end
@@ -284,20 +272,19 @@ function next(o::Raw)
284272
ctx = o.ctx
285273
k = findnext(!isspace, data, i)
286274
if (isnothing(k) || length(String(o.data[o.pos + o.len + 1:end]))==0)
287-
length(ctx.preserve_space)>0 && pop!(ctx.preserve_space) # pop the previous context
288275
return nothing
289276
end
290-
i = length(ctx.preserve_space) == 0 || !(ctx.preserve_space[end]) ? k : i
277+
i = (ctx[1]) ? i : k
291278
j = i + 1
292279
c = Char(o.data[k])
293280
d = Char(o.data[k+1])
294281
if type === RawElementOpen || type === RawDocument
295282
depth += 1
296283
end
297-
if c !== '<' || type === RawElementOpen && d === '/' && length(ctx.preserve_space) > 0 && (ctx.preserve_space[end])
284+
if c !== '<' || type === RawElementOpen && d === '/' && (ctx[1])
298285
type = RawText
299286
j = findnext(==(UInt8('<')), data, i) - 1
300-
j = length(ctx.preserve_space) == 0 || !(ctx.preserve_space[end]) ? findprev(!isspace, data, j) : j # preserving whitespace if needed
287+
j = (ctx[1]) ? j : findprev(!isspace, data, j) # preserving whitespace if needed
301288
else
302289
i=k
303290
j=k+1
@@ -357,18 +344,17 @@ function prev(o::Raw)
357344
j = o.pos - 1
358345
k = findprev(!isspace, data, j)
359346
if isnothing(k) || length(String(o.data[o.pos + o.len + 1:end]))==0
360-
length(ctx.preserve_space)>0 && pop!(ctx.preserve_space) # pop the previous context
361347
return Raw(data, ctx) # RawDocument
362348
end
363-
j = length(ctx.preserve_space) == 0 || !(ctx.preserve_space[end]) ? k : j
349+
j = (ctx[1]) ? j : k
364350
c = Char(o.data[j])
365351
d = Char(data[findprev(==(UInt8('<')), data, j)+1])
366352
i = j - 1
367353
next_type = type
368-
if c !== '>' || type === RawElementClose && d !== '/' && length(ctx.preserve_space) > 0 && (ctx.preserve_space[end]) # text or empty whitespace
354+
if c !== '>' || type === RawElementClose && d !== '/' && (ctx[1]) # text or empty whitespace
369355
type = RawText
370356
i=findprev(==(UInt8('>')), data, j) + 1
371-
i = length(ctx.preserve_space) == 0 || !(ctx.preserve_space[end]) ? findprev(!isspace, data, i) : i # If preserving whitespace, retain leading and trailing whitespace
357+
i = (ctx[1]) ? i : findprev(!isspace, data, i) # If preserving whitespace, retain leading and trailing whitespace
372358
else
373359
j=k
374360
i=k-1

0 commit comments

Comments
 (0)