Commit 8eab4284 by Timothée Peignier

Merge pull request #470 from lydell/safe-js-concat

Safely concatenate JavaScript
parents 7079de7a 2af0561d
...@@ -142,7 +142,11 @@ class Compressor(object): ...@@ -142,7 +142,11 @@ class Compressor(object):
def concatenate(self, paths): def concatenate(self, paths):
"""Concatenate together a list of files""" """Concatenate together a list of files"""
return "\n".join([self.read_text(path) for path in paths]) # Note how a semicolon is added between the two files to make sure that
# their behavior is not changed. '(expression1)\n(expression2)' calls
# `expression1` with `expression2` as an argument! Superfluos semicolons
# are valid in JavaScript and will be removed by the minifier.
return "\n;".join([self.read_text(path) for path in paths])
def construct_asset_path(self, asset_path, css_path, output_filename, variant=None): def construct_asset_path(self, asset_path, css_path, output_filename, variant=None):
"""Return a rewritten asset URL for a stylesheet""" """Return a rewritten asset URL for a stylesheet"""
......
function concat() { (function() {
console.log(arguments); window.concat = function() {
} console.log(arguments);
}
}()) // No semicolon
function cat() { (function() {
console.log("hello world"); window.cat = function() {
} console.log("hello world");
}
}());
...@@ -46,7 +46,7 @@ class CompressorTest(TestCase): ...@@ -46,7 +46,7 @@ class CompressorTest(TestCase):
_('pipeline/js/first.js'), _('pipeline/js/first.js'),
_('pipeline/js/second.js') _('pipeline/js/second.js')
]) ])
self.assertEqual("""function concat() {\n console.log(arguments);\n}\n\nfunction cat() {\n console.log("hello world");\n}\n""", js) self.assertEqual("""(function() {\n window.concat = function() {\n console.log(arguments);\n }\n}()) // No semicolon\n\n;(function() {\n window.cat = function() {\n console.log("hello world");\n }\n}());\n""", js)
@patch.object(base64, 'b64encode') @patch.object(base64, 'b64encode')
def test_encoded_content(self, mock): def test_encoded_content(self, mock):
......
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