aboutsummaryrefslogtreecommitdiffstats
path: root/modules/markup/html_test.go
diff options
context:
space:
mode:
authorwxiaoguang <wxiaoguang@gmail.com>2024-11-14 13:02:11 +0800
committerGitHub <noreply@github.com>2024-11-14 05:02:11 +0000
commit3f9c3e7bc394c115ccc4818d6505f1f68de350d2 (patch)
treee07e4a3dc07ce80104f7949af5cb180b7fce449e /modules/markup/html_test.go
parent985e2a8af3d6468bac3ab178148c38bdbd8414f5 (diff)
downloadgitea-3f9c3e7bc394c115ccc4818d6505f1f68de350d2.tar.gz
gitea-3f9c3e7bc394c115ccc4818d6505f1f68de350d2.zip
Refactor render system (#32492)
There were too many patches to the Render system, it's really difficult to make further improvements. This PR clears the legacy problems and fix TODOs. 1. Rename `RenderContext.Type` to `RenderContext.MarkupType` to clarify its usage. 2. Use `ContentMode` to replace `meta["mode"]` and `IsWiki`, to clarify the rendering behaviors. 3. Use "wiki" mode instead of "mode=gfm + wiki=true" 4. Merge `renderByType` and `renderByFile` 5. Add more comments ---- The problem of "mode=document": in many cases it is not set, so many non-comment places use comment's hard line break incorrectly
Diffstat (limited to 'modules/markup/html_test.go')
-rw-r--r--modules/markup/html_test.go52
1 files changed, 20 insertions, 32 deletions
diff --git a/modules/markup/html_test.go b/modules/markup/html_test.go
index 82aded4407..262d0fc4dd 100644
--- a/modules/markup/html_test.go
+++ b/modules/markup/html_test.go
@@ -14,6 +14,7 @@ import (
"code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/markup/markdown"
"code.gitea.io/gitea/modules/setting"
+ testModule "code.gitea.io/gitea/modules/test"
"code.gitea.io/gitea/modules/util"
"github.com/stretchr/testify/assert"
@@ -104,7 +105,7 @@ func TestRender_Commits(t *testing.T) {
func TestRender_CrossReferences(t *testing.T) {
setting.AppURL = markup.TestAppURL
-
+ defer testModule.MockVariableValue(&markup.RenderBehaviorForTesting.DisableInternalAttributes, true)()
test := func(input, expected string) {
buffer, err := markup.RenderString(&markup.RenderContext{
Ctx: git.DefaultContext,
@@ -116,9 +117,7 @@ func TestRender_CrossReferences(t *testing.T) {
Metas: localMetas,
}, input)
assert.NoError(t, err)
- actual := strings.TrimSpace(buffer)
- actual = strings.ReplaceAll(actual, ` data-markdown-generated-content=""`, "")
- assert.Equal(t, strings.TrimSpace(expected), actual)
+ assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
}
test(
@@ -148,7 +147,7 @@ func TestRender_CrossReferences(t *testing.T) {
func TestRender_links(t *testing.T) {
setting.AppURL = markup.TestAppURL
-
+ defer testModule.MockVariableValue(&markup.RenderBehaviorForTesting.DisableInternalAttributes, true)()
test := func(input, expected string) {
buffer, err := markup.RenderString(&markup.RenderContext{
Ctx: git.DefaultContext,
@@ -158,9 +157,7 @@ func TestRender_links(t *testing.T) {
},
}, input)
assert.NoError(t, err)
- actual := strings.TrimSpace(buffer)
- actual = strings.ReplaceAll(actual, ` data-markdown-generated-content=""`, "")
- assert.Equal(t, strings.TrimSpace(expected), actual)
+ assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
}
oldCustomURLSchemes := setting.Markdown.CustomURLSchemes
@@ -261,7 +258,7 @@ func TestRender_links(t *testing.T) {
func TestRender_email(t *testing.T) {
setting.AppURL = markup.TestAppURL
-
+ defer testModule.MockVariableValue(&markup.RenderBehaviorForTesting.DisableInternalAttributes, true)()
test := func(input, expected string) {
res, err := markup.RenderString(&markup.RenderContext{
Ctx: git.DefaultContext,
@@ -271,9 +268,7 @@ func TestRender_email(t *testing.T) {
},
}, input)
assert.NoError(t, err)
- actual := strings.TrimSpace(res)
- actual = strings.ReplaceAll(actual, ` data-markdown-generated-content=""`, "")
- assert.Equal(t, strings.TrimSpace(expected), actual)
+ assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(res))
}
// Text that should be turned into email link
@@ -302,10 +297,10 @@ func TestRender_email(t *testing.T) {
j.doe@example.com;
j.doe@example.com?
j.doe@example.com!`,
- `<p><a href="mailto:j.doe@example.com" rel="nofollow">j.doe@example.com</a>,<br/>
-<a href="mailto:j.doe@example.com" rel="nofollow">j.doe@example.com</a>.<br/>
-<a href="mailto:j.doe@example.com" rel="nofollow">j.doe@example.com</a>;<br/>
-<a href="mailto:j.doe@example.com" rel="nofollow">j.doe@example.com</a>?<br/>
+ `<p><a href="mailto:j.doe@example.com" rel="nofollow">j.doe@example.com</a>,
+<a href="mailto:j.doe@example.com" rel="nofollow">j.doe@example.com</a>.
+<a href="mailto:j.doe@example.com" rel="nofollow">j.doe@example.com</a>;
+<a href="mailto:j.doe@example.com" rel="nofollow">j.doe@example.com</a>?
<a href="mailto:j.doe@example.com" rel="nofollow">j.doe@example.com</a>!</p>`)
// Test that should *not* be turned into email links
@@ -418,8 +413,8 @@ func TestRender_ShortLinks(t *testing.T) {
Links: markup.Links{
Base: markup.TestRepoURL,
},
- Metas: localMetas,
- IsWiki: true,
+ Metas: localMetas,
+ ContentMode: markup.RenderContentAsWiki,
}, input)
assert.NoError(t, err)
assert.Equal(t, strings.TrimSpace(expectedWiki), strings.TrimSpace(string(buffer)))
@@ -531,10 +526,10 @@ func TestRender_ShortLinks(t *testing.T) {
func TestRender_RelativeMedias(t *testing.T) {
render := func(input string, isWiki bool, links markup.Links) string {
buffer, err := markdown.RenderString(&markup.RenderContext{
- Ctx: git.DefaultContext,
- Links: links,
- Metas: localMetas,
- IsWiki: isWiki,
+ Ctx: git.DefaultContext,
+ Links: links,
+ Metas: localMetas,
+ ContentMode: util.Iif(isWiki, markup.RenderContentAsWiki, markup.RenderContentAsComment),
}, input)
assert.NoError(t, err)
return strings.TrimSpace(string(buffer))
@@ -604,12 +599,7 @@ func Test_ParseClusterFuzz(t *testing.T) {
func TestPostProcess_RenderDocument(t *testing.T) {
setting.AppURL = markup.TestAppURL
setting.StaticURLPrefix = markup.TestAppURL // can't run standalone
-
- localMetas := map[string]string{
- "user": "go-gitea",
- "repo": "gitea",
- "mode": "document",
- }
+ defer testModule.MockVariableValue(&markup.RenderBehaviorForTesting.DisableInternalAttributes, true)()
test := func(input, expected string) {
var res strings.Builder
@@ -619,12 +609,10 @@ func TestPostProcess_RenderDocument(t *testing.T) {
AbsolutePrefix: true,
Base: "https://example.com",
},
- Metas: localMetas,
+ Metas: map[string]string{"user": "go-gitea", "repo": "gitea"},
}, strings.NewReader(input), &res)
assert.NoError(t, err)
- actual := strings.TrimSpace(res.String())
- actual = strings.ReplaceAll(actual, ` data-markdown-generated-content=""`, "")
- assert.Equal(t, strings.TrimSpace(expected), actual)
+ assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(res.String()))
}
// Issue index shouldn't be post processing in a document.