Skip to content

Commit

Permalink
Fix HasMenuCurrent and IsDescendant/IsAncestor when comparing to itself
Browse files Browse the repository at this point in the history
Fixes #9846
  • Loading branch information
bep committed May 25, 2022
1 parent 8ca7052 commit 561ab91
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
8 changes: 4 additions & 4 deletions hugolib/content_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,8 @@ Blog Section: {{ template "print-page" $blog }}
Blog Sub Section: {{ template "print-page" $blogSub }}
Page: {{ template "print-page" $page }}
Bundle: {{ template "print-page" $bundle }}
IsDescendant: true: {{ $page.IsDescendant $blog }} true: {{ $blogSub.IsDescendant $blog }} true: {{ $bundle.IsDescendant $blog }} true: {{ $page4.IsDescendant $blog }} true: {{ $blog.IsDescendant $home }} true: {{ $blog.IsDescendant $blog }} false: {{ $home.IsDescendant $blog }}
IsAncestor: true: {{ $blog.IsAncestor $page }} true: {{ $home.IsAncestor $blog }} true: {{ $blog.IsAncestor $blogSub }} true: {{ $blog.IsAncestor $bundle }} true: {{ $blog.IsAncestor $page4 }} true: {{ $home.IsAncestor $page }} true: {{ $blog.IsAncestor $blog }} false: {{ $page.IsAncestor $blog }} false: {{ $blog.IsAncestor $home }} false: {{ $blogSub.IsAncestor $blog }}
IsDescendant: true: {{ $page.IsDescendant $blog }} true: {{ $blogSub.IsDescendant $blog }} true: {{ $bundle.IsDescendant $blog }} true: {{ $page4.IsDescendant $blog }} true: {{ $blog.IsDescendant $home }} false: {{ $blog.IsDescendant $blog }} false: {{ $home.IsDescendant $blog }}
IsAncestor: true: {{ $blog.IsAncestor $page }} true: {{ $home.IsAncestor $blog }} true: {{ $blog.IsAncestor $blogSub }} true: {{ $blog.IsAncestor $bundle }} true: {{ $blog.IsAncestor $page4 }} true: {{ $home.IsAncestor $page }} false: {{ $blog.IsAncestor $blog }} false: {{ $page.IsAncestor $blog }} false: {{ $blog.IsAncestor $home }} false: {{ $blogSub.IsAncestor $blog }}
IsDescendant overlap1: false: {{ $overlap1.IsDescendant $overlap2 }}
IsDescendant overlap2: false: {{ $overlap2.IsDescendant $overlap1 }}
IsAncestor overlap1: false: {{ $overlap1.IsAncestor $overlap2 }}
Expand Down Expand Up @@ -426,8 +426,8 @@ Draft5: {{ if (.Site.GetPage "blog/draftsection/sub/page") }}FOUND{{ end }}|
Blog Sub Section: Page 3|/blog/subsection/|2019-06-03|Current Section: blog/subsection|Resources: application: /blog/subsection/subdata.json|
Page: Page 1|/blog/page1/|2019-06-01|Current Section: blog|Resources:
Bundle: Page 12|/blog/bundle/|0001-01-01|Current Section: blog|Resources: application: /blog/bundle/data.json|page: |
IsDescendant: true: true true: true true: true true: true true: true true: true false: false
IsAncestor: true: true true: true true: true true: true true: true true: true true: true false: false false: false false: false
IsDescendant: true: true true: true true: true true: true true: true false: false false: false
IsAncestor: true: true true: true true: true true: true true: true true: true false: false false: false false: false false: false
IsDescendant overlap1: false: false
IsDescendant overlap2: false: false
IsAncestor overlap1: false: false
Expand Down
47 changes: 47 additions & 0 deletions hugolib/menu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,3 +524,50 @@ Blog|HasMenuCurrent: false|Page: Page(/blog/_index.md)
Blog|IsMenuCurrent: false|Page: Page(/blog/_index.md)
`)
}

// Issue 9846
func TestMenuHasMenuCurrentSection(t *testing.T) {
t.Parallel()

files := `
-- config.toml --
disableKinds = ['RSS','sitemap','taxonomy','term', 'home']
[[menu.main]]
name = 'Home'
pageRef = '/'
weight = 1
[[menu.main]]
name = 'Tests'
pageRef = '/tests'
weight = 2
[[menu.main]]
name = 'Test 1'
pageRef = '/tests/test-1'
parent = 'Tests'
weight = 1
-- content/tests/test-1.md --
---
title: "Test 1"
---
-- layouts/_default/list.html --
{{ range site.Menus.main }}
{{ .Name }}|{{ .URL }}|IsMenuCurrent = {{ $.IsMenuCurrent "main" . }}|HasMenuCurrent = {{ $.HasMenuCurrent "main" . }}|
{{ range .Children }}
{{ .Name }}|{{ .URL }}|IsMenuCurrent = {{ $.IsMenuCurrent "main" . }}|HasMenuCurrent = {{ $.HasMenuCurrent "main" . }}|
{{ end }}
{{ end }}
`

b := NewIntegrationTestBuilder(
IntegrationTestConfig{
T: t,
TxtarString: files,
},
).Build()

b.AssertFileContent("public/tests/index.html", `
Tests|/tests/|IsMenuCurrent = true|HasMenuCurrent = false
`)
}
4 changes: 2 additions & 2 deletions hugolib/page__tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (pt pageTree) IsAncestor(other any) (bool, error) {
}

if ref1.key == ref2.key {
return true, nil
return false, nil
}

if strings.HasPrefix(ref2.key, ref1.key) {
Expand Down Expand Up @@ -97,7 +97,7 @@ func (pt pageTree) IsDescendant(other any) (bool, error) {
}

if ref1.key == ref2.key {
return true, nil
return false, nil
}

if strings.HasPrefix(ref1.key, ref2.key) {
Expand Down

0 comments on commit 561ab91

Please sign in to comment.