1
0
mirror of https://github.com/django/django.git synced 2024-12-22 17:16:24 +00:00

Fixed #25484 -- Made {% static %} render escaped URLs.

This commit is contained in:
alix- 2016-12-17 16:03:44 +01:00 committed by Tim Graham
parent 33e86b3488
commit 374e6230ca
4 changed files with 19 additions and 3 deletions

View File

@ -1,7 +1,8 @@
from django import template
from django.apps import apps
from django.utils.encoding import iri_to_uri
from django.utils.six.moves.urllib.parse import urljoin
from django.utils.html import conditional_escape
from django.utils.six.moves.urllib.parse import unquote, urljoin
register = template.Library()
@ -101,7 +102,9 @@ class StaticNode(template.Node):
return self.handle_simple(path)
def render(self, context):
url = self.url(context)
url = unquote(self.url(context))
if context.autoescape:
url = conditional_escape(url)
if self.varname is None:
return url
context[self.varname] = url

View File

@ -35,7 +35,7 @@ class BaseStaticFilesMixin(object):
def render_template(self, template, **kwargs):
if isinstance(template, six.string_types):
template = Template(template)
return template.render(Context(kwargs)).strip()
return template.render(Context(**kwargs)).strip()
def static_template_snippet(self, path, asvar=False):
if asvar:

View File

@ -8,3 +8,5 @@ class TestTemplateTag(StaticFilesTestCase):
def test_template_tag(self):
self.assertStaticRenders("does/not/exist.png", "/static/does/not/exist.png")
self.assertStaticRenders("testfile.txt", "/static/testfile.txt")
self.assertStaticRenders("test.html?foo=1&bar=2", "/static/test.html?foo=1&bar=2", autoescape=False)
self.assertStaticRenders("test.html?foo=1&bar=2", "/static/test.html?foo=1&bar=2", autoescape=True)

View File

@ -50,3 +50,14 @@ class StaticTagTests(SimpleTestCase):
def test_static_statictag04(self):
output = self.engine.render_to_string('static-statictag04', {'base_css': 'admin/base.css'})
self.assertEqual(output, urljoin(settings.STATIC_URL, 'admin/base.css'))
@setup({'static-statictag05': '{% load static %}{% static "test.html?foo=1&bar=2" %}'})
def test_static_escapes_urls(self):
output = self.engine.render_to_string('static-statictag05')
self.assertEqual(output, urljoin(settings.STATIC_URL, '/static/test.html?foo=1&bar=2'))
@setup({'static-statictag06': '{% load static %}'
'{% autoescape off %}{% static "test.html?foo=1&bar=2" %}{% endautoescape %}'})
def test_static_escapes_urls_autoescape_off(self):
output = self.engine.render_to_string('static-statictag06')
self.assertEqual(output, urljoin(settings.STATIC_URL, '/static/test.html?foo=1&bar=2'))