Skip to content

Commit

Permalink
Problem: autoescape not getting passed to urlize_quoted_links filter (#…
Browse files Browse the repository at this point in the history
…6191)

Solution: set needs_autoescape=True when registering the filter

Without this patch, the disabling autoescape in the template does not work.
  • Loading branch information
dkliban authored and carltongibson committed Oct 10, 2018
1 parent 5feb835 commit dd19a44
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion rest_framework/templatetags/rest_framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ def smart_urlquote_wrapper(matched_url):
return None


@register.filter
@register.filter(needs_autoescape=True)
def urlize_quoted_links(text, trim_url_limit=None, nofollow=True, autoescape=True):
"""
Converts any URLs in text into clickable links.
Expand Down
11 changes: 11 additions & 0 deletions tests/test_templatetags.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import unittest

from django.template import Context, Template
from django.test import TestCase

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

def test_template_render_with_noautoescape(self):
"""
Test if the autoescape value is getting passed to urlize_quoted_links filter.
"""
template = Template("{% load rest_framework %}"
"{% autoescape off %}{{ content|urlize_quoted_links }}"
"{% endautoescape %}")
rendered = template.render(Context({'content': '"http://example.com"'}))
assert rendered == '"<a href="http://example.com" rel="nofollow">http://example.com</a>"'


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

0 comments on commit dd19a44

Please sign in to comment.