mirror of
https://github.com/hanzoai/forge.git
synced 2026-10-30 15:00:39 +00:00
Storage buckets, indexer names, the bleve analyzer, the lock prefix, SSH host keys, the markdown front-matter key, the container data paths, the RPM repo id and the API security scheme all read as this product now, and app.example.ini matches the code again. The upstream maintainer list and the changelog generator config went with the release history they belonged to, and the citation comments state the fact rather than link the issue. Three inert rules turn out to have been inert because they named a module path this tree does not use: gci grouped no first-party imports under prefix(), and depguard let models/migrations import models and modules/structs. They now name github.com/hanzoai/git. The config example also documented .gitea/workflows while the code has read .hanzo/workflows for some time. The authorized_keys marker follows the delegate-hook precedent: the new name is written and the old one is still recognised, because a marker we do not know is copied through as a hand-added key and leaves a revoked key authorized. The stored user_setting key keeps its value; only a migration may change that. Co-authored-by: Hanzo Dev <[email protected]>
609 lines
24 KiB
Go
609 lines
24 KiB
Go
// Copyright 2026 Hanzo AI, Inc. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package markup_test
|
|
|
|
import (
|
|
"io"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/hanzoai/git/modules/emoji"
|
|
"github.com/hanzoai/git/modules/markup"
|
|
"github.com/hanzoai/git/modules/markup/markdown"
|
|
"github.com/hanzoai/git/modules/setting"
|
|
testModule "github.com/hanzoai/git/modules/test"
|
|
"github.com/hanzoai/git/modules/util"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
var (
|
|
testRepoOwnerName = "user13"
|
|
testRepoName = "repo11"
|
|
localMetas = map[string]string{"user": testRepoOwnerName, "repo": testRepoName}
|
|
)
|
|
|
|
func testRenderString(ctx *markup.RenderContext, content string) (string, error) {
|
|
var buf strings.Builder
|
|
err := markup.Render(ctx, strings.NewReader(content), &buf)
|
|
return buf.String(), err
|
|
}
|
|
|
|
func TestRender_Commits(t *testing.T) {
|
|
test := func(input, expected string) {
|
|
rctx := markup.NewTestRenderContext(markup.TestAppURL, localMetas).WithRelativePath("a.md")
|
|
buffer, err := testRenderString(rctx, input)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
|
|
}
|
|
|
|
sha := "65f1bf27bc3bf70f64657658635e66094edbcb4d"
|
|
repo := markup.TestAppURL + testRepoOwnerName + "/" + testRepoName + "/"
|
|
commit := repo + "commit/" + sha
|
|
commitPath := "/user13/repo11/commit/" + sha
|
|
tree := repo + "tree/" + sha + "/src"
|
|
|
|
file := repo + "commit/" + sha + "/example.txt"
|
|
fileWithExtra := file + ":"
|
|
fileWithHash := file + "#L2"
|
|
fileWithHasExtra := file + "#L2:"
|
|
commitCompare := repo + "compare/" + sha + "..." + sha
|
|
commitCompareWithHash := commitCompare + "#L2"
|
|
|
|
test(sha, `<p><a href="`+commitPath+`" rel="nofollow"><code>65f1bf27bc</code></a></p>`)
|
|
test(sha[:7], `<p><a href="`+commitPath[:len(commitPath)-(40-7)]+`" rel="nofollow"><code>65f1bf2</code></a></p>`)
|
|
test(sha[:39], `<p><a href="`+commitPath[:len(commitPath)-(40-39)]+`" rel="nofollow"><code>65f1bf27bc</code></a></p>`)
|
|
test(commit, `<p><a href="`+commit+`" rel="nofollow"><code>65f1bf27bc</code></a></p>`)
|
|
test(tree, `<p><a href="`+tree+`" rel="nofollow"><code>65f1bf27bc/src</code></a></p>`)
|
|
|
|
test(file, `<p><a href="`+file+`" rel="nofollow"><code>65f1bf27bc/example.txt</code></a></p>`)
|
|
test(fileWithExtra, `<p><a href="`+file+`" rel="nofollow"><code>65f1bf27bc/example.txt</code></a>:</p>`)
|
|
test(fileWithHash, `<p><a href="`+fileWithHash+`" rel="nofollow"><code>65f1bf27bc/example.txt (L2)</code></a></p>`)
|
|
test(fileWithHasExtra, `<p><a href="`+fileWithHash+`" rel="nofollow"><code>65f1bf27bc/example.txt (L2)</code></a>:</p>`)
|
|
test(commitCompare, `<p><a href="`+commitCompare+`" rel="nofollow"><code>65f1bf27bc...65f1bf27bc</code></a></p>`)
|
|
test(commitCompareWithHash, `<p><a href="`+commitCompareWithHash+`" rel="nofollow"><code>65f1bf27bc...65f1bf27bc (L2)</code></a></p>`)
|
|
|
|
test("commit "+sha, `<p>commit <a href="`+commitPath+`" rel="nofollow"><code>65f1bf27bc</code></a></p>`)
|
|
test("/home/forge/"+sha, "<p>/home/forge/"+sha+"</p>")
|
|
test("deadbeef", `<p>deadbeef</p>`)
|
|
test("d27ace93", `<p>d27ace93</p>`)
|
|
test(sha[:14]+".x", `<p>`+sha[:14]+`.x</p>`)
|
|
|
|
expected14 := `<a href="` + commitPath[:len(commitPath)-(40-14)] + `" rel="nofollow"><code>` + sha[:10] + `</code></a>`
|
|
test(sha[:14]+".", `<p>`+expected14+`.</p>`)
|
|
test(sha[:14]+",", `<p>`+expected14+`,</p>`)
|
|
test("["+sha[:14]+"]", `<p>[`+expected14+`]</p>`)
|
|
}
|
|
|
|
func TestRender_CrossReferences(t *testing.T) {
|
|
defer testModule.MockVariableValue(&setting.AppURL, markup.TestAppURL)()
|
|
defer testModule.MockVariableValue(&markup.RenderBehaviorForTesting.DisableAdditionalAttributes, true)()
|
|
test := func(input, expected string) {
|
|
rctx := markup.NewTestRenderContext(markup.TestAppURL, localMetas).WithRelativePath("a.md")
|
|
buffer, err := testRenderString(rctx, input)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
|
|
}
|
|
|
|
test(
|
|
"test-owner/test-repo#12345",
|
|
`<p><a href="/test-owner/test-repo/issues/12345" class="ref-issue" rel="nofollow">test-owner/test-repo#12345</a></p>`)
|
|
test(
|
|
"hanzoai/forge#12345",
|
|
`<p><a href="/hanzoai/forge/issues/12345" class="ref-issue" rel="nofollow">hanzoai/forge#12345</a></p>`)
|
|
test(
|
|
"/home/forge/hanzoai/forge#12345",
|
|
`<p>/home/forge/hanzoai/forge#12345</p>`)
|
|
test(
|
|
markup.TestAppURL+"hanzoai/forge/issues/12345",
|
|
`<p><a href="`+markup.TestAppURL+`hanzoai/forge/issues/12345" class="ref-issue" rel="nofollow">hanzoai/forge#12345</a></p>`)
|
|
test(
|
|
markup.TestAppURL+"hanzo-ai/forge/issues/12345",
|
|
`<p><a href="`+markup.TestAppURL+`hanzo-ai/forge/issues/12345" class="ref-issue" rel="nofollow">hanzo-ai/forge#12345</a></p>`)
|
|
test(
|
|
markup.TestAppURL+"hanzoai/some-repo-name/issues/12345",
|
|
`<p><a href="`+markup.TestAppURL+`hanzoai/some-repo-name/issues/12345" class="ref-issue" rel="nofollow">hanzoai/some-repo-name#12345</a></p>`)
|
|
|
|
inputURL := setting.AppURL + "a/b/commit/0123456789012345678901234567890123456789/foo.txt?a=b#L2-L3"
|
|
test(
|
|
inputURL,
|
|
`<p><a href="`+inputURL+`" rel="nofollow"><code>0123456789/foo.txt (L2-L3)</code></a></p>`)
|
|
|
|
inputURL = setting.AppURL + "repo/owner/archive/0123456789012345678901234567890123456789.tar.gz"
|
|
test(
|
|
inputURL,
|
|
`<p><a href="`+inputURL+`" rel="nofollow"><code>0123456789.tar.gz</code></a></p>`)
|
|
|
|
inputURL = setting.AppURL + "owner/repo/commit/0123456789012345678901234567890123456789.patch?key=val"
|
|
test(
|
|
inputURL,
|
|
`<p><a href="`+inputURL+`" rel="nofollow"><code>0123456789.patch</code></a></p>`)
|
|
}
|
|
|
|
func TestRender_links(t *testing.T) {
|
|
setting.AppURL = markup.TestAppURL
|
|
defer testModule.MockVariableValue(&markup.RenderBehaviorForTesting.DisableAdditionalAttributes, true)()
|
|
test := func(input, expected string) {
|
|
buffer, err := testRenderString(markup.NewTestRenderContext().WithRelativePath("a.md"), input)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
|
|
}
|
|
|
|
oldCustomURLSchemes := setting.Markdown.CustomURLSchemes
|
|
markup.ResetDefaultSanitizerForTesting()
|
|
defer func() {
|
|
setting.Markdown.CustomURLSchemes = oldCustomURLSchemes
|
|
markup.ResetDefaultSanitizerForTesting()
|
|
markup.CustomLinkURLSchemes(oldCustomURLSchemes)
|
|
}()
|
|
setting.Markdown.CustomURLSchemes = []string{"ftp", "magnet"}
|
|
markup.CustomLinkURLSchemes(setting.Markdown.CustomURLSchemes)
|
|
|
|
// Text that should be turned into URL
|
|
test(
|
|
"https://www.example.com",
|
|
`<p><a href="https://www.example.com" rel="nofollow">https://www.example.com</a></p>`)
|
|
test(
|
|
"http://www.example.com",
|
|
`<p><a href="http://www.example.com" rel="nofollow">http://www.example.com</a></p>`)
|
|
test(
|
|
"https://example.com",
|
|
`<p><a href="https://example.com" rel="nofollow">https://example.com</a></p>`)
|
|
test(
|
|
"http://example.com",
|
|
`<p><a href="http://example.com" rel="nofollow">http://example.com</a></p>`)
|
|
test(
|
|
"http://foo.com/blah_blah",
|
|
`<p><a href="http://foo.com/blah_blah" rel="nofollow">http://foo.com/blah_blah</a></p>`)
|
|
test(
|
|
"http://foo.com/blah_blah/",
|
|
`<p><a href="http://foo.com/blah_blah/" rel="nofollow">http://foo.com/blah_blah/</a></p>`)
|
|
test(
|
|
"http://www.example.com/wpstyle/?p=364",
|
|
`<p><a href="http://www.example.com/wpstyle/?p=364" rel="nofollow">http://www.example.com/wpstyle/?p=364</a></p>`)
|
|
test(
|
|
"https://www.example.com/foo/?bar=baz&inga=42&quux",
|
|
`<p><a href="https://www.example.com/foo/?bar=baz&inga=42&quux" rel="nofollow">https://www.example.com/foo/?bar=baz&inga=42&quux</a></p>`)
|
|
test(
|
|
"http://142.42.1.1/",
|
|
`<p><a href="http://142.42.1.1/" rel="nofollow">http://142.42.1.1/</a></p>`)
|
|
test(
|
|
"https://github.com/hanzoai/forge/?p=aaa/bbb.html#ccc-ddd",
|
|
`<p><a href="https://github.com/hanzoai/forge/?p=aaa/bbb.html#ccc-ddd" rel="nofollow">https://github.com/hanzoai/forge/?p=aaa/bbb.html#ccc-ddd</a></p>`)
|
|
test(
|
|
"https://en.wikipedia.org/wiki/URL_(disambiguation)",
|
|
`<p><a href="https://en.wikipedia.org/wiki/URL_(disambiguation)" rel="nofollow">https://en.wikipedia.org/wiki/URL_(disambiguation)</a></p>`)
|
|
test(
|
|
"https://foo_bar.example.com/",
|
|
`<p><a href="https://foo_bar.example.com/" rel="nofollow">https://foo_bar.example.com/</a></p>`)
|
|
test(
|
|
"https://stackoverflow.com/questions/2896191/what-is-go-used-fore",
|
|
`<p><a href="https://stackoverflow.com/questions/2896191/what-is-go-used-fore" rel="nofollow">https://stackoverflow.com/questions/2896191/what-is-go-used-fore</a></p>`)
|
|
test(
|
|
"https://username:[email protected]",
|
|
`<p><a href="https://username:[email protected]" rel="nofollow">https://username:[email protected]</a></p>`)
|
|
test(
|
|
"ftp://forge.example/file.txt",
|
|
`<p><a href="ftp://forge.example/file.txt" rel="nofollow">ftp://forge.example/file.txt</a></p>`)
|
|
test(
|
|
"magnet:?xt=urn:btih:5dee65101db281ac9c46344cd6b175cdcadabcde&dn=download",
|
|
`<p><a href="magnet:?xt=urn:btih:5dee65101db281ac9c46344cd6b175cdcadabcde&dn=download" rel="nofollow">magnet:?xt=urn:btih:5dee65101db281ac9c46344cd6b175cdcadabcde&dn=download</a></p>`)
|
|
test(
|
|
`[link](https://example.com)`,
|
|
`<p><a href="https://example.com" rel="nofollow">link</a></p>`)
|
|
test(
|
|
`[link](mailto:[email protected])`,
|
|
`<p><a href="mailto:[email protected]" rel="nofollow">link</a></p>`)
|
|
test(
|
|
`[link](javascript:xss)`,
|
|
`<p>link</p>`)
|
|
|
|
// Test that should *not* be turned into URL
|
|
test(
|
|
"www.example.com",
|
|
`<p>www.example.com</p>`)
|
|
test(
|
|
"example.com",
|
|
`<p>example.com</p>`)
|
|
test(
|
|
"test.example.com",
|
|
`<p>test.example.com</p>`)
|
|
test(
|
|
"http://",
|
|
`<p>http://</p>`)
|
|
test(
|
|
"https://",
|
|
`<p>https://</p>`)
|
|
test(
|
|
"://",
|
|
`<p>://</p>`)
|
|
test(
|
|
"www",
|
|
`<p>www</p>`)
|
|
test(
|
|
"ftps://forge.example",
|
|
`<p>ftps://forge.example</p>`)
|
|
|
|
t.Run("LinkEllipsis", func(t *testing.T) {
|
|
input := util.EllipsisDisplayString("http://10.1.2.3", 12)
|
|
assert.Equal(t, "http://10…", input)
|
|
test(input, "<p>http://10…</p>")
|
|
|
|
input = util.EllipsisDisplayString("http://10.1.2.3", 13)
|
|
assert.Equal(t, "http://10.…", input)
|
|
test(input, "<p>http://10.…</p>")
|
|
})
|
|
}
|
|
|
|
func TestRender_email(t *testing.T) {
|
|
setting.AppURL = markup.TestAppURL
|
|
defer testModule.MockVariableValue(&markup.RenderBehaviorForTesting.DisableAdditionalAttributes, true)()
|
|
test := func(input, expected string) {
|
|
res, err := testRenderString(markup.NewTestRenderContext().WithRelativePath("a.md"), input)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(res), "input: %s", input)
|
|
}
|
|
|
|
// Text that should be turned into email link
|
|
test(
|
|
"[email protected]",
|
|
`<p><a href="mailto:[email protected]" rel="nofollow">[email protected]</a></p>`)
|
|
test(
|
|
"([email protected])",
|
|
`<p>(<a href="mailto:[email protected]" rel="nofollow">[email protected]</a>)</p>`)
|
|
test(
|
|
"[[email protected]]",
|
|
`<p>[<a href="mailto:[email protected]" rel="nofollow">[email protected]</a>]</p>`)
|
|
test(
|
|
"[email protected].",
|
|
`<p><a href="mailto:[email protected]" rel="nofollow">[email protected]</a>.</p>`)
|
|
test(
|
|
"[email protected]",
|
|
`<p><a href="mailto:[email protected]" rel="nofollow">[email protected]</a></p>`)
|
|
test(
|
|
"send email to [email protected].",
|
|
`<p>send email to <a href="mailto:[email protected]" rel="nofollow">[email protected]</a>.</p>`)
|
|
|
|
test(
|
|
`[email protected],
|
|
[email protected].
|
|
[email protected];
|
|
[email protected]?
|
|
[email protected]!`,
|
|
`<p><a href="mailto:[email protected]" rel="nofollow">[email protected]</a>,
|
|
<a href="mailto:[email protected]" rel="nofollow">[email protected]</a>.
|
|
<a href="mailto:[email protected]" rel="nofollow">[email protected]</a>;
|
|
<a href="mailto:[email protected]" rel="nofollow">[email protected]</a>?
|
|
<a href="mailto:[email protected]" rel="nofollow">[email protected]</a>!</p>`)
|
|
|
|
// match GitHub behavior
|
|
test("email@[email protected]", `<p>email@<a href="mailto:[email protected]" rel="nofollow">[email protected]</a></p>`)
|
|
|
|
// match GitHub behavior
|
|
test(`"[email protected]"`, `<p>"<a href="mailto:[email protected]" rel="nofollow">[email protected]</a>"</p>`)
|
|
|
|
// Test that should *not* be turned into email links
|
|
test(
|
|
"/home/forge/mailstore/info@forge/com",
|
|
`<p>/home/forge/mailstore/info@forge/com</p>`)
|
|
test(
|
|
"[email protected]:hanzoai/forge.git",
|
|
`<p>[email protected]:hanzoai/forge.git</p>`)
|
|
test(
|
|
"https://foo:[email protected]",
|
|
`<p><a href="https://foo:[email protected]" rel="nofollow">https://foo:[email protected]</a></p>`)
|
|
test(
|
|
"forge@3",
|
|
`<p>forge@3</p>`)
|
|
test(
|
|
"[email protected]",
|
|
`<p>[email protected]</p>`)
|
|
test(
|
|
"[email protected]",
|
|
`<p>[email protected]</p>`)
|
|
|
|
cases := []struct {
|
|
input, expected string
|
|
}{
|
|
// match GitHub behavior
|
|
{"[email protected]", `<p>?<a href="mailto:[email protected]" rel="nofollow">[email protected]</a></p>`},
|
|
{"*[email protected]", `<p>*<a href="mailto:[email protected]" rel="nofollow">[email protected]</a></p>`},
|
|
{"[email protected]", `<p>~<a href="mailto:[email protected]" rel="nofollow">[email protected]</a></p>`},
|
|
|
|
// the following cases don't match GitHub behavior, but they are valid email addresses ...
|
|
// maybe we should reduce the candidate characters for the "name" part in the future
|
|
{"a*[email protected]", `<p><a href="mailto:a*[email protected]" rel="nofollow">a*[email protected]</a></p>`},
|
|
{"[email protected]", `<p><a href="mailto:[email protected]" rel="nofollow">[email protected]</a></p>`},
|
|
}
|
|
for _, c := range cases {
|
|
test(c.input, c.expected)
|
|
}
|
|
}
|
|
|
|
func TestRender_emoji(t *testing.T) {
|
|
setting.AppURL = markup.TestAppURL
|
|
setting.StaticURLPrefix = strings.TrimSuffix(markup.TestAppURL, "/")
|
|
|
|
test := func(input, expected string) {
|
|
expected = strings.ReplaceAll(expected, "&", "&")
|
|
buffer, err := testRenderString(markup.NewTestRenderContext().WithRelativePath("a.md"), input)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
|
|
}
|
|
|
|
// Make sure we can successfully match every emoji in our dataset with regex
|
|
for i := range emoji.GemojiData {
|
|
test(
|
|
emoji.GemojiData[i].Emoji,
|
|
`<p><span class="emoji" aria-label="`+emoji.GemojiData[i].Description+`">`+emoji.GemojiData[i].Emoji+`</span></p>`)
|
|
}
|
|
for i := range emoji.GemojiData {
|
|
test(
|
|
":"+emoji.GemojiData[i].Aliases[0]+":",
|
|
`<p><span class="emoji" aria-label="`+emoji.GemojiData[i].Description+`">`+emoji.GemojiData[i].Emoji+`</span></p>`)
|
|
}
|
|
|
|
// Text that should be turned into or recognized as emoji
|
|
// :git:, because the shipped asset is git.png and CustomEmojis is
|
|
// {git, gitlab, github}. An alias with no asset renders as literal text.
|
|
test(
|
|
":git:",
|
|
`<p><span class="emoji" aria-label="git"><img alt=":git:" src="`+setting.StaticURLPrefix+`/assets/img/emoji/git.png"/></span></p>`)
|
|
test(
|
|
":custom-emoji:",
|
|
`<p>:custom-emoji:</p>`)
|
|
setting.UI.CustomEmojisMap["custom-emoji"] = ":custom-emoji:"
|
|
test(
|
|
":custom-emoji:",
|
|
`<p><span class="emoji" aria-label="custom-emoji"><img alt=":custom-emoji:" src="`+setting.StaticURLPrefix+`/assets/img/emoji/custom-emoji.png"/></span></p>`)
|
|
test(
|
|
"这是字符:1::+1: some🐊 \U0001f44d:custom-emoji: :git:",
|
|
`<p>这是字符:1:<span class="emoji" aria-label="thumbs up">👍</span> some<span class="emoji" aria-label="crocodile">🐊</span> `+
|
|
`<span class="emoji" aria-label="thumbs up">👍</span><span class="emoji" aria-label="custom-emoji"><img alt=":custom-emoji:" src="`+setting.StaticURLPrefix+`/assets/img/emoji/custom-emoji.png"/></span> `+
|
|
`<span class="emoji" aria-label="git"><img alt=":git:" src="`+setting.StaticURLPrefix+`/assets/img/emoji/git.png"/></span></p>`)
|
|
test(
|
|
"Some text with 😄 in the middle",
|
|
`<p>Some text with <span class="emoji" aria-label="grinning face with smiling eyes">😄</span> in the middle</p>`)
|
|
test(
|
|
"Some text with :smile: in the middle",
|
|
`<p>Some text with <span class="emoji" aria-label="grinning face with smiling eyes">😄</span> in the middle</p>`)
|
|
test(
|
|
"Some text with 😄😄 2 emoji next to each other",
|
|
`<p>Some text with <span class="emoji" aria-label="grinning face with smiling eyes">😄</span><span class="emoji" aria-label="grinning face with smiling eyes">😄</span> 2 emoji next to each other</p>`)
|
|
test(
|
|
"😎🤪🔐🤑❓",
|
|
`<p><span class="emoji" aria-label="smiling face with sunglasses">😎</span><span class="emoji" aria-label="zany face">🤪</span><span class="emoji" aria-label="locked with key">🔐</span><span class="emoji" aria-label="money-mouth face">🤑</span><span class="emoji" aria-label="red question mark">❓</span></p>`)
|
|
|
|
// should match nothing
|
|
test(":100:200", `<p>:100:200</p>`)
|
|
test("std::thread::something", `<p>std::thread::something</p>`)
|
|
test(":not exist:", `<p>:not exist:</p>`)
|
|
test("foo `:smile:", "<p>foo `:smile:</p>")
|
|
test("foo `:smile:`", `<p>foo <code>:smile:</code></p>`)
|
|
test("foo ` :smile:", "<p>foo ` <span class=\"emoji\" aria-label=\"grinning face with smiling eyes\">😄</span></p>")
|
|
}
|
|
|
|
func TestRender_ShortLinks(t *testing.T) {
|
|
setting.AppURL = markup.TestAppURL
|
|
tree := markup.TestRepoURL + "src/master"
|
|
|
|
test := func(input, expected string) {
|
|
buffer, err := markdown.RenderString(markup.NewTestRenderContext(tree), input)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
|
|
}
|
|
|
|
url := tree + "/Link"
|
|
otherURL := tree + "/Other-Link"
|
|
encodedURL := tree + "/Link%3F"
|
|
imgurl := tree + "/Link.jpg"
|
|
otherImgurl := tree + "/Link+Other.jpg"
|
|
encodedImgurl := tree + "/Link+%23.jpg"
|
|
notencodedImgurl := tree + "/some/path/Link%20#.jpg"
|
|
renderableFileURL := tree + "/markdown_file.md"
|
|
unrenderableFileURL := tree + "/file.zip"
|
|
favicon := "http://google.com/favicon.ico"
|
|
|
|
test(
|
|
"[[Link]]",
|
|
`<p><a href="`+url+`" rel="nofollow">Link</a></p>`,
|
|
)
|
|
test(
|
|
"[[Link.-]]",
|
|
`<p><a href="http://localhost:3000/test-owner/test-repo/src/master/Link.-" rel="nofollow">Link.-</a></p>`,
|
|
)
|
|
test(
|
|
"[[Link.jpg]]",
|
|
`<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="Link.jpg" alt="Link.jpg"/></a></p>`,
|
|
)
|
|
test(
|
|
"[["+favicon+"]]",
|
|
`<p><a href="`+favicon+`" rel="nofollow"><img src="`+favicon+`" title="favicon.ico" alt="`+favicon+`"/></a></p>`,
|
|
)
|
|
test(
|
|
"[[Name|Link]]",
|
|
`<p><a href="`+url+`" rel="nofollow">Name</a></p>`,
|
|
)
|
|
test(
|
|
"[[Name|Link.jpg]]",
|
|
`<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="Name" alt="Name"/></a></p>`,
|
|
)
|
|
test(
|
|
"[[Name|Link.jpg|alt=AltName]]",
|
|
`<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="AltName" alt="AltName"/></a></p>`,
|
|
)
|
|
test(
|
|
"[[Name|Link.jpg|title=Title]]",
|
|
`<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="Title" alt="Title"/></a></p>`,
|
|
)
|
|
test(
|
|
"[[Name|Link.jpg|alt=AltName|title=Title]]",
|
|
`<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="Title" alt="AltName"/></a></p>`,
|
|
)
|
|
test(
|
|
"[[Name|Link.jpg|alt=\"AltName\"|title='Title']]",
|
|
`<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="Title" alt="AltName"/></a></p>`,
|
|
)
|
|
test(
|
|
"[[Name|Link Other.jpg|alt=\"AltName\"|title='Title']]",
|
|
`<p><a href="`+otherImgurl+`" rel="nofollow"><img src="`+otherImgurl+`" title="Title" alt="AltName"/></a></p>`,
|
|
)
|
|
test(
|
|
"[[Link]] [[Other Link]]",
|
|
`<p><a href="`+url+`" rel="nofollow">Link</a> <a href="`+otherURL+`" rel="nofollow">Other Link</a></p>`,
|
|
)
|
|
test(
|
|
"[[Link?]]",
|
|
`<p><a href="`+encodedURL+`" rel="nofollow">Link?</a></p>`,
|
|
)
|
|
test(
|
|
"[[Link]] [[Other Link]] [[Link?]]",
|
|
`<p><a href="`+url+`" rel="nofollow">Link</a> <a href="`+otherURL+`" rel="nofollow">Other Link</a> <a href="`+encodedURL+`" rel="nofollow">Link?</a></p>`,
|
|
)
|
|
test(
|
|
"[[markdown_file.md]]",
|
|
`<p><a href="`+renderableFileURL+`" rel="nofollow">markdown_file.md</a></p>`,
|
|
)
|
|
test(
|
|
"[[file.zip]]",
|
|
`<p><a href="`+unrenderableFileURL+`" rel="nofollow">file.zip</a></p>`,
|
|
)
|
|
test(
|
|
"[[Link #.jpg]]",
|
|
`<p><a href="`+encodedImgurl+`" rel="nofollow"><img src="`+encodedImgurl+`" title="Link #.jpg" alt="Link #.jpg"/></a></p>`,
|
|
)
|
|
test(
|
|
"[[Name|Link #.jpg|alt=\"AltName\"|title='Title']]",
|
|
`<p><a href="`+encodedImgurl+`" rel="nofollow"><img src="`+encodedImgurl+`" title="Title" alt="AltName"/></a></p>`,
|
|
)
|
|
// FIXME: it's unable to resolve: [[link?k=v]]
|
|
// FIXME: it is a wrong test case, it is not an image, but a link with anchor "#.jpg"
|
|
test(
|
|
"[[some/path/Link #.jpg]]",
|
|
`<p><a href="`+notencodedImgurl+`" rel="nofollow"><img src="`+notencodedImgurl+`" title="Link #.jpg" alt="some/path/Link #.jpg"/></a></p>`,
|
|
)
|
|
test(
|
|
"<p><a href=\"https://example.org\">[[foobar]]</a></p>",
|
|
`<p><a href="https://example.org" rel="nofollow">[[foobar]]</a></p>`,
|
|
)
|
|
}
|
|
|
|
func Test_ParseClusterFuzz(t *testing.T) {
|
|
setting.AppURL = markup.TestAppURL
|
|
|
|
localMetas := map[string]string{"user": "hanzoai", "repo": "forge"}
|
|
|
|
data := "<A><maTH><tr><MN><bodY ÿ><temPlate></template><tH><tr></A><tH><d<bodY "
|
|
|
|
var res strings.Builder
|
|
err := markup.PostProcessDefault(markup.NewTestRenderContext(localMetas), strings.NewReader(data), &res)
|
|
assert.NoError(t, err)
|
|
assert.NotContains(t, res.String(), "<html")
|
|
|
|
data = "<!DOCTYPE html>\n<A><maTH><tr><MN><bodY ÿ><temPlate></template><tH><tr></A><tH><d<bodY "
|
|
|
|
res.Reset()
|
|
err = markup.PostProcessDefault(markup.NewTestRenderContext(localMetas), strings.NewReader(data), &res)
|
|
|
|
assert.NoError(t, err)
|
|
assert.NotContains(t, res.String(), "<html")
|
|
}
|
|
|
|
func TestPostProcess(t *testing.T) {
|
|
setting.StaticURLPrefix = strings.TrimSuffix(markup.TestAppURL, "/") // can't run standalone
|
|
defer testModule.MockVariableValue(&markup.RenderBehaviorForTesting.DisableAdditionalAttributes, true)()
|
|
|
|
test := func(input, expected string) {
|
|
var res strings.Builder
|
|
err := markup.PostProcessDefault(markup.NewTestRenderContext(markup.TestAppURL, map[string]string{"user": "hanzoai", "repo": "forge"}), strings.NewReader(input), &res)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(res.String()))
|
|
}
|
|
|
|
// Issue index shouldn't be post-processing in a document.
|
|
test(
|
|
"#1",
|
|
"#1")
|
|
|
|
// But cross-referenced issue index should work.
|
|
test(
|
|
"hanzoai/forge#12345",
|
|
`<a href="/hanzoai/forge/issues/12345" class="ref-issue">hanzoai/forge#12345</a>`)
|
|
|
|
// Test that other post-processing still works.
|
|
test(
|
|
":git:",
|
|
`<span class="emoji" aria-label="git"><img alt=":git:" src="`+setting.StaticURLPrefix+`/assets/img/emoji/git.png"/></span>`)
|
|
test(
|
|
"Some text with 😄 in the middle",
|
|
`Some text with <span class="emoji" aria-label="grinning face with smiling eyes">😄</span> in the middle`)
|
|
test("http://localhost:3000/person/repo/issues/4#issuecomment-1234",
|
|
`<a href="http://localhost:3000/person/repo/issues/4#issuecomment-1234" class="ref-issue">person/repo#4 (comment)</a>`)
|
|
|
|
// special tags, GitHub's behavior, and for unclosed tags, output as text content as much as possible
|
|
test("<script>a", `<script>a`)
|
|
test("<script>a</script>", `<script>a</script>`)
|
|
test("<STYLE>a", `<STYLE>a`)
|
|
test("<style>a</STYLE>", `<style>a</STYLE>`)
|
|
|
|
// other special tags, our special behavior
|
|
test("<?php\nfoo", "<?php\nfoo")
|
|
test("<%asp\nfoo", "<%asp\nfoo")
|
|
}
|
|
|
|
func TestIssue16020(t *testing.T) {
|
|
setting.AppURL = markup.TestAppURL
|
|
|
|
localMetas := map[string]string{
|
|
"user": "hanzoai",
|
|
"repo": "forge",
|
|
}
|
|
|
|
data := `<img src="data:image/png;base64,i//V"/>`
|
|
|
|
var res strings.Builder
|
|
err := markup.PostProcessDefault(markup.NewTestRenderContext(localMetas), strings.NewReader(data), &res)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, data, res.String())
|
|
}
|
|
|
|
func BenchmarkEmojiPostprocess(b *testing.B) {
|
|
data := "🥰 "
|
|
for len(data) < 1<<16 {
|
|
data += data
|
|
}
|
|
b.ResetTimer()
|
|
for b.Loop() {
|
|
var res strings.Builder
|
|
err := markup.PostProcessDefault(markup.NewTestRenderContext(localMetas), strings.NewReader(data), &res)
|
|
assert.NoError(b, err)
|
|
}
|
|
}
|
|
|
|
func TestFuzz(t *testing.T) {
|
|
s := "t/l/issues/8#/../../a"
|
|
renderContext := markup.NewTestRenderContext()
|
|
err := markup.PostProcessDefault(renderContext, strings.NewReader(s), io.Discard)
|
|
assert.NoError(t, err)
|
|
}
|
|
|
|
func TestIssue18471(t *testing.T) {
|
|
defer testModule.MockVariableValue(&setting.AppURL, markup.TestAppURL)()
|
|
|
|
data := markup.TestAppURL + `org/repo/compare/783b039...da951ce`
|
|
|
|
var res strings.Builder
|
|
err := markup.PostProcessDefault(markup.NewTestRenderContext(localMetas), strings.NewReader(data), &res)
|
|
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, `<a href="`+markup.TestAppURL+`org/repo/compare/783b039...da951ce" class="compare"><code>783b039...da951ce</code></a>`, res.String())
|
|
}
|
|
|
|
func TestIsFullURL(t *testing.T) {
|
|
assert.True(t, markup.IsFullURLString("https://example.com"))
|
|
assert.True(t, markup.IsFullURLString("mailto:[email protected]"))
|
|
assert.True(t, markup.IsFullURLString("data:image/11111"))
|
|
assert.False(t, markup.IsFullURLString("/foo:bar"))
|
|
}
|