# 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 unittest import jsmin
class JsTests(unittest.TestCase): def _minify(self, js): return jsmin.jsmin(js)
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
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
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_no_final_newline(self):
original = '"s"'
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_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 testBackticksExpressions(self):
original = '`Fifteen is ${a + b} and not ${2 * a + b}.`'
self.assertMinified(original, original, quote_chars="'\"`")
original = '''`Fifteen is ${a +
b} andnot ${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_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_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)
def test_issue_14(self):
original = 'return x / 1;'
self.assertMinified(original, 'return x/1;')
def test_issue_14_with_char_from_return(self):
original = 'return r / 1;'
self.assertMinified(original, 'return r/1;')
class RegexTests(unittest.TestCase):
def regex_recognise(self, js): ifnot jsmin.is_3: if jsmin.cStringIO andnot 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_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()
Messung V0.5
¤ Dauer der Verarbeitung: 0.16 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.