mirror of
https://github.com/gohugoio/hugo.git
synced 2025-09-10 13:12:05 -04:00
Fix nilpointer on ToC heading
Fixes #11843 Co-authored-by: Joe Mooring <joe.mooring@veriphor.com>
This commit is contained in:
parent
b8eb45c9df
commit
4f2d2b2cc4
@ -101,6 +101,10 @@ func IsTruthfulValue(val reflect.Value) (truth bool) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if val.Kind() == reflect.Pointer && val.IsNil() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if val.Type().Implements(zeroType) {
|
if val.Type().Implements(zeroType) {
|
||||||
return !val.Interface().(types.Zeroer).IsZero()
|
return !val.Interface().(types.Zeroer).IsZero()
|
||||||
}
|
}
|
||||||
|
@ -22,13 +22,29 @@ import (
|
|||||||
qt "github.com/frankban/quicktest"
|
qt "github.com/frankban/quicktest"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type zeroStruct struct {
|
||||||
|
zero bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (z zeroStruct) IsZero() bool {
|
||||||
|
return z.zero
|
||||||
|
}
|
||||||
|
|
||||||
func TestIsTruthful(t *testing.T) {
|
func TestIsTruthful(t *testing.T) {
|
||||||
c := qt.New(t)
|
c := qt.New(t)
|
||||||
|
|
||||||
|
var nilpointerZero *zeroStruct
|
||||||
|
|
||||||
c.Assert(IsTruthful(true), qt.Equals, true)
|
c.Assert(IsTruthful(true), qt.Equals, true)
|
||||||
c.Assert(IsTruthful(false), qt.Equals, false)
|
c.Assert(IsTruthful(false), qt.Equals, false)
|
||||||
c.Assert(IsTruthful(time.Now()), qt.Equals, true)
|
c.Assert(IsTruthful(time.Now()), qt.Equals, true)
|
||||||
c.Assert(IsTruthful(time.Time{}), qt.Equals, false)
|
c.Assert(IsTruthful(time.Time{}), qt.Equals, false)
|
||||||
|
c.Assert(IsTruthful(&zeroStruct{zero: false}), qt.Equals, true)
|
||||||
|
c.Assert(IsTruthful(&zeroStruct{zero: true}), qt.Equals, false)
|
||||||
|
c.Assert(IsTruthful(zeroStruct{zero: false}), qt.Equals, true)
|
||||||
|
c.Assert(IsTruthful(zeroStruct{zero: true}), qt.Equals, false)
|
||||||
|
c.Assert(IsTruthful(nil), qt.Equals, false)
|
||||||
|
c.Assert(IsTruthful(nilpointerZero), qt.Equals, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetMethodByName(t *testing.T) {
|
func TestGetMethodByName(t *testing.T) {
|
||||||
|
@ -121,3 +121,16 @@ CONTENT
|
|||||||
b, _ = hugolib.TestE(t, files)
|
b, _ = hugolib.TestE(t, files)
|
||||||
b.AssertLogMatches(`error calling ToHTML: startLevel: unable to cast "x" of type string`)
|
b.AssertLogMatches(`error calling ToHTML: startLevel: unable to cast "x" of type string`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHeadingsNilpointerIssue11843(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
files := `
|
||||||
|
-- hugo.toml --
|
||||||
|
-- layouts/home.html --
|
||||||
|
{{ $h := index .Fragments.HeadingsMap "bad_id" }}
|
||||||
|
{{ if not $h }}OK{{ end }}
|
||||||
|
`
|
||||||
|
b := hugolib.Test(t, files)
|
||||||
|
|
||||||
|
b.AssertFileContent("public/index.html", "OK")
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user