Skip to content

Commit 8f3d85a

Browse files
committed
tests looking good
1 parent e56b407 commit 8f3d85a

File tree

3 files changed

+82
-9
lines changed

3 files changed

+82
-9
lines changed

README.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ doc[end][2] # Second child of root
4343

4444
## Data Structures
4545

46-
**`XML.Node`**
46+
### `XML.Node`
4747
- An eager data structure that loads the entire XML DOM in memory.
4848
- **This is what you should use to build an XML document programmatically.**
4949
- `Node`s have some additional methods that aid in construction/mutation:
@@ -65,11 +65,21 @@ parent[2] = child
6565
Node(node; kw...)
6666
```
6767

68-
**`XML.RowNode`**
68+
- Bring convenience functions into your namespace with `using XML.NodeConstructors`:
69+
70+
```julia
71+
using XML.NodeConstructors
72+
# cdata, comment, declaration, document, dtd, element, processing_instruction, text
73+
74+
cdata("hello > < ' \" I have odd characters")
75+
# Node CDATA <![CDATA[hello > < ' " I have odd characters]]>
76+
```
77+
78+
### `XML.RowNode`
6979
- A data structure that can used as a *Tables.jl* source. It is only lazy in how it access its children.
7080

7181

72-
**`XML.RawData`**
82+
### `XML.RawData`
7383
- A super lazy data structure that holds the reference `Vector{UInt8}` data and the sta
7484

7585
## Reading

src/XML.jl

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ Base.show(io::IO, o::Node) = _show_node(io, o)
518518

519519
#-----------------------------------------------------------------------------# Node Constructors
520520
# auto-detect how to create a Node
521-
_node(x; depth=-1) = Node(text(string(x)); depth)
521+
_node(x; depth=-1) = Node(nodetype=TEXT, value=string(x); depth)
522522
_node(x::Node; depth=x.depth) = Node(x; depth)
523523

524524
module NodeConstructors
@@ -530,26 +530,69 @@ export document, dtd, declaration, processing_instruction, comment, cdata, text,
530530

531531
attrs(kw) = OrderedDict{String,String}(string(k) => string(v) for (k,v) in kw)
532532

533-
document(children::Vector{Node}) = Node(nodetype=DOCUMENT, children)
534-
document(children::Node...) = document(Node.(children))
533+
"""
534+
document(children::Vector{Node})
535+
document(children::Node...)
536+
"""
537+
document(children::Vector{Node}) = Node(;nodetype=DOCUMENT, children)
538+
document(children::Node...) = document(collect(children))
535539

540+
"""
541+
dtd(value::AbstractString)
542+
"""
536543
dtd(value::AbstractString) = Node(nodetype=DTD, value=String(value))
537544

545+
"""
546+
declaration(; attributes...)
547+
"""
538548
declaration(attributes::OrderedDict{String,String}) = Node(;nodetype=DECLARATION, attributes)
539549
declaration(; kw...) = declaration(attrs(kw))
540550

551+
"""
552+
processing_instruction(tag::AbstractString; attributes...)
553+
"""
541554
processing_instruction(tag, attributes::OrderedDict{String,String}) = Node(;nodetype=PROCESSING_INSTRUCTION, tag=string(tag), attributes)
542555
processing_instruction(tag; kw...) = processing_instruction(tag, attrs(kw))
543556

557+
"""
558+
comment(value::AbstractString)
559+
"""
544560
comment(value::AbstractString) = Node(nodetype=COMMENT, value=String(value))
545561

562+
"""
563+
cdata(value::AbstractString)
564+
"""
546565
cdata(value::AbstractString) = Node(nodetype=CDATA, value=String(value))
547566

567+
"""
568+
text(value::AbstractString)
569+
"""
548570
text(value::AbstractString) = Node(nodetype=TEXT, value=String(value))
549571

572+
"""
573+
element(tag::AbstractString, children::Vector{Node}; attributes...)
574+
element(tag::AbstractString, children::Node...; attributes...)
575+
576+
Example:
550577
551-
element(tag, children...; kw...) = Node(; nodetype=ELEMENT, tag=string(tag), attributes=attrs(kw))(children...)
552-
Base.getproperty(::typeof(element), tag::Symbol) = element(string(tag))
578+
using XML.NodeConstructors
579+
580+
n = element("tag", "child"; key="value")
581+
# Node ELEMENT <tag key="value"> (1 child)
582+
583+
only(n)
584+
# Node TEXT "child"
585+
586+
push!(n, cdata("hello > < ' \" I have odd characters"))
587+
588+
children(n)
589+
# 2-element Vector{Node}:
590+
# Node TEXT "child"
591+
# Node CDATA <![CDATA[hello > < ' " I have odd characters]]>
592+
"""
593+
element(tag, children...; kw...) = Node(; nodetype=ELEMENT, tag=string(tag), attributes=attrs(kw))(_node.(children)...)
594+
element(tag, children::Vector{Node}; kw...) = element(tag, children...; kw...)
595+
# Base.getproperty(::typeof(element), tag::Symbol) = element(string(tag))
553596
end
554597

555598

test/runtests.jl

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using XML
22
using Downloads: download
33
using Test
4-
using AbstractTrees
54
using OrderedCollections
65

76
#-----------------------------------------------------------------------------# files
@@ -150,5 +149,26 @@ end
150149
end
151150

152151
#-----------------------------------------------------------------------------# Node writing
152+
using XML.NodeConstructors
153+
153154
@testset "Node writing" begin
155+
doc = document(
156+
dtd("root_tag"),
157+
declaration(version=1.0),
158+
comment("comment"),
159+
processing_instruction("xml-stylesheet", href="mystyle.css", type="text/css"),
160+
element("root_tag", cdata("cdata"), text("text"))
161+
)
162+
@test map(nodetype, children(doc)) == [
163+
XML.DTD,
164+
XML.DECLARATION,
165+
XML.COMMENT,
166+
XML.PROCESSING_INSTRUCTION,
167+
XML.ELEMENT
168+
]
169+
@test length(children(doc[end])) == 2
170+
@test nodetype(doc[end][1]) == XML.CDATA
171+
@test nodetype(doc[end][2]) == XML.TEXT
172+
@test value(doc[end][1]) == "cdata"
173+
@test value(doc[end][2]) == "text"
154174
end

0 commit comments

Comments
 (0)