Renamed EmptyElement to VoidElement

This commit is contained in:
Brandon Dyck 2019-03-28 20:18:25 -06:00
parent 379bd3ad00
commit a73ec53de9
4 changed files with 28 additions and 28 deletions

View File

@ -4,7 +4,7 @@ import "io"
//go:generate go run ./internal/codegen/codegen.go -input htmldefs.json -output html5/generated.go -package html5 -import //go:generate go run ./internal/codegen/codegen.go -input htmldefs.json -output html5/generated.go -package html5 -import
// Term represents a fragment of HTML markup, and is one of EmptyElement, ParentElement, or Text. // Term represents a fragment of HTML markup, and is one of VoidElement, ParentElement, or Text.
type Term interface { type Term interface {
io.WriterTo io.WriterTo
@ -46,20 +46,20 @@ func (a Attrib) WriteTo(w io.Writer) (n int64, err error) {
return return
} }
// EmptyElement represents an empty HTML element, that is one that cannot have // VoidElement represents a void HTML element, that is one that cannot have
// children. // children.
type EmptyElement struct { type VoidElement struct {
TagName string TagName string
Attribs []Attrib Attribs []Attrib
} }
func (EmptyElement) isHtml() {} func (VoidElement) isHtml() {}
// WriteTo writes the HTML markup represented by e to w, returning the number // WriteTo writes the HTML markup represented by e to w, returning the number
// of bytes written and any error encountered. // of bytes written and any error encountered.
// //
// See the warning about sanitization in the (Attrib).WriteTo documentation. // See the warning about sanitization in the (Attrib).WriteTo documentation.
func (e EmptyElement) WriteTo(w io.Writer) (n int64, err error) { func (e VoidElement) WriteTo(w io.Writer) (n int64, err error) {
err = writeStringsTo(w, &n, "<", e.TagName) err = writeStringsTo(w, &n, "<", e.TagName)
if err != nil { if err != nil {
return return
@ -85,7 +85,7 @@ func (e EmptyElement) WriteTo(w io.Writer) (n int64, err error) {
// ParentElement represents an HTML element that can have children. // ParentElement represents an HTML element that can have children.
type ParentElement struct { type ParentElement struct {
EmptyElement VoidElement
Children []Term Children []Term
} }
@ -96,7 +96,7 @@ func (e ParentElement) isHtml() {}
// //
// See the warning about sanitization in the (Attrib).WriteTo documentation. // See the warning about sanitization in the (Attrib).WriteTo documentation.
func (e ParentElement) WriteTo(w io.Writer) (n int64, err error) { func (e ParentElement) WriteTo(w io.Writer) (n int64, err error) {
n, err = e.EmptyElement.WriteTo(w) n, err = e.VoidElement.WriteTo(w)
if err != nil { if err != nil {
return return
} }

View File

@ -88,7 +88,7 @@ func TestEmptyElement(t *testing.T) {
properties.Property("WriteTo writes element correctly with no attributes", prop.ForAll( properties.Property("WriteTo writes element correctly with no attributes", prop.ForAll(
func(tagName string) bool { func(tagName string) bool {
elem := &hatmill.EmptyElement{ elem := &hatmill.VoidElement{
TagName: tagName, TagName: tagName,
} }
expected := "<" + tagName + ">" expected := "<" + tagName + ">"
@ -99,7 +99,7 @@ func TestEmptyElement(t *testing.T) {
properties.Property("WriteTo writes element correctly with attributes", prop.ForAll( properties.Property("WriteTo writes element correctly with attributes", prop.ForAll(
func(tagName string, attribs []hatmill.Attrib) bool { func(tagName string, attribs []hatmill.Attrib) bool {
elem := hatmill.EmptyElement{ elem := hatmill.VoidElement{
TagName: tagName, TagName: tagName,
Attribs: attribs, Attribs: attribs,
} }
@ -132,13 +132,13 @@ func TestParentElement(t *testing.T) {
properties.Property("WriteTo writes element correctly without children", prop.ForAll( properties.Property("WriteTo writes element correctly without children", prop.ForAll(
func(tagName string, attribs []hatmill.Attrib) bool { func(tagName string, attribs []hatmill.Attrib) bool {
elem := &hatmill.ParentElement{ elem := &hatmill.ParentElement{
EmptyElement: hatmill.EmptyElement{ VoidElement: hatmill.VoidElement{
TagName: tagName, TagName: tagName,
Attribs: attribs, Attribs: attribs,
}, },
} }
openTag := writeToString(elem.EmptyElement) openTag := writeToString(elem.VoidElement)
expected := openTag + "</" + tagName + ">" expected := openTag + "</" + tagName + ">"
return checkWrite(elem, expected) return checkWrite(elem, expected)
}, },
@ -149,14 +149,14 @@ func TestParentElement(t *testing.T) {
properties.Property("WriteTo writes element correctly with children", prop.ForAll( properties.Property("WriteTo writes element correctly with children", prop.ForAll(
func(tagName string, attribs []hatmill.Attrib, children []hatmill.Term) bool { func(tagName string, attribs []hatmill.Attrib, children []hatmill.Term) bool {
elem := &hatmill.ParentElement{ elem := &hatmill.ParentElement{
EmptyElement: hatmill.EmptyElement{ VoidElement: hatmill.VoidElement{
TagName: tagName, TagName: tagName,
Attribs: attribs, Attribs: attribs,
}, },
Children: children, Children: children,
} }
openTag := writeToString(elem.EmptyElement) openTag := writeToString(elem.VoidElement)
var childStrings []string var childStrings []string
for _, child := range children { for _, child := range children {

View File

@ -9,7 +9,7 @@ import "gitlab.codemonkeysoftware.net/b/hatmill"
func Body(attribs ...hatmill.Attrib) func(children ...hatmill.Term) hatmill.ParentElement { func Body(attribs ...hatmill.Attrib) func(children ...hatmill.Term) hatmill.ParentElement {
return func(children ...hatmill.Term) hatmill.ParentElement { return func(children ...hatmill.Term) hatmill.ParentElement {
return hatmill.ParentElement{ return hatmill.ParentElement{
EmptyElement: hatmill.EmptyElement{ VoidElement: hatmill.VoidElement{
TagName: "body", TagName: "body",
Attribs: attribs, Attribs: attribs,
}, },
@ -22,7 +22,7 @@ func Body(attribs ...hatmill.Attrib) func(children ...hatmill.Term) hatmill.Pare
func Div(attribs ...hatmill.Attrib) func(children ...hatmill.Term) hatmill.ParentElement { func Div(attribs ...hatmill.Attrib) func(children ...hatmill.Term) hatmill.ParentElement {
return func(children ...hatmill.Term) hatmill.ParentElement { return func(children ...hatmill.Term) hatmill.ParentElement {
return hatmill.ParentElement{ return hatmill.ParentElement{
EmptyElement: hatmill.EmptyElement{ VoidElement: hatmill.VoidElement{
TagName: "div", TagName: "div",
Attribs: attribs, Attribs: attribs,
}, },
@ -35,7 +35,7 @@ func Div(attribs ...hatmill.Attrib) func(children ...hatmill.Term) hatmill.Paren
func Head(attribs ...hatmill.Attrib) func(children ...hatmill.Term) hatmill.ParentElement { func Head(attribs ...hatmill.Attrib) func(children ...hatmill.Term) hatmill.ParentElement {
return func(children ...hatmill.Term) hatmill.ParentElement { return func(children ...hatmill.Term) hatmill.ParentElement {
return hatmill.ParentElement{ return hatmill.ParentElement{
EmptyElement: hatmill.EmptyElement{ VoidElement: hatmill.VoidElement{
TagName: "head", TagName: "head",
Attribs: attribs, Attribs: attribs,
}, },
@ -48,7 +48,7 @@ func Head(attribs ...hatmill.Attrib) func(children ...hatmill.Term) hatmill.Pare
func Html(attribs ...hatmill.Attrib) func(children ...hatmill.Term) hatmill.ParentElement { func Html(attribs ...hatmill.Attrib) func(children ...hatmill.Term) hatmill.ParentElement {
return func(children ...hatmill.Term) hatmill.ParentElement { return func(children ...hatmill.Term) hatmill.ParentElement {
return hatmill.ParentElement{ return hatmill.ParentElement{
EmptyElement: hatmill.EmptyElement{ VoidElement: hatmill.VoidElement{
TagName: "html", TagName: "html",
Attribs: attribs, Attribs: attribs,
}, },
@ -58,8 +58,8 @@ func Html(attribs ...hatmill.Attrib) func(children ...hatmill.Term) hatmill.Pare
} }
// Img creates a <img> element. // Img creates a <img> element.
func Img(attribs ...hatmill.Attrib) hatmill.EmptyElement { func Img(attribs ...hatmill.Attrib) hatmill.VoidElement {
return hatmill.EmptyElement{ return hatmill.VoidElement{
TagName: "img", TagName: "img",
Attribs: attribs, Attribs: attribs,
} }
@ -69,7 +69,7 @@ func Img(attribs ...hatmill.Attrib) hatmill.EmptyElement {
func Li(attribs ...hatmill.Attrib) func(children ...hatmill.Term) hatmill.ParentElement { func Li(attribs ...hatmill.Attrib) func(children ...hatmill.Term) hatmill.ParentElement {
return func(children ...hatmill.Term) hatmill.ParentElement { return func(children ...hatmill.Term) hatmill.ParentElement {
return hatmill.ParentElement{ return hatmill.ParentElement{
EmptyElement: hatmill.EmptyElement{ VoidElement: hatmill.VoidElement{
TagName: "li", TagName: "li",
Attribs: attribs, Attribs: attribs,
}, },
@ -82,7 +82,7 @@ func Li(attribs ...hatmill.Attrib) func(children ...hatmill.Term) hatmill.Parent
func Span(attribs ...hatmill.Attrib) func(children ...hatmill.Term) hatmill.ParentElement { func Span(attribs ...hatmill.Attrib) func(children ...hatmill.Term) hatmill.ParentElement {
return func(children ...hatmill.Term) hatmill.ParentElement { return func(children ...hatmill.Term) hatmill.ParentElement {
return hatmill.ParentElement{ return hatmill.ParentElement{
EmptyElement: hatmill.EmptyElement{ VoidElement: hatmill.VoidElement{
TagName: "span", TagName: "span",
Attribs: attribs, Attribs: attribs,
}, },
@ -95,7 +95,7 @@ func Span(attribs ...hatmill.Attrib) func(children ...hatmill.Term) hatmill.Pare
func Title(attribs ...hatmill.Attrib) func(children ...hatmill.Term) hatmill.ParentElement { func Title(attribs ...hatmill.Attrib) func(children ...hatmill.Term) hatmill.ParentElement {
return func(children ...hatmill.Term) hatmill.ParentElement { return func(children ...hatmill.Term) hatmill.ParentElement {
return hatmill.ParentElement{ return hatmill.ParentElement{
EmptyElement: hatmill.EmptyElement{ VoidElement: hatmill.VoidElement{
TagName: "title", TagName: "title",
Attribs: attribs, Attribs: attribs,
}, },
@ -108,7 +108,7 @@ func Title(attribs ...hatmill.Attrib) func(children ...hatmill.Term) hatmill.Par
func Ul(attribs ...hatmill.Attrib) func(children ...hatmill.Term) hatmill.ParentElement { func Ul(attribs ...hatmill.Attrib) func(children ...hatmill.Term) hatmill.ParentElement {
return func(children ...hatmill.Term) hatmill.ParentElement { return func(children ...hatmill.Term) hatmill.ParentElement {
return hatmill.ParentElement{ return hatmill.ParentElement{
EmptyElement: hatmill.EmptyElement{ VoidElement: hatmill.VoidElement{
TagName: "ul", TagName: "ul",
Attribs: attribs, Attribs: attribs,
}, },

View File

@ -108,7 +108,7 @@ func (def ElemDef) Generate(qualified bool) string {
func %[1]s(attribs ...%[3]sAttrib) func(children ...%[3]sTerm) %[3]sParentElement { func %[1]s(attribs ...%[3]sAttrib) func(children ...%[3]sTerm) %[3]sParentElement {
return func(children ...%[3]sTerm) %[3]sParentElement { return func(children ...%[3]sTerm) %[3]sParentElement {
return %[3]sParentElement{ return %[3]sParentElement{
EmptyElement: %[3]sEmptyElement{ VoidElement: %[3]sVoidElement{
TagName: "%[2]s", TagName: "%[2]s",
Attribs: attribs, Attribs: attribs,
}, },
@ -117,9 +117,9 @@ func (def ElemDef) Generate(qualified bool) string {
} }
} }
` `
emptyTemplate = `// %[1]s creates a <%[2]s> element. voidTemplate = `// %[1]s creates a <%[2]s> element.
func %[1]s(attribs ...%[3]sAttrib) %[3]sEmptyElement { func %[1]s(attribs ...%[3]sAttrib) %[3]sVoidElement {
return %[3]sEmptyElement{ return %[3]sVoidElement{
TagName: "%[2]s", TagName: "%[2]s",
Attribs: attribs, Attribs: attribs,
} }
@ -133,7 +133,7 @@ func (def ElemDef) Generate(qualified bool) string {
} }
template := parentTemplate template := parentTemplate
if def.Empty { if def.Empty {
template = emptyTemplate template = voidTemplate
} }
return fmt.Sprintf(template, strings.Title(def.Name), def.Name, pkg) return fmt.Sprintf(template, strings.Title(def.Name), def.Name, pkg)
} }