From 80adfde3c830f734eb186a294f9d856c2df81593 Mon Sep 17 00:00:00 2001 From: Anhgelus Morhtuuzh Date: Tue, 3 Feb 2026 18:47:14 +0100 Subject: fix(dom): remove trailing slash for void element was never required for html --- dom/html.go | 13 +++++-------- dom/html_test.go | 25 ++++++++++++------------- 2 files changed, 17 insertions(+), 21 deletions(-) (limited to 'dom') diff --git a/dom/html.go b/dom/html.go index 3e8117f..7325b70 100644 --- a/dom/html.go +++ b/dom/html.go @@ -5,15 +5,12 @@ import ( "html/template" ) -func render(tag string, attributes map[string]string, endSlash bool) template.HTML { +func render(tag string, attributes map[string]string) template.HTML { base := fmt.Sprintf(`<%s`, tag) for k, v := range attributes { - base += fmt.Sprintf(` %s="%s"`, k, v) + base = fmt.Sprintf(`%s %s="%s"`, base, k, v) } - if !endSlash { - return template.HTML(base + `>`) - } - return template.HTML(base + ` />`) + return template.HTML(base + `>`) } type Element interface { @@ -58,7 +55,7 @@ type VoidElement struct { func (e VoidElement) Render() template.HTML { e.cl.set(e) - return render(e.Tag, e.attributes, true) + return render(e.Tag, e.attributes) } func (e VoidElement) HasAttribute(k string) bool { @@ -95,7 +92,7 @@ type ContentElement struct { func (e ContentElement) Render() template.HTML { e.cl.set(e) - base := render(e.Tag, e.attributes, false) + base := render(e.Tag, e.attributes) for _, el := range e.Contents { base += el.Render() } diff --git a/dom/html_test.go b/dom/html_test.go index 39f6a1f..0fbd894 100644 --- a/dom/html_test.go +++ b/dom/html_test.go @@ -6,19 +6,18 @@ import ( ) func TestRender(t *testing.T) { - fn := func(tag string, attributes map[string]string, endSlash bool, expected string) func(*testing.T) { + fn := func(tag string, attributes map[string]string, expected string) func(*testing.T) { return func(t *testing.T) { t.Parallel() - got := string(render(tag, attributes, endSlash)) + got := string(render(tag, attributes)) if got != expected { t.Errorf("invalid value, got %s", got) } } } t.Run("render", func(t *testing.T) { - t.Run("simple", fn("p", map[string]string{}, false, "

")) - t.Run("endslash", fn("img", map[string]string{}, true, "")) - t.Run("attributes", fn("a", map[string]string{"href": "link"}, false, ``)) + t.Run("simple", fn("p", map[string]string{}, "

")) + t.Run("attributes", fn("a", map[string]string{"href": "link"}, ``)) }) } @@ -55,12 +54,12 @@ func TestVoidElement(t *testing.T) { } } t.Run("no_attributes", func(t *testing.T) { - t.Run("simple1", fn("br", nil, "
")) - t.Run("simple2", fn("img", nil, "")) + t.Run("simple1", fn("br", nil, "
")) + t.Run("simple2", fn("img", nil, "")) }) t.Run("attributes", func(t *testing.T) { - t.Run("one", fn("img", map[string]string{"src": "link"}, ``)) - t.Run("two", fn("img", map[string]string{"src": "link", "alt": "well"}, `well`)) + t.Run("one", fn("img", map[string]string{"src": "link"}, ``)) + t.Run("two", fn("img", map[string]string{"src": "link", "alt": "well"}, `well`)) }) } @@ -87,7 +86,7 @@ func TestContentElement(t *testing.T) { t.Run("no_attributes", func(t *testing.T) { t.Run("simple", fnLiteral("p", "", nil, `

`)) t.Run("literal", fnLiteral("p", "content", nil, `

content

`)) - t.Run("elements", fn("div", []Element{NewVoidElement("img"), NewVoidElement("br")}, nil, `

`)) + t.Run("elements", fn("div", []Element{NewVoidElement("img"), NewVoidElement("br")}, nil, `

`)) }) t.Run("attributes", func(t *testing.T) { t.Run("simple_one", fnLiteral("script", "", map[string]string{"src": "link"}, ``)) @@ -113,8 +112,8 @@ func TestElement_ClassList(t *testing.T) { } } t.Run("add", func(t *testing.T) { - t.Run("empty", fn(NewVoidElement("img"), ``)) - t.Run("one", fn(NewVoidElement("img"), ``, "bg")) - t.Run("two", fn(NewVoidElement("img"), ``, "bg", "large")) + t.Run("empty", fn(NewVoidElement("img"), ``)) + t.Run("one", fn(NewVoidElement("img"), ``, "bg")) + t.Run("two", fn(NewVoidElement("img"), ``, "bg", "large")) }) } -- cgit v1.2.3