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 26KB

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