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.

417 lines
15 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. defmodule LinkifyTest do
  2. use ExUnit.Case, async: true
  3. doctest Linkify
  4. test "default link" do
  5. assert Linkify.link("google.com") ==
  6. "<a href=\"http://google.com\">google.com</a>"
  7. end
  8. test "does on link existing links" do
  9. text = ~s(<a href="http://google.com">google.com</a>)
  10. assert Linkify.link(text) == text
  11. end
  12. test "all kinds of links" do
  13. text = "hello google.com https://ddg.com user@email.com irc:///mIRC"
  14. expected =
  15. "hello <a href=\"http://google.com\">google.com</a> <a href=\"https://ddg.com\">https://ddg.com</a> <a href=\"mailto:user@email.com\">user@email.com</a> <a href=\"irc:///mIRC\">irc:///mIRC</a>"
  16. assert Linkify.link(text,
  17. email: true,
  18. extra: true
  19. ) == expected
  20. end
  21. test "class attribute" do
  22. assert Linkify.link("google.com", class: "linkified") ==
  23. "<a href=\"http://google.com\" class=\"linkified\">google.com</a>"
  24. end
  25. test "rel attribute" do
  26. assert Linkify.link("google.com", rel: "noopener noreferrer") ==
  27. "<a href=\"http://google.com\" rel=\"noopener noreferrer\">google.com</a>"
  28. end
  29. test "rel as function" do
  30. text = "google.com"
  31. expected = "<a href=\"http://google.com\" rel=\"com\">google.com</a>"
  32. custom_rel = fn url ->
  33. url |> String.split(".") |> List.last()
  34. end
  35. assert Linkify.link(text, rel: custom_rel) == expected
  36. text = "google.com"
  37. expected = "<a href=\"http://google.com\">google.com</a>"
  38. custom_rel = fn _ -> nil end
  39. assert Linkify.link(text, rel: custom_rel) == expected
  40. end
  41. test "link_map/2" do
  42. assert Linkify.link_map("google.com", []) ==
  43. {"<a href=\"http://google.com\">google.com</a>", []}
  44. end
  45. describe "custom handlers" do
  46. test "mentions handler" do
  47. text = "hello @user, @valid_user and @invalid_user"
  48. valid_users = ["user", "valid_user"]
  49. handler = fn "@" <> user = mention, buffer, _opts, acc ->
  50. if Enum.member?(valid_users, user) do
  51. link = ~s(<a href="https://example.com/user/#{user}" data-user="#{user}">#{mention}</a>)
  52. {link, %{acc | mentions: MapSet.put(acc.mentions, {mention, user})}}
  53. else
  54. {buffer, acc}
  55. end
  56. end
  57. {result_text, %{mentions: mentions}} =
  58. Linkify.link_map(text, %{mentions: MapSet.new()},
  59. mention: true,
  60. mention_handler: handler
  61. )
  62. assert result_text ==
  63. "hello <a href=\"https://example.com/user/user\" data-user=\"user\">@user</a>, <a href=\"https://example.com/user/valid_user\" data-user=\"valid_user\">@valid_user</a> and @invalid_user"
  64. assert mentions |> MapSet.to_list() |> Enum.map(&elem(&1, 1)) == valid_users
  65. end
  66. test "hashtags handler" do
  67. text = "#hello #world"
  68. handler = fn hashtag, buffer, opts, acc ->
  69. link = Linkify.Builder.create_hashtag_link(hashtag, buffer, opts)
  70. {link, %{acc | tags: MapSet.put(acc.tags, hashtag)}}
  71. end
  72. {result_text, %{tags: tags}} =
  73. Linkify.link_map(text, %{tags: MapSet.new()},
  74. hashtag: true,
  75. hashtag_handler: handler,
  76. hashtag_prefix: "https://example.com/user/",
  77. rel: false
  78. )
  79. assert result_text ==
  80. "<a href=\"https://example.com/user/hello\">#hello</a> <a href=\"https://example.com/user/world\">#world</a>"
  81. assert MapSet.to_list(tags) == ["#hello", "#world"]
  82. end
  83. test "mention handler and hashtag prefix" do
  84. text =
  85. "Hello again, @user.&lt;script&gt;&lt;/script&gt;\nThis is on another :moominmamma: line. #2hu #epic #phantasmagoric"
  86. handler = fn "@" <> user = mention, _, _, _ ->
  87. ~s(<span class="h-card"><a href="#/user/#{user}">@<span>#{mention}</span></a></span>)
  88. end
  89. expected =
  90. ~s(Hello again, <span class="h-card"><a href="#/user/user">@<span>@user</span></a></span>.&lt;script&gt;&lt;/script&gt;\nThis is on another :moominmamma: line. <a href="/tag/2hu" target="_blank">#2hu</a> <a href="/tag/epic" target="_blank">#epic</a> <a href="/tag/phantasmagoric" target="_blank">#phantasmagoric</a>)
  91. assert Linkify.link(text,
  92. mention: true,
  93. mention_handler: handler,
  94. hashtag: true,
  95. hashtag_prefix: "/tag/",
  96. new_window: true
  97. ) == expected
  98. end
  99. end
  100. describe "mentions" do
  101. test "simple mentions" do
  102. expected =
  103. ~s{hello <a href="https://example.com/user/user" target="_blank">@user</a> and <a href="https://example.com/user/anotherUser" target="_blank">@anotherUser</a>.}
  104. assert Linkify.link("hello @user and @anotherUser.",
  105. mention: true,
  106. mention_prefix: "https://example.com/user/",
  107. new_window: true
  108. ) == expected
  109. end
  110. test "mentions inside html tags" do
  111. text =
  112. "<p><strong>hello world</strong></p>\n<p><`em>another @user__test and @user__test google.com paragraph</em></p>\n"
  113. expected =
  114. "<p><strong>hello world</strong></p>\n<p><`em>another <a href=\"u/user__test\">@user__test</a> and <a href=\"u/user__test\">@user__test</a> <a href=\"http://google.com\">google.com</a> paragraph</em></p>\n"
  115. assert Linkify.link(text, mention: true, mention_prefix: "u/") == expected
  116. end
  117. test "metion @user@example.com" do
  118. text = "hey @user@example.com"
  119. expected =
  120. "hey <a href=\"https://example.com/user/user@example.com\" target=\"_blank\">@user@example.com</a>"
  121. assert Linkify.link(text,
  122. mention: true,
  123. mention_prefix: "https://example.com/user/",
  124. new_window: true
  125. ) == expected
  126. end
  127. end
  128. describe "hashtag links" do
  129. test "hashtag" do
  130. expected =
  131. " one <a href=\"https://example.com/tag/2two\" target=\"_blank\">#2two</a> three <a href=\"https://example.com/tag/four\" target=\"_blank\">#four</a>."
  132. assert Linkify.link(" one #2two three #four.",
  133. hashtag: true,
  134. hashtag_prefix: "https://example.com/tag/",
  135. new_window: true
  136. ) == expected
  137. end
  138. test "must have non-numbers" do
  139. expected = "<a href=\"/t/1ok\">#1ok</a> #42 #7"
  140. assert Linkify.link("#1ok #42 #7",
  141. hashtag: true,
  142. hashtag_prefix: "/t/",
  143. rel: false
  144. ) == expected
  145. end
  146. test "support French" do
  147. text = "#administrateur·rice·s #ingénieur·e·s"
  148. expected =
  149. "<a href=\"/t/administrateur·rice·s\">#administrateur·rice·s</a> <a href=\"/t/ingénieur·e·s\">#ingénieur·e·s</a>"
  150. assert Linkify.link(text,
  151. hashtag: true,
  152. hashtag_prefix: "/t/",
  153. rel: false
  154. ) == expected
  155. end
  156. test "support Telugu" do
  157. text = "#చక్రం #కకకకక్ #కకకకాక #కకకక్రకకకక"
  158. expected =
  159. "<a href=\"/t/చక్రం\">#చక్రం</a> <a href=\"/t/కకకకక్\">#కకకకక్</a> <a href=\"/t/కకకకాక\">#కకకకాక</a> <a href=\"/t/కకకక్రకకకక\">#కకకక్రకకకక</a>"
  160. assert Linkify.link(text,
  161. hashtag: true,
  162. hashtag_prefix: "/t/",
  163. rel: false
  164. ) == expected
  165. end
  166. test "do not turn urls with hashes into hashtags" do
  167. text = "google.com#test #test google.com/#test #tag"
  168. expected =
  169. "<a href=\"http://google.com#test\">google.com#test</a> <a href=\"https://example.com/tag/test\">#test</a> <a href=\"http://google.com/#test\">google.com/#test</a> <a href=\"https://example.com/tag/tag\">#tag</a>"
  170. assert Linkify.link(text,
  171. hashtag: true,
  172. rel: false,
  173. hashtag_prefix: "https://example.com/tag/"
  174. ) == expected
  175. end
  176. test "works with non-latin characters" do
  177. text = "#漢字 ##тест #ทดสอบ"
  178. expected =
  179. "<a href=\"https://example.com/tag/漢字\">#漢字</a> <a href=\"https://example.com/tag/は\">#は</a> <a href=\"https://example.com/tag/тест\">#тест</a> <a href=\"https://example.com/tag/ทดสอบ\">#ทดสอบ</a>"
  180. assert Linkify.link(text,
  181. rel: false,
  182. hashtag: true,
  183. hashtag_prefix: "https://example.com/tag/"
  184. ) == expected
  185. end
  186. end
  187. describe "links" do
  188. test "turning urls into links" do
  189. text = "Hey, check out http://www.youtube.com/watch?v=8Zg1-TufF%20zY?x=1&y=2#blabla ."
  190. expected =
  191. "Hey, check out <a href=\"http://www.youtube.com/watch?v=8Zg1-TufF%20zY?x=1&y=2#blabla\" target=\"_blank\">http://www.youtube.com/watch?v=8Zg1-TufF%20zY?x=1&y=2#blabla</a> ."
  192. assert Linkify.link(text, new_window: true) == expected
  193. # no scheme
  194. text = "Hey, check out www.youtube.com/watch?v=8Zg1-TufF%20zY?x=1&y=2#blabla ."
  195. expected =
  196. "Hey, check out <a href=\"http://www.youtube.com/watch?v=8Zg1-TufF%20zY?x=1&y=2#blabla\" target=\"_blank\">www.youtube.com/watch?v=8Zg1-TufF%20zY?x=1&y=2#blabla</a> ."
  197. assert Linkify.link(text, new_window: true) == expected
  198. end
  199. test "turn urls with schema into urls" do
  200. text = "📌https://google.com"
  201. expected = "📌<a href=\"https://google.com\">https://google.com</a>"
  202. assert Linkify.link(text, rel: false) == expected
  203. end
  204. test "skip prefix" do
  205. assert Linkify.link("http://google.com", strip_prefix: true) ==
  206. "<a href=\"http://google.com\">google.com</a>"
  207. assert Linkify.link("http://www.google.com", strip_prefix: true) ==
  208. "<a href=\"http://www.google.com\">google.com</a>"
  209. end
  210. test "hostname/@user" do
  211. text = "https://example.com/@user"
  212. expected =
  213. "<a href=\"https://example.com/@user\" target=\"_blank\">https://example.com/@user</a>"
  214. assert Linkify.link(text, new_window: true) == expected
  215. text = "https://example.com:4000/@user"
  216. expected =
  217. "<a href=\"https://example.com:4000/@user\" target=\"_blank\">https://example.com:4000/@user</a>"
  218. assert Linkify.link(text, new_window: true) == expected
  219. text = "https://example.com:4000/@user"
  220. expected =
  221. "<a href=\"https://example.com:4000/@user\" target=\"_blank\">https://example.com:4000/@user</a>"
  222. assert Linkify.link(text, new_window: true) == expected
  223. text = "@username"
  224. expected = "@username"
  225. assert Linkify.link(text, new_window: true) == expected
  226. text = "http://www.cs.vu.nl/~ast/intel/"
  227. expected = "<a href=\"http://www.cs.vu.nl/~ast/intel/\">http://www.cs.vu.nl/~ast/intel/</a>"
  228. assert Linkify.link(text) == expected
  229. text = "https://forum.zdoom.org/viewtopic.php?f=44&t=57087"
  230. expected =
  231. "<a href=\"https://forum.zdoom.org/viewtopic.php?f=44&t=57087\">https://forum.zdoom.org/viewtopic.php?f=44&t=57087</a>"
  232. assert Linkify.link(text) == expected
  233. text = "https://en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul"
  234. expected =
  235. "<a href=\"https://en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul\">https://en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul</a>"
  236. assert Linkify.link(text) == expected
  237. text = "https://en.wikipedia.org/wiki/Duff's_device"
  238. expected =
  239. "<a href=\"https://en.wikipedia.org/wiki/Duff's_device\">https://en.wikipedia.org/wiki/Duff's_device</a>"
  240. assert Linkify.link(text) == expected
  241. end
  242. end
  243. describe "non http links" do
  244. test "xmpp" do
  245. text = "xmpp:user@example.com"
  246. expected = "<a href=\"xmpp:user@example.com\">xmpp:user@example.com</a>"
  247. assert Linkify.link(text, extra: true) == expected
  248. end
  249. test "email" do
  250. text = "user@example.com"
  251. expected = "<a href=\"mailto:user@example.com\">user@example.com</a>"
  252. assert Linkify.link(text, email: true) == expected
  253. end
  254. test "magnet" do
  255. text =
  256. "magnet:?xt=urn:btih:a4104a9d2f5615601c429fe8bab8177c47c05c84&dn=ubuntu-18.04.1.0-live-server-amd64.iso&tr=http%3A%2F%2Ftorrent.ubuntu.com%3A6969%2Fannounce&tr=http%3A%2F%2Fipv6.torrent.ubuntu.com%3A6969%2Fannounce"
  257. expected =
  258. "<a href=\"magnet:?xt=urn:btih:a4104a9d2f5615601c429fe8bab8177c47c05c84&dn=ubuntu-18.04.1.0-live-server-amd64.iso&tr=http%3A%2F%2Ftorrent.ubuntu.com%3A6969%2Fannounce&tr=http%3A%2F%2Fipv6.torrent.ubuntu.com%3A6969%2Fannounce\">magnet:?xt=urn:btih:a4104a9d2f5615601c429fe8bab8177c47c05c84&dn=ubuntu-18.04.1.0-live-server-amd64.iso&tr=http%3A%2F%2Ftorrent.ubuntu.com%3A6969%2Fannounce&tr=http%3A%2F%2Fipv6.torrent.ubuntu.com%3A6969%2Fannounce</a>"
  259. assert Linkify.link(text, extra: true) == expected
  260. end
  261. test "dweb" do
  262. text =
  263. "dweb://584faa05d394190ab1a3f0240607f9bf2b7e2bd9968830a11cf77db0cea36a21+v1.0.0/path/to/file.txt"
  264. expected =
  265. "<a href=\"dweb://584faa05d394190ab1a3f0240607f9bf2b7e2bd9968830a11cf77db0cea36a21+v1.0.0/path/to/file.txt\">dweb://584faa05d394190ab1a3f0240607f9bf2b7e2bd9968830a11cf77db0cea36a21+v1.0.0/path/to/file.txt</a>"
  266. assert Linkify.link(text, extra: true) == expected
  267. end
  268. end
  269. describe "TLDs" do
  270. test "parse with scheme" do
  271. text = "https://google.com"
  272. expected = "<a href=\"https://google.com\">https://google.com</a>"
  273. assert Linkify.link(text) == expected
  274. end
  275. test "only existing TLDs with scheme" do
  276. text = "this url https://google.foobar.blah11blah/ has invalid TLD"
  277. expected = "this url https://google.foobar.blah11blah/ has invalid TLD"
  278. assert Linkify.link(text) == expected
  279. text = "this url https://google.foobar.com/ has valid TLD"
  280. expected =
  281. "this url <a href=\"https://google.foobar.com/\">https://google.foobar.com/</a> has valid TLD"
  282. assert Linkify.link(text) == expected
  283. end
  284. test "only existing TLDs without scheme" do
  285. text = "this url google.foobar.blah11blah/ has invalid TLD"
  286. assert Linkify.link(text) == text
  287. text = "this url google.foobar.com/ has valid TLD"
  288. expected =
  289. "this url <a href=\"http://google.foobar.com/\">google.foobar.com/</a> has valid TLD"
  290. assert Linkify.link(text) == expected
  291. end
  292. test "only existing TLDs with and without scheme" do
  293. text = "this url http://google.foobar.com/ has valid TLD"
  294. expected =
  295. "this url <a href=\"http://google.foobar.com/\">http://google.foobar.com/</a> has valid TLD"
  296. assert Linkify.link(text) == expected
  297. text = "this url google.foobar.com/ has valid TLD"
  298. expected =
  299. "this url <a href=\"http://google.foobar.com/\">google.foobar.com/</a> has valid TLD"
  300. assert Linkify.link(text) == expected
  301. end
  302. end
  303. end