@@ -6,7 +6,7 @@ using OrderedCollections: OrderedDict
6
6
using StyledStrings: @styled_str
7
7
import AbstractTrees: printnode, print_tree, children
8
8
9
- export h, preview, IFrame, Style, @js_str , @css_str
9
+ export h, preview, IFrame, Style, Children, @js_str , @css_str
10
10
11
11
# -----------------------------------------------------------------------------# init
12
12
function __init__ ()
@@ -167,8 +167,6 @@ h(tag, children...; kw...) = Node(Symbol(tag), attrs(kw), collect(children))
167
167
168
168
h (tag, attrs:: AbstractDict , children... ) = Node (tag, attrs, collect (children))
169
169
170
- h () = Node (" " , OrderedDict {Symbol, Any} (), [])
171
-
172
170
Base. getproperty (:: typeof (h), tag:: Symbol ) = h (tag)
173
171
174
172
Base. propertynames (:: typeof (h)) = HTML5_TAGS
@@ -272,6 +270,21 @@ function Base.show(io::IO, ::MIME"text/html", o::IFrame{T}) where {T}
272
270
end
273
271
274
272
# -----------------------------------------------------------------------------# 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
+ """
275
288
struct Style <: AbstractDict{Symbol, Any}
276
289
dict:: OrderedDict{Symbol, Any}
277
290
Style (x... ) = new (OrderedDict {Symbol, Any} (x... ))
@@ -282,11 +295,35 @@ Base.length(o::Style) = length(getfield(o, :dict))
282
295
Base. iterate (o:: Style , state... ) = iterate (getfield (o, :dict ), state... )
283
296
Base. keys (o:: Style ) = keys (getfield (o, :dict ))
284
297
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)
286
299
Base. propertynames (o:: Style ) = keys (o)
287
300
Base. getproperty (o:: Style , k:: Symbol ) = o[k]
288
301
Base. setproperty! (o:: Style , k:: Symbol , v) = (o[k] = v)
289
302
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
290
327
include (" parser.jl" )
291
328
292
329
end # module
0 commit comments