From 75ed6da9bbca9ab1b9530d8e32ccb986e7ebaa55 Mon Sep 17 00:00:00 2001 From: "Felipe S. S. Schneider" Date: Fri, 21 May 2021 17:07:23 -0300 Subject: [PATCH] Support boolean values for boolean attributes --- src/Hyperscript.jl | 36 ++++++++++++++++++++++++++++++++++++ test/runtests.jl | 6 ++++++ 2 files changed, 42 insertions(+) diff --git a/src/Hyperscript.jl b/src/Hyperscript.jl index 598ee9c..e655887 100644 --- a/src/Hyperscript.jl +++ b/src/Hyperscript.jl @@ -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 @@ -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", diff --git a/test/runtests.jl b/test/runtests.jl index 6dd06f9..788b94c 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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`

` +# Support boolean values for boolean attributes +@renders m("input", type="checkbox", checked=true) s`` +@renders m("input", type="checkbox", checked=false) s`` +# @errors m("input", type="checkbox", checked="true") +@renders m("input", type="text", value=true) s`` + ## Children # Can render children @renders m("p", "child") s`

child

`