commit: 70cbfdc84eaa0ba478c396f8af581b25b50be5f3
parent 1a341fdac33211f5eae1ad6e645fe1b9f39834bc
Author: rinpatch <rinpatch@sdf.org>
Date: Tue, 18 Jun 2019 13:25:44 +0300
Extend email? to utilize validate_tld
Diffstat:
3 files changed, 39 insertions(+), 5 deletions(-)
diff --git a/lib/auto_linker.ex b/lib/auto_linker.ex
@@ -48,7 +48,7 @@ defmodule AutoLinker do
* `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
* `extra: false` - link urls with rarely used schemes (magnet, ipfs, irc, etc.)
- * `validate_tld: true` - Set to false to disable TLD validation for urls, 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 `:auto_linker`'s configuration. For example:
diff --git a/lib/auto_linker/parser.ex b/lib/auto_linker/parser.ex
@@ -285,7 +285,7 @@ defmodule AutoLinker.Parser do
defp strip_parens(buffer), do: buffer
def check_and_link_email(buffer, opts, _user_acc) do
- if email?(buffer), do: link_email(buffer, opts), else: buffer
+ if email?(buffer, opts), do: link_email(buffer, opts), else: buffer
end
def check_and_link_phone(buffer, opts, _user_acc) do
@@ -307,7 +307,7 @@ defmodule AutoLinker.Parser do
end
def check_and_link_extra("xmpp:" <> handle, opts, _user_acc) do
- if email?(handle), do: link_extra("xmpp:" <> handle, opts), else: handle
+ if email?(handle, opts), do: link_extra("xmpp:" <> handle, opts), else: handle
end
def check_and_link_extra(buffer, opts, _user_acc) do
@@ -324,8 +324,8 @@ defmodule AutoLinker.Parser do
end
end
- def email?(buffer) do
- valid_url?(buffer) && Regex.match?(@match_email, buffer) && valid_tld?(buffer, [])
+ def email?(buffer, opts) do
+ valid_url?(buffer) && Regex.match?(@match_email, buffer) && valid_tld?(buffer, opts)
end
defp valid_url?(url), do: !Regex.match?(@invalid_url, url)
diff --git a/test/parser_test.exs b/test/parser_test.exs
@@ -76,6 +76,36 @@ defmodule AutoLinker.ParserTest do
end
end
+ describe "email?" do
+ test "identifies valid emails" do
+ valid_emails()
+ |> Enum.each(fn email ->
+ assert email?(email, [])
+ end)
+ end
+
+ test "identifies invalid emails" do
+ invalid_emails()
+ |> Enum.each(fn email ->
+ 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 "validates tlds when validate_tld: true" do
+ valid_custom_tld_emails()
+ |> Enum.each(fn email ->
+ refute email?(email, validate_tld: true)
+ end)
+ end
+ end
+
describe "match_phone" do
test "valid" do
valid_phone_nunbers()
@@ -274,4 +304,8 @@ defmodule AutoLinker.ParserTest do
"pleroma.i2p/test",
"misskey.loki"
]
+
+ def valid_emails, do: ["rms@ai.mit.edu", "vc@cock.li"]
+ def invalid_emails, do: ["rms[at]ai.mit.edu", "vc@cock", "xmpp:lain@trashserver.net"]
+ def valid_custom_tld_emails, do: ["guardian@33y6fjyhs3phzfjj.onion", "hi@company.null"]
end