ref: b09a40333f382cc1034d2eda856230258ab6b8cc
parent: 7540a62834d4465af8936967e430a9e05a1e1359
author: Bjørn Erik Pedersen <[email protected]>
date: Wed Nov 28 07:36:59 EST 2018
hugolib: Improve nil handling in IsDescendant and IsAncestor Fixes #5461
--- a/hugolib/site_sections.go
+++ b/hugolib/site_sections.go
@@ -104,8 +104,11 @@
// IsDescendant returns whether the current page is a descendant of the given page.
// Note that this method is not relevant for taxonomy lists and taxonomy terms pages.
func (p *Page) IsDescendant(other interface{}) (bool, error) {
+ if p == nil {
+ return false, nil
+ }
pp, err := unwrapPage(other)
- if err != nil {
+ if err != nil || pp == nil {
return false, err
}
@@ -119,8 +122,12 @@
// IsAncestor returns whether the current page is an ancestor of the given page.
// Note that this method is not relevant for taxonomy lists and taxonomy terms pages.
func (p *Page) IsAncestor(other interface{}) (bool, error) {
+ if p == nil {
+ return false, nil
+ }
+
pp, err := unwrapPage(other)
- if err != nil {
+ if err != nil || pp == nil {
return false, err
}
--- a/hugolib/site_sections_test.go
+++ b/hugolib/site_sections_test.go
@@ -238,6 +238,8 @@
assert.Len(p.Sections(), 0)
}},
{"l1,l2,l3", func(p *Page) {
+ var nilp *Page
+
assert.Equal("T3_-1", p.title)
assert.Len(p.Pages, 2)
assert.Equal("T2_-1", p.Parent().title)
@@ -247,6 +249,12 @@
isDescendant, err := l1.IsDescendant(p)
assert.NoError(err)
assert.False(isDescendant)
+ isDescendant, err = l1.IsDescendant(nil)
+ assert.NoError(err)
+ assert.False(isDescendant)
+ isDescendant, err = nilp.IsDescendant(p)
+ assert.NoError(err)
+ assert.False(isDescendant)
isDescendant, err = p.IsDescendant(l1)
assert.NoError(err)
assert.True(isDescendant)
@@ -258,6 +266,12 @@
assert.NoError(err)
assert.False(isAncestor)
assert.Equal(l1, p.FirstSection())
+ isAncestor, err = p.IsAncestor(nil)
+ assert.NoError(err)
+ assert.False(isAncestor)
+ isAncestor, err = nilp.IsAncestor(l1)
+ assert.NoError(err)
+ assert.False(isAncestor)
}},
{"perm a,link", func(p *Page) {