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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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/markup"
  9. "code.gitea.io/gitea/modules/markup/markdown"
  10. "code.gitea.io/gitea/modules/setting"
  11. "code.gitea.io/gitea/modules/util"
  12. "github.com/stretchr/testify/assert"
  13. )
  14. var localMetas = map[string]string{
  15. "user": "gogits",
  16. "repo": "gogs",
  17. }
  18. func TestRender_Commits(t *testing.T) {
  19. setting.AppURL = AppURL
  20. setting.AppSubURL = AppSubURL
  21. test := func(input, expected string) {
  22. buffer := RenderString(".md", input, setting.AppSubURL, localMetas)
  23. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
  24. }
  25. var sha = "b6dd6210eaebc915fd5be5579c58cce4da2e2579"
  26. var commit = util.URLJoin(AppSubURL, "commit", sha)
  27. var subtree = util.URLJoin(commit, "src")
  28. var tree = strings.Replace(subtree, "/commit/", "/tree/", -1)
  29. test(sha, `<p><a href="`+commit+`" rel="nofollow"><code>b6dd6210ea</code></a></p>`)
  30. test(sha[:7], `<p><a href="`+commit[:len(commit)-(40-7)]+`" rel="nofollow"><code>b6dd621</code></a></p>`)
  31. test(sha[:39], `<p><a href="`+commit[:len(commit)-(40-39)]+`" rel="nofollow"><code>b6dd6210ea</code></a></p>`)
  32. test(commit, `<p><a href="`+commit+`" rel="nofollow"><code>b6dd6210ea</code></a></p>`)
  33. test(tree, `<p><a href="`+tree+`" rel="nofollow"><code>b6dd6210ea/src</code></a></p>`)
  34. test("commit "+sha, `<p>commit <a href="`+commit+`" rel="nofollow"><code>b6dd6210ea</code></a></p>`)
  35. test("/home/gitea/"+sha, "<p>/home/gitea/"+sha+"</p>")
  36. }
  37. func TestRender_CrossReferences(t *testing.T) {
  38. setting.AppURL = AppURL
  39. setting.AppSubURL = AppSubURL
  40. test := func(input, expected string) {
  41. buffer := RenderString("a.md", input, setting.AppSubURL, localMetas)
  42. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
  43. }
  44. test(
  45. "gogits/gogs#12345",
  46. `<p><a href="`+util.URLJoin(AppURL, "gogits", "gogs", "issues", "12345")+`" rel="nofollow">gogits/gogs#12345</a></p>`)
  47. test(
  48. "go-gitea/gitea#12345",
  49. `<p><a href="`+util.URLJoin(AppURL, "go-gitea", "gitea", "issues", "12345")+`" rel="nofollow">go-gitea/gitea#12345</a></p>`)
  50. test(
  51. "/home/gitea/go-gitea/gitea#12345",
  52. `<p>/home/gitea/go-gitea/gitea#12345</p>`)
  53. }
  54. func TestMisc_IsSameDomain(t *testing.T) {
  55. setting.AppURL = AppURL
  56. setting.AppSubURL = AppSubURL
  57. var sha = "b6dd6210eaebc915fd5be5579c58cce4da2e2579"
  58. var commit = util.URLJoin(AppSubURL, "commit", sha)
  59. assert.True(t, IsSameDomain(commit))
  60. assert.False(t, IsSameDomain("http://google.com/ncr"))
  61. assert.False(t, IsSameDomain("favicon.ico"))
  62. }
  63. func TestRender_links(t *testing.T) {
  64. setting.AppURL = AppURL
  65. setting.AppSubURL = AppSubURL
  66. test := func(input, expected string) {
  67. buffer := RenderString("a.md", input, setting.AppSubURL, nil)
  68. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
  69. }
  70. // Text that should be turned into URL
  71. test(
  72. "https://www.example.com",
  73. `<p><a href="https://www.example.com" rel="nofollow">https://www.example.com</a></p>`)
  74. test(
  75. "http://www.example.com",
  76. `<p><a href="http://www.example.com" rel="nofollow">http://www.example.com</a></p>`)
  77. test(
  78. "https://example.com",
  79. `<p><a href="https://example.com" rel="nofollow">https://example.com</a></p>`)
  80. test(
  81. "http://example.com",
  82. `<p><a href="http://example.com" rel="nofollow">http://example.com</a></p>`)
  83. test(
  84. "http://foo.com/blah_blah",
  85. `<p><a href="http://foo.com/blah_blah" rel="nofollow">http://foo.com/blah_blah</a></p>`)
  86. test(
  87. "http://foo.com/blah_blah/",
  88. `<p><a href="http://foo.com/blah_blah/" rel="nofollow">http://foo.com/blah_blah/</a></p>`)
  89. test(
  90. "http://www.example.com/wpstyle/?p=364",
  91. `<p><a href="http://www.example.com/wpstyle/?p=364" rel="nofollow">http://www.example.com/wpstyle/?p=364</a></p>`)
  92. test(
  93. "https://www.example.com/foo/?bar=baz&inga=42&quux",
  94. `<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>`)
  95. test(
  96. "http://142.42.1.1/",
  97. `<p><a href="http://142.42.1.1/" rel="nofollow">http://142.42.1.1/</a></p>`)
  98. test(
  99. "https://github.com/go-gitea/gitea/?p=aaa/bbb.html#ccc-ddd",
  100. `<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>`)
  101. test(
  102. "https://en.wikipedia.org/wiki/URL_(disambiguation)",
  103. `<p><a href="https://en.wikipedia.org/wiki/URL_(disambiguation)" rel="nofollow">https://en.wikipedia.org/wiki/URL_(disambiguation)</a></p>`)
  104. test(
  105. "https://foo_bar.example.com/",
  106. `<p><a href="https://foo_bar.example.com/" rel="nofollow">https://foo_bar.example.com/</a></p>`)
  107. test(
  108. "https://stackoverflow.com/questions/2896191/what-is-go-used-fore",
  109. `<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>`)
  110. test(
  111. "https://username:password@gitea.com",
  112. `<p><a href="https://username:password@gitea.com" rel="nofollow">https://username:password@gitea.com</a></p>`)
  113. // Test that should *not* be turned into URL
  114. test(
  115. "www.example.com",
  116. `<p>www.example.com</p>`)
  117. test(
  118. "example.com",
  119. `<p>example.com</p>`)
  120. test(
  121. "test.example.com",
  122. `<p>test.example.com</p>`)
  123. test(
  124. "http://",
  125. `<p>http://</p>`)
  126. test(
  127. "https://",
  128. `<p>https://</p>`)
  129. test(
  130. "://",
  131. `<p>://</p>`)
  132. test(
  133. "www",
  134. `<p>www</p>`)
  135. }
  136. func TestRender_email(t *testing.T) {
  137. setting.AppURL = AppURL
  138. setting.AppSubURL = AppSubURL
  139. test := func(input, expected string) {
  140. buffer := RenderString("a.md", input, setting.AppSubURL, nil)
  141. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
  142. }
  143. // Text that should be turned into email link
  144. test(
  145. "info@gitea.com",
  146. `<p><a href="mailto:info@gitea.com" rel="nofollow">info@gitea.com</a></p>`)
  147. test(
  148. "(info@gitea.com)",
  149. `<p>(<a href="mailto:info@gitea.com" rel="nofollow">info@gitea.com</a>)</p>`)
  150. test(
  151. "[info@gitea.com]",
  152. `<p>[<a href="mailto:info@gitea.com" rel="nofollow">info@gitea.com</a>]</p>`)
  153. test(
  154. "info@gitea.com.",
  155. `<p><a href="mailto:info@gitea.com" rel="nofollow">info@gitea.com</a>.</p>`)
  156. test(
  157. "firstname+lastname@gitea.com",
  158. `<p><a href="mailto:firstname+lastname@gitea.com" rel="nofollow">firstname+lastname@gitea.com</a></p>`)
  159. test(
  160. "send email to info@gitea.co.uk.",
  161. `<p>send email to <a href="mailto:info@gitea.co.uk" rel="nofollow">info@gitea.co.uk</a>.</p>`)
  162. // Test that should *not* be turned into email links
  163. test(
  164. "\"info@gitea.com\"",
  165. `<p>“info@gitea.com”</p>`)
  166. test(
  167. "/home/gitea/mailstore/info@gitea/com",
  168. `<p>/home/gitea/mailstore/info@gitea/com</p>`)
  169. test(
  170. "git@try.gitea.io:go-gitea/gitea.git",
  171. `<p>git@try.gitea.io:go-gitea/gitea.git</p>`)
  172. test(
  173. "gitea@3",
  174. `<p>gitea@3</p>`)
  175. test(
  176. "gitea@gmail.c",
  177. `<p>gitea@gmail.c</p>`)
  178. test(
  179. "email@domain@domain.com",
  180. `<p>email@domain@domain.com</p>`)
  181. test(
  182. "email@domain..com",
  183. `<p>email@domain..com</p>`)
  184. }
  185. func TestRender_ShortLinks(t *testing.T) {
  186. setting.AppURL = AppURL
  187. setting.AppSubURL = AppSubURL
  188. tree := util.URLJoin(AppSubURL, "src", "master")
  189. test := func(input, expected, expectedWiki string) {
  190. buffer := markdown.RenderString(input, tree, nil)
  191. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
  192. buffer = markdown.RenderWiki([]byte(input), setting.AppSubURL, localMetas)
  193. assert.Equal(t, strings.TrimSpace(expectedWiki), strings.TrimSpace(string(buffer)))
  194. }
  195. rawtree := util.URLJoin(AppSubURL, "raw", "master")
  196. url := util.URLJoin(tree, "Link")
  197. otherURL := util.URLJoin(tree, "Other-Link")
  198. encodedURL := util.URLJoin(tree, "Link%3F")
  199. imgurl := util.URLJoin(rawtree, "Link.jpg")
  200. otherImgurl := util.URLJoin(rawtree, "Link+Other.jpg")
  201. encodedImgurl := util.URLJoin(rawtree, "Link+%23.jpg")
  202. notencodedImgurl := util.URLJoin(rawtree, "some", "path", "Link+#.jpg")
  203. urlWiki := util.URLJoin(AppSubURL, "wiki", "Link")
  204. otherURLWiki := util.URLJoin(AppSubURL, "wiki", "Other-Link")
  205. encodedURLWiki := util.URLJoin(AppSubURL, "wiki", "Link%3F")
  206. imgurlWiki := util.URLJoin(AppSubURL, "wiki", "raw", "Link.jpg")
  207. otherImgurlWiki := util.URLJoin(AppSubURL, "wiki", "raw", "Link+Other.jpg")
  208. encodedImgurlWiki := util.URLJoin(AppSubURL, "wiki", "raw", "Link+%23.jpg")
  209. notencodedImgurlWiki := util.URLJoin(AppSubURL, "wiki", "raw", "some", "path", "Link+#.jpg")
  210. favicon := "http://google.com/favicon.ico"
  211. test(
  212. "[[Link]]",
  213. `<p><a href="`+url+`" rel="nofollow">Link</a></p>`,
  214. `<p><a href="`+urlWiki+`" rel="nofollow">Link</a></p>`)
  215. test(
  216. "[[Link.jpg]]",
  217. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="Link.jpg" alt="Link.jpg"/></a></p>`,
  218. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" title="Link.jpg" alt="Link.jpg"/></a></p>`)
  219. test(
  220. "[["+favicon+"]]",
  221. `<p><a href="`+favicon+`" rel="nofollow"><img src="`+favicon+`" title="favicon.ico"/></a></p>`,
  222. `<p><a href="`+favicon+`" rel="nofollow"><img src="`+favicon+`" title="favicon.ico"/></a></p>`)
  223. test(
  224. "[[Name|Link]]",
  225. `<p><a href="`+url+`" rel="nofollow">Name</a></p>`,
  226. `<p><a href="`+urlWiki+`" rel="nofollow">Name</a></p>`)
  227. test(
  228. "[[Name|Link.jpg]]",
  229. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="Name" alt="Name"/></a></p>`,
  230. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" title="Name" alt="Name"/></a></p>`)
  231. test(
  232. "[[Name|Link.jpg|alt=AltName]]",
  233. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="AltName" alt="AltName"/></a></p>`,
  234. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" title="AltName" alt="AltName"/></a></p>`)
  235. test(
  236. "[[Name|Link.jpg|title=Title]]",
  237. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="Title" alt="Title"/></a></p>`,
  238. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" title="Title" alt="Title"/></a></p>`)
  239. test(
  240. "[[Name|Link.jpg|alt=AltName|title=Title]]",
  241. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="Title" alt="AltName"/></a></p>`,
  242. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" title="Title" alt="AltName"/></a></p>`)
  243. test(
  244. "[[Name|Link.jpg|alt=\"AltName\"|title='Title']]",
  245. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="Title" alt="AltName"/></a></p>`,
  246. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" title="Title" alt="AltName"/></a></p>`)
  247. test(
  248. "[[Name|Link Other.jpg|alt=\"AltName\"|title='Title']]",
  249. `<p><a href="`+otherImgurl+`" rel="nofollow"><img src="`+otherImgurl+`" title="Title" alt="AltName"/></a></p>`,
  250. `<p><a href="`+otherImgurlWiki+`" rel="nofollow"><img src="`+otherImgurlWiki+`" title="Title" alt="AltName"/></a></p>`)
  251. test(
  252. "[[Link]] [[Other Link]]",
  253. `<p><a href="`+url+`" rel="nofollow">Link</a> <a href="`+otherURL+`" rel="nofollow">Other Link</a></p>`,
  254. `<p><a href="`+urlWiki+`" rel="nofollow">Link</a> <a href="`+otherURLWiki+`" rel="nofollow">Other Link</a></p>`)
  255. test(
  256. "[[Link?]]",
  257. `<p><a href="`+encodedURL+`" rel="nofollow">Link?</a></p>`,
  258. `<p><a href="`+encodedURLWiki+`" rel="nofollow">Link?</a></p>`)
  259. test(
  260. "[[Link]] [[Other Link]] [[Link?]]",
  261. `<p><a href="`+url+`" rel="nofollow">Link</a> <a href="`+otherURL+`" rel="nofollow">Other Link</a> <a href="`+encodedURL+`" rel="nofollow">Link?</a></p>`,
  262. `<p><a href="`+urlWiki+`" rel="nofollow">Link</a> <a href="`+otherURLWiki+`" rel="nofollow">Other Link</a> <a href="`+encodedURLWiki+`" rel="nofollow">Link?</a></p>`)
  263. test(
  264. "[[Link #.jpg]]",
  265. `<p><a href="`+encodedImgurl+`" rel="nofollow"><img src="`+encodedImgurl+`"/></a></p>`,
  266. `<p><a href="`+encodedImgurlWiki+`" rel="nofollow"><img src="`+encodedImgurlWiki+`"/></a></p>`)
  267. test(
  268. "[[Name|Link #.jpg|alt=\"AltName\"|title='Title']]",
  269. `<p><a href="`+encodedImgurl+`" rel="nofollow"><img src="`+encodedImgurl+`" title="Title" alt="AltName"/></a></p>`,
  270. `<p><a href="`+encodedImgurlWiki+`" rel="nofollow"><img src="`+encodedImgurlWiki+`" title="Title" alt="AltName"/></a></p>`)
  271. test(
  272. "[[some/path/Link #.jpg]]",
  273. `<p><a href="`+notencodedImgurl+`" rel="nofollow"><img src="`+notencodedImgurl+`"/></a></p>`,
  274. `<p><a href="`+notencodedImgurlWiki+`" rel="nofollow"><img src="`+notencodedImgurlWiki+`"/></a></p>`)
  275. test(
  276. "<p><a href=\"https://example.org\">[[foobar]]</a></p>",
  277. `<p><a href="https://example.org" rel="nofollow">[[foobar]]</a></p>`,
  278. `<p><a href="https://example.org" rel="nofollow">[[foobar]]</a></p>`)
  279. }