From 1c65ecc1c62ae33499420c28891b088a98ff1cf7 Mon Sep 17 00:00:00 2001 From: Lauris BH Date: Mon, 4 Nov 2019 20:59:17 +0200 Subject: [PATCH] Fix commit expand button to not go to commit link (#8745) (#8825) * 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 --- modules/templates/helper.go | 41 ++++++++++++++++++++++++------------- public/js/index.js | 3 ++- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/modules/templates/helper.go b/modules/templates/helper.go index 2c53e05fc..81ab13980 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" @@ -331,34 +332,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. diff --git a/public/js/index.js b/public/js/index.js index 1daeef1a5..966fc05ef 100644 --- a/public/js/index.js +++ b/public/js/index.js @@ -2873,7 +2873,8 @@ function initFilterBranchTagDropdown(selector) { }); } -$(".commit-button").click(function() { +$(".commit-button").click(function(e) { + e.preventDefault(); $(this).parent().find('.commit-body').toggle(); });