Skip to content

Commit

Permalink
Merge pull request #470 from lydell/safe-js-concat
Browse files Browse the repository at this point in the history
Safely concatenate JavaScript
  • Loading branch information
cyberdelia committed Aug 1, 2015
2 parents 7079de7 + 2af0561 commit 8eab428
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
6 changes: 5 additions & 1 deletion pipeline/compressors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,11 @@ def reconstruct(match):

def concatenate(self, paths):
"""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):
"""Return a rewritten asset URL for a stylesheet"""
Expand Down
8 changes: 5 additions & 3 deletions tests/assets/js/first.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
function concat() {
console.log(arguments);
}
(function() {
window.concat = function() {
console.log(arguments);
}
}()) // No semicolon
8 changes: 5 additions & 3 deletions tests/assets/js/second.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
function cat() {
console.log("hello world");
}
(function() {
window.cat = function() {
console.log("hello world");
}
}());
2 changes: 1 addition & 1 deletion tests/tests/test_compressor.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def test_concatenate(self):
_('pipeline/js/first.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')
def test_encoded_content(self, mock):
Expand Down

0 comments on commit 8eab428

Please sign in to comment.