Skip to content

Commit 23ba5c8

Browse files
sapklunny
authored andcommitted
markup: escape short wiki link (#4091)
1 parent c919b07 commit 23ba5c8

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

modules/markup/html.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,9 @@ func shortLinkProcessorFull(ctx *postProcessCtx, node *html.Node, noLink bool) {
469469
} else {
470470
link = strings.Replace(link, " ", "-", -1)
471471
}
472+
if !strings.Contains(link, "/") {
473+
link = url.PathEscape(link)
474+
}
472475
}
473476
urlPrefix := ctx.urlPrefix
474477
if image {

modules/markup/html_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,18 @@ func TestRender_ShortLinks(t *testing.T) {
8282
rawtree := util.URLJoin(AppSubURL, "raw", "master")
8383
url := util.URLJoin(tree, "Link")
8484
otherURL := util.URLJoin(tree, "Other-Link")
85+
encodedURL := util.URLJoin(tree, "Link%3F")
8586
imgurl := util.URLJoin(rawtree, "Link.jpg")
8687
otherImgurl := util.URLJoin(rawtree, "Link+Other.jpg")
88+
encodedImgurl := util.URLJoin(rawtree, "Link+%23.jpg")
89+
notencodedImgurl := util.URLJoin(rawtree, "some", "path", "Link+#.jpg")
8790
urlUncyclo := util.URLJoin(AppSubURL, "wiki", "Link")
8891
otherURLUncyclo := util.URLJoin(AppSubURL, "wiki", "Other-Link")
92+
encodedURLUncyclo := util.URLJoin(AppSubURL, "wiki", "Link%3F")
8993
imgurlUncyclo := util.URLJoin(AppSubURL, "wiki", "raw", "Link.jpg")
9094
otherImgurlUncyclo := util.URLJoin(AppSubURL, "wiki", "raw", "Link+Other.jpg")
95+
encodedImgurlUncyclo := util.URLJoin(AppSubURL, "wiki", "raw", "Link+%23.jpg")
96+
notencodedImgurlUncyclo := util.URLJoin(AppSubURL, "wiki", "raw", "some", "path", "Link+#.jpg")
9197
favicon := "http://google.com/favicon.ico"
9298

9399
test(
@@ -134,4 +140,24 @@ func TestRender_ShortLinks(t *testing.T) {
134140
"[[Link]] [[Other Link]]",
135141
`<p><a href="`+url+`" rel="nofollow">Link</a> <a href="`+otherURL+`" rel="nofollow">Other Link</a></p>`,
136142
`<p><a href="`+urlUncyclo+`" rel="nofollow">Link</a> <a href="`+otherURLUncyclo+`" rel="nofollow">Other Link</a></p>`)
143+
test(
144+
"[[Link?]]",
145+
`<p><a href="`+encodedURL+`" rel="nofollow">Link?</a></p>`,
146+
`<p><a href="`+encodedURLUncyclo+`" rel="nofollow">Link?</a></p>`)
147+
test(
148+
"[[Link]] [[Other Link]] [[Link?]]",
149+
`<p><a href="`+url+`" rel="nofollow">Link</a> <a href="`+otherURL+`" rel="nofollow">Other Link</a> <a href="`+encodedURL+`" rel="nofollow">Link?</a></p>`,
150+
`<p><a href="`+urlUncyclo+`" rel="nofollow">Link</a> <a href="`+otherURLUncyclo+`" rel="nofollow">Other Link</a> <a href="`+encodedURLUncyclo+`" rel="nofollow">Link?</a></p>`)
151+
test(
152+
"[[Link #.jpg]]",
153+
`<p><a href="`+encodedImgurl+`" rel="nofollow"><img src="`+encodedImgurl+`"/></a></p>`,
154+
`<p><a href="`+encodedImgurlUncyclo+`" rel="nofollow"><img src="`+encodedImgurlUncyclo+`"/></a></p>`)
155+
test(
156+
"[[Name|Link #.jpg|alt=\"AltName\"|title='Title']]",
157+
`<p><a href="`+encodedImgurl+`" rel="nofollow"><img src="`+encodedImgurl+`" title="Title" alt="AltName"/></a></p>`,
158+
`<p><a href="`+encodedImgurlUncyclo+`" rel="nofollow"><img src="`+encodedImgurlUncyclo+`" title="Title" alt="AltName"/></a></p>`)
159+
test(
160+
"[[some/path/Link #.jpg]]",
161+
`<p><a href="`+notencodedImgurl+`" rel="nofollow"><img src="`+notencodedImgurl+`"/></a></p>`,
162+
`<p><a href="`+notencodedImgurlUncyclo+`" rel="nofollow"><img src="`+notencodedImgurlUncyclo+`"/></a></p>`)
137163
}

0 commit comments

Comments
 (0)