[z3-checkins] r36093 - z3/jsonserver/branch/merge/concatresource/compression/thirdparty
reebalazs at codespeak.net
reebalazs at codespeak.net
Mon Jan 1 15:31:10 CET 2007
Author: reebalazs
Date: Mon Jan 1 15:31:05 2007
New Revision: 36093
Modified:
z3/jsonserver/branch/merge/concatresource/compression/thirdparty/packer.py
Log:
Update javascript compression to newest packer.py
Modified: z3/jsonserver/branch/merge/concatresource/compression/thirdparty/packer.py
==============================================================================
--- z3/jsonserver/branch/merge/concatresource/compression/thirdparty/packer.py (original)
+++ z3/jsonserver/branch/merge/concatresource/compression/thirdparty/packer.py Mon Jan 1 15:31:05 2007
@@ -1,7 +1,7 @@
#
# packer.py
#
-# Copyright (c) 2006 Florian Schulze <florian.schulze at gmx.net>
+# Copyright (c) 2006-2007 Florian Schulze <florian.schulze at gmx.net>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
@@ -21,7 +21,7 @@
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
-import re
+import re, unittest, textwrap
class KeywordMapper:
@@ -368,44 +368,554 @@
self.protect(r'''("(?:\\"|\\\n|.)*?")''')
# strip whitespace
self.sub(r'^[ \t\r\f\v]*(.*?)[ \t\r\f\v]*$', r'\1', re.MULTILINE)
- # remove comment contents
- self.sub(r'/\*.*?( ?[\\/*]*\*/)', r'/*\1', re.DOTALL)
- # remove lines with comments only (consisting of stars only)
- self.sub(r'^/\*+\*/$', '', re.MULTILINE)
+ if level == 'full':
+ # remove comments
+ self.sub(r'/\*.*? ?[\\/*]*\*/', r'', re.DOTALL)
+ #remove more whitespace
+ self.sub(r'\s*([{,;:])\s+', r'\1')
+ else:
+ # remove comment contents
+ self.sub(r'/\*.*?( ?[\\/*]*\*/)', r'/*\1', re.DOTALL)
+ # remove lines with comments only (consisting of stars only)
+ self.sub(r'^/\*+\*/$', '', re.MULTILINE)
# excessive newlines
self.sub(r'\n+', '\n')
# first newline
self.sub(r'^\n', '')
- if level == 'full':
- #remove more whitespace
- self.sub(r'([{,;])\s+', r'\1')
-## jspacker = JavascriptPacker('safe')
-## jspacker_full = JavascriptPacker('full')
+# be aware that the initial indentation gets removed in the following tests,
+# the inner indentation is preserved though (see textwrap.dedent)
+js_compression_tests = (
+ (
+ 'standardJS',
+ """\
+ /* a comment */
+
+ function dummy() {
+
+ var localvar = 10 // one line comment
+
+ document.write(localvar);
+ return 'bar'
+ }
+ """,
+ """\
+ function dummy(){var localvar=10
+ document.write(localvar);return 'bar'}
+ """,
+ 'safe'
+ ),
+ (
+ 'standardJS',
+ """\
+ /* a comment */
+
+ function dummy() {
+
+ var localvar = 10 // one line comment
+
+ document.write(localvar);
+ return 'bar'
+ }
+ """,
+ """\
+ function dummy(){var localvar=10
+ document.write(localvar);return'bar'}""",
+ 'full'
+ ),
+ (
+ 'stringProtection',
+ """
+ var leafnode = this.shared.xmldata.selectSingleNode('//*[@selected]');
+ var portal_url = 'http://127.0.0.1:9080/plone';
+ """,
+ """var leafnode=this.shared.xmldata.selectSingleNode('//*[@selected]');var portal_url='http://127.0.0.1:9080/plone';"""
+ ),
+ (
+ 'newlinesInStrings',
+ r"""var message = "foo: " + foo + "\nbar: " + bar;""",
+ r"""var message="foo: "+foo+"\nbar: "+bar;"""
+ ),
+ (
+ 'escapedStrings',
+ r"""var message = "foo: \"something in quotes\"" + foo + "\nbar: " + bar;""",
+ r"""var message="foo: \"something in quotes\""+foo+"\nbar: "+bar;"""
+ ),
+ (
+ 'whitspaceAroundPlus',
+ """\
+ var message = foo + bar;
+ message = foo++ + bar;
+ message = foo + ++bar;
+ """,
+ """\
+ var message=foo+bar;message=foo++ +bar;message=foo+ ++bar;"""
+ ),
+ (
+ 'whitspaceAroundMinus',
+ """\
+ var message = foo - bar;
+ message = foo-- - bar;
+ message = foo - --bar;
+ """,
+ """\
+ var message=foo-bar;message=foo-- -bar;message=foo- --bar;"""
+ ),
+ (
+ 'missingSemicolon',
+ """\
+ var x = function() {
+
+ } /* missing ; here */
+ next_instr;
+ """,
+ """\
+ var x=function(){}
+ next_instr;""",
+ 'safe'
+ ),
+ # be aware that the following produces invalid code. You *have* to add
+ # a semicolon after a '}' followed by a normal instruction
+ (
+ 'missingSemicolon',
+ """\
+ var x = function() {
+
+ } /* missing ; here */
+ next_instr;
+ """,
+ """\
+ var x=function(){}next_instr;""",
+ 'full'
+ ),
+ # excessive semicolons after curly brackets get removed
+ (
+ 'nestedCurlyBracketsWithSemicolons',
+ """\
+ function dummy(a, b) {
+ if (a > b) {
+ do something
+ } else {
+ do something else
+ };
+ };
+ next_instr;
+ """,
+ """\
+ function dummy(a,b){if(a>b){do something} else{do something else}};next_instr;""",
+ 'safe'
+ ),
+ (
+ 'nestedCurlyBracketsWithSemicolons',
+ """\
+ function dummy(a, b) {
+ if (a > b) {
+ do something
+ } else {
+ do something else
+ };
+ };
+ next_instr;
+ """,
+ """\
+ function dummy(a,b){if(a>b){do something}else{do something else}};next_instr;""",
+ 'full'
+ ),
+)
+
+
+css_safe_compression_tests = (
+ (
+ 'commentCompression',
+ """
+ /* this is a comment */
+ #testElement {
+ property: value; /* another comment */
+ }
+ /**********/
+ /* this is a multi
+ line comment */
+ #testElement {
+ /* yet another comment */
+ property: value;
+ }
+ """,
+ """\
+ /* */
+ #testElement {
+ property: value; /* */
+ }
+ /* */
+ #testElement {
+ /* */
+ property: value;
+ }
+ """
+ ),
+ (
+ 'newlineCompression',
+ """
+
+
+ /* this is a comment */
+
+ #testElement {
+ property: value; /* another comment */
+ }
+
+ /* this is a multi
+ line comment */
+ #testElement {
+
+ /* yet another comment */
+ property: value;
+
+ }
+
+
+ """,
+ """\
+ /* */
+ #testElement {
+ property: value; /* */
+ }
+ /* */
+ #testElement {
+ /* */
+ property: value;
+ }
+ """
+ ),
+ # see http://www.dithered.com/css_filters/index.html
+ (
+ 'commentHacks1',
+ """
+ #testElement {
+ property/**/: value;
+ property/* */: value;
+ property /**/: value;
+ property: /**/value;
+ }
+ """,
+ """\
+ #testElement {
+ property/**/: value;
+ property/* */: value;
+ property /**/: value;
+ property: /**/value;
+ }
+ """
+ ),
+ (
+ 'commentHacks2',
+ """
+ selector/* */ { }
+ """,
+ """\
+ selector/* */ { }
+ """
+ ),
+ (
+ 'commentHacks3',
+ """
+ selector/* foobar */ { }
+ """,
+ """\
+ selector/* */ { }
+ """
+ ),
+ (
+ 'commentHacks4',
+ """
+ selector/**/ { }
+ """,
+ """\
+ selector/**/ { }
+ """
+ ),
+ (
+ 'commentHacks5',
+ """
+ /* \*/
+ rules
+ /* */
+ """,
+ """\
+ /* \*/
+ rules
+ /* */
+ """
+ ),
+ (
+ 'commentHacks6',
+ """
+ /* foobar \*/
+ rules
+ /* */
+ """,
+ """\
+ /* \*/
+ rules
+ /* */
+ """
+ ),
+ (
+ 'commentHacks7',
+ """
+ /*/*/
+ rules
+ /* */
+ """,
+ """\
+ /*/*/
+ rules
+ /* */
+ """
+ ),
+ (
+ 'commentHacks8',
+ """
+ /*/*//*/
+ rules
+ /* */
+ """,
+ """\
+ /*/*//*/
+ rules
+ /* */
+ """
+ ),
+ (
+ 'stringProtection',
+ """
+ /* test string protection */
+ #selector,
+ #another {
+ content: 'foo; bar';
+ }
+ """,
+ """\
+ /* */
+ #selector,
+ #another {
+ content: 'foo; bar';
+ }
+ """
+ ),
+)
+
+css_full_compression_tests = (
+ (
+ 'commentCompression',
+ """
+ /* this is a comment */
+ #testElement {
+ property: value; /* another comment */
+ }
+ /**********/
+ /* this is a multi
+ line comment */
+ #testElement {
+ /* yet another comment */
+ property: value;
+ }
+ """,
+ """\
+ #testElement{property:value;}
+ #testElement{property:value;}
+ """
+ ),
+ (
+ 'newlineCompression',
+ """
+
+
+ /* this is a comment */
+
+ #testElement {
+ property: value; /* another comment */
+ }
+
+ /* this is a multi
+ line comment */
+ #testElement {
+
+ /* yet another comment */
+ property: value;
+
+ }
+
+
+ """,
+ """\
+ #testElement{property:value;}
+ #testElement{property:value;}
+ """
+ ),
+ # see http://www.dithered.com/css_filters/index.html
+ # in full compression all hacks get removed
+ (
+ 'commentHacks1',
+ """
+ #testElement {
+ property/**/: value;
+ property/* */: value;
+ property /**/: value;
+ property: /**/value;
+ }
+ """,
+ """\
+ #testElement{property:value;property:value;property:value;property:value;}
+ """
+ ),
+ (
+ 'commentHacks2',
+ """
+ selector/* */ { }
+ """,
+ """\
+ selector{}
+ """
+ ),
+ (
+ 'commentHacks3',
+ """
+ selector/* foobar */ { }
+ """,
+ """\
+ selector{}
+ """
+ ),
+ (
+ 'commentHacks4',
+ """
+ selector/**/ { }
+ """,
+ """\
+ selector{}
+ """
+ ),
+ (
+ 'commentHacks5',
+ """
+ /* \*/
+ rules
+ /* */
+ """,
+ """\
+ rules
+ """
+ ),
+ (
+ 'commentHacks6',
+ """
+ /* foobar \*/
+ rules
+ /* */
+ """,
+ """\
+ rules
+ """
+ ),
+ (
+ 'commentHacks7',
+ """
+ /*/*/
+ rules
+ /* */
+ """,
+ """\
+ rules
+ """
+ ),
+ (
+ 'commentHacks8',
+ """
+ /*/*//*/
+ rules
+ /* */
+ """,
+ """\
+ rules
+ """
+ ),
+ (
+ 'stringProtection',
+ """
+ /* test string protection and full compression */
+ #selector,
+ #another {
+ content: 'foo; bar';
+ }
+ """,
+ """\
+ #selector,#another{content:'foo; bar';}
+ """
+ ),
+)
+
+class PackerTestCase(unittest.TestCase):
+ def __init__(self, name, input, output, packer):
+ unittest.TestCase.__init__(self)
+ self.name = name
+ self.input = input
+ self.output = output
+ self.packer = packer
+
+ def __str__(self):
+ return self.name
+
+ def runTest(self):
+ self.assertEqual(self.packer.pack(self.input), self.output)
+
+
+def test_suite():
+ suite = unittest.TestSuite()
+
+ jspacker = {
+ 'safe': JavascriptPacker('safe'),
+ 'full': JavascriptPacker('full'),
+ }
+ csspacker = {
+ 'safe': CSSPacker('safe'),
+ 'full': CSSPacker('full'),
+ }
+
+ for info in js_compression_tests:
+ name = info[0]
+ input = textwrap.dedent(info[1])
+ output = textwrap.dedent(info[2])
+ if (len(info) == 4):
+ compression = info[3].split(",")
+ else:
+ compression = ("safe", "full")
+
+ for packer in compression:
+ suite.addTest(PackerTestCase("%s (%s)" % (name, packer),
+ input, output,
+ jspacker[packer]))
+
+ packer = "safe"
+ for name, input, output in css_safe_compression_tests:
+ input = textwrap.dedent(input)
+ output = textwrap.dedent(output)
+
+ suite.addTest(PackerTestCase("%s (%s)" % (name, packer),
+ input, output,
+ csspacker[packer]))
+
+ packer = "full"
+ for name, input, output in css_full_compression_tests:
+ input = textwrap.dedent(input)
+ output = textwrap.dedent(output)
+
+ suite.addTest(PackerTestCase("%s (%s)" % (name, packer),
+ input, output,
+ csspacker[packer]))
-## def run():
- ## script = open('cssQuery.js').read()
- ## script = jspacker_full.pack(script)
- ## open('output.js','w').write(script)
- ## mapper = JavascriptKeywordMapper()
- ## mapper.analyse(script)
- ## keywords = mapper.getKeywords()
- ## script = mapper.sub(script)
- ## f = open('output1.js','w')
- ## #f.write("keywords='%s'.split('|');\n" % "|".join(keywords))
- ## #f.write(mapper.getDecodeFunction(name='__dEcOdE'))
- ## f.write(mapper.getDecoder(script))
- ## for index, keyword in enumerate(keywords):
- ## encoded = mapper._encode(index)
- ## if keyword == '':
- ## replacement = encoded
- ## else:
- ## replacement = keyword
- ## regexp = re.compile(r'\b%s\b' % encoded)
- ## script = regexp.sub(lambda m: replacement, script)
- ## open('output2.js','w').write(script)
+ return suite
-## if __name__=='__main__':
- ## run()
+if __name__ == '__main__':
+ unittest.main(defaultTest='test_suite')
More information about the z3-checkins
mailing list