Bug 1105052 - Update in-tree jsmin to commit a878bf0. rs=gps

This is a straight copy from

a878bf0ba0

paired with a tiny change to use the new quote_chars option.

--HG--
extra : rebase_source : 75d604ffafc7062c663bca4242af35546d2c1e3a
This commit is contained in:
Nick Alexander 2014-11-25 16:02:08 -08:00
parent 65f573bb9c
commit 7aa14436a6
4 changed files with 68 additions and 21 deletions

View File

@ -40,7 +40,7 @@ __all__ = ['jsmin', 'JavascriptMinify']
__version__ = '2.0.11'
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,17 +65,18 @@ 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:
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
@ -90,7 +91,7 @@ class JavascriptMinify(object):
space_strings = "abcdefghijklmnopqrstuvwxyz"\
"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_$\\"
starters, enders = '{[(+-', '}])+-"\''
starters, enders = '{[(+-', '}])+-' + self.quote_chars
newlinestart_strings = starters + space_strings
newlineend_strings = enders + space_strings
do_newline = False
@ -102,7 +103,7 @@ class JavascriptMinify(object):
in_re = False
in_quote = ''
quote_buf = []
previous = read(1)
if previous == '\\':
escape_slash_count += 1
@ -120,7 +121,7 @@ class JavascriptMinify(object):
elif not previous:
return
elif previous >= '!':
if previous in "'\"":
if previous in self.quote_chars:
in_quote = previous
write(previous)
previous_non_space = previous
@ -200,7 +201,7 @@ class JavascriptMinify(object):
if previous != '\\' or (not escape_slash_count % 2) or next2 in 'gimy':
in_re = False
write('/')
elif next2 == '/':
elif next2 == '/':
doing_single_comment = True
previous_before_comment = previous_non_space
elif next2 == '*':
@ -221,7 +222,7 @@ class JavascriptMinify(object):
do_newline = False
write(next1)
if not in_re and next1 in "'\"":
if not in_re and next1 in self.quote_chars:
in_quote = next1
quote_buf = []

View File

@ -12,8 +12,8 @@ class JsTests(unittest.TestCase):
raise AssertionError
return True
def assertMinified(self, js_input, expected):
minified = jsmin.jsmin(js_input)
def assertMinified(self, js_input, expected, **kwargs):
minified = jsmin.jsmin(js_input, **kwargs)
assert minified == expected, "%r != %r" % (minified, expected)
def testQuoted(self):
@ -150,7 +150,7 @@ another thing;"""
hoverclass: options.hoverclass
}
// fix for gecko engine
// 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}
@ -179,7 +179,7 @@ Element.cleanWhitespace(element);"""
"""
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, '/');
@ -187,13 +187,13 @@ Element.cleanWhitespace(element);"""
"""
expected = r"""myString.replace(/\\/g,'/');console.log("hi");"""
self.assertMinified(js, expected)
js = r''' return /^data:image\//i.test(url) ||
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) {
@ -295,7 +295,7 @@ var foo = "hey";
'''
expected = '''{a:n/2,}'''
self.assertMinified(original, expected)
def testReturn(self):
original = '''
return foo;//comment
@ -347,9 +347,48 @@ var foo = "hey";
expected = 'var msie;'
self.assertMinified(original, expected)
def test_angular_4(self):
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="'\"`")
if __name__ == '__main__':
unittest.main()

View File

@ -13,9 +13,12 @@ setup(
name="jsmin",
version=re.search(r'__version__ = ["\']([^"\']+)', open('jsmin/__init__.py').read()).group(1),
packages=['jsmin'],
description='JavaScript minifier.',
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/',
@ -26,7 +29,11 @@ setup(
'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',

View File

@ -609,7 +609,7 @@ class MinifiedJavaScript(BaseFile):
def open(self):
output = BytesIO()
minify = JavascriptMinify(self._file.open(), output)
minify = JavascriptMinify(self._file.open(), output, quote_chars="'\"`")
minify.minify()
output.seek(0)