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
This commit is contained in:
Bjørn Erik Pedersen
2022-05-25 18:46:42 +02:00
parent f343b8eb78
commit 3b478f50b7
3 changed files with 74 additions and 12 deletions

View File

@@ -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
}