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 11 kB

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