Skip to content

Commit

Permalink
Fix urls creation from richtext [STTNHUB-252] (superdesk#2481)
Browse files Browse the repository at this point in the history
* Fix urls creation from richtext

* add test

* reformat code via black
  • Loading branch information
devketanpro authored Oct 23, 2023
1 parent 527ecb0 commit ce3862e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
7 changes: 6 additions & 1 deletion superdesk/text_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,12 @@ def replacement(match_object):
# so return the whole match.
return match_object.group(0)
else:
return '<a href="{0}" target="_blank">{1}</a>'.format(html.unescape(groups[1]), groups[1])
url = html.unescape(groups[1])
if url.startswith("www."):
url = "https://" + url
elif not url.startswith(("http://", "https://")):
url = "https://" + url
return '<a href="{0}" target="_blank">{1}</a>'.format(url, groups[1])

value = EMAIL_REGEX.sub(r'<a href="mailto:\1">\1</a>', text)
value = re.sub(URL_REGEX, replacement, value)
Expand Down
10 changes: 7 additions & 3 deletions tests/text_utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def test_convert_plain_text_to_html(self):
test_strings = [
["plain text", "<p>plain text</p>"],
["email me at [email protected]", '<p>email me at <a href="mailto:[email protected]">[email protected]</a></p>'],
["find me on foobar.com", '<p>find me on <a href="foobar.com" target="_blank">foobar.com</a></p>'],
["find me on foobar.com", '<p>find me on <a href="https://foobar.com" target="_blank">foobar.com</a></p>'],
[
"https://www.foobar.com",
'<p><a href="https://www.foobar.com" target="_blank">https://www.foobar.com</a></p>',
Expand All @@ -152,7 +152,7 @@ def test_convert_plain_text_to_html(self):
"https://www.foobar.com/test?one=two&three=4</a></p>",
],
["https://foobar.com", '<p><a href="https://foobar.com" target="_blank">https://foobar.com</a></p>'],
["www.foobar.com", '<p><a href="www.foobar.com" target="_blank">www.foobar.com</a></p>'],
["www.foobar.com", '<p><a href="https://www.foobar.com" target="_blank">www.foobar.com</a></p>'],
[
"""Foo Bar
Expand All @@ -164,8 +164,12 @@ def test_convert_plain_text_to_html(self):
"<p>Foo Bar PTY LTD</p>"
"<p>Ph +61 23456789</p>"
'<p>Email: <a href="mailto:[email protected]">[email protected]</a> '
'Website: <a href="www.foobar.com" target="_blank">www.foobar.com</a></p>',
'Website: <a href="https://www.foobar.com" target="_blank">www.foobar.com</a></p>',
],
]
for tests in test_strings:
self.assertEqual(text_utils.plain_text_to_html(tests[0]), tests[1])

self.assertEqual(
text_utils.plain_text_to_html("foo.com"), '<p><a href="https://foo.com" target="_blank">foo.com</a></p>'
)

0 comments on commit ce3862e

Please sign in to comment.