contrib.sekizai: fix double-rendering: use TextNode, not Template

Use `TextNode(data)` instead of `Template(data).nodelist` to parse the
template only once.

The report and fix was initially provided by Aaron McMillin.

Fixes #361
Closes #362
This commit is contained in:
Daniel Hahler
2015-06-20 16:35:15 +02:00
committed by Johannes Linke
parent 9d9b20a14a
commit a13c143e26
3 changed files with 19 additions and 2 deletions

View File

@@ -13,14 +13,18 @@ from compressor.conf import settings
from compressor.signals import post_compress
from compressor.tests.test_base import css_tag, test_dir
from sekizai.context import SekizaiContext
def render(template_string, context_dict=None):
def render(template_string, context_dict=None, context=None):
"""
A shortcut for testing template output.
"""
if context_dict is None:
context_dict = {}
c = Context(context_dict)
if context is None:
context = Context
c = context(context_dict)
t = Template(template_string)
return t.render(c).strip()
@@ -126,6 +130,14 @@ class TemplatetagTestCase(TestCase):
context = kwargs['context']
self.assertEqual('foo', context['compressed']['name'])
def test_sekizai_only_once(self):
template = """{% load sekizai_tags %}{% addtoblock "js" %}
<script type="text/javascript">var tmpl="{% templatetag openblock %} if x == 3 %}x IS 3{% templatetag openblock %} endif %}"</script>
{% endaddtoblock %}{% render_block "js" postprocessor "compressor.contrib.sekizai.compress" %}
"""
out = '<script type="text/javascript" src="/static/CACHE/js/e9fce10d884d.js"></script>'
self.assertEqual(out, render(template, self.context, SekizaiContext))
class PrecompilerTemplatetagTestCase(TestCase):
def setUp(self):

View File

@@ -8,3 +8,4 @@ BeautifulSoup==3.2.1
unittest2==1.0.0
coffin==0.4.0
jingo==0.7
django-sekizai==0.8.2

View File

@@ -10,6 +10,7 @@ two =
unittest2==1.0.0
jingo==0.7
coffin==0.4.0
django-sekizai==0.8.2
two_six =
flake8==2.4.0
coverage==3.7.1
@@ -21,6 +22,7 @@ two_six =
unittest2==1.0.0
jingo==0.7
coffin==0.4.0
django-sekizai==0.8.2
three =
flake8==2.4.0
coverage==3.7.1
@@ -31,6 +33,7 @@ three =
beautifulsoup4==4.4.0
jingo==0.7
coffin==0.4.0
django-sekizai==0.8.2
three_two =
flake8==2.4.0
coverage==3.7.1
@@ -41,6 +44,7 @@ three_two =
beautifulsoup4==4.4.0
jingo==0.7
coffin==0.4.0
django-sekizai==0.8.2
[tox]
envlist =
{py26,py27}-1.4.X,