From 150d9f222f344117a41864eac3042808a7c82f8b Mon Sep 17 00:00:00 2001 From: Egor Kislitsyn Date: Fri, 21 Jun 2019 18:09:52 +0700 Subject: [PATCH] Remove `scheme` setting --- lib/auto_linker.ex | 1 - lib/auto_linker/parser.ex | 40 +++++++++++++------------------------- test/auto_linker_test.exs | 41 +++++++++++++++++---------------------- 3 files changed, 31 insertions(+), 51 deletions(-) diff --git a/lib/auto_linker.ex b/lib/auto_linker.ex index b4b98a7..b843a8d 100644 --- a/lib/auto_linker.ex +++ b/lib/auto_linker.ex @@ -27,7 +27,6 @@ defmodule AutoLinker do * `class: "auto-linker"` - specify the class to be added to the generated link. false to clear * `rel: "noopener noreferrer"` - override the rel attribute. false to clear * `new_window: true` - set to false to remove `target='_blank'` attribute - * `scheme: false` - Set to true to link urls with schema `http://google` * `truncate: false` - Set to a number to truncate urls longer then the number. Truncated urls will end in `..` * `strip_prefix: true` - Strip the scheme prefix * `exclude_class: false` - Set to a class name when you don't want urls auto linked in the html of the give class diff --git a/lib/auto_linker/parser.ex b/lib/auto_linker/parser.ex index eba051a..006258d 100644 --- a/lib/auto_linker/parser.ex +++ b/lib/auto_linker/parser.ex @@ -7,13 +7,11 @@ defmodule AutoLinker.Parser do @invalid_url ~r/(\.\.+)|(^(\d+\.){1,2}\d+$)/ - @match_url ~r{^[\w\.-]+(?:\.[\w\.-]+)+[\w\-\._~%:/?#[\]@!\$&'\(\)\*\+,;=.]+$} - - @match_scheme ~r{^(?:\W*)?(?(?:https?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~%:\/?#[\]@!\$&'\(\)\*\+,;=.]+$)}u + @match_url ~r{^(?:\W*)?(?(?:https?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~%:\/?#[\]@!\$&'\(\)\*\+,;=.]+$)}u @match_phone ~r"((?:x\d{2,7})|(?:(?:\+?1\s?(?:[.-]\s?)?)?(?:\(\s?(?:[2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s?\)|(?:[2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s?(?:[.-]\s?)?)(?:[2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s?(?:[.-]\s?)?(?:[0-9]{4}))" - @match_hostname ~r{^(?:\W*https?:\/\/)?(?:[^@\n]+\\w@)?(?[^:#~\/\n?]+)}u + @match_hostname ~r{^\W*(?https?:\/\/)?(?:[^@\n]+\\w@)?(?[^:#~\/\n?]+)}u @match_ip ~r"^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$" @@ -256,7 +254,7 @@ defmodule AutoLinker.Parser do str = strip_parens(buffer) if url?(str, opts) do - case parse_link(str, opts) do + case @match_url |> Regex.run(str, capture: [:url]) |> hd() do ^buffer -> link_url(buffer, opts) url -> String.replace(buffer, url, link_url(url, opts)) end @@ -265,12 +263,6 @@ defmodule AutoLinker.Parser do end end - defp parse_link(str, %{scheme: true}) do - @match_scheme |> Regex.run(str, capture: [:url]) |> hd() - end - - defp parse_link(str, _), do: str - defp strip_parens("(" <> buffer) do ~r/[^\)]*/ |> Regex.run(buffer) |> hd() end @@ -310,11 +302,7 @@ defmodule AutoLinker.Parser do # @doc false def url?(buffer, opts) do - if opts[:scheme] do - valid_url?(buffer) && Regex.match?(@match_scheme, buffer) && valid_tld?(buffer, opts) - else - valid_url?(buffer) && Regex.match?(@match_url, buffer) && valid_tld?(buffer, opts) - end + valid_url?(buffer) && Regex.match?(@match_url, buffer) && valid_tld?(buffer, opts) end def email?(buffer, opts) do @@ -324,24 +312,22 @@ defmodule AutoLinker.Parser do defp valid_url?(url), do: !Regex.match?(@invalid_url, url) def valid_tld?(buffer, opts) do + [scheme, host] = Regex.run(@match_hostname, buffer, capture: [:scheme, :host]) + cond do opts[:validate_tld] == false -> true - opts[:validate_tld] == :no_scheme && opts[:scheme] -> + ip?(host) -> + true + + # don't validate if scheme is present + opts[:validate_tld] == :no_scheme and scheme != "" -> true true -> - with [host] <- Regex.run(@match_hostname, buffer, capture: [:host]) do - if ip?(host) do - true - else - tld = host |> String.split(".") |> List.last() - MapSet.member?(@tlds, tld) - end - else - _ -> false - end + tld = host |> String.split(".") |> List.last() + MapSet.member?(@tlds, tld) end end diff --git a/test/auto_linker_test.exs b/test/auto_linker_test.exs index 9baf243..a21f009 100644 --- a/test/auto_linker_test.exs +++ b/test/auto_linker_test.exs @@ -26,7 +26,6 @@ defmodule AutoLinkerTest do assert AutoLinker.link(text, phone: true, email: true, - scheme: true, extra: true, class: false, new_window: false, @@ -238,7 +237,6 @@ defmodule AutoLinkerTest do "google.com#test #test google.com/#test #tag" assert AutoLinker.link(text, - scheme: true, hashtag: true, class: false, new_window: false, @@ -254,7 +252,6 @@ defmodule AutoLinkerTest do "#漢字 #は #тест #ทดสอบ" assert AutoLinker.link(text, - scheme: true, class: false, new_window: false, rel: false, @@ -271,19 +268,18 @@ defmodule AutoLinkerTest do expected = "Hey, check out youtube.com/watch?v=8Zg1-TufF%20zY?x=1&y=2#blabla ." - assert AutoLinker.link(text, scheme: true) == expected + assert AutoLinker.link(text) == expected # no scheme text = "Hey, check out www.youtube.com/watch?v=8Zg1-TufF%20zY?x=1&y=2#blabla ." - assert AutoLinker.link(text, scheme: true) == expected + assert AutoLinker.link(text) == expected end test "turn urls with schema into urls" do text = "📌https://google.com" expected = "📌google.com" - assert AutoLinker.link(text, scheme: true, class: false, new_window: false, rel: false) == - expected + assert AutoLinker.link(text, class: false, new_window: false, rel: false) == expected end test "hostname/@user" do @@ -292,53 +288,53 @@ defmodule AutoLinkerTest do expected = "example.com/@user" - assert AutoLinker.link(text, scheme: true) == expected + assert AutoLinker.link(text) == expected text = "https://example.com:4000/@user" expected = "example.com:4000/@user" - assert AutoLinker.link(text, scheme: true) == expected + assert AutoLinker.link(text) == expected text = "https://example.com:4000/@user" expected = "example.com:4000/@user" - assert AutoLinker.link(text, scheme: true) == expected + assert AutoLinker.link(text) == expected text = "@username" expected = "@username" - assert AutoLinker.link(text, scheme: true) == expected + assert AutoLinker.link(text) == expected text = "http://www.cs.vu.nl/~ast/intel/" expected = "cs.vu.nl/~ast/intel/" - assert AutoLinker.link(text, scheme: true) == expected + assert AutoLinker.link(text) == expected text = "https://forum.zdoom.org/viewtopic.php?f=44&t=57087" expected = "forum.zdoom.org/viewtopic.php?f=44&t=57087" - assert AutoLinker.link(text, scheme: true) == expected + assert AutoLinker.link(text) == expected text = "https://en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul" expected = "en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul" - assert AutoLinker.link(text, scheme: true) == expected + assert AutoLinker.link(text) == expected text = "https://en.wikipedia.org/wiki/Duff's_device" expected = "en.wikipedia.org/wiki/Duff's_device" - assert AutoLinker.link(text, scheme: true) == expected + assert AutoLinker.link(text) == expected end end @@ -386,34 +382,33 @@ defmodule AutoLinkerTest do expected = "google.com" - assert AutoLinker.link(text, scheme: true) == expected + assert AutoLinker.link(text) == expected end test "only existing TLDs with scheme" do text = "this url https://google.foobar.blah11blah/ has invalid TLD" expected = "this url https://google.foobar.blah11blah/ has invalid TLD" - assert AutoLinker.link(text, scheme: true) == expected + assert AutoLinker.link(text) == expected text = "this url https://google.foobar.com/ has valid TLD" expected = "this url google.foobar.com/ has valid TLD" - assert AutoLinker.link(text, scheme: true) == expected + assert AutoLinker.link(text) == expected end test "only existing TLDs without scheme" do text = "this url google.foobar.blah11blah/ has invalid TLD" - expected = "this url google.foobar.blah11blah/ has invalid TLD" - assert AutoLinker.link(text, scheme: false) == expected + assert AutoLinker.link(text) == text text = "this url google.foobar.com/ has valid TLD" expected = "this url google.foobar.com/ has valid TLD" - assert AutoLinker.link(text, scheme: false) == expected + assert AutoLinker.link(text) == expected end test "only existing TLDs with and without scheme" do @@ -422,14 +417,14 @@ defmodule AutoLinkerTest do expected = "this url google.foobar.com/ has valid TLD" - assert AutoLinker.link(text, scheme: true) == expected + assert AutoLinker.link(text) == expected text = "this url google.foobar.com/ has valid TLD" expected = "this url google.foobar.com/ has valid TLD" - assert AutoLinker.link(text, scheme: true) == expected + assert AutoLinker.link(text) == expected end end end