This file is indexed.

/usr/lib/python2.7/dist-packages/slimit/tests/test_minifier.py is in python-slimit 0.8.1-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
###############################################################################
#
# Copyright (c) 2011 Ruslan Spivak
#
# 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.
#
###############################################################################

__author__ = 'Ruslan Spivak <ruslan.spivak@gmail.com>'

import unittest

from slimit import minify


class MinifierTestCase(unittest.TestCase):

    def assertMinified(self, source, expected):
        minified = minify(source)
        self.maxDiff = None
        self.assertSequenceEqual(minified, expected)

    TEST_CASES = [
        ("""
        jQuery.fn = jQuery.prototype = {
                // For internal use only.
                _data: function( elem, name, data ) {
                        return jQuery.data( elem, name, data, true );
                }
        };
        """,
         'jQuery.fn=jQuery.prototype={_data:function(elem,name,data){return jQuery.data(elem,name,data,true);}};'),

        ('context = context instanceof jQuery ? context[0] : context;',
         'context=context instanceof jQuery?context[0]:context;'
         ),

        ("""
        /*
        * A number of helper functions used for managing events.
        * Many of the ideas behind this code originated from
        * Dean Edwards' addEvent library.
        */
        if ( elem && elem.parentNode ) {
                // Handle the case where IE and Opera return items
                // by name instead of ID
                if ( elem.id !== match[2] ) {
                        return rootjQuery.find( selector );
                }

                // Otherwise, we inject the element directly into the jQuery object
                this.length = 1;
                this[0] = elem;
        }
        """,

         'if(elem&&elem.parentNode){if(elem.id!==match[2])return rootjQuery.find(selector);this.length=1;this[0]=elem;}'
         ),

        ("""
        var a = function( obj ) {
                for ( var name in obj ) {
                        return false;
                }
                return true;
        };
        """,
         'var a=function(obj){for(var name in obj)return false;return true;};'
         ),

        ("""
        x = "string", y = 5;

        (x = 5) ? true : false;

        for (p in obj)
        ;

        if (true)
          val = null;
        else
          val = false;

        """,
         'x="string",y=5;(x=5)?true:false;for(p in obj);if(true)val=null;else val=false;'
         ),

        # for loops + empty statement in loop body
        ("""
        for (x = 0; true; x++)
        ;
        for (; true; x++)
        ;
        for (x = 0, y = 5; true; x++)
        ;

        y = (x + 5) * 20;

        """,
         'for(x=0;true;x++);for(;true;x++);for(x=0,y=5;true;x++);y=(x+5)*20;'),


        # unary expressions
        ("""
        delete x;
        typeof x;
        void x;
        x += (!y)++;
        """,
         'delete x;typeof x;void x;x+=(!y)++;'),

        # label + break label + continue label
        ("""
        label:
        if ( i == 0 )
          continue label;
        switch (day) {
        case 5:
          break ;
        default:
          break label;
        }
        """,
         'label:if(i==0)continue label;switch(day){case 5:break;default:break label;}'),

        # break + continue: no labels
        ("""
        while (i <= 7) {
          if ( i == 3 )
              continue;
          if ( i == 0 )
              break;
        }
        """,
         'while(i<=7){if(i==3)continue;if(i==0)break;}'),

        # regex + one line statements in if and if .. else
        ("""
        function a(x, y) {
         var re = /ab+c/;
         if (x == 1)
           return x + y;
         if (x == 3)
           return {x: 1};
         else
           return;
        }
        """,
         'function a(x,y){var re=/ab+c/;if(x==1)return x+y;if(x==3)return{x:1};else return;}'),

        # new
        ('return new jQuery.fn.init( selector, context, rootjQuery );',
         'return new jQuery.fn.init(selector,context,rootjQuery);'
         ),

        # no space after 'else' when the next token is (, {
        ("""
        if (true) {
          x = true;
          y = 3;
        } else {
          x = false
          y = 5
        }
        """,
         'if(true){x=true;y=3;}else{x=false;y=5;}'),

        ("""
        if (true) {
          x = true;
          y = 3;
        } else
          (x + ' qw').split(' ');
        """,
         "if(true){x=true;y=3;}else(x+' qw').split(' ');"),


        ##############################################################
        # Block braces removal
        ##############################################################

        # do while
        ('do { x += 1; } while(true);', 'do x+=1;while(true);'),
        # do while: multiple statements
        ('do { x += 1; y += 1;} while(true);', 'do{x+=1;y+=1;}while(true);'),

        # elision
        ('var a = [1, 2, 3, ,,,5];', 'var a=[1,2,3,,,,5];'),

        # with
        ("""
        with (obj) {
          a = b;
        }
        """,
         'with(obj)a=b;'),

        # with: multiple statements
        ("""
        with (obj) {
          a = b;
          c = d;
        }
        """,
         'with(obj){a=b;c=d;}'),

        # if else
        ("""
        if (true) {
          x = true;
        } else {
          x = false
        }
        """,
         'if(true)x=true;else x=false;'),

        # if: multiple statements
        ("""
        if (true) {
          x = true;
          y = false;
        } else {
          x = false;
          y = true;
        }
        """,
         'if(true){x=true;y=false;}else{x=false;y=true;}'),

        # try catch finally: one statement
        ("""
        try {
          throw "my_exception"; // generates an exception
        }
        catch (e) {
          // statements to handle any exceptions
          log(e); // pass exception object to error handler
        }
        finally {
          closefiles(); // always close the resource
        }
        """,
         'try{throw "my_exception";}catch(e){log(e);}finally{closefiles();}'
         ),

        # try catch finally: no statements
        ("""
        try {
        }
        catch (e) {
        }
        finally {
        }
        """,
         'try{}catch(e){}finally{}'
         ),

        # try catch finally: multiple statements
        ("""
        try {
          x = 3;
          y = 5;
        }
        catch (e) {
          log(e);
          log('e');
        }
        finally {
          z = 7;
          log('z');
        }
        """,
         "try{x=3;y=5;}catch(e){log(e);log('e');}finally{z=7;log('z');}"
         ),

        # tricky case with an 'if' nested in 'if .. else'
        # We need to preserve braces in the first 'if' otherwise
        # 'else' might get associated with nested 'if' instead
        ("""
        if ( obj ) {
                for ( n in obj ) {
                        if ( v === false) {
                                break;
                        }
                }
        } else {
                for ( ; i < l; ) {
                        if ( nv === false ) {
                                break;
                        }
                }
        }
        """,
         'if(obj){for(n in obj)if(v===false)break;}else for(;i<l;)if(nv===false)break;'),

        # We don't care about nested 'if' when enclosing 'if' block
        # contains multiple statements because braces won't be removed
        # by visit_Block when there are multiple statements in the block
        ("""
        if ( obj ) {
                for ( n in obj ) {
                        if ( v === false) {
                                break;
                        }
                }
                x = 5;
        } else {
                for ( ; i < l; ) {
                        if ( nv === false ) {
                                break;
                        }
                }
        }
        """,
         'if(obj){for(n in obj)if(v===false)break;x=5;}else for(;i<l;)if(nv===false)break;'),


        # No dangling 'else' - remove braces
        ("""
        if ( obj ) {
                for ( n in obj ) {
                        if ( v === false) {
                                break;
                        } else {
                                n = 3;
                        }
                }
        } else {
                for ( ; i < l; ) {
                        if ( nv === false ) {
                                break;
                        }
                }
        }
        """,
         'if(obj)for(n in obj)if(v===false)break;else n=3;else for(;i<l;)if(nv===false)break;'),

        # foo["bar"] --> foo.bar
        ('foo["bar"];', 'foo.bar;'),
        ("foo['bar'];", 'foo.bar;'),
        ("""foo['bar"']=42;""", """foo['bar"']=42;"""),
        ("""foo["bar'"]=42;""", """foo["bar'"]=42;"""),
        ('foo["bar bar"];', 'foo["bar bar"];'),
        ('foo["bar"+"bar"];', 'foo["bar"+"bar"];'),
        # https://github.com/rspivak/slimit/issues/34
        # test some reserved keywords
        ('foo["for"];', 'foo["for"];'),
        ('foo["class"];', 'foo["class"];'),


        # https://github.com/rspivak/slimit/issues/21
        # c||(c=393,a=323,b=2321); --> c||c=393,a=323,b=2321; ERROR
        ('c||(c=393);', 'c||(c=393);'),
        ('c||(c=393,a=323,b=2321);', 'c||(c=393,a=323,b=2321);'),

        # https://github.com/rspivak/slimit/issues/25
        ('for(a?b:c;d;)e=1;', 'for(a?b:c;d;)e=1;'),

        # https://github.com/rspivak/slimit/issues/26
        ('"begin"+ ++a+"end";', '"begin"+ ++a+"end";'),

        # https://github.com/rspivak/slimit/issues/28
        ("""
         (function($) {
             $.hello = 'world';
         }(jQuery));
         """,
         "(function($){$.hello='world';}(jQuery));"),

        # function call in FOR init
        ('for(o(); i < 3; i++) {}', 'for(o();i<3;i++){}'),

        # unary increment operator in FOR init
        ('for(i++; i < 3; i++) {}', 'for(i++;i<3;i++){}'),

        # unary decrement operator in FOR init
        ('for(i--; i < 3; i++) {}', 'for(i--;i<3;i++){}'),

        # issue-37, simple identifier in FOR init
        ('for(i; i < 3; i++) {}', 'for(i;i<3;i++){}'),

        # https://github.com/rspivak/slimit/issues/32
        ("""
         Name.prototype = {
           getPageProp: function Page_getPageProp(key) {
             return this.pageDict.get(key);
           },

           get fullName() {
             return this.first + " " + this.last;
           },

           set fullName(name) {
             var names = name.split(" ");
             this.first = names[0];
             this.last = names[1];
           }
         };
         """,
         ('Name.prototype={getPageProp:function Page_getPageProp(key){'
          'return this.pageDict.get(key);},'
          'get fullName(){return this.first+" "+this.last;},'
          'set fullName(name){var names=name.split(" ");this.first=names[0];'
          'this.last=names[1];}};')
        ),
        ]


def make_test_function(input, expected):

    def test_func(self):
        self.assertMinified(input, expected)

    return test_func

for index, (input, expected) in enumerate(MinifierTestCase.TEST_CASES):
    func = make_test_function(input, expected)
    setattr(MinifierTestCase, 'test_case_%d' % index, func)