From 3879865fd8a5307e0373178e668e3e8d132c90e1 Mon Sep 17 00:00:00 2001 From: Felix Sun Date: Sat, 18 Sep 2021 17:31:50 +0800 Subject: [PATCH] empty html element support https://developer.mozilla.org/en-US/docs/Glossary/Empty_element --- elements.go | 36 ++++++++++++++++++------------------ example_test.go | 17 +++++++++-------- go.mod | 7 +++++++ go.sum | 4 ++++ tag.go | 7 ++++++- 5 files changed, 44 insertions(+), 27 deletions(-) create mode 100644 go.mod create mode 100644 go.sum diff --git a/elements.go b/elements.go index b3e9a25..0eff60b 100644 --- a/elements.go +++ b/elements.go @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; @@ -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() } diff --git a/example_test.go b/example_test.go index 1f1aad4..27c31d5 100644 --- a/example_test.go +++ b/example_test.go @@ -23,7 +23,8 @@ func ExampleTag_01simplediv() { Fprint(os.Stdout, comp, context.TODO()) //Output: //
123<h1>Hello, We write html in Go - //
+ //
+ // } /* @@ -45,13 +46,13 @@ func ExampleTag_02fullhtml() { // // // - // + // // // My test page // // // - // My test image + // My test image // // } @@ -85,7 +86,7 @@ func ExampleTag_03rawhtmlandcomponent() { //
//

felix<h1>

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

john

// - // + // // complicated svg //
// @@ -305,7 +306,7 @@ func ExampleTag_06httphandler() { // // // - // + // // // // @@ -341,9 +342,9 @@ func ExampleTag_07MutipleTypeAttrs() { Fprint(os.Stdout, comp, context.TODO()) //Output: //
- // + // // - // + // //
} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..0730012 --- /dev/null +++ b/go.mod @@ -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 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..6e2a8e9 100644 --- a/tag.go +++ b/tag.go @@ -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")