Skip to content

Add support for a do-block syntax #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Hyperscript.jl
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,11 @@ const NOESCAPE_HTMLSVG_CONTEXT = HTMLSVG(false, true)
m(tag::AbstractString, cs...; as...) = Node(DEFAULT_HTMLSVG_CONTEXT, tag, cs, as)
m(ctx::Context, tag::AbstractString, cs...; as...) = Node(ctx, tag, cs, as)

# Do block syntax
Node(f::Function, ctx::T, tag::AbstractString, children, attrs) where T <: Context = Node(ctx, tag, [children..., f()], attrs)
(x::Node)(f::Function, cs...; as...) = x(cs..., f(); as...)
m(f::Function, args...; as...) = m(args..., f(); as...)

# HTML/SVG tags macros
macro tags(args::Symbol...)
blk = Expr(:block)
Expand Down
46 changes: 46 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,52 @@ end
# Dot syntax with regular class specification as an override
@renders m("p", class="a")(class="b") s`<p class="b"></p>`

# Do block syntax for Node application
@renders p() do
end s`<p></p>`
@renders p() do
"child"
end s`<p>child</p>`
@renders p("child1") do
"child2"
end s`<p>child1child2</p>`
# Do block syntax for m
@renders m("p") do
end s`<p></p>`
@renders m("p") do
"child"
end s`<p>child</p>`
@renders m("p", "child1") do
"child2"
end s`<p>child1child2</p>`
# Do block syntax for Node
@renders Hyperscript.Node(Hyperscript.DEFAULT_HTMLSVG_CONTEXT, "p", [], []) do
end s`<p></p>`
@renders Hyperscript.Node(Hyperscript.DEFAULT_HTMLSVG_CONTEXT, "p", [], []) do
"child"
end s`<p>child</p>`
@renders Hyperscript.Node(Hyperscript.DEFAULT_HTMLSVG_CONTEXT, "p", ["child1"], []) do
"child2"
end s`<p>child1child2</p>`
# Do block syntax with attributes
@renders m("p", class="a") do
end s`<p class="a"></p>`
@renders m("p", class="a") do
"child"
end s`<p class="a">child</p>`
@renders m("p", "child1", class="a") do
"child2"
end s`<p class="a">child1child2</p>`
# Do block syntax with dot syntax
@renders m("p").a() do
end s`<p class="a"></p>`
@renders m("p").a() do
"child"
end s`<p class="a">child</p>`
@renders m("p").a("child1") do
"child2"
end s`<p class="a">child1child2</p>`

## CSS nodes
# Tags must be strings
@renders css("p") s`p {}`
Expand Down