Skip to content

Commit 3879865

Browse files
committed
1 parent ac2ad17 commit 3879865

File tree

5 files changed

+44
-27
lines changed

5 files changed

+44
-27
lines changed

elements.go

+18-18
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ func Address(children ...HTMLComponent) (r *HTMLTagBuilder) {
1919
// Not support
2020

2121
// "area": HTMLAreaElement;
22-
func Area(children ...HTMLComponent) (r *HTMLTagBuilder) {
23-
return Tag("area").Children(children...)
22+
func Area() (r *HTMLTagBuilder) {
23+
return Tag("area").OmitEndTag()
2424
}
2525

2626
// "article": HTMLElement;
@@ -44,8 +44,8 @@ func B(text string) (r *HTMLTagBuilder) {
4444
}
4545

4646
// "base": HTMLBaseElement;
47-
func Base(children ...HTMLComponent) (r *HTMLTagBuilder) {
48-
return Tag("base").Children(children...)
47+
func Base() (r *HTMLTagBuilder) {
48+
return Tag("base").OmitEndTag()
4949
}
5050

5151
// "basefont": HTMLBaseFontElement;
@@ -102,8 +102,8 @@ func Code(text string) (r *HTMLTagBuilder) {
102102
}
103103

104104
// "col": HTMLTableColElement;
105-
func Col(children ...HTMLComponent) (r *HTMLTagBuilder) {
106-
return Tag("col").Children(children...)
105+
func Col() (r *HTMLTagBuilder) {
106+
return Tag("col").OmitEndTag()
107107
}
108108

109109
// "colgroup": HTMLTableColElement;
@@ -171,8 +171,8 @@ func Em(text string) (r *HTMLTagBuilder) {
171171
}
172172

173173
// "embed": HTMLEmbedElement;
174-
func Embed(children ...HTMLComponent) (r *HTMLTagBuilder) {
175-
return Tag("embed").Children(children...)
174+
func Embed() (r *HTMLTagBuilder) {
175+
return Tag("embed").OmitEndTag()
176176
}
177177

178178
// "fieldset": HTMLFieldSetElement;
@@ -257,7 +257,7 @@ func Hgroup(children ...HTMLComponent) (r *HTMLTagBuilder) {
257257

258258
// "hr": HTMLHRElement;
259259
func Hr() (r *HTMLTagBuilder) {
260-
return Tag("hr")
260+
return Tag("hr").OmitEndTag()
261261
}
262262

263263
// "html": HTMLHtmlElement;
@@ -280,13 +280,13 @@ func Iframe(children ...HTMLComponent) (r *HTMLTagBuilder) {
280280

281281
// "img": HTMLImageElement;
282282
func Img(src string) (r *HTMLTagBuilder) {
283-
return Tag("img").Attr("src", src)
283+
return Tag("img").OmitEndTag().Attr("src", src)
284284
}
285285

286286
// "input": HTMLInputElement;
287287

288288
func Input(name string) (r *HTMLTagBuilder) {
289-
return Tag("input").Attr("name", name)
289+
return Tag("input").OmitEndTag().Attr("name", name)
290290
}
291291

292292
// "ins": HTMLModElement;
@@ -316,7 +316,7 @@ func Li(children ...HTMLComponent) (r *HTMLTagBuilder) {
316316

317317
// "link": HTMLLinkElement;
318318
func Link(href string) (r *HTMLTagBuilder) {
319-
return Tag("link").Attr("href", href)
319+
return Tag("link").OmitEndTag().Attr("href", href)
320320
}
321321

322322
// "main": HTMLElement;
@@ -345,7 +345,7 @@ func Menu(children ...HTMLComponent) (r *HTMLTagBuilder) {
345345
// "meta": HTMLMetaElement;
346346

347347
func Meta() (r *HTMLTagBuilder) {
348-
return Tag("meta")
348+
return Tag("meta").OmitEndTag()
349349
}
350350

351351
// "meter": HTMLMeterElement;
@@ -395,7 +395,7 @@ func P(children ...HTMLComponent) (r *HTMLTagBuilder) {
395395

396396
// "param": HTMLParamElement;
397397
func Param(name string) (r *HTMLTagBuilder) {
398-
return Tag("param").Attr("name", name)
398+
return Tag("param").OmitEndTag().Attr("name", name)
399399
}
400400

401401
// "picture": HTMLPictureElement;
@@ -473,7 +473,7 @@ func Small(text string) (r *HTMLTagBuilder) {
473473

474474
// "source": HTMLSourceElement;
475475
func Source(src string) (r *HTMLTagBuilder) {
476-
return Tag("source").Attr("src", src)
476+
return Tag("source").OmitEndTag().Attr("src", src)
477477
}
478478

479479
// "span": HTMLSpanElement;
@@ -565,7 +565,7 @@ func Tr(children ...HTMLComponent) (r *HTMLTagBuilder) {
565565

566566
// "track": HTMLTrackElement;
567567
func Track(src string) (r *HTMLTagBuilder) {
568-
return Tag("track").Attr("src", src)
568+
return Tag("track").OmitEndTag().Attr("src", src)
569569
}
570570

571571
// "u": HTMLElement;
@@ -589,6 +589,6 @@ func Video(children ...HTMLComponent) (r *HTMLTagBuilder) {
589589
}
590590

591591
// "wbr": HTMLElement;
592-
func Wbr(text string) (r *HTMLTagBuilder) {
593-
return Tag("wbr").Text(text)
592+
func Wbr() (r *HTMLTagBuilder) {
593+
return Tag("wbr").OmitEndTag()
594594
}

example_test.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ func ExampleTag_01simplediv() {
2323
Fprint(os.Stdout, comp, context.TODO())
2424
//Output:
2525
// <div>123&lt;h1&gt;Hello, We write html in Go
26-
// <br></div>
26+
// <br>
27+
// </div>
2728
}
2829

2930
/*
@@ -45,13 +46,13 @@ func ExampleTag_02fullhtml() {
4546
//
4647
// <html>
4748
// <head>
48-
// <meta charset='utf8'></meta>
49+
// <meta charset='utf8'>
4950
//
5051
// <title>My test page</title>
5152
// </head>
5253
//
5354
// <body>
54-
// <img src='images/firefox-icon.png' alt='My test image'></img>
55+
// <img src='images/firefox-icon.png' alt='My test image'>
5556
// </body>
5657
// </html>
5758
}
@@ -85,7 +86,7 @@ func ExampleTag_03rawhtmlandcomponent() {
8586
// <div class='userProfile'>
8687
// <h1 class='profileName'>felix&lt;h1&gt;</h1>
8788
//
88-
// <img src='http://image.com/img1.png' class='profileImage'></img>
89+
// <img src='http://image.com/img1.png' class='profileImage'>
8990
// <svg>complicated svg</svg>
9091
// </div>
9192
// </li>
@@ -94,7 +95,7 @@ func ExampleTag_03rawhtmlandcomponent() {
9495
// <div class='userProfile'>
9596
// <h1 class='profileName'>john</h1>
9697
//
97-
// <img src='http://image.com/img2.png' class='profileImage'></img>
98+
// <img src='http://image.com/img2.png' class='profileImage'>
9899
// <svg>complicated svg</svg>
99100
// </div>
100101
// </li>
@@ -305,7 +306,7 @@ func ExampleTag_06httphandler() {
305306
//
306307
// <html>
307308
// <head>
308-
// <meta charset='utf8'></meta>
309+
// <meta charset='utf8'>
309310
// </head>
310311
//
311312
// <body>
@@ -341,9 +342,9 @@ func ExampleTag_07MutipleTypeAttrs() {
341342
Fprint(os.Stdout, comp, context.TODO())
342343
//Output:
343344
// <div>
344-
// <input name='username' type='checkbox' checked more-data='{"Name":"felix","Count":100}' max-length='10'></input>
345+
// <input name='username' type='checkbox' checked more-data='{"Name":"felix","Count":100}' max-length='10'>
345346
//
346-
// <input name='username2' type='checkbox'></input>
347+
// <input name='username2' type='checkbox'>
347348
// </div>
348349
}
349350

go.mod

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

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,12 @@ 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))
335+
newline := ""
336+
337+
if b.omitEndTag {
338+
newline = "\n"
339+
}
340+
buf.WriteString(fmt.Sprintf("\n<%s%s>%s", b.tag, attrStr, newline))
336341
if !b.omitEndTag {
337342
if len(cs) > 0 {
338343
// buf.WriteString("\n")

0 commit comments

Comments
 (0)