Skip to content

Commit

Permalink
add more some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wxiaoguang committed Nov 23, 2024
1 parent ef1af4a commit 312fc2b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
21 changes: 18 additions & 3 deletions models/renderhelper/repo_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ func TestRepoFile(t *testing.T) {
})

t.Run("AbsoluteAndRelative", func(t *testing.T) {
rctx := NewRenderContextRepoFile(context.Background(), repo1, RepoFileOptions{
CurrentRefPath: "branch/main",
}).WithMarkupType(markdown.MarkupName)
rctx := NewRenderContextRepoFile(context.Background(), repo1, RepoFileOptions{CurrentRefPath: "branch/main"}).
WithMarkupType(markdown.MarkupName)
rendered, err := markup.RenderString(rctx, `
[/test](/test)
[./test](./test)
Expand Down Expand Up @@ -65,4 +64,20 @@ func TestRepoFile(t *testing.T) {
<a href="/user2/repo1/media/commit/1234/image" target="_blank" rel="nofollow noopener"><img src="/user2/repo1/media/commit/1234/image" alt="/image"/></a></p>
`, rendered)
})

t.Run("WithCurrentRefPathByTag", func(t *testing.T) {
rctx := NewRenderContextRepoFile(context.Background(), repo1, RepoFileOptions{
CurrentRefPath: "/commit/1234",
CurrentTreePath: "my-dir",
}).
WithMarkupType(markdown.MarkupName)
rendered, err := markup.RenderString(rctx, `
<img src="LINK">
<video src="LINK">
`)
assert.NoError(t, err)
assert.Equal(t, `<a href="/user2/repo1/media/commit/1234/my-dir/LINK" target="_blank" rel="nofollow noopener"><img src="/user2/repo1/media/commit/1234/my-dir/LINK"/></a>
<video src="/user2/repo1/media/commit/1234/my-dir/LINK">
</video>`, rendered)
})
}
6 changes: 2 additions & 4 deletions modules/markup/html_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
package markup

import (
"code.gitea.io/gitea/modules/util"

"golang.org/x/net/html"
)

Expand All @@ -17,7 +15,7 @@ func visitNodeImg(ctx *RenderContext, img *html.Node) (next *html.Node) {
}

if IsNonEmptyRelativePath(attr.Val) {
attr.Val = util.URLJoin(ctx.RenderHelper.ResolveLink(attr.Val, LinkTypeMedia))
attr.Val = ctx.RenderHelper.ResolveLink(attr.Val, LinkTypeMedia)

// By default, the "<img>" tag should also be clickable,
// because frontend use `<img>` to paste the re-scaled image into the markdown,
Expand Down Expand Up @@ -53,7 +51,7 @@ func visitNodeVideo(ctx *RenderContext, node *html.Node) (next *html.Node) {
continue
}
if IsNonEmptyRelativePath(attr.Val) {
attr.Val = util.URLJoin(ctx.RenderHelper.ResolveLink(attr.Val, LinkTypeMedia))
attr.Val = ctx.RenderHelper.ResolveLink(attr.Val, LinkTypeMedia)
}
attr.Val = camoHandleLink(attr.Val)
node.Attr[i] = attr
Expand Down

0 comments on commit 312fc2b

Please sign in to comment.