summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSarah Boyce <42296566+sarahboyce@users.noreply.github.com>2024-10-11 13:44:12 +0200
committerSarah Boyce <42296566+sarahboyce@users.noreply.github.com>2025-01-14 17:59:32 +0100
commitb721f127603516c75ebda6912046ff5f0694e150 (patch)
treea9a2047b449e8bba07c6b61f832c5d0c85316492
parentd206d4c200d71c0847e7f6720d88c587e7b46843 (diff)
Fixed #35998 -- Added caching to django.utils.html.urlize().
-rw-r--r--django/utils/html.py14
-rw-r--r--tests/template_tests/filter_tests/test_urlize.py37
2 files changed, 45 insertions, 6 deletions
diff --git a/django/utils/html.py b/django/utils/html.py
index 0d107a0da9..10036c38c4 100644
--- a/django/utils/html.py
+++ b/django/utils/html.py
@@ -317,18 +317,20 @@ class Urlizer:
safe_input = isinstance(text, SafeData)
words = self.word_split_re.split(str(text))
- return "".join(
- [
- self.handle_word(
+ local_cache = {}
+ urlized_words = []
+ for word in words:
+ if (urlized_word := local_cache.get(word)) is None:
+ urlized_word = self.handle_word(
word,
safe_input=safe_input,
trim_url_limit=trim_url_limit,
nofollow=nofollow,
autoescape=autoescape,
)
- for word in words
- ]
- )
+ local_cache[word] = urlized_word
+ urlized_words.append(urlized_word)
+ return "".join(urlized_words)
def handle_word(
self,
diff --git a/tests/template_tests/filter_tests/test_urlize.py b/tests/template_tests/filter_tests/test_urlize.py
index 546bd6c7d6..80dd94cd9f 100644
--- a/tests/template_tests/filter_tests/test_urlize.py
+++ b/tests/template_tests/filter_tests/test_urlize.py
@@ -1,6 +1,9 @@
+from unittest import mock
+
from django.template.defaultfilters import urlize
from django.test import SimpleTestCase
from django.utils.functional import lazy
+from django.utils.html import Urlizer
from django.utils.safestring import mark_safe
from ..utils import setup
@@ -467,3 +470,37 @@ class FunctionTests(SimpleTestCase):
urlize(prepend_www("google.com")),
'<a href="http://www.google.com" rel="nofollow">www.google.com</a>',
)
+
+ @mock.patch.object(Urlizer, "handle_word", return_value="test")
+ def test_caching_repeated_words(self, mock_handle_word):
+ urlize("test test test test")
+ common_handle_word_args = {
+ "safe_input": False,
+ "trim_url_limit": None,
+ "nofollow": True,
+ "autoescape": True,
+ }
+ self.assertEqual(
+ mock_handle_word.mock_calls,
+ [
+ mock.call("test", **common_handle_word_args),
+ mock.call(" ", **common_handle_word_args),
+ ],
+ )
+
+ @mock.patch.object(Urlizer, "handle_word", return_value="test")
+ def test_caching_repeated_calls(self, mock_handle_word):
+ urlize("test")
+ handle_word_test = mock.call(
+ "test",
+ safe_input=False,
+ trim_url_limit=None,
+ nofollow=True,
+ autoescape=True,
+ )
+ self.assertEqual(mock_handle_word.mock_calls, [handle_word_test])
+
+ urlize("test")
+ self.assertEqual(
+ mock_handle_word.mock_calls, [handle_word_test, handle_word_test]
+ )