diff options
author | Christopher Baines <mail@cbaines.net> | 2015-12-22 16:55:48 +0000 |
---|---|---|
committer | Christopher Baines <mail@cbaines.net> | 2015-12-22 16:55:48 +0000 |
commit | 05199198d690228f2a030c0f007e23819a997f0b (patch) | |
tree | 5c15f671edecb369469e1ee68548f912b958e197 | |
download | python-jsmin-05199198d690228f2a030c0f007e23819a997f0b.tar python-jsmin-05199198d690228f2a030c0f007e23819a997f0b.tar.gz |
Import python-jsmin_2.0.9.orig.tar.gz
-rw-r--r-- | LICENSE.txt | 23 | ||||
-rw-r--r-- | MANIFEST.in | 1 | ||||
-rw-r--r-- | PKG-INFO | 92 | ||||
-rw-r--r-- | README.rst | 67 | ||||
-rw-r--r-- | jsmin.egg-info/PKG-INFO | 92 | ||||
-rw-r--r-- | jsmin.egg-info/SOURCES.txt | 12 | ||||
-rw-r--r-- | jsmin.egg-info/dependency_links.txt | 1 | ||||
-rw-r--r-- | jsmin.egg-info/top_level.txt | 1 | ||||
-rw-r--r-- | jsmin/__init__.py | 233 | ||||
-rw-r--r-- | jsmin/__main__.py | 10 | ||||
-rw-r--r-- | jsmin/test.py | 317 | ||||
-rw-r--r-- | setup.cfg | 5 | ||||
-rw-r--r-- | setup.py | 42 |
13 files changed, 896 insertions, 0 deletions
diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..193a853 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,23 @@ +The MIT License (MIT) + +Copyright (c) 2013 Dave St.Germain + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + + diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..ab30e9a --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1 @@ +include *.txt diff --git a/PKG-INFO b/PKG-INFO new file mode 100644 index 0000000..bbba187 --- /dev/null +++ b/PKG-INFO @@ -0,0 +1,92 @@ +Metadata-Version: 1.1 +Name: jsmin +Version: 2.0.9 +Summary: JavaScript minifier. +PLEASE UPDATE TO VERSION >= 2.0.6. Older versions have a serious bug related to comments. +Home-page: https://bitbucket.org/dcs/jsmin/ +Author: Tikitu de Jager +Author-email: tikitu+jsmin@logophile.org +License: MIT License +Description: ===== + jsmin + ===== + + JavaScript minifier. + + Usage + ===== + + .. code:: python + + from jsmin import jsmin + with open('myfile.js') as js_file: + minified = jsmin(js_file.read()) + + You can run it as a commandline tool also:: + + python -m jsmin myfile.js + + Where to get it + =============== + + * install the package `from pypi <https://pypi.python.org/pypi/jsmin/>`_ + * get the latest release `from the stable branch on bitbucket <https://bitbucket.org/dcs/jsmin/branch/stable>`_ + * get the development version `from the default branch on bitbucket <https://bitbucket.org/dcs/jsmin/branch/default>`_ + + Contributing + ============ + + `Issues <https://bitbucket.org/dcs/jsmin/issues>`_ and `Pull requests <https://bitbucket.org/dcs/jsmin/pull-requests>`_ + will be gratefully received on Bitbucket. Pull requests on github are great too, but the issue tracker lives on + bitbucket. + + If possible, please make separate pull requests for tests and for code: tests will be committed on the stable branch + (which tracks the latest released version) while code will go to default by, erm, default. + + Unless you request otherwise, your Bitbucket identity will be added to the contributor's list below; if you prefer a + different name feel free to add it in your pull request instead. (If you prefer not to be mentioned you'll have to let + the maintainer know somehow.) + + Build/test status + ================= + + Both default and stable branches are tested with Travis: https://travis-ci.org/tikitu/jsmin + + Stable (latest released version plus any new tests) is tested against CPython 2.6, 2.7, 3.2, and 3.3. + Currently: + + .. image:: https://travis-ci.org/tikitu/jsmin.png?branch=ghstable + + If stable is failing that means there's a new test that fails on *the latest released version on pypi*, with no fix yet + released. + + Default (development version, might be ahead of latest released version) is tested against CPython 2.6, 2.7, 3.2, and + 3.3. Currently: + + .. image:: https://travis-ci.org/tikitu/jsmin.png?branch=master + + If default is failing don't use it, but as long as stable is passing the pypi release should be ok. + + Contributors (chronological commit order) + ========================================= + + * `Dave St.Germain <https://bitbucket.org/dcs>`_ (original author) + * `Hans weltar <https://bitbucket.org/hansweltar>`_ + * `Tikitu de Jager <mailto:tikitu+jsmin@logophile.org>`_ (current maintainer) + * https://bitbucket.org/rennat + +Platform: UNKNOWN +Classifier: Development Status :: 5 - Production/Stable +Classifier: Environment :: Web Environment +Classifier: Intended Audience :: Developers +Classifier: License :: OSI Approved :: MIT License +Classifier: Operating System :: OS Independent +Classifier: Programming Language :: Python :: 2 +Classifier: Programming Language :: Python :: 2.6 +Classifier: Programming Language :: Python :: 2.7 +Classifier: Programming Language :: Python :: 3 +Classifier: Programming Language :: Python :: 3.2 +Classifier: Programming Language :: Python :: 3.3 +Classifier: Topic :: Internet :: WWW/HTTP :: Dynamic Content +Classifier: Topic :: Software Development :: Pre-processors +Classifier: Topic :: Text Processing :: Filters diff --git a/README.rst b/README.rst new file mode 100644 index 0000000..ade024a --- /dev/null +++ b/README.rst @@ -0,0 +1,67 @@ +===== +jsmin +===== + +JavaScript minifier. + +Usage +===== + +.. code:: python + + from jsmin import jsmin + with open('myfile.js') as js_file: + minified = jsmin(js_file.read()) + +You can run it as a commandline tool also:: + + python -m jsmin myfile.js + +Where to get it +=============== + +* install the package `from pypi <https://pypi.python.org/pypi/jsmin/>`_ +* get the latest release `from the stable branch on bitbucket <https://bitbucket.org/dcs/jsmin/branch/stable>`_ +* get the development version `from the default branch on bitbucket <https://bitbucket.org/dcs/jsmin/branch/default>`_ + +Contributing +============ + +`Issues <https://bitbucket.org/dcs/jsmin/issues>`_ and `Pull requests <https://bitbucket.org/dcs/jsmin/pull-requests>`_ +will be gratefully received on Bitbucket. Pull requests on github are great too, but the issue tracker lives on +bitbucket. + +If possible, please make separate pull requests for tests and for code: tests will be committed on the stable branch +(which tracks the latest released version) while code will go to default by, erm, default. + +Unless you request otherwise, your Bitbucket identity will be added to the contributor's list below; if you prefer a +different name feel free to add it in your pull request instead. (If you prefer not to be mentioned you'll have to let +the maintainer know somehow.) + +Build/test status +================= + +Both default and stable branches are tested with Travis: https://travis-ci.org/tikitu/jsmin + +Stable (latest released version plus any new tests) is tested against CPython 2.6, 2.7, 3.2, and 3.3. +Currently: + +.. image:: https://travis-ci.org/tikitu/jsmin.png?branch=ghstable + +If stable is failing that means there's a new test that fails on *the latest released version on pypi*, with no fix yet +released. + +Default (development version, might be ahead of latest released version) is tested against CPython 2.6, 2.7, 3.2, and +3.3. Currently: + +.. image:: https://travis-ci.org/tikitu/jsmin.png?branch=master + +If default is failing don't use it, but as long as stable is passing the pypi release should be ok. + +Contributors (chronological commit order) +========================================= + +* `Dave St.Germain <https://bitbucket.org/dcs>`_ (original author) +* `Hans weltar <https://bitbucket.org/hansweltar>`_ +* `Tikitu de Jager <mailto:tikitu+jsmin@logophile.org>`_ (current maintainer) +* https://bitbucket.org/rennat diff --git a/jsmin.egg-info/PKG-INFO b/jsmin.egg-info/PKG-INFO new file mode 100644 index 0000000..bbba187 --- /dev/null +++ b/jsmin.egg-info/PKG-INFO @@ -0,0 +1,92 @@ +Metadata-Version: 1.1 +Name: jsmin +Version: 2.0.9 +Summary: JavaScript minifier. +PLEASE UPDATE TO VERSION >= 2.0.6. Older versions have a serious bug related to comments. +Home-page: https://bitbucket.org/dcs/jsmin/ +Author: Tikitu de Jager +Author-email: tikitu+jsmin@logophile.org +License: MIT License +Description: ===== + jsmin + ===== + + JavaScript minifier. + + Usage + ===== + + .. code:: python + + from jsmin import jsmin + with open('myfile.js') as js_file: + minified = jsmin(js_file.read()) + + You can run it as a commandline tool also:: + + python -m jsmin myfile.js + + Where to get it + =============== + + * install the package `from pypi <https://pypi.python.org/pypi/jsmin/>`_ + * get the latest release `from the stable branch on bitbucket <https://bitbucket.org/dcs/jsmin/branch/stable>`_ + * get the development version `from the default branch on bitbucket <https://bitbucket.org/dcs/jsmin/branch/default>`_ + + Contributing + ============ + + `Issues <https://bitbucket.org/dcs/jsmin/issues>`_ and `Pull requests <https://bitbucket.org/dcs/jsmin/pull-requests>`_ + will be gratefully received on Bitbucket. Pull requests on github are great too, but the issue tracker lives on + bitbucket. + + If possible, please make separate pull requests for tests and for code: tests will be committed on the stable branch + (which tracks the latest released version) while code will go to default by, erm, default. + + Unless you request otherwise, your Bitbucket identity will be added to the contributor's list below; if you prefer a + different name feel free to add it in your pull request instead. (If you prefer not to be mentioned you'll have to let + the maintainer know somehow.) + + Build/test status + ================= + + Both default and stable branches are tested with Travis: https://travis-ci.org/tikitu/jsmin + + Stable (latest released version plus any new tests) is tested against CPython 2.6, 2.7, 3.2, and 3.3. + Currently: + + .. image:: https://travis-ci.org/tikitu/jsmin.png?branch=ghstable + + If stable is failing that means there's a new test that fails on *the latest released version on pypi*, with no fix yet + released. + + Default (development version, might be ahead of latest released version) is tested against CPython 2.6, 2.7, 3.2, and + 3.3. Currently: + + .. image:: https://travis-ci.org/tikitu/jsmin.png?branch=master + + If default is failing don't use it, but as long as stable is passing the pypi release should be ok. + + Contributors (chronological commit order) + ========================================= + + * `Dave St.Germain <https://bitbucket.org/dcs>`_ (original author) + * `Hans weltar <https://bitbucket.org/hansweltar>`_ + * `Tikitu de Jager <mailto:tikitu+jsmin@logophile.org>`_ (current maintainer) + * https://bitbucket.org/rennat + +Platform: UNKNOWN +Classifier: Development Status :: 5 - Production/Stable +Classifier: Environment :: Web Environment +Classifier: Intended Audience :: Developers +Classifier: License :: OSI Approved :: MIT License +Classifier: Operating System :: OS Independent +Classifier: Programming Language :: Python :: 2 +Classifier: Programming Language :: Python :: 2.6 +Classifier: Programming Language :: Python :: 2.7 +Classifier: Programming Language :: Python :: 3 +Classifier: Programming Language :: Python :: 3.2 +Classifier: Programming Language :: Python :: 3.3 +Classifier: Topic :: Internet :: WWW/HTTP :: Dynamic Content +Classifier: Topic :: Software Development :: Pre-processors +Classifier: Topic :: Text Processing :: Filters diff --git a/jsmin.egg-info/SOURCES.txt b/jsmin.egg-info/SOURCES.txt new file mode 100644 index 0000000..7131d58 --- /dev/null +++ b/jsmin.egg-info/SOURCES.txt @@ -0,0 +1,12 @@ +LICENSE.txt +MANIFEST.in +README.rst +setup.cfg +setup.py +jsmin/__init__.py +jsmin/__main__.py +jsmin/test.py +jsmin.egg-info/PKG-INFO +jsmin.egg-info/SOURCES.txt +jsmin.egg-info/dependency_links.txt +jsmin.egg-info/top_level.txt
\ No newline at end of file diff --git a/jsmin.egg-info/dependency_links.txt b/jsmin.egg-info/dependency_links.txt new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/jsmin.egg-info/dependency_links.txt @@ -0,0 +1 @@ + diff --git a/jsmin.egg-info/top_level.txt b/jsmin.egg-info/top_level.txt new file mode 100644 index 0000000..79abaa9 --- /dev/null +++ b/jsmin.egg-info/top_level.txt @@ -0,0 +1 @@ +jsmin diff --git a/jsmin/__init__.py b/jsmin/__init__.py new file mode 100644 index 0000000..23bed60 --- /dev/null +++ b/jsmin/__init__.py @@ -0,0 +1,233 @@ +# This code is original from jsmin by Douglas Crockford, it was translated to +# Python by Baruch Even. It was rewritten by Dave St.Germain for speed. +# +# The MIT License (MIT) +# +# Copyright (c) 2013 Dave St.Germain +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. + + +import sys +is_3 = sys.version_info >= (3, 0) +if is_3: + import io +else: + import StringIO + try: + import cStringIO + except ImportError: + cStringIO = None + + +__all__ = ['jsmin', 'JavascriptMinify'] +__version__ = '2.0.9' + + +def jsmin(js): + """ + returns a minified version of the javascript string + """ + if not is_3: + if cStringIO and not isinstance(js, unicode): + # strings can use cStringIO for a 3x performance + # improvement, but unicode (in python2) cannot + klass = cStringIO.StringIO + else: + klass = StringIO.StringIO + else: + klass = io.StringIO + ins = klass(js) + outs = klass() + JavascriptMinify(ins, outs).minify() + return outs.getvalue() + + +class JavascriptMinify(object): + """ + Minify an input stream of javascript, writing + to an output stream + """ + + def __init__(self, instream=None, outstream=None): + self.ins = instream + self.outs = outstream + + def minify(self, instream=None, outstream=None): + if instream and outstream: + self.ins, self.outs = instream, outstream + + self.is_return = False + self.return_buf = '' + + def write(char): + # all of this is to support literal regular expressions. + # sigh + if char in 'return': + self.return_buf += char + self.is_return = self.return_buf == 'return' + self.outs.write(char) + if self.is_return: + self.return_buf = '' + + read = self.ins.read + + space_strings = "abcdefghijklmnopqrstuvwxyz"\ + "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_$\\" + starters, enders = '{[(+-', '}])+-"\'' + newlinestart_strings = starters + space_strings + newlineend_strings = enders + space_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 + 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: + 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: + quote_buf.append(next1) + + if next1 == in_quote: + numslashes = 0 + for c in reversed(quote_buf[:-1]): + if c != '\\': + break + else: + numslashes += 1 + if numslashes % 2 == 0: + 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: + if (previous_non_space in space_strings \ + or previous_non_space > '~') \ + and (next2 in space_strings or next2 > '~'): + do_space = True + elif previous_non_space in '-+' and next2 == previous_non_space: + # protect against + ++ or - -- sequences + do_space = True + elif self.is_return and next2 == '/': + # returning a regex... + write(' ') + 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 + elif next2 == '*': + doing_multi_comment = True + previous = next1 + next1 = next2 + next2 = read(1) + else: + in_re = previous_non_space in '(,=:[?!&|' or self.is_return # literal regular expression + write('/') + else: + if do_space: + do_space = False + write(' ') + if do_newline: + write('\n') + do_newline = False + + write(next1) + if not in_re and next1 in "'\"": + in_quote = next1 + quote_buf = [] + + previous = next1 + next1 = next2 + + if previous >= '!': + previous_non_space = previous + + if previous == '\\': + escape_slash_count += 1 + else: + escape_slash_count = 0 diff --git a/jsmin/__main__.py b/jsmin/__main__.py new file mode 100644 index 0000000..58da2e2 --- /dev/null +++ b/jsmin/__main__.py @@ -0,0 +1,10 @@ +import sys, os, glob +from jsmin import JavascriptMinify + +for f in sys.argv[1:]: + with open(f, 'r') as js: + minifier = JavascriptMinify(js, sys.stdout) + minifier.minify() + sys.stdout.write('\n') + + diff --git a/jsmin/test.py b/jsmin/test.py new file mode 100644 index 0000000..7171a5b --- /dev/null +++ b/jsmin/test.py @@ -0,0 +1,317 @@ +import unittest +import jsmin +import sys + +class JsTests(unittest.TestCase): + def _minify(self, js): + return jsmin.jsmin(js) + + def assertEqual(self, thing1, thing2): + if thing1 != thing2: + print(repr(thing1), repr(thing2)) + raise AssertionError + return True + + def assertMinified(self, js_input, expected): + minified = jsmin.jsmin(js_input) + assert minified == expected, "%r != %r" % (minified, expected) + + def testQuoted(self): + js = r''' + Object.extend(String, { + interpret: function(value) { + return value == null ? '' : String(value); + }, + specialChar: { + '\b': '\\b', + '\t': '\\t', + '\n': '\\n', + '\f': '\\f', + '\r': '\\r', + '\\': '\\\\' + } + }); + + ''' + expected = r"""Object.extend(String,{interpret:function(value){return value==null?'':String(value);},specialChar:{'\b':'\\b','\t':'\\t','\n':'\\n','\f':'\\f','\r':'\\r','\\':'\\\\'}});""" + self.assertMinified(js, expected) + + def testSingleComment(self): + js = r'''// use native browser JS 1.6 implementation if available + if (Object.isFunction(Array.prototype.forEach)) + Array.prototype._each = Array.prototype.forEach; + + if (!Array.prototype.indexOf) Array.prototype.indexOf = function(item, i) { + + // hey there + function() {// testing comment + foo; + //something something + + location = 'http://foo.com;'; // goodbye + } + //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 + self.assertMinified(js, expected) + + def testEmpty(self): + self.assertMinified('', '') + self.assertMinified(' ', '') + self.assertMinified('\n', '') + self.assertMinified('\r\n', '') + self.assertMinified('\t', '') + + + def testMultiComment(self): + js = r""" + function foo() { + print('hey'); + } + /* + if(this.options.zindex) { + this.originalZ = parseInt(Element.getStyle(this.element,'z-index') || 0); + this.element.style.zIndex = this.options.zindex; + } + */ + another thing; + """ + expected = r"""function foo(){print('hey');} +another thing;""" + self.assertMinified(js, expected) + + def testLeadingComment(self): + js = r"""/* here is a comment at the top + + it ends here */ + function foo() { + alert('crud'); + } + + """ + expected = r"""function foo(){alert('crud');}""" + self.assertMinified(js, expected) + + def testBlockCommentStartingWithSlash(self): + self.assertMinified('A; /*/ comment */ B', 'A;B') + + def testBlockCommentEndingWithSlash(self): + self.assertMinified('A; /* comment /*/ B', 'A;B') + + def testLeadingBlockCommentStartingWithSlash(self): + self.assertMinified('/*/ comment */ A', 'A') + + def testLeadingBlockCommentEndingWithSlash(self): + self.assertMinified('/* comment /*/ A', 'A') + + def testEmptyBlockComment(self): + self.assertMinified('/**/ A', 'A') + + def testBlockCommentMultipleOpen(self): + self.assertMinified('/* A /* B */ C', 'C') + + def testJustAComment(self): + self.assertMinified(' // a comment', '') + + def test_issue_10(self): + js = ''' + files = [{name: value.replace(/^.*\\\\/, '')}]; + // comment + A + ''' + expected = '''files=[{name:value.replace(/^.*\\\\/,'')}]; A''' + self.assertMinified(js, expected) + + def testRe(self): + js = r''' + var str = this.replace(/\\./g, '@').replace(/"[^"\\\n\r]*"/g, ''); + return (/^[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]*$/).test(str); + });''' + expected = r"""var str=this.replace(/\\./g,'@').replace(/"[^"\\\n\r]*"/g,'');return(/^[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]*$/).test(str);});""" + self.assertMinified(js, expected) + + def testIgnoreComment(self): + js = 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 + } + + // fix for gecko engine + 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} +Element.cleanWhitespace(element);""" + self.assertMinified(js, expected) + + def testHairyRe(self): + js = r""" + inspect: function(useDoubleQuotes) { + var escapedString = this.gsub(/[\x00-\x1f\\]/, function(match) { + var character = String.specialChar[match[0]]; + return character ? character : '\\u00' + match[0].charCodeAt().toPaddedString(2, 16); + }); + if (useDoubleQuotes) return '"' + escapedString.replace(/"/g, '\\"') + '"'; + return "'" + escapedString.replace(/'/g, '\\\'') + "'"; + }, + + toJSON: function() { + return this.inspect(true); + }, + + unfilterJSON: function(filter) { + return this.sub(filter || Prototype.JSONFilter, '#{1}'); + }, + """ + expected = r"""inspect:function(useDoubleQuotes){var escapedString=this.gsub(/[\x00-\x1f\\]/,function(match){var character=String.specialChar[match[0]];return character?character:'\\u00'+match[0].charCodeAt().toPaddedString(2,16);});if(useDoubleQuotes)return'"'+escapedString.replace(/"/g,'\\"')+'"';return"'"+escapedString.replace(/'/g,'\\\'')+"'";},toJSON:function(){return this.inspect(true);},unfilterJSON:function(filter){return this.sub(filter||Prototype.JSONFilter,'#{1}');},""" + self.assertMinified(js, expected) + + def testLiteralRe(self): + js = r""" + myString.replace(/\\/g, '/'); + console.log("hi"); + """ + expected = r"""myString.replace(/\\/g,'/');console.log("hi");""" + self.assertMinified(js, expected) + + js = r''' return /^data:image\//i.test(url) || + /^(https?|ftp|file|about|chrome|resource):/.test(url); + ''' + expected = r'''return /^data:image\//i.test(url)||/^(https?|ftp|file|about|chrome|resource):/.test(url);''' + self.assertMinified(js, expected) + + def testNoBracesWithComment(self): + js = r""" + onSuccess: function(transport) { + var js = transport.responseText.strip(); + if (!/^\[.*\]$/.test(js)) // TODO: improve sanity check + throw 'Server returned an invalid collection representation.'; + this._collection = eval(js); + this.checkForExternalText(); + }.bind(this), + onFailure: this.onFailure + }); + """ + 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) + + def testSpaceInRe(self): + js = r""" + num = num.replace(/ /g,''); + """ + self.assertMinified(js, "num=num.replace(/ /g,'');") + + def testEmptyString(self): + js = r''' + function foo('') { + + } + ''' + self.assertMinified(js, "function foo(''){}") + + def testDoubleSpace(self): + js = r''' +var foo = "hey"; + ''' + self.assertMinified(js, 'var foo="hey";') + + def testLeadingRegex(self): + js = r'/[d]+/g ' + self.assertMinified(js, js.strip()) + + def testLeadingString(self): + js = r"'a string in the middle of nowhere'; // and a comment" + self.assertMinified(js, "'a string in the middle of nowhere';") + + def testSingleCommentEnd(self): + js = r'// a comment\n' + self.assertMinified(js, '') + + def testInputStream(self): + try: + from StringIO import StringIO + except ImportError: + from io import StringIO + + ins = StringIO(r''' + function foo('') { + + } + ''') + outs = StringIO() + m = jsmin.JavascriptMinify() + m.minify(ins, outs) + output = outs.getvalue() + assert output == "function foo(''){}" + + def testUnicode(self): + instr = u'\u4000 //foo' + expected = u'\u4000' + output = jsmin.jsmin(instr) + self.assertEqual(output, expected) + + def testCommentBeforeEOF(self): + self.assertMinified("//test\r\n", "") + + def testCommentInObj(self): + self.assertMinified("""{ + a: 1,//comment + }""", "{a:1,}") + + def testCommentInObj2(self): + self.assertMinified("{a: 1//comment\r\n}", "{a:1\n}") + + def testImplicitSemicolon(self): + # return \n 1 is equivalent with return; 1 + # so best make sure jsmin retains the newline + self.assertMinified("return;//comment\r\na", "return;a") + + def testImplicitSemicolon2(self): + self.assertMinified("return//comment...\r\na", "return\na") + + def testSingleComment2(self): + self.assertMinified('x.replace(/\//, "_")// slash to underscore', + 'x.replace(/\//,"_")') + + def testSlashesNearComments(self): + original = ''' + { a: n / 2, } + // comment + ''' + expected = '''{a:n/2,}''' + self.assertMinified(original, expected) + + def testReturn(self): + original = ''' + return foo;//comment + return bar;''' + expected = 'return foo; return bar;' + self.assertMinified(original, expected) + + def test_space_plus(self): + original = '"s" + ++e + "s"' + expected = '"s"+ ++e+"s"' + self.assertMinified(original, expected) + + def test_no_final_newline(self): + original = '"s"' + expected = '"s"' + self.assertMinified(original, expected) + +if __name__ == '__main__': + unittest.main() diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..861a9f5 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,5 @@ +[egg_info] +tag_build = +tag_date = 0 +tag_svn_revision = 0 + diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..8fff566 --- /dev/null +++ b/setup.py @@ -0,0 +1,42 @@ +from setuptools import setup + +import os, sys, re + +os.environ['COPYFILE_DISABLE'] = 'true' # this disables including resource forks in tar files on os x + + +extra = {} +if sys.version_info >= (3,0): + extra['use_2to3'] = True + +setup( + name="jsmin", + version=re.search(r'__version__ = ["\']([^"\']+)', open('jsmin/__init__.py').read()).group(1), + packages=['jsmin'], + description='JavaScript minifier.\nPLEASE UPDATE TO VERSION >= 2.0.6. Older versions have a serious bug related to comments.', + long_description=open('README.rst').read(), + author='Dave St.Germain', + author_email='dave@st.germa.in', + maintainer='Tikitu de Jager', + maintainer_email='tikitu+jsmin@logophile.org', + test_suite='jsmin.test.JsTests', + license='MIT License', + url='https://bitbucket.org/dcs/jsmin/', + classifiers=[ + 'Development Status :: 5 - Production/Stable', + 'Environment :: Web Environment', + 'Intended Audience :: Developers', + 'License :: OSI Approved :: MIT License', + 'Operating System :: OS Independent', + 'Programming Language :: Python :: 2', + 'Programming Language :: Python :: 2.6', + 'Programming Language :: Python :: 2.7', + 'Programming Language :: Python :: 3', + 'Programming Language :: Python :: 3.2', + 'Programming Language :: Python :: 3.3', + 'Topic :: Internet :: WWW/HTTP :: Dynamic Content', + 'Topic :: Software Development :: Pre-processors', + 'Topic :: Text Processing :: Filters', + ], + **extra +) |