Fix invalid IPv4 linking; optimize

thx @href !
This commit is contained in:
Mark Felder 2020-12-19 13:20:11 -06:00
parent 33558484a6
commit 26fec6cff1
2 changed files with 11 additions and 17 deletions

View file

@ -258,23 +258,9 @@ defmodule Linkify.Parser do
end
def ip?(buffer) do
v4 = String.split(buffer, ".")
v6 =
buffer
|> String.trim_leading("[")
|> String.trim_trailing("]")
|> String.split(":", trim: true)
cond do
length(v4) == 4 ->
!Enum.any?(v4, fn x -> safe_to_integer(x, 10) not in 0..255 end)
length(v6) in 1..8 ->
!Enum.any?(v4, fn x -> safe_to_integer(x, 16) not in 0..0xFFFF end)
false ->
false
case :inet.parse_strict_address(to_charlist(buffer)) do
{:error, _} -> false
{:ok, _} -> true
end
end

View file

@ -760,5 +760,13 @@ defmodule LinkifyTest do
assert Linkify.link(text) == expected
end
test "IPv4 and IPv6 are linked" do
text = "1.1.1.1"
expected = "<a href=\"http://1.1.1.1\">1.1.1.1</a>"
assert Linkify.link(text) == expected
end
end
end