logo

auto_linker

AutoLinker-shim, based on https://git.pleroma.social/pleroma/auto_linker
commit: 31c7d7ae9e11250b3f7944acee61b19168dac70c
parent 061191c44071d21585bb56da6f9cda67b3319f30
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Wed, 27 Feb 2019 04:39:46 +0100

Move extra option to extra_prefixes, remove auto-linking

Diffstat:

Mlib/linkify.ex15++++++++-------
Mlib/linkify/builder.ex10++--------
Mlib/linkify/parser.ex67++++++++++++++++++++++++++++---------------------------------------
Mtest/builder_test.exs24++++++++++++------------
Mtest/linkify_test.exs125+++++++++++++++++++++++++++++++++++--------------------------------------------
Mtest/parser_test.exs102++++++++++++++++++++++++++++---------------------------------------------------
6 files changed, 141 insertions(+), 202 deletions(-)

diff --git a/lib/linkify.ex b/lib/linkify.ex @@ -7,14 +7,14 @@ defmodule Linkify do ## Examples - iex> Linkify.link("google.com") - ~s(<a href="http://google.com">google.com</a>) + iex> Linkify.link("http://google.com") + ~s(<a href="http://google.com">http://google.com</a>) - iex> Linkify.link("google.com", new_window: true, rel: "noopener noreferrer") - ~s(<a href="http://google.com" target="_blank" rel="noopener noreferrer">google.com</a>) + iex> Linkify.link("http://google.com", new_window: true, rel: "noopener noreferrer") + ~s(<a href="http://google.com" target="_blank" rel="noopener noreferrer">http://google.com</a>) - iex> Linkify.link("google.com", class: "linkified") - ~s(<a href="http://google.com" class="linkified">google.com</a>) + iex> Linkify.link("http://google.com", class: "linkified") + ~s(<a href="http://google.com" class="linkified">http://google.com</a>) """ import Linkify.Parser @@ -38,7 +38,8 @@ defmodule Linkify do * `hashtag: false` - link #hashtags (when `true`, requires `hashtag_prefix` or `hashtag_handler` options to be set) * `hashtag_prefix: nil` - a prefix to build a link for a hashtag (example: `https://example.com/tag/`) * `hashtag_handler: nil` - a custom handler to validate and format a hashtag - * `extra: false` - link urls with rarely used schemes (magnet, ipfs, irc, etc.) + * `extra: false` - ignored, moved to detection if `extra_prefixes` is a non-empty list + * `extra_prefixes` - list of prefixes that aren't http(s)://, mailto: or xmpp: * `validate_tld: true` - Set to false to disable TLD validation for urls/emails, also can be set to :no_scheme to validate TLDs only for urls without a scheme (e.g `example.com` will be validated, but `http://example.loki` won't) * `iodata` - Set to `true` to return iodata as a result, or `:safe` for iodata with linkified anchor tags wrapped in Phoenix.HTML `:safe` tuples (removes need for further sanitization) * `href_handler: nil` - a custom handler to process a url before it is set as the link href, useful for generating exit links diff --git a/lib/linkify/builder.ex b/lib/linkify/builder.ex @@ -6,15 +6,13 @@ defmodule Linkify.Builder do @doc """ Create a link. """ - def create_link(text, opts) do - url = add_scheme(text) - + def create_link(url, opts) do [] |> build_attrs(url, opts, :rel) |> build_attrs(url, opts, :target) |> build_attrs(url, opts, :class) |> build_attrs(url, opts, :href) - |> format_url(text, opts) + |> format_url(url, opts) end defp build_attrs(attrs, uri, %{rel: get_rel}, :rel) when is_function(get_rel, 1) do @@ -49,10 +47,6 @@ defmodule Linkify.Builder do end end - defp add_scheme("http://" <> _ = url), do: url - defp add_scheme("https://" <> _ = url), do: url - defp add_scheme(url), do: "http://" <> url - defp format_url(attrs, url, opts) do url = url diff --git a/lib/linkify/parser.ex b/lib/linkify/parser.ex @@ -5,30 +5,12 @@ defmodule Linkify.Parser do alias Linkify.Builder - @invalid_url ~r/(\.\.+)|(^(\d+\.){1,2}\d+$)/ - - @match_url ~r{^(?:\W*)?(?<url>(?:https?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~%:\/?#[\]@!\$&'\(\)\*\+,;=.]+$)}u - @match_hashtag ~r/^(?<tag>\#[[:word:]_]*[[:alpha:]_·][[:word:]_·\p{M}]*)/u @match_skipped_tag ~r/^(?<tag>(a|code|pre)).*>*/ @delimiters ~r/[,.;:>?!]*$/ - @prefix_extra [ - "magnet:?", - "dweb://", - "dat://", - "gopher://", - "ipfs://", - "ipns://", - "irc://", - "ircs://", - "irc6://", - "mumble://", - "ssb://" - ] - @default_opts %{ url: true, validate_tld: true @@ -41,11 +23,11 @@ defmodule Linkify.Parser do ## Examples - iex> Linkify.Parser.parse("Check out google.com") - ~s{Check out <a href="http://google.com">google.com</a>} + iex> Linkify.Parser.parse("Check out http://google.com") + ~s{Check out <a href="http://google.com">http://google.com</a>} """ - @types [:url, :hashtag, :extra, :mention, :email] + @types [:url, :hashtag, :mention, :email] def parse(input, opts \\ %{}) def parse(input, opts) when is_binary(input), do: {input, %{}} |> parse(opts) |> elem(0) @@ -156,14 +138,7 @@ defmodule Linkify.Parser do def check_and_link(:url, buffer, opts, _user_acc) do if url?(buffer, opts) do - case @match_url |> Regex.run(buffer, capture: [:url]) |> hd() do - ^buffer -> - link_url(buffer, opts) - - url -> - link = link_url(url, opts) - restore_stripped_symbols(buffer, url, link) - end + link_url(buffer, opts) else :nomatch end @@ -185,14 +160,6 @@ defmodule Linkify.Parser do |> link_hashtag(buffer, opts, user_acc) end - def check_and_link(:extra, "xmpp:" <> handle = buffer, opts, _user_acc) do - if email?(handle, opts), do: link_extra(buffer, opts), else: :nomatch - end - - def check_and_link(:extra, buffer, opts, _user_acc) do - if String.starts_with?(buffer, @prefix_extra), do: link_extra(buffer, opts), else: :nomatch - end - defp strip_parens(buffer) do buffer |> String.trim_leading("(") @@ -202,7 +169,9 @@ defmodule Linkify.Parser do defp strip_punctuation(buffer), do: String.replace(buffer, @delimiters, "") def url?(buffer, opts) do - valid_url?(buffer) && Regex.match?(@match_url, buffer) && valid_tld?(buffer, opts) + prefixes = ["http://", "https://", "mailto:"] ++ (opts[:extra_prefixes] || []) + + valid_url?(buffer) && String.starts_with?(buffer, prefixes) && valid_tld?(buffer, opts) end def email?(buffer, opts) do @@ -213,7 +182,27 @@ defmodule Linkify.Parser do end end - defp valid_url?(url), do: !Regex.match?(@invalid_url, url) + defp valid_url?(url) do + # ~r/^[0-9a-z+\-\.]+:[0-9a-z\-\._~!$&'()*+,;=:@?\/#%]+$/ui + case String.split(url, ":", parts: 2) do + [scheme, rest] -> + valid_scheme? = Regex.match?(~r/^[0-9a-z+\-\.]+/, scheme) + + valid_rest? = + rest + |> String.to_charlist() + |> Enum.any?(fn s -> + !(s >= 0x80 || s in 0x30..0x39 || s in 0x41..0x5A || s in 0x61..0x7A || + s in '-._~!$&\'()*+,;=:@?/#%') + end) + |> Kernel.!() + + valid_scheme? && valid_rest? + + _ -> + false + end + end @doc """ Validates a URL's TLD. Returns a boolean. diff --git a/test/builder_test.exs b/test/builder_test.exs @@ -5,27 +5,27 @@ defmodule Linkify.BuilderTest do import Linkify.Builder test "create_link/2" do - expected = "<a href=\"http://text\">text</a>" + expected = "<a href=\"http://text\">http://text</a>" - assert create_link("text", %{}) == expected + assert create_link("http://text", %{}) == expected - expected = "<a href=\"http://text\" target=\"_blank\">text</a>" + expected = "<a href=\"http://text\" target=\"_blank\">http://text</a>" - assert create_link("text", %{new_window: true}) == expected + assert create_link("http://text", %{new_window: true}) == expected - expected = "<a href=\"http://text\" class=\"linkified\">text</a>" - assert create_link("text", %{class: "linkified"}) == expected + expected = "<a href=\"http://text\" class=\"linkified\">http://text</a>" + assert create_link("http://text", %{class: "linkified"}) == expected - expected = "<a href=\"http://text\" rel=\"me\">text</a>" + expected = "<a href=\"http://text\" rel=\"me\">http://text</a>" - assert create_link("text", %{rel: "me"}) == expected + assert create_link("http://text", %{rel: "me"}) == expected - expected = "<a href=\"http://text\">t...</a>" + expected = "<a href=\"http://text\">h...</a>" - assert create_link("text", %{truncate: 3}) == expected + assert create_link("http://text", %{truncate: 3}) == expected - expected = "<a href=\"http://text\">text</a>" - assert create_link("text", %{truncate: 2}) == expected + expected = "<a href=\"http://text\">http://text</a>" + assert create_link("http://text", %{truncate: 2}) == expected expected = "<a href=\"http://text\">http://text</a>" assert create_link("http://text", %{strip_prefix: false}) == expected diff --git a/test/linkify_test.exs b/test/linkify_test.exs @@ -3,21 +3,21 @@ defmodule LinkifyTest do doctest Linkify test "default link" do - assert Linkify.link("google.com") == - "<a href=\"http://google.com\">google.com</a>" + assert Linkify.link("http://google.com") == + "<a href=\"http://google.com\">http://google.com</a>" end test "default link iodata" do - assert Linkify.link_to_iodata("google.com") == - [["<a ", "href=\"http://google.com\"", ">", "google.com", "</a>"]] + assert Linkify.link_to_iodata("http://google.com") == + [["<a ", "href=\"http://google.com\"", ">", "http://google.com", "</a>"]] end test "default link safe iodata" do - assert Linkify.link_safe("google.com") == + assert Linkify.link_safe("http://google.com") == [ [ {:safe, ["<a ", "href=\"http://google.com\"", ">"]}, - "google.com", + "http://google.com", {:safe, "</a>"} ] ] @@ -32,27 +32,27 @@ defmodule LinkifyTest do text = "hello google.com https://ddg.com user@email.com irc:///mIRC" expected = - "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>" + "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>" assert Linkify.link(text, email: true, - extra: true + extra_prefixes: ["irc://"] ) == expected end test "all kinds of links iodata" do - text = "hello google.com https://ddg.com user@email.com irc:///mIRC" + text = "hello http://google.com https://ddg.com user@email.com irc:///mIRC" expected = [ "hello", " ", - ["<a ", "href=\"http://google.com\"", ">", "google.com", "</a>"], + ["<a ", "href=\"http://google.com\"", ">", "http://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>"] + "irc:///mIRC" ] assert Linkify.link_to_iodata(text, @@ -62,45 +62,45 @@ defmodule LinkifyTest do end test "class attribute" do - assert Linkify.link("google.com", class: "linkified") == - "<a href=\"http://google.com\" class=\"linkified\">google.com</a>" + assert Linkify.link("http://google.com", class: "linkified") == + "<a href=\"http://google.com\" class=\"linkified\">http://google.com</a>" end test "class attribute iodata" do - assert Linkify.link_to_iodata("google.com", class: "linkified") == + assert Linkify.link_to_iodata("http://google.com", class: "linkified") == [ [ "<a ", "href=\"http://google.com\" class=\"linkified\"", ">", - "google.com", + "http://google.com", "</a>" ] ] end test "rel attribute" do - assert Linkify.link("google.com", rel: "noopener noreferrer") == - "<a href=\"http://google.com\" rel=\"noopener noreferrer\">google.com</a>" + assert Linkify.link("http://google.com", rel: "noopener noreferrer") == + "<a href=\"http://google.com\" rel=\"noopener noreferrer\">http://google.com</a>" end test "rel attribute iodata" do - assert Linkify.link_to_iodata("google.com", rel: "noopener noreferrer") == + assert Linkify.link_to_iodata("http://google.com", rel: "noopener noreferrer") == [ [ "<a ", "href=\"http://google.com\" rel=\"noopener noreferrer\"", ">", - "google.com", + "http://google.com", "</a>" ] ] end test "rel as function" do - text = "google.com" + text = "http://google.com" - expected = "<a href=\"http://google.com\" rel=\"com\">google.com</a>" + expected = "<a href=\"http://google.com\" rel=\"com\">http://google.com</a>" custom_rel = fn url -> url |> String.split(".") |> List.last() @@ -108,9 +108,7 @@ defmodule LinkifyTest do assert Linkify.link(text, rel: custom_rel) == expected - text = "google.com" - - expected = "<a href=\"http://google.com\">google.com</a>" + expected = "<a href=\"http://google.com\">http://google.com</a>" custom_rel = fn _ -> nil end @@ -118,18 +116,18 @@ defmodule LinkifyTest do end test "strip parens" do - assert Linkify.link("(google.com)") == - "(<a href=\"http://google.com\">google.com</a>)" + assert Linkify.link("(http://google.com)") == + "(<a href=\"http://google.com\">http://google.com</a>)" end test "strip parens iodata" do - assert Linkify.link_to_iodata("(google.com)") == - [["(", ["<a ", "href=\"http://google.com\"", ">", "google.com", "</a>"], ")"]] + assert Linkify.link_to_iodata("(http://google.com)") == + [["(", ["<a ", "href=\"http://google.com\"", ">", "http://google.com", "</a>"], ")"]] end test "link_map/2" do - assert Linkify.link_map("google.com", []) == - {"<a href=\"http://google.com\">google.com</a>", []} + assert Linkify.link_map("http://google.com", []) == + {"<a href=\"http://google.com\">http://google.com</a>", []} end describe "custom handlers" do @@ -285,7 +283,7 @@ defmodule LinkifyTest do test "mentions handler and extra links" do text = - "hi @user, text me asap xmpp:me@cofe.ai, (or contact me at me@cofe.ai), please.<br>cofe.ai." + "hi @user, text me asap xmpp:me@cofe.ai, (or contact me at me@cofe.ai), please.<br>http://cofe.ai." valid_users = ["user", "cofe"] @@ -303,11 +301,12 @@ defmodule LinkifyTest do mention: true, mention_handler: handler, extra: true, + extra_prefixes: ["xmpp:"], email: true ) assert result_text == - "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\">cofe.ai</a>." + "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>." assert MapSet.to_list(mentions) == [{"@user", "user"}] end @@ -341,11 +340,12 @@ defmodule LinkifyTest do end test "href handler" do - text = ~s(google.com) + text = ~s(http://google.com) result_text = Linkify.link(text, href_handler: &"/redirect?#{URI.encode_query(to: &1)}") - assert result_text == ~s(<a href="/redirect?to=http%3A%2F%2Fgoogle.com">google.com</a>) + assert result_text == + ~s(<a href="/redirect?to=http%3A%2F%2Fgoogle.com">http://google.com</a>) end end @@ -363,10 +363,10 @@ defmodule LinkifyTest do test "mentions inside html tags" do text = - "<p><strong>hello world</strong></p>\n<p><`em>another @user__test and @user__test google.com paragraph</em></p>\n" + "<p><strong>hello world</strong></p>\n<p><`em>another @user__test and @user__test http://google.com paragraph</em></p>\n" expected = - "<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" + "<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" assert Linkify.link(text, mention: true, mention_prefix: "u/") == expected end @@ -474,10 +474,10 @@ defmodule LinkifyTest do end test "do not turn urls with hashes into hashtags" do - text = "google.com#test #test google.com/#test #tag" + text = "http://google.com#test #test http://google.com/#test #tag" expected = - "<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>" + "<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>" assert Linkify.link(text, hashtag: true, @@ -508,20 +508,12 @@ defmodule LinkifyTest do "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> ." assert Linkify.link(text, new_window: true) == expected - - # no scheme - text = "Hey, check out www.youtube.com/watch?v=8Zg1-TufF%20zY?x=1&y=2#blabla ." - - expected = - "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> ." - - assert Linkify.link(text, new_window: true) == expected end test "turn urls with schema into urls" do - text = "📌https://google.com" + text = "📌 https://google.com" - expected = "📌<a href=\"https://google.com\">https://google.com</a>" + expected = "📌 <a href=\"https://google.com\">https://google.com</a>" assert Linkify.link(text, rel: false) == expected @@ -609,7 +601,7 @@ defmodule LinkifyTest do expected = "<a href=\"xmpp:user@example.com\">xmpp:user@example.com</a>" - assert Linkify.link(text, extra: true) == expected + assert Linkify.link(text, extra_prefixes: ["xmpp:"]) == expected end test "wrong xmpp" do @@ -631,7 +623,7 @@ defmodule LinkifyTest do expected = "<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>" - assert Linkify.link(text, extra: true) == expected + assert Linkify.link(text, extra_prefixes: ["magnet:"]) == expected end test "dweb" do @@ -641,7 +633,7 @@ defmodule LinkifyTest do expected = "<a href=\"dweb://584faa05d394190ab1a3f0240607f9bf2b7e2bd9968830a11cf77db0cea36a21+v1.0.0/path/to/file.txt\">dweb://584faa05d394190ab1a3f0240607f9bf2b7e2bd9968830a11cf77db0cea36a21+v1.0.0/path/to/file.txt</a>" - assert Linkify.link(text, extra: true) == expected + assert Linkify.link(text, extra_prefixes: ["dweb://"]) == expected end end @@ -670,13 +662,6 @@ defmodule LinkifyTest do "this url <a href=\"http://google.foobar.com/\">http://google.foobar.com/</a> has valid TLD" assert Linkify.link(text) == expected - - text = "this url google.foobar.com/ has valid TLD" - - expected = - "this url <a href=\"http://google.foobar.com/\">google.foobar.com/</a> has valid TLD" - - assert Linkify.link(text) == expected end test "FQDN (with trailing period)" do @@ -697,49 +682,49 @@ defmodule LinkifyTest do assert Linkify.link(text) == expected - text = "Of course it was google.com!!" + text = "Of course it was http://google.com!!" - expected = "Of course it was <a href=\"http://google.com\">google.com</a>!!" + expected = "Of course it was <a href=\"http://google.com\">http://google.com</a>!!" assert Linkify.link(text) == expected text = - "First I had to login to hotmail.com, then I had to delete emails because my 15MB quota was full." + "First I had to login to http://hotmail.com, then I had to delete emails because my 15MB quota was full." expected = - "First I had to login to <a href=\"http://hotmail.com\">hotmail.com</a>, then I had to delete emails because my 15MB quota was full." + "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." assert Linkify.link(text) == expected - text = "I looked at theonion.com; it was no longer funny." + text = "I looked at http://theonion.com; it was no longer funny." expected = - "I looked at <a href=\"http://theonion.com\">theonion.com</a>; it was no longer funny." + "I looked at <a href=\"http://theonion.com\">http://theonion.com</a>; it was no longer funny." assert Linkify.link(text) == expected end test "IDN and punycode domain" do - text = "FrauBücher.com says Neiiighhh!" + text = "http://FrauBücher.com says Neiiighhh!" - expected = "<a href=\"http://FrauBücher.com\">FrauBücher.com</a> says Neiiighhh!" + expected = "<a href=\"http://FrauBücher.com\">http://FrauBücher.com</a> says Neiiighhh!" assert Linkify.link(text) == expected - text = "xn--fraubcher-u9a.com says Neiiighhh!" + text = "http://xn--fraubcher-u9a.com says Neiiighhh!" expected = - "<a href=\"http://xn--fraubcher-u9a.com\">xn--fraubcher-u9a.com</a> says Neiiighhh!" + "<a href=\"http://xn--fraubcher-u9a.com\">http://xn--fraubcher-u9a.com</a> says Neiiighhh!" assert Linkify.link(text) == expected end test ".onion domain" do text = - "The riseup.net hidden service is at vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd.onion" + "The http://riseup.net hidden service is at http://vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd.onion" expected = - "The <a href=\"http://riseup.net\">riseup.net</a> hidden service is at <a href=\"http://vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd.onion\">vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd.onion</a>" + "The <a href=\"http://riseup.net\">http://riseup.net</a> hidden service is at <a href=\"http://vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd.onion\">http://vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd.onion</a>" assert Linkify.link(text) == expected end diff --git a/test/parser_test.exs b/test/parser_test.exs @@ -22,7 +22,7 @@ defmodule Linkify.ParserTest do test "valid scheme false" do valid_non_scheme_urls() |> Enum.each(fn url -> - assert url?(url, scheme: false, validate_tld: true) + refute url?(url, scheme: false, validate_tld: true) end) end @@ -54,24 +54,15 @@ defmodule Linkify.ParserTest do end) end - test "does not checks the tld for url without a scheme when validate_tld: true" do - custom_tld_non_scheme_urls() - |> Enum.each(fn url -> - assert url?(url, scheme: false, validate_tld: true) - end) - end - - test "does not checks the tld for url without a scheme when validate_tld: :no_scheme" do + test "does not links for scheme-less urls" do custom_tld_non_scheme_urls() |> Enum.each(fn url -> - assert url?(url, scheme: false, validate_tld: :no_scheme) + refute url?(url, scheme: false, validate_tld: true) end) - end - test "does not check the tld for url without a scheme when validate_tld: false" do custom_tld_non_scheme_urls() |> Enum.each(fn url -> - assert url?(url, scheme: false, validate_tld: false) + refute url?(url, scheme: false, validate_tld: :no_scheme) end) end end @@ -90,48 +81,34 @@ defmodule Linkify.ParserTest do refute email?(email, []) end) end - - test "does not validate tlds when validate_tld: false" do - valid_custom_tld_emails() - |> Enum.each(fn email -> - assert email?(email, validate_tld: false) - end) - end - - test "does not validates tlds when validate_tld: true" do - valid_custom_tld_emails() - |> Enum.each(fn email -> - assert email?(email, validate_tld: true) - end) - end end describe "parse" do test "handle line breakes" do - text = "google.com\r\nssss" - expected = "<a href=\"http://google.com\">google.com</a>\r\nssss" + text = "http://google.com\r\nssss" + expected = "<a href=\"http://google.com\">http://google.com</a>\r\nssss" assert parse(text) == expected end test "handle angle bracket in the end" do - text = "google.com <br>" - assert parse(text) == "<a href=\"http://google.com\">google.com</a> <br>" + text = "http://google.com <br>" + assert parse(text) == "<a href=\"http://google.com\">http://google.com</a> <br>" - text = "google.com<br>hey" - assert parse(text) == "<a href=\"http://google.com\">google.com</a><br>hey" + text = "http://google.com<br>hey" + assert parse(text) == "<a href=\"http://google.com\">http://google.com</a><br>hey" - text = "hey<br>google.com" - assert parse(text) == "hey<br><a href=\"http://google.com\">google.com</a>" + text = "hey<br>http://google.com" + assert parse(text) == "hey<br><a href=\"http://google.com\">http://google.com</a>" - text = "<br />google.com" - assert parse(text) == "<br /><a href=\"http://google.com\">google.com</a>" + text = "<br />http://google.com" + assert parse(text) == "<br /><a href=\"http://google.com\">http://google.com</a>" - text = "google.com<" - assert parse(text) == "<a href=\"http://google.com\">google.com</a><" + text = "http://google.com<" + assert parse(text) == "<a href=\"http://google.com\">http://google.com</a><" - text = "google.com>" - assert parse(text) == "<a href=\"http://google.com\">google.com</a>>" + text = "http://google.com>" + assert parse(text) == "<a href=\"http://google.com\">http://google.com</a>>" end test "does not link attributes" do @@ -155,23 +132,23 @@ defmodule Linkify.ParserTest do end test "links url inside html" do - text = "<div>google.com</div>" + text = "<div>http://google.com</div>" - expected = "<div><a href=\"http://google.com\">google.com</a></div>" + expected = "<div><a href=\"http://google.com\">http://google.com</a></div>" assert parse(text, class: false, rel: false) == expected - text = "Check out <div class='section'>google.com</div>" + text = "Check out <div class='section'>http://google.com</div>" expected = - "Check out <div class='section'><a href=\"http://google.com\">google.com</a></div>" + "Check out <div class='section'><a href=\"http://google.com\">http://google.com</a></div>" assert parse(text, class: false, rel: false) == expected end test "links url inside nested html" do - text = "<p><strong>google.com</strong></p>" - expected = "<p><strong><a href=\"http://google.com\">google.com</a></strong></p>" + text = "<p><strong>http://google.com</strong></p>" + expected = "<p><strong><a href=\"http://google.com\">http://google.com</a></strong></p>" assert parse(text, class: false, rel: false) == expected end @@ -196,30 +173,23 @@ defmodule Linkify.ParserTest do " foo (<a href=\"https://example.com/path/folder/\">https://example.com/path/folder/</a>), bar" assert parse(text, class: false, rel: false, scheme: true) == expected - - text = " foo (example.com/path/folder/), bar" - - expected = - " foo (<a href=\"http://example.com/path/folder/\">example.com/path/folder/</a>), bar" - - assert parse(text, class: false, rel: false) == expected end test "do not link punctuation marks in the end" do - text = "google.com." - assert parse(text) == "<a href=\"http://google.com\">google.com</a>." + text = "http://google.com." + assert parse(text) == "<a href=\"http://google.com\">http://google.com</a>." - text = "google.com;" - assert parse(text) == "<a href=\"http://google.com\">google.com</a>;" + text = "http://google.com;" + assert parse(text) == "<a href=\"http://google.com\">http://google.com</a>;" - text = "google.com:" - assert parse(text) == "<a href=\"http://google.com\">google.com</a>:" + text = "http://google.com:" + assert parse(text) == "<a href=\"http://google.com\">http://google.com</a>:" - text = "hack google.com, please" - assert parse(text) == "hack <a href=\"http://google.com\">google.com</a>, please" + text = "hack http://google.com, please" + assert parse(text) == "hack <a href=\"http://google.com\">http://google.com</a>, please" - text = "(check out google.com)" - assert parse(text) == "(check out <a href=\"http://google.com\">google.com</a>)" + text = "(check out http://google.com)" + assert parse(text) == "(check out <a href=\"http://google.com\">http://google.com</a>)" end test "do not link urls" do @@ -227,7 +197,7 @@ defmodule Linkify.ParserTest do assert parse(text, url: false) == text end - test "links `:test.test`" do + test "does not link `:test.test`" do text = ":test.test" assert parse(text, %{ @@ -237,7 +207,7 @@ defmodule Linkify.ParserTest do strip_prefix: false, new_window: false, rel: false - }) == ~s(:<a href="http://test.test">test.test</a>) + }) == text end end