From e0ccb4549efa7f30295759153bf8b40a64ed4dc4 Mon Sep 17 00:00:00 2001 From: Brandon Dyck Date: Fri, 26 Apr 2019 20:49:10 -0600 Subject: [PATCH] Added explicit bool attribute type --- attribute/generated.go | 12 ++++++------ defs.json | 6 +++--- hatmill_test.go | 4 ++-- internal/codegen/codegen.go | 12 ++++++++++++ 4 files changed, 23 insertions(+), 11 deletions(-) diff --git a/attribute/generated.go b/attribute/generated.go index 92c1d3e..c458d7c 100644 --- a/attribute/generated.go +++ b/attribute/generated.go @@ -139,10 +139,10 @@ func Content(value string) hatmill.Attrib { } // Contenteditable creates a "contenteditable" attribute -func Contenteditable(value string) hatmill.Attrib { +func Contenteditable(value bool) hatmill.Attrib { return hatmill.Attrib{ Key: "contenteditable", - Value: value, + Value: strconv.FormatBool(value), } } @@ -239,10 +239,10 @@ func Download(value string) hatmill.Attrib { } // Draggable creates a "draggable" attribute -func Draggable(value string) hatmill.Attrib { +func Draggable(value bool) hatmill.Attrib { return hatmill.Attrib{ Key: "draggable", - Value: value, + Value: strconv.FormatBool(value), } } @@ -644,10 +644,10 @@ func Span(value int) hatmill.Attrib { } // Spellcheck creates a "spellcheck" attribute -func Spellcheck(value string) hatmill.Attrib { +func Spellcheck(value bool) hatmill.Attrib { return hatmill.Attrib{ Key: "spellcheck", - Value: value, + Value: strconv.FormatBool(value), } } diff --git a/defs.json b/defs.json index e7992fd..2bfd22b 100644 --- a/defs.json +++ b/defs.json @@ -17,7 +17,7 @@ {"name": "cols", "type": "int"}, {"name": "colspan", "type": "int"}, {"name": "content", "type": "string"}, - {"name": "contenteditable", "type": "string"}, + {"name": "contenteditable", "type": "explicit bool"}, {"name": "contextmenu", "type": "string"}, {"name": "controls", "type": "bool"}, {"name": "coords", "type": "string"}, @@ -30,7 +30,7 @@ {"name": "dirname", "type": "string"}, {"name": "disabled", "type": "bool"}, {"name": "download", "type": "string"}, - {"name": "draggable", "type": "string"}, + {"name": "draggable", "type": "explicit bool"}, {"name": "enctype", "type": "string"}, {"name": "for", "type": "string"}, {"name": "form", "type": "string"}, @@ -82,7 +82,7 @@ {"name": "size", "type": "int"}, {"name": "sizes", "type": "string"}, {"name": "span", "type": "int"}, - {"name": "spellcheck", "type": "string"}, + {"name": "spellcheck", "type": "explicit bool"}, {"name": "src", "type": "string"}, {"name": "srcdoc", "type": "string"}, {"name": "srclang", "type": "string"}, diff --git a/hatmill_test.go b/hatmill_test.go index e7509ba..15b4805 100644 --- a/hatmill_test.go +++ b/hatmill_test.go @@ -183,7 +183,7 @@ func Example() { document := he.Html()( he.Body()( - he.Img(ha.Src("./photo.jpg")), + he.Img(ha.Src("./photo.jpg"), ha.Contenteditable(true)), hatmill.Text(html.EscapeString(userInput)), he.Div(ha.Disabled(), ha.CustomData("coolness", "awesome"))(), he.Textarea(ha.Rows(25))(), @@ -191,5 +191,5 @@ func Example() { ), ) hatmill.WriteDocument(os.Stdout, document) - // Output: <script>launchMissiles();</script>
+ // Output: <script>launchMissiles();</script>
} diff --git a/internal/codegen/codegen.go b/internal/codegen/codegen.go index 7c58775..60245e6 100644 --- a/internal/codegen/codegen.go +++ b/internal/codegen/codegen.go @@ -46,6 +46,18 @@ var attribTypes = map[string]AttribTypeInfo{ `, }, + "explicit bool": { + Template: `// %[1]s creates a "%[2]s" attribute + func %[1]s(value bool) hatmill.Attrib { + return hatmill.Attrib{ + Key: "%[2]s", + Value: strconv.FormatBool(value), + } + } + `, + Imports: []string{"strconv"}, + }, + "int": { Template: `// %[1]s creates a "%[2]s" attribute func %[1]s(value int) hatmill.Attrib {