fix parsing inside HTML tags

This commit is contained in:
Egor Kislitsyn 2019-04-09 13:08:13 +07:00
parent 50882b427a
commit 4949c02cb6
3 changed files with 31 additions and 13 deletions

View file

@ -204,19 +204,8 @@ defmodule AutoLinker.Parser do
handler
)
defp do_parse(
{<<char::bytes-size(1), text::binary>>, user_acc},
opts,
{buffer, acc, {:open, level}},
handler
)
when char in [" ", "\r", "\n"] do
do_parse(
{text, user_acc},
opts,
{"", acc <> buffer <> char, {:attrs, level}},
handler
)
defp do_parse({text, user_acc}, opts, {buffer, acc, {:open, level}}, handler) do
do_parse({text, user_acc}, opts, {"", acc <> buffer, {:attrs, level}}, handler)
end
# default cases where state is not important

View file

@ -144,6 +144,22 @@ defmodule AutoLinkerTest do
) == expected
end
test "mentions inside html tags" do
text =
"<p><strong>hello world</strong></p>\n<p><`em>another @user__test and @user__test google.com paragraph</em></p>\n"
expected =
"<p><strong>hello world</strong></p>\n<p><`em>another <a href=\"u/user__test\">@user__test</a> and <a href=\"u/user__test\">@user__test</a> <a href=\"http://google.com\">google.com</a> paragraph</em></p>\n"
assert AutoLinker.link(text,
mention: true,
mention_prefix: "u/",
class: false,
rel: false,
new_window: false
) == expected
end
test "metion @user@example.com" do
text = "hey @user@example.com"

View file

@ -70,6 +70,19 @@ defmodule AutoLinker.ParserTest do
end
test "links url inside html" do
text = "<div>google.com</div>"
expected = "<div><a href=\"http://google.com\">google.com</a></div>"
assert parse(text, class: false, rel: false, new_window: false) == expected
text = "Check out <div class='section'>google.com</div>"
expected =
"Check out <div class='section'><a href=\"http://google.com\">google.com</a></div>"
assert parse(text, class: false, rel: false, new_window: false) == expected
end
text = "Check out <div class='section'>google.com</div>"
expected =