Skip to content
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

Support boolean values for boolean attributes #35

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
36 changes: 36 additions & 0 deletions src/Hyperscript.jl
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,11 @@ function render(io::IO, rctx::RenderContext, ctx::HTMLSVG, node::Node{HTMLSVG})
end
printescaped(io, tag(node), etag)
for (name, value) in pairs(attrs(node))
if isboolattr(name)
value isa Bool || error("Boolean attribute \"$(name)\" expects `Bool`, found `$(typeof(value))`: $(stringify(ctx, tag(node), name => value))")
value = value ? "" : continue
end

print(io, " ")
printescaped(io, name, eattrname)
if value != nothing
Expand Down Expand Up @@ -223,6 +228,37 @@ function render(io::IO, rctx::RenderContext, ctx::HTMLSVG, node::Node{HTMLSVG})
end
end

const BOOL_ATTRS = Set([
"allowfullscreen",
"allowpaymentrequest",
"async",
"autofocus",
"autoplay",
"checked",
"controls",
"default",
"defer",
"disabled",
"formnovalidate",
"hidden",
"ismap",
"itemscope",
"loop",
"multiple",
"muted",
"nomodule",
"novalidate",
"open",
"playsinline",
"readonly",
"required",
"reversed",
"selected",
"truespeed",
"typemustmatch"
])
isboolattr(attr::AbstractString) = attr in BOOL_ATTRS

const VOID_TAGS = Set([
"track", "hr", "col", "embed", "br", "circle", "input", "base",
"use", "source", "polyline", "param", "ellipse", "link", "img",
Expand Down
6 changes: 6 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ end
# Passing a string as an attribute name preserves it un-normalized
@renders Hyperscript.Node(Hyperscript.DEFAULT_HTMLSVG_CONTEXT, "p", [], ["camelName" => 7.0]) s`<p camelName="7.0"></p>`

# Support boolean values for boolean attributes
@renders m("input", type="checkbox", checked=true) s`<input checked="" type="checkbox" />`
@renders m("input", type="checkbox", checked=false) s`<input type="checkbox" />`
# @errors m("input", type="checkbox", checked="true")
@renders m("input", type="text", value=true) s`<input value="true" type="text" />`

## Children
# Can render children
@renders m("p", "child") s`<p>child</p>`
Expand Down