Skip to content

Commit 4d9f538

Browse files
committed
tests and remove keys/haskey methods
1 parent b92b11d commit 4d9f538

File tree

3 files changed

+43
-33
lines changed

3 files changed

+43
-33
lines changed

docs/make.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using Cobweb
2-
using Cobweb: h, Page, CSS
2+
using Cobweb: h, CSS
33
using Markdown
44

55
page = h.html(

src/Cobweb.jl

+24-29
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,6 @@ function preview(content; reuse=true)
1919
DefaultApplication.open(file)
2020
end
2121

22-
#-----------------------------------------------------------------------------# Page
23-
function Page(x)
24-
Base.depwarn("Page(x) is deprecated. Use preview(x) instead.", :Page; force=true)
25-
preview(x)
26-
end
27-
function Tab(x)
28-
Base.depwarn("Tab(x) is deprecated. Use preview(x; reuse=false) instead.", :Tab; force=true)
29-
preview(x; reuse=false)
30-
end
31-
3222
#-----------------------------------------------------------------------------# consts
3323
const HTML5_TAGS = [:a,:abbr,:address,:area,:article,:aside,:audio,:b,:base,:bdi,:bdo,:blockquote,:body,:br,:button,:canvas,:caption,:cite,:code,:col,:colgroup,:command,:datalist,:dd,:del,:details,:dfn,:dialog,:div,:dl,:dt,:em,:embed,:fieldset,:figcaption,:figure,:footer,:form,:h1,:h2,:h3,:h4,:h5,:h6,:head,:header,:hgroup,:hr,:html,:i,:iframe,:img,:input,:ins,:kbd,:label,:legend,:li,:link,:main,:map,:mark,:math,:menu,:menuitem,:meta,:meter,:nav,:noscript,:object,:ol,:optgroup,:option,:output,:p,:param,:picture,:pre,:progress,:q,:rb,:rp,:rt,:rtc,:ruby,:s,:samp,:script,:section,:select,:slot,:small,:source,:span,:strong,:style,:sub,:summary,:sup,:svg,:table,:tbody,:td,:template,:textarea,:tfoot,:th,:thead,:time,:title,:tr,:track,:u,:ul,:var,:video,:wbr]
3424

@@ -48,11 +38,14 @@ struct Node
4838
tag::Symbol
4939
attrs::OrderedDict{Symbol, String}
5040
children::Vector{Any}
41+
function Node(tag, attributes, children)
42+
sym = Symbol(tag)
43+
sym in HTML5_TAGS || @warn "<$tag> is not a valid HTML5 tag."
44+
new(sym, attrs(attributes), [children...])
45+
end
5146
end
52-
function Node(tag::Symbol, attrs::OrderedDict{Symbol, String}, children::AbstractVector)
53-
tag in HTML5_TAGS || @warn "<$tag> is not a valid HTML5 tag."
54-
Node(tag, attrs, collect(children))
55-
end
47+
48+
5649
tag(o::Node) = getfield(o, :tag)
5750
attrs(o::Node) = getfield(o, :attrs)
5851
children(o::Node) = getfield(o, :children)
@@ -67,13 +60,11 @@ Base.:(==)(a::Node, b::Node) = all(f(a) == f(b) for f in (tag, attrs, children))
6760
Base.getproperty(o::Node, class::String) = o(class = lstrip(get(o, :class, "") * " " * class))
6861

6962
# methods that pass through to attrs(o)
70-
Base.propertynames(o::Node) = Symbol.(keys(o))
63+
Base.propertynames(o::Node) = Symbol.(keys(attrs(o)))
7164
Base.getproperty(o::Node, name::Symbol) = attrs(o)[name]
7265
Base.setproperty!(o::Node, name::Symbol, x) = attrs(o)[name] = string(x)
73-
Base.get(o::Node, name, val) = get(attrs(o), string(name), string(val))
74-
Base.get!(o::Node, name, val) = get!(attrs(o), string(name), string(val))
75-
Base.haskey(o::Node, name) = haskey(attrs(o), string(name))
76-
Base.keys(o::Node) = keys(attrs(o))
66+
Base.get(o::Node, name, val) = get(attrs(o), Symbol(name), val)
67+
Base.get!(o::Node, name, val) = get!(attrs(o), Symbol(name), val)
7768

7869
# methods that pass through to children(o)
7970
Base.lastindex(o::Node) = lastindex(children(o))
@@ -161,7 +152,7 @@ Create an html node with the given `tag`, `children`, and `kw` attributes.
161152
h.div."myclass"("content")
162153
# <div class="myclass">content</div>
163154
"""
164-
h(tag, children...; kw...) = Node(tag, attrs(kw), collect(children))
155+
h(tag, children...; kw...) = Node(Symbol(tag), attrs(kw), collect(children))
165156

166157
h(tag, attrs::AbstractDict, children...) = Node(tag, attrs, collect(children))
167158

@@ -170,19 +161,23 @@ Base.getproperty(::typeof(h), tag::Symbol) = h(tag)
170161
Base.propertynames(::typeof(h)) = HTML5_TAGS
171162

172163
#-----------------------------------------------------------------------------# @h
164+
"""
165+
@h ex
166+
167+
Convert any valid HTML `<tag>` in `ex` to `Cobweb.h.<tag>`.
168+
169+
### Examples
170+
171+
@h div(p("This is a paragraph"), p("Here is some", strong("bold"), "text"))
172+
# <div><p>This is a paragraph</p><p>Here is some<strong>bold</strong>text</p></div>
173+
"""
173174
macro h(ex)
174175
esc(_h(ex))
175176
end
176177

177-
function _h(ex::Expr)
178-
for (i, x) in enumerate(ex.args)
179-
ex.args[i] = x isa Expr ? _h(x) :
180-
x isa Symbol && x in HTML5_TAGS ? Expr(:., :(Cobweb.h), QuoteNode(x)) :
181-
x
182-
end
183-
ex
184-
end
185-
_h(x::Symbol) = x in propertynames(typeof(h)) ? Expr(:., :(Cobweb.h), QuoteNode(x)) : x
178+
_h(ex::Expr) = (ex.args .= _h.(ex.args); return ex)
179+
_h(x::Symbol) = x in propertynames(h) ? Expr(:., :(Cobweb.h), QuoteNode(x)) : x
180+
_h(x) = x
186181

187182
#-----------------------------------------------------------------------------# escape
188183
escape_chars = ['&' => "&amp;", '"' => "&quot;", ''' => "&#39;", '<' => "&lt;", '>' => "&gt;"]

test/runtests.jl

+18-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
using Cobweb
2-
using Cobweb: h, Page, Node, attrs, tag, children
3-
using Test
1+
using Cobweb, Test, OrderedCollections
2+
using Cobweb: h, Node, attrs, tag, children
43

54
n1 = h.div("hi")
65
n2 = h(:div, "hi")
76

87
#-----------------------------------------------------------------------------# Creating Nodes
98
@testset "Node Creation" begin
109
for node in [
10+
Node(:div, OrderedDict(:id => "my_id"), (["test"])),
1111
h(:div),
1212
h.div(),
1313
h(:div, "child"),
@@ -24,6 +24,7 @@ n2 = h(:div, "hi")
2424
@test n1 == n2
2525

2626
# _h
27+
@test Cobweb._h(:div) == :(Cobweb.h.div)
2728
@test Cobweb._h(:(div("hi"))) == :(Cobweb.h.div("hi"))
2829
@test Cobweb.@h div(b(), hr(), p("text")) == h.div(h.b(), h.hr(), h.p("text"))
2930

@@ -39,6 +40,20 @@ n2 = h(:div, "hi")
3940
n[1] = "new"
4041
@test n[1] == "new"
4142
end
43+
#-----------------------------------------------------------------------------# Attributes
44+
@testset "Attributes and Children access/mutation" begin
45+
node = h.div(id="myid", class="myclass", "content")
46+
@test propertynames(node) == [:id, :class]
47+
@test get(node, :id, 1) == "myid"
48+
@test get(node, :id2, 1) == 1
49+
get!(node, :id2, "myid2")
50+
@test propertynames(node) == [:id, :class, :id2]
51+
@test node[end] == "content"
52+
push!(node, h.span("child"))
53+
@test node[end] == h.span("child")
54+
append!(node, [h.span("child2"), h.span("child3")])
55+
@test node[end] == h.span("child3")
56+
end
4257
#-----------------------------------------------------------------------------# indexing
4358
@testset "get/setindex" begin
4459
o = h.div("hi")

0 commit comments

Comments
 (0)