Refs #25484 -- Made non-staticfiles {% static %} tag quote its output.

This commit is contained in:
Tim Graham 2016-12-19 13:44:02 -05:00
parent e233f357bd
commit 1a04b1762b
2 changed files with 5 additions and 11 deletions

View File

@ -2,7 +2,7 @@ from django import template
from django.apps import apps
from django.utils.encoding import iri_to_uri
from django.utils.html import conditional_escape
from django.utils.six.moves.urllib.parse import urljoin
from django.utils.six.moves.urllib.parse import quote, urljoin
register = template.Library()
@ -116,7 +116,7 @@ class StaticNode(template.Node):
from django.contrib.staticfiles.storage import staticfiles_storage
return staticfiles_storage.url(path)
else:
return urljoin(PrefixNode.handle_simple("STATIC_URL"), path)
return urljoin(PrefixNode.handle_simple("STATIC_URL"), quote(path))
@classmethod
def handle_token(cls, parser, token):

View File

@ -51,13 +51,7 @@ class StaticTagTests(SimpleTestCase):
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):
@setup({'static-statictag05': '{% load static %}{% static "special?chars&quoted.html" %}'})
def test_static_quotes_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'))
self.assertEqual(output, urljoin(settings.STATIC_URL, '/static/special%3Fchars%26quoted.html'))