logo

auto_linker

AutoLinker-shim, based on https://git.pleroma.social/pleroma/auto_linker git clone https://hacktivis.me/git/auto_linker.git

linkify_test.exs (25545B)


  1. defmodule LinkifyTest do
  2. use ExUnit.Case, async: true
  3. doctest Linkify
  4. test "default link" do
  5. assert Linkify.link("http://google.com") ==
  6. "<a href=\"http://google.com\">http://google.com</a>"
  7. end
  8. test "default link iodata" do
  9. assert Linkify.link_to_iodata("http://google.com") ==
  10. [["<a ", "href=\"http://google.com\"", ">", "http://google.com", "</a>"]]
  11. end
  12. test "default link safe iodata" do
  13. assert Linkify.link_safe("http://google.com") ==
  14. [
  15. [
  16. {:safe, ["<a ", "href=\"http://google.com\"", ">"]},
  17. "http://google.com",
  18. {:safe, "</a>"}
  19. ]
  20. ]
  21. end
  22. test "does on link existing links" do
  23. text = ~s(<a href="http://google.com">google.com</a>)
  24. assert Linkify.link(text) == text
  25. end
  26. test "all kinds of links" do
  27. text = "hello google.com https://ddg.com user@email.com irc:///mIRC"
  28. expected =
  29. "hello google.com <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>"
  30. assert Linkify.link(text,
  31. email: true,
  32. extra_prefixes: ["irc://"]
  33. ) == expected
  34. end
  35. test "all kinds of links iodata" do
  36. text = "hello http://google.com https://ddg.com user@email.com irc:///mIRC"
  37. expected = [
  38. "hello",
  39. " ",
  40. ["<a ", "href=\"http://google.com\"", ">", "http://google.com", "</a>"],
  41. " ",
  42. ["<a ", "href=\"https://ddg.com\"", ">", "https://ddg.com", "</a>"],
  43. " ",
  44. ["<a ", "href=\"mailto:user@email.com\"", ">", "user@email.com", "</a>"],
  45. " ",
  46. "irc:///mIRC"
  47. ]
  48. assert Linkify.link_to_iodata(text,
  49. email: true,
  50. extra: true
  51. ) == expected
  52. end
  53. test "class attribute" do
  54. assert Linkify.link("http://google.com", class: "linkified") ==
  55. "<a href=\"http://google.com\" class=\"linkified\">http://google.com</a>"
  56. end
  57. test "class attribute iodata" do
  58. assert Linkify.link_to_iodata("http://google.com", class: "linkified") ==
  59. [
  60. [
  61. "<a ",
  62. "href=\"http://google.com\" class=\"linkified\"",
  63. ">",
  64. "http://google.com",
  65. "</a>"
  66. ]
  67. ]
  68. end
  69. test "rel attribute" do
  70. assert Linkify.link("http://google.com", rel: "noopener noreferrer") ==
  71. "<a href=\"http://google.com\" rel=\"noopener noreferrer\">http://google.com</a>"
  72. end
  73. test "rel attribute iodata" do
  74. assert Linkify.link_to_iodata("http://google.com", rel: "noopener noreferrer") ==
  75. [
  76. [
  77. "<a ",
  78. "href=\"http://google.com\" rel=\"noopener noreferrer\"",
  79. ">",
  80. "http://google.com",
  81. "</a>"
  82. ]
  83. ]
  84. end
  85. test "rel as function" do
  86. text = "http://google.com"
  87. expected = "<a href=\"http://google.com\" rel=\"com\">http://google.com</a>"
  88. custom_rel = fn url ->
  89. url |> String.split(".") |> List.last()
  90. end
  91. assert Linkify.link(text, rel: custom_rel) == expected
  92. expected = "<a href=\"http://google.com\">http://google.com</a>"
  93. custom_rel = fn _ -> nil end
  94. assert Linkify.link(text, rel: custom_rel) == expected
  95. end
  96. test "strip parens" do
  97. assert Linkify.link("(http://google.com)") ==
  98. "(<a href=\"http://google.com\">http://google.com</a>)"
  99. end
  100. test "strip parens iodata" do
  101. assert Linkify.link_to_iodata("(http://google.com)") ==
  102. [["(", ["<a ", "href=\"http://google.com\"", ">", "http://google.com", "</a>"], ")"]]
  103. end
  104. test "link_map/2" do
  105. assert Linkify.link_map("http://google.com", []) ==
  106. {"<a href=\"http://google.com\">http://google.com</a>", []}
  107. end
  108. describe "custom handlers" do
  109. test "mentions handler" do
  110. text = "hello @user, @valid_user and @invalid_user"
  111. valid_users = ["user", "valid_user"]
  112. handler = fn "@" <> user = mention, buffer, _opts, acc ->
  113. if Enum.member?(valid_users, user) do
  114. link = ~s(<a href="https://example.com/user/#{user}" data-user="#{user}">#{mention}</a>)
  115. {link, %{acc | mentions: MapSet.put(acc.mentions, {mention, user})}}
  116. else
  117. {buffer, acc}
  118. end
  119. end
  120. {result_text, %{mentions: mentions}} =
  121. Linkify.link_map(text, %{mentions: MapSet.new()},
  122. mention: true,
  123. mention_handler: handler
  124. )
  125. assert result_text ==
  126. "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"
  127. assert mentions |> MapSet.to_list() |> Enum.map(&elem(&1, 1)) == valid_users
  128. end
  129. test "hashtags handler" do
  130. text = "#hello #world"
  131. handler = fn hashtag, buffer, opts, acc ->
  132. link = Linkify.Builder.create_hashtag_link(hashtag, buffer, opts)
  133. {link, %{acc | tags: MapSet.put(acc.tags, hashtag)}}
  134. end
  135. {result_text, %{tags: tags}} =
  136. Linkify.link_map(text, %{tags: MapSet.new()},
  137. hashtag: true,
  138. hashtag_handler: handler,
  139. hashtag_prefix: "https://example.com/user/",
  140. rel: false
  141. )
  142. assert result_text ==
  143. "<a href=\"https://example.com/user/hello\">#hello</a> <a href=\"https://example.com/user/world\">#world</a>"
  144. assert MapSet.to_list(tags) == ["#hello", "#world"]
  145. text = "#cofe <br><a href=\"https://pleroma.social/\">Source</a>"
  146. {_result_text, %{tags: tags}} =
  147. Linkify.link_map(text, %{tags: MapSet.new()},
  148. hashtag: true,
  149. hashtag_handler: handler,
  150. hashtag_prefix: "https://example.com/tag/"
  151. )
  152. assert MapSet.to_list(tags) == ["#cofe"]
  153. text = "#cofe<br><a href=\"https://pleroma.social/\">Source</a>"
  154. {_result_text, %{tags: tags}} =
  155. Linkify.link_map(text, %{tags: MapSet.new()},
  156. hashtag: true,
  157. hashtag_handler: handler,
  158. hashtag_prefix: "https://example.com/tag/"
  159. )
  160. assert MapSet.to_list(tags) == ["#cofe"]
  161. text = "#cofe<a href=\"https://pleroma.social/\">Source</a>"
  162. {_result_text, %{tags: tags}} =
  163. Linkify.link_map(text, %{tags: MapSet.new()},
  164. hashtag: true,
  165. hashtag_handler: handler,
  166. hashtag_prefix: "https://example.com/tag/"
  167. )
  168. assert MapSet.to_list(tags) == ["#cofe"]
  169. text = "#cofe<code>fetch()</code>"
  170. {_result_text, %{tags: tags}} =
  171. Linkify.link_map(text, %{tags: MapSet.new()},
  172. hashtag: true,
  173. hashtag_handler: handler,
  174. hashtag_prefix: "https://example.com/tag/"
  175. )
  176. assert MapSet.to_list(tags) == ["#cofe"]
  177. text = "#cofe<pre>fetch()</pre>"
  178. {_result_text, %{tags: tags}} =
  179. Linkify.link_map(text, %{tags: MapSet.new()},
  180. hashtag: true,
  181. hashtag_handler: handler,
  182. hashtag_prefix: "https://example.com/tag/"
  183. )
  184. assert MapSet.to_list(tags) == ["#cofe"]
  185. end
  186. test "mention handler and hashtag prefix" do
  187. text =
  188. "Hello again, @user.&lt;script&gt;&lt;/script&gt;\nThis is on another :moominmamma: line. #2hu #epic #phantasmagoric"
  189. handler = fn "@" <> user = mention, _, _, _ ->
  190. ~s(<span class="h-card"><a href="#/user/#{user}">@<span>#{mention}</span></a></span>)
  191. end
  192. expected =
  193. ~s(Hello again, @user.&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>)
  194. assert Linkify.link(text,
  195. mention: true,
  196. mention_handler: handler,
  197. hashtag: true,
  198. hashtag_prefix: "/tag/",
  199. new_window: true
  200. ) == expected
  201. end
  202. test "mentions handler with hostname/@user links" do
  203. text =
  204. "hi @user, take a look at this post: https://example.com/@valid_user/posts/9w5AkQp956XIh74apc"
  205. valid_users = ["user", "valid_user"]
  206. handler = fn "@" <> user = mention, buffer, _opts, acc ->
  207. if Enum.member?(valid_users, user) do
  208. link = ~s(<a href="https://example.com/user/#{user}" data-user="#{user}">#{mention}</a>)
  209. {link, %{acc | mentions: MapSet.put(acc.mentions, {mention, user})}}
  210. else
  211. {buffer, acc}
  212. end
  213. end
  214. {result_text, %{mentions: mentions}} =
  215. Linkify.link_map(text, %{mentions: MapSet.new()},
  216. mention: true,
  217. mention_handler: handler,
  218. new_window: true
  219. )
  220. assert result_text ==
  221. "hi <a href=\"https://example.com/user/user\" data-user=\"user\">@user</a>, take a look at this post: <a href=\"https://example.com/@valid_user/posts/9w5AkQp956XIh74apc\" target=\"_blank\">https://example.com/@valid_user/posts/9w5AkQp956XIh74apc</a>"
  222. assert mentions |> MapSet.to_list() |> Enum.map(&elem(&1, 1)) == ["user"]
  223. end
  224. test "mentions handler and extra links" do
  225. text =
  226. "hi @user, text me asap xmpp:me@cofe.ai, (or contact me at me@cofe.ai), please.<br>http://cofe.ai."
  227. valid_users = ["user", "cofe"]
  228. handler = fn "@" <> user = mention, buffer, _opts, acc ->
  229. if Enum.member?(valid_users, user) do
  230. link = ~s(<a href="https://example.com/user/#{user}" data-user="#{user}">#{mention}</a>)
  231. {link, %{acc | mentions: MapSet.put(acc.mentions, {mention, user})}}
  232. else
  233. {buffer, acc}
  234. end
  235. end
  236. {result_text, %{mentions: mentions}} =
  237. Linkify.link_map(text, %{mentions: MapSet.new()},
  238. mention: true,
  239. mention_handler: handler,
  240. extra: true,
  241. extra_prefixes: ["xmpp:"],
  242. email: true
  243. )
  244. assert result_text ==
  245. "hi <a href=\"https://example.com/user/user\" data-user=\"user\">@user</a>, text me asap <a href=\"xmpp:me@cofe.ai\">xmpp:me@cofe.ai</a>, (or contact me at <a href=\"mailto:me@cofe.ai\">me@cofe.ai</a>), please.<br><a href=\"http://cofe.ai\">http://cofe.ai</a>."
  246. assert MapSet.to_list(mentions) == [{"@user", "user"}]
  247. end
  248. test "mentions handler and emails" do
  249. text = "hi @friend, here is my email<br><br>user@user.me"
  250. valid_users = ["user", "friend"]
  251. handler = fn "@" <> user = mention, buffer, _opts, acc ->
  252. if Enum.member?(valid_users, user) do
  253. link = ~s(<a href="https://example.com/user/#{user}" data-user="#{user}">#{mention}</a>)
  254. {link, %{acc | mentions: MapSet.put(acc.mentions, {mention, user})}}
  255. else
  256. {buffer, acc}
  257. end
  258. end
  259. {result_text, %{mentions: mentions}} =
  260. Linkify.link_map(text, %{mentions: MapSet.new()},
  261. mention: true,
  262. mention_handler: handler,
  263. extra: true,
  264. email: true
  265. )
  266. assert result_text ==
  267. "hi <a href=\"https://example.com/user/friend\" data-user=\"friend\">@friend</a>, here is my email<br><br><a href=\"mailto:user@user.me\">user@user.me</a>"
  268. assert MapSet.to_list(mentions) == [{"@friend", "friend"}]
  269. end
  270. test "href handler" do
  271. text = ~s(http://google.com)
  272. result_text = Linkify.link(text, href_handler: &"/redirect?#{URI.encode_query(to: &1)}")
  273. assert result_text ==
  274. ~s(<a href="/redirect?to=http%3A%2F%2Fgoogle.com">http://google.com</a>)
  275. end
  276. end
  277. describe "mentions" do
  278. test "simple mentions" do
  279. expected =
  280. ~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>.}
  281. assert Linkify.link("hello @user and @anotherUser.",
  282. mention: true,
  283. mention_prefix: "https://example.com/user/",
  284. new_window: true
  285. ) == expected
  286. end
  287. test "mentions inside html tags" do
  288. text =
  289. "<p><strong>hello world</strong></p>\n<p><`em>another @user__test and @user__test http://google.com paragraph</em></p>\n"
  290. expected =
  291. "<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\">http://google.com</a> paragraph</em></p>\n"
  292. assert Linkify.link(text, mention: true, mention_prefix: "u/") == expected
  293. end
  294. test "mention @user@example.com" do
  295. text = "hey @user@example.com"
  296. expected =
  297. "hey <a href=\"https://example.com/user/user@example.com\" target=\"_blank\">@user@example.com</a>"
  298. assert Linkify.link(text,
  299. mention: true,
  300. mention_prefix: "https://example.com/user/",
  301. new_window: true
  302. ) == expected
  303. text = "That's @user@example.com's server"
  304. assert Linkify.link(text, mention: true, mention_prefix: "https://example.com/user/") ==
  305. text
  306. end
  307. test "mentions with no word-separation before them" do
  308. text = "@@example hey! >@@test@example.com idolm@ster"
  309. assert Linkify.link(text, mention: true, mention_prefix: "/users/") == text
  310. end
  311. test "invalid mentions" do
  312. text = "hey user@example"
  313. assert Linkify.link(text, mention: true, mention_prefix: "/users/") == text
  314. end
  315. test "IDN domain" do
  316. text = "hello @lain@我爱你.com"
  317. expected = "hello <a href=\"/users/lain@我爱你.com\">@lain@我爱你.com</a>"
  318. assert Linkify.link(text, mention: true, mention_prefix: "/users/") == expected
  319. text = "hello @lain@xn--6qq986b3xl.com"
  320. expected = "hello <a href=\"/users/lain@xn--6qq986b3xl.com\">@lain@xn--6qq986b3xl.com</a>"
  321. assert Linkify.link(text, mention: true, mention_prefix: "/users/") == expected
  322. end
  323. test ".onion domain" do
  324. text = "Hey @admin@vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd.onion"
  325. expected =
  326. "Hey <a href=\"/users/admin@vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd.onion\">@admin@vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd.onion</a>"
  327. assert Linkify.link(text, mention: true, mention_prefix: "/users/") == expected
  328. end
  329. end
  330. describe "hashtag links" do
  331. test "hashtag" do
  332. expected =
  333. " one <a href=\"https://example.com/tag/2two\" target=\"_blank\">#2two</a> three <a href=\"https://example.com/tag/four\" target=\"_blank\">#four</a>."
  334. assert Linkify.link(" one #2two three #four.",
  335. hashtag: true,
  336. hashtag_prefix: "https://example.com/tag/",
  337. new_window: true
  338. ) == expected
  339. end
  340. test "must have non-numbers" do
  341. expected = "<a href=\"/t/1ok\">#1ok</a> #42 #7"
  342. assert Linkify.link("#1ok #42 #7",
  343. hashtag: true,
  344. hashtag_prefix: "/t/",
  345. rel: false
  346. ) == expected
  347. end
  348. test "support French" do
  349. text = "#administrateur·rice·s #ingénieur·e·s"
  350. expected =
  351. "<a href=\"/t/administrateur·rice·s\">#administrateur·rice·s</a> <a href=\"/t/ingénieur·e·s\">#ingénieur·e·s</a>"
  352. assert Linkify.link(text,
  353. hashtag: true,
  354. hashtag_prefix: "/t/",
  355. rel: false
  356. ) == expected
  357. end
  358. test "support Telugu" do
  359. text = "#చక్రం #కకకకక్ #కకకకాక #కకకక్రకకకక"
  360. expected =
  361. "<a href=\"/t/చక్రం\">#చక్రం</a> <a href=\"/t/కకకకక్\">#కకకకక్</a> <a href=\"/t/కకకకాక\">#కకకకాక</a> <a href=\"/t/కకకక్రకకకక\">#కకకక్రకకకక</a>"
  362. assert Linkify.link(text,
  363. hashtag: true,
  364. hashtag_prefix: "/t/",
  365. rel: false
  366. ) == expected
  367. end
  368. test "do not turn urls with hashes into hashtags" do
  369. text = "http://google.com#test #test http://google.com/#test #tag"
  370. expected =
  371. "<a href=\"http://google.com#test\">http://google.com#test</a> <a href=\"https://example.com/tag/test\">#test</a> <a href=\"http://google.com/#test\">http://google.com/#test</a> <a href=\"https://example.com/tag/tag\">#tag</a>"
  372. assert Linkify.link(text,
  373. hashtag: true,
  374. rel: false,
  375. hashtag_prefix: "https://example.com/tag/"
  376. ) == expected
  377. end
  378. test "works with non-latin characters" do
  379. text = "#漢字 #は #тест #ทดสอบ"
  380. expected =
  381. "<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>"
  382. assert Linkify.link(text,
  383. rel: false,
  384. hashtag: true,
  385. hashtag_prefix: "https://example.com/tag/"
  386. ) == expected
  387. end
  388. end
  389. describe "links" do
  390. test "turning urls into links" do
  391. text = "Hey, check out http://www.youtube.com/watch?v=8Zg1-TufF%20zY?x=1&y=2#blabla ."
  392. expected =
  393. "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> ."
  394. assert Linkify.link(text, new_window: true) == expected
  395. end
  396. test "turn urls with schema into urls" do
  397. text = "📌 https://google.com"
  398. expected = "📌 <a href=\"https://google.com\">https://google.com</a>"
  399. assert Linkify.link(text, rel: false) == expected
  400. text = "http://www.cs.vu.nl/~ast/intel/"
  401. expected = "<a href=\"http://www.cs.vu.nl/~ast/intel/\">http://www.cs.vu.nl/~ast/intel/</a>"
  402. assert Linkify.link(text) == expected
  403. text = "https://forum.zdoom.org/viewtopic.php?f=44&t=57087"
  404. expected =
  405. "<a href=\"https://forum.zdoom.org/viewtopic.php?f=44&t=57087\">https://forum.zdoom.org/viewtopic.php?f=44&t=57087</a>"
  406. assert Linkify.link(text) == expected
  407. text = "https://en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul"
  408. expected =
  409. "<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>"
  410. assert Linkify.link(text) == expected
  411. text = "https://en.wikipedia.org/wiki/Duff's_device"
  412. expected =
  413. "<a href=\"https://en.wikipedia.org/wiki/Duff's_device\">https://en.wikipedia.org/wiki/Duff's_device</a>"
  414. assert Linkify.link(text) == expected
  415. text = "https://1.1.1.1/"
  416. expected = "<a href=\"https://1.1.1.1/\">https://1.1.1.1/</a>"
  417. assert Linkify.link(text) == expected
  418. text = "https://1.1.1.1:8080/"
  419. expected = "<a href=\"https://1.1.1.1:8080/\">https://1.1.1.1:8080/</a>"
  420. assert Linkify.link(text) == expected
  421. end
  422. test "strip prefix" do
  423. assert Linkify.link("http://google.com", strip_prefix: true) ==
  424. "<a href=\"http://google.com\">google.com</a>"
  425. assert Linkify.link("http://www.google.com", strip_prefix: true) ==
  426. "<a href=\"http://www.google.com\">google.com</a>"
  427. end
  428. test "hostname/@user" do
  429. text = "https://example.com/@user"
  430. expected =
  431. "<a href=\"https://example.com/@user\" target=\"_blank\">https://example.com/@user</a>"
  432. assert Linkify.link(text, new_window: true) == expected
  433. text = "https://example.com:4000/@user"
  434. expected =
  435. "<a href=\"https://example.com:4000/@user\" target=\"_blank\">https://example.com:4000/@user</a>"
  436. assert Linkify.link(text, new_window: true) == expected
  437. text = "https://example.com:4000/@user"
  438. expected =
  439. "<a href=\"https://example.com:4000/@user\" target=\"_blank\">https://example.com:4000/@user</a>"
  440. assert Linkify.link(text, new_window: true) == expected
  441. text = "@username"
  442. expected = "@username"
  443. assert Linkify.link(text, new_window: true) == expected
  444. end
  445. end
  446. describe "non http links" do
  447. test "xmpp" do
  448. text = "xmpp:user@example.com"
  449. expected = "<a href=\"xmpp:user@example.com\">xmpp:user@example.com</a>"
  450. assert Linkify.link(text, extra_prefixes: ["xmpp:"]) == expected
  451. end
  452. test "wrong xmpp" do
  453. text = "xmpp:user.example.com"
  454. assert Linkify.link(text, extra: true) == text
  455. end
  456. test "email" do
  457. text = "user@example.com"
  458. expected = "<a href=\"mailto:user@example.com\">user@example.com</a>"
  459. assert Linkify.link(text, email: true) == expected
  460. end
  461. test "magnet" do
  462. text =
  463. "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"
  464. expected =
  465. "<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>"
  466. assert Linkify.link(text, extra_prefixes: ["magnet:"]) == expected
  467. end
  468. test "dweb" do
  469. text =
  470. "dweb://584faa05d394190ab1a3f0240607f9bf2b7e2bd9968830a11cf77db0cea36a21+v1.0.0/path/to/file.txt"
  471. expected =
  472. "<a href=\"dweb://584faa05d394190ab1a3f0240607f9bf2b7e2bd9968830a11cf77db0cea36a21+v1.0.0/path/to/file.txt\">dweb://584faa05d394190ab1a3f0240607f9bf2b7e2bd9968830a11cf77db0cea36a21+v1.0.0/path/to/file.txt</a>"
  473. assert Linkify.link(text, extra_prefixes: ["dweb://"]) == expected
  474. end
  475. end
  476. describe "TLDs" do
  477. test "parse with scheme" do
  478. text = "https://google.com"
  479. expected = "<a href=\"https://google.com\">https://google.com</a>"
  480. assert Linkify.link(text) == expected
  481. end
  482. test "only existing TLDs with scheme" do
  483. text = "this url https://google.foobar.com/ has valid TLD"
  484. expected =
  485. "this url <a href=\"https://google.foobar.com/\">https://google.foobar.com/</a> has valid TLD"
  486. assert Linkify.link(text) == expected
  487. end
  488. test "only existing TLDs with and without scheme" do
  489. text = "this url http://google.foobar.com/ has valid TLD"
  490. expected =
  491. "this url <a href=\"http://google.foobar.com/\">http://google.foobar.com/</a> has valid TLD"
  492. assert Linkify.link(text) == expected
  493. end
  494. test "FQDN (with trailing period)" do
  495. text =
  496. "Check out this article: https://www.wired.com./story/marissa-mayer-startup-sunshine-contacts/"
  497. expected =
  498. "Check out this article: <a href=\"https://www.wired.com./story/marissa-mayer-startup-sunshine-contacts/\">https://www.wired.com./story/marissa-mayer-startup-sunshine-contacts/</a>"
  499. assert Linkify.link(text) == expected
  500. end
  501. test "Do not link trailing punctuation" do
  502. text = "You can find more info at https://pleroma.social."
  503. expected =
  504. "You can find more info at <a href=\"https://pleroma.social\">https://pleroma.social</a>."
  505. assert Linkify.link(text) == expected
  506. text = "Of course it was http://google.com!!"
  507. expected = "Of course it was <a href=\"http://google.com\">http://google.com</a>!!"
  508. assert Linkify.link(text) == expected
  509. text =
  510. "First I had to login to http://hotmail.com, then I had to delete emails because my 15MB quota was full."
  511. expected =
  512. "First I had to login to <a href=\"http://hotmail.com\">http://hotmail.com</a>, then I had to delete emails because my 15MB quota was full."
  513. assert Linkify.link(text) == expected
  514. text = "I looked at http://theonion.com; it was no longer funny."
  515. expected =
  516. "I looked at <a href=\"http://theonion.com\">http://theonion.com</a>; it was no longer funny."
  517. assert Linkify.link(text) == expected
  518. end
  519. test "IDN and punycode domain" do
  520. text = "http://FrauBücher.com says Neiiighhh!"
  521. expected = "<a href=\"http://FrauBücher.com\">http://FrauBücher.com</a> says Neiiighhh!"
  522. assert Linkify.link(text) == expected
  523. text = "http://xn--fraubcher-u9a.com says Neiiighhh!"
  524. expected =
  525. "<a href=\"http://xn--fraubcher-u9a.com\">http://xn--fraubcher-u9a.com</a> says Neiiighhh!"
  526. assert Linkify.link(text) == expected
  527. end
  528. test ".onion domain" do
  529. text =
  530. "The http://riseup.net hidden service is at http://vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd.onion"
  531. expected =
  532. "The <a href=\"http://riseup.net\">http://riseup.net</a> hidden service is at <a href=\"http://vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd.onion\">http://vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd.onion</a>"
  533. assert Linkify.link(text) == expected
  534. end
  535. end
  536. end