all repos — gomponents @ 3198b25445ce1dc81158353b3e012e8881014790

HTML components in pure Go

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
commit

3198b25445ce1dc81158353b3e012e8881014790

parent

6d2fb0eeb15d6b9774f127517d160137251264a4

2 files changed, 8 insertions(+), 0 deletions(-)

jump to
M gomponents.gogomponents.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.gogomponents_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) {