Do not add class, target and rel attributes by default

This commit is contained in:
Egor Kislitsyn 2019-06-27 15:32:07 +07:00
parent de6e003b8c
commit 66f5165655
7 changed files with 112 additions and 145 deletions

View file

@ -20,24 +20,19 @@ end
The following examples illustrate some examples on how to use the auto linker. The following examples illustrate some examples on how to use the auto linker.
```iex ```elixir
iex> Linkify.link("google.com") iex> Linkify.link("google.com")
"<a href='http://google.com' class='linkified' target='_blank' rel='noopener noreferrer'>google.com</a>" "<a href=\"http://google.com\">google.com</a>"
iex> Linkify.link("google.com", new_window: false, rel: false) iex> Linkify.link("google.com", class: "linkified")
"<a href='http://google.com' class='linkified'>google.com</a>" "<a href=\"http://google.com\" class=\"linkified\">google.com</a>"
iex> Linkify.link("google.com", new_window: false, rel: false, class: false) iex> Linkify.link("google.com", new_window: true)
"<a href='http://google.com'>google.com</a>" "<a href=\"http://google.com\" target=\"_blank\">google.com</a>"
iex> Linkify.link("call me at x9999", phone: true) iex> Linkify.link("google.com", new_window: true, rel: "noopener noreferrer")
"call me at <a href=\"#\" class=\"phone-number\" data-phone=\"9999\">x9999</a>" "<a href=\"http://google.com\" target=\"_blank\" rel=\"noopener noreferrer\">google.com</a>"
iex> Linkify.link("or at home on 555.555.5555", phone: true)
"or at home on <a href=\"#\" class=\"phone-number\" data-phone=\"5555555555\">555.555.5555</a>"
iex> Linkify.link(", work (555) 555-5555", phone: true)
", work <a href=\"#\" class=\"phone-number\" data-phone=\"5555555555\">(555) 555-5555</a>"
``` ```
See the [Docs](https://hexdocs.pm/linkify/) for more examples See the [Docs](https://hexdocs.pm/linkify/) for more examples

View file

@ -3,52 +3,43 @@ defmodule Linkify do
Create url links from text containing urls. Create url links from text containing urls.
Turns an input string like `"Check out google.com"` into Turns an input string like `"Check out google.com"` into
`Check out "<a href=\"http://google.com\" target=\"_blank\" rel=\"noopener noreferrer\">google.com</a>"` `Check out "<a href=\"http://google.com\">google.com</a>"`
## Examples ## Examples
iex> Linkify.link("google.com") iex> Linkify.link("google.com")
~s(<a href="http://google.com" class="linkified" target="_blank" rel="noopener noreferrer">google.com</a>)
iex> Linkify.link("google.com", new_window: false, rel: false)
~s(<a href="http://google.com" class="linkified">google.com</a>)
iex> Linkify.link("google.com", new_window: false, rel: false, class: false)
~s(<a href="http://google.com">google.com</a>) ~s(<a href="http://google.com">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("google.com", class: "linkified")
~s(<a href="http://google.com" class="linkified">google.com</a>)
""" """
import Linkify.Parser import Linkify.Parser
@doc """ @doc """
Auto link a string. Finds links and turns them into HTML `<a>` tag.
Options: Options:
* `class: "linkified"` - specify the class to be added to the generated link. false to clear * `class` - specify the class to be added to the generated link.
* `rel: "noopener noreferrer"` - override the rel attribute. false to clear * `rel` - specify the rel attribute.
* `new_window: true` - set to false to remove `target='_blank'` attribute * `new_window` - set to `true` to add `target="_blank"` attribute
* `truncate: false` - Set to a number to truncate urls longer then the number. Truncated urls will end in `..` * `truncate` - Set to a number to truncate urls longer then the number. Truncated urls will end in `...`
* `strip_prefix: true` - Strip the scheme prefix * `strip_prefix` - Strip the scheme prefix (default: `false`)
* `exclude_class: false` - Set to a class name when you don't want urls auto linked in the html of the give class * `exclude_class` - Set to a class name when you don't want urls auto linked in the html of the give class (default: `false`)
* `exclude_id: false` - Set to an element id when you don't want urls auto linked in the html of the give element * `exclude_id` - Set to an element id when you don't want urls auto linked in the html of the give element (default: `false`)
* `email: false` - link email links * `email` - link email links (default: `false`)
* `mention: false` - link @mentions (when `true`, requires `mention_prefix` or `mention_handler` options to be set) * `mention` - link @mentions (when `true`, requires `mention_prefix` or `mention_handler` options to be set) (default: `false`)
* `mention_prefix: nil` - a prefix to build a link for a mention (example: `https://example.com/user/`) * `mention_prefix` - a prefix to build a link for a mention (example: `https://example.com/user/`, default: `nil`)
* `mention_handler: nil` - a custom handler to validate and formart a mention * `mention_handler` - a custom handler to validate and formart a mention (default: `nil`)
* `hashtag: false` - link #hashtags (when `true`, requires `hashtag_prefix` or `hashtag_handler` options to be set) * `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_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 formart a hashtag * `hashtag_handler: nil` - a custom handler to validate and formart a hashtag
* `extra: false` - link urls with rarely used schemes (magnet, ipfs, irc, etc.) * `extra: false` - link urls with rarely used schemes (magnet, ipfs, irc, etc.)
* `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) * `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)
Each of the above options can be specified when calling `link(text, opts)`
or can be set in the `:linkify`'s configuration. For example:
config :linkify,
class: false,
new_window: false
Note that passing opts to `link/2` will override the configuration settings.
""" """
def link(text, opts \\ []) do def link(text, opts \\ []) do
parse(text, opts) parse(text, opts)

View file

@ -25,18 +25,18 @@ defmodule Linkify.Builder do
end end
defp build_attrs(attrs, _, opts, :rel) do defp build_attrs(attrs, _, opts, :rel) do
case Map.get(opts, :rel, "noopener noreferrer") do case Map.get(opts, :rel) do
rel when is_binary(rel) -> [{:rel, rel} | attrs] rel when is_binary(rel) -> [{:rel, rel} | attrs]
_ -> attrs _ -> attrs
end end
end end
defp build_attrs(attrs, _, opts, :target) do defp build_attrs(attrs, _, opts, :target) do
if Map.get(opts, :new_window, true), do: [{:target, :_blank} | attrs], else: attrs if Map.get(opts, :new_window), do: [{:target, :_blank} | attrs], else: attrs
end end
defp build_attrs(attrs, _, opts, :class) do defp build_attrs(attrs, _, opts, :class) do
case Map.get(opts, :class, "linkified") do case Map.get(opts, :class) do
cls when is_binary(cls) -> [{:class, cls} | attrs] cls when is_binary(cls) -> [{:class, cls} | attrs]
_ -> attrs _ -> attrs
end end

View file

@ -51,7 +51,7 @@ defmodule Linkify.Parser do
## Examples ## Examples
iex> Linkify.Parser.parse("Check out google.com") iex> Linkify.Parser.parse("Check out google.com")
~s{Check out <a href="http://google.com" class="linkified" target="_blank" rel="noopener noreferrer">google.com</a>} ~s{Check out <a href="http://google.com">google.com</a>}
""" """
@types [:url, :email, :hashtag, :mention, :extra] @types [:url, :email, :hashtag, :mention, :extra]

View file

@ -5,27 +5,30 @@ defmodule Linkify.BuilderTest do
import Linkify.Builder import Linkify.Builder
test "create_link/2" do test "create_link/2" do
expected = expected = "<a href=\"http://text\">text</a>"
"<a href=\"http://text\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">text</a>"
assert create_link("text", %{}) == expected assert create_link("text", %{}) == expected
expected = "<a href=\"http://text\" class=\"linkified\" target=\"_blank\">text</a>" expected = "<a href=\"http://text\" target=\"_blank\">text</a>"
assert create_link("text", %{rel: nil}) == expected
expected = "<a href=\"http://text\" class=\"linkified\" target=\"_blank\" rel=\"me\">text</a>" assert create_link("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\" rel=\"me\">text</a>"
assert create_link("text", %{rel: "me"}) == expected assert create_link("text", %{rel: "me"}) == expected
expected = "<a href=\"http://text\" class=\"linkified\" target=\"_blank\">t...</a>" expected = "<a href=\"http://text\">t...</a>"
assert create_link("text", %{truncate: 3, rel: false}) == expected assert create_link("text", %{truncate: 3}) == expected
expected = "<a href=\"http://text\" class=\"linkified\" target=\"_blank\">text</a>" expected = "<a href=\"http://text\">text</a>"
assert create_link("text", %{truncate: 2, rel: false}) == expected assert create_link("text", %{truncate: 2}) == expected
expected = "<a href=\"http://text\" class=\"linkified\" target=\"_blank\">http://text</a>" expected = "<a href=\"http://text\">http://text</a>"
assert create_link("http://text", %{rel: false, strip_prefix: false}) == expected assert create_link("http://text", %{strip_prefix: false}) == expected
end end
test "format_hashtag/3" do test "format_hashtag/3" do
@ -46,14 +49,13 @@ defmodule Linkify.BuilderTest do
end end
test "create_mention_link/3" do test "create_mention_link/3" do
expected = expected = "<a href=\"/u/navi\">@navi</a>"
"<a href=\"/u/navi\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">@navi</a>"
assert create_mention_link("@navi", "hello @navi", %{mention_prefix: "/u/"}) == expected assert create_mention_link("@navi", "hello @navi", %{mention_prefix: "/u/"}) == expected
end end
test "create_email_link/3" do test "create_email_link/3" do
expected = "<a href=\"mailto:user@example.org\" class=\"linkified\">user@example.org</a>" expected = "<a href=\"mailto:user@example.org\">user@example.org</a>"
assert create_email_link("user@example.org", %{}) == expected assert create_email_link("user@example.org", %{}) == expected
assert create_email_link("user@example.org", %{href: "mailto:user@example.org"}) == expected assert create_email_link("user@example.org", %{href: "mailto:user@example.org"}) == expected
end end

View file

@ -4,7 +4,7 @@ defmodule LinkifyTest do
test "default link" do test "default link" do
assert Linkify.link("google.com") == assert Linkify.link("google.com") ==
"<a href=\"http://google.com\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">google.com</a>" "<a href=\"http://google.com\">google.com</a>"
end end
test "does on link existing links" do test "does on link existing links" do
@ -20,13 +20,20 @@ defmodule LinkifyTest do
assert Linkify.link(text, assert Linkify.link(text,
email: true, email: true,
extra: true, extra: true
class: false,
new_window: false,
rel: false
) == expected ) == expected
end end
test "class attribute" do
assert Linkify.link("google.com", class: "linkified") ==
"<a href=\"http://google.com\" class=\"linkified\">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>"
end
test "rel as function" do test "rel as function" do
text = "google.com" text = "google.com"
@ -36,11 +43,7 @@ defmodule LinkifyTest do
url |> String.split(".") |> List.last() url |> String.split(".") |> List.last()
end end
assert Linkify.link(text, assert Linkify.link(text, rel: custom_rel) == expected
class: false,
new_window: false,
rel: custom_rel
) == expected
text = "google.com" text = "google.com"
@ -48,17 +51,12 @@ defmodule LinkifyTest do
custom_rel = fn _ -> nil end custom_rel = fn _ -> nil end
assert Linkify.link(text, assert Linkify.link(text, rel: custom_rel) == expected
class: false,
new_window: false,
rel: custom_rel
) == expected
end end
test "link_map/2" do test "link_map/2" do
assert Linkify.link_map("google.com", []) == assert Linkify.link_map("google.com", []) ==
{"<a href=\"http://google.com\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">google.com</a>", {"<a href=\"http://google.com\">google.com</a>", []}
[]}
end end
describe "custom handlers" do describe "custom handlers" do
@ -100,8 +98,6 @@ defmodule LinkifyTest do
hashtag: true, hashtag: true,
hashtag_handler: handler, hashtag_handler: handler,
hashtag_prefix: "https://example.com/user/", hashtag_prefix: "https://example.com/user/",
class: false,
new_window: false,
rel: false rel: false
) )
@ -120,13 +116,14 @@ defmodule LinkifyTest do
end end
expected = expected =
~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" class="linkified" target="_blank" rel="noopener noreferrer">#2hu</a> <a href="/tag/epic" class="linkified" target="_blank" rel="noopener noreferrer">#epic</a> <a href="/tag/phantasmagoric" class="linkified" target="_blank" rel="noopener noreferrer">#phantasmagoric</a>) ~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>)
assert Linkify.link(text, assert Linkify.link(text,
mention: true, mention: true,
mention_handler: handler, mention_handler: handler,
hashtag: true, hashtag: true,
hashtag_prefix: "/tag/" hashtag_prefix: "/tag/",
new_window: true
) == expected ) == expected
end end
end end
@ -134,11 +131,12 @@ defmodule LinkifyTest do
describe "mentions" do describe "mentions" do
test "simple mentions" do test "simple mentions" do
expected = expected =
~s{hello <a href="https://example.com/user/user" class="linkified" target="_blank" rel="noopener noreferrer">@user</a> and <a href="https://example.com/user/anotherUser" class="linkified" target="_blank" rel="noopener noreferrer">@anotherUser</a>.} ~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>.}
assert Linkify.link("hello @user and @anotherUser.", assert Linkify.link("hello @user and @anotherUser.",
mention: true, mention: true,
mention_prefix: "https://example.com/user/" mention_prefix: "https://example.com/user/",
new_window: true
) == expected ) == expected
end end
@ -149,24 +147,19 @@ defmodule LinkifyTest do
expected = 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\">google.com</a> paragraph</em></p>\n"
assert Linkify.link(text, assert Linkify.link(text, mention: true, mention_prefix: "u/") == expected
mention: true,
mention_prefix: "u/",
class: false,
rel: false,
new_window: false
) == expected
end end
test "metion @user@example.com" do test "metion @user@example.com" do
text = "hey @user@example.com" text = "hey @user@example.com"
expected = expected =
"hey <a href=\"https://example.com/user/user@example.com\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">@user@example.com</a>" "hey <a href=\"https://example.com/user/user@example.com\" target=\"_blank\">@user@example.com</a>"
assert Linkify.link(text, assert Linkify.link(text,
mention: true, mention: true,
mention_prefix: "https://example.com/user/" mention_prefix: "https://example.com/user/",
new_window: true
) == expected ) == expected
end end
end end
@ -174,11 +167,12 @@ defmodule LinkifyTest do
describe "hashtag links" do describe "hashtag links" do
test "hashtag" do test "hashtag" do
expected = expected =
" one <a href=\"https://example.com/tag/2two\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">#2two</a> three <a href=\"https://example.com/tag/four\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">#four</a>." " one <a href=\"https://example.com/tag/2two\" target=\"_blank\">#2two</a> three <a href=\"https://example.com/tag/four\" target=\"_blank\">#four</a>."
assert Linkify.link(" one #2two three #four.", assert Linkify.link(" one #2two three #four.",
hashtag: true, hashtag: true,
hashtag_prefix: "https://example.com/tag/" hashtag_prefix: "https://example.com/tag/",
new_window: true
) == expected ) == expected
end end
@ -188,9 +182,7 @@ defmodule LinkifyTest do
assert Linkify.link("#1ok #42 #7", assert Linkify.link("#1ok #42 #7",
hashtag: true, hashtag: true,
hashtag_prefix: "/t/", hashtag_prefix: "/t/",
class: false, rel: false
rel: false,
new_window: false
) == expected ) == expected
end end
@ -203,9 +195,7 @@ defmodule LinkifyTest do
assert Linkify.link(text, assert Linkify.link(text,
hashtag: true, hashtag: true,
hashtag_prefix: "/t/", hashtag_prefix: "/t/",
class: false, rel: false
rel: false,
new_window: false
) == expected ) == expected
end end
@ -218,9 +208,7 @@ defmodule LinkifyTest do
assert Linkify.link(text, assert Linkify.link(text,
hashtag: true, hashtag: true,
hashtag_prefix: "/t/", hashtag_prefix: "/t/",
class: false, rel: false
rel: false,
new_window: false
) == expected ) == expected
end end
@ -232,8 +220,6 @@ defmodule LinkifyTest do
assert Linkify.link(text, assert Linkify.link(text,
hashtag: true, hashtag: true,
class: false,
new_window: false,
rel: false, rel: false,
hashtag_prefix: "https://example.com/tag/" hashtag_prefix: "https://example.com/tag/"
) == expected ) == expected
@ -246,8 +232,6 @@ defmodule LinkifyTest do
"<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>" "<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>"
assert Linkify.link(text, assert Linkify.link(text,
class: false,
new_window: false,
rel: false, rel: false,
hashtag: true, hashtag: true,
hashtag_prefix: "https://example.com/tag/" hashtag_prefix: "https://example.com/tag/"
@ -260,73 +244,71 @@ defmodule LinkifyTest do
text = "Hey, check out http://www.youtube.com/watch?v=8Zg1-TufF%20zY?x=1&y=2#blabla ." text = "Hey, check out http://www.youtube.com/watch?v=8Zg1-TufF%20zY?x=1&y=2#blabla ."
expected = expected =
"Hey, check out <a href=\"http://www.youtube.com/watch?v=8Zg1-TufF%20zY?x=1&y=2#blabla\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">youtube.com/watch?v=8Zg1-TufF%20zY?x=1&y=2#blabla</a> ." "Hey, check out <a href=\"http://www.youtube.com/watch?v=8Zg1-TufF%20zY?x=1&y=2#blabla\" target=\"_blank\">youtube.com/watch?v=8Zg1-TufF%20zY?x=1&y=2#blabla</a> ."
assert Linkify.link(text) == expected assert Linkify.link(text, new_window: true) == expected
# no scheme # no scheme
text = "Hey, check out www.youtube.com/watch?v=8Zg1-TufF%20zY?x=1&y=2#blabla ." text = "Hey, check out www.youtube.com/watch?v=8Zg1-TufF%20zY?x=1&y=2#blabla ."
assert Linkify.link(text) == expected assert Linkify.link(text, new_window: true) == expected
end end
test "turn urls with schema into urls" do test "turn urls with schema into urls" do
text = "📌https://google.com" text = "📌https://google.com"
expected = "📌<a href=\"https://google.com\">google.com</a>" expected = "📌<a href=\"https://google.com\">google.com</a>"
assert Linkify.link(text, class: false, new_window: false, rel: false) == expected assert Linkify.link(text, rel: false) == expected
end end
test "hostname/@user" do test "hostname/@user" do
text = "https://example.com/@user" text = "https://example.com/@user"
expected = expected = "<a href=\"https://example.com/@user\" target=\"_blank\">example.com/@user</a>"
"<a href=\"https://example.com/@user\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">example.com/@user</a>"
assert Linkify.link(text) == expected assert Linkify.link(text, new_window: true) == expected
text = "https://example.com:4000/@user" text = "https://example.com:4000/@user"
expected = expected =
"<a href=\"https://example.com:4000/@user\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">example.com:4000/@user</a>" "<a href=\"https://example.com:4000/@user\" target=\"_blank\">example.com:4000/@user</a>"
assert Linkify.link(text) == expected assert Linkify.link(text, new_window: true) == expected
text = "https://example.com:4000/@user" text = "https://example.com:4000/@user"
expected = expected =
"<a href=\"https://example.com:4000/@user\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">example.com:4000/@user</a>" "<a href=\"https://example.com:4000/@user\" target=\"_blank\">example.com:4000/@user</a>"
assert Linkify.link(text) == expected assert Linkify.link(text, new_window: true) == expected
text = "@username" text = "@username"
expected = "@username" expected = "@username"
assert Linkify.link(text) == expected assert Linkify.link(text, new_window: true) == expected
text = "http://www.cs.vu.nl/~ast/intel/" text = "http://www.cs.vu.nl/~ast/intel/"
expected = expected = "<a href=\"http://www.cs.vu.nl/~ast/intel/\">cs.vu.nl/~ast/intel/</a>"
"<a href=\"http://www.cs.vu.nl/~ast/intel/\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">cs.vu.nl/~ast/intel/</a>"
assert Linkify.link(text) == expected assert Linkify.link(text) == expected
text = "https://forum.zdoom.org/viewtopic.php?f=44&t=57087" text = "https://forum.zdoom.org/viewtopic.php?f=44&t=57087"
expected = expected =
"<a href=\"https://forum.zdoom.org/viewtopic.php?f=44&t=57087\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">forum.zdoom.org/viewtopic.php?f=44&t=57087</a>" "<a href=\"https://forum.zdoom.org/viewtopic.php?f=44&t=57087\">forum.zdoom.org/viewtopic.php?f=44&t=57087</a>"
assert Linkify.link(text) == expected assert Linkify.link(text) == expected
text = "https://en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul" text = "https://en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul"
expected = expected =
"<a href=\"https://en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul</a>" "<a href=\"https://en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul\">en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul</a>"
assert Linkify.link(text) == expected assert Linkify.link(text) == expected
text = "https://en.wikipedia.org/wiki/Duff's_device" text = "https://en.wikipedia.org/wiki/Duff's_device"
expected = expected =
"<a href=\"https://en.wikipedia.org/wiki/Duff's_device\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">en.wikipedia.org/wiki/Duff's_device</a>" "<a href=\"https://en.wikipedia.org/wiki/Duff's_device\">en.wikipedia.org/wiki/Duff's_device</a>"
assert Linkify.link(text) == expected assert Linkify.link(text) == expected
end end
@ -336,14 +318,14 @@ defmodule LinkifyTest do
test "xmpp" do test "xmpp" do
text = "xmpp:user@example.com" text = "xmpp:user@example.com"
expected = "<a href=\"xmpp:user@example.com\" class=\"linkified\">xmpp:user@example.com</a>" expected = "<a href=\"xmpp:user@example.com\">xmpp:user@example.com</a>"
assert Linkify.link(text, extra: true, new_window: false, rel: false) == expected assert Linkify.link(text, extra: true) == expected
end end
test "email" do test "email" do
text = "user@example.com" text = "user@example.com"
expected = "<a href=\"mailto:user@example.com\" class=\"linkified\">user@example.com</a>" expected = "<a href=\"mailto:user@example.com\">user@example.com</a>"
assert Linkify.link(text, email: true) == expected assert Linkify.link(text, email: true) == expected
end end
@ -352,9 +334,9 @@ defmodule LinkifyTest do
"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"
expected = 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\" class=\"linkified\">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>" "<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, new_window: false, rel: false) == expected assert Linkify.link(text, extra: true) == expected
end end
test "dweb" do test "dweb" do
@ -362,9 +344,9 @@ defmodule LinkifyTest do
"dweb://584faa05d394190ab1a3f0240607f9bf2b7e2bd9968830a11cf77db0cea36a21+v1.0.0/path/to/file.txt" "dweb://584faa05d394190ab1a3f0240607f9bf2b7e2bd9968830a11cf77db0cea36a21+v1.0.0/path/to/file.txt"
expected = expected =
"<a href=\"dweb://584faa05d394190ab1a3f0240607f9bf2b7e2bd9968830a11cf77db0cea36a21+v1.0.0/path/to/file.txt\" class=\"linkified\">dweb://584faa05d394190ab1a3f0240607f9bf2b7e2bd9968830a11cf77db0cea36a21+v1.0.0/path/to/file.txt</a>" "<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, new_window: false, rel: false) == expected assert Linkify.link(text, extra: true) == expected
end end
end end
@ -372,8 +354,7 @@ defmodule LinkifyTest do
test "parse with scheme" do test "parse with scheme" do
text = "https://google.com" text = "https://google.com"
expected = expected = "<a href=\"https://google.com\">google.com</a>"
"<a href=\"https://google.com\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">google.com</a>"
assert Linkify.link(text) == expected assert Linkify.link(text) == expected
end end
@ -387,7 +368,7 @@ defmodule LinkifyTest do
text = "this url https://google.foobar.com/ has valid TLD" text = "this url https://google.foobar.com/ has valid TLD"
expected = expected =
"this url <a href=\"https://google.foobar.com/\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">google.foobar.com/</a> has valid TLD" "this url <a href=\"https://google.foobar.com/\">google.foobar.com/</a> has valid TLD"
assert Linkify.link(text) == expected assert Linkify.link(text) == expected
end end
@ -399,7 +380,7 @@ defmodule LinkifyTest do
text = "this url google.foobar.com/ has valid TLD" text = "this url google.foobar.com/ has valid TLD"
expected = expected =
"this url <a href=\"http://google.foobar.com/\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">google.foobar.com/</a> has valid TLD" "this url <a href=\"http://google.foobar.com/\">google.foobar.com/</a> has valid TLD"
assert Linkify.link(text) == expected assert Linkify.link(text) == expected
end end
@ -408,14 +389,14 @@ defmodule LinkifyTest do
text = "this url http://google.foobar.com/ has valid TLD" text = "this url http://google.foobar.com/ has valid TLD"
expected = expected =
"this url <a href=\"http://google.foobar.com/\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">google.foobar.com/</a> has valid TLD" "this url <a href=\"http://google.foobar.com/\">google.foobar.com/</a> has valid TLD"
assert Linkify.link(text) == expected assert Linkify.link(text) == expected
text = "this url google.foobar.com/ has valid TLD" text = "this url google.foobar.com/ has valid TLD"
expected = expected =
"this url <a href=\"http://google.foobar.com/\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">google.foobar.com/</a> has valid TLD" "this url <a href=\"http://google.foobar.com/\">google.foobar.com/</a> has valid TLD"
assert Linkify.link(text) == expected assert Linkify.link(text) == expected
end end

View file

@ -109,9 +109,7 @@ defmodule Linkify.ParserTest do
describe "parse" do describe "parse" do
test "handle line breakes" do test "handle line breakes" do
text = "google.com\r\nssss" text = "google.com\r\nssss"
expected = "<a href=\"http://google.com\">google.com</a>\r\nssss"
expected =
"<a href=\"http://google.com\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">google.com</a>\r\nssss"
assert parse(text) == expected assert parse(text) == expected
end end
@ -141,20 +139,20 @@ defmodule Linkify.ParserTest do
expected = "<div><a href=\"http://google.com\">google.com</a></div>" expected = "<div><a href=\"http://google.com\">google.com</a></div>"
assert parse(text, class: false, rel: false, new_window: false) == expected assert parse(text, class: false, rel: false) == expected
text = "Check out <div class='section'>google.com</div>" text = "Check out <div class='section'>google.com</div>"
expected = 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\">google.com</a></div>"
assert parse(text, class: false, rel: false, new_window: false) == expected assert parse(text, class: false, rel: false) == expected
end end
test "links url inside nested html" do test "links url inside nested html" do
text = "<p><strong>google.com</strong></p>" text = "<p><strong>google.com</strong></p>"
expected = "<p><strong><a href=\"http://google.com\">google.com</a></strong></p>" expected = "<p><strong><a href=\"http://google.com\">google.com</a></strong></p>"
assert parse(text, class: false, rel: false, new_window: false) == expected assert parse(text, class: false, rel: false) == expected
end end
test "do not link parens" do test "do not link parens" do
@ -163,14 +161,14 @@ defmodule Linkify.ParserTest do
expected = expected =
" foo (<a href=\"https://example.com/path/folder/\">example.com/path/folder/</a>), bar" " foo (<a href=\"https://example.com/path/folder/\">example.com/path/folder/</a>), bar"
assert parse(text, class: false, rel: false, new_window: false, scheme: true) == expected assert parse(text, class: false, rel: false, scheme: true) == expected
text = " foo (example.com/path/folder/), bar" text = " foo (example.com/path/folder/), bar"
expected = expected =
" foo (<a href=\"http://example.com/path/folder/\">example.com/path/folder/</a>), bar" " foo (<a href=\"http://example.com/path/folder/\">example.com/path/folder/</a>), bar"
assert parse(text, class: false, rel: false, new_window: false) == expected assert parse(text, class: false, rel: false) == expected
end end
test "do not link urls" do test "do not link urls" do