diff options
| author | Anhgelus Morhtuuzh <william@herges.fr> | 2026-02-03 18:47:14 +0100 |
|---|---|---|
| committer | Anhgelus Morhtuuzh <william@herges.fr> | 2026-02-03 18:47:14 +0100 |
| commit | 80adfde3c830f734eb186a294f9d856c2df81593 (patch) | |
| tree | 411ac4659e630be89945036a700592d707d6b05d /dom | |
| parent | bec5e1d43a516b39d3aceaa022933020f81f2793 (diff) | |
fix(dom): remove trailing slash for void element
was never required for html
Diffstat (limited to 'dom')
| -rw-r--r-- | dom/html.go | 13 | ||||
| -rw-r--r-- | dom/html_test.go | 25 |
2 files changed, 17 insertions, 21 deletions
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, "<p>")) - t.Run("endslash", fn("img", map[string]string{}, true, "<img />")) - t.Run("attributes", fn("a", map[string]string{"href": "link"}, false, `<a href="link">`)) + t.Run("simple", fn("p", map[string]string{}, "<p>")) + t.Run("attributes", fn("a", map[string]string{"href": "link"}, `<a 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, "<br />")) - t.Run("simple2", fn("img", nil, "<img />")) + t.Run("simple1", fn("br", nil, "<br>")) + t.Run("simple2", fn("img", nil, "<img>")) }) t.Run("attributes", func(t *testing.T) { - t.Run("one", fn("img", map[string]string{"src": "link"}, `<img src="link" />`)) - t.Run("two", fn("img", map[string]string{"src": "link", "alt": "well"}, `<img src="link" alt="well" />`)) + t.Run("one", fn("img", map[string]string{"src": "link"}, `<img src="link">`)) + t.Run("two", fn("img", map[string]string{"src": "link", "alt": "well"}, `<img src="link" alt="well">`)) }) } @@ -87,7 +86,7 @@ func TestContentElement(t *testing.T) { t.Run("no_attributes", func(t *testing.T) { t.Run("simple", fnLiteral("p", "", nil, `<p></p>`)) t.Run("literal", fnLiteral("p", "content", nil, `<p>content</p>`)) - t.Run("elements", fn("div", []Element{NewVoidElement("img"), NewVoidElement("br")}, nil, `<div><img /><br /></div>`)) + t.Run("elements", fn("div", []Element{NewVoidElement("img"), NewVoidElement("br")}, nil, `<div><img><br></div>`)) }) t.Run("attributes", func(t *testing.T) { t.Run("simple_one", fnLiteral("script", "", map[string]string{"src": "link"}, `<script src="link"></script>`)) @@ -113,8 +112,8 @@ func TestElement_ClassList(t *testing.T) { } } t.Run("add", func(t *testing.T) { - t.Run("empty", fn(NewVoidElement("img"), `<img />`)) - t.Run("one", fn(NewVoidElement("img"), `<img class="bg" />`, "bg")) - t.Run("two", fn(NewVoidElement("img"), `<img class="bg large" />`, "bg", "large")) + t.Run("empty", fn(NewVoidElement("img"), `<img>`)) + t.Run("one", fn(NewVoidElement("img"), `<img class="bg">`, "bg")) + t.Run("two", fn(NewVoidElement("img"), `<img class="bg large">`, "bg", "large")) }) } |
