|
28 | 28 | x === RawDocument ? Document :
|
29 | 29 | nothing
|
30 | 30 |
|
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 |
35 | 35 |
|
36 | 36 | #-----------------------------------------------------------------------------# Raw
|
37 | 37 | """
|
@@ -69,9 +69,9 @@ struct Raw
|
69 | 69 | pos::Int
|
70 | 70 | len::Int
|
71 | 71 | data::Vector{UInt8}
|
72 |
| - ctx::XMLSpaceContext |
| 72 | + ctx::Vector{Bool} # Context for xml:space (Vector so mutable) |
73 | 73 | 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) |
75 | 75 |
|
76 | 76 |
|
77 | 77 | Base.read(filename::String, ::Type{Raw}) = isfile(filename) ?
|
@@ -169,25 +169,17 @@ function attributes(o::Raw)
|
169 | 169 | i = name_start(o.data, i)
|
170 | 170 | i = name_stop(o.data, i)
|
171 | 171 | 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") |
173 | 173 | # If xml:space attribute is present, we need to preserve whitespace
|
174 | 174 | if out["xml:space"] == "preserve"
|
175 |
| - push!(o.ctx.preserve_space, true) |
| 175 | + o.ctx[1]= true |
176 | 176 | elseif out["xml:space"] == "default"
|
177 |
| - push!(o.ctx.preserve_space, false) |
| 177 | + o.ctx[1] = false |
178 | 178 | else
|
179 | 179 | error("Invalid value for xml:space attribute: $(out["xml:space"]). Must be 'preserve' or 'default'.")
|
180 | 180 | end
|
181 | 181 | end
|
182 | 182 | 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 |
191 | 183 | elseif o.type === RawDeclaration
|
192 | 184 | get_attributes(o.data, o.pos + 6, o.pos + o.len)
|
193 | 185 | else
|
@@ -225,11 +217,7 @@ function children(o::Raw)
|
225 | 217 | out = Raw[]
|
226 | 218 | for item in xml_nodes(o)
|
227 | 219 | 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 |
233 | 221 | o.type==RawElementOpen && attributes(item)
|
234 | 222 | push!(out, item)
|
235 | 223 | end
|
@@ -284,20 +272,19 @@ function next(o::Raw)
|
284 | 272 | ctx = o.ctx
|
285 | 273 | k = findnext(!isspace, data, i)
|
286 | 274 | 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 |
288 | 275 | return nothing
|
289 | 276 | end
|
290 |
| - i = length(ctx.preserve_space) == 0 || !(ctx.preserve_space[end]) ? k : i |
| 277 | + i = (ctx[1]) ? i : k |
291 | 278 | j = i + 1
|
292 | 279 | c = Char(o.data[k])
|
293 | 280 | d = Char(o.data[k+1])
|
294 | 281 | if type === RawElementOpen || type === RawDocument
|
295 | 282 | depth += 1
|
296 | 283 | end
|
297 |
| - if c !== '<' || type === RawElementOpen && d === '/' && length(ctx.preserve_space) > 0 && (ctx.preserve_space[end]) |
| 284 | + if c !== '<' || type === RawElementOpen && d === '/' && (ctx[1]) |
298 | 285 | type = RawText
|
299 | 286 | 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 |
301 | 288 | else
|
302 | 289 | i=k
|
303 | 290 | j=k+1
|
@@ -357,18 +344,17 @@ function prev(o::Raw)
|
357 | 344 | j = o.pos - 1
|
358 | 345 | k = findprev(!isspace, data, j)
|
359 | 346 | 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 |
361 | 347 | return Raw(data, ctx) # RawDocument
|
362 | 348 | end
|
363 |
| - j = length(ctx.preserve_space) == 0 || !(ctx.preserve_space[end]) ? k : j |
| 349 | + j = (ctx[1]) ? j : k |
364 | 350 | c = Char(o.data[j])
|
365 | 351 | d = Char(data[findprev(==(UInt8('<')), data, j)+1])
|
366 | 352 | i = j - 1
|
367 | 353 | 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 |
369 | 355 | type = RawText
|
370 | 356 | 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 |
372 | 358 | else
|
373 | 359 | j=k
|
374 | 360 | i=k-1
|
|
0 commit comments