Skip to content

Commit

Permalink
html: Improve
Browse files Browse the repository at this point in the history
  • Loading branch information
sunshineplan committed Dec 14, 2023
1 parent 89d9c8f commit cb1f055
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 13 deletions.
36 changes: 30 additions & 6 deletions html/element.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ func (e *Element) Style(style string) *Element {
return e.Attribute("style", style)
}

func (e *Element) Title(title string) *Element {
return e.Attribute("title", title)
}

func content(v any) HTML {
switch v := v.(type) {
case nil:
Expand All @@ -55,6 +59,14 @@ func (e *Element) Content(v any) *Element {
return e
}

func (e *Element) Contentf(format string, a ...any) *Element {
return e.Content(fmt.Sprintf(format, a...))
}

func (e *Element) HTMLContent(html string) *Element {
return e.Content(HTML(html))
}

func (e *Element) AppendContent(v any) *Element {
e.content += content(v)
return e
Expand All @@ -64,6 +76,10 @@ func (e *Element) AppendChild(child *Element) *Element {
return e.AppendContent(child)
}

func (e *Element) AppendHTML(html string) *Element {
return e.AppendContent(HTML(html))
}

// https://developer.mozilla.org/en-US/docs/Glossary/Void_element
func (e Element) isVoidElement() bool {
return slices.Contains([]string{
Expand Down Expand Up @@ -100,19 +116,27 @@ func (e Element) printAttrs() string {
return strings.Join(s, " ")
}

func (e *Element) HTML() HTML {
func (e *Element) String() string {
var b strings.Builder
fmt.Fprint(&b, "<", e.tag)
if attrs := e.printAttrs(); attrs != "" {
fmt.Fprint(&b, " ", attrs)
if e.tag != "" {
fmt.Fprint(&b, "<", e.tag)
if attrs := e.printAttrs(); attrs != "" {
fmt.Fprint(&b, " ", attrs)
}
}
if e.isVoidElement() {
if e.tag == "" {
fmt.Fprint(&b, e.content)
} else if e.isVoidElement() {
fmt.Fprint(&b, ">")
} else {
fmt.Fprint(&b, ">", e.content)
fmt.Fprintf(&b, "</%s>", e.tag)
}
return HTML(b.String())
return b.String()
}

func (e *Element) HTML() HTML {
return HTML(e.String())
}

func NewElement(tag string) *Element {
Expand Down
29 changes: 22 additions & 7 deletions html/element_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,37 @@ func TestElement(t *testing.T) {
t.Errorf("expected %q; got %q", tc.html, res)
}
}
if div, expect := Div().Content(Br()).HTML(), "<div><br></div>"; expect != string(div) {
if div, expect := Div().Content(Br()).String(), "<div><br></div>"; expect != div {
t.Errorf("expected %q; got %q", expect, div)
}
}

func TestAppend(t *testing.T) {
e := Div()
if expect := "<div></div>"; expect != string(e.HTML()) {
t.Errorf("expected %q; got %q", expect, e.HTML())
if expect := "<div></div>"; expect != e.String() {
t.Errorf("expected %q; got %q", expect, e.String())
}
e.AppendContent("test")
if expect := "<div>test</div>"; expect != string(e.HTML()) {
t.Errorf("expected %q; got %q", expect, e.HTML())
if expect := "<div>test</div>"; expect != e.String() {
t.Errorf("expected %q; got %q", expect, e.String())
}
e.AppendChild(Img().Src("test"))
if expect := `<div>test<img src="test"></div>`; expect != string(e.HTML()) {
t.Errorf("expected %q; got %q", expect, e.HTML())
if expect := `<div>test<img src="test"></div>`; expect != e.String() {
t.Errorf("expected %q; got %q", expect, e.String())
}
}

func TestBackground(t *testing.T) {
e := Background()
if expect := ""; expect != e.String() {
t.Errorf("expected %q; got %q", expect, e.String())
}
e.AppendContent("test")
if expect := "test"; expect != e.String() {
t.Errorf("expected %q; got %q", expect, e.String())
}
e.AppendChild(Img().Src("test"))
if expect := `test<img src="test">`; expect != e.String() {
t.Errorf("expected %q; got %q", expect, e.String())
}
}
2 changes: 2 additions & 0 deletions html/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ type HTMLer interface {
HTML() HTML
}

func Background() *Element { return NewElement("") }

func A() *Element { return NewElement("a") }
func B() *Element { return NewElement("b") }
func Br() *Element { return NewElement("br") }
Expand Down

0 comments on commit cb1f055

Please sign in to comment.