From 0e6869c9ea36d81bdb12356c0a36debb2e4a0181 Mon Sep 17 00:00:00 2001 From: Egor Kislitsyn Date: Wed, 1 May 2019 14:48:04 +0700 Subject: [PATCH] Fix tld validation --- lib/auto_linker/parser.ex | 14 ++++++++------ test/parser_test.exs | 13 +++++++++++++ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/lib/auto_linker/parser.ex b/lib/auto_linker/parser.ex index b897dd6..c3ecc3c 100644 --- a/lib/auto_linker/parser.ex +++ b/lib/auto_linker/parser.ex @@ -338,13 +338,15 @@ defmodule AutoLinker.Parser do end def is_valid_tld?(true, buffer) do - [host] = Regex.run(@match_hostname, buffer, capture: [:host]) - - if is_ip?(host) do - true + with [host] <- Regex.run(@match_hostname, buffer, capture: [:host]) do + if is_ip?(host) do + true + else + tld = host |> String.split(".") |> List.last() + MapSet.member?(@tlds, tld) + end else - tld = host |> String.split(".") |> List.last() - MapSet.member?(@tlds, tld) + _ -> false end end diff --git a/test/parser_test.exs b/test/parser_test.exs index da68edc..90cf197 100644 --- a/test/parser_test.exs +++ b/test/parser_test.exs @@ -110,6 +110,19 @@ defmodule AutoLinker.ParserTest do text = "google.com" assert parse(text, url: false, phone: true) == text end + + test "do not link `:test.test`" do + text = ":test.test" + + assert parse(text, %{ + scheme: true, + extra: true, + class: false, + strip_prefix: false, + new_window: false, + rel: false + }) == text + end end def valid_number?([list], number) do