Skip to content

Commit ebcc381

Browse files
authored
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
1 parent ac0fb36 commit ebcc381

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

modules/templates/helper.go

+27-14
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"runtime"
2020
"strings"
2121
"time"
22+
"unicode"
2223

2324
"code.gitea.io/gitea/models"
2425
"code.gitea.io/gitea/modules/base"
@@ -338,34 +339,46 @@ func RenderCommitMessageLink(msg, urlPrefix, urlDefault string, metas map[string
338339
// RenderCommitMessageLinkSubject renders commit message as a XXS-safe link to
339340
// the provided default url, handling for special links without email to links.
340341
func RenderCommitMessageLinkSubject(msg, urlPrefix, urlDefault string, metas map[string]string) template.HTML {
341-
cleanMsg := template.HTMLEscapeString(msg)
342+
msgLine := strings.TrimLeftFunc(msg, unicode.IsSpace)
343+
lineEnd := strings.IndexByte(msgLine, '\n')
344+
if lineEnd > 0 {
345+
msgLine = msgLine[:lineEnd]
346+
}
347+
msgLine = strings.TrimRightFunc(msgLine, unicode.IsSpace)
348+
if len(msgLine) == 0 {
349+
return template.HTML("")
350+
}
351+
342352
// we can safely assume that it will not return any error, since there
343353
// shouldn't be any special HTML.
344-
fullMessage, err := markup.RenderCommitMessageSubject([]byte(cleanMsg), urlPrefix, urlDefault, metas)
354+
renderedMessage, err := markup.RenderCommitMessageSubject([]byte(template.HTMLEscapeString(msgLine)), urlPrefix, urlDefault, metas)
345355
if err != nil {
346356
log.Error("RenderCommitMessageSubject: %v", err)
347-
return ""
348-
}
349-
msgLines := strings.Split(strings.TrimSpace(string(fullMessage)), "\n")
350-
if len(msgLines) == 0 {
351357
return template.HTML("")
352358
}
353-
return template.HTML(msgLines[0])
359+
return template.HTML(renderedMessage)
354360
}
355361

356362
// RenderCommitBody extracts the body of a commit message without its title.
357363
func RenderCommitBody(msg, urlPrefix string, metas map[string]string) template.HTML {
358-
cleanMsg := template.HTMLEscapeString(msg)
359-
fullMessage, err := markup.RenderCommitMessage([]byte(cleanMsg), urlPrefix, "", metas)
364+
msgLine := strings.TrimRightFunc(msg, unicode.IsSpace)
365+
lineEnd := strings.IndexByte(msgLine, '\n')
366+
if lineEnd > 0 {
367+
msgLine = msgLine[lineEnd+1:]
368+
} else {
369+
return template.HTML("")
370+
}
371+
msgLine = strings.TrimLeftFunc(msgLine, unicode.IsSpace)
372+
if len(msgLine) == 0 {
373+
return template.HTML("")
374+
}
375+
376+
renderedMessage, err := markup.RenderCommitMessage([]byte(template.HTMLEscapeString(msgLine)), urlPrefix, "", metas)
360377
if err != nil {
361378
log.Error("RenderCommitMessage: %v", err)
362379
return ""
363380
}
364-
body := strings.Split(strings.TrimSpace(string(fullMessage)), "\n")
365-
if len(body) == 0 {
366-
return template.HTML("")
367-
}
368-
return template.HTML(strings.Join(body[1:], "\n"))
381+
return template.HTML(renderedMessage)
369382
}
370383

371384
// RenderNote renders the contents of a git-notes file as a commit message.

public/js/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -2994,7 +2994,8 @@ function initFilterBranchTagDropdown(selector) {
29942994
});
29952995
}
29962996

2997-
$(".commit-button").click(function() {
2997+
$(".commit-button").click(function(e) {
2998+
e.preventDefault();
29982999
$(this).parent().find('.commit-body').toggle();
29993000
});
30003001

0 commit comments

Comments
 (0)