Skip to content

Commit

Permalink
No close tag if no content, close inside first tag
Browse files Browse the repository at this point in the history
  • Loading branch information
sunfmin committed Sep 18, 2021
1 parent 609b712 commit 6717c0d
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 18 deletions.
16 changes: 8 additions & 8 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ func ExampleTag_02fullhtml() {
//
// <html>
// <head>
// <meta charset='utf8'></meta>
// <meta charset='utf8'/>
//
// <title>My test page</title>
// </head>
//
// <body>
// <img src='images/firefox-icon.png' alt='My test image'></img>
// <img src='images/firefox-icon.png' alt='My test image'/>
// </body>
// </html>
}
Expand Down Expand Up @@ -85,7 +85,7 @@ func ExampleTag_03rawhtmlandcomponent() {
// <div class='userProfile'>
// <h1 class='profileName'>felix&lt;h1&gt;</h1>
//
// <img src='http://image.com/img1.png' class='profileImage'></img>
// <img src='http://image.com/img1.png' class='profileImage'/>
// <svg>complicated svg</svg>
// </div>
// </li>
Expand All @@ -94,7 +94,7 @@ func ExampleTag_03rawhtmlandcomponent() {
// <div class='userProfile'>
// <h1 class='profileName'>john</h1>
//
// <img src='http://image.com/img2.png' class='profileImage'></img>
// <img src='http://image.com/img2.png' class='profileImage'/>
// <svg>complicated svg</svg>
// </div>
// </li>
Expand Down Expand Up @@ -305,7 +305,7 @@ func ExampleTag_06httphandler() {
//
// <html>
// <head>
// <meta charset='utf8'></meta>
// <meta charset='utf8'/>
// </head>
//
// <body>
Expand Down Expand Up @@ -341,9 +341,9 @@ func ExampleTag_07MutipleTypeAttrs() {
Fprint(os.Stdout, comp, context.TODO())
//Output:
// <div>
// <input name='username' type='checkbox' checked more-data='{"Name":"felix","Count":100}' max-length='10'></input>
// <input name='username' type='checkbox' checked more-data='{"Name":"felix","Count":100}' max-length='10'/>
//
// <input name='username2' type='checkbox'></input>
// <input name='username2' type='checkbox'/>
// </div>
}

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

/*
Expand Down
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
10 changes: 8 additions & 2 deletions tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
32 changes: 24 additions & 8 deletions tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,35 @@ var htmltagCases = []struct {
<div>
<div class='menu' id='the><&"&#39;-menu'>Hello</div>
</div>
`,
},
{
name: "void tag",
tag: Div(),
expected: `
<div/>
`,
},
{
name: "void tag",
tag: Img("a"),
expected: `
<img src='a'/>
`,
},
}

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)
}
})
}
}

0 comments on commit 6717c0d

Please sign in to comment.