dont close void tags
This commit is contained in:
parent
0f04094533
commit
1ac2d630c7
2 changed files with 60 additions and 14 deletions
|
|
@ -105,6 +105,24 @@ func TestConditional(t *testing.T) {
|
|||
assert.Equal(t, "<div ></div>", result)
|
||||
}
|
||||
|
||||
func TestTagSelfClosing(t *testing.T) {
|
||||
t.Parallel()
|
||||
assert.Equal(t, `<input type="text"/>`, Render(
|
||||
Input("text"),
|
||||
))
|
||||
// assert the tag cannot have children
|
||||
assert.Equal(t, `<input type="text"/>`, Render(
|
||||
Input("text", Div()),
|
||||
))
|
||||
|
||||
assert.Equal(t, `<div id="test"></div>`, Render(
|
||||
Div(Id("test")),
|
||||
))
|
||||
assert.Equal(t, `<div id="test"><div ></div></div>`, Render(
|
||||
Div(Id("test"), Div()),
|
||||
))
|
||||
}
|
||||
|
||||
func BenchmarkMailToStatic(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
ctx := RenderContext{
|
||||
|
|
|
|||
|
|
@ -7,6 +7,26 @@ import (
|
|||
"strings"
|
||||
)
|
||||
|
||||
/*
|
||||
*
|
||||
void tags are tags that cannot have children
|
||||
*/
|
||||
var voidTags = map[string]bool{
|
||||
"area": true,
|
||||
"base": true,
|
||||
"br": true,
|
||||
"col": true,
|
||||
"embed": true,
|
||||
"hr": true,
|
||||
"img": true,
|
||||
"input": true,
|
||||
"link": true,
|
||||
"meta": true,
|
||||
"source": true,
|
||||
"track": true,
|
||||
"wbr": true,
|
||||
}
|
||||
|
||||
type RenderContext struct {
|
||||
builder *strings.Builder
|
||||
scripts []string
|
||||
|
|
@ -79,28 +99,36 @@ func (node *Element) Render(context *RenderContext) {
|
|||
|
||||
// close the tag
|
||||
if node.tag != "" {
|
||||
if voidTags[node.tag] {
|
||||
context.builder.WriteString("/")
|
||||
}
|
||||
context.builder.WriteString(">")
|
||||
}
|
||||
|
||||
// render the children elements that are not attributes
|
||||
for _, child := range node.children {
|
||||
switch child.(type) {
|
||||
case *AttributeMap:
|
||||
continue
|
||||
case *AttributeR:
|
||||
continue
|
||||
case *LifeCycle:
|
||||
continue
|
||||
default:
|
||||
child.Render(context)
|
||||
// void elements do not have children
|
||||
if !voidTags[node.tag] {
|
||||
// render the children elements that are not attributes
|
||||
for _, child := range node.children {
|
||||
switch child.(type) {
|
||||
case *AttributeMap:
|
||||
continue
|
||||
case *AttributeR:
|
||||
continue
|
||||
case *LifeCycle:
|
||||
continue
|
||||
default:
|
||||
child.Render(context)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if node.tag != "" {
|
||||
renderScripts(context)
|
||||
context.builder.WriteString("</")
|
||||
context.builder.WriteString(node.tag)
|
||||
context.builder.WriteString(">")
|
||||
if !voidTags[node.tag] {
|
||||
context.builder.WriteString("</")
|
||||
context.builder.WriteString(node.tag)
|
||||
context.builder.WriteString(">")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue