fix(html): default nil render contexts
All checks were successful
Security Scan / security (push) Successful in 9s
Test / test (push) Successful in 48s

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-03-31 19:22:02 +00:00
parent f9f0aa197b
commit 65c0dd3e27
4 changed files with 27 additions and 0 deletions

View file

@ -109,6 +109,9 @@ func (l *Layout) Render(ctx *Context) string {
if l == nil {
return ""
}
if ctx == nil {
ctx = NewContext()
}
b := newTextBuilder()

View file

@ -137,3 +137,13 @@ func TestLayout_Methods_NilLayout_Ugly(t *testing.T) {
t.Fatalf("nil layout render should be empty, got %q", got)
}
}
func TestLayout_Render_NilContext_Good(t *testing.T) {
layout := NewLayout("C").C(Raw("content"))
got := layout.Render(nil)
want := `<main role="main" data-block="C-0">content</main>`
if got != want {
t.Fatalf("layout.Render(nil) = %q, want %q", got, want)
}
}

View file

@ -38,6 +38,9 @@ func (r *Responsive) Render(ctx *Context) string {
if r == nil {
return ""
}
if ctx == nil {
ctx = NewContext()
}
b := newTextBuilder()
for _, v := range r.variants {

View file

@ -99,3 +99,14 @@ func TestResponsive_Variant_NilResponsive_Ugly(t *testing.T) {
t.Fatalf("unexpected output from nil receiver Variant path: %q", output)
}
}
func TestResponsive_Render_NilContext_Good(t *testing.T) {
r := NewResponsive().
Variant("mobile", NewLayout("C").C(Raw("content")))
got := r.Render(nil)
want := `<div data-variant="mobile"><main role="main" data-block="C-0">content</main></div>`
if got != want {
t.Fatalf("responsive.Render(nil) = %q, want %q", got, want)
}
}