Skip to content

Commit 0c220f8

Browse files
committed
Change attribute internal representation from Dict to OrderedDict
1 parent bfb6a30 commit 0c220f8

File tree

5 files changed

+18
-14
lines changed

5 files changed

+18
-14
lines changed

Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ version = "0.2.3"
55

66
[deps]
77
Mmap = "a63ad114-7e13-5084-954f-fe012c677804"
8+
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
89

910
[compat]
1011
julia = "1.7"
12+
OrderedCollections = "1.4, 1.5"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ This package offers fast data structures for reading and writing XML files with
1717
```
1818
nodetype(node) → XML.NodeType (an enum type)
1919
tag(node) → String or Nothing
20-
attributes(node) → Dict{String, String} or Nothing
20+
attributes(node) → OrderedDict{String, String} or Nothing
2121
value(node) → String or Nothing
2222
children(node) → Vector{typeof(node)}
2323
is_simple(node) → Bool (whether node is simple .e.g. <tag>item</tag>)

src/XML.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module XML
22

33
using Mmap
4+
using OrderedCollections: OrderedDict
45

56
export
67
# Core Types:
@@ -71,7 +72,7 @@ A Lazy representation of an XML node.
7172
mutable struct LazyNode <: AbstractXMLNode
7273
raw::Raw
7374
tag::Union{Nothing, String}
74-
attributes::Union{Nothing, Dict{String, String}}
75+
attributes::Union{Nothing, OrderedDict{String, String}}
7576
value::Union{Nothing, String}
7677
end
7778
LazyNode(raw::Raw) = LazyNode(raw, nothing, nothing, nothing)
@@ -128,14 +129,14 @@ A representation of an XML DOM node. For simpler construction, use `(::NodeType
128129
struct Node <: AbstractXMLNode
129130
nodetype::NodeType
130131
tag::Union{Nothing, String}
131-
attributes::Union{Nothing, Dict{String, String}}
132+
attributes::Union{Nothing, OrderedDict{String, String}}
132133
value::Union{Nothing, String}
133134
children::Union{Nothing, Vector{Node}}
134135

135136
function Node(nodetype::NodeType, tag=nothing, attributes=nothing, value=nothing, children=nothing)
136137
new(nodetype,
137138
isnothing(tag) ? nothing : string(tag),
138-
isnothing(attributes) ? nothing : Dict(string(k) => string(v) for (k, v) in pairs(attributes)),
139+
isnothing(attributes) ? nothing : OrderedDict(string(k) => string(v) for (k, v) in pairs(attributes)),
139140
isnothing(value) ? nothing : string(value),
140141
isnothing(children) ? nothing :
141142
children isa Node ? [children] :

src/raw.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Useful functions:
5252
- next(o::Raw) --> Raw of the next chunk (or `nothing`).
5353
- prev(o::Raw) --> Raw of the previous chunk (or `nothing`).
5454
- tag(o::Raw) --> String of the tag name (or `nothing`).
55-
- attributes(o::Raw) --> Dict{String, String} of the attributes (or `nothing`).
55+
- attributes(o::Raw) --> OrderedDict{String, String} of the attributes (or `nothing`).
5656
- value(o::Raw) --> String of the value (or `nothing`).
5757
- children(o::Raw) --> Vector{Raw} of the children (or `nothing`).
5858
- parent(o::Raw) --> Raw of the parent (or `nothing`)
@@ -118,7 +118,7 @@ end
118118
function get_attributes(data, i, j)
119119
i = name_start(data, i)
120120
i > j && return nothing
121-
out = Dict{String, String}()
121+
out = OrderedDict{String, String}()
122122
while !isnothing(i) && i < j
123123
key, i = get_name(data, i)
124124
# get quotechar the value is wrapped in (either ' or ")
@@ -152,7 +152,7 @@ function tag(o::Raw)
152152
end
153153

154154
"""
155-
attributes(node) --> Dict{String, String} or Nothing
155+
attributes(node) --> OrderedDict{String, String} or Nothing
156156
157157
Return the attributes of `Element`, `Declaration`, or `ProcessingInstruction` nodes.
158158
"""

test/runtests.jl

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using XML
2-
using XML: Document, Element, Declaration, Comment, CData, DTD, ProcessingInstruction, Text, escape, unescape
2+
using XML: Document, Element, Declaration, Comment, CData, DTD, ProcessingInstruction, Text, escape, unescape, OrderedDict
33
using Downloads: download
44
using Test
55
import AbstractTrees
@@ -200,7 +200,7 @@ end
200200

201201
#-----------------------------------------------------------------------------# Issues
202202
@testset "Issues" begin
203-
#12: DTD content was cut short
203+
# https://github.com/JuliaComputing/XML.jl/issues/12: DTD content was cut short
204204
s = """
205205
<!DOCTYPE note [
206206
<!ENTITY nbsp "&#xA0;">
@@ -210,9 +210,10 @@ end
210210
"""
211211

212212
doc = parse(Node, s)
213-
@test value(only(doc)) == """note [
214-
<!ENTITY nbsp "&#xA0;">
215-
<!ENTITY writer "Writer: Donald Duck.">
216-
<!ENTITY copyright "Copyright: W3Schools.">
217-
]"""
213+
@test value(only(doc)) == s[11:end-2] # note [...]
214+
215+
# https://github.com/JuliaComputing/XML.jl/issues/14 (Sorted Attributes)
216+
kw = NamedTuple(OrderedDict(Symbol(k) => Int(k) for k in 'a':'z'))
217+
xyz = XML.Element("point"; kw...)
218+
@test collect(keys(attributes(xyz))) == string.(collect('a':'z'))
218219
end

0 commit comments

Comments
 (0)