Skip to content

Commit 63ed312

Browse files
committed
add Children
1 parent 5d9b076 commit 63ed312

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

src/Cobweb.jl

+41-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ using OrderedCollections: OrderedDict
66
using StyledStrings: @styled_str
77
import AbstractTrees: printnode, print_tree, children
88

9-
export h, preview, IFrame, Style, @js_str, @css_str
9+
export h, preview, IFrame, Style, Children, @js_str, @css_str
1010

1111
#-----------------------------------------------------------------------------# init
1212
function __init__()
@@ -167,8 +167,6 @@ h(tag, children...; kw...) = Node(Symbol(tag), attrs(kw), collect(children))
167167

168168
h(tag, attrs::AbstractDict, children...) = Node(tag, attrs, collect(children))
169169

170-
h() = Node("", OrderedDict{Symbol, Any}(), [])
171-
172170
Base.getproperty(::typeof(h), tag::Symbol) = h(tag)
173171

174172
Base.propertynames(::typeof(h)) = HTML5_TAGS
@@ -272,6 +270,21 @@ function Base.show(io::IO, ::MIME"text/html", o::IFrame{T}) where {T}
272270
end
273271

274272
#-----------------------------------------------------------------------------# Style
273+
"""
274+
Style(; kw...)
275+
276+
Create a style object (for inline styles) with the given `kw` attributes.
277+
278+
### Examples
279+
280+
using Cobweb
281+
282+
node = h.div("content", style=Style(color="red", font_size="20px"))
283+
284+
node.style.color = "blue"
285+
286+
preview(node)
287+
"""
275288
struct Style <: AbstractDict{Symbol, Any}
276289
dict::OrderedDict{Symbol, Any}
277290
Style(x...) = new(OrderedDict{Symbol, Any}(x...))
@@ -282,11 +295,35 @@ Base.length(o::Style) = length(getfield(o, :dict))
282295
Base.iterate(o::Style, state...) = iterate(getfield(o, :dict), state...)
283296
Base.keys(o::Style) = keys(getfield(o, :dict))
284297
Base.getindex(o::Style, k::Symbol) = getindex(getfield(o, :dict), k)
285-
Base.setindex!(o::Style, v::Symbol, k::Symbol) = setindex!(getfield(o, :dict), v, k)
298+
Base.setindex!(o::Style, v, k::Symbol) = setindex!(getfield(o, :dict), v, k)
286299
Base.propertynames(o::Style) = keys(o)
287300
Base.getproperty(o::Style, k::Symbol) = o[k]
288301
Base.setproperty!(o::Style, k::Symbol, v) = (o[k] = v)
289302

303+
#-----------------------------------------------------------------------------# Children
304+
"""
305+
Children(x...)
306+
307+
Join the given `x` values into a single object.
308+
309+
### Example
310+
311+
node = Cobweb.Children(h.div("first"), h.p("second"))
312+
313+
repr("text/html", node) == "<div>first</div><p>second</p>"
314+
"""
315+
struct Children
316+
x::Vector
317+
Children(x::AbstractVector) = new(collect(x))
318+
Children(x...) = new(collect(x))
319+
end
320+
Base.show(io::IO, ::MIME"text/html", o::Children) = foreach(x -> show(io, MIME("text/html"), x), o.x)
321+
322+
printnode(io::IO, o::Children) = print(io, "Cobweb.Children ", styled"{gray:($(length(o.x)) children)}")
323+
children(o::Children) = o.x
324+
Base.show(io::IO, o::Children) = print_tree(io, o)
325+
326+
#-----------------------------------------------------------------------------# parser
290327
include("parser.jl")
291328

292329
end #module

test/runtests.jl

+7-1
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,16 @@ end
142142
@testset "Style" begin
143143
n = h.div(style=Style(color = :red))
144144
@test n.style.color == :red
145-
n.style.color = :blue
145+
n.style.color = "blue"
146146
@test occursin("blue", repr("text/html", n))
147147
end
148148

149+
#-----------------------------------------------------------------------------# Children
150+
@testset "Children" begin
151+
c = Children(h.p("A"), h.p("B"))
152+
@test html(c) == "<p>A</p><p>B</p>"
153+
end
154+
149155
#-----------------------------------------------------------------------------# other
150156
@testset "other" begin
151157
@test repr("text/html", Cobweb.Doctype()) == "<!DOCTYPE html>"

0 commit comments

Comments
 (0)