2018-09-29 11:33:54 +03:00
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
2014-04-10 14:20:58 -04:00
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2014-04-10 14:20:58 -04:00
|
|
|
|
2016-12-06 18:58:31 +01:00
|
|
|
package templates
|
2014-04-10 14:20:58 -04:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2026-03-29 12:24:30 +02:00
|
|
|
"html"
|
2014-04-10 14:20:58 -04:00
|
|
|
"html/template"
|
2017-11-28 01:43:51 -08:00
|
|
|
"net/url"
|
2025-04-01 12:14:01 +02:00
|
|
|
"strconv"
|
2014-04-10 14:20:58 -04:00
|
|
|
"strings"
|
2026-03-29 12:24:30 +02:00
|
|
|
"sync"
|
2014-04-10 14:20:58 -04:00
|
|
|
"time"
|
2014-05-25 20:11:25 -04:00
|
|
|
|
2016-11-10 17:24:48 +01:00
|
|
|
"code.gitea.io/gitea/modules/base"
|
2024-11-18 13:25:42 +08:00
|
|
|
"code.gitea.io/gitea/modules/htmlutil"
|
2017-09-17 01:17:57 +08:00
|
|
|
"code.gitea.io/gitea/modules/markup"
|
2026-03-29 12:24:30 +02:00
|
|
|
"code.gitea.io/gitea/modules/public"
|
2016-11-10 17:24:48 +01:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2020-07-12 11:10:56 +02:00
|
|
|
"code.gitea.io/gitea/modules/svg"
|
2023-04-07 21:25:49 +08:00
|
|
|
"code.gitea.io/gitea/modules/templates/eval"
|
2019-08-15 22:46:21 +08:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2019-09-06 10:20:09 +08:00
|
|
|
"code.gitea.io/gitea/services/gitdiff"
|
2014-04-10 14:20:58 -04:00
|
|
|
)
|
|
|
|
|
|
2026-04-04 01:56:31 +08:00
|
|
|
func newFuncMapWebPage() template.FuncMap {
|
2023-07-04 20:36:08 +02:00
|
|
|
return map[string]any{
|
2023-04-29 20:02:29 +08:00
|
|
|
"DumpVar": dumpVar,
|
2024-11-11 04:07:54 +08:00
|
|
|
"NIL": func() any { return nil },
|
2023-04-29 20:02:29 +08:00
|
|
|
|
2023-04-08 21:15:22 +08:00
|
|
|
// -----------------------------------------------------------------
|
|
|
|
|
// html/template related functions
|
2024-03-01 18:16:19 +08:00
|
|
|
"dict": dict, // it's lowercase because this name has been widely used. Our other functions should have uppercase names.
|
2024-06-19 06:32:45 +08:00
|
|
|
"Iif": iif,
|
|
|
|
|
"Eval": evalTokens,
|
2025-01-08 11:44:32 +08:00
|
|
|
"HTMLFormat": htmlFormat,
|
2024-06-19 06:32:45 +08:00
|
|
|
"QueryEscape": queryEscape,
|
2024-12-10 11:38:22 +08:00
|
|
|
"QueryBuild": QueryBuild,
|
2024-03-01 18:16:19 +08:00
|
|
|
"SanitizeHTML": SanitizeHTML,
|
2023-04-08 21:15:22 +08:00
|
|
|
|
|
|
|
|
"PathEscape": url.PathEscape,
|
|
|
|
|
"PathEscapeSegments": util.PathEscapeSegments,
|
|
|
|
|
|
2023-04-23 02:16:22 +08:00
|
|
|
// utils
|
|
|
|
|
"StringUtils": NewStringUtils,
|
|
|
|
|
"SliceUtils": NewSliceUtils,
|
2023-04-29 20:02:29 +08:00
|
|
|
"JsonUtils": NewJsonUtils,
|
2024-11-03 05:04:53 +08:00
|
|
|
"DateUtils": NewDateUtils,
|
2023-04-08 21:15:22 +08:00
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------
|
2024-04-07 18:19:25 +02:00
|
|
|
// svg / avatar / icon / color
|
2023-08-10 11:19:39 +08:00
|
|
|
"svg": svg.RenderHTML,
|
2024-06-19 06:32:45 +08:00
|
|
|
"MigrationIcon": migrationIcon,
|
|
|
|
|
"ActionIcon": actionIcon,
|
|
|
|
|
"SortArrow": sortArrow,
|
2024-04-07 18:19:25 +02:00
|
|
|
"ContrastColor": util.ContrastColor,
|
2023-04-08 21:15:22 +08:00
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------
|
|
|
|
|
// time / number / format
|
2026-04-04 01:56:31 +08:00
|
|
|
"ShortSha": base.ShortSha,
|
2024-11-04 19:30:00 +08:00
|
|
|
"FileSize": base.FileSize,
|
2024-12-22 23:33:19 +08:00
|
|
|
"CountFmt": countFmt,
|
2025-02-02 18:51:12 +01:00
|
|
|
"Sec2Hour": util.SecToHours,
|
2024-12-05 15:07:53 +02:00
|
|
|
|
|
|
|
|
"TimeEstimateString": timeEstimateString,
|
|
|
|
|
|
2023-04-08 21:15:22 +08:00
|
|
|
"LoadTimes": func(startTime time.Time) string {
|
2025-04-01 12:14:01 +02:00
|
|
|
return strconv.FormatInt(time.Since(startTime).Nanoseconds()/1e6, 10) + "ms"
|
2023-04-08 21:15:22 +08:00
|
|
|
},
|
|
|
|
|
|
2026-03-29 12:24:30 +02:00
|
|
|
"AssetURI": public.AssetURI,
|
|
|
|
|
"ScriptImport": scriptImport,
|
2026-04-04 01:56:31 +08:00
|
|
|
|
2023-04-08 21:15:22 +08:00
|
|
|
// -----------------------------------------------------------------
|
|
|
|
|
// setting
|
2016-03-06 16:40:04 -05:00
|
|
|
"AppName": func() string {
|
|
|
|
|
return setting.AppName
|
|
|
|
|
},
|
|
|
|
|
"AppSubUrl": func() string {
|
2016-11-27 18:14:25 +08:00
|
|
|
return setting.AppSubURL
|
2016-03-06 16:40:04 -05:00
|
|
|
},
|
2021-05-08 16:27:25 +02:00
|
|
|
"AssetUrlPrefix": func() string {
|
2021-04-28 20:35:06 +08:00
|
|
|
return setting.StaticURLPrefix + "/assets"
|
2019-10-22 14:11:01 +02:00
|
|
|
},
|
2016-03-06 16:40:04 -05:00
|
|
|
"AppVer": func() string {
|
|
|
|
|
return setting.AppVer
|
|
|
|
|
},
|
2026-04-04 01:56:31 +08:00
|
|
|
"AppDomain": func() string { // TODO: helm registry still uses it, need to use current request host in the future
|
2016-03-06 16:40:04 -05:00
|
|
|
return setting.Domain
|
|
|
|
|
},
|
2016-09-01 07:01:32 +02:00
|
|
|
"ShowFooterTemplateLoadTime": func() bool {
|
2023-04-23 07:38:25 +08:00
|
|
|
return setting.Other.ShowFooterTemplateLoadTime
|
2016-09-01 07:01:32 +02:00
|
|
|
},
|
2024-04-03 09:01:50 -07:00
|
|
|
"ShowFooterPoweredBy": func() bool {
|
|
|
|
|
return setting.Other.ShowFooterPoweredBy
|
|
|
|
|
},
|
2019-12-28 00:43:56 +01:00
|
|
|
"AllowedReactions": func() []string {
|
|
|
|
|
return setting.UI.Reactions
|
|
|
|
|
},
|
2021-06-29 16:28:38 +02:00
|
|
|
"CustomEmojis": func() map[string]string {
|
|
|
|
|
return setting.UI.CustomEmojisMap
|
|
|
|
|
},
|
2017-04-01 03:03:01 +02:00
|
|
|
"MetaAuthor": func() string {
|
|
|
|
|
return setting.UI.Meta.Author
|
|
|
|
|
},
|
|
|
|
|
"MetaDescription": func() string {
|
|
|
|
|
return setting.UI.Meta.Description
|
|
|
|
|
},
|
|
|
|
|
"MetaKeywords": func() string {
|
|
|
|
|
return setting.UI.Meta.Keywords
|
|
|
|
|
},
|
2021-02-19 23:06:56 +00:00
|
|
|
"EnableTimetracking": func() bool {
|
|
|
|
|
return setting.Service.EnableTimetracking
|
|
|
|
|
},
|
2021-02-11 18:34:34 +01:00
|
|
|
"DisableWebhooks": func() bool {
|
|
|
|
|
return setting.DisableWebhooks
|
|
|
|
|
},
|
2023-07-04 20:36:08 +02:00
|
|
|
"NotificationSettings": func() map[string]any {
|
|
|
|
|
return map[string]any{
|
2020-05-07 22:49:00 +01:00
|
|
|
"MinTimeout": int(setting.UI.Notification.MinTimeout / time.Millisecond),
|
|
|
|
|
"TimeoutStep": int(setting.UI.Notification.TimeoutStep / time.Millisecond),
|
|
|
|
|
"MaxTimeout": int(setting.UI.Notification.MaxTimeout / time.Millisecond),
|
|
|
|
|
"EventSourceUpdateTime": int(setting.UI.Notification.EventSourceUpdateTime / time.Millisecond),
|
2020-04-24 04:57:38 +01:00
|
|
|
}
|
|
|
|
|
},
|
2023-04-08 21:15:22 +08:00
|
|
|
"MermaidMaxSourceCharacters": func() int {
|
|
|
|
|
return setting.MermaidMaxSourceCharacters
|
|
|
|
|
},
|
2020-11-08 17:21:54 +00:00
|
|
|
|
2023-04-08 21:15:22 +08:00
|
|
|
// -----------------------------------------------------------------
|
|
|
|
|
// render
|
2024-11-05 14:04:26 +08:00
|
|
|
"RenderCodeBlock": renderCodeBlock,
|
|
|
|
|
"ReactionToEmoji": reactionToEmoji,
|
2023-04-08 21:15:22 +08:00
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------
|
2026-03-07 21:37:37 +01:00
|
|
|
// misc (TODO: move them to MiscUtils to avoid bloating the main func map)
|
2023-04-08 21:15:22 +08:00
|
|
|
"ActionContent2Commits": ActionContent2Commits,
|
2024-06-19 06:32:45 +08:00
|
|
|
"IsMultilineCommitMessage": isMultilineCommitMessage,
|
2023-04-08 21:15:22 +08:00
|
|
|
"CommentMustAsDiff": gitdiff.CommentMustAsDiff,
|
|
|
|
|
"MirrorRemoteAddress": mirrorRemoteAddress,
|
|
|
|
|
|
2024-06-19 06:32:45 +08:00
|
|
|
"FilenameIsImage": filenameIsImage,
|
|
|
|
|
"TabSizeClass": tabSizeClass,
|
2023-04-30 20:22:23 +08:00
|
|
|
}
|
2019-11-07 10:34:28 -03:00
|
|
|
}
|
|
|
|
|
|
2025-06-22 18:53:33 +08:00
|
|
|
// SanitizeHTML sanitizes the input by default sanitization rules.
|
2024-03-04 20:02:45 +08:00
|
|
|
func SanitizeHTML(s string) template.HTML {
|
2025-06-22 18:53:33 +08:00
|
|
|
return markup.Sanitize(s)
|
2015-08-08 17:10:34 +08:00
|
|
|
}
|
|
|
|
|
|
2025-01-08 11:44:32 +08:00
|
|
|
func htmlFormat(s any, args ...any) template.HTML {
|
|
|
|
|
if len(args) == 0 {
|
|
|
|
|
// to prevent developers from calling "HTMLFormat $userInput" by mistake which will lead to XSS
|
|
|
|
|
panic("missing arguments for HTMLFormat")
|
|
|
|
|
}
|
|
|
|
|
switch v := s.(type) {
|
|
|
|
|
case string:
|
|
|
|
|
return htmlutil.HTMLFormat(template.HTML(v), args...)
|
|
|
|
|
case template.HTML:
|
|
|
|
|
return htmlutil.HTMLFormat(v, args...)
|
|
|
|
|
}
|
|
|
|
|
panic(fmt.Sprintf("unexpected type %T", s))
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-19 06:32:45 +08:00
|
|
|
func queryEscape(s string) template.URL {
|
2024-03-13 21:32:30 +08:00
|
|
|
return template.URL(url.QueryEscape(s))
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-19 06:32:45 +08:00
|
|
|
// iif is an "inline-if", similar util.Iif[T] but templates need the non-generic version,
|
|
|
|
|
// and it could be simply used as "{{iif expr trueVal}}" (omit the falseVal).
|
|
|
|
|
func iif(condition any, vals ...any) any {
|
2024-06-11 22:52:12 +08:00
|
|
|
if isTemplateTruthy(condition) {
|
2024-04-17 23:58:37 +08:00
|
|
|
return vals[0]
|
|
|
|
|
} else if len(vals) > 1 {
|
|
|
|
|
return vals[1]
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-11 22:52:12 +08:00
|
|
|
func isTemplateTruthy(v any) bool {
|
2024-12-22 23:33:19 +08:00
|
|
|
truth, _ := template.IsTrue(v)
|
|
|
|
|
return truth
|
2024-06-11 21:07:10 +08:00
|
|
|
}
|
|
|
|
|
|
2024-06-19 06:32:45 +08:00
|
|
|
// evalTokens evaluates the expression by tokens and returns the result, see the comment of eval.Expr for details.
|
2023-04-07 21:25:49 +08:00
|
|
|
// To use this helper function in templates, pass each token as a separate parameter.
|
|
|
|
|
//
|
|
|
|
|
// {{ $int64 := Eval $var "+" 1 }}
|
|
|
|
|
// {{ $float64 := Eval $var "+" 1.0 }}
|
|
|
|
|
//
|
|
|
|
|
// Golang's template supports comparable int types, so the int64 result can be used in later statements like {{if lt $int64 10}}
|
2024-06-19 06:32:45 +08:00
|
|
|
func evalTokens(tokens ...any) (any, error) {
|
2023-04-07 21:25:49 +08:00
|
|
|
n, err := eval.Expr(tokens...)
|
|
|
|
|
return n.Value, err
|
|
|
|
|
}
|
2024-04-24 00:18:41 +08:00
|
|
|
|
2024-12-31 12:22:09 +08:00
|
|
|
func isQueryParamEmpty(v any) bool {
|
|
|
|
|
return v == nil || v == false || v == 0 || v == int64(0) || v == ""
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-10 11:38:22 +08:00
|
|
|
// QueryBuild builds a query string from a list of key-value pairs.
|
2024-12-31 12:22:09 +08:00
|
|
|
// It omits the nil, false, zero int/int64 and empty string values,
|
|
|
|
|
// because they are default empty values for "ctx.FormXxx" calls.
|
|
|
|
|
// If 0 or false need to be included, use string values: "0" and "false".
|
|
|
|
|
// Build rules:
|
|
|
|
|
// * Even parameters: always build as query string: a=b&c=d
|
|
|
|
|
// * Odd parameters:
|
|
|
|
|
// * * {"/anything", param-pairs...} => "/?param-paris"
|
|
|
|
|
// * * {"anything?old-params", new-param-pairs...} => "anything?old-params&new-param-paris"
|
|
|
|
|
// * * Otherwise: {"old¶ms", new-param-pairs...} => "old¶ms&new-param-paris"
|
|
|
|
|
// * * Other behaviors are undefined yet.
|
2024-12-10 11:38:22 +08:00
|
|
|
func QueryBuild(a ...any) template.URL {
|
2024-12-31 12:22:09 +08:00
|
|
|
var reqPath, s string
|
|
|
|
|
hasTrailingSep := false
|
2024-12-08 20:44:17 +08:00
|
|
|
if len(a)%2 == 1 {
|
|
|
|
|
if v, ok := a[0].(string); ok {
|
|
|
|
|
s = v
|
2024-12-09 15:54:59 +08:00
|
|
|
} else if v, ok := a[0].(template.URL); ok {
|
2024-12-08 20:44:17 +08:00
|
|
|
s = string(v)
|
|
|
|
|
} else {
|
2024-12-10 11:38:22 +08:00
|
|
|
panic("QueryBuild: invalid argument")
|
2024-12-08 20:44:17 +08:00
|
|
|
}
|
2024-12-31 12:22:09 +08:00
|
|
|
hasTrailingSep = s != "&" && strings.HasSuffix(s, "&")
|
|
|
|
|
if strings.HasPrefix(s, "/") || strings.Contains(s, "?") {
|
|
|
|
|
if s1, s2, ok := strings.Cut(s, "?"); ok {
|
|
|
|
|
reqPath = s1 + "?"
|
|
|
|
|
s = s2
|
|
|
|
|
} else {
|
|
|
|
|
reqPath += s + "?"
|
|
|
|
|
s = ""
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-12-08 20:44:17 +08:00
|
|
|
}
|
|
|
|
|
for i := len(a) % 2; i < len(a); i += 2 {
|
|
|
|
|
k, ok := a[i].(string)
|
|
|
|
|
if !ok {
|
2024-12-10 11:38:22 +08:00
|
|
|
panic("QueryBuild: invalid argument")
|
2024-12-08 20:44:17 +08:00
|
|
|
}
|
|
|
|
|
var v string
|
|
|
|
|
if va, ok := a[i+1].(string); ok {
|
|
|
|
|
v = va
|
|
|
|
|
} else if a[i+1] != nil {
|
2024-12-31 12:22:09 +08:00
|
|
|
if !isQueryParamEmpty(a[i+1]) {
|
|
|
|
|
v = fmt.Sprint(a[i+1])
|
|
|
|
|
}
|
2024-12-08 20:44:17 +08:00
|
|
|
}
|
|
|
|
|
// pos1 to pos2 is the "k=v&" part, "&" is optional
|
|
|
|
|
pos1 := strings.Index(s, "&"+k+"=")
|
|
|
|
|
if pos1 != -1 {
|
|
|
|
|
pos1++
|
2024-12-31 12:22:09 +08:00
|
|
|
} else if strings.HasPrefix(s, k+"=") {
|
|
|
|
|
pos1 = 0
|
2024-12-08 20:44:17 +08:00
|
|
|
}
|
|
|
|
|
pos2 := len(s)
|
|
|
|
|
if pos1 == -1 {
|
|
|
|
|
pos1 = len(s)
|
|
|
|
|
} else {
|
|
|
|
|
pos2 = pos1 + 1
|
|
|
|
|
for pos2 < len(s) && s[pos2-1] != '&' {
|
|
|
|
|
pos2++
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if v != "" {
|
|
|
|
|
sep := ""
|
2024-12-31 12:22:09 +08:00
|
|
|
hasPrefixSep := pos1 == 0 || (pos1 <= len(s) && s[pos1-1] == '&')
|
2024-12-08 20:44:17 +08:00
|
|
|
if !hasPrefixSep {
|
|
|
|
|
sep = "&"
|
|
|
|
|
}
|
|
|
|
|
s = s[:pos1] + sep + k + "=" + url.QueryEscape(v) + "&" + s[pos2:]
|
|
|
|
|
} else {
|
|
|
|
|
s = s[:pos1] + s[pos2:]
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-12-31 12:22:09 +08:00
|
|
|
if s != "" && s[len(s)-1] == '&' && !hasTrailingSep {
|
2024-12-08 20:44:17 +08:00
|
|
|
s = s[:len(s)-1]
|
|
|
|
|
}
|
2024-12-31 12:22:09 +08:00
|
|
|
if reqPath != "" {
|
|
|
|
|
if s == "" {
|
|
|
|
|
s = reqPath
|
|
|
|
|
if s != "?" {
|
|
|
|
|
s = s[:len(s)-1]
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if s[0] == '&' {
|
|
|
|
|
s = s[1:]
|
|
|
|
|
}
|
|
|
|
|
s = reqPath + s
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-12-09 15:54:59 +08:00
|
|
|
return template.URL(s)
|
2024-12-08 20:44:17 +08:00
|
|
|
}
|
2026-03-29 12:24:30 +02:00
|
|
|
|
|
|
|
|
var globalVars = sync.OnceValue(func() (ret struct {
|
|
|
|
|
scriptImportRemainingPart string
|
|
|
|
|
},
|
|
|
|
|
) {
|
|
|
|
|
// add onerror handler to alert users when the script fails to load:
|
|
|
|
|
// * for end users: there were many users reporting that "UI doesn't work", actually they made mistakes in their config
|
|
|
|
|
// * for developers: help them to remember to run "make watch-frontend" to build frontend assets
|
|
|
|
|
// the message will be directly put in the onerror JS code's string
|
|
|
|
|
onScriptErrorPrompt := `Please make sure the asset files can be accessed.`
|
|
|
|
|
if !setting.IsProd {
|
|
|
|
|
onScriptErrorPrompt += `\n\nFor development, run: make watch-frontend.`
|
|
|
|
|
}
|
|
|
|
|
onScriptErrorJS := fmt.Sprintf(`alert('Failed to load asset file from ' + this.src + '. %s')`, onScriptErrorPrompt)
|
|
|
|
|
ret.scriptImportRemainingPart = `onerror="` + html.EscapeString(onScriptErrorJS) + `"></script>`
|
|
|
|
|
return ret
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
func scriptImport(path string, typ ...string) template.HTML {
|
|
|
|
|
if len(typ) > 0 {
|
|
|
|
|
if typ[0] == "module" {
|
|
|
|
|
return template.HTML(`<script type="module" src="` + html.EscapeString(public.AssetURI(path)) + `" ` + globalVars().scriptImportRemainingPart)
|
|
|
|
|
}
|
|
|
|
|
panic("unsupported script type: " + typ[0])
|
|
|
|
|
}
|
|
|
|
|
return template.HTML(`<script src="` + html.EscapeString(public.AssetURI(path)) + `" ` + globalVars().scriptImportRemainingPart)
|
|
|
|
|
}
|