mirror of
https://github.com/django/django.git
synced 2024-12-23 01:25:58 +00:00
Fixed #35998 -- Added caching to django.utils.html.urlize().
This commit is contained in:
parent
f05edb2b43
commit
6eaca5da4f
@ -317,18 +317,22 @@ class Urlizer:
|
|||||||
safe_input = isinstance(text, SafeData)
|
safe_input = isinstance(text, SafeData)
|
||||||
|
|
||||||
words = self.word_split_re.split(str(text))
|
words = self.word_split_re.split(str(text))
|
||||||
return "".join(
|
mapping = {}
|
||||||
[
|
urlized_words = []
|
||||||
self.handle_word(
|
for word in words:
|
||||||
|
if word in mapping:
|
||||||
|
urlized_words.append(mapping[word])
|
||||||
|
else:
|
||||||
|
urlized_word = self.handle_word(
|
||||||
word,
|
word,
|
||||||
safe_input=safe_input,
|
safe_input=safe_input,
|
||||||
trim_url_limit=trim_url_limit,
|
trim_url_limit=trim_url_limit,
|
||||||
nofollow=nofollow,
|
nofollow=nofollow,
|
||||||
autoescape=autoescape,
|
autoescape=autoescape,
|
||||||
)
|
)
|
||||||
for word in words
|
urlized_words.append(urlized_word)
|
||||||
]
|
mapping[word] = urlized_word
|
||||||
)
|
return "".join(urlized_words)
|
||||||
|
|
||||||
def handle_word(
|
def handle_word(
|
||||||
self,
|
self,
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
|
from unittest import mock
|
||||||
|
|
||||||
from django.template.defaultfilters import urlize
|
from django.template.defaultfilters import urlize
|
||||||
from django.test import SimpleTestCase
|
from django.test import SimpleTestCase
|
||||||
from django.utils.functional import lazy
|
from django.utils.functional import lazy
|
||||||
|
from django.utils.html import Urlizer
|
||||||
from django.utils.safestring import mark_safe
|
from django.utils.safestring import mark_safe
|
||||||
|
|
||||||
from ..utils import setup
|
from ..utils import setup
|
||||||
@ -467,3 +470,25 @@ class FunctionTests(SimpleTestCase):
|
|||||||
urlize(prepend_www("google.com")),
|
urlize(prepend_www("google.com")),
|
||||||
'<a href="http://www.google.com" rel="nofollow">www.google.com</a>',
|
'<a href="http://www.google.com" rel="nofollow">www.google.com</a>',
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@mock.patch.object(Urlizer, "handle_word", return_value="test")
|
||||||
|
def test_caching(self, mock_handle_word):
|
||||||
|
urlize("test test test test")
|
||||||
|
mock_handle_word.assert_has_calls(
|
||||||
|
[
|
||||||
|
mock.call(
|
||||||
|
"test",
|
||||||
|
safe_input=False,
|
||||||
|
trim_url_limit=None,
|
||||||
|
nofollow=True,
|
||||||
|
autoescape=True,
|
||||||
|
),
|
||||||
|
mock.call(
|
||||||
|
" ",
|
||||||
|
safe_input=False,
|
||||||
|
trim_url_limit=None,
|
||||||
|
nofollow=True,
|
||||||
|
autoescape=True,
|
||||||
|
),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user