Autolinking is more robust

and handles URLs with CGI params, recognizes more (and multiple)
trailing punctuation marks including entity-encoded punctuation like
`"`.
This commit is contained in:
Mike Dalessio
2025-12-09 16:29:38 -05:00
parent c5721b8187
commit e0c821f69b
2 changed files with 60 additions and 15 deletions
+53 -13
View File
@@ -10,25 +10,65 @@ class HtmlHelperTest < ActionView::TestCase
format_html("<p>Check this: https://example.com/</p>")
end
test "don't' include punctuation in URL autolinking" do
test "don't include punctuation in URL autolinking" do
assert_equal_html \
%(<p>Check this: <a href="https://example.com" rel="noreferrer">https://example.com</a>!</p>),
format_html("<p>Check this: https://example.com!</p>")
%(<p>Check this: <a href="https://example.com/" rel="noreferrer">https://example.com/</a>!</p>),
format_html("<p>Check this: https://example.com/!</p>")
assert_equal_html \
%(<p>Check this: <a href="https://example.com" rel="noreferrer">https://example.com</a>.</p>),
format_html("<p>Check this: https://example.com.</p>")
%(<p>Check this: <a href="https://example.com/" rel="noreferrer">https://example.com/</a>.</p>),
format_html("<p>Check this: https://example.com/.</p>")
assert_equal_html \
%(<p>Check this: <a href="https://example.com" rel="noreferrer">https://example.com</a>?</p>),
format_html("<p>Check this: https://example.com?</p>")
%(<p>Check this: <a href="https://example.com/" rel="noreferrer">https://example.com/</a>?</p>),
format_html("<p>Check this: https://example.com/?</p>")
assert_equal_html \
%(<p>Check this: <a href="https://example.com" rel="noreferrer">https://example.com</a>,</p>),
format_html("<p>Check this: https://example.com,</p>")
%(<p>Check this: <a href="https://example.com/" rel="noreferrer">https://example.com/</a>,</p>),
format_html("<p>Check this: https://example.com/,</p>")
assert_equal_html \
%(<p>Check this: <a href="https://example.com" rel="noreferrer">https://example.com</a>:</p>),
format_html("<p>Check this: https://example.com:</p>")
%(<p>Check this: <a href="https://example.com/" rel="noreferrer">https://example.com/</a>:</p>),
format_html("<p>Check this: https://example.com/:</p>")
assert_equal_html \
%(<p>Check this: <a href="https://example.com" rel="noreferrer">https://example.com</a>;</p>),
format_html("<p>Check this: https://example.com;</p>")
%(<p>Check this: <a href="https://example.com/" rel="noreferrer">https://example.com/</a>;</p>),
format_html("<p>Check this: https://example.com/;</p>")
assert_equal_html \
%(<p>Check this: <a href="https://example.com/" rel="noreferrer">https://example.com/</a>"</p>),
format_html("<p>Check this: https://example.com/\"</p>")
assert_equal_html \
%(<p>Check this: <a href="https://example.com/" rel="noreferrer">https://example.com/</a>'</p>),
format_html("<p>Check this: https://example.com/'</p>")
# trailing entities that decode to punctuation
# use assert_equal and not assert_equal_html to make sure we're getting entities correct
assert_equal \
%(<p>Check this: <a href="https://example.com/" rel="noreferrer">https://example.com/</a>&lt;</p>),
format_html("<p>Check this: https://example.com/&lt;</p>")
assert_equal \
%(<p>Check this: <a href="https://example.com/" rel="noreferrer">https://example.com/</a>&gt;</p>),
format_html("<p>Check this: https://example.com/&gt;</p>")
assert_equal \
%(<p>Check this: <a href="https://example.com/" rel="noreferrer">https://example.com/</a>"</p>),
format_html("<p>Check this: https://example.com/&quot;</p>")
# multiple punctuation characters including entities
assert_equal_html \
%(<p>Check this: <a href="https://example.com/" rel="noreferrer">https://example.com/</a>!?;</p>),
format_html("<p>Check this: https://example.com/!?;</p>")
assert_equal_html \
%(&lt;img src="<a href="https://example.com/" rel="noreferrer">https://example.com/</a>"&gt;),
format_html(%(&lt;img src=&quot;https://example.com/&quot;&gt;))
assert_equal_html \
%(&lt;img src="<a href="https://example.com/" rel="noreferrer">https://example.com/</a>"!&gt;),
format_html(%(&lt;img src=&quot;https://example.com/&quot;!&gt;))
end
test "handle URLs with query parameters" do
# use assert_equal and not assert_equal_html to make sure we're getting entities correct
assert_equal \
%(<p>Check this: <a href="https://example.com/a?b=c&amp;d=e" rel="noreferrer">https://example.com/a?b=c&amp;d=e</a></p>),
format_html("<p>Check this: https://example.com/a?b=c&amp;d=e</p>")
assert_equal \
%(<p>Check this: <a href="https://example.com/a?b=c&amp;d=e" rel="noreferrer">https://example.com/a?b=c&amp;d=e</a></p>),
format_html("<p>Check this: https://example.com/a?b=c&d=e</p>")
end
test "respect existing links" do