Skip to content

Commit 6ee60a6

Browse files
committed
fix
1 parent e650f64 commit 6ee60a6

File tree

7 files changed

+34
-29
lines changed

7 files changed

+34
-29
lines changed

routers/web/repo/branch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ func CreateBranch(ctx *context.Context) {
231231
if len(e.Message) == 0 {
232232
ctx.Flash.Error(ctx.Tr("repo.editor.push_rejected_no_message"))
233233
} else {
234-
flashError, err := ctx.RenderToString(tplAlertDetails, map[string]any{
234+
flashError, err := ctx.RenderToHTML(tplAlertDetails, map[string]any{
235235
"Message": ctx.Tr("repo.editor.push_rejected"),
236236
"Summary": ctx.Tr("repo.editor.push_rejected_summary"),
237237
"Details": utils.SanitizeFlashErrorString(e.Message),

routers/web/repo/editor.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ func editFilePost(ctx *context.Context, form forms.EditRepoFileForm, isNewFile b
341341
if len(errPushRej.Message) == 0 {
342342
ctx.RenderWithErr(ctx.Tr("repo.editor.push_rejected_no_message"), tplEditFile, &form)
343343
} else {
344-
flashError, err := ctx.RenderToString(tplAlertDetails, map[string]any{
344+
flashError, err := ctx.RenderToHTML(tplAlertDetails, map[string]any{
345345
"Message": ctx.Tr("repo.editor.push_rejected"),
346346
"Summary": ctx.Tr("repo.editor.push_rejected_summary"),
347347
"Details": utils.SanitizeFlashErrorString(errPushRej.Message),
@@ -353,7 +353,7 @@ func editFilePost(ctx *context.Context, form forms.EditRepoFileForm, isNewFile b
353353
ctx.RenderWithErr(flashError, tplEditFile, &form)
354354
}
355355
} else {
356-
flashError, err := ctx.RenderToString(tplAlertDetails, map[string]any{
356+
flashError, err := ctx.RenderToHTML(tplAlertDetails, map[string]any{
357357
"Message": ctx.Tr("repo.editor.fail_to_update_file", form.TreePath),
358358
"Summary": ctx.Tr("repo.editor.fail_to_update_file_summary"),
359359
"Details": utils.SanitizeFlashErrorString(err.Error()),
@@ -542,7 +542,7 @@ func DeleteFilePost(ctx *context.Context) {
542542
if len(errPushRej.Message) == 0 {
543543
ctx.RenderWithErr(ctx.Tr("repo.editor.push_rejected_no_message"), tplDeleteFile, &form)
544544
} else {
545-
flashError, err := ctx.RenderToString(tplAlertDetails, map[string]any{
545+
flashError, err := ctx.RenderToHTML(tplAlertDetails, map[string]any{
546546
"Message": ctx.Tr("repo.editor.push_rejected"),
547547
"Summary": ctx.Tr("repo.editor.push_rejected_summary"),
548548
"Details": utils.SanitizeFlashErrorString(errPushRej.Message),
@@ -742,7 +742,7 @@ func UploadFilePost(ctx *context.Context) {
742742
if len(errPushRej.Message) == 0 {
743743
ctx.RenderWithErr(ctx.Tr("repo.editor.push_rejected_no_message"), tplUploadFile, &form)
744744
} else {
745-
flashError, err := ctx.RenderToString(tplAlertDetails, map[string]any{
745+
flashError, err := ctx.RenderToHTML(tplAlertDetails, map[string]any{
746746
"Message": ctx.Tr("repo.editor.push_rejected"),
747747
"Summary": ctx.Tr("repo.editor.push_rejected_summary"),
748748
"Details": utils.SanitizeFlashErrorString(errPushRej.Message),

routers/web/repo/issue.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
stdCtx "context"
1010
"errors"
1111
"fmt"
12+
"html/template"
1213
"math/big"
1314
"net/http"
1415
"net/url"
@@ -1016,7 +1017,7 @@ func NewIssue(ctx *context.Context) {
10161017
ctx.HTML(http.StatusOK, tplIssueNew)
10171018
}
10181019

1019-
func renderErrorOfTemplates(ctx *context.Context, errs map[string]error) string {
1020+
func renderErrorOfTemplates(ctx *context.Context, errs map[string]error) template.HTML {
10201021
var files []string
10211022
for k := range errs {
10221023
files = append(files, k)
@@ -1028,14 +1029,14 @@ func renderErrorOfTemplates(ctx *context.Context, errs map[string]error) string
10281029
lines = append(lines, fmt.Sprintf("%s: %v", file, errs[file]))
10291030
}
10301031

1031-
flashError, err := ctx.RenderToString(tplAlertDetails, map[string]any{
1032+
flashError, err := ctx.RenderToHTML(tplAlertDetails, map[string]any{
10321033
"Message": ctx.Tr("repo.issues.choose.ignore_invalid_templates"),
10331034
"Summary": ctx.Tr("repo.issues.choose.invalid_templates", len(errs)),
10341035
"Details": utils.SanitizeFlashErrorString(strings.Join(lines, "\n")),
10351036
})
10361037
if err != nil {
10371038
log.Debug("render flash error: %v", err)
1038-
flashError = ctx.Locale.TrString("repo.issues.choose.ignore_invalid_templates")
1039+
flashError = ctx.Locale.Tr("repo.issues.choose.ignore_invalid_templates")
10391040
}
10401041
return flashError
10411042
}
@@ -3296,7 +3297,7 @@ func ChangeIssueReaction(ctx *context.Context) {
32963297
return
32973298
}
32983299

3299-
html, err := ctx.RenderToString(tplReactions, map[string]any{
3300+
html, err := ctx.RenderToHTML(tplReactions, map[string]any{
33003301
"ctxData": ctx.Data,
33013302
"ActionURL": fmt.Sprintf("%s/issues/%d/reactions", ctx.Repo.RepoLink, issue.Index),
33023303
"Reactions": issue.Reactions.GroupByType(),
@@ -3403,7 +3404,7 @@ func ChangeCommentReaction(ctx *context.Context) {
34033404
return
34043405
}
34053406

3406-
html, err := ctx.RenderToString(tplReactions, map[string]any{
3407+
html, err := ctx.RenderToHTML(tplReactions, map[string]any{
34073408
"ctxData": ctx.Data,
34083409
"ActionURL": fmt.Sprintf("%s/comments/%d/reactions", ctx.Repo.RepoLink, comment.ID),
34093410
"Reactions": comment.Reactions.GroupByType(),
@@ -3546,8 +3547,8 @@ func updateAttachments(ctx *context.Context, item any, files []string) error {
35463547
return err
35473548
}
35483549

3549-
func attachmentsHTML(ctx *context.Context, attachments []*repo_model.Attachment, content string) string {
3550-
attachHTML, err := ctx.RenderToString(tplAttachment, map[string]any{
3550+
func attachmentsHTML(ctx *context.Context, attachments []*repo_model.Attachment, content string) template.HTML {
3551+
attachHTML, err := ctx.RenderToHTML(tplAttachment, map[string]any{
35513552
"ctxData": ctx.Data,
35523553
"Attachments": attachments,
35533554
"Content": content,

routers/web/repo/pull.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,7 +1129,7 @@ func UpdatePullRequest(ctx *context.Context) {
11291129
if err = pull_service.Update(ctx, issue.PullRequest, ctx.Doer, message, rebase); err != nil {
11301130
if models.IsErrMergeConflicts(err) {
11311131
conflictError := err.(models.ErrMergeConflicts)
1132-
flashError, err := ctx.RenderToString(tplAlertDetails, map[string]any{
1132+
flashError, err := ctx.RenderToHTML(tplAlertDetails, map[string]any{
11331133
"Message": ctx.Tr("repo.pulls.merge_conflict"),
11341134
"Summary": ctx.Tr("repo.pulls.merge_conflict_summary"),
11351135
"Details": utils.SanitizeFlashErrorString(conflictError.StdErr) + "<br>" + utils.SanitizeFlashErrorString(conflictError.StdOut),
@@ -1143,7 +1143,7 @@ func UpdatePullRequest(ctx *context.Context) {
11431143
return
11441144
} else if models.IsErrRebaseConflicts(err) {
11451145
conflictError := err.(models.ErrRebaseConflicts)
1146-
flashError, err := ctx.RenderToString(tplAlertDetails, map[string]any{
1146+
flashError, err := ctx.RenderToHTML(tplAlertDetails, map[string]any{
11471147
"Message": ctx.Tr("repo.pulls.rebase_conflict", utils.SanitizeFlashErrorString(conflictError.CommitSHA)),
11481148
"Summary": ctx.Tr("repo.pulls.rebase_conflict_summary"),
11491149
"Details": utils.SanitizeFlashErrorString(conflictError.StdErr) + "<br>" + utils.SanitizeFlashErrorString(conflictError.StdOut),
@@ -1275,7 +1275,7 @@ func MergePullRequest(ctx *context.Context) {
12751275
ctx.JSONError(ctx.Tr("repo.pulls.invalid_merge_option"))
12761276
} else if models.IsErrMergeConflicts(err) {
12771277
conflictError := err.(models.ErrMergeConflicts)
1278-
flashError, err := ctx.RenderToString(tplAlertDetails, map[string]any{
1278+
flashError, err := ctx.RenderToHTML(tplAlertDetails, map[string]any{
12791279
"Message": ctx.Tr("repo.editor.merge_conflict"),
12801280
"Summary": ctx.Tr("repo.editor.merge_conflict_summary"),
12811281
"Details": utils.SanitizeFlashErrorString(conflictError.StdErr) + "<br>" + utils.SanitizeFlashErrorString(conflictError.StdOut),
@@ -1288,7 +1288,7 @@ func MergePullRequest(ctx *context.Context) {
12881288
ctx.JSONRedirect(issue.Link())
12891289
} else if models.IsErrRebaseConflicts(err) {
12901290
conflictError := err.(models.ErrRebaseConflicts)
1291-
flashError, err := ctx.RenderToString(tplAlertDetails, map[string]any{
1291+
flashError, err := ctx.RenderToHTML(tplAlertDetails, map[string]any{
12921292
"Message": ctx.Tr("repo.pulls.rebase_conflict", utils.SanitizeFlashErrorString(conflictError.CommitSHA)),
12931293
"Summary": ctx.Tr("repo.pulls.rebase_conflict_summary"),
12941294
"Details": utils.SanitizeFlashErrorString(conflictError.StdErr) + "<br>" + utils.SanitizeFlashErrorString(conflictError.StdOut),
@@ -1318,7 +1318,7 @@ func MergePullRequest(ctx *context.Context) {
13181318
if len(message) == 0 {
13191319
ctx.Flash.Error(ctx.Tr("repo.pulls.push_rejected_no_message"))
13201320
} else {
1321-
flashError, err := ctx.RenderToString(tplAlertDetails, map[string]any{
1321+
flashError, err := ctx.RenderToHTML(tplAlertDetails, map[string]any{
13221322
"Message": ctx.Tr("repo.pulls.push_rejected"),
13231323
"Summary": ctx.Tr("repo.pulls.push_rejected_summary"),
13241324
"Details": utils.SanitizeFlashErrorString(pushrejErr.Message),
@@ -1491,7 +1491,7 @@ func CompareAndPullRequestPost(ctx *context.Context) {
14911491
ctx.JSONError(ctx.Tr("repo.pulls.push_rejected_no_message"))
14921492
return
14931493
}
1494-
flashError, err := ctx.RenderToString(tplAlertDetails, map[string]any{
1494+
flashError, err := ctx.RenderToHTML(tplAlertDetails, map[string]any{
14951495
"Message": ctx.Tr("repo.pulls.push_rejected"),
14961496
"Summary": ctx.Tr("repo.pulls.push_rejected_summary"),
14971497
"Details": utils.SanitizeFlashErrorString(pushrejErr.Message),
@@ -1500,8 +1500,7 @@ func CompareAndPullRequestPost(ctx *context.Context) {
15001500
ctx.ServerError("CompareAndPullRequest.HTMLString", err)
15011501
return
15021502
}
1503-
ctx.Flash.Error(flashError)
1504-
ctx.JSONRedirect(ctx.Link + "?" + ctx.Req.URL.RawQuery) // FIXME: it's unfriendly, and will make the content lost
1503+
ctx.JSONError(flashError)
15051504
return
15061505
}
15071506
ctx.ServerError("NewPullRequest", err)

services/context/context_response.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package context
66
import (
77
"errors"
88
"fmt"
9+
"html/template"
910
"net"
1011
"net/http"
1112
"net/url"
@@ -104,11 +105,11 @@ func (ctx *Context) JSONTemplate(tmpl base.TplName) {
104105
}
105106
}
106107

107-
// RenderToString renders the template content to a string
108-
func (ctx *Context) RenderToString(name base.TplName, data map[string]any) (string, error) {
108+
// RenderToHTML renders the template content to a HTML string
109+
func (ctx *Context) RenderToHTML(name base.TplName, data map[string]any) (template.HTML, error) {
109110
var buf strings.Builder
110-
err := ctx.Render.HTML(&buf, http.StatusOK, string(name), data, ctx.TemplateContext)
111-
return buf.String(), err
111+
err := ctx.Render.HTML(&buf, 0, string(name), data, ctx.TemplateContext)
112+
return template.HTML(buf.String()), err
112113
}
113114

114115
// RenderWithErr used for page has form validation but need to prompt error to users.

web_src/js/features/common-global.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,19 +91,24 @@ async function fetchActionDoRequest(actionElem, url, opt) {
9191
} else {
9292
window.location.reload();
9393
}
94+
return;
9495
} else if (resp.status >= 400 && resp.status < 500) {
9596
const data = await resp.json();
9697
// the code was quite messy, sometimes the backend uses "err", sometimes it uses "error", and even "user_error"
9798
// but at the moment, as a new approach, we only use "errorMessage" here, backend can use JSONError() to respond.
98-
showErrorToast(data.errorMessage || `server error: ${resp.status}`);
99+
if (data.errorMessage) {
100+
showErrorToast(data.errorMessage, {useHtmlBody: data.renderFormat === 'html'});
101+
} else {
102+
showErrorToast(`server error: ${resp.status}`);
103+
}
99104
} else {
100105
showErrorToast(`server error: ${resp.status}`);
101106
}
102107
} catch (e) {
103108
console.error('error when doRequest', e);
104-
actionElem.classList.remove('is-loading', 'small-loading-icon');
105109
showErrorToast(i18n.network_error);
106110
}
111+
actionElem.classList.remove('is-loading', 'small-loading-icon');
107112
}
108113

109114
async function formFetchAction(e) {

web_src/js/modules/toast.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,12 @@ const levels = {
2121
};
2222

2323
// See https://github.com/apvarun/toastify-js#api for options
24-
function showToast(message, level, {gravity, position, duration, ...other} = {}) {
24+
function showToast(message, level, {gravity, position, duration, useHtmlBody, ...other} = {}) {
2525
const {icon, background, duration: levelDuration} = levels[level ?? 'info'];
26-
2726
const toast = Toastify({
2827
text: `
2928
<div class='toast-icon'>${svg(icon)}</div>
30-
<div class='toast-body'>${htmlEscape(message)}</div>
29+
<div class='toast-body'>${useHtmlBody ? message : htmlEscape(message)}</div>
3130
<button class='toast-close'>${svg('octicon-x')}</button>
3231
`,
3332
escapeMarkup: false,

0 commit comments

Comments
 (0)