aboutsummaryrefslogtreecommitdiff
path: root/jsmin
diff options
context:
space:
mode:
authorChristopher Baines <mail@cbaines.net>2015-12-22 17:03:30 +0000
committerChristopher Baines <mail@cbaines.net>2015-12-22 17:03:30 +0000
commit3f41d4ade1401421756550dc0cdf0046677c7f08 (patch)
tree99b02d064ba6b357b51e53cc2df6a10fd44999ae /jsmin
parent05199198d690228f2a030c0f007e23819a997f0b (diff)
downloadpython-jsmin-3f41d4ade1401421756550dc0cdf0046677c7f08.tar
python-jsmin-3f41d4ade1401421756550dc0cdf0046677c7f08.tar.gz
Import python-jsmin_2.2.0.orig.tar.gzupstream-2.2.0upstream
Diffstat (limited to 'jsmin')
-rw-r--r--jsmin/__init__.py226
-rw-r--r--jsmin/test.py324
2 files changed, 438 insertions, 112 deletions
diff --git a/jsmin/__init__.py b/jsmin/__init__.py
index 23bed60..ed79e8e 100644
--- a/jsmin/__init__.py
+++ b/jsmin/__init__.py
@@ -37,10 +37,10 @@ else:
__all__ = ['jsmin', 'JavascriptMinify']
-__version__ = '2.0.9'
+__version__ = '2.2.0'
-def jsmin(js):
+def jsmin(js, **kwargs):
"""
returns a minified version of the javascript string
"""
@@ -55,7 +55,7 @@ def jsmin(js):
klass = io.StringIO
ins = klass(js)
outs = klass()
- JavascriptMinify(ins, outs).minify()
+ JavascriptMinify(ins, outs, **kwargs).minify()
return outs.getvalue()
@@ -65,9 +65,10 @@ class JavascriptMinify(object):
to an output stream
"""
- def __init__(self, instream=None, outstream=None):
+ def __init__(self, instream=None, outstream=None, quote_chars="'\""):
self.ins = instream
self.outs = outstream
+ self.quote_chars = quote_chars
def minify(self, instream=None, outstream=None):
if instream and outstream:
@@ -82,6 +83,8 @@ class JavascriptMinify(object):
if char in 'return':
self.return_buf += char
self.is_return = self.return_buf == 'return'
+ else:
+ self.return_buf = ''
self.outs.write(char)
if self.is_return:
self.return_buf = ''
@@ -90,70 +93,26 @@ class JavascriptMinify(object):
space_strings = "abcdefghijklmnopqrstuvwxyz"\
"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_$\\"
- starters, enders = '{[(+-', '}])+-"\''
- newlinestart_strings = starters + space_strings
- newlineend_strings = enders + space_strings
+ self.space_strings = space_strings
+ starters, enders = '{[(+-', '}])+-/' + self.quote_chars
+ newlinestart_strings = starters + space_strings + self.quote_chars
+ newlineend_strings = enders + space_strings + self.quote_chars
+ self.newlinestart_strings = newlinestart_strings
+ self.newlineend_strings = newlineend_strings
+
do_newline = False
do_space = False
escape_slash_count = 0
- doing_single_comment = False
- previous_before_comment = ''
- doing_multi_comment = False
- in_re = False
in_quote = ''
quote_buf = []
-
- previous = read(1)
- if previous == '\\':
- escape_slash_count += 1
+
+ previous = ';'
+ previous_non_space = ';'
next1 = read(1)
- if previous == '/':
- if next1 == '/':
- doing_single_comment = True
- elif next1 == '*':
- doing_multi_comment = True
- previous = next1
- next1 = read(1)
- else:
- write(previous)
- elif not previous:
- return
- elif previous >= '!':
- if previous in "'\"":
- in_quote = previous
- write(previous)
- previous_non_space = previous
- else:
- previous_non_space = ' '
- if not next1:
- return
- while 1:
+ while next1:
next2 = read(1)
- if not next2:
- last = next1.strip()
- if not (doing_single_comment or doing_multi_comment)\
- and last not in ('', '/'):
- if in_quote:
- write(''.join(quote_buf))
- write(last)
- break
- if doing_multi_comment:
- if next1 == '*' and next2 == '/':
- doing_multi_comment = False
- next2 = read(1)
- elif doing_single_comment:
- if next1 in '\r\n':
- doing_single_comment = False
- while next2 in '\r\n':
- next2 = read(1)
- if not next2:
- break
- if previous_before_comment in ')}]':
- do_newline = True
- elif previous_before_comment in space_strings:
- write('\n')
- elif in_quote:
+ if in_quote:
quote_buf.append(next1)
if next1 == in_quote:
@@ -167,19 +126,9 @@ class JavascriptMinify(object):
in_quote = ''
write(''.join(quote_buf))
elif next1 in '\r\n':
- if previous_non_space in newlineend_strings \
- or previous_non_space > '~':
- while 1:
- if next2 < '!':
- next2 = read(1)
- if not next2:
- break
- else:
- if next2 in newlinestart_strings \
- or next2 > '~' or next2 == '/':
- do_newline = True
- break
- elif next1 < '!' and not in_re:
+ next2, do_newline = self.newline(
+ previous_non_space, next2, do_newline)
+ elif next1 < '!':
if (previous_non_space in space_strings \
or previous_non_space > '~') \
and (next2 in space_strings or next2 > '~'):
@@ -193,41 +142,126 @@ class JavascriptMinify(object):
elif next1 == '/':
if do_space:
write(' ')
- if in_re:
- if previous != '\\' or (not escape_slash_count % 2) or next2 in 'gimy':
- in_re = False
- write('/')
- elif next2 == '/':
- doing_single_comment = True
- previous_before_comment = previous_non_space
+ if next2 == '/':
+ # Line comment: treat it as a newline, but skip it
+ next2 = self.line_comment(next1, next2)
+ next1 = '\n'
+ next2, do_newline = self.newline(
+ previous_non_space, next2, do_newline)
elif next2 == '*':
- doing_multi_comment = True
- previous = next1
- next1 = next2
+ self.block_comment(next1, next2)
next2 = read(1)
+ if previous_non_space in space_strings:
+ do_space = True
+ next1 = previous
else:
- in_re = previous_non_space in '(,=:[?!&|' or self.is_return # literal regular expression
- write('/')
+ if previous_non_space in '{(,=:[?!&|;' or self.is_return:
+ self.regex_literal(next1, next2)
+ # hackish: after regex literal next1 is still /
+ # (it was the initial /, now it's the last /)
+ next2 = read(1)
+ else:
+ write('/')
else:
- if do_space:
- do_space = False
- write(' ')
if do_newline:
write('\n')
do_newline = False
-
+ do_space = False
+ if do_space:
+ do_space = False
+ write(' ')
+
write(next1)
- if not in_re and next1 in "'\"":
+ if next1 in self.quote_chars:
in_quote = next1
quote_buf = []
- previous = next1
- next1 = next2
+ if next1 >= '!':
+ previous_non_space = next1
- if previous >= '!':
- previous_non_space = previous
-
- if previous == '\\':
+ if next1 == '\\':
escape_slash_count += 1
else:
escape_slash_count = 0
+
+ previous = next1
+ next1 = next2
+
+ def regex_literal(self, next1, next2):
+ assert next1 == '/' # otherwise we should not be called!
+
+ self.return_buf = ''
+
+ read = self.ins.read
+ write = self.outs.write
+
+ in_char_class = False
+
+ write('/')
+
+ next = next2
+ while next != '/' or in_char_class:
+ write(next)
+ if next == '\\':
+ write(read(1)) # whatever is next is escaped
+ elif next == '[':
+ write(read(1)) # character class cannot be empty
+ in_char_class = True
+ elif next == ']':
+ in_char_class = False
+ next = read(1)
+
+ write('/')
+
+ def line_comment(self, next1, next2):
+ assert next1 == next2 == '/'
+
+ read = self.ins.read
+
+ while next1 and next1 not in '\r\n':
+ next1 = read(1)
+ while next1 and next1 in '\r\n':
+ next1 = read(1)
+
+ return next1
+
+ def block_comment(self, next1, next2):
+ assert next1 == '/'
+ assert next2 == '*'
+
+ read = self.ins.read
+
+ # Skip past first /* and avoid catching on /*/...*/
+ next1 = read(1)
+ next2 = read(1)
+
+ comment_buffer = '/*'
+ while next1 != '*' or next2 != '/':
+ comment_buffer += next1
+ next1 = next2
+ next2 = read(1)
+
+ if comment_buffer.startswith("/*!"):
+ # comment needs preserving
+ self.outs.write(comment_buffer)
+ self.outs.write("*/\n")
+
+
+ def newline(self, previous_non_space, next2, do_newline):
+ read = self.ins.read
+
+ if previous_non_space and (
+ previous_non_space in self.newlineend_strings
+ or previous_non_space > '~'):
+ while 1:
+ if next2 < '!':
+ next2 = read(1)
+ if not next2:
+ break
+ else:
+ if next2 in self.newlinestart_strings \
+ or next2 > '~' or next2 == '/':
+ do_newline = True
+ break
+
+ return next2, do_newline
diff --git a/jsmin/test.py b/jsmin/test.py
index 7171a5b..1638cf9 100644
--- a/jsmin/test.py
+++ b/jsmin/test.py
@@ -1,6 +1,6 @@
import unittest
import jsmin
-import sys
+
class JsTests(unittest.TestCase):
def _minify(self, js):
@@ -12,9 +12,9 @@ class JsTests(unittest.TestCase):
raise AssertionError
return True
- def assertMinified(self, js_input, expected):
- minified = jsmin.jsmin(js_input)
- assert minified == expected, "%r != %r" % (minified, expected)
+ def assertMinified(self, js_input, expected, **kwargs):
+ minified = jsmin.jsmin(js_input, **kwargs)
+ assert minified == expected, "\ngot: %r\nexp: %r" % (minified, expected)
def testQuoted(self):
js = r'''
@@ -52,10 +52,8 @@ class JsTests(unittest.TestCase):
}
//bye
'''
- expected = r"""
-if(Object.isFunction(Array.prototype.forEach))
-Array.prototype._each=Array.prototype.forEach;if(!Array.prototype.indexOf)Array.prototype.indexOf=function(item,i){ function(){ foo; location='http://foo.com;';}"""
- # print expected
+ expected = r"""if(Object.isFunction(Array.prototype.forEach))
+Array.prototype._each=Array.prototype.forEach;if(!Array.prototype.indexOf)Array.prototype.indexOf=function(item,i){function(){foo;location='http://foo.com;';}"""
self.assertMinified(js, expected)
def testEmpty(self):
@@ -116,13 +114,22 @@ another thing;"""
def testJustAComment(self):
self.assertMinified(' // a comment', '')
- def test_issue_10(self):
+ def test_issue_bitbucket_10(self):
js = '''
files = [{name: value.replace(/^.*\\\\/, '')}];
// comment
A
'''
- expected = '''files=[{name:value.replace(/^.*\\\\/,'')}]; A'''
+ expected = '''files=[{name:value.replace(/^.*\\\\/,'')}];A'''
+ self.assertMinified(js, expected)
+
+ def test_issue_bitbucket_10_without_semicolon(self):
+ js = '''
+ files = [{name: value.replace(/^.*\\\\/, '')}]
+ // comment
+ A
+ '''
+ expected = '''files=[{name:value.replace(/^.*\\\\/,'')}]\nA'''
self.assertMinified(js, expected)
def testRe(self):
@@ -154,7 +161,7 @@ another thing;"""
Element.cleanWhitespace(element);
"""
expected = r"""var options_for_droppable={overlap:options.overlap,containment:options.containment,tree:options.tree,hoverclass:options.hoverclass,onHover:Sortable.onHover}
-var options_for_tree={onHover:Sortable.onEmptyHover,overlap:options.overlap,containment:options.containment,hoverclass:options.hoverclass}
+var options_for_tree={onHover:Sortable.onEmptyHover,overlap:options.overlap,containment:options.containment,hoverclass:options.hoverclass}
Element.cleanWhitespace(element);"""
self.assertMinified(js, expected)
@@ -206,10 +213,22 @@ Element.cleanWhitespace(element);"""
onFailure: this.onFailure
});
"""
- expected = r"""onSuccess:function(transport){var js=transport.responseText.strip();if(!/^\[.*\]$/.test(js))
+ expected = r"""onSuccess:function(transport){var js=transport.responseText.strip();if(!/^\[.*\]$/.test(js))
throw'Server returned an invalid collection representation.';this._collection=eval(js);this.checkForExternalText();}.bind(this),onFailure:this.onFailure});"""
self.assertMinified(js, expected)
-
+ js_without_comment = r"""
+ onSuccess: function(transport) {
+ var js = transport.responseText.strip();
+ if (!/^\[.*\]$/.test(js))
+ throw 'Server returned an invalid collection representation.';
+ this._collection = eval(js);
+ this.checkForExternalText();
+ }.bind(this),
+ onFailure: this.onFailure
+ });
+ """
+ self.assertMinified(js_without_comment, expected)
+
def testSpaceInRe(self):
js = r"""
num = num.replace(/ /g,'');
@@ -274,16 +293,22 @@ var foo = "hey";
}""", "{a:1,}")
def testCommentInObj2(self):
- self.assertMinified("{a: 1//comment\r\n}", "{a:1\n}")
+ self.assertMinified("{a: 1//comment\r\n}", "{a:1}")
def testImplicitSemicolon(self):
# return \n 1 is equivalent with return; 1
# so best make sure jsmin retains the newline
+ self.assertMinified("return\na", "return\na")
+
+ def test_explicit_semicolon(self):
self.assertMinified("return;//comment\r\na", "return;a")
def testImplicitSemicolon2(self):
+ self.assertMinified("return//comment...\r\nar", "return\nar")
+
+ def testImplicitSemicolon3(self):
self.assertMinified("return//comment...\r\na", "return\na")
-
+
def testSingleComment2(self):
self.assertMinified('x.replace(/\//, "_")// slash to underscore',
'x.replace(/\//,"_")')
@@ -300,7 +325,12 @@ var foo = "hey";
original = '''
return foo;//comment
return bar;'''
- expected = 'return foo; return bar;'
+ expected = 'return foo;return bar;'
+ self.assertMinified(original, expected)
+ original = '''
+ return foo
+ return bar;'''
+ expected = 'return foo\nreturn bar;'
self.assertMinified(original, expected)
def test_space_plus(self):
@@ -313,5 +343,267 @@ var foo = "hey";
expected = '"s"'
self.assertMinified(original, expected)
+ def test_space_with_regex_repeats(self):
+ original = '/(NaN| {2}|^$)/.test(a)&&(a="M 0 0");'
+ self.assertMinified(original, original) # there should be nothing jsmin can do here
+
+ def test_space_with_regex_repeats_not_at_start(self):
+ original = 'aaa;/(NaN| {2}|^$)/.test(a)&&(a="M 0 0");'
+ self.assertMinified(original, original) # there should be nothing jsmin can do here
+
+ def test_space_in_regex(self):
+ original = '/a (a)/.test("a")'
+ self.assertMinified(original, original)
+
+ def test_brackets_around_slashed_regex(self):
+ original = 'function a() { /\//.test("a") }'
+ expected = 'function a(){/\//.test("a")}'
+ self.assertMinified(original, expected)
+
+ def test_angular_1(self):
+ original = '''var /** holds major version number for IE or NaN for real browsers */
+ msie,
+ jqLite, // delay binding since jQuery could be loaded after us.'''
+ minified = jsmin.jsmin(original)
+ self.assertTrue('var\nmsie' in minified)
+
+ def test_angular_2(self):
+ original = 'var/* comment */msie;'
+ expected = 'var msie;'
+ self.assertMinified(original, expected)
+
+ def test_angular_3(self):
+ original = 'var /* comment */msie;'
+ expected = 'var msie;'
+ self.assertMinified(original, expected)
+
+ def test_angular_4(self):
+ original = 'var /* comment */ msie;'
+ expected = 'var msie;'
+ self.assertMinified(original, expected)
+
+ def test_angular_5(self):
+ original = 'a/b'
+ self.assertMinified(original, original)
+
+ def testBackticks(self):
+ original = '`test`'
+ self.assertMinified(original, original, quote_chars="'\"`")
+
+ original = '` test with leading whitespace`'
+ self.assertMinified(original, original, quote_chars="'\"`")
+
+ original = '`test with trailing whitespace `'
+ self.assertMinified(original, original, quote_chars="'\"`")
+
+ original = '''`test
+with a new line`'''
+ self.assertMinified(original, original, quote_chars="'\"`")
+
+ original = '''dumpAvStats: function(stats) {
+ var statsString = "";
+ if (stats.mozAvSyncDelay) {
+ statsString += `A/V sync: ${stats.mozAvSyncDelay} ms `;
+ }
+ if (stats.mozJitterBufferDelay) {
+ statsString += `Jitter-buffer delay: ${stats.mozJitterBufferDelay} ms`;
+ }
+
+ return React.DOM.div(null, statsString);'''
+ expected = 'dumpAvStats:function(stats){var statsString="";if(stats.mozAvSyncDelay){statsString+=`A/V sync: ${stats.mozAvSyncDelay} ms `;}\nif(stats.mozJitterBufferDelay){statsString+=`Jitter-buffer delay: ${stats.mozJitterBufferDelay} ms`;}\nreturn React.DOM.div(null,statsString);'
+ self.assertMinified(original, expected, quote_chars="'\"`")
+
+ def testBackticksExpressions(self):
+ original = '`Fifteen is ${a + b} and not ${2 * a + b}.`'
+ self.assertMinified(original, original, quote_chars="'\"`")
+
+ original = '''`Fifteen is ${a +
+b} and not ${2 * a + "b"}.`'''
+ self.assertMinified(original, original, quote_chars="'\"`")
+
+ def testBackticksTagged(self):
+ original = 'tag`Hello ${ a + b } world ${ a * b}`;'
+ self.assertMinified(original, original, quote_chars="'\"`")
+
+ def test_issue_bitbucket_16(self):
+ original = """
+ f = function() {
+ return /DataTree\/(.*)\//.exec(this._url)[1];
+ }
+ """
+ self.assertMinified(
+ original,
+ 'f=function(){return /DataTree\/(.*)\//.exec(this._url)[1];}')
+
+ def test_issue_bitbucket_17(self):
+ original = "// hi\n/^(get|post|head|put)$/i.test('POST')"
+ self.assertMinified(original,
+ "/^(get|post|head|put)$/i.test('POST')")
+
+ def test_issue_6(self):
+ original = '''
+ respond.regex = {
+ comments: /\/\*[^*]*\*+([^/][^*]*\*+)*\//gi,
+ urls: 'whatever'
+ };
+ '''
+ expected = original.replace(' ', '').replace('\n', '')
+ self.assertMinified(original, expected)
+
+ def test_issue_9(self):
+ original = '\n'.join([
+ 'var a = \'hi\' // this is a comment',
+ 'var a = \'hi\' /* this is also a comment */',
+ 'console.log(1) // this is a comment',
+ 'console.log(1) /* this is also a comment */',
+ '1 // this is a comment',
+ '1 /* this is also a comment */',
+ '{} // this is a comment',
+ '{} /* this is also a comment */',
+ '"YOLO" /* this is a comment */',
+ '"YOLO" // this is a comment',
+ '(1 + 2) // comment',
+ '(1 + 2) /* yup still comment */',
+ 'var b'
+ ])
+ expected = '\n'.join([
+ 'var a=\'hi\'',
+ 'var a=\'hi\'',
+ 'console.log(1)',
+ 'console.log(1)',
+ '1',
+ '1',
+ '{}',
+ '{}',
+ '"YOLO"',
+ '"YOLO"',
+ '(1+2)',
+ '(1+2)',
+ 'var b'
+ ])
+ self.assertMinified(expected, expected)
+ self.assertMinified(original, expected)
+
+ def test_newline_between_strings(self):
+ self.assertMinified('"yolo"\n"loyo"', '"yolo"\n"loyo"')
+
+ def test_issue_10_comments_between_tokens(self):
+ self.assertMinified('var/* comment */a', 'var a')
+
+ def test_ends_with_string(self):
+ self.assertMinified('var s = "s"', 'var s="s"')
+
+ def test_short_comment(self):
+ self.assertMinified('a;/**/b', 'a;b')
+
+ def test_shorter_comment(self):
+ self.assertMinified('a;/*/*/b', 'a;b')
+
+ def test_block_comment_with_semicolon(self):
+ self.assertMinified('a;/**/\nb', 'a;b')
+
+ def test_block_comment_With_implicit_semicolon(self):
+ self.assertMinified('a/**/\nvar b', 'a\nvar b')
+
+ def test_issue_9_single_comments(self):
+ original = '''
+ var a = "hello" // this is a comment
+ a += " world"
+ '''
+ self.assertMinified(original, 'var a="hello"\na+=" world"')
+
+ def test_issue_9_multi_comments(self):
+ original = '''
+ var a = "hello" /* this is a comment */
+ a += " world"
+ '''
+ self.assertMinified(original, 'var a="hello"\na+=" world"')
+
+ def test_issue_12_re_nl_if(self):
+ original = '''
+ var re = /\d{4}/
+ if (1) { console.log(2); }'''
+ self.assertMinified(
+ original, 'var re=/\d{4}/\nif(1){console.log(2);}')
+
+ def test_issue_12_re_nl_other(self):
+ original = '''
+ var re = /\d{4}/
+ g = 10'''
+ self.assertMinified(original , 'var re=/\d{4}/\ng=10')
+
+ def test_preserve_copyright(self):
+ original = '''
+ function this() {
+ /*! Copyright year person */
+ console.log('hello!');
+ }
+
+ /*! Copyright blah blah
+ *
+ * Some other text
+ */
+
+ var a;
+ '''
+ expected = """function this(){/*! Copyright year person */
+console.log('hello!');}/*! Copyright blah blah
+ *
+ * Some other text
+ */\n\nvar a;"""
+ self.assertMinified(original, expected)
+
+
+class RegexTests(unittest.TestCase):
+
+ def regex_recognise(self, js):
+ if not jsmin.is_3:
+ if jsmin.cStringIO and not isinstance(js, unicode):
+ # strings can use cStringIO for a 3x performance
+ # improvement, but unicode (in python2) cannot
+ klass = jsmin.cStringIO.StringIO
+ else:
+ klass = jsmin.StringIO.StringIO
+ else:
+ klass = jsmin.io.StringIO
+ ins = klass(js[2:])
+ outs = klass()
+ jsmin.JavascriptMinify(ins, outs).regex_literal(js[0], js[1])
+ return outs.getvalue()
+
+ def assert_regex(self, js_input, expected):
+ assert js_input[0] == '/' # otherwise we should not be testing!
+ recognised = self.regex_recognise(js_input)
+ assert recognised == expected, "\n in: %r\ngot: %r\nexp: %r" % (js_input, recognised, expected)
+
+ def test_simple(self):
+ self.assert_regex('/123/g', '/123/')
+
+ def test_character_class(self):
+ self.assert_regex('/a[0-9]b/g', '/a[0-9]b/')
+
+ def test_character_class_with_slash(self):
+ self.assert_regex('/a[/]b/g', '/a[/]b/')
+
+ def test_escaped_forward_slash(self):
+ self.assert_regex(r'/a\/b/g', r'/a\/b/')
+
+ def test_escaped_back_slash(self):
+ self.assert_regex(r'/a\\/g', r'/a\\/')
+
+ def test_empty_character_class(self):
+ # This one is subtle: an empty character class is not allowed, afaics
+ # from http://regexpal.com/ Chrome Version 44.0.2403.155 (64-bit) Mac
+ # so this char class is interpreted as containing ]/ *not* as char
+ # class [] followed by end-of-regex /.
+ self.assert_regex('/a[]/]b/g', '/a[]/]b/')
+
+ def test_precedence_of_parens(self):
+ # judging from
+ # http://regexpal.com/ Chrome Version 44.0.2403.155 (64-bit) Mac
+ # () have lower precedence than []
+ self.assert_regex('/a([)])b/g', '/a([)])b/')
+ self.assert_regex('/a[(]b/g', '/a[(]b/')
+
if __name__ == '__main__':
unittest.main()