diff --git a/layout.go b/layout.go index 08751bb..6d6e25c 100644 --- a/layout.go +++ b/layout.go @@ -80,6 +80,9 @@ func ValidateLayoutVariant(variant string) error { // layout.go: H appends nodes to the Header slot. // Example: NewLayout("HCF").H(Raw("head")). func (l *Layout) H(nodes ...Node) *Layout { + if l == nil { + return nil + } l.slots['H'] = append(l.slots['H'], nodes...) return l } @@ -87,6 +90,9 @@ func (l *Layout) H(nodes ...Node) *Layout { // layout.go: L appends nodes to the Left aside slot. // Example: NewLayout("HLCRF").L(Raw("nav")). func (l *Layout) L(nodes ...Node) *Layout { + if l == nil { + return nil + } l.slots['L'] = append(l.slots['L'], nodes...) return l } @@ -94,6 +100,9 @@ func (l *Layout) L(nodes ...Node) *Layout { // layout.go: C appends nodes to the Content (main) slot. // Example: NewLayout("C").C(Raw("body")). func (l *Layout) C(nodes ...Node) *Layout { + if l == nil { + return nil + } l.slots['C'] = append(l.slots['C'], nodes...) return l } @@ -101,6 +110,9 @@ func (l *Layout) C(nodes ...Node) *Layout { // layout.go: R appends nodes to the Right aside slot. // Example: NewLayout("HLCRF").R(Raw("aside")). func (l *Layout) R(nodes ...Node) *Layout { + if l == nil { + return nil + } l.slots['R'] = append(l.slots['R'], nodes...) return l } @@ -108,6 +120,9 @@ func (l *Layout) R(nodes ...Node) *Layout { // layout.go: F appends nodes to the Footer slot. // Example: NewLayout("HCF").F(Raw("foot")). func (l *Layout) F(nodes ...Node) *Layout { + if l == nil { + return nil + } l.slots['F'] = append(l.slots['F'], nodes...) return l } diff --git a/layout_test.go b/layout_test.go index 4ba6e0c..2617831 100644 --- a/layout_test.go +++ b/layout_test.go @@ -250,6 +250,26 @@ func TestLayout_RenderNilReceiver(t *testing.T) { } } +func TestLayout_BuilderNilReceiver(t *testing.T) { + var layout *Layout + + if got := layout.H(Raw("header")); got != nil { + t.Fatalf("nil Layout.H() should return nil, got %v", got) + } + if got := layout.L(Raw("left")); got != nil { + t.Fatalf("nil Layout.L() should return nil, got %v", got) + } + if got := layout.C(Raw("main")); got != nil { + t.Fatalf("nil Layout.C() should return nil, got %v", got) + } + if got := layout.R(Raw("right")); got != nil { + t.Fatalf("nil Layout.R() should return nil, got %v", got) + } + if got := layout.F(Raw("footer")); got != nil { + t.Fatalf("nil Layout.F() should return nil, got %v", got) + } +} + func TestLayout_RenderNilContext(t *testing.T) { layout := NewLayout("C").C(Raw("content")) got := layout.Render(nil) diff --git a/responsive.go b/responsive.go index 4c85f96..28d614e 100644 --- a/responsive.go +++ b/responsive.go @@ -100,6 +100,9 @@ func ScopeVariant(name, selector string) string { // Example: r.Variant("mobile", NewLayout("C").C(Raw("body"))). // Variants render in insertion order. func (r *Responsive) Variant(name string, layout *Layout) *Responsive { + if r == nil { + return nil + } r.variants = append(r.variants, responsiveVariant{name: name, layout: layout}) return r } diff --git a/responsive_test.go b/responsive_test.go index b5aab76..7182b12 100644 --- a/responsive_test.go +++ b/responsive_test.go @@ -96,6 +96,13 @@ func TestResponsive_RenderNilReceiver(t *testing.T) { } } +func TestResponsive_BuilderNilReceiver(t *testing.T) { + var r *Responsive + if got := r.Variant("desktop", NewLayout("C")); got != nil { + t.Fatalf("nil Responsive.Variant() should return nil, got %v", got) + } +} + func TestResponsive_RenderNilContext(t *testing.T) { r := NewResponsive(). Variant("desktop", NewLayout("C").C(Raw("main")))