Skip to content

Commit

Permalink
html: Add support for adding multiple contents
Browse files Browse the repository at this point in the history
  • Loading branch information
sunshineplan committed Dec 14, 2023
1 parent cb1f055 commit 1aa9d42
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
26 changes: 17 additions & 9 deletions html/element.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ func content(v any) HTML {
}
}

func (e *Element) Content(v any) *Element {
e.content = content(v)
return e
func (e *Element) Content(v ...any) *Element {
e.content = ""
return e.AppendContent(v...)
}

func (e *Element) Contentf(format string, a ...any) *Element {
Expand All @@ -67,17 +67,25 @@ func (e *Element) HTMLContent(html string) *Element {
return e.Content(HTML(html))
}

func (e *Element) AppendContent(v any) *Element {
e.content += content(v)
func (e *Element) AppendContent(v ...any) *Element {
for _, v := range v {
e.content += content(v)
}
return e
}

func (e *Element) AppendChild(child *Element) *Element {
return e.AppendContent(child)
func (e *Element) AppendChild(child ...*Element) *Element {
for _, i := range child {
e.AppendContent(i)
}
return e
}

func (e *Element) AppendHTML(html string) *Element {
return e.AppendContent(HTML(html))
func (e *Element) AppendHTML(html ...string) *Element {
for _, i := range html {
e.AppendContent(HTML(i))
}
return e
}

// https://developer.mozilla.org/en-US/docs/Glossary/Void_element
Expand Down
7 changes: 4 additions & 3 deletions html/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import "testing"

func TestTable(t *testing.T) {
table := `<table><thead><tr><th colspan="2">H1</th><th>H2</th></tr></thead><tbody><tr><td>B1</td><td>B2</td></tr></tbody></table>`
if e := Table().
AppendChild(Thead().AppendChild(Tr(Th("H1").Colspan(2), Th("H2")))).
AppendChild(Tbody().AppendChild(Tr(Td("B1"), Td("B2")))); string(e.HTML()) != table {
if e := Table().AppendChild(
Thead().AppendChild(Tr(Th("H1").Colspan(2), Th("H2"))),
Tbody().AppendChild(Tr(Td("B1"), Td("B2"))),
); e.String() != table {
t.Errorf("expected %q; got %q", table, e.HTML())
}
}

0 comments on commit 1aa9d42

Please sign in to comment.