Skip to content

Commit 4ef7e49

Browse files
authored
Add a trailing slash to dashboard links (#29555) (#29573)
Backport #29555
1 parent b519e47 commit 4ef7e49

File tree

5 files changed

+32
-9
lines changed

5 files changed

+32
-9
lines changed

modules/contexttest/context_tests.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"net/url"
1313
"strings"
1414
"testing"
15+
"time"
1516

1617
access_model "code.gitea.io/gitea/models/perm/access"
1718
repo_model "code.gitea.io/gitea/models/repo"
@@ -61,7 +62,8 @@ func MockContext(t *testing.T, reqPath string, opts ...MockContextOption) (*cont
6162
base.Locale = &translation.MockLocale{}
6263

6364
ctx := context.NewWebContext(base, opt.Render, nil)
64-
65+
ctx.PageData = map[string]any{}
66+
ctx.Data["PageStartTime"] = time.Now()
6567
chiCtx := chi.NewRouteContext()
6668
ctx.Base.AppendContextValue(chi.RouteCtxKey, chiCtx)
6769
return ctx, resp

modules/translation/mock.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ package translation
66
import "fmt"
77

88
// MockLocale provides a mocked locale without any translations
9-
type MockLocale struct{}
9+
type MockLocale struct {
10+
Lang, LangName string // these fields are used directly in templates: ctx.Locale.Lang
11+
}
1012

1113
var _ Locale = (*MockLocale)(nil)
1214

modules/translation/translation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func Match(tags ...language.Tag) language.Tag {
140140
// locale represents the information of localization.
141141
type locale struct {
142142
i18n.Locale
143-
Lang, LangName string // these fields are used directly in templates: .i18n.Lang
143+
Lang, LangName string // these fields are used directly in templates: ctx.Locale.Lang
144144
msgPrinter *message.Printer
145145
}
146146

routers/web/user/home_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ import (
1010
"code.gitea.io/gitea/models/db"
1111
repo_model "code.gitea.io/gitea/models/repo"
1212
"code.gitea.io/gitea/models/unittest"
13+
"code.gitea.io/gitea/modules/context"
1314
"code.gitea.io/gitea/modules/contexttest"
1415
"code.gitea.io/gitea/modules/setting"
16+
"code.gitea.io/gitea/modules/templates"
1517

1618
"github.com/stretchr/testify/assert"
1719
)
@@ -117,3 +119,18 @@ func TestMilestonesForSpecificRepo(t *testing.T) {
117119
assert.Len(t, ctx.Data["Milestones"], 1)
118120
assert.Len(t, ctx.Data["Repos"], 2) // both repo 42 and 1 have milestones and both are owned by user 2
119121
}
122+
123+
func TestDashboardPagination(t *testing.T) {
124+
ctx, _ := contexttest.MockContext(t, "/", contexttest.MockContextOption{Render: templates.HTMLRenderer()})
125+
page := context.NewPagination(10, 3, 1, 3)
126+
127+
setting.AppSubURL = "/SubPath"
128+
out, err := ctx.RenderToString("base/paginate", map[string]any{"Link": setting.AppSubURL, "Page": page})
129+
assert.NoError(t, err)
130+
assert.Contains(t, out, `<a class=" item navigation" href="/SubPath/?page=2">`)
131+
132+
setting.AppSubURL = ""
133+
out, err = ctx.RenderToString("base/paginate", map[string]any{"Link": setting.AppSubURL, "Page": page})
134+
assert.NoError(t, err)
135+
assert.Contains(t, out, `<a class=" item navigation" href="/?page=2">`)
136+
}

templates/base/paginate.tmpl

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
1-
{{$paginationLink := .Page.GetParams}}
1+
{{$paginationParams := .Page.GetParams}}
2+
{{$paginationLink := $.Link}}
3+
{{if eq $paginationLink AppSubUrl}}{{$paginationLink = print $paginationLink "/"}}{{end}}
24
{{with .Page.Paginater}}
35
{{if gt .TotalPages 1}}
46
<div class="center page buttons">
57
<div class="ui borderless pagination menu">
6-
<a class="{{if .IsFirst}}disabled{{end}} item navigation" {{if not .IsFirst}}href="{{$.Link}}{{if $paginationLink}}?{{$paginationLink}}{{end}}"{{end}}>
8+
<a class="{{if .IsFirst}}disabled{{end}} item navigation" {{if not .IsFirst}}href="{{$paginationLink}}{{if $paginationParams}}?{{$paginationParams}}{{end}}"{{end}}>
79
{{svg "gitea-double-chevron-left" 16 "gt-mr-2"}}
810
<span class="navigation_label">{{ctx.Locale.Tr "admin.first_page"}}</span>
911
</a>
10-
<a class="{{if not .HasPrevious}}disabled{{end}} item navigation" {{if .HasPrevious}}href="{{$.Link}}?page={{.Previous}}{{if $paginationLink}}&{{$paginationLink}}{{end}}"{{end}}>
12+
<a class="{{if not .HasPrevious}}disabled{{end}} item navigation" {{if .HasPrevious}}href="{{$paginationLink}}?page={{.Previous}}{{if $paginationParams}}&{{$paginationParams}}{{end}}"{{end}}>
1113
{{svg "octicon-chevron-left" 16 "gt-mr-2"}}
1214
<span class="navigation_label">{{ctx.Locale.Tr "repo.issues.previous"}}</span>
1315
</a>
1416
{{range .Pages}}
1517
{{if eq .Num -1}}
1618
<a class="disabled item">...</a>
1719
{{else}}
18-
<a class="{{if .IsCurrent}}active {{end}}item gt-content-center" {{if not .IsCurrent}}href="{{$.Link}}?page={{.Num}}{{if $paginationLink}}&{{$paginationLink}}{{end}}"{{end}}>{{.Num}}</a>
20+
<a class="{{if .IsCurrent}}active {{end}}item gt-content-center" {{if not .IsCurrent}}href="{{$paginationLink}}?page={{.Num}}{{if $paginationParams}}&{{$paginationParams}}{{end}}"{{end}}>{{.Num}}</a>
1921
{{end}}
2022
{{end}}
21-
<a class="{{if not .HasNext}}disabled{{end}} item navigation" {{if .HasNext}}href="{{$.Link}}?page={{.Next}}{{if $paginationLink}}&{{$paginationLink}}{{end}}"{{end}}>
23+
<a class="{{if not .HasNext}}disabled{{end}} item navigation" {{if .HasNext}}href="{{$paginationLink}}?page={{.Next}}{{if $paginationParams}}&{{$paginationParams}}{{end}}"{{end}}>
2224
<span class="navigation_label">{{ctx.Locale.Tr "repo.issues.next"}}</span>
2325
{{svg "octicon-chevron-right" 16 "gt-ml-2"}}
2426
</a>
25-
<a class="{{if .IsLast}}disabled{{end}} item navigation" {{if not .IsLast}}href="{{$.Link}}?page={{.TotalPages}}{{if $paginationLink}}&{{$paginationLink}}{{end}}"{{end}}>
27+
<a class="{{if .IsLast}}disabled{{end}} item navigation" {{if not .IsLast}}href="{{$paginationLink}}?page={{.TotalPages}}{{if $paginationParams}}&{{$paginationParams}}{{end}}"{{end}}>
2628
<span class="navigation_label">{{ctx.Locale.Tr "admin.last_page"}}</span>
2729
{{svg "gitea-double-chevron-right" 16 "gt-ml-2"}}
2830
</a>

0 commit comments

Comments
 (0)