Skip to content

Commit dd19a44

Browse files
dklibancarltongibson
authored andcommitted
Problem: autoescape not getting passed to urlize_quoted_links filter (#6191)
Solution: set needs_autoescape=True when registering the filter Without this patch, the disabling autoescape in the template does not work.
1 parent 5feb835 commit dd19a44

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

rest_framework/templatetags/rest_framework.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ def smart_urlquote_wrapper(matched_url):
314314
return None
315315

316316

317-
@register.filter
317+
@register.filter(needs_autoescape=True)
318318
def urlize_quoted_links(text, trim_url_limit=None, nofollow=True, autoescape=True):
319319
"""
320320
Converts any URLs in text into clickable links.

tests/test_templatetags.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import unittest
55

6+
from django.template import Context, Template
67
from django.test import TestCase
78

89
from rest_framework.compat import coreapi, coreschema
@@ -304,6 +305,16 @@ def test_json_with_url(self):
304305
'&quot;foo_set&quot;: [\n &quot;<a href="http://api/foos/1/">http://api/foos/1/</a>&quot;\n], '
305306
self._urlize_dict_check(data)
306307

308+
def test_template_render_with_noautoescape(self):
309+
"""
310+
Test if the autoescape value is getting passed to urlize_quoted_links filter.
311+
"""
312+
template = Template("{% load rest_framework %}"
313+
"{% autoescape off %}{{ content|urlize_quoted_links }}"
314+
"{% endautoescape %}")
315+
rendered = template.render(Context({'content': '"http://example.com"'}))
316+
assert rendered == '"<a href="http://example.com" rel="nofollow">http://example.com</a>"'
317+
307318

308319
@unittest.skipUnless(coreapi, 'coreapi is not installed')
309320
class SchemaLinksTests(TestCase):

0 commit comments

Comments
 (0)