Commit f6c4a89d authored by Jannis Leidel's avatar Jannis Leidel

Merge remote-tracking branch 'GermanoGuerrini/develop' into develop

Fix #132.
parents 02b57e76 a327860d
......@@ -34,7 +34,7 @@ class CssCompressor(Compressor):
self.media_nodes[-1][1].split_content.append(data)
else:
node = self.__class__(content=self.parser.elem_str(elem),
context=self.context)
context=self.context)
node.split_content.append(data)
self.media_nodes.append((media, node))
return self.split_content
......
......@@ -12,14 +12,42 @@ class JsCompressor(Compressor):
def split_contents(self):
if self.split_content:
return self.split_content
self.extra_nodes = []
for elem in self.parser.js_elems():
attribs = self.parser.elem_attribs(elem)
if 'src' in attribs:
basename = self.get_basename(attribs['src'])
filename = self.get_filename(basename)
content = (SOURCE_FILE, filename, basename, elem)
self.split_content.append(content)
else:
content = self.parser.elem_content(elem)
self.split_content.append((SOURCE_HUNK, content, None, elem))
content = (SOURCE_HUNK, self.parser.elem_content(elem), None, elem)
self.split_content.append(content)
if 'async' in attribs:
extra = ' async'
elif 'defer' in attribs:
extra = ' defer'
else:
extra = ''
# Append to the previous node if it had the same attribute
append_to_previous = (self.extra_nodes and
self.extra_nodes[-1][0] == extra)
if append_to_previous and settings.COMPRESS_ENABLED:
self.extra_nodes[-1][1].split_content.append(content)
else:
node = self.__class__(content=self.parser.elem_str(elem),
context=self.context)
node.split_content.append(content)
self.extra_nodes.append((extra, node))
return self.split_content
def output(self, *args, **kwargs):
if (settings.COMPRESS_ENABLED or settings.COMPRESS_PRECOMPILERS or
kwargs.get('forced', False)):
self.split_contents()
if hasattr(self, 'nodes'):
ret = []
for extra, subnode in self.extra_nodes:
subnode.extra_context.update({'extra': extra})
ret.append(subnode.output(*args, **kwargs))
return '\n'.join(ret)
return super(JsCompressor, self).output(*args, **kwargs)
<script type="text/javascript" src="{{ compressed.url }}"></script>
\ No newline at end of file
<script type="text/javascript" src="{{ compressed.url }}"{{ compressed.extra }}></script>
\ No newline at end of file
hermanos = {}
\ No newline at end of file
pollos = {}
\ No newline at end of file
......@@ -276,3 +276,31 @@ class CacheBackendTestCase(CompressorTestCase):
def test_correct_backend(self):
from compressor.cache import cache
self.assertEqual(cache.__class__, locmem.LocMemCache)
class JsAsyncDeferTestCase(SimpleTestCase):
def setUp(self):
self.js = """\
<script src="/static/js/one.js" type="text/javascript"></script>
<script src="/static/js/two.js" type="text/javascript" async></script>
<script src="/static/js/three.js" type="text/javascript" defer></script>
<script type="text/javascript">obj.value = "value";</script>
<script src="/static/js/one.js" type="text/javascript" async></script>
<script src="/static/js/two.js" type="text/javascript" async></script>
<script src="/static/js/three.js" type="text/javascript"></script>"""
def test_js_output(self):
def extract_attr(tag):
if tag.has_attr('async'):
return 'async'
if tag.has_attr('defer'):
return 'defer'
js_node = JsCompressor(self.js)
output = [None, 'async', 'defer', None, 'async', None]
if six.PY3:
scripts = make_soup(js_node.output()).find_all('script')
attrs = [extract_attr(i) for i in scripts]
else:
scripts = make_soup(js_node.output()).findAll('script')
attrs = [s.get('async') or s.get('defer') for s in scripts]
self.assertEqual(output, attrs)
......@@ -65,8 +65,7 @@ class TestJinja2CompressorExtension(TestCase):
self.assertEqual(tag_body, template.render())
def test_empty_tag(self):
template = self.env.from_string("""{% compress js %}{% block js %}
{% endblock %}{% endcompress %}""")
template = self.env.from_string("""{% compress js %}{% block js %}{% endblock %}{% endcompress %}""")
context = {'STATIC_URL': settings.COMPRESS_URL}
self.assertEqual('', template.render(context))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment