From 902b3952641c38dec86b796721c94365123df88b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauris=20Buk=C5=A1is-Haberkorns?= Date: Wed, 30 Oct 2019 10:42:20 +0200 Subject: [PATCH 1/4] Fix commit expand button to not go to commit link --- public/js/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/public/js/index.js b/public/js/index.js index bfcf36f52866..53650890f0c9 100644 --- a/public/js/index.js +++ b/public/js/index.js @@ -2994,7 +2994,8 @@ function initFilterBranchTagDropdown(selector) { }); } -$(".commit-button").click(function() { +$(".commit-button").click(function(e) { + e.preventDefault(); $(this).parent().find('.commit-body').toggle(); }); From 28db976fc42b979b408677dcdfbadbc8993d4a90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauris=20Buk=C5=A1is-Haberkorns?= Date: Wed, 30 Oct 2019 13:08:57 +0200 Subject: [PATCH 2/4] Fix message rendering to have correct HTML in result --- modules/templates/helper.go | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/modules/templates/helper.go b/modules/templates/helper.go index b5287bf97130..a27584874de9 100644 --- a/modules/templates/helper.go +++ b/modules/templates/helper.go @@ -339,33 +339,37 @@ func RenderCommitMessageLink(msg, urlPrefix, urlDefault string, metas map[string // 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) + + msgLines := strings.Split(strings.TrimSpace(string(cleanMsg)), "\n") + if len(msgLines) == 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(msgLines[0]), 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) + + msgLines := strings.Split(strings.TrimSpace(string(cleanMsg)), "\n") + if len(msgLines) <= 1 { + return template.HTML("") + } + + renderedMessage, err := markup.RenderCommitMessage([]byte(strings.Join(msgLines[1:], "\n")), 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. From 7c36155d1f89a76ed3b07709f99e215852bccef7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauris=20Buk=C5=A1is-Haberkorns?= Date: Wed, 30 Oct 2019 14:07:49 +0200 Subject: [PATCH 3/4] Fix check for empty commit message --- modules/templates/helper.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/templates/helper.go b/modules/templates/helper.go index a27584874de9..a9fa540eee10 100644 --- a/modules/templates/helper.go +++ b/modules/templates/helper.go @@ -341,7 +341,7 @@ func RenderCommitMessageLinkSubject(msg, urlPrefix, urlDefault string, metas map cleanMsg := template.HTMLEscapeString(msg) msgLines := strings.Split(strings.TrimSpace(string(cleanMsg)), "\n") - if len(msgLines) == 0 { + if len(msgLines[0]) == 0 { return template.HTML("") } From 78d6006f91c62ff5ab8872ab7729137ca7509b05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauris=20Buk=C5=A1is-Haberkorns?= Date: Thu, 31 Oct 2019 16:13:34 +0200 Subject: [PATCH 4/4] Code optimization --- modules/templates/helper.go | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/modules/templates/helper.go b/modules/templates/helper.go index a9fa540eee10..bdcaa12754ab 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,16 +339,19 @@ 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) - - msgLines := strings.Split(strings.TrimSpace(string(cleanMsg)), "\n") - if len(msgLines[0]) == 0 { + 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. - renderedMessage, err := markup.RenderCommitMessageSubject([]byte(msgLines[0]), urlPrefix, urlDefault, metas) + renderedMessage, err := markup.RenderCommitMessageSubject([]byte(template.HTMLEscapeString(msgLine)), urlPrefix, urlDefault, metas) if err != nil { log.Error("RenderCommitMessageSubject: %v", err) return template.HTML("") @@ -357,14 +361,19 @@ func RenderCommitMessageLinkSubject(msg, urlPrefix, urlDefault string, metas map // 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) - - msgLines := strings.Split(strings.TrimSpace(string(cleanMsg)), "\n") - if len(msgLines) <= 1 { + 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(strings.Join(msgLines[1:], "\n")), urlPrefix, "", metas) + renderedMessage, err := markup.RenderCommitMessage([]byte(template.HTMLEscapeString(msgLine)), urlPrefix, "", metas) if err != nil { log.Error("RenderCommitMessage: %v", err) return ""