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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 that should *not* be turned into URL
  100. test(
  101. "www.example.com",
  102. `<p>www.example.com</p>`)
  103. test(
  104. "example.com",
  105. `<p>example.com</p>`)
  106. test(
  107. "test.example.com",
  108. `<p>test.example.com</p>`)
  109. test(
  110. "http://",
  111. `<p>http://</p>`)
  112. test(
  113. "https://",
  114. `<p>https://</p>`)
  115. test(
  116. "://",
  117. `<p>://</p>`)
  118. test(
  119. "www",
  120. `<p>www</p>`)
  121. }
  122. func TestRender_ShortLinks(t *testing.T) {
  123. setting.AppURL = AppURL
  124. setting.AppSubURL = AppSubURL
  125. tree := util.URLJoin(AppSubURL, "src", "master")
  126. test := func(input, expected, expectedWiki string) {
  127. buffer := markdown.RenderString(input, tree, nil)
  128. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
  129. buffer = markdown.RenderWiki([]byte(input), setting.AppSubURL, nil)
  130. assert.Equal(t, strings.TrimSpace(expectedWiki), strings.TrimSpace(string(buffer)))
  131. }
  132. rawtree := util.URLJoin(AppSubURL, "raw", "master")
  133. url := util.URLJoin(tree, "Link")
  134. otherURL := util.URLJoin(tree, "Other-Link")
  135. encodedURL := util.URLJoin(tree, "Link%3F")
  136. imgurl := util.URLJoin(rawtree, "Link.jpg")
  137. otherImgurl := util.URLJoin(rawtree, "Link+Other.jpg")
  138. encodedImgurl := util.URLJoin(rawtree, "Link+%23.jpg")
  139. notencodedImgurl := util.URLJoin(rawtree, "some", "path", "Link+#.jpg")
  140. urlWiki := util.URLJoin(AppSubURL, "wiki", "Link")
  141. otherURLWiki := util.URLJoin(AppSubURL, "wiki", "Other-Link")
  142. encodedURLWiki := util.URLJoin(AppSubURL, "wiki", "Link%3F")
  143. imgurlWiki := util.URLJoin(AppSubURL, "wiki", "raw", "Link.jpg")
  144. otherImgurlWiki := util.URLJoin(AppSubURL, "wiki", "raw", "Link+Other.jpg")
  145. encodedImgurlWiki := util.URLJoin(AppSubURL, "wiki", "raw", "Link+%23.jpg")
  146. notencodedImgurlWiki := util.URLJoin(AppSubURL, "wiki", "raw", "some", "path", "Link+#.jpg")
  147. favicon := "http://google.com/favicon.ico"
  148. test(
  149. "[[Link]]",
  150. `<p><a href="`+url+`" rel="nofollow">Link</a></p>`,
  151. `<p><a href="`+urlWiki+`" rel="nofollow">Link</a></p>`)
  152. test(
  153. "[[Link.jpg]]",
  154. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="Link.jpg" alt="Link.jpg"/></a></p>`,
  155. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" title="Link.jpg" alt="Link.jpg"/></a></p>`)
  156. test(
  157. "[["+favicon+"]]",
  158. `<p><a href="`+favicon+`" rel="nofollow"><img src="`+favicon+`" title="favicon.ico"/></a></p>`,
  159. `<p><a href="`+favicon+`" rel="nofollow"><img src="`+favicon+`" title="favicon.ico"/></a></p>`)
  160. test(
  161. "[[Name|Link]]",
  162. `<p><a href="`+url+`" rel="nofollow">Name</a></p>`,
  163. `<p><a href="`+urlWiki+`" rel="nofollow">Name</a></p>`)
  164. test(
  165. "[[Name|Link.jpg]]",
  166. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="Name" alt="Name"/></a></p>`,
  167. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" title="Name" alt="Name"/></a></p>`)
  168. test(
  169. "[[Name|Link.jpg|alt=AltName]]",
  170. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="AltName" alt="AltName"/></a></p>`,
  171. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" title="AltName" alt="AltName"/></a></p>`)
  172. test(
  173. "[[Name|Link.jpg|title=Title]]",
  174. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="Title" alt="Title"/></a></p>`,
  175. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" title="Title" alt="Title"/></a></p>`)
  176. test(
  177. "[[Name|Link.jpg|alt=AltName|title=Title]]",
  178. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="Title" alt="AltName"/></a></p>`,
  179. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" title="Title" alt="AltName"/></a></p>`)
  180. test(
  181. "[[Name|Link.jpg|alt=\"AltName\"|title='Title']]",
  182. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="Title" alt="AltName"/></a></p>`,
  183. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" title="Title" alt="AltName"/></a></p>`)
  184. test(
  185. "[[Name|Link Other.jpg|alt=\"AltName\"|title='Title']]",
  186. `<p><a href="`+otherImgurl+`" rel="nofollow"><img src="`+otherImgurl+`" title="Title" alt="AltName"/></a></p>`,
  187. `<p><a href="`+otherImgurlWiki+`" rel="nofollow"><img src="`+otherImgurlWiki+`" title="Title" alt="AltName"/></a></p>`)
  188. test(
  189. "[[Link]] [[Other Link]]",
  190. `<p><a href="`+url+`" rel="nofollow">Link</a> <a href="`+otherURL+`" rel="nofollow">Other Link</a></p>`,
  191. `<p><a href="`+urlWiki+`" rel="nofollow">Link</a> <a href="`+otherURLWiki+`" rel="nofollow">Other Link</a></p>`)
  192. test(
  193. "[[Link?]]",
  194. `<p><a href="`+encodedURL+`" rel="nofollow">Link?</a></p>`,
  195. `<p><a href="`+encodedURLWiki+`" rel="nofollow">Link?</a></p>`)
  196. test(
  197. "[[Link]] [[Other Link]] [[Link?]]",
  198. `<p><a href="`+url+`" rel="nofollow">Link</a> <a href="`+otherURL+`" rel="nofollow">Other Link</a> <a href="`+encodedURL+`" rel="nofollow">Link?</a></p>`,
  199. `<p><a href="`+urlWiki+`" rel="nofollow">Link</a> <a href="`+otherURLWiki+`" rel="nofollow">Other Link</a> <a href="`+encodedURLWiki+`" rel="nofollow">Link?</a></p>`)
  200. test(
  201. "[[Link #.jpg]]",
  202. `<p><a href="`+encodedImgurl+`" rel="nofollow"><img src="`+encodedImgurl+`"/></a></p>`,
  203. `<p><a href="`+encodedImgurlWiki+`" rel="nofollow"><img src="`+encodedImgurlWiki+`"/></a></p>`)
  204. test(
  205. "[[Name|Link #.jpg|alt=\"AltName\"|title='Title']]",
  206. `<p><a href="`+encodedImgurl+`" rel="nofollow"><img src="`+encodedImgurl+`" title="Title" alt="AltName"/></a></p>`,
  207. `<p><a href="`+encodedImgurlWiki+`" rel="nofollow"><img src="`+encodedImgurlWiki+`" title="Title" alt="AltName"/></a></p>`)
  208. test(
  209. "[[some/path/Link #.jpg]]",
  210. `<p><a href="`+notencodedImgurl+`" rel="nofollow"><img src="`+notencodedImgurl+`"/></a></p>`,
  211. `<p><a href="`+notencodedImgurlWiki+`" rel="nofollow"><img src="`+notencodedImgurlWiki+`"/></a></p>`)
  212. test(
  213. "<p><a href=\"https://example.org\">[[foobar]]</a></p>",
  214. `<p><a href="https://example.org" rel="nofollow">[[foobar]]</a></p>`,
  215. `<p><a href="https://example.org" rel="nofollow">[[foobar]]</a></p>`)
  216. }