From 766072e776bb4167187436dc69c4d212c1decd4c Mon Sep 17 00:00:00 2001 From: Mahmood Ali Date: Thu, 22 Jul 2021 10:49:34 -0400 Subject: [PATCH] Protect against nil LayoutRoots When `Renderer.LayoutRoot()` is nil, rendering should do nothing, but currently it panics with nil pointer dereference. --- document.go | 3 +++ document_test.go | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/document.go b/document.go index 45a4407..76db4d1 100644 --- a/document.go +++ b/document.go @@ -185,6 +185,9 @@ func (d *Document) RenderFrame() { // Setup our root node root := d.r.LayoutRoot() + if root == nil { + return + } // Build our render tree tree(ctx, root, Fragment(d.els...), false) diff --git a/document_test.go b/document_test.go index 81300f3..22bf9b5 100644 --- a/document_test.go +++ b/document_test.go @@ -1,6 +1,7 @@ package glint import ( + "bytes" "context" "sync/atomic" "testing" @@ -69,6 +70,23 @@ func TestDocument_unmountClose(t *testing.T) { require.Equal(uint32(1), atomic.LoadUint32(&c.unmount)) } +func TestDocument_renderingWithoutLayout(t *testing.T) { + var buf bytes.Buffer + + d := New() + d.SetRenderer(&TerminalRenderer{ + Output: &buf, + }) + + var c testMount + d.Append(&c) + + // Render once + d.RenderFrame() + require.Empty(t, buf.String()) + require.Zero(t, atomic.LoadUint32(&c.mount)) +} + type testMount struct { terminalComponent