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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package markup_test
  5. import (
  6. "strings"
  7. "testing"
  8. "code.gitea.io/gitea/modules/emoji"
  9. . "code.gitea.io/gitea/modules/markup"
  10. "code.gitea.io/gitea/modules/markup/markdown"
  11. "code.gitea.io/gitea/modules/setting"
  12. "code.gitea.io/gitea/modules/util"
  13. "github.com/stretchr/testify/assert"
  14. )
  15. var localMetas = map[string]string{
  16. "user": "gogits",
  17. "repo": "gogs",
  18. "repoPath": "../../integrations/gitea-repositories-meta/user13/repo11.git/",
  19. }
  20. func TestRender_Commits(t *testing.T) {
  21. setting.AppURL = AppURL
  22. setting.AppSubURL = AppSubURL
  23. test := func(input, expected string) {
  24. buffer := RenderString(".md", input, setting.AppSubURL, localMetas)
  25. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
  26. }
  27. var sha = "65f1bf27bc3bf70f64657658635e66094edbcb4d"
  28. var commit = util.URLJoin(AppSubURL, "commit", sha)
  29. var subtree = util.URLJoin(commit, "src")
  30. var tree = strings.Replace(subtree, "/commit/", "/tree/", -1)
  31. test(sha, `<p><a href="`+commit+`" rel="nofollow"><code>65f1bf27bc</code></a></p>`)
  32. test(sha[:7], `<p><a href="`+commit[:len(commit)-(40-7)]+`" rel="nofollow"><code>65f1bf2</code></a></p>`)
  33. test(sha[:39], `<p><a href="`+commit[:len(commit)-(40-39)]+`" rel="nofollow"><code>65f1bf27bc</code></a></p>`)
  34. test(commit, `<p><a href="`+commit+`" rel="nofollow"><code>65f1bf27bc</code></a></p>`)
  35. test(tree, `<p><a href="`+tree+`" rel="nofollow"><code>65f1bf27bc/src</code></a></p>`)
  36. test("commit "+sha, `<p>commit <a href="`+commit+`" rel="nofollow"><code>65f1bf27bc</code></a></p>`)
  37. test("/home/gitea/"+sha, "<p>/home/gitea/"+sha+"</p>")
  38. test("deadbeef", `<p>deadbeef</p>`)
  39. test("d27ace93", `<p>d27ace93</p>`)
  40. }
  41. func TestRender_CrossReferences(t *testing.T) {
  42. setting.AppURL = AppURL
  43. setting.AppSubURL = AppSubURL
  44. test := func(input, expected string) {
  45. buffer := RenderString("a.md", input, setting.AppSubURL, localMetas)
  46. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
  47. }
  48. test(
  49. "gogits/gogs#12345",
  50. `<p><a href="`+util.URLJoin(AppURL, "gogits", "gogs", "issues", "12345")+`" class="ref-issue" rel="nofollow">gogits/gogs#12345</a></p>`)
  51. test(
  52. "go-gitea/gitea#12345",
  53. `<p><a href="`+util.URLJoin(AppURL, "go-gitea", "gitea", "issues", "12345")+`" class="ref-issue" rel="nofollow">go-gitea/gitea#12345</a></p>`)
  54. test(
  55. "/home/gitea/go-gitea/gitea#12345",
  56. `<p>/home/gitea/go-gitea/gitea#12345</p>`)
  57. }
  58. func TestMisc_IsSameDomain(t *testing.T) {
  59. setting.AppURL = AppURL
  60. setting.AppSubURL = AppSubURL
  61. var sha = "b6dd6210eaebc915fd5be5579c58cce4da2e2579"
  62. var commit = util.URLJoin(AppSubURL, "commit", sha)
  63. assert.True(t, IsSameDomain(commit))
  64. assert.False(t, IsSameDomain("http://google.com/ncr"))
  65. assert.False(t, IsSameDomain("favicon.ico"))
  66. }
  67. func TestRender_links(t *testing.T) {
  68. setting.AppURL = AppURL
  69. setting.AppSubURL = AppSubURL
  70. test := func(input, expected string) {
  71. buffer := RenderString("a.md", input, setting.AppSubURL, nil)
  72. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
  73. }
  74. // Text that should be turned into URL
  75. defaultCustom := setting.Markdown.CustomURLSchemes
  76. setting.Markdown.CustomURLSchemes = []string{"ftp", "magnet"}
  77. ReplaceSanitizer()
  78. CustomLinkURLSchemes(setting.Markdown.CustomURLSchemes)
  79. test(
  80. "https://www.example.com",
  81. `<p><a href="https://www.example.com" rel="nofollow">https://www.example.com</a></p>`)
  82. test(
  83. "http://www.example.com",
  84. `<p><a href="http://www.example.com" rel="nofollow">http://www.example.com</a></p>`)
  85. test(
  86. "https://example.com",
  87. `<p><a href="https://example.com" rel="nofollow">https://example.com</a></p>`)
  88. test(
  89. "http://example.com",
  90. `<p><a href="http://example.com" rel="nofollow">http://example.com</a></p>`)
  91. test(
  92. "http://foo.com/blah_blah",
  93. `<p><a href="http://foo.com/blah_blah" rel="nofollow">http://foo.com/blah_blah</a></p>`)
  94. test(
  95. "http://foo.com/blah_blah/",
  96. `<p><a href="http://foo.com/blah_blah/" rel="nofollow">http://foo.com/blah_blah/</a></p>`)
  97. test(
  98. "http://www.example.com/wpstyle/?p=364",
  99. `<p><a href="http://www.example.com/wpstyle/?p=364" rel="nofollow">http://www.example.com/wpstyle/?p=364</a></p>`)
  100. test(
  101. "https://www.example.com/foo/?bar=baz&inga=42&quux",
  102. `<p><a href="https://www.example.com/foo/?bar=baz&inga=42&quux=" rel="nofollow">https://www.example.com/foo/?bar=baz&amp;inga=42&amp;quux</a></p>`)
  103. test(
  104. "http://142.42.1.1/",
  105. `<p><a href="http://142.42.1.1/" rel="nofollow">http://142.42.1.1/</a></p>`)
  106. test(
  107. "https://github.com/go-gitea/gitea/?p=aaa/bbb.html#ccc-ddd",
  108. `<p><a href="https://github.com/go-gitea/gitea/?p=aaa%2Fbbb.html#ccc-ddd" rel="nofollow">https://github.com/go-gitea/gitea/?p=aaa/bbb.html#ccc-ddd</a></p>`)
  109. test(
  110. "https://en.wikipedia.org/wiki/URL_(disambiguation)",
  111. `<p><a href="https://en.wikipedia.org/wiki/URL_(disambiguation)" rel="nofollow">https://en.wikipedia.org/wiki/URL_(disambiguation)</a></p>`)
  112. test(
  113. "https://foo_bar.example.com/",
  114. `<p><a href="https://foo_bar.example.com/" rel="nofollow">https://foo_bar.example.com/</a></p>`)
  115. test(
  116. "https://stackoverflow.com/questions/2896191/what-is-go-used-fore",
  117. `<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>`)
  118. test(
  119. "https://username:password@gitea.com",
  120. `<p><a href="https://username:password@gitea.com" rel="nofollow">https://username:password@gitea.com</a></p>`)
  121. test(
  122. "ftp://gitea.com/file.txt",
  123. `<p><a href="ftp://gitea.com/file.txt" rel="nofollow">ftp://gitea.com/file.txt</a></p>`)
  124. test(
  125. "magnet:?xt=urn:btih:5dee65101db281ac9c46344cd6b175cdcadabcde&dn=download",
  126. `<p><a href="magnet:?dn=download&xt=urn%3Abtih%3A5dee65101db281ac9c46344cd6b175cdcadabcde" rel="nofollow">magnet:?xt=urn:btih:5dee65101db281ac9c46344cd6b175cdcadabcde&amp;dn=download</a></p>`)
  127. // Test that should *not* be turned into URL
  128. test(
  129. "www.example.com",
  130. `<p>www.example.com</p>`)
  131. test(
  132. "example.com",
  133. `<p>example.com</p>`)
  134. test(
  135. "test.example.com",
  136. `<p>test.example.com</p>`)
  137. test(
  138. "http://",
  139. `<p>http://</p>`)
  140. test(
  141. "https://",
  142. `<p>https://</p>`)
  143. test(
  144. "://",
  145. `<p>://</p>`)
  146. test(
  147. "www",
  148. `<p>www</p>`)
  149. test(
  150. "ftps://gitea.com",
  151. `<p>ftps://gitea.com</p>`)
  152. // Restore previous settings
  153. setting.Markdown.CustomURLSchemes = defaultCustom
  154. ReplaceSanitizer()
  155. CustomLinkURLSchemes(setting.Markdown.CustomURLSchemes)
  156. }
  157. func TestRender_email(t *testing.T) {
  158. setting.AppURL = AppURL
  159. setting.AppSubURL = AppSubURL
  160. test := func(input, expected string) {
  161. buffer := RenderString("a.md", input, setting.AppSubURL, nil)
  162. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
  163. }
  164. // Text that should be turned into email link
  165. test(
  166. "info@gitea.com",
  167. `<p><a href="mailto:info@gitea.com" rel="nofollow">info@gitea.com</a></p>`)
  168. test(
  169. "(info@gitea.com)",
  170. `<p>(<a href="mailto:info@gitea.com" rel="nofollow">info@gitea.com</a>)</p>`)
  171. test(
  172. "[info@gitea.com]",
  173. `<p>[<a href="mailto:info@gitea.com" rel="nofollow">info@gitea.com</a>]</p>`)
  174. test(
  175. "info@gitea.com.",
  176. `<p><a href="mailto:info@gitea.com" rel="nofollow">info@gitea.com</a>.</p>`)
  177. test(
  178. "firstname+lastname@gitea.com",
  179. `<p><a href="mailto:firstname+lastname@gitea.com" rel="nofollow">firstname+lastname@gitea.com</a></p>`)
  180. test(
  181. "send email to info@gitea.co.uk.",
  182. `<p>send email to <a href="mailto:info@gitea.co.uk" rel="nofollow">info@gitea.co.uk</a>.</p>`)
  183. // Test that should *not* be turned into email links
  184. test(
  185. "\"info@gitea.com\"",
  186. `<p>&#34;info@gitea.com&#34;</p>`)
  187. test(
  188. "/home/gitea/mailstore/info@gitea/com",
  189. `<p>/home/gitea/mailstore/info@gitea/com</p>`)
  190. test(
  191. "git@try.gitea.io:go-gitea/gitea.git",
  192. `<p>git@try.gitea.io:go-gitea/gitea.git</p>`)
  193. test(
  194. "gitea@3",
  195. `<p>gitea@3</p>`)
  196. test(
  197. "gitea@gmail.c",
  198. `<p>gitea@gmail.c</p>`)
  199. test(
  200. "email@domain@domain.com",
  201. `<p>email@domain@domain.com</p>`)
  202. test(
  203. "email@domain..com",
  204. `<p>email@domain..com</p>`)
  205. }
  206. func TestRender_emoji(t *testing.T) {
  207. setting.AppURL = AppURL
  208. setting.AppSubURL = AppSubURL
  209. setting.StaticURLPrefix = AppURL
  210. test := func(input, expected string) {
  211. expected = strings.Replace(expected, "&", "&amp;", -1)
  212. buffer := RenderString("a.md", input, setting.AppSubURL, nil)
  213. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
  214. }
  215. // Make sure we can successfully match every emoji in our dataset with regex
  216. for i := range emoji.GemojiData {
  217. test(
  218. emoji.GemojiData[i].Emoji,
  219. `<p><span class="emoji" aria-label="`+emoji.GemojiData[i].Description+`">`+emoji.GemojiData[i].Emoji+`</span></p>`)
  220. }
  221. for i := range emoji.GemojiData {
  222. test(
  223. ":"+emoji.GemojiData[i].Aliases[0]+":",
  224. `<p><span class="emoji" aria-label="`+emoji.GemojiData[i].Description+`">`+emoji.GemojiData[i].Emoji+`</span></p>`)
  225. }
  226. //Text that should be turned into or recognized as emoji
  227. test(
  228. ":gitea:",
  229. `<p><span class="emoji" aria-label="gitea"><img src="`+setting.StaticURLPrefix+`/img/emoji/gitea.png"/></span></p>`)
  230. test(
  231. "Some text with 😄 in the middle",
  232. `<p>Some text with <span class="emoji" aria-label="grinning face with smiling eyes">😄</span> in the middle</p>`)
  233. test(
  234. "Some text with :smile: in the middle",
  235. `<p>Some text with <span class="emoji" aria-label="grinning face with smiling eyes">😄</span> in the middle</p>`)
  236. test(
  237. "Some text with 😄😄 2 emoji next to each other",
  238. `<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>`)
  239. test(
  240. "😎🤪🔐🤑❓",
  241. `<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="question mark">❓</span></p>`)
  242. // should match nothing
  243. test(
  244. "2001:0db8:85a3:0000:0000:8a2e:0370:7334",
  245. `<p>2001:0db8:85a3:0000:0000:8a2e:0370:7334</p>`)
  246. test(
  247. ":not exist:",
  248. `<p>:not exist:</p>`)
  249. }
  250. func TestRender_ShortLinks(t *testing.T) {
  251. setting.AppURL = AppURL
  252. setting.AppSubURL = AppSubURL
  253. tree := util.URLJoin(AppSubURL, "src", "master")
  254. test := func(input, expected, expectedWiki string) {
  255. buffer := markdown.RenderString(input, tree, nil)
  256. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
  257. buffer = markdown.RenderWiki([]byte(input), setting.AppSubURL, localMetas)
  258. assert.Equal(t, strings.TrimSpace(expectedWiki), strings.TrimSpace(buffer))
  259. }
  260. rawtree := util.URLJoin(AppSubURL, "raw", "master")
  261. url := util.URLJoin(tree, "Link")
  262. otherURL := util.URLJoin(tree, "Other-Link")
  263. encodedURL := util.URLJoin(tree, "Link%3F")
  264. imgurl := util.URLJoin(rawtree, "Link.jpg")
  265. otherImgurl := util.URLJoin(rawtree, "Link+Other.jpg")
  266. encodedImgurl := util.URLJoin(rawtree, "Link+%23.jpg")
  267. notencodedImgurl := util.URLJoin(rawtree, "some", "path", "Link+#.jpg")
  268. urlWiki := util.URLJoin(AppSubURL, "wiki", "Link")
  269. otherURLWiki := util.URLJoin(AppSubURL, "wiki", "Other-Link")
  270. encodedURLWiki := util.URLJoin(AppSubURL, "wiki", "Link%3F")
  271. imgurlWiki := util.URLJoin(AppSubURL, "wiki", "raw", "Link.jpg")
  272. otherImgurlWiki := util.URLJoin(AppSubURL, "wiki", "raw", "Link+Other.jpg")
  273. encodedImgurlWiki := util.URLJoin(AppSubURL, "wiki", "raw", "Link+%23.jpg")
  274. notencodedImgurlWiki := util.URLJoin(AppSubURL, "wiki", "raw", "some", "path", "Link+#.jpg")
  275. favicon := "http://google.com/favicon.ico"
  276. test(
  277. "[[Link]]",
  278. `<p><a href="`+url+`" rel="nofollow">Link</a></p>`,
  279. `<p><a href="`+urlWiki+`" rel="nofollow">Link</a></p>`)
  280. test(
  281. "[[Link.jpg]]",
  282. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="Link.jpg" alt="Link.jpg"/></a></p>`,
  283. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" title="Link.jpg" alt="Link.jpg"/></a></p>`)
  284. test(
  285. "[["+favicon+"]]",
  286. `<p><a href="`+favicon+`" rel="nofollow"><img src="`+favicon+`" title="favicon.ico" alt="`+favicon+`"/></a></p>`,
  287. `<p><a href="`+favicon+`" rel="nofollow"><img src="`+favicon+`" title="favicon.ico" alt="`+favicon+`"/></a></p>`)
  288. test(
  289. "[[Name|Link]]",
  290. `<p><a href="`+url+`" rel="nofollow">Name</a></p>`,
  291. `<p><a href="`+urlWiki+`" rel="nofollow">Name</a></p>`)
  292. test(
  293. "[[Name|Link.jpg]]",
  294. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="Name" alt="Name"/></a></p>`,
  295. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" title="Name" alt="Name"/></a></p>`)
  296. test(
  297. "[[Name|Link.jpg|alt=AltName]]",
  298. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="AltName" alt="AltName"/></a></p>`,
  299. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" title="AltName" alt="AltName"/></a></p>`)
  300. test(
  301. "[[Name|Link.jpg|title=Title]]",
  302. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="Title" alt="Title"/></a></p>`,
  303. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" title="Title" alt="Title"/></a></p>`)
  304. test(
  305. "[[Name|Link.jpg|alt=AltName|title=Title]]",
  306. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="Title" alt="AltName"/></a></p>`,
  307. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" title="Title" alt="AltName"/></a></p>`)
  308. test(
  309. "[[Name|Link.jpg|alt=\"AltName\"|title='Title']]",
  310. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="Title" alt="AltName"/></a></p>`,
  311. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" title="Title" alt="AltName"/></a></p>`)
  312. test(
  313. "[[Name|Link Other.jpg|alt=\"AltName\"|title='Title']]",
  314. `<p><a href="`+otherImgurl+`" rel="nofollow"><img src="`+otherImgurl+`" title="Title" alt="AltName"/></a></p>`,
  315. `<p><a href="`+otherImgurlWiki+`" rel="nofollow"><img src="`+otherImgurlWiki+`" title="Title" alt="AltName"/></a></p>`)
  316. test(
  317. "[[Link]] [[Other Link]]",
  318. `<p><a href="`+url+`" rel="nofollow">Link</a> <a href="`+otherURL+`" rel="nofollow">Other Link</a></p>`,
  319. `<p><a href="`+urlWiki+`" rel="nofollow">Link</a> <a href="`+otherURLWiki+`" rel="nofollow">Other Link</a></p>`)
  320. test(
  321. "[[Link?]]",
  322. `<p><a href="`+encodedURL+`" rel="nofollow">Link?</a></p>`,
  323. `<p><a href="`+encodedURLWiki+`" rel="nofollow">Link?</a></p>`)
  324. test(
  325. "[[Link]] [[Other Link]] [[Link?]]",
  326. `<p><a href="`+url+`" rel="nofollow">Link</a> <a href="`+otherURL+`" rel="nofollow">Other Link</a> <a href="`+encodedURL+`" rel="nofollow">Link?</a></p>`,
  327. `<p><a href="`+urlWiki+`" rel="nofollow">Link</a> <a href="`+otherURLWiki+`" rel="nofollow">Other Link</a> <a href="`+encodedURLWiki+`" rel="nofollow">Link?</a></p>`)
  328. test(
  329. "[[Link #.jpg]]",
  330. `<p><a href="`+encodedImgurl+`" rel="nofollow"><img src="`+encodedImgurl+`" title="Link #.jpg" alt="Link #.jpg"/></a></p>`,
  331. `<p><a href="`+encodedImgurlWiki+`" rel="nofollow"><img src="`+encodedImgurlWiki+`" title="Link #.jpg" alt="Link #.jpg"/></a></p>`)
  332. test(
  333. "[[Name|Link #.jpg|alt=\"AltName\"|title='Title']]",
  334. `<p><a href="`+encodedImgurl+`" rel="nofollow"><img src="`+encodedImgurl+`" title="Title" alt="AltName"/></a></p>`,
  335. `<p><a href="`+encodedImgurlWiki+`" rel="nofollow"><img src="`+encodedImgurlWiki+`" title="Title" alt="AltName"/></a></p>`)
  336. test(
  337. "[[some/path/Link #.jpg]]",
  338. `<p><a href="`+notencodedImgurl+`" rel="nofollow"><img src="`+notencodedImgurl+`" title="Link #.jpg" alt="some/path/Link #.jpg"/></a></p>`,
  339. `<p><a href="`+notencodedImgurlWiki+`" rel="nofollow"><img src="`+notencodedImgurlWiki+`" title="Link #.jpg" alt="some/path/Link #.jpg"/></a></p>`)
  340. test(
  341. "<p><a href=\"https://example.org\">[[foobar]]</a></p>",
  342. `<p><a href="https://example.org" rel="nofollow">[[foobar]]</a></p>`,
  343. `<p><a href="https://example.org" rel="nofollow">[[foobar]]</a></p>`)
  344. }