Skip to content

Commit c89901d

Browse files
committed
fix
1 parent ffd5e06 commit c89901d

6 files changed

Lines changed: 47 additions & 12 deletions

File tree

modules/htmlutil/html.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,34 @@ func HTMLPrintTag(w io.Writer, tag template.HTML, attrs map[string]string) (writ
8383
written += n
8484
return written, err
8585
}
86+
87+
func EscapeString(s string) template.HTML {
88+
return template.HTML(template.HTMLEscapeString(s))
89+
}
90+
91+
type HTMLBuilder struct {
92+
sb strings.Builder
93+
}
94+
95+
func (b *HTMLBuilder) WriteString(s string) *HTMLBuilder {
96+
b.sb.WriteString(template.HTMLEscapeString(s))
97+
return b
98+
}
99+
100+
func (b *HTMLBuilder) WriteHTML(s template.HTML) *HTMLBuilder {
101+
b.sb.WriteString(string(s))
102+
return b
103+
}
104+
105+
func (b *HTMLBuilder) WriteFormat(fmt template.HTML, args ...any) *HTMLBuilder {
106+
_, _ = HTMLPrintf(&b.sb, fmt, args...)
107+
return b
108+
}
109+
110+
func (b *HTMLBuilder) HTMLString() template.HTML {
111+
return template.HTML(b.sb.String())
112+
}
113+
114+
func (b *HTMLBuilder) String() string {
115+
return b.sb.String()
116+
}

modules/htmlutil/html_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,10 @@ func TestHTMLFormat(t *testing.T) {
2222
assert.Equal(t, template.HTML("&lt;&gt;"), HTMLFormat("%s", template.URL("<>")))
2323
assert.Equal(t, template.HTML("&amp;StringMethod &amp;StringMethod"), HTMLFormat("%s %s", testStringer{}, &testStringer{}))
2424
}
25+
26+
func TestHTMLBuilder(t *testing.T) {
27+
b := &HTMLBuilder{}
28+
b.WriteString("<").WriteHTML("<hr>").WriteFormat("<span>%s%s</span>", ">", EscapeString(">"))
29+
assert.Equal(t, "&lt;<hr><span>&gt;&gt;</span>", b.String())
30+
assert.Equal(t, template.HTML("&lt;<hr><span>&gt;&gt;</span>"), b.HTMLString())
31+
}

modules/markup/internal/renderinternal.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ func (r *RenderInternal) ProtectSafeAttrs(content template.HTML) template.HTML {
7777
}
7878

7979
func (r *RenderInternal) FormatWithSafeAttrs(w io.Writer, fmt template.HTML, a ...any) error {
80-
_, err := w.Write([]byte(r.ProtectSafeAttrs(htmlutil.HTMLFormat(fmt, a...))))
80+
htmlStr := r.ProtectSafeAttrs(htmlutil.HTMLFormat(fmt, a...))
81+
_, err := io.WriteString(w, string(htmlStr))
8182
return err
8283
}

modules/markup/orgmode/orgmode.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,31 +106,27 @@ func (r *orgWriter) resolveLink(link string) string {
106106
// WriteRegularLink renders images, links or videos
107107
func (r *orgWriter) WriteRegularLink(l org.RegularLink) {
108108
link := r.resolveLink(l.URL)
109-
110-
printHTML := func(html template.HTML, a ...any) {
111-
_, _ = fmt.Fprint(r, htmlutil.HTMLFormat(html, a...))
112-
}
113109
// Inspired by https://github.com/niklasfasching/go-org/blob/6eb20dbda93cb88c3503f7508dc78cbbc639378f/org/html_writer.go#L406-L427
114110
switch l.Kind() {
115111
case "image":
116112
if l.Description == nil {
117-
printHTML(`<img src="%s" alt="%s">`, link, link)
113+
_, _ = htmlutil.HTMLPrintf(r, `<img src="%s" alt="%s">`, link, link)
118114
} else {
119115
imageSrc := r.resolveLink(org.String(l.Description...))
120-
printHTML(`<a href="%s"><img src="%s" alt="%s"></a>`, link, imageSrc, imageSrc)
116+
_, _ = htmlutil.HTMLPrintf(r, `<a href="%s"><img src="%s" alt="%s"></a>`, link, imageSrc, imageSrc)
121117
}
122118
case "video":
123119
if l.Description == nil {
124-
printHTML(`<video src="%s">%s</video>`, link, link)
120+
_, _ = htmlutil.HTMLPrintf(r, `<video src="%s">%s</video>`, link, link)
125121
} else {
126122
videoSrc := r.resolveLink(org.String(l.Description...))
127-
printHTML(`<a href="%s"><video src="%s">%s</video></a>`, link, videoSrc, videoSrc)
123+
_, _ = htmlutil.HTMLPrintf(r, `<a href="%s"><video src="%s">%s</video></a>`, link, videoSrc, videoSrc)
128124
}
129125
default:
130126
var description any = link
131127
if l.Description != nil {
132128
description = template.HTML(r.WriteNodesAsString(l.Description...)) // orgmode HTMLWriter outputs HTML content
133129
}
134-
printHTML(`<a href="%s">%s</a>`, link, description)
130+
_, _ = htmlutil.HTMLPrintf(r, `<a href="%s">%s</a>`, link, description)
135131
}
136132
}

modules/web/router.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ func (r *Router) normalizeRequestPath(resp http.ResponseWriter, req *http.Reques
260260
// do not respond to other requests, to simulate a real sub-path environment
261261
resp.Header().Add("Content-Type", "text/html; charset=utf-8")
262262
resp.WriteHeader(http.StatusNotFound)
263-
_, _ = resp.Write([]byte(htmlutil.HTMLFormat(`404 page not found, sub-path is: <a href="%s">%s</a>`, setting.AppSubURL, setting.AppSubURL)))
263+
_, _ = htmlutil.HTMLPrintf(resp, `404 page not found, sub-path is: <a href="%s">%s</a>`, setting.AppSubURL, setting.AppSubURL)
264264
return
265265
}
266266
normalized = true

routers/web/repo/view_home.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ func handleRepoViewSubmodule(ctx *context.Context, commitSubmoduleFile *git.Comm
276276
redirectLink := submoduleWebLink.CommitWebLink
277277
if isViewHomeOnlyContent(ctx) {
278278
ctx.Resp.Header().Set("Content-Type", "text/html; charset=utf-8")
279-
_, _ = ctx.Resp.Write([]byte(htmlutil.HTMLFormat(`<a href="%s">%s</a>`, redirectLink, redirectLink)))
279+
_, _ = htmlutil.HTMLPrintf(ctx.Resp, `<meta http-equiv="refresh" content="0;url=%s">`, redirectLink)
280280
} else if !httplib.IsCurrentGiteaSiteURL(ctx, redirectLink) {
281281
// don't auto-redirect to external URL, to avoid open redirect or phishing
282282
ctx.Data["NotFoundPrompt"] = redirectLink

0 commit comments

Comments
 (0)