1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
|
import re
import pytest
from linkify_it import LinkifyIt, SchemaError
from linkify_it.main import Match
from linkify_it.tlds import TLDS
def test_pretest_false():
linkifyit = LinkifyIt()
assert linkifyit.pretest("nolink") is False
def test_create_instance_with_schemas():
schemas = {"my:": {"validate": r"^\/\/[a-z]+"}}
linkifyit = LinkifyIt(schemas)
match = linkifyit.match("google.com. my:// my://asdf!")
assert match[0].text == "google.com"
assert match[1].text == "my://asdf"
def test_match_class():
linkifyit = LinkifyIt()
match = Match(linkifyit, 0)
assert (
match.__repr__()
== "linkify_it.main.Match({'schema': '', 'index': -1, 'last_index': -1, 'raw': '', 'text': '', 'url': ''})" # noqa: E501
)
def test_api_extend_tlds():
linkifyit = LinkifyIt()
assert linkifyit.test("google.myroot") is False
linkifyit.tlds("myroot", True)
assert linkifyit.test("google.myroot") is True
assert linkifyit.test("google.xyz") is False
# ref - http://data.iana.org/TLD/tlds-alpha-by-domain.txt
linkifyit.tlds(TLDS)
assert linkifyit.test("google.xyz") is True
assert linkifyit.test("google.myroot") is False
def test_api_add_rule_as_regex_with_default_normalizer():
linkifyit = LinkifyIt().add("my:", {"validate": re.compile(r"^\/\/[a-z]+")})
match = linkifyit.match("google.com. my:// my://asdf!")
assert match[0].text == "google.com"
assert match[1].text == "my://asdf"
def test_api_add_rule_as_regex_with_default_normalizer_with_no_compile():
linkifyit = LinkifyIt().add("my:", {"validate": r"^\/\/[a-z]+"})
match = linkifyit.match("google.com. my:// my://asdf!")
assert match[0].text == "google.com"
assert match[1].text == "my://asdf"
def test_api_add_rule_with_normalizer():
def func_normalize(self, m):
m.text = re.sub(r"^my://", "", m.text).upper()
m.url = m.url.upper()
linkifyit = LinkifyIt().add(
"my:", {"validate": re.compile(r"^\/\/[a-z]+"), "normalize": func_normalize}
)
match = linkifyit.match("google.com. my:// my://asdf!")
assert match[1].text == "ASDF"
assert match[1].url == "MY://ASDF"
def test_api_add_rule_with_normalizer_no_cimpile():
def func_normalize(self, m):
m.text = re.sub(r"^my://", "", m.text).upper()
m.url = m.url.upper()
linkifyit = LinkifyIt().add(
"my:", {"validate": r"^\/\/[a-z]+", "normalize": func_normalize}
)
match = linkifyit.match("google.com. my:// my://asdf!")
assert match[1].text == "ASDF"
assert match[1].url == "MY://ASDF"
def test_api_disable_rule():
linkifyit = LinkifyIt()
assert linkifyit.test("http://google.com")
assert linkifyit.test("foo@bar.com")
linkifyit.add("http:", None)
linkifyit.add("mailto:", None)
assert not linkifyit.test("http://google.com")
assert not linkifyit.test("foo@bar.com")
def test_api_add_bad_definition():
with pytest.raises(SchemaError):
linkifyit = LinkifyIt({"fuzzy_link": False})
linkifyit = LinkifyIt()
with pytest.raises(SchemaError):
linkifyit.add("test:", [])
linkifyit = LinkifyIt()
with pytest.raises(SchemaError):
linkifyit.add("test:", {"validate": []})
linkifyit = LinkifyIt()
with pytest.raises(SchemaError):
def func():
return False
linkifyit.add("test:", {"validate": func, "normalize": "bad"})
def test_api_at_position():
linkifyit = LinkifyIt()
assert linkifyit.test_schema_at("http://google.com", "http:", 5)
assert linkifyit.test_schema_at("http://google.com", "HTTP:", 5)
assert not linkifyit.test_schema_at("http://google.com", "http:", 6)
assert not linkifyit.test_schema_at("http://google.com", "bad_schema:", 6)
def test_api_correct_cache_value():
linkifyit = LinkifyIt()
match = linkifyit.match(".com. http://google.com google.com ftp://google.com")
assert match[0].text == "http://google.com"
assert match[1].text == "google.com"
assert match[2].text == "ftp://google.com"
def test_api_normalize():
linkifyit = LinkifyIt()
match = linkifyit.match("mailto:foo@bar.com")[0]
# assert match.text == "foo@bar.com"
assert match.url == "mailto:foo@bar.com"
match = linkifyit.match("foo@bar.com")[0]
# assert match.text == "foo@bar.com"
assert match.url == "mailto:foo@bar.com"
def test_api_twitter_rule():
linkifyit = LinkifyIt()
def validate(self, text, pos):
tail = text[pos:]
if not self.re.get("twitter"):
self.re["twitter"] = re.compile(
"^([a-zA-Z0-9_]){1,15}(?!_)(?=$|" + self.re["src_ZPCc"] + ")"
)
if self.re["twitter"].search(tail):
if pos > 2 and tail[pos - 2] == "@":
return False
return len(self.re["twitter"].search(tail).group())
return 0
def normalize(self, m):
m.url = "https://twitter.com/" + re.sub(r"^@", "", m.url)
linkifyit.add("@", {"validate": validate, "normalize": normalize})
assert linkifyit.match("hello, @gamajoba_!")[0].text == "@gamajoba_"
assert linkifyit.match(":@givi")[0].text == "@givi"
assert linkifyit.match(":@givi")[0].url == "https://twitter.com/givi"
assert not linkifyit.test("@@invalid")
def test_api_twitter_rule_no_compile():
linkifyit = LinkifyIt()
def validate(self, text, pos):
tail = text[pos:]
if not self.re.get("twitter"):
self.re["twitter"] = (
"^([a-zA-Z0-9_]){1,15}(?!_)(?=$|" + self.re["src_ZPCc"] + ")"
)
if re.search(self.re["twitter"], tail):
if pos > 2 and tail[pos - 2] == "@":
return False
return len(re.search(self.re["twitter"], tail).group())
return 0
def normalize(self, m):
m.url = "https://twitter.com/" + re.sub(r"^@", "", m.url)
linkifyit.add("@", {"validate": validate, "normalize": normalize})
assert linkifyit.match("hello, @gamajoba_!")[0].text == "@gamajoba_"
assert linkifyit.match(":@givi")[0].text == "@givi"
assert linkifyit.match(":@givi")[0].url == "https://twitter.com/givi"
assert not linkifyit.test("@@invalid")
def test_api_set_option_fuzzylink():
linkifyit = LinkifyIt(options={"fuzzy_link": False})
assert not linkifyit.test("google.com")
linkifyit.set({"fuzzy_link": True})
assert linkifyit.test("google.com")
assert linkifyit.match("google.com")[0].text == "google.com"
def test_api_set_option_fuzzyemail():
linkifyit = LinkifyIt(options={"fuzzy_email": False})
assert not linkifyit.test("foo@bar.com")
linkifyit.set({"fuzzy_email": True})
assert linkifyit.test("foo@bar.com")
assert linkifyit.match("foo@bar.com")[0].text == "foo@bar.com"
def test_api_set_option_fuzzyip():
linkifyit = LinkifyIt()
assert not linkifyit.test("1.1.1.1")
linkifyit.set({"fuzzy_ip": True})
assert linkifyit.test("1.1.1.1")
assert linkifyit.match("1.1.1.1")[0].text == "1.1.1.1"
def test_api_shoud_not_hang_in_fuzzy_mode_with_sequence_of_astrals():
linkifyit = LinkifyIt()
linkifyit.set({"fuzzy_link": True})
linkifyit.match("😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡 .com")
def test_api_shoud_accept_triple_minus():
linkifyit = LinkifyIt()
assert linkifyit.match("http://e.com/foo---bar")[0].text == "http://e.com/foo---bar"
assert linkifyit.match("text@example.com---foo") is None
linkifyit = LinkifyIt(None, {"---": True})
assert linkifyit.match("http://e.com/foo---bar")[0].text == "http://e.com/foo"
assert linkifyit.match("text@example.com---foo")[0].text == "text@example.com"
# issue #25. Schema key containing - not producing matches
@pytest.mark.parametrize(
"escape_str",
{".", "?", "*", "+", "^", "$", "[", "]", "\\", "(", ")", "{", "}", "|", "-"},
)
def test_api_add_alias_rule_with_excape_re_string(escape_str):
linkifyit = LinkifyIt()
linkifyit.add("foo{}bar:".format(escape_str), "http:")
assert linkifyit.test("Check foo{}bar://test".format(escape_str)) is True
def test_api_blank_test_match_at_the_start():
linkifyit = LinkifyIt()
assert not linkifyit.match_at_start("")
def test_api_should_find_a_match_at_the_start():
linkifyit = LinkifyIt()
linkifyit = LinkifyIt(options={"fuzzy_link": True})
linkifyit.set({"fuzzy_link": True})
assert not linkifyit.test("@@invalid")
assert not linkifyit.match_at_start("google.com 123")
assert not linkifyit.match_at_start(" http://google.com 123")
def test_api_match_a_start_should_not_interfere_with_normal_match():
linkifyit = LinkifyIt()
str = "http://google.com http://google.com"
assert linkifyit.match_at_start(str)
assert len(linkifyit.match(str)) == 2
str = "aaa http://google.com http://google.com"
assert not linkifyit.match_at_start(str)
assert len(linkifyit.match(str)) == 2
def test_api_should_not_match_incomplete_links():
# regression test for https://github.com/markdown-it/markdown-it/issues/868
linkifyit = LinkifyIt()
assert not linkifyit.match_at_start("http://")
assert not linkifyit.match_at_start("https://")
|