Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
sunfmin committed Sep 18, 2021
1 parent ac2ad17 commit 3879865
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 27 deletions.
36 changes: 18 additions & 18 deletions elements.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ func Address(children ...HTMLComponent) (r *HTMLTagBuilder) {
// Not support

// "area": HTMLAreaElement;
func Area(children ...HTMLComponent) (r *HTMLTagBuilder) {
return Tag("area").Children(children...)
func Area() (r *HTMLTagBuilder) {
return Tag("area").OmitEndTag()
}

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

// "base": HTMLBaseElement;
func Base(children ...HTMLComponent) (r *HTMLTagBuilder) {
return Tag("base").Children(children...)
func Base() (r *HTMLTagBuilder) {
return Tag("base").OmitEndTag()
}

// "basefont": HTMLBaseFontElement;
Expand Down Expand Up @@ -102,8 +102,8 @@ func Code(text string) (r *HTMLTagBuilder) {
}

// "col": HTMLTableColElement;
func Col(children ...HTMLComponent) (r *HTMLTagBuilder) {
return Tag("col").Children(children...)
func Col() (r *HTMLTagBuilder) {
return Tag("col").OmitEndTag()
}

// "colgroup": HTMLTableColElement;
Expand Down Expand Up @@ -171,8 +171,8 @@ func Em(text string) (r *HTMLTagBuilder) {
}

// "embed": HTMLEmbedElement;
func Embed(children ...HTMLComponent) (r *HTMLTagBuilder) {
return Tag("embed").Children(children...)
func Embed() (r *HTMLTagBuilder) {
return Tag("embed").OmitEndTag()
}

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

// "hr": HTMLHRElement;
func Hr() (r *HTMLTagBuilder) {
return Tag("hr")
return Tag("hr").OmitEndTag()
}

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

// "img": HTMLImageElement;
func Img(src string) (r *HTMLTagBuilder) {
return Tag("img").Attr("src", src)
return Tag("img").OmitEndTag().Attr("src", src)
}

// "input": HTMLInputElement;

func Input(name string) (r *HTMLTagBuilder) {
return Tag("input").Attr("name", name)
return Tag("input").OmitEndTag().Attr("name", name)
}

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

// "link": HTMLLinkElement;
func Link(href string) (r *HTMLTagBuilder) {
return Tag("link").Attr("href", href)
return Tag("link").OmitEndTag().Attr("href", href)
}

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

func Meta() (r *HTMLTagBuilder) {
return Tag("meta")
return Tag("meta").OmitEndTag()
}

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

// "param": HTMLParamElement;
func Param(name string) (r *HTMLTagBuilder) {
return Tag("param").Attr("name", name)
return Tag("param").OmitEndTag().Attr("name", name)
}

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

// "source": HTMLSourceElement;
func Source(src string) (r *HTMLTagBuilder) {
return Tag("source").Attr("src", src)
return Tag("source").OmitEndTag().Attr("src", src)
}

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

// "track": HTMLTrackElement;
func Track(src string) (r *HTMLTagBuilder) {
return Tag("track").Attr("src", src)
return Tag("track").OmitEndTag().Attr("src", src)
}

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

// "wbr": HTMLElement;
func Wbr(text string) (r *HTMLTagBuilder) {
return Tag("wbr").Text(text)
func Wbr() (r *HTMLTagBuilder) {
return Tag("wbr").OmitEndTag()
}
17 changes: 9 additions & 8 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ func ExampleTag_01simplediv() {
Fprint(os.Stdout, comp, context.TODO())
//Output:
// <div>123&lt;h1&gt;Hello, We write html in Go
// <br></div>
// <br>
// </div>
}

/*
Expand All @@ -45,13 +46,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 +86,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 +95,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 +306,7 @@ func ExampleTag_06httphandler() {
//
// <html>
// <head>
// <meta charset='utf8'></meta>
// <meta charset='utf8'>
// </head>
//
// <body>
Expand Down Expand Up @@ -341,9 +342,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 Down
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module github.com/theplant/htmlgo

go 1.17

require github.com/theplant/testingutils v0.0.0-20190603093022-26d8b4d95c61

require github.com/pmezard/go-difflib v1.0.0 // 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=
7 changes: 6 additions & 1 deletion tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,12 @@ 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))
newline := ""

if b.omitEndTag {
newline = "\n"
}
buf.WriteString(fmt.Sprintf("\n<%s%s>%s", b.tag, attrStr, newline))
if !b.omitEndTag {
if len(cs) > 0 {
// buf.WriteString("\n")
Expand Down

0 comments on commit 3879865

Please sign in to comment.