summaryrefslogtreecommitdiffstats
path: root/modules/templates/helper.go
diff options
context:
space:
mode:
authorLauris BH <lauris@nix.lv>2019-11-01 06:48:30 +0200
committerGitHub <noreply@github.com>2019-11-01 06:48:30 +0200
commitebcc38188eab15cfd03b3cf2d1771aa189abfc18 (patch)
tree22398d3abf0c8964e898b0bed3094f7aa5147526 /modules/templates/helper.go
parentac0fb36c417376f3967d0d98ae5066d8946c41c9 (diff)
downloadgitea-ebcc38188eab15cfd03b3cf2d1771aa189abfc18.tar.gz
gitea-ebcc38188eab15cfd03b3cf2d1771aa189abfc18.zip
Fix commit expand button to not go to commit link (#8745)
* Fix commit expand button to not go to commit link * Fix message rendering to have correct HTML in result * Fix check for empty commit message * Code optimization
Diffstat (limited to 'modules/templates/helper.go')
-rw-r--r--modules/templates/helper.go41
1 files changed, 27 insertions, 14 deletions
diff --git a/modules/templates/helper.go b/modules/templates/helper.go
index b5287bf971..bdcaa12754 100644
--- a/modules/templates/helper.go
+++ b/modules/templates/helper.go
@@ -19,6 +19,7 @@ import (
"runtime"
"strings"
"time"
+ "unicode"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/base"
@@ -338,34 +339,46 @@ func RenderCommitMessageLink(msg, urlPrefix, urlDefault string, metas map[string
// RenderCommitMessageLinkSubject renders commit message as a XXS-safe link to
// the provided default url, handling for special links without email to links.
func RenderCommitMessageLinkSubject(msg, urlPrefix, urlDefault string, metas map[string]string) template.HTML {
- cleanMsg := template.HTMLEscapeString(msg)
+ msgLine := strings.TrimLeftFunc(msg, unicode.IsSpace)
+ lineEnd := strings.IndexByte(msgLine, '\n')
+ if lineEnd > 0 {
+ msgLine = msgLine[:lineEnd]
+ }
+ msgLine = strings.TrimRightFunc(msgLine, unicode.IsSpace)
+ if len(msgLine) == 0 {
+ return template.HTML("")
+ }
+
// we can safely assume that it will not return any error, since there
// shouldn't be any special HTML.
- fullMessage, err := markup.RenderCommitMessageSubject([]byte(cleanMsg), urlPrefix, urlDefault, metas)
+ renderedMessage, err := markup.RenderCommitMessageSubject([]byte(template.HTMLEscapeString(msgLine)), urlPrefix, urlDefault, metas)
if err != nil {
log.Error("RenderCommitMessageSubject: %v", err)
- return ""
- }
- msgLines := strings.Split(strings.TrimSpace(string(fullMessage)), "\n")
- if len(msgLines) == 0 {
return template.HTML("")
}
- return template.HTML(msgLines[0])
+ return template.HTML(renderedMessage)
}
// RenderCommitBody extracts the body of a commit message without its title.
func RenderCommitBody(msg, urlPrefix string, metas map[string]string) template.HTML {
- cleanMsg := template.HTMLEscapeString(msg)
- fullMessage, err := markup.RenderCommitMessage([]byte(cleanMsg), urlPrefix, "", metas)
+ msgLine := strings.TrimRightFunc(msg, unicode.IsSpace)
+ lineEnd := strings.IndexByte(msgLine, '\n')
+ if lineEnd > 0 {
+ msgLine = msgLine[lineEnd+1:]
+ } else {
+ return template.HTML("")
+ }
+ msgLine = strings.TrimLeftFunc(msgLine, unicode.IsSpace)
+ if len(msgLine) == 0 {
+ return template.HTML("")
+ }
+
+ renderedMessage, err := markup.RenderCommitMessage([]byte(template.HTMLEscapeString(msgLine)), urlPrefix, "", metas)
if err != nil {
log.Error("RenderCommitMessage: %v", err)
return ""
}
- body := strings.Split(strings.TrimSpace(string(fullMessage)), "\n")
- if len(body) == 0 {
- return template.HTML("")
- }
- return template.HTML(strings.Join(body[1:], "\n"))
+ return template.HTML(renderedMessage)
}
// RenderNote renders the contents of a git-notes file as a commit message.