You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

html_test.go 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package markup_test
  4. import (
  5. "context"
  6. "io"
  7. "os"
  8. "strings"
  9. "testing"
  10. "code.gitea.io/gitea/models/unittest"
  11. "code.gitea.io/gitea/modules/emoji"
  12. "code.gitea.io/gitea/modules/git"
  13. "code.gitea.io/gitea/modules/log"
  14. "code.gitea.io/gitea/modules/markup"
  15. "code.gitea.io/gitea/modules/markup/markdown"
  16. "code.gitea.io/gitea/modules/setting"
  17. "code.gitea.io/gitea/modules/util"
  18. "github.com/stretchr/testify/assert"
  19. )
  20. var localMetas = map[string]string{
  21. "user": "gogits",
  22. "repo": "gogs",
  23. "repoPath": "../../tests/gitea-repositories-meta/user13/repo11.git/",
  24. }
  25. func TestMain(m *testing.M) {
  26. unittest.InitSettings()
  27. if err := git.InitSimple(context.Background()); err != nil {
  28. log.Fatal("git init failed, err: %v", err)
  29. }
  30. os.Exit(m.Run())
  31. }
  32. func TestRender_Commits(t *testing.T) {
  33. setting.AppURL = markup.TestAppURL
  34. test := func(input, expected string) {
  35. buffer, err := markup.RenderString(&markup.RenderContext{
  36. Ctx: git.DefaultContext,
  37. RelativePath: ".md",
  38. Links: markup.Links{
  39. AbsolutePrefix: true,
  40. Base: markup.TestRepoURL,
  41. },
  42. Metas: localMetas,
  43. }, input)
  44. assert.NoError(t, err)
  45. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
  46. }
  47. sha := "65f1bf27bc3bf70f64657658635e66094edbcb4d"
  48. repo := markup.TestRepoURL
  49. commit := util.URLJoin(repo, "commit", sha)
  50. tree := util.URLJoin(repo, "tree", sha, "src")
  51. file := util.URLJoin(repo, "commit", sha, "example.txt")
  52. fileWithExtra := file + ":"
  53. fileWithHash := file + "#L2"
  54. fileWithHasExtra := file + "#L2:"
  55. commitCompare := util.URLJoin(repo, "compare", sha+"..."+sha)
  56. commitCompareWithHash := commitCompare + "#L2"
  57. test(sha, `<p><a href="`+commit+`" rel="nofollow"><code>65f1bf27bc</code></a></p>`)
  58. test(sha[:7], `<p><a href="`+commit[:len(commit)-(40-7)]+`" rel="nofollow"><code>65f1bf2</code></a></p>`)
  59. test(sha[:39], `<p><a href="`+commit[:len(commit)-(40-39)]+`" rel="nofollow"><code>65f1bf27bc</code></a></p>`)
  60. test(commit, `<p><a href="`+commit+`" rel="nofollow"><code>65f1bf27bc</code></a></p>`)
  61. test(tree, `<p><a href="`+tree+`" rel="nofollow"><code>65f1bf27bc/src</code></a></p>`)
  62. test(file, `<p><a href="`+file+`" rel="nofollow"><code>65f1bf27bc/example.txt</code></a></p>`)
  63. test(fileWithExtra, `<p><a href="`+file+`" rel="nofollow"><code>65f1bf27bc/example.txt</code></a>:</p>`)
  64. test(fileWithHash, `<p><a href="`+fileWithHash+`" rel="nofollow"><code>65f1bf27bc/example.txt (L2)</code></a></p>`)
  65. test(fileWithHasExtra, `<p><a href="`+fileWithHash+`" rel="nofollow"><code>65f1bf27bc/example.txt (L2)</code></a>:</p>`)
  66. test(commitCompare, `<p><a href="`+commitCompare+`" rel="nofollow"><code>65f1bf27bc...65f1bf27bc</code></a></p>`)
  67. test(commitCompareWithHash, `<p><a href="`+commitCompareWithHash+`" rel="nofollow"><code>65f1bf27bc...65f1bf27bc (L2)</code></a></p>`)
  68. test("commit "+sha, `<p>commit <a href="`+commit+`" rel="nofollow"><code>65f1bf27bc</code></a></p>`)
  69. test("/home/gitea/"+sha, "<p>/home/gitea/"+sha+"</p>")
  70. test("deadbeef", `<p>deadbeef</p>`)
  71. test("d27ace93", `<p>d27ace93</p>`)
  72. test(sha[:14]+".x", `<p>`+sha[:14]+`.x</p>`)
  73. expected14 := `<a href="` + commit[:len(commit)-(40-14)] + `" rel="nofollow"><code>` + sha[:10] + `</code></a>`
  74. test(sha[:14]+".", `<p>`+expected14+`.</p>`)
  75. test(sha[:14]+",", `<p>`+expected14+`,</p>`)
  76. test("["+sha[:14]+"]", `<p>[`+expected14+`]</p>`)
  77. }
  78. func TestRender_CrossReferences(t *testing.T) {
  79. setting.AppURL = markup.TestAppURL
  80. test := func(input, expected string) {
  81. buffer, err := markup.RenderString(&markup.RenderContext{
  82. Ctx: git.DefaultContext,
  83. RelativePath: "a.md",
  84. Links: markup.Links{
  85. AbsolutePrefix: true,
  86. Base: setting.AppSubURL,
  87. },
  88. Metas: localMetas,
  89. }, input)
  90. assert.NoError(t, err)
  91. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
  92. }
  93. test(
  94. "gogits/gogs#12345",
  95. `<p><a href="`+util.URLJoin(markup.TestAppURL, "gogits", "gogs", "issues", "12345")+`" class="ref-issue" rel="nofollow">gogits/gogs#12345</a></p>`)
  96. test(
  97. "go-gitea/gitea#12345",
  98. `<p><a href="`+util.URLJoin(markup.TestAppURL, "go-gitea", "gitea", "issues", "12345")+`" class="ref-issue" rel="nofollow">go-gitea/gitea#12345</a></p>`)
  99. test(
  100. "/home/gitea/go-gitea/gitea#12345",
  101. `<p>/home/gitea/go-gitea/gitea#12345</p>`)
  102. test(
  103. util.URLJoin(markup.TestAppURL, "gogitea", "gitea", "issues", "12345"),
  104. `<p><a href="`+util.URLJoin(markup.TestAppURL, "gogitea", "gitea", "issues", "12345")+`" class="ref-issue" rel="nofollow">gogitea/gitea#12345</a></p>`)
  105. test(
  106. util.URLJoin(markup.TestAppURL, "go-gitea", "gitea", "issues", "12345"),
  107. `<p><a href="`+util.URLJoin(markup.TestAppURL, "go-gitea", "gitea", "issues", "12345")+`" class="ref-issue" rel="nofollow">go-gitea/gitea#12345</a></p>`)
  108. test(
  109. util.URLJoin(markup.TestAppURL, "gogitea", "some-repo-name", "issues", "12345"),
  110. `<p><a href="`+util.URLJoin(markup.TestAppURL, "gogitea", "some-repo-name", "issues", "12345")+`" class="ref-issue" rel="nofollow">gogitea/some-repo-name#12345</a></p>`)
  111. inputURL := "https://host/a/b/commit/0123456789012345678901234567890123456789/foo.txt?a=b#L2-L3"
  112. test(
  113. inputURL,
  114. `<p><a href="`+inputURL+`" rel="nofollow"><code>0123456789/foo.txt (L2-L3)</code></a></p>`)
  115. }
  116. func TestMisc_IsSameDomain(t *testing.T) {
  117. setting.AppURL = markup.TestAppURL
  118. sha := "b6dd6210eaebc915fd5be5579c58cce4da2e2579"
  119. commit := util.URLJoin(markup.TestRepoURL, "commit", sha)
  120. assert.True(t, markup.IsSameDomain(commit))
  121. assert.False(t, markup.IsSameDomain("http://google.com/ncr"))
  122. assert.False(t, markup.IsSameDomain("favicon.ico"))
  123. }
  124. func TestRender_links(t *testing.T) {
  125. setting.AppURL = markup.TestAppURL
  126. test := func(input, expected string) {
  127. buffer, err := markup.RenderString(&markup.RenderContext{
  128. Ctx: git.DefaultContext,
  129. RelativePath: "a.md",
  130. Links: markup.Links{
  131. Base: markup.TestRepoURL,
  132. },
  133. }, input)
  134. assert.NoError(t, err)
  135. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
  136. }
  137. // Text that should be turned into URL
  138. defaultCustom := setting.Markdown.CustomURLSchemes
  139. setting.Markdown.CustomURLSchemes = []string{"ftp", "magnet"}
  140. markup.InitializeSanitizer()
  141. markup.CustomLinkURLSchemes(setting.Markdown.CustomURLSchemes)
  142. test(
  143. "https://www.example.com",
  144. `<p><a href="https://www.example.com" rel="nofollow">https://www.example.com</a></p>`)
  145. test(
  146. "http://www.example.com",
  147. `<p><a href="http://www.example.com" rel="nofollow">http://www.example.com</a></p>`)
  148. test(
  149. "https://example.com",
  150. `<p><a href="https://example.com" rel="nofollow">https://example.com</a></p>`)
  151. test(
  152. "http://example.com",
  153. `<p><a href="http://example.com" rel="nofollow">http://example.com</a></p>`)
  154. test(
  155. "http://foo.com/blah_blah",
  156. `<p><a href="http://foo.com/blah_blah" rel="nofollow">http://foo.com/blah_blah</a></p>`)
  157. test(
  158. "http://foo.com/blah_blah/",
  159. `<p><a href="http://foo.com/blah_blah/" rel="nofollow">http://foo.com/blah_blah/</a></p>`)
  160. test(
  161. "http://www.example.com/wpstyle/?p=364",
  162. `<p><a href="http://www.example.com/wpstyle/?p=364" rel="nofollow">http://www.example.com/wpstyle/?p=364</a></p>`)
  163. test(
  164. "https://www.example.com/foo/?bar=baz&inga=42&quux",
  165. `<p><a href="https://www.example.com/foo/?bar=baz&amp;inga=42&amp;quux" rel="nofollow">https://www.example.com/foo/?bar=baz&amp;inga=42&amp;quux</a></p>`)
  166. test(
  167. "http://142.42.1.1/",
  168. `<p><a href="http://142.42.1.1/" rel="nofollow">http://142.42.1.1/</a></p>`)
  169. test(
  170. "https://github.com/go-gitea/gitea/?p=aaa/bbb.html#ccc-ddd",
  171. `<p><a href="https://github.com/go-gitea/gitea/?p=aaa/bbb.html#ccc-ddd" rel="nofollow">https://github.com/go-gitea/gitea/?p=aaa/bbb.html#ccc-ddd</a></p>`)
  172. test(
  173. "https://en.wikipedia.org/wiki/URL_(disambiguation)",
  174. `<p><a href="https://en.wikipedia.org/wiki/URL_(disambiguation)" rel="nofollow">https://en.wikipedia.org/wiki/URL_(disambiguation)</a></p>`)
  175. test(
  176. "https://foo_bar.example.com/",
  177. `<p><a href="https://foo_bar.example.com/" rel="nofollow">https://foo_bar.example.com/</a></p>`)
  178. test(
  179. "https://stackoverflow.com/questions/2896191/what-is-go-used-fore",
  180. `<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>`)
  181. test(
  182. "https://username:password@gitea.com",
  183. `<p><a href="https://username:password@gitea.com" rel="nofollow">https://username:password@gitea.com</a></p>`)
  184. test(
  185. "ftp://gitea.com/file.txt",
  186. `<p><a href="ftp://gitea.com/file.txt" rel="nofollow">ftp://gitea.com/file.txt</a></p>`)
  187. test(
  188. "magnet:?xt=urn:btih:5dee65101db281ac9c46344cd6b175cdcadabcde&dn=download",
  189. `<p><a href="magnet:?xt=urn:btih:5dee65101db281ac9c46344cd6b175cdcadabcde&amp;dn=download" rel="nofollow">magnet:?xt=urn:btih:5dee65101db281ac9c46344cd6b175cdcadabcde&amp;dn=download</a></p>`)
  190. test(
  191. `[link](https://example.com)`,
  192. `<p><a href="https://example.com" rel="nofollow">link</a></p>`)
  193. test(
  194. `[link](mailto:test@example.com)`,
  195. `<p><a href="mailto:test@example.com" rel="nofollow">link</a></p>`)
  196. test(
  197. `[link](javascript:xss)`,
  198. `<p>link</p>`)
  199. // Test that should *not* be turned into URL
  200. test(
  201. "www.example.com",
  202. `<p>www.example.com</p>`)
  203. test(
  204. "example.com",
  205. `<p>example.com</p>`)
  206. test(
  207. "test.example.com",
  208. `<p>test.example.com</p>`)
  209. test(
  210. "http://",
  211. `<p>http://</p>`)
  212. test(
  213. "https://",
  214. `<p>https://</p>`)
  215. test(
  216. "://",
  217. `<p>://</p>`)
  218. test(
  219. "www",
  220. `<p>www</p>`)
  221. test(
  222. "ftps://gitea.com",
  223. `<p>ftps://gitea.com</p>`)
  224. // Restore previous settings
  225. setting.Markdown.CustomURLSchemes = defaultCustom
  226. markup.InitializeSanitizer()
  227. markup.CustomLinkURLSchemes(setting.Markdown.CustomURLSchemes)
  228. }
  229. func TestRender_email(t *testing.T) {
  230. setting.AppURL = markup.TestAppURL
  231. test := func(input, expected string) {
  232. res, err := markup.RenderString(&markup.RenderContext{
  233. Ctx: git.DefaultContext,
  234. RelativePath: "a.md",
  235. Links: markup.Links{
  236. Base: markup.TestRepoURL,
  237. },
  238. }, input)
  239. assert.NoError(t, err)
  240. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(res))
  241. }
  242. // Text that should be turned into email link
  243. test(
  244. "info@gitea.com",
  245. `<p><a href="mailto:info@gitea.com" rel="nofollow">info@gitea.com</a></p>`)
  246. test(
  247. "(info@gitea.com)",
  248. `<p>(<a href="mailto:info@gitea.com" rel="nofollow">info@gitea.com</a>)</p>`)
  249. test(
  250. "[info@gitea.com]",
  251. `<p>[<a href="mailto:info@gitea.com" rel="nofollow">info@gitea.com</a>]</p>`)
  252. test(
  253. "info@gitea.com.",
  254. `<p><a href="mailto:info@gitea.com" rel="nofollow">info@gitea.com</a>.</p>`)
  255. test(
  256. "firstname+lastname@gitea.com",
  257. `<p><a href="mailto:firstname+lastname@gitea.com" rel="nofollow">firstname+lastname@gitea.com</a></p>`)
  258. test(
  259. "send email to info@gitea.co.uk.",
  260. `<p>send email to <a href="mailto:info@gitea.co.uk" rel="nofollow">info@gitea.co.uk</a>.</p>`)
  261. test(
  262. `j.doe@example.com,
  263. j.doe@example.com.
  264. j.doe@example.com;
  265. j.doe@example.com?
  266. j.doe@example.com!`,
  267. `<p><a href="mailto:j.doe@example.com" rel="nofollow">j.doe@example.com</a>,<br/>
  268. <a href="mailto:j.doe@example.com" rel="nofollow">j.doe@example.com</a>.<br/>
  269. <a href="mailto:j.doe@example.com" rel="nofollow">j.doe@example.com</a>;<br/>
  270. <a href="mailto:j.doe@example.com" rel="nofollow">j.doe@example.com</a>?<br/>
  271. <a href="mailto:j.doe@example.com" rel="nofollow">j.doe@example.com</a>!</p>`)
  272. // Test that should *not* be turned into email links
  273. test(
  274. "\"info@gitea.com\"",
  275. `<p>&#34;info@gitea.com&#34;</p>`)
  276. test(
  277. "/home/gitea/mailstore/info@gitea/com",
  278. `<p>/home/gitea/mailstore/info@gitea/com</p>`)
  279. test(
  280. "git@try.gitea.io:go-gitea/gitea.git",
  281. `<p>git@try.gitea.io:go-gitea/gitea.git</p>`)
  282. test(
  283. "gitea@3",
  284. `<p>gitea@3</p>`)
  285. test(
  286. "gitea@gmail.c",
  287. `<p>gitea@gmail.c</p>`)
  288. test(
  289. "email@domain@domain.com",
  290. `<p>email@domain@domain.com</p>`)
  291. test(
  292. "email@domain..com",
  293. `<p>email@domain..com</p>`)
  294. }
  295. func TestRender_emoji(t *testing.T) {
  296. setting.AppURL = markup.TestAppURL
  297. setting.StaticURLPrefix = markup.TestAppURL
  298. test := func(input, expected string) {
  299. expected = strings.ReplaceAll(expected, "&", "&amp;")
  300. buffer, err := markup.RenderString(&markup.RenderContext{
  301. Ctx: git.DefaultContext,
  302. RelativePath: "a.md",
  303. Links: markup.Links{
  304. Base: markup.TestRepoURL,
  305. },
  306. }, input)
  307. assert.NoError(t, err)
  308. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
  309. }
  310. // Make sure we can successfully match every emoji in our dataset with regex
  311. for i := range emoji.GemojiData {
  312. test(
  313. emoji.GemojiData[i].Emoji,
  314. `<p><span class="emoji" aria-label="`+emoji.GemojiData[i].Description+`">`+emoji.GemojiData[i].Emoji+`</span></p>`)
  315. }
  316. for i := range emoji.GemojiData {
  317. test(
  318. ":"+emoji.GemojiData[i].Aliases[0]+":",
  319. `<p><span class="emoji" aria-label="`+emoji.GemojiData[i].Description+`">`+emoji.GemojiData[i].Emoji+`</span></p>`)
  320. }
  321. // Text that should be turned into or recognized as emoji
  322. test(
  323. ":gitea:",
  324. `<p><span class="emoji" aria-label="gitea"><img alt=":gitea:" src="`+setting.StaticURLPrefix+`/assets/img/emoji/gitea.png"/></span></p>`)
  325. test(
  326. ":custom-emoji:",
  327. `<p>:custom-emoji:</p>`)
  328. setting.UI.CustomEmojisMap["custom-emoji"] = ":custom-emoji:"
  329. test(
  330. ":custom-emoji:",
  331. `<p><span class="emoji" aria-label="custom-emoji"><img alt=":custom-emoji:" src="`+setting.StaticURLPrefix+`/assets/img/emoji/custom-emoji.png"/></span></p>`)
  332. test(
  333. "这是字符:1::+1: some🐊 \U0001f44d:custom-emoji: :gitea:",
  334. `<p>这是字符:1:<span class="emoji" aria-label="thumbs up">👍</span> some<span class="emoji" aria-label="crocodile">🐊</span> `+
  335. `<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> `+
  336. `<span class="emoji" aria-label="gitea"><img alt=":gitea:" src="`+setting.StaticURLPrefix+`/assets/img/emoji/gitea.png"/></span></p>`)
  337. test(
  338. "Some text with 😄 in the middle",
  339. `<p>Some text with <span class="emoji" aria-label="grinning face with smiling eyes">😄</span> in the middle</p>`)
  340. test(
  341. "Some text with :smile: in the middle",
  342. `<p>Some text with <span class="emoji" aria-label="grinning face with smiling eyes">😄</span> in the middle</p>`)
  343. test(
  344. "Some text with 😄😄 2 emoji next to each other",
  345. `<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>`)
  346. test(
  347. "😎🤪🔐🤑❓",
  348. `<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>`)
  349. // should match nothing
  350. test(
  351. "2001:0db8:85a3:0000:0000:8a2e:0370:7334",
  352. `<p>2001:0db8:85a3:0000:0000:8a2e:0370:7334</p>`)
  353. test(
  354. ":not exist:",
  355. `<p>:not exist:</p>`)
  356. }
  357. func TestRender_ShortLinks(t *testing.T) {
  358. setting.AppURL = markup.TestAppURL
  359. tree := util.URLJoin(markup.TestRepoURL, "src", "master")
  360. test := func(input, expected, expectedWiki string) {
  361. buffer, err := markdown.RenderString(&markup.RenderContext{
  362. Ctx: git.DefaultContext,
  363. Links: markup.Links{
  364. Base: markup.TestRepoURL,
  365. BranchPath: "master",
  366. },
  367. }, input)
  368. assert.NoError(t, err)
  369. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
  370. buffer, err = markdown.RenderString(&markup.RenderContext{
  371. Ctx: git.DefaultContext,
  372. Links: markup.Links{
  373. Base: markup.TestRepoURL,
  374. },
  375. Metas: localMetas,
  376. IsWiki: true,
  377. }, input)
  378. assert.NoError(t, err)
  379. assert.Equal(t, strings.TrimSpace(expectedWiki), strings.TrimSpace(string(buffer)))
  380. }
  381. mediatree := util.URLJoin(markup.TestRepoURL, "media", "master")
  382. url := util.URLJoin(tree, "Link")
  383. otherURL := util.URLJoin(tree, "Other-Link")
  384. encodedURL := util.URLJoin(tree, "Link%3F")
  385. imgurl := util.URLJoin(mediatree, "Link.jpg")
  386. otherImgurl := util.URLJoin(mediatree, "Link+Other.jpg")
  387. encodedImgurl := util.URLJoin(mediatree, "Link+%23.jpg")
  388. notencodedImgurl := util.URLJoin(mediatree, "some", "path", "Link+#.jpg")
  389. urlWiki := util.URLJoin(markup.TestRepoURL, "wiki", "Link")
  390. otherURLWiki := util.URLJoin(markup.TestRepoURL, "wiki", "Other-Link")
  391. encodedURLWiki := util.URLJoin(markup.TestRepoURL, "wiki", "Link%3F")
  392. imgurlWiki := util.URLJoin(markup.TestRepoURL, "wiki", "raw", "Link.jpg")
  393. otherImgurlWiki := util.URLJoin(markup.TestRepoURL, "wiki", "raw", "Link+Other.jpg")
  394. encodedImgurlWiki := util.URLJoin(markup.TestRepoURL, "wiki", "raw", "Link+%23.jpg")
  395. notencodedImgurlWiki := util.URLJoin(markup.TestRepoURL, "wiki", "raw", "some", "path", "Link+#.jpg")
  396. renderableFileURL := util.URLJoin(tree, "markdown_file.md")
  397. renderableFileURLWiki := util.URLJoin(markup.TestRepoURL, "wiki", "markdown_file.md")
  398. unrenderableFileURL := util.URLJoin(tree, "file.zip")
  399. unrenderableFileURLWiki := util.URLJoin(markup.TestRepoURL, "wiki", "raw", "file.zip")
  400. favicon := "http://google.com/favicon.ico"
  401. test(
  402. "[[Link]]",
  403. `<p><a href="`+url+`" rel="nofollow">Link</a></p>`,
  404. `<p><a href="`+urlWiki+`" rel="nofollow">Link</a></p>`)
  405. test(
  406. "[[Link.jpg]]",
  407. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="Link.jpg" alt="Link.jpg"/></a></p>`,
  408. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" title="Link.jpg" alt="Link.jpg"/></a></p>`)
  409. test(
  410. "[["+favicon+"]]",
  411. `<p><a href="`+favicon+`" rel="nofollow"><img src="`+favicon+`" title="favicon.ico" alt="`+favicon+`"/></a></p>`,
  412. `<p><a href="`+favicon+`" rel="nofollow"><img src="`+favicon+`" title="favicon.ico" alt="`+favicon+`"/></a></p>`)
  413. test(
  414. "[[Name|Link]]",
  415. `<p><a href="`+url+`" rel="nofollow">Name</a></p>`,
  416. `<p><a href="`+urlWiki+`" rel="nofollow">Name</a></p>`)
  417. test(
  418. "[[Name|Link.jpg]]",
  419. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="Name" alt="Name"/></a></p>`,
  420. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" title="Name" alt="Name"/></a></p>`)
  421. test(
  422. "[[Name|Link.jpg|alt=AltName]]",
  423. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="AltName" alt="AltName"/></a></p>`,
  424. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" title="AltName" alt="AltName"/></a></p>`)
  425. test(
  426. "[[Name|Link.jpg|title=Title]]",
  427. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="Title" alt="Title"/></a></p>`,
  428. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" title="Title" alt="Title"/></a></p>`)
  429. test(
  430. "[[Name|Link.jpg|alt=AltName|title=Title]]",
  431. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="Title" alt="AltName"/></a></p>`,
  432. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" title="Title" alt="AltName"/></a></p>`)
  433. test(
  434. "[[Name|Link.jpg|alt=\"AltName\"|title='Title']]",
  435. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="Title" alt="AltName"/></a></p>`,
  436. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" title="Title" alt="AltName"/></a></p>`)
  437. test(
  438. "[[Name|Link Other.jpg|alt=\"AltName\"|title='Title']]",
  439. `<p><a href="`+otherImgurl+`" rel="nofollow"><img src="`+otherImgurl+`" title="Title" alt="AltName"/></a></p>`,
  440. `<p><a href="`+otherImgurlWiki+`" rel="nofollow"><img src="`+otherImgurlWiki+`" title="Title" alt="AltName"/></a></p>`)
  441. test(
  442. "[[Link]] [[Other Link]]",
  443. `<p><a href="`+url+`" rel="nofollow">Link</a> <a href="`+otherURL+`" rel="nofollow">Other Link</a></p>`,
  444. `<p><a href="`+urlWiki+`" rel="nofollow">Link</a> <a href="`+otherURLWiki+`" rel="nofollow">Other Link</a></p>`)
  445. test(
  446. "[[Link?]]",
  447. `<p><a href="`+encodedURL+`" rel="nofollow">Link?</a></p>`,
  448. `<p><a href="`+encodedURLWiki+`" rel="nofollow">Link?</a></p>`)
  449. test(
  450. "[[Link]] [[Other Link]] [[Link?]]",
  451. `<p><a href="`+url+`" rel="nofollow">Link</a> <a href="`+otherURL+`" rel="nofollow">Other Link</a> <a href="`+encodedURL+`" rel="nofollow">Link?</a></p>`,
  452. `<p><a href="`+urlWiki+`" rel="nofollow">Link</a> <a href="`+otherURLWiki+`" rel="nofollow">Other Link</a> <a href="`+encodedURLWiki+`" rel="nofollow">Link?</a></p>`)
  453. test(
  454. "[[markdown_file.md]]",
  455. `<p><a href="`+renderableFileURL+`" rel="nofollow">markdown_file.md</a></p>`,
  456. `<p><a href="`+renderableFileURLWiki+`" rel="nofollow">markdown_file.md</a></p>`)
  457. test(
  458. "[[file.zip]]",
  459. `<p><a href="`+unrenderableFileURL+`" rel="nofollow">file.zip</a></p>`,
  460. `<p><a href="`+unrenderableFileURLWiki+`" rel="nofollow">file.zip</a></p>`)
  461. test(
  462. "[[Link #.jpg]]",
  463. `<p><a href="`+encodedImgurl+`" rel="nofollow"><img src="`+encodedImgurl+`" title="Link #.jpg" alt="Link #.jpg"/></a></p>`,
  464. `<p><a href="`+encodedImgurlWiki+`" rel="nofollow"><img src="`+encodedImgurlWiki+`" title="Link #.jpg" alt="Link #.jpg"/></a></p>`)
  465. test(
  466. "[[Name|Link #.jpg|alt=\"AltName\"|title='Title']]",
  467. `<p><a href="`+encodedImgurl+`" rel="nofollow"><img src="`+encodedImgurl+`" title="Title" alt="AltName"/></a></p>`,
  468. `<p><a href="`+encodedImgurlWiki+`" rel="nofollow"><img src="`+encodedImgurlWiki+`" title="Title" alt="AltName"/></a></p>`)
  469. test(
  470. "[[some/path/Link #.jpg]]",
  471. `<p><a href="`+notencodedImgurl+`" rel="nofollow"><img src="`+notencodedImgurl+`" title="Link #.jpg" alt="some/path/Link #.jpg"/></a></p>`,
  472. `<p><a href="`+notencodedImgurlWiki+`" rel="nofollow"><img src="`+notencodedImgurlWiki+`" title="Link #.jpg" alt="some/path/Link #.jpg"/></a></p>`)
  473. test(
  474. "<p><a href=\"https://example.org\">[[foobar]]</a></p>",
  475. `<p><a href="https://example.org" rel="nofollow">[[foobar]]</a></p>`,
  476. `<p><a href="https://example.org" rel="nofollow">[[foobar]]</a></p>`)
  477. }
  478. func TestRender_RelativeImages(t *testing.T) {
  479. setting.AppURL = markup.TestAppURL
  480. test := func(input, expected, expectedWiki string) {
  481. buffer, err := markdown.RenderString(&markup.RenderContext{
  482. Ctx: git.DefaultContext,
  483. Links: markup.Links{
  484. Base: markup.TestRepoURL,
  485. BranchPath: "master",
  486. },
  487. Metas: localMetas,
  488. }, input)
  489. assert.NoError(t, err)
  490. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
  491. buffer, err = markdown.RenderString(&markup.RenderContext{
  492. Ctx: git.DefaultContext,
  493. Links: markup.Links{
  494. Base: markup.TestRepoURL,
  495. },
  496. Metas: localMetas,
  497. IsWiki: true,
  498. }, input)
  499. assert.NoError(t, err)
  500. assert.Equal(t, strings.TrimSpace(expectedWiki), strings.TrimSpace(string(buffer)))
  501. }
  502. rawwiki := util.URLJoin(markup.TestRepoURL, "wiki", "raw")
  503. mediatree := util.URLJoin(markup.TestRepoURL, "media", "master")
  504. test(
  505. `<img src="Link">`,
  506. `<img src="`+util.URLJoin(mediatree, "Link")+`"/>`,
  507. `<img src="`+util.URLJoin(rawwiki, "Link")+`"/>`)
  508. test(
  509. `<img src="./icon.png">`,
  510. `<img src="`+util.URLJoin(mediatree, "icon.png")+`"/>`,
  511. `<img src="`+util.URLJoin(rawwiki, "icon.png")+`"/>`)
  512. }
  513. func Test_ParseClusterFuzz(t *testing.T) {
  514. setting.AppURL = markup.TestAppURL
  515. localMetas := map[string]string{
  516. "user": "go-gitea",
  517. "repo": "gitea",
  518. }
  519. data := "<A><maTH><tr><MN><bodY ÿ><temPlate></template><tH><tr></A><tH><d<bodY "
  520. var res strings.Builder
  521. err := markup.PostProcess(&markup.RenderContext{
  522. Ctx: git.DefaultContext,
  523. Links: markup.Links{
  524. Base: "https://example.com",
  525. },
  526. Metas: localMetas,
  527. }, strings.NewReader(data), &res)
  528. assert.NoError(t, err)
  529. assert.NotContains(t, res.String(), "<html")
  530. data = "<!DOCTYPE html>\n<A><maTH><tr><MN><bodY ÿ><temPlate></template><tH><tr></A><tH><d<bodY "
  531. res.Reset()
  532. err = markup.PostProcess(&markup.RenderContext{
  533. Ctx: git.DefaultContext,
  534. Links: markup.Links{
  535. Base: "https://example.com",
  536. },
  537. Metas: localMetas,
  538. }, strings.NewReader(data), &res)
  539. assert.NoError(t, err)
  540. assert.NotContains(t, res.String(), "<html")
  541. }
  542. func TestPostProcess_RenderDocument(t *testing.T) {
  543. setting.AppURL = markup.TestAppURL
  544. setting.StaticURLPrefix = markup.TestAppURL // can't run standalone
  545. localMetas := map[string]string{
  546. "user": "go-gitea",
  547. "repo": "gitea",
  548. "mode": "document",
  549. }
  550. test := func(input, expected string) {
  551. var res strings.Builder
  552. err := markup.PostProcess(&markup.RenderContext{
  553. Ctx: git.DefaultContext,
  554. Links: markup.Links{
  555. AbsolutePrefix: true,
  556. Base: "https://example.com",
  557. },
  558. Metas: localMetas,
  559. }, strings.NewReader(input), &res)
  560. assert.NoError(t, err)
  561. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(res.String()))
  562. }
  563. // Issue index shouldn't be post processing in a document.
  564. test(
  565. "#1",
  566. "#1")
  567. // But cross-referenced issue index should work.
  568. test(
  569. "go-gitea/gitea#12345",
  570. `<a href="`+util.URLJoin(markup.TestAppURL, "go-gitea", "gitea", "issues", "12345")+`" class="ref-issue">go-gitea/gitea#12345</a>`)
  571. // Test that other post processing still works.
  572. test(
  573. ":gitea:",
  574. `<span class="emoji" aria-label="gitea"><img alt=":gitea:" src="`+setting.StaticURLPrefix+`/assets/img/emoji/gitea.png"/></span>`)
  575. test(
  576. "Some text with 😄 in the middle",
  577. `Some text with <span class="emoji" aria-label="grinning face with smiling eyes">😄</span> in the middle`)
  578. test("http://localhost:3000/person/repo/issues/4#issuecomment-1234",
  579. `<a href="http://localhost:3000/person/repo/issues/4#issuecomment-1234" class="ref-issue">person/repo#4 (comment)</a>`)
  580. }
  581. func TestIssue16020(t *testing.T) {
  582. setting.AppURL = markup.TestAppURL
  583. localMetas := map[string]string{
  584. "user": "go-gitea",
  585. "repo": "gitea",
  586. }
  587. data := `<img src="data:image/png;base64,i//V"/>`
  588. var res strings.Builder
  589. err := markup.PostProcess(&markup.RenderContext{
  590. Ctx: git.DefaultContext,
  591. Metas: localMetas,
  592. }, strings.NewReader(data), &res)
  593. assert.NoError(t, err)
  594. assert.Equal(t, data, res.String())
  595. }
  596. func BenchmarkEmojiPostprocess(b *testing.B) {
  597. data := "🥰 "
  598. for len(data) < 1<<16 {
  599. data += data
  600. }
  601. b.ResetTimer()
  602. for i := 0; i < b.N; i++ {
  603. var res strings.Builder
  604. err := markup.PostProcess(&markup.RenderContext{
  605. Ctx: git.DefaultContext,
  606. Metas: localMetas,
  607. }, strings.NewReader(data), &res)
  608. assert.NoError(b, err)
  609. }
  610. }
  611. func TestFuzz(t *testing.T) {
  612. s := "t/l/issues/8#/../../a"
  613. renderContext := markup.RenderContext{
  614. Ctx: git.DefaultContext,
  615. Links: markup.Links{
  616. Base: "https://example.com/go-gitea/gitea",
  617. },
  618. Metas: map[string]string{
  619. "user": "go-gitea",
  620. "repo": "gitea",
  621. },
  622. }
  623. err := markup.PostProcess(&renderContext, strings.NewReader(s), io.Discard)
  624. assert.NoError(t, err)
  625. }
  626. func TestIssue18471(t *testing.T) {
  627. data := `http://domain/org/repo/compare/783b039...da951ce`
  628. var res strings.Builder
  629. err := markup.PostProcess(&markup.RenderContext{
  630. Ctx: git.DefaultContext,
  631. Metas: localMetas,
  632. }, strings.NewReader(data), &res)
  633. assert.NoError(t, err)
  634. assert.Equal(t, `<a href="http://domain/org/repo/compare/783b039...da951ce" class="compare"><code class="nohighlight">783b039...da951ce</code></a>`, res.String())
  635. }
  636. func TestIsFullURL(t *testing.T) {
  637. assert.True(t, markup.IsFullURLString("https://example.com"))
  638. assert.True(t, markup.IsFullURLString("mailto:test@example.com"))
  639. assert.False(t, markup.IsFullURLString("/foo:bar"))
  640. }