diff --git a/html/element.go b/html/element.go index c4037b1..be69418 100644 --- a/html/element.go +++ b/html/element.go @@ -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: @@ -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 @@ -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{ @@ -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, "", e.tag) } - return HTML(b.String()) + return b.String() +} + +func (e *Element) HTML() HTML { + return HTML(e.String()) } func NewElement(tag string) *Element { diff --git a/html/element_test.go b/html/element_test.go index 459800a..2bea657 100644 --- a/html/element_test.go +++ b/html/element_test.go @@ -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(), "

"; expect != string(div) { + if div, expect := Div().Content(Br()).String(), "

"; expect != div { t.Errorf("expected %q; got %q", expect, div) } } func TestAppend(t *testing.T) { e := Div() - if expect := "
"; expect != string(e.HTML()) { - t.Errorf("expected %q; got %q", expect, e.HTML()) + if expect := "
"; expect != e.String() { + t.Errorf("expected %q; got %q", expect, e.String()) } e.AppendContent("test") - if expect := "
test
"; expect != string(e.HTML()) { - t.Errorf("expected %q; got %q", expect, e.HTML()) + if expect := "
test
"; expect != e.String() { + t.Errorf("expected %q; got %q", expect, e.String()) } e.AppendChild(Img().Src("test")) - if expect := `
test
`; expect != string(e.HTML()) { - t.Errorf("expected %q; got %q", expect, e.HTML()) + if expect := `
test
`; 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`; expect != e.String() { + t.Errorf("expected %q; got %q", expect, e.String()) } } diff --git a/html/html.go b/html/html.go index b00869a..6e1ea7c 100644 --- a/html/html.go +++ b/html/html.go @@ -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") }