Renamed EmptyElement to VoidElement
This commit is contained in:
parent
379bd3ad00
commit
a73ec53de9
14
hatmill.go
14
hatmill.go
@ -4,7 +4,7 @@ import "io"
|
||||
|
||||
//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 {
|
||||
io.WriterTo
|
||||
|
||||
@ -46,20 +46,20 @@ func (a Attrib) WriteTo(w io.Writer) (n int64, err error) {
|
||||
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.
|
||||
type EmptyElement struct {
|
||||
type VoidElement struct {
|
||||
TagName string
|
||||
Attribs []Attrib
|
||||
}
|
||||
|
||||
func (EmptyElement) isHtml() {}
|
||||
func (VoidElement) isHtml() {}
|
||||
|
||||
// WriteTo writes the HTML markup represented by e to w, returning the number
|
||||
// of bytes written and any error encountered.
|
||||
//
|
||||
// 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)
|
||||
if err != nil {
|
||||
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.
|
||||
type ParentElement struct {
|
||||
EmptyElement
|
||||
VoidElement
|
||||
Children []Term
|
||||
}
|
||||
|
||||
@ -96,7 +96,7 @@ func (e ParentElement) isHtml() {}
|
||||
//
|
||||
// See the warning about sanitization in the (Attrib).WriteTo documentation.
|
||||
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 {
|
||||
return
|
||||
}
|
||||
|
@ -88,7 +88,7 @@ func TestEmptyElement(t *testing.T) {
|
||||
|
||||
properties.Property("WriteTo writes element correctly with no attributes", prop.ForAll(
|
||||
func(tagName string) bool {
|
||||
elem := &hatmill.EmptyElement{
|
||||
elem := &hatmill.VoidElement{
|
||||
TagName: tagName,
|
||||
}
|
||||
expected := "<" + tagName + ">"
|
||||
@ -99,7 +99,7 @@ func TestEmptyElement(t *testing.T) {
|
||||
|
||||
properties.Property("WriteTo writes element correctly with attributes", prop.ForAll(
|
||||
func(tagName string, attribs []hatmill.Attrib) bool {
|
||||
elem := hatmill.EmptyElement{
|
||||
elem := hatmill.VoidElement{
|
||||
TagName: tagName,
|
||||
Attribs: attribs,
|
||||
}
|
||||
@ -132,13 +132,13 @@ func TestParentElement(t *testing.T) {
|
||||
properties.Property("WriteTo writes element correctly without children", prop.ForAll(
|
||||
func(tagName string, attribs []hatmill.Attrib) bool {
|
||||
elem := &hatmill.ParentElement{
|
||||
EmptyElement: hatmill.EmptyElement{
|
||||
VoidElement: hatmill.VoidElement{
|
||||
TagName: tagName,
|
||||
Attribs: attribs,
|
||||
},
|
||||
}
|
||||
|
||||
openTag := writeToString(elem.EmptyElement)
|
||||
openTag := writeToString(elem.VoidElement)
|
||||
expected := openTag + "</" + tagName + ">"
|
||||
return checkWrite(elem, expected)
|
||||
},
|
||||
@ -149,14 +149,14 @@ func TestParentElement(t *testing.T) {
|
||||
properties.Property("WriteTo writes element correctly with children", prop.ForAll(
|
||||
func(tagName string, attribs []hatmill.Attrib, children []hatmill.Term) bool {
|
||||
elem := &hatmill.ParentElement{
|
||||
EmptyElement: hatmill.EmptyElement{
|
||||
VoidElement: hatmill.VoidElement{
|
||||
TagName: tagName,
|
||||
Attribs: attribs,
|
||||
},
|
||||
Children: children,
|
||||
}
|
||||
|
||||
openTag := writeToString(elem.EmptyElement)
|
||||
openTag := writeToString(elem.VoidElement)
|
||||
|
||||
var childStrings []string
|
||||
for _, child := range children {
|
||||
|
@ -9,7 +9,7 @@ import "gitlab.codemonkeysoftware.net/b/hatmill"
|
||||
func Body(attribs ...hatmill.Attrib) func(children ...hatmill.Term) hatmill.ParentElement {
|
||||
return func(children ...hatmill.Term) hatmill.ParentElement {
|
||||
return hatmill.ParentElement{
|
||||
EmptyElement: hatmill.EmptyElement{
|
||||
VoidElement: hatmill.VoidElement{
|
||||
TagName: "body",
|
||||
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 {
|
||||
return func(children ...hatmill.Term) hatmill.ParentElement {
|
||||
return hatmill.ParentElement{
|
||||
EmptyElement: hatmill.EmptyElement{
|
||||
VoidElement: hatmill.VoidElement{
|
||||
TagName: "div",
|
||||
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 {
|
||||
return func(children ...hatmill.Term) hatmill.ParentElement {
|
||||
return hatmill.ParentElement{
|
||||
EmptyElement: hatmill.EmptyElement{
|
||||
VoidElement: hatmill.VoidElement{
|
||||
TagName: "head",
|
||||
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 {
|
||||
return func(children ...hatmill.Term) hatmill.ParentElement {
|
||||
return hatmill.ParentElement{
|
||||
EmptyElement: hatmill.EmptyElement{
|
||||
VoidElement: hatmill.VoidElement{
|
||||
TagName: "html",
|
||||
Attribs: attribs,
|
||||
},
|
||||
@ -58,8 +58,8 @@ func Html(attribs ...hatmill.Attrib) func(children ...hatmill.Term) hatmill.Pare
|
||||
}
|
||||
|
||||
// Img creates a <img> element.
|
||||
func Img(attribs ...hatmill.Attrib) hatmill.EmptyElement {
|
||||
return hatmill.EmptyElement{
|
||||
func Img(attribs ...hatmill.Attrib) hatmill.VoidElement {
|
||||
return hatmill.VoidElement{
|
||||
TagName: "img",
|
||||
Attribs: attribs,
|
||||
}
|
||||
@ -69,7 +69,7 @@ func Img(attribs ...hatmill.Attrib) hatmill.EmptyElement {
|
||||
func Li(attribs ...hatmill.Attrib) func(children ...hatmill.Term) hatmill.ParentElement {
|
||||
return func(children ...hatmill.Term) hatmill.ParentElement {
|
||||
return hatmill.ParentElement{
|
||||
EmptyElement: hatmill.EmptyElement{
|
||||
VoidElement: hatmill.VoidElement{
|
||||
TagName: "li",
|
||||
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 {
|
||||
return func(children ...hatmill.Term) hatmill.ParentElement {
|
||||
return hatmill.ParentElement{
|
||||
EmptyElement: hatmill.EmptyElement{
|
||||
VoidElement: hatmill.VoidElement{
|
||||
TagName: "span",
|
||||
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 {
|
||||
return func(children ...hatmill.Term) hatmill.ParentElement {
|
||||
return hatmill.ParentElement{
|
||||
EmptyElement: hatmill.EmptyElement{
|
||||
VoidElement: hatmill.VoidElement{
|
||||
TagName: "title",
|
||||
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 {
|
||||
return func(children ...hatmill.Term) hatmill.ParentElement {
|
||||
return hatmill.ParentElement{
|
||||
EmptyElement: hatmill.EmptyElement{
|
||||
VoidElement: hatmill.VoidElement{
|
||||
TagName: "ul",
|
||||
Attribs: attribs,
|
||||
},
|
||||
|
@ -108,7 +108,7 @@ func (def ElemDef) Generate(qualified bool) string {
|
||||
func %[1]s(attribs ...%[3]sAttrib) func(children ...%[3]sTerm) %[3]sParentElement {
|
||||
return func(children ...%[3]sTerm) %[3]sParentElement {
|
||||
return %[3]sParentElement{
|
||||
EmptyElement: %[3]sEmptyElement{
|
||||
VoidElement: %[3]sVoidElement{
|
||||
TagName: "%[2]s",
|
||||
Attribs: attribs,
|
||||
},
|
||||
@ -117,9 +117,9 @@ func (def ElemDef) Generate(qualified bool) string {
|
||||
}
|
||||
}
|
||||
`
|
||||
emptyTemplate = `// %[1]s creates a <%[2]s> element.
|
||||
func %[1]s(attribs ...%[3]sAttrib) %[3]sEmptyElement {
|
||||
return %[3]sEmptyElement{
|
||||
voidTemplate = `// %[1]s creates a <%[2]s> element.
|
||||
func %[1]s(attribs ...%[3]sAttrib) %[3]sVoidElement {
|
||||
return %[3]sVoidElement{
|
||||
TagName: "%[2]s",
|
||||
Attribs: attribs,
|
||||
}
|
||||
@ -133,7 +133,7 @@ func (def ElemDef) Generate(qualified bool) string {
|
||||
}
|
||||
template := parentTemplate
|
||||
if def.Empty {
|
||||
template = emptyTemplate
|
||||
template = voidTemplate
|
||||
}
|
||||
return fmt.Sprintf(template, strings.Title(def.Name), def.Name, pkg)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user