1
0
mirror of https://github.com/django/django.git synced 2025-10-25 22:56:12 +00:00

Fixed #35533 -- Improved urlize function to handle markdown links correctly.

Updated the urlize function to correctly handle markdown links. Added tests to ensure the correct behavior of the urlize function with various markdown link inputs.
This commit is contained in:
DongwookKim0823
2024-06-24 14:34:14 +09:00
parent 72b7aecbbf
commit ee5b8e53cb
4 changed files with 231 additions and 3 deletions

View File

@@ -17,6 +17,7 @@ from django.utils.html import (
strip_spaces_between_tags,
strip_tags,
urlize,
urlizer,
)
from django.utils.safestring import mark_safe
@@ -356,3 +357,78 @@ class TestUtilsHtml(SimpleTestCase):
for value in tests:
with self.subTest(value=value):
self.assertEqual(urlize(value), value)
def test_handle_markdown_link(self):
tests = [
{
"input": "Here's a [link with [nested] brackets](https://example.com)",
"expected": "Here's a [link with [nested] brackets](<a href=\"https://"
'example.com">https://example.com</a>)',
"params": {
"trim_url_limit": None,
"nofollow": False,
"autoescape": False,
},
},
{
"input": "Check out [this link](https://example.com/page(1))",
"expected": 'Check out [this link](<a href="https://example.com/'
'page(1)">https://example.com/page(1)</a>)',
"params": {
"trim_url_limit": None,
"nofollow": False,
"autoescape": False,
},
},
{
"input": "Here's a [complex URL](https://example.com/"
"path?param1=value1&param2=value2#fragment)",
"expected": "Here's a [complex URL](<a href=\"https://example.com/"
'path?param1=value1&amp;param2=value2#fragment">'
"https://example.com/path?param1=value1&amp;"
"param2=value2#fragment</a>)",
"params": {
"trim_url_limit": None,
"nofollow": False,
"autoescape": True,
},
},
{
"input": "Multiple [link1](https://example1.com) and "
"[link2](https://example2.com)",
"expected": 'Multiple [link1](<a href="https://example1.com">'
"https://example1.com</a>) and [link2]"
'(<a href="https://example2.com">https://example2.com</a>)',
"params": {
"trim_url_limit": None,
"nofollow": False,
"autoescape": False,
},
},
{
"input": "This is a [broken link(https://example.com)",
"expected": "This is a [broken link(https://example.com)",
"params": {
"trim_url_limit": None,
"nofollow": False,
"autoescape": False,
},
},
{
"input": "Here's a [very long URL](https://example.com/"
+ "x" * 100
+ ")",
"expected": "Here's a [very long URL](<a href=\"https://example.com/"
+ "x" * 100
+ '">https://example.com/xxxxxxxxx…</a>)',
"params": {
"trim_url_limit": 30,
"nofollow": False,
"autoescape": False,
},
},
]
for test in tests:
with self.subTest(test=test):
output = urlizer(test["input"], **test["params"])
self.assertEqual(output, test["expected"])