diff --git a/modules/markup/html.go b/modules/markup/html.go
index 0af74d2680..8dbc958299 100644
--- a/modules/markup/html.go
+++ b/modules/markup/html.go
@@ -372,7 +372,42 @@ func postProcess(ctx *RenderContext, procs []processor, input io.Reader, output
return nil
}
-func visitNode(ctx *RenderContext, procs []processor, node *html.Node) {
+func handleNodeImg(ctx *RenderContext, img *html.Node) {
+ for i, attr := range img.Attr {
+ if attr.Key != "src" {
+ continue
+ }
+
+ if attr.Val != "" && !IsFullURLString(attr.Val) && !strings.HasPrefix(attr.Val, "/") {
+ attr.Val = util.URLJoin(ctx.Links.ResolveMediaLink(ctx.IsWiki), attr.Val)
+
+ // By default, the "" tag should also be clickable,
+ // because frontend use `
` to paste the re-scaled image into the markdown,
+ // so it must match the default markdown image behavior.
+ hasParentAnchor := false
+ for p := img.Parent; p != nil; p = p.Parent {
+ if hasParentAnchor = p.Type == html.ElementNode && p.Data == "a"; hasParentAnchor {
+ break
+ }
+ }
+ if !hasParentAnchor {
+ imgA := &html.Node{Type: html.ElementNode, Data: "a", Attr: []html.Attribute{
+ {Key: "href", Val: attr.Val},
+ {Key: "target", Val: "_blank"},
+ }}
+ parent := img.Parent
+ imgNext := img.NextSibling
+ parent.RemoveChild(img)
+ parent.InsertBefore(imgA, imgNext)
+ imgA.AppendChild(img)
+ }
+ }
+ attr.Val = camoHandleLink(attr.Val)
+ img.Attr[i] = attr
+ }
+}
+
+func visitNode(ctx *RenderContext, procs []processor, node *html.Node) *html.Node {
// Add user-content- to IDs and "#" links if they don't already have them
for idx, attr := range node.Attr {
val := strings.TrimPrefix(attr.Val, "#")
@@ -397,21 +432,14 @@ func visitNode(ctx *RenderContext, procs []processor, node *html.Node) {
textNode(ctx, procs, node)
case html.ElementNode:
if node.Data == "img" {
- for i, attr := range node.Attr {
- if attr.Key != "src" {
- continue
- }
- if len(attr.Val) > 0 && !IsFullURLString(attr.Val) && !strings.HasPrefix(attr.Val, "data:image/") {
- attr.Val = util.URLJoin(ctx.Links.ResolveMediaLink(ctx.IsWiki), attr.Val)
- }
- attr.Val = camoHandleLink(attr.Val)
- node.Attr[i] = attr
- }
+ next := node.NextSibling
+ handleNodeImg(ctx, node)
+ return next
} else if node.Data == "a" {
// Restrict text in links to emojis
procs = emojiProcessors
} else if node.Data == "code" || node.Data == "pre" {
- return
+ return node.NextSibling
} else if node.Data == "i" {
for _, attr := range node.Attr {
if attr.Key != "class" {
@@ -434,11 +462,11 @@ func visitNode(ctx *RenderContext, procs []processor, node *html.Node) {
}
}
}
- for n := node.FirstChild; n != nil; n = n.NextSibling {
- visitNode(ctx, procs, n)
+ for n := node.FirstChild; n != nil; {
+ n = visitNode(ctx, procs, n)
}
}
- // ignore everything else
+ return node.NextSibling
}
// textNode runs the passed node through various processors, in order to handle
@@ -851,7 +879,7 @@ func issueIndexPatternProcessor(ctx *RenderContext, node *html.Node) {
// FIXME: the use of "mode" is quite dirty and hacky, for example: what is a "document"? how should it be rendered?
// The "mode" approach should be refactored to some other more clear&reliable way.
- crossLinkOnly := (ctx.Metas["mode"] == "document" && !ctx.IsWiki)
+ crossLinkOnly := ctx.Metas["mode"] == "document" && !ctx.IsWiki
var (
found bool
diff --git a/modules/markup/html_internal_test.go b/modules/markup/html_internal_test.go
index 3ff0597851..9aa9c22d70 100644
--- a/modules/markup/html_internal_test.go
+++ b/modules/markup/html_internal_test.go
@@ -18,8 +18,7 @@ import (
const (
TestAppURL = "http://localhost:3000/"
- TestOrgRepo = "gogits/gogs"
- TestRepoURL = TestAppURL + TestOrgRepo + "/"
+ TestRepoURL = TestAppURL + "test-owner/test-repo/"
)
// externalIssueLink an HTML link to an alphanumeric-style issue
@@ -64,8 +63,8 @@ var regexpMetas = map[string]string{
// these values should match the TestOrgRepo const above
var localMetas = map[string]string{
- "user": "gogits",
- "repo": "gogs",
+ "user": "test-owner",
+ "repo": "test-repo",
}
func TestRender_IssueIndexPattern(t *testing.T) {
@@ -362,12 +361,12 @@ func TestRender_FullIssueURLs(t *testing.T) {
`Look here person/repo#4`)
test("http://localhost:3000/person/repo/issues/4#issuecomment-1234",
`person/repo#4 (comment)`)
- test("http://localhost:3000/gogits/gogs/issues/4",
- `#4`)
- test("http://localhost:3000/gogits/gogs/issues/4 test",
- `#4 test`)
- test("http://localhost:3000/gogits/gogs/issues/4?a=1&b=2#comment-123 test",
- `#4 (comment) test`)
+ test("http://localhost:3000/test-owner/test-repo/issues/4",
+ `#4`)
+ test("http://localhost:3000/test-owner/test-repo/issues/4 test",
+ `#4 test`)
+ test("http://localhost:3000/test-owner/test-repo/issues/4?a=1&b=2#comment-123 test",
+ `#4 (comment) test`)
test("http://localhost:3000/testOrg/testOrgRepo/pulls/2/files#issuecomment-24",
"http://localhost:3000/testOrg/testOrgRepo/pulls/2/files#issuecomment-24")
test("http://localhost:3000/testOrg/testOrgRepo/pulls/2/files",
diff --git a/modules/markup/html_test.go b/modules/markup/html_test.go
index e2d08692e4..df3c2609ef 100644
--- a/modules/markup/html_test.go
+++ b/modules/markup/html_test.go
@@ -120,8 +120,8 @@ func TestRender_CrossReferences(t *testing.T) {
}
test(
- "gogits/gogs#12345",
- `