summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Graham <timograham@gmail.com>2015-03-10 18:40:33 -0400
committerTim Graham <timograham@gmail.com>2015-03-10 19:12:20 -0400
commitaba74d6f1e221c10be2798f55971d710521cb404 (patch)
treec772fd938a24abd81a809c47ad015ec3cdd101c7
parent12e199356e1cfe74dd7d874a2d3fdc16a21a60d8 (diff)
[1.8.x] Fixed escaping regression in urlize filter.
Now that the URL is always unescaped as of refs #22267, we should re-escape it before inserting it into the anchor. Backport of 7b1a67cce52e5c191fbfa1bca501c6f0222db019 from master
-rw-r--r--django/utils/html.py2
-rw-r--r--tests/template_tests/filter_tests/test_urlize.py10
-rw-r--r--tests/template_tests/filter_tests/test_urlizetrunc.py10
3 files changed, 11 insertions, 11 deletions
diff --git a/django/utils/html.py b/django/utils/html.py
index d7ecde64c3..66cbcee8f3 100644
--- a/django/utils/html.py
+++ b/django/utils/html.py
@@ -345,7 +345,7 @@ def urlize(text, trim_url_limit=None, nofollow=False, autoescape=False):
if autoescape and not safe_input:
lead, trail = escape(lead), escape(trail)
trimmed = escape(trimmed)
- middle = '<a href="%s"%s>%s</a>' % (url, nofollow_attr, trimmed)
+ middle = '<a href="%s"%s>%s</a>' % (escape(url), nofollow_attr, trimmed)
words[i] = mark_safe('%s%s%s' % (lead, middle, trail))
else:
if safe_input:
diff --git a/tests/template_tests/filter_tests/test_urlize.py b/tests/template_tests/filter_tests/test_urlize.py
index ee6744e6cb..38a0a3e3ed 100644
--- a/tests/template_tests/filter_tests/test_urlize.py
+++ b/tests/template_tests/filter_tests/test_urlize.py
@@ -18,8 +18,8 @@ class UrlizeTests(SimpleTestCase):
)
self.assertEqual(
output,
- '<a href="http://example.com/?x=&y=" rel="nofollow">http://example.com/?x=&y=</a> '
- '<a href="http://example.com?x=&y=%3C2%3E" rel="nofollow">http://example.com?x=&amp;y=&lt;2&gt;</a>'
+ '<a href="http://example.com/?x=&amp;y=" rel="nofollow">http://example.com/?x=&y=</a> '
+ '<a href="http://example.com?x=&amp;y=%3C2%3E" rel="nofollow">http://example.com?x=&amp;y=&lt;2&gt;</a>'
)
@setup({'urlize02': '{{ a|urlize }} {{ b|urlize }}'})
@@ -30,8 +30,8 @@ class UrlizeTests(SimpleTestCase):
)
self.assertEqual(
output,
- '<a href="http://example.com/?x=&y=" rel="nofollow">http://example.com/?x=&amp;y=</a> '
- '<a href="http://example.com?x=&y=" rel="nofollow">http://example.com?x=&amp;y=</a>'
+ '<a href="http://example.com/?x=&amp;y=" rel="nofollow">http://example.com/?x=&amp;y=</a> '
+ '<a href="http://example.com?x=&amp;y=" rel="nofollow">http://example.com?x=&amp;y=</a>'
)
@setup({'urlize03': '{% autoescape off %}{{ a|urlize }}{% endautoescape %}'})
@@ -78,7 +78,7 @@ class UrlizeTests(SimpleTestCase):
output = self.engine.render_to_string('urlize09', {'a': "http://example.com/?x=&amp;y=&lt;2&gt;"})
self.assertEqual(
output,
- '<a href="http://example.com/?x=&y=%3C2%3E" rel="nofollow">http://example.com/?x=&amp;y=&lt;2&gt;</a>',
+ '<a href="http://example.com/?x=&amp;y=%3C2%3E" rel="nofollow">http://example.com/?x=&amp;y=&lt;2&gt;</a>',
)
diff --git a/tests/template_tests/filter_tests/test_urlizetrunc.py b/tests/template_tests/filter_tests/test_urlizetrunc.py
index 2b4b18e1f5..cb5c2a6daf 100644
--- a/tests/template_tests/filter_tests/test_urlizetrunc.py
+++ b/tests/template_tests/filter_tests/test_urlizetrunc.py
@@ -19,8 +19,8 @@ class UrlizetruncTests(SimpleTestCase):
)
self.assertEqual(
output,
- '"Unsafe" <a href="http://example.com/x=&y=" rel="nofollow">http:...</a> '
- '&quot;Safe&quot; <a href="http://example.com?x=&y=" rel="nofollow">http:...</a>'
+ '"Unsafe" <a href="http://example.com/x=&amp;y=" rel="nofollow">http:...</a> '
+ '&quot;Safe&quot; <a href="http://example.com?x=&amp;y=" rel="nofollow">http:...</a>'
)
@setup({'urlizetrunc02': '{{ a|urlizetrunc:"8" }} {{ b|urlizetrunc:"8" }}'})
@@ -34,8 +34,8 @@ class UrlizetruncTests(SimpleTestCase):
)
self.assertEqual(
output,
- '&quot;Unsafe&quot; <a href="http://example.com/x=&y=" rel="nofollow">http:...</a> '
- '&quot;Safe&quot; <a href="http://example.com?x=&y=" rel="nofollow">http:...</a>'
+ '&quot;Unsafe&quot; <a href="http://example.com/x=&amp;y=" rel="nofollow">http:...</a> '
+ '&quot;Safe&quot; <a href="http://example.com?x=&amp;y=" rel="nofollow">http:...</a>'
)
@@ -72,7 +72,7 @@ class FunctionTests(SimpleTestCase):
def test_query_string(self):
self.assertEqual(
urlizetrunc('http://www.google.co.uk/search?hl=en&q=some+long+url&btnG=Search&meta=', 20),
- '<a href="http://www.google.co.uk/search?hl=en&q=some+long+url&btnG=Search&'
+ '<a href="http://www.google.co.uk/search?hl=en&amp;q=some+long+url&amp;btnG=Search&amp;'
'meta=" rel="nofollow">http://www.google...</a>',
)