Fix invalid IPv4 linking; optimize
thx @href !
This commit is contained in:
parent
33558484a6
commit
26fec6cff1
2 changed files with 11 additions and 17 deletions
|
@ -258,23 +258,9 @@ defmodule Linkify.Parser do
|
||||||
end
|
end
|
||||||
|
|
||||||
def ip?(buffer) do
|
def ip?(buffer) do
|
||||||
v4 = String.split(buffer, ".")
|
case :inet.parse_strict_address(to_charlist(buffer)) do
|
||||||
|
{:error, _} -> false
|
||||||
v6 =
|
{:ok, _} -> true
|
||||||
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
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -760,5 +760,13 @@ defmodule LinkifyTest do
|
||||||
|
|
||||||
assert Linkify.link(text) == expected
|
assert Linkify.link(text) == expected
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
Reference in a new issue