Skip to content

Commit 3731090

Browse files
committed
fix
1 parent b068dbd commit 3731090

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+264
-189
lines changed

modules/templates/helper.go

+24-17
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,6 @@ func NewFuncMap() template.FuncMap {
7373
return fmt.Sprint(time.Since(startTime).Nanoseconds()/1e6) + "ms"
7474
},
7575

76-
// for backward compatibility only, do not use them anymore
77-
"TimeSince": timeSinceLegacy,
78-
"TimeSinceUnix": timeSinceLegacy,
79-
"DateTime": dateTimeLegacy,
80-
8176
// -----------------------------------------------------------------
8277
// setting
8378
"AppName": func() string {
@@ -156,18 +151,8 @@ func NewFuncMap() template.FuncMap {
156151

157152
// -----------------------------------------------------------------
158153
// render
159-
"RenderCommitMessage": RenderCommitMessage,
160-
"RenderCommitMessageLinkSubject": renderCommitMessageLinkSubject,
161-
162-
"RenderCommitBody": renderCommitBody,
163-
"RenderCodeBlock": renderCodeBlock,
164-
"RenderIssueTitle": renderIssueTitle,
165-
"RenderEmoji": renderEmoji,
166-
"ReactionToEmoji": reactionToEmoji,
167-
168-
"RenderMarkdownToHtml": RenderMarkdownToHtml,
169-
"RenderLabel": renderLabel,
170-
"RenderLabels": RenderLabels,
154+
"RenderCodeBlock": renderCodeBlock,
155+
"ReactionToEmoji": reactionToEmoji,
171156

172157
// -----------------------------------------------------------------
173158
// misc
@@ -179,6 +164,22 @@ func NewFuncMap() template.FuncMap {
179164

180165
"FilenameIsImage": filenameIsImage,
181166
"TabSizeClass": tabSizeClass,
167+
168+
// for backward compatibility only, do not use them anymore
169+
"TimeSince": timeSinceLegacy,
170+
"TimeSinceUnix": timeSinceLegacy,
171+
"DateTime": dateTimeLegacy,
172+
173+
"RenderEmoji": renderEmojiLegacy,
174+
"RenderLabel": renderLabelLegacy,
175+
"RenderLabels": renderLabelsLegacy,
176+
"RenderIssueTitle": renderIssueTitleLegacy,
177+
178+
"RenderMarkdownToHtml": renderMarkdownToHtmlLegacy,
179+
180+
"RenderCommitMessage": renderCommitMessageLegacy,
181+
"RenderCommitMessageLinkSubject": renderCommitMessageLinkSubjectLegacy,
182+
"RenderCommitBody": renderCommitBodyLegacy,
182183
}
183184
}
184185

@@ -296,3 +297,9 @@ func userThemeName(user *user_model.User) string {
296297
}
297298
return setting.UI.DefaultTheme
298299
}
300+
301+
func panicIfDevOrTesting() {
302+
if !setting.IsProd || setting.IsInTesting {
303+
panic("legacy template functions are for backward compatibility only, do not use them in new code")
304+
}
305+
}

modules/templates/util_date.go

-18
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212

1313
"code.gitea.io/gitea/modules/setting"
1414
"code.gitea.io/gitea/modules/timeutil"
15-
"code.gitea.io/gitea/modules/translation"
1615
)
1716

1817
type DateUtils struct{}
@@ -54,23 +53,6 @@ func parseLegacy(datetime string) time.Time {
5453
return t
5554
}
5655

