From 3b478f50b7408ad78d085fa6dbc3c7a683c9d943 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Wed, 25 May 2022 18:46:42 +0200 Subject: Fix HasMenuCurrent and IsDescendant/IsAncestor when comparing to itself There may be sites in the wild that depends on the faulty behaviour of IsDescendant/IsAncestor when comparing to itself, but * The documentation and common sense says that a thing cannot be descendant or ancestor to itself. * The bug introduced in `HasMenuCurrent` comes directly from that confusion. Fixes #9846 --- hugolib/page__tree.go | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) (limited to 'hugolib/page__tree.go') diff --git a/hugolib/page__tree.go b/hugolib/page__tree.go index d2b1f684a..828500e62 100644 --- a/hugolib/page__tree.go +++ b/hugolib/page__tree.go @@ -36,6 +36,9 @@ func (pt pageTree) IsAncestor(other any) (bool, error) { } ref1, ref2 := pt.p.getTreeRef(), tp.getTreeRef() + if ref1 != nil && ref2 != nil && ref1.key == ref2.key { + return false, nil + } if ref1 != nil && ref1.key == "/" { return true, nil @@ -50,10 +53,6 @@ func (pt pageTree) IsAncestor(other any) (bool, error) { return ref1.n.p.IsHome(), nil } - if ref1.key == ref2.key { - return true, nil - } - if strings.HasPrefix(ref2.key, ref1.key) { return true, nil } @@ -82,6 +81,9 @@ func (pt pageTree) IsDescendant(other any) (bool, error) { } ref1, ref2 := pt.p.getTreeRef(), tp.getTreeRef() + if ref1 != nil && ref2 != nil && ref1.key == ref2.key { + return false, nil + } if ref2 != nil && ref2.key == "/" { return true, nil @@ -96,10 +98,6 @@ func (pt pageTree) IsDescendant(other any) (bool, error) { return ref2.n.p.IsHome(), nil } - if ref1.key == ref2.key { - return true, nil - } - if strings.HasPrefix(ref1.key, ref2.key) { return true, nil } -- cgit v1.2.3