Skip to content

Commit 6717c0d

Browse files
committed
No close tag if no content, close inside first tag
1 parent 609b712 commit 6717c0d

File tree

5 files changed

+52
-18
lines changed

5 files changed

+52
-18
lines changed

example_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ func ExampleTag_02fullhtml() {
4545
//
4646
// <html>
4747
// <head>
48-
// <meta charset='utf8'></meta>
48+
// <meta charset='utf8'/>
4949
//
5050
// <title>My test page</title>
5151
// </head>
5252
//
5353
// <body>
54-
// <img src='images/firefox-icon.png' alt='My test image'></img>
54+
// <img src='images/firefox-icon.png' alt='My test image'/>
5555
// </body>
5656
// </html>
5757
}
@@ -85,7 +85,7 @@ func ExampleTag_03rawhtmlandcomponent() {
8585
// <div class='userProfile'>
8686
// <h1 class='profileName'>felix&lt;h1&gt;</h1>
8787
//
88-
// <img src='http://image.com/img1.png' class='profileImage'></img>
88+
// <img src='http://image.com/img1.png' class='profileImage'/>
8989
// <svg>complicated svg</svg>
9090
// </div>
9191
// </li>
@@ -94,7 +94,7 @@ func ExampleTag_03rawhtmlandcomponent() {
9494
// <div class='userProfile'>
9595
// <h1 class='profileName'>john</h1>
9696
//
97-
// <img src='http://image.com/img2.png' class='profileImage'></img>
97+
// <img src='http://image.com/img2.png' class='profileImage'/>
9898
// <svg>complicated svg</svg>
9999
// </div>
100100
// </li>
@@ -305,7 +305,7 @@ func ExampleTag_06httphandler() {
305305
//
306306
// <html>
307307
// <head>
308-
// <meta charset='utf8'></meta>
308+
// <meta charset='utf8'/>
309309
// </head>
310310
//
311311
// <body>
@@ -341,9 +341,9 @@ func ExampleTag_07MutipleTypeAttrs() {
341341
Fprint(os.Stdout, comp, context.TODO())
342342
//Output:
343343
// <div>
344-
// <input name='username' type='checkbox' checked more-data='{"Name":"felix","Count":100}' max-length='10'></input>
344+
// <input name='username' type='checkbox' checked more-data='{"Name":"felix","Count":100}' max-length='10'/>
345345
//
346-
// <input name='username2' type='checkbox'></input>
346+
// <input name='username2' type='checkbox'/>
347347
// </div>
348348
}
349349

@@ -356,7 +356,7 @@ func ExampleTag_08styles() {
356356
StyleIf("color:blue", true)
357357
Fprint(os.Stdout, comp, context.TODO())
358358
//Output:
359-
// <div style='background-color:red; border:1px solid red; color:blue;'></div>
359+
// <div style='background-color:red; border:1px solid red; color:blue;'/>
360360
}
361361

362362
/*

go.mod

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module github.com/theplant/htmlgo
2+
3+
go 1.17
4+
5+
require (
6+
github.com/pmezard/go-difflib v1.0.0 // indirect
7+
github.com/theplant/testingutils v0.0.0-20190603093022-26d8b4d95c61 // indirect
8+
)

go.sum

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
3+
github.com/theplant/testingutils v0.0.0-20190603093022-26d8b4d95c61 h1:757/ruZNgTsOf5EkQBo0i3Bx/P2wgF5ljVkODeUX/uA=
4+
github.com/theplant/testingutils v0.0.0-20190603093022-26d8b4d95c61/go.mod h1:p22Q3Bg5ML+hdI3QSQkB/pZ2+CjfOnGugoQIoyE2Ub8=

tag.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -332,8 +332,14 @@ func (b *HTMLTagBuilder) MarshalHTML(ctx context.Context) (r []byte, err error)
332332
}
333333

334334
buf := bytes.NewBuffer(nil)
335-
buf.WriteString(fmt.Sprintf("\n<%s%s>", b.tag, attrStr))
336-
if !b.omitEndTag {
335+
tagClosed := false
336+
if len(cs) == 0 && !b.omitEndTag {
337+
tagClosed = true
338+
buf.WriteString(fmt.Sprintf("\n<%s%s/>\n", b.tag, attrStr))
339+
} else {
340+
buf.WriteString(fmt.Sprintf("\n<%s%s>", b.tag, attrStr))
341+
}
342+
if !b.omitEndTag && !tagClosed {
337343
if len(cs) > 0 {
338344
// buf.WriteString("\n")
339345
for _, c := range cs {

tag_test.go

+24-8
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,35 @@ var htmltagCases = []struct {
5151
<div>
5252
<div class='menu' id='the><&"&#39;-menu'>Hello</div>
5353
</div>
54+
`,
55+
},
56+
{
57+
name: "void tag",
58+
tag: Div(),
59+
expected: `
60+
<div/>
61+
`,
62+
},
63+
{
64+
name: "void tag",
65+
tag: Img("a"),
66+
expected: `
67+
<img src='a'/>
5468
`,
5569
},
5670
}
5771

5872
func TestHtmlTag(t *testing.T) {
5973
for _, c := range htmltagCases {
60-
r, err := c.tag.MarshalHTML(context.TODO())
61-
if err != nil {
62-
panic(err)
63-
}
64-
diff := testingutils.PrettyJsonDiff(c.expected, string(r))
65-
if len(diff) > 0 {
66-
t.Error(c.name, diff)
67-
}
74+
t.Run(c.name, func(t *testing.T){
75+
r, err := c.tag.MarshalHTML(context.TODO())
76+
if err != nil {
77+
panic(err)
78+
}
79+
diff := testingutils.PrettyJsonDiff(c.expected, string(r))
80+
if len(diff) > 0 {
81+
t.Error(c.name, diff)
82+
}
83+
})
6884
}
6985
}

0 commit comments

Comments
 (0)