diff --git a/example_test.go b/example_test.go index 1f1aad4..a318bb4 100644 --- a/example_test.go +++ b/example_test.go @@ -45,13 +45,13 @@ func ExampleTag_02fullhtml() { // // // - // + // // // My test page // // // - // My test image + // My test image // // } @@ -85,7 +85,7 @@ func ExampleTag_03rawhtmlandcomponent() { //
//

felix<h1>

// - // + // // complicated svg //
// @@ -94,7 +94,7 @@ func ExampleTag_03rawhtmlandcomponent() { //
//

john

// - // + // // complicated svg //
// @@ -305,7 +305,7 @@ func ExampleTag_06httphandler() { // // // - // + // // // // @@ -341,9 +341,9 @@ func ExampleTag_07MutipleTypeAttrs() { Fprint(os.Stdout, comp, context.TODO()) //Output: //
- // + // // - // + // //
} @@ -356,7 +356,7 @@ func ExampleTag_08styles() { StyleIf("color:blue", true) Fprint(os.Stdout, comp, context.TODO()) //Output: - //
+ //
} /* diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..71785fc --- /dev/null +++ b/go.mod @@ -0,0 +1,8 @@ +module github.com/theplant/htmlgo + +go 1.17 + +require ( + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/theplant/testingutils v0.0.0-20190603093022-26d8b4d95c61 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..03e832b --- /dev/null +++ b/go.sum @@ -0,0 +1,4 @@ +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/theplant/testingutils v0.0.0-20190603093022-26d8b4d95c61 h1:757/ruZNgTsOf5EkQBo0i3Bx/P2wgF5ljVkODeUX/uA= +github.com/theplant/testingutils v0.0.0-20190603093022-26d8b4d95c61/go.mod h1:p22Q3Bg5ML+hdI3QSQkB/pZ2+CjfOnGugoQIoyE2Ub8= diff --git a/tag.go b/tag.go index 2f73c89..21bb1de 100644 --- a/tag.go +++ b/tag.go @@ -332,8 +332,14 @@ func (b *HTMLTagBuilder) MarshalHTML(ctx context.Context) (r []byte, err error) } buf := bytes.NewBuffer(nil) - buf.WriteString(fmt.Sprintf("\n<%s%s>", b.tag, attrStr)) - if !b.omitEndTag { + tagClosed := false + if len(cs) == 0 && !b.omitEndTag { + tagClosed = true + buf.WriteString(fmt.Sprintf("\n<%s%s/>\n", b.tag, attrStr)) + } else { + buf.WriteString(fmt.Sprintf("\n<%s%s>", b.tag, attrStr)) + } + if !b.omitEndTag && !tagClosed { if len(cs) > 0 { // buf.WriteString("\n") for _, c := range cs { diff --git a/tag_test.go b/tag_test.go index 88aeffb..711a9d9 100644 --- a/tag_test.go +++ b/tag_test.go @@ -51,19 +51,35 @@ var htmltagCases = []struct {
+`, + }, + { + name: "void tag", + tag: Div(), + expected: ` +
+`, + }, + { + name: "void tag", + tag: Img("a"), + expected: ` + `, }, } func TestHtmlTag(t *testing.T) { for _, c := range htmltagCases { - r, err := c.tag.MarshalHTML(context.TODO()) - if err != nil { - panic(err) - } - diff := testingutils.PrettyJsonDiff(c.expected, string(r)) - if len(diff) > 0 { - t.Error(c.name, diff) - } + t.Run(c.name, func(t *testing.T){ + r, err := c.tag.MarshalHTML(context.TODO()) + if err != nil { + panic(err) + } + diff := testingutils.PrettyJsonDiff(c.expected, string(r)) + if len(diff) > 0 { + t.Error(c.name, diff) + } + }) } }