Merge branch 'iodata' into 'master'
Support returning result as iodata and as safe iodata Closes #20 See merge request pleroma/elixir-libraries/linkify!23
This commit is contained in:
commit
4764e1819e
4 changed files with 194 additions and 80 deletions
|
@ -40,11 +40,20 @@ defmodule Linkify do
|
||||||
* `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)
|
||||||
|
* `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)
|
||||||
"""
|
"""
|
||||||
def link(text, opts \\ []) do
|
def link(text, opts \\ []) do
|
||||||
parse(text, opts)
|
parse(text, opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def link_to_iodata(text, opts \\ []) do
|
||||||
|
parse(text, Keyword.merge(opts, iodata: true))
|
||||||
|
end
|
||||||
|
|
||||||
|
def link_safe(text, opts \\ []) do
|
||||||
|
parse(text, Keyword.merge(opts, iodata: :safe))
|
||||||
|
end
|
||||||
|
|
||||||
def link_map(text, acc, opts \\ []) do
|
def link_map(text, acc, opts \\ []) do
|
||||||
parse({text, acc}, opts)
|
parse({text, acc}, opts)
|
||||||
end
|
end
|
||||||
|
|
|
@ -56,8 +56,9 @@ defmodule Linkify.Builder do
|
||||||
|> strip_prefix(Map.get(opts, :strip_prefix, false))
|
|> strip_prefix(Map.get(opts, :strip_prefix, false))
|
||||||
|> truncate(Map.get(opts, :truncate, false))
|
|> truncate(Map.get(opts, :truncate, false))
|
||||||
|
|
||||||
attrs = format_attrs(attrs)
|
attrs
|
||||||
"<a #{attrs}>#{url}</a>"
|
|> format_attrs()
|
||||||
|
|> format_tag(url, opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp format_attrs(attrs) do
|
defp format_attrs(attrs) do
|
||||||
|
@ -123,23 +124,39 @@ defmodule Linkify.Builder do
|
||||||
|> format_extra(uri, opts)
|
|> format_extra(uri, opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
def format_mention(attrs, name, _opts) do
|
def format_mention(attrs, name, opts) do
|
||||||
attrs = format_attrs(attrs)
|
attrs
|
||||||
"<a #{attrs}>@#{name}</a>"
|
|> format_attrs()
|
||||||
|
|> format_tag("@#{name}", opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
def format_hashtag(attrs, tag, _opts) do
|
def format_hashtag(attrs, tag, opts) do
|
||||||
attrs = format_attrs(attrs)
|
attrs
|
||||||
"<a #{attrs}>##{tag}</a>"
|
|> format_attrs()
|
||||||
|
|> format_tag("##{tag}", opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
def format_email(attrs, email, _opts) do
|
def format_email(attrs, email, opts) do
|
||||||
attrs = format_attrs(attrs)
|
attrs
|
||||||
~s(<a #{attrs}>#{email}</a>)
|
|> format_attrs()
|
||||||
|
|> format_tag(email, opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
def format_extra(attrs, uri, _opts) do
|
def format_extra(attrs, uri, opts) do
|
||||||
attrs = format_attrs(attrs)
|
attrs
|
||||||
~s(<a #{attrs}>#{uri}</a>)
|
|> format_attrs()
|
||||||
|
|> format_tag(uri, opts)
|
||||||
|
end
|
||||||
|
|
||||||
|
def format_tag(attrs, content, %{iodata: true}) do
|
||||||
|
["<a ", attrs, ">", content, "</a>"]
|
||||||
|
end
|
||||||
|
|
||||||
|
def format_tag(attrs, content, %{iodata: :safe}) do
|
||||||
|
[{:safe, ["<a ", attrs, ">"]}, content, {:safe, "</a>"}]
|
||||||
|
end
|
||||||
|
|
||||||
|
def format_tag(attrs, content, _opts) do
|
||||||
|
"<a #{attrs}>#{content}</a>"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -62,135 +62,139 @@ defmodule Linkify.Parser do
|
||||||
|
|
||||||
def parse(input, opts) do
|
def parse(input, opts) do
|
||||||
opts = Map.merge(@default_opts, opts)
|
opts = Map.merge(@default_opts, opts)
|
||||||
opts_list = Map.to_list(opts)
|
|
||||||
|
|
||||||
Enum.reduce(@types, input, fn
|
{buffer, user_acc} = do_parse(input, opts, {"", [], :parsing})
|
||||||
type, input ->
|
|
||||||
if {type, true} in opts_list do
|
if opts[:iodata] do
|
||||||
do_parse(input, opts, {"", "", :parsing}, type)
|
{buffer, user_acc}
|
||||||
else
|
else
|
||||||
input
|
{IO.iodata_to_binary(buffer), user_acc}
|
||||||
end
|
end
|
||||||
end)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
defp do_parse({"", user_acc}, _opts, {"", acc, _}, _handler),
|
defp accumulate(acc, buffer),
|
||||||
do: {acc, user_acc}
|
do: [buffer | acc]
|
||||||
|
|
||||||
defp do_parse({"@" <> text, user_acc}, opts, {buffer, acc, :skip}, type),
|
defp accumulate(acc, buffer, trailing),
|
||||||
do: do_parse({text, user_acc}, opts, {"", acc <> buffer <> "@", :skip}, type)
|
do: [trailing, buffer | acc]
|
||||||
|
|
||||||
defp do_parse({"<a" <> text, user_acc}, opts, {buffer, acc, :parsing}, type),
|
defp do_parse({"", user_acc}, _opts, {"", acc, _}),
|
||||||
do: do_parse({text, user_acc}, opts, {"", acc <> buffer <> "<a", :skip}, type)
|
do: {Enum.reverse(acc), user_acc}
|
||||||
|
|
||||||
defp do_parse({"<pre" <> text, user_acc}, opts, {buffer, acc, :parsing}, type),
|
defp do_parse({"@" <> text, user_acc}, opts, {buffer, acc, :skip}),
|
||||||
do: do_parse({text, user_acc}, opts, {"", acc <> buffer <> "<pre", :skip}, type)
|
do: do_parse({text, user_acc}, opts, {"", accumulate(acc, buffer, "@"), :skip})
|
||||||
|
|
||||||
defp do_parse({"<code" <> text, user_acc}, opts, {buffer, acc, :parsing}, type),
|
defp do_parse({"<a" <> text, user_acc}, opts, {buffer, acc, :parsing}),
|
||||||
do: do_parse({text, user_acc}, opts, {"", acc <> buffer <> "<code", :skip}, type)
|
do: do_parse({text, user_acc}, opts, {"", accumulate(acc, buffer, "<a"), :skip})
|
||||||
|
|
||||||
defp do_parse({"</a>" <> text, user_acc}, opts, {buffer, acc, :skip}, type),
|
defp do_parse({"<pre" <> text, user_acc}, opts, {buffer, acc, :parsing}),
|
||||||
do: do_parse({text, user_acc}, opts, {"", acc <> buffer <> "</a>", :parsing}, type)
|
do: do_parse({text, user_acc}, opts, {"", accumulate(acc, buffer, "<pre"), :skip})
|
||||||
|
|
||||||
defp do_parse({"</pre>" <> text, user_acc}, opts, {buffer, acc, :skip}, type),
|
defp do_parse({"<code" <> text, user_acc}, opts, {buffer, acc, :parsing}),
|
||||||
do: do_parse({text, user_acc}, opts, {"", acc <> buffer <> "</pre>", :parsing}, type)
|
do: do_parse({text, user_acc}, opts, {"", accumulate(acc, buffer, "<code"), :skip})
|
||||||
|
|
||||||
defp do_parse({"</code>" <> text, user_acc}, opts, {buffer, acc, :skip}, type),
|
defp do_parse({"</a>" <> text, user_acc}, opts, {buffer, acc, :skip}),
|
||||||
do: do_parse({text, user_acc}, opts, {"", acc <> buffer <> "</code>", :parsing}, type)
|
do: do_parse({text, user_acc}, opts, {"", accumulate(acc, buffer, "</a>"), :parsing})
|
||||||
|
|
||||||
defp do_parse({"<" <> text, user_acc}, opts, {"", acc, :parsing}, type),
|
defp do_parse({"</pre>" <> text, user_acc}, opts, {buffer, acc, :skip}),
|
||||||
do: do_parse({text, user_acc}, opts, {"<", acc, {:open, 1}}, type)
|
do: do_parse({text, user_acc}, opts, {"", accumulate(acc, buffer, "</pre>"), :parsing})
|
||||||
|
|
||||||
defp do_parse({"<" <> text, user_acc}, opts, {"", acc, {:html, level}}, type) do
|
defp do_parse({"</code>" <> text, user_acc}, opts, {buffer, acc, :skip}),
|
||||||
do_parse({text, user_acc}, opts, {"<", acc, {:open, level + 1}}, type)
|
do: do_parse({text, user_acc}, opts, {"", accumulate(acc, buffer, "</code>"), :parsing})
|
||||||
|
|
||||||
|
defp do_parse({"<" <> text, user_acc}, opts, {"", acc, :parsing}),
|
||||||
|
do: do_parse({text, user_acc}, opts, {"<", acc, {:open, 1}})
|
||||||
|
|
||||||
|
defp do_parse({"<" <> text, user_acc}, opts, {"", acc, {:html, level}}) do
|
||||||
|
do_parse({text, user_acc}, opts, {"<", acc, {:open, level + 1}})
|
||||||
end
|
end
|
||||||
|
|
||||||
defp do_parse({">" <> text, user_acc}, opts, {buffer, acc, {:attrs, level}}, type),
|
defp do_parse({">" <> text, user_acc}, opts, {buffer, acc, {:attrs, level}}),
|
||||||
do:
|
do:
|
||||||
do_parse(
|
do_parse(
|
||||||
{text, user_acc},
|
{text, user_acc},
|
||||||
opts,
|
opts,
|
||||||
{"", acc <> buffer <> ">", {:html, level}},
|
{"", accumulate(acc, buffer, ">"), {:html, level}}
|
||||||
type
|
|
||||||
)
|
)
|
||||||
|
|
||||||
defp do_parse({<<ch::8>> <> text, user_acc}, opts, {"", acc, {:attrs, level}}, type) do
|
defp do_parse({<<ch::8>> <> text, user_acc}, opts, {"", acc, {:attrs, level}}) do
|
||||||
do_parse({text, user_acc}, opts, {"", acc <> <<ch::8>>, {:attrs, level}}, type)
|
do_parse({text, user_acc}, opts, {"", accumulate(acc, <<ch::8>>), {:attrs, level}})
|
||||||
end
|
end
|
||||||
|
|
||||||
defp do_parse({"</" <> text, user_acc}, opts, {buffer, acc, {:html, level}}, type) do
|
defp do_parse({"</" <> text, user_acc}, opts, {buffer, acc, {:html, level}}) do
|
||||||
{buffer, user_acc} = link(type, buffer, opts, user_acc)
|
{buffer, user_acc} = link(buffer, opts, user_acc)
|
||||||
|
|
||||||
do_parse(
|
do_parse(
|
||||||
{text, user_acc},
|
{text, user_acc},
|
||||||
opts,
|
opts,
|
||||||
{"", acc <> buffer <> "</", {:close, level}},
|
{"", accumulate(acc, buffer, "</"), {:close, level}}
|
||||||
type
|
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp do_parse({">" <> text, user_acc}, opts, {buffer, acc, {:close, 1}}, type),
|
defp do_parse({">" <> text, user_acc}, opts, {buffer, acc, {:close, 1}}),
|
||||||
do: do_parse({text, user_acc}, opts, {"", acc <> buffer <> ">", :parsing}, type)
|
do: do_parse({text, user_acc}, opts, {"", accumulate(acc, buffer, ">"), :parsing})
|
||||||
|
|
||||||
defp do_parse({">" <> text, user_acc}, opts, {buffer, acc, {:close, level}}, type),
|
defp do_parse({">" <> text, user_acc}, opts, {buffer, acc, {:close, level}}),
|
||||||
do:
|
do:
|
||||||
do_parse(
|
do_parse(
|
||||||
{text, user_acc},
|
{text, user_acc},
|
||||||
opts,
|
opts,
|
||||||
{"", acc <> buffer <> ">", {:html, level - 1}},
|
{"", accumulate(acc, buffer, ">"), {:html, level - 1}}
|
||||||
type
|
|
||||||
)
|
)
|
||||||
|
|
||||||
defp do_parse({text, user_acc}, opts, {buffer, acc, {:open, level}}, type) do
|
defp do_parse({text, user_acc}, opts, {buffer, acc, {:open, level}}) do
|
||||||
do_parse({text, user_acc}, opts, {"", acc <> buffer, {:attrs, level}}, type)
|
do_parse({text, user_acc}, opts, {"", accumulate(acc, buffer), {:attrs, level}})
|
||||||
end
|
end
|
||||||
|
|
||||||
defp do_parse(
|
defp do_parse(
|
||||||
{<<char::bytes-size(1), text::binary>>, user_acc},
|
{<<char::bytes-size(1), text::binary>>, user_acc},
|
||||||
opts,
|
opts,
|
||||||
{buffer, acc, state},
|
{buffer, acc, state}
|
||||||
type
|
|
||||||
)
|
)
|
||||||
when char in [" ", "\r", "\n"] do
|
when char in [" ", "\r", "\n"] do
|
||||||
{buffer, user_acc} = link(type, buffer, opts, user_acc)
|
{buffer, user_acc} = link(buffer, opts, user_acc)
|
||||||
|
|
||||||
do_parse(
|
do_parse(
|
||||||
{text, user_acc},
|
{text, user_acc},
|
||||||
opts,
|
opts,
|
||||||
{"", acc <> buffer <> char, state},
|
{"", accumulate(acc, buffer, char), state}
|
||||||
type
|
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp do_parse({<<ch::8>>, user_acc}, opts, {buffer, acc, state}, type) do
|
defp do_parse({<<ch::8>>, user_acc}, opts, {buffer, acc, state}) do
|
||||||
{buffer, user_acc} = link(type, buffer <> <<ch::8>>, opts, user_acc)
|
{buffer, user_acc} = link(buffer <> <<ch::8>>, opts, user_acc)
|
||||||
|
|
||||||
do_parse(
|
do_parse(
|
||||||
{"", user_acc},
|
{"", user_acc},
|
||||||
opts,
|
opts,
|
||||||
{"", acc <> buffer, state},
|
{"", accumulate(acc, buffer), state}
|
||||||
type
|
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp do_parse({<<ch::8>> <> text, user_acc}, opts, {buffer, acc, state}, type),
|
defp do_parse({<<ch::8>> <> text, user_acc}, opts, {buffer, acc, state}),
|
||||||
do: do_parse({text, user_acc}, opts, {buffer <> <<ch::8>>, acc, state}, type)
|
do: do_parse({text, user_acc}, opts, {buffer <> <<ch::8>>, acc, state})
|
||||||
|
|
||||||
def check_and_link(:url, buffer, opts, _user_acc) do
|
def check_and_link(:url, buffer, opts, _user_acc) do
|
||||||
str = strip_parens(buffer)
|
str = strip_parens(buffer)
|
||||||
|
|
||||||
if url?(str, opts) do
|
if url?(str, opts) do
|
||||||
case @match_url |> Regex.run(str, capture: [:url]) |> hd() do
|
case @match_url |> Regex.run(str, capture: [:url]) |> hd() do
|
||||||
^buffer -> link_url(buffer, opts)
|
^buffer ->
|
||||||
url -> String.replace(buffer, url, link_url(url, opts))
|
link_url(buffer, opts)
|
||||||
|
|
||||||
|
url ->
|
||||||
|
buffer
|
||||||
|
|> String.split(url)
|
||||||
|
|> Enum.intersperse(link_url(url, opts))
|
||||||
|
|> if(opts[:iodata], do: & &1, else: &Enum.join(&1)).()
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
buffer
|
:nomatch
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def check_and_link(:email, buffer, opts, _user_acc) do
|
def check_and_link(:email, buffer, opts, _user_acc) do
|
||||||
if email?(buffer, opts), do: link_email(buffer, opts), else: buffer
|
if email?(buffer, opts), do: link_email(buffer, opts), else: :nomatch
|
||||||
end
|
end
|
||||||
|
|
||||||
def check_and_link(:mention, buffer, opts, user_acc) do
|
def check_and_link(:mention, buffer, opts, user_acc) do
|
||||||
|
@ -210,7 +214,7 @@ defmodule Linkify.Parser do
|
||||||
end
|
end
|
||||||
|
|
||||||
def check_and_link(:extra, buffer, opts, _user_acc) do
|
def check_and_link(:extra, buffer, opts, _user_acc) do
|
||||||
if String.starts_with?(buffer, @prefix_extra), do: link_extra(buffer, opts), else: buffer
|
if String.starts_with?(buffer, @prefix_extra), do: link_extra(buffer, opts), else: :nomatch
|
||||||
end
|
end
|
||||||
|
|
||||||
defp strip_parens("(" <> buffer) do
|
defp strip_parens("(" <> buffer) do
|
||||||
|
@ -272,7 +276,7 @@ defmodule Linkify.Parser do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def link_hashtag(nil, buffer, _, _user_acc), do: buffer
|
def link_hashtag(nil, _buffer, _, _user_acc), do: :nomatch
|
||||||
|
|
||||||
def link_hashtag(hashtag, buffer, %{hashtag_handler: hashtag_handler} = opts, user_acc) do
|
def link_hashtag(hashtag, buffer, %{hashtag_handler: hashtag_handler} = opts, user_acc) do
|
||||||
hashtag
|
hashtag
|
||||||
|
@ -286,7 +290,7 @@ defmodule Linkify.Parser do
|
||||||
|> maybe_update_buffer(hashtag, buffer)
|
|> maybe_update_buffer(hashtag, buffer)
|
||||||
end
|
end
|
||||||
|
|
||||||
def link_mention(nil, buffer, _, user_acc), do: {buffer, user_acc}
|
def link_mention(nil, _buffer, _, _user_acc), do: :nomatch
|
||||||
|
|
||||||
def link_mention(mention, buffer, %{mention_handler: mention_handler} = opts, user_acc) do
|
def link_mention(mention, buffer, %{mention_handler: mention_handler} = opts, user_acc) do
|
||||||
mention
|
mention
|
||||||
|
@ -326,10 +330,21 @@ defmodule Linkify.Parser do
|
||||||
Builder.create_extra_link(buffer, opts)
|
Builder.create_extra_link(buffer, opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp link(type, buffer, opts, user_acc) do
|
defp link(buffer, opts, user_acc) do
|
||||||
|
Enum.reduce_while(@types, {buffer, user_acc}, fn type, _ ->
|
||||||
|
if opts[type] == true do
|
||||||
|
check_and_link_reducer(type, buffer, opts, user_acc)
|
||||||
|
else
|
||||||
|
{:cont, {buffer, user_acc}}
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp check_and_link_reducer(type, buffer, opts, user_acc) do
|
||||||
case check_and_link(type, buffer, opts, user_acc) do
|
case check_and_link(type, buffer, opts, user_acc) do
|
||||||
{buffer, user_acc} -> {buffer, user_acc}
|
:nomatch -> {:cont, {buffer, user_acc}}
|
||||||
buffer -> {buffer, user_acc}
|
{buffer, user_acc} -> {:halt, {buffer, user_acc}}
|
||||||
|
buffer -> {:halt, {buffer, user_acc}}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -7,6 +7,22 @@ defmodule LinkifyTest do
|
||||||
"<a href=\"http://google.com\">google.com</a>"
|
"<a href=\"http://google.com\">google.com</a>"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "default link iodata" do
|
||||||
|
assert Linkify.link_to_iodata("google.com") ==
|
||||||
|
[["<a ", "href=\"http://google.com\"", ">", "google.com", "</a>"]]
|
||||||
|
end
|
||||||
|
|
||||||
|
test "default link safe iodata" do
|
||||||
|
assert Linkify.link_safe("google.com") ==
|
||||||
|
[
|
||||||
|
[
|
||||||
|
{:safe, ["<a ", "href=\"http://google.com\"", ">"]},
|
||||||
|
"google.com",
|
||||||
|
{:safe, "</a>"}
|
||||||
|
]
|
||||||
|
]
|
||||||
|
end
|
||||||
|
|
||||||
test "does on link existing links" do
|
test "does on link existing links" do
|
||||||
text = ~s(<a href="http://google.com">google.com</a>)
|
text = ~s(<a href="http://google.com">google.com</a>)
|
||||||
assert Linkify.link(text) == text
|
assert Linkify.link(text) == text
|
||||||
|
@ -24,16 +40,63 @@ defmodule LinkifyTest do
|
||||||
) == expected
|
) == expected
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "all kinds of links iodata" 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>"]
|
||||||
|
]
|
||||||
|
|
||||||
|
assert Linkify.link_to_iodata(text,
|
||||||
|
email: true,
|
||||||
|
extra: true
|
||||||
|
) == expected
|
||||||
|
end
|
||||||
|
|
||||||
test "class attribute" do
|
test "class attribute" do
|
||||||
assert Linkify.link("google.com", class: "linkified") ==
|
assert 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>"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "class attribute iodata" do
|
||||||
|
assert Linkify.link_to_iodata("google.com", class: "linkified") ==
|
||||||
|
[
|
||||||
|
[
|
||||||
|
"<a ",
|
||||||
|
"href=\"http://google.com\" class=\"linkified\"",
|
||||||
|
">",
|
||||||
|
"google.com",
|
||||||
|
"</a>"
|
||||||
|
]
|
||||||
|
]
|
||||||
|
end
|
||||||
|
|
||||||
test "rel attribute" do
|
test "rel attribute" do
|
||||||
assert Linkify.link("google.com", rel: "noopener noreferrer") ==
|
assert Linkify.link("google.com", rel: "noopener noreferrer") ==
|
||||||
"<a href=\"http://google.com\" rel=\"noopener noreferrer\">google.com</a>"
|
"<a href=\"http://google.com\" rel=\"noopener noreferrer\">google.com</a>"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "rel attribute iodata" do
|
||||||
|
assert Linkify.link_to_iodata("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"
|
||||||
|
|
||||||
|
@ -54,6 +117,16 @@ defmodule LinkifyTest do
|
||||||
assert Linkify.link(text, rel: custom_rel) == expected
|
assert Linkify.link(text, rel: custom_rel) == expected
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "strip parens" do
|
||||||
|
assert Linkify.link("(google.com)") ==
|
||||||
|
"(<a href=\"http://google.com\">google.com</a>)"
|
||||||
|
end
|
||||||
|
|
||||||
|
test "strip parens iodata" do
|
||||||
|
assert Linkify.link_to_iodata("(google.com)") ==
|
||||||
|
[["(", ["<a ", "href=\"http://google.com\"", ">", "google.com", "</a>"], ")"]]
|
||||||
|
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\">google.com</a>", []}
|
{"<a href=\"http://google.com\">google.com</a>", []}
|
||||||
|
|
Reference in a new issue