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

html_test.go 12 kB

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