Don't render or error on nil children (#32) This makes it easier to e.g. conditionally skip a component for rendering.
Markus Wüstenberg markus@maragu.dk
Fri, 23 Oct 2020 12:14:23 +0200
2 files changed, 8 insertions(+), 0 deletions(-)
M gomponents.go → gomponents.go
@@ -84,6 +84,9 @@ } } func renderChild(c Node, inside, outside *strings.Builder) { + if c == nil { + return + } if g, ok := c.(group); ok { for _, groupC := range g.children { renderChild(groupC, inside, outside)
M gomponents_test.go → gomponents_test.go
@@ -83,6 +83,11 @@ t.Run("renders outside if node does not implement placer", func(t *testing.T) { e := g.El("div", outsider{}) assert.Equal(t, `<div>outsider</div>`, e) }) + + t.Run("does not fail on nil node", func(t *testing.T) { + e := g.El("div", g.El("span"), nil, g.El("span")) + assert.Equal(t, `<div><span /><span /></div>`, e) + }) } func TestText(t *testing.T) {