57-
func dateTimeLegacy(format string, datetime any, _ ...string) template.HTML {
58-
if !setting.IsProd || setting.IsInTesting {
59-
panic("dateTimeLegacy is for backward compatibility only, do not use it in new code")
60-
}
61-
if s, ok := datetime.(string); ok {
62-
datetime = parseLegacy(s)
63-
}
64-
return dateTimeFormat(format, datetime)
65-
}
66-
67-
func timeSinceLegacy(time any, _ translation.Locale) template.HTML {
68-
if !setting.IsProd || setting.IsInTesting {
69-
panic("timeSinceLegacy is for backward compatibility only, do not use it in new code")
70-
}
71-
return TimeSince(time)
72-
}
73-
7456
func anyToTime(any any) (t time.Time, isZero bool) {
7557
switch v := any.(type) {
7658
case nil:

modules/templates/util_date_legacy.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package templates
5+
6+
import (
7+
"html/template"
8+
9+
"code.gitea.io/gitea/modules/translation"
10+
)
11+
12+
func dateTimeLegacy(format string, datetime any, _ ...string) template.HTML {
13+
panicIfDevOrTesting()
14+
if s, ok := datetime.(string); ok {
15+
datetime = parseLegacy(s)
16+
}
17+
return dateTimeFormat(format, datetime)
18+
}
19+
20+
func timeSinceLegacy(time any, _ translation.Locale) template.HTML {
21+
panicIfDevOrTesting()
22+
return TimeSince(time)
23+
}

modules/templates/util_render.go

+34-27
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,21 @@ import (
2424
"code.gitea.io/gitea/modules/util"
2525
)
2626

27+
type RenderUtils struct {
28+
ctx context.Context
29+
}
30+
31+
func NewRenderUtils(ctx context.Context) *RenderUtils {
32+
return &RenderUtils{ctx: ctx}
33+
}
34+
2735
// RenderCommitMessage renders commit message with XSS-safe and special links.
28-
func RenderCommitMessage(ctx context.Context, msg string, metas map[string]string) template.HTML {
36+
func (ut *RenderUtils) RenderCommitMessage(msg string, metas map[string]string) template.HTML {
2937
cleanMsg := template.HTMLEscapeString(msg)
3038
// we can safely assume that it will not return any error, since there
3139
// shouldn't be any special HTML.
3240
fullMessage, err := markup.RenderCommitMessage(&markup.RenderContext{
33-
Ctx: ctx,
41+
Ctx: ut.ctx,
3442
Metas: metas,
3543
}, cleanMsg)
3644
if err != nil {
@@ -44,9 +52,9 @@ func RenderCommitMessage(ctx context.Context, msg string, metas map[string]strin
4452
return renderCodeBlock(template.HTML(msgLines[0]))
4553
}
4654

47-
// renderCommitMessageLinkSubject renders commit message as a XSS-safe link to
55+
// RenderCommitMessageLinkSubject renders commit message as a XSS-safe link to
4856
// the provided default url, handling for special links without email to links.
49-
func renderCommitMessageLinkSubject(ctx context.Context, msg, urlDefault string, metas map[string]string) template.HTML {
57+
func (ut *RenderUtils) RenderCommitMessageLinkSubject(msg, urlDefault string, metas map[string]string) template.HTML {
5058
msgLine := strings.TrimLeftFunc(msg, unicode.IsSpace)
5159
lineEnd := strings.IndexByte(msgLine, '\n')
5260
if lineEnd > 0 {
@@ -60,7 +68,7 @@ func renderCommitMessageLinkSubject(ctx context.Context, msg, urlDefault string,
6068
// we can safely assume that it will not return any error, since there
6169
// shouldn't be any special HTML.
6270
renderedMessage, err := markup.RenderCommitMessageSubject(&markup.RenderContext{
63-
Ctx: ctx,
71+
Ctx: ut.ctx,
6472
DefaultLink: urlDefault,
6573
Metas: metas,
6674
}, template.HTMLEscapeString(msgLine))
@@ -71,8 +79,8 @@ func renderCommitMessageLinkSubject(ctx context.Context, msg, urlDefault string,
7179
return renderCodeBlock(template.HTML(renderedMessage))
7280
}
7381

74-
// renderCommitBody extracts the body of a commit message without its title.
75-
func renderCommitBody(ctx context.Context, msg string, metas map[string]string) template.HTML {
82+
// RenderCommitBody extracts the body of a commit message without its title.
83+
func (ut *RenderUtils) RenderCommitBody(msg string, metas map[string]string) template.HTML {
7684
msgLine := strings.TrimSpace(msg)
7785
lineEnd := strings.IndexByte(msgLine, '\n')
7886
if lineEnd > 0 {
@@ -86,7 +94,7 @@ func renderCommitBody(ctx context.Context, msg string, metas map[string]string)
8694
}
8795

8896
renderedMessage, err := markup.RenderCommitMessage(&markup.RenderContext{
89-
Ctx: ctx,
97+
Ctx: ut.ctx,
9098
Metas: metas,
9199
}, template.HTMLEscapeString(msgLine))
92100
if err != nil {
@@ -105,22 +113,22 @@ func renderCodeBlock(htmlEscapedTextToRender template.HTML) template.HTML {
105113
return template.HTML(htmlWithCodeTags)
106114
}
107115

108-
// renderIssueTitle renders issue/pull title with defined post processors
109-
func renderIssueTitle(ctx context.Context, text string, metas map[string]string) template.HTML {
116+
// RenderIssueTitle renders issue/pull title with defined post processors
117+
func (ut *RenderUtils) RenderIssueTitle(text string, metas map[string]string) template.HTML {
110118
renderedText, err := markup.RenderIssueTitle(&markup.RenderContext{
111-
Ctx: ctx,
119+
Ctx: ut.ctx,
112120
Metas: metas,
113121
}, template.HTMLEscapeString(text))
114122
if err != nil {
115123
log.Error("RenderIssueTitle: %v", err)
116-
return template.HTML("")
124+
return ""
117125
}
118126
return template.HTML(renderedText)
119127
}
120128

121-
// renderLabel renders a label
122-
// locale is needed due to an import cycle with our context providing the `Tr` function
123-
func renderLabel(ctx context.Context, locale translation.Locale, label *issues_model.Label) template.HTML {
129+
// RenderLabel renders a label
130+
func (ut *RenderUtils) RenderLabel(label *issues_model.Label) template.HTML {
131+
locale := ut.ctx.Value(translation.ContextKey).(translation.Locale)
124132
var extraCSSClasses string
125133
textColor := util.ContrastColor(label.Color)
126134
labelScope := label.ExclusiveScope()
@@ -134,12 +142,12 @@ func renderLabel(ctx context.Context, locale translation.Locale, label *issues_m
134142
if labelScope == "" {
135143
// Regular label
136144
return HTMLFormat(`<div class="ui label %s" style="color: %s !important; background-color: %s !important;" data-tooltip-content title="%s">%s</div>`,
137-
extraCSSClasses, textColor, label.Color, descriptionText, renderEmoji(ctx, label.Name))
145+
extraCSSClasses, textColor, label.Color, descriptionText, ut.RenderEmoji(label.Name))
138146
}
139147

140148
// Scoped label
141-
scopeHTML := renderEmoji(ctx, labelScope)
142-
itemHTML := renderEmoji(ctx, label.Name[len(labelScope)+1:])
149+
scopeHTML := ut.RenderEmoji(labelScope)
150+
itemHTML := ut.RenderEmoji(label.Name[len(labelScope)+1:])
143151

144152
// Make scope and item background colors slightly darker and lighter respectively.
145153
// More contrast needed with higher luminance, empirically tweaked.
@@ -176,13 +184,12 @@ func renderLabel(ctx context.Context, locale translation.Locale, label *issues_m
176184
textColor, itemColor, itemHTML)
177185
}
178186

179-
// renderEmoji renders html text with emoji post processors
180-
func renderEmoji(ctx context.Context, text string) template.HTML {
181-
renderedText, err := markup.RenderEmoji(&markup.RenderContext{Ctx: ctx},
182-
template.HTMLEscapeString(text))
187+
// RenderEmoji renders html text with emoji post processors
188+
func (ut *RenderUtils) RenderEmoji(text string) template.HTML {
189+
renderedText, err := markup.RenderEmoji(&markup.RenderContext{Ctx: ut.ctx}, template.HTMLEscapeString(text))
183190
if err != nil {
184191
log.Error("RenderEmoji: %v", err)
185-
return template.HTML("")
192+
return ""
186193
}
187194
return template.HTML(renderedText)
188195
}
@@ -200,9 +207,9 @@ func reactionToEmoji(reaction string) template.HTML {
200207
return template.HTML(fmt.Sprintf(`<img alt=":%s:" src="%s/assets/img/emoji/%s.png"></img>`, reaction, setting.StaticURLPrefix, url.PathEscape(reaction)))
201208
}
202209

203-
func RenderMarkdownToHtml(ctx context.Context, input string) template.HTML { //nolint:revive
210+
func (ut *RenderUtils) MarkdownToHtml(input string) template.HTML { //nolint:revive
204211
output, err := markdown.RenderString(&markup.RenderContext{
205-
Ctx: ctx,
212+
Ctx: ut.ctx,
206213
Metas: map[string]string{"mode": "document"},
207214
}, input)
208215
if err != nil {
@@ -211,7 +218,7 @@ func RenderMarkdownToHtml(ctx context.Context, input string) template.HTML { //n
211218
return output
212219
}
213220

214-
func RenderLabels(ctx context.Context, locale translation.Locale, labels []*issues_model.Label, repoLink string, issue *issues_model.Issue) template.HTML {
221+
func (ut *RenderUtils) RenderLabels(labels []*issues_model.Label, repoLink string, issue *issues_model.Issue) template.HTML {
215222
isPullRequest := issue != nil && issue.IsPull
216223
baseLink := fmt.Sprintf("%s/%s", repoLink, util.Iif(isPullRequest, "pulls", "issues"))
217224
htmlCode := `<span class="labels-list">`
@@ -220,7 +227,7 @@ func RenderLabels(ctx context.Context, locale translation.Locale, labels []*issu
220227
if label == nil {
221228
continue
222229
}
223-
htmlCode += fmt.Sprintf(`<a href="%s?labels=%d">%s</a>`, baseLink, label.ID, renderLabel(ctx, locale, label))
230+
htmlCode += fmt.Sprintf(`<a href="%s?labels=%d">%s</a>`, baseLink, label.ID, ut.RenderLabel(label))
224231
}
225232
htmlCode += "</span>"
226233
return template.HTML(htmlCode)
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package templates
5+
6+
import (
7+
"context"
8+
"html/template"
9+
10+
issues_model "code.gitea.io/gitea/models/issues"
11+
"code.gitea.io/gitea/modules/translation"
12+
)
13+
14+
func renderEmojiLegacy(ctx context.Context, text string) template.HTML {
15+
panicIfDevOrTesting()
16+
return NewRenderUtils(ctx).RenderEmoji(text)
17+
}
18+
19+
func renderLabelLegacy(ctx context.Context, locale translation.Locale, label *issues_model.Label) template.HTML {
20+
panicIfDevOrTesting()
21+
return NewRenderUtils(ctx).RenderLabel(label)
22+
}
23+
24+
func renderLabelsLegacy(ctx context.Context, locale translation.Locale, labels []*issues_model.Label, repoLink string, issue *issues_model.Issue) template.HTML {
25+
panicIfDevOrTesting()
26+
return NewRenderUtils(ctx).RenderLabels(labels, repoLink, issue)
27+
}
28+
29+
func renderMarkdownToHtmlLegacy(ctx context.Context, input string) template.HTML { //nolint:revive
30+
panicIfDevOrTesting()
31+
return NewRenderUtils(ctx).MarkdownToHtml(input)
32+
}
33+
34+
func renderCommitMessageLegacy(ctx context.Context, msg string, metas map[string]string) template.HTML {
35+
panicIfDevOrTesting()
36+
return NewRenderUtils(ctx).RenderCommitMessage(msg, metas)
37+
}
38+
39+
func renderCommitMessageLinkSubjectLegacy(ctx context.Context, msg, urlDefault string, metas map[string]string) template.HTML {
40+
panicIfDevOrTesting()
41+
return NewRenderUtils(ctx).RenderCommitMessageLinkSubject(msg, urlDefault, metas)
42+
}
43+
44+
func renderIssueTitleLegacy(ctx context.Context, text string, metas map[string]string) template.HTML {
45+
panicIfDevOrTesting()
46+
return NewRenderUtils(ctx).RenderIssueTitle(text, metas)
47+
}
48+
49+
func renderCommitBodyLegacy(ctx context.Context, msg string, metas map[string]string) template.HTML {
50+
panicIfDevOrTesting()
51+
return NewRenderUtils(ctx).RenderCommitBody(msg, metas)
52+
}

routers/web/feed/convert.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ func renderMarkdown(ctx *context.Context, act *activities_model.Action, content
7171

7272
// feedActionsToFeedItems convert gitea's Action feed to feeds Item
7373
func feedActionsToFeedItems(ctx *context.Context, actions activities_model.ActionList) (items []*feeds.Item, err error) {
74+
renderUtils := templates.NewRenderUtils(ctx)
7475
for _, act := range actions {
7576
act.LoadActUser(ctx)
7677

@@ -215,7 +216,7 @@ func feedActionsToFeedItems(ctx *context.Context, actions activities_model.Actio
215216
desc += fmt.Sprintf("<a href=\"%s\">%s</a>\n%s",
216217
html.EscapeString(fmt.Sprintf("%s/commit/%s", act.GetRepoAbsoluteLink(ctx), commit.Sha1)),
217218
commit.Sha1,
218-
templates.RenderCommitMessage(ctx, commit.Message, nil),
219+
renderUtils.RenderCommitMessage(commit.Message, nil),
219220
)
220221
}
221222

routers/web/org/projects.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ func Projects(ctx *context.Context) {
103103
} else {
104104
ctx.Data["State"] = "open"
105105
}
106-
106+
renderUtils := templates.NewRenderUtils(ctx)
107107
for _, project := range projects {
108-
project.RenderedContent = templates.RenderMarkdownToHtml(ctx, project.Description)
108+
project.RenderedContent = renderUtils.MarkdownToHtml(project.Description)
109109
}
110110

111111
err = shared_user.LoadHeaderCount(ctx)
@@ -435,7 +435,7 @@ func ViewProject(ctx *context.Context) {
435435
ctx.Data["SelectLabels"] = selectLabels
436436
ctx.Data["AssigneeID"] = assigneeID
437437

438-
project.RenderedContent = templates.RenderMarkdownToHtml(ctx, project.Description)
438+
project.RenderedContent = templates.NewRenderUtils(ctx).MarkdownToHtml(project.Description)
439439
ctx.Data["LinkedPRs"] = linkedPrsMap
440440
ctx.Data["PageIsViewProjects"] = true
441441
ctx.Data["CanWriteProjects"] = canWriteProjects(ctx)

routers/web/repo/issue.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2215,7 +2215,7 @@ func GetIssueInfo(ctx *context.Context) {
22152215

22162216
ctx.JSON(http.StatusOK, map[string]any{
22172217
"convertedIssue": convert.ToIssue(ctx, ctx.Doer, issue),
2218-
"renderedLabels": templates.RenderLabels(ctx, ctx.Locale, issue.Labels, ctx.Repo.RepoLink, issue),
2218+
"renderedLabels": templates.NewRenderUtils(ctx).RenderLabels(issue.Labels, ctx.Repo.RepoLink, issue),
22192219
})
22202220
}
22212221

services/context/context.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ func NewTemplateContextForWeb(ctx *Context) TemplateContext {
100100
tmplCtx := NewTemplateContext(ctx)
101101
tmplCtx["Locale"] = ctx.Base.Locale
102102
tmplCtx["AvatarUtils"] = templates.NewAvatarUtils(ctx)
103+
tmplCtx["RenderUtils"] = templates.NewRenderUtils(ctx)
103104
tmplCtx["RootData"] = ctx.Data
104105
tmplCtx["Consts"] = map[string]any{
105106
"RepoUnitTypeCode": unit.TypeCode,
@@ -154,7 +155,9 @@ func Contexter() func(next http.Handler) http.Handler {
154155
ctx := NewWebContext(base, rnd, session.GetContextSession(req))
155156

156157
ctx.Data.MergeFrom(middleware.CommonTemplateContextData())
157-
ctx.Data["Context"] = ctx // TODO: use "ctx" in template and remove this
158+
if setting.IsProd && !setting.IsInTesting {
159+
ctx.Data["Context"] = ctx // TODO: use "ctx" in template and remove this
160+
}
158161
ctx.Data["CurrentURL"] = setting.AppSubURL + req.URL.RequestURI()
159162
ctx.Data["Link"] = ctx.Link
160163

0 commit comments

Comments
 (0)