File Coverage

blib/lib/JavaScript/Packer.pm
Criterion Covered Total %
statement 343 359 95.5
branch 107 130 82.3
condition 29 33 87.8
subroutine 25 25 100.0
pod 1 3 33.3
total 505 550 91.8


line stmt bran cond sub pod time code
1             package JavaScript::Packer;
2              
3 5     5   704254 use 5.008009;
  5         17  
4 5     5   25 use warnings;
  5         20  
  5         287  
5 5     5   32 use strict;
  5         8  
  5         113  
6 5     5   28 use Carp;
  5         14  
  5         373  
7 5     5   2676 use Regexp::RegGrp;
  5         18251  
  5         8921  
8              
9             # =========================================================================== #
10              
11             our $VERSION = "2.12";
12              
13             our @BOOLEAN_ACCESSORS = ( 'no_compress_comment', 'remove_copyright' );
14              
15             our @COPYRIGHT_ACCESSORS = ( 'copyright', 'copyright_comment' );
16              
17             our @COMPRESS_OPTS = ( 'clean', 'obfuscate', 'shrink', 'best' );
18             our $DEFAULT_COMPRESS = 'clean';
19              
20             our $PACKER_COMMENT = '\/\*\s*JavaScript::Packer\s*(\w+)\s*\*\/';
21             our $COPYRIGHT_COMMENT = '\/\*((?>[^\*]|\*[^\/])*copyright(?>[^\*]|\*[^\/])*)\*\/';
22              
23             our $RESTORE_PATTERN = qr~\x01(\d+)\x01~;
24             our $RESTORE_REPLACEMENT = "\x01%d\x01";
25              
26             our $SHRINK_VARS = {
27             BLOCK => qr/(((catch|do|if|while|with|function)\b[^~{};]*(\(\s*[^{};]*\s*\))\s*)?(\{[^{}]*\}))/, # function ( arg ) { ... }
28             ENCODED_BLOCK => qr/~#?(\d+)~/,
29             CALLER => qr/((?>[a-zA-Z0-9_\x24\.]+)\s*\([^\(\)]*\))(?=[,\)])/, # do_something( arg1, arg2 ) as argument of another function call
30             BRACKETS => qr/\{[^{}]*\}|\[[^\[\]]*\]|\([^\(\)]*\)|~[^~]+~/,
31             IDENTIFIER => qr~[a-zA-Z_\x24][a-zA-Z_0-9\\x24]*~,
32             SCOPED => qr/~#(\d+)~/,
33             VARS => qr~\b(?:var|function)\s+((?>[a-zA-Z0-9_\x24]+))~, # var x, funktion blah
34             PREFIX => qr~\x02~,
35             SHRUNK => qr~\x02\d+\b~
36             };
37              
38             our $BASE62_VARS = {
39             WORDS => qr~(\b[0-9a-zA-Z]\b|(?>[a-zA-Z0-9_]{2,}))~,
40             ENCODE10 => 'String',
41             ENCODE36 => 'function(c){return c.toString(36)}',
42             ENCODE62 => q~function(c){return(c<62?'':e(parseInt(c/62)))+((c=c%62)>35?String.fromCharCode(c+29):c.toString(36))}~,
43             UNPACK =>
44             q~eval(function(p,a,c,k,e,r){e=%s;if('0'.replace(0,e)==0){while(c--)r[e(c)]=k[c];k=[function(e){return r[e]||e}];e=function(){return'%s'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\\\b'+e(c)+'\\\\b','g'),k[c]);return p}('%s',%s,%d,'%s'.split('|'),0,{}))~
45             };
46              
47             our $DICTIONARY = {
48             STRING1 => qr~"(?>(?:(?>[^"\\]+)|\\.|\\")*)"~,
49             STRING2 => qr~'(?>(?:(?>[^'\\]+)|\\.|\\')*)'~,
50             STRING3 => qr~`(?>(?:(?>[^`\\]+)|\\.|\\`)*)`~,
51             REGEXP => qr~\/(\\[\/\\]|[^*\/])(\\.|[^\/\n\\])*\/[gim]*~,
52             OPERATOR => qr'return|typeof|[\[(\^=,{}:;&|!*?]',
53             CONDITIONAL => qr~\/\*\@\w*|\w*\@\*\/|\/\/\@\w*|\@(?>\w+)~,
54              
55             # single line comments
56             COMMENT1 => qr~(:?)\/\/([\@#])?([^\n]*)?\n~,
57              
58             # multline comments
59             COMMENT2 => qr~\/\*[^*]*\*+(?:[^\/][^*]*\*+)*\/~
60             };
61              
62             our $DATA = [
63             { regexp => $DICTIONARY->{STRING1} },
64             { regexp => $DICTIONARY->{STRING2} },
65             { regexp => $DICTIONARY->{STRING3} },
66             { regexp => $DICTIONARY->{CONDITIONAL} },
67             {
68             regexp => '(' . $DICTIONARY->{OPERATOR} . ')\s*(' . $DICTIONARY->{REGEXP} . ')',
69             replacement => sub {
70             return sprintf( "%s%s", $_[0]->{submatches}->[0], $_[0]->{submatches}->[1] );
71             },
72             },
73             ];
74              
75             our $COMMENTS = [
76             {
77             regexp => ';;;[^\n]*\n',
78             replacement => ''
79             },
80             { regexp => $DICTIONARY->{COMMENT1} . '\s*(' . $DICTIONARY->{REGEXP} . ')?', },
81             { regexp => '(' . $DICTIONARY->{COMMENT2} . ')\s*(' . $DICTIONARY->{REGEXP} . ')?' }
82             ];
83              
84             our $CLEAN = [
85             {
86             regexp => '\(\s*([^;)]*)\s*;\s*([^;)]*)\s*;\s*([^;)]*)\)',
87             replacement => sub { return sprintf( "(%s;%s;%s)", @{ $_[0]->{submatches} } ); }
88             },
89             { regexp => 'throw[^};]+[};]' },
90             {
91             regexp => ';+\s*([};])',
92             replacement => sub { return $_[0]->{submatches}->[0]; }
93             }
94             ];
95              
96             our $WHITESPACE = [
97             { regexp => '\/\/@[^\n]*\n' },
98             {
99             regexp => '@\s+\b',
100             replacement => '@ '
101             },
102             {
103             regexp => '\b\s+@',
104             replacement => ' @'
105             },
106             {
107             regexp => '(\d)\s+(\.\s*[a-z\x24_\[(])',
108             replacement => sub { return sprintf( "%s %s", @{ $_[0]->{submatches} } ); }
109             },
110             {
111             regexp => '([+-])\s+([+-])',
112             replacement => sub { return sprintf( "%s %s", @{ $_[0]->{submatches} } ); }
113             },
114             {
115             regexp => '(?>\s+)(\x24)(?>\s+)',
116             replacement => sub { return sprintf( " %s ", $_[0]->{submatches}->[0] ); }
117             },
118             {
119             regexp => '(\x24)(?>\s+)(?!=)',
120             replacement => sub { return sprintf( "%s ", $_[0]->{submatches}->[0] ); }
121             },
122             {
123             regexp => '(?\s+)(\x24)',
124             replacement => sub { return sprintf( " %s", $_[0]->{submatches}->[0] ); }
125             },
126             {
127             regexp => '\b\s+\b',
128             replacement => ' '
129             },
130             {
131             # Handle ternary operator with plus/minus literal in quotes
132             regexp => '(\?)\s*(["\'])([+\-])(["\'])\s*(:)',
133             replacement => sub { return sprintf( "%s%s%s%s%s", $_[0]->{submatches}->[0], $_[0]->{submatches}->[1], $_[0]->{submatches}->[2], $_[0]->{submatches}->[3], $_[0]->{submatches}->[4] ); }
134             },
135             {
136             # Keep space between quoted strings (handle the case of "string" + "string")
137             regexp => '(["\'])\s+\+\s+(["\'])',
138             replacement => sub { return sprintf( "%s+%s", $_[0]->{submatches}->[0], $_[0]->{submatches}->[1] ); }
139             },
140             {
141             regexp => '\s+',
142             replacement => ''
143             }
144             ];
145              
146             our $TRIM = [
147             {
148             regexp => '(\d)(?:\|\d)+\|(\d)',
149             replacement => sub { return sprintf( "%d-%d", $_[0]->{submatches}->[0] || 0, $_[0]->{submatches}->[1] || 0 ); }
150             },
151             {
152             regexp => '([a-z])(?:\|[a-z])+\|([a-z])',
153             replacement => sub { return sprintf( "%s-%s", $_[0]->{submatches}->[0], $_[0]->{submatches}->[1] ); }
154             },
155             {
156             regexp => '([A-Z])(?:\|[A-Z])+\|([A-Z])',
157             replacement => sub { return sprintf( "%s-%s", $_[0]->{submatches}->[0], $_[0]->{submatches}->[1] ); }
158             },
159             {
160             regexp => '\|',
161             replacement => ''
162             }
163             ];
164              
165             our @REGGRPS = ( 'comments', 'clean', 'whitespace', 'concat', 'trim', 'data_store', 'concat_store' );
166              
167             # --------------------------------------------------------------------------- #
168              
169             {
170 5     5   37 no strict 'refs';
  5         9  
  5         26369  
171              
172             foreach my $field ( @BOOLEAN_ACCESSORS ) {
173             next if defined *{ __PACKAGE__ . '::' . $field }{CODE};
174              
175             *{ __PACKAGE__ . '::' . $field } = sub {
176 119     119   312 my ( $self, $value ) = @_;
177              
178 119 100       329 $self->{ '_' . $field } = $value ? 1 : undef if ( defined( $value ) );
    100          
179              
180 119         529 return $self->{ '_' . $field };
181             };
182             }
183              
184             foreach my $field ( @COPYRIGHT_ACCESSORS ) {
185             $field = '_' . $field if ( $field eq 'copyright_comment' );
186             next if defined *{ __PACKAGE__ . '::' . $field }{CODE};
187              
188             *{ __PACKAGE__ . '::' . $field } = sub {
189 156     156   1400 my ( $self, $value ) = @_;
190              
191 156 100 66     513 if ( defined( $value ) and not ref( $value ) ) {
192 56         605 $value =~ s/^\s*|\s*$//gs;
193 56         217 $self->{ '_' . $field } = $value;
194             }
195              
196 156         256 my $ret = '';
197              
198 156 100       453 if ( $self->{ '_' . $field } ) {
199 14         69 $ret = '/* ' . $self->{ '_' . $field } . ' */' . "\n";
200             }
201              
202 156         520 return $ret;
203             };
204             }
205              
206             foreach my $reggrp ( @REGGRPS ) {
207             next if defined *{ __PACKAGE__ . '::reggrp_' . $reggrp }{CODE};
208              
209             *{ __PACKAGE__ . '::reggrp_' . $reggrp } = sub {
210 503     503   1081 my ( $self ) = shift;
211              
212 503         5516 return $self->{ '_reggrp_' . $reggrp };
213             };
214             }
215             }
216              
217             sub compress {
218 198     198 1 3644 my ( $self, $value ) = @_;
219              
220 198 100       536 if ( defined( $value ) ) {
221 34 100       220 if ( grep( $value eq $_, @COMPRESS_OPTS ) ) {
    50          
222 26         94 $self->{_compress} = $value;
223             }
224             elsif ( !$value ) {
225 0         0 $self->{_compress} = undef;
226             }
227             }
228              
229 198   66     587 $self->{_compress} ||= $DEFAULT_COMPRESS;
230              
231 198         825 return $self->{_compress};
232             }
233              
234             # these variables are used in the closures defined in the init function
235             # below - we have to use globals as using $self within the closures leads
236             # to a reference cycle and thus memory leak, and we can't scope them to
237             # the init method as they may change. they are set by the minify sub
238             our $reggrp_comments;
239             our $reggrp_clean;
240             our $reggrp_whitespace;
241              
242             sub init {
243 33     33 0 2597836 my $class = shift;
244 33         98 my $self = {};
245              
246 33         113 bless( $self, $class );
247              
248 33         123 @{ $self->{clean}->{reggrp_data} } = ( @$DATA, @$CLEAN );
  33         259  
249 33         134 @{ $self->{whitespace}->{reggrp_data} } = ( @$DATA[ 0, 1, 2, 4 ], @$WHITESPACE );
  33         171  
250 33         117 $self->{trim}->{reggrp_data} = $TRIM;
251              
252 33         142 @{ $self->{data_store}->{reggrp_data} } = map {
253 33         124 {
254             regexp => $_->{regexp},
255 792     792   10283 store => sub { return sprintf( "%s", $_[0]->{match} ); },
256             replacement => sub {
257 792     792   83896 return sprintf( $RESTORE_REPLACEMENT, $_[0]->{store_index} );
258             },
259             }
260 165         1120 } @$DATA;
261              
262             $self->{data_store}->{reggrp_data}->[-1]->{replacement} = sub {
263 9     9   3914 return sprintf( "%s$RESTORE_REPLACEMENT", $_[0]->{submatches}->[0], $_[0]->{store_index} );
264 33         287 };
265              
266             $self->{data_store}->{reggrp_data}->[-1]->{store} = sub {
267 9     9   171 return $_[0]->{submatches}->[1];
268 33         172 };
269              
270 33         121 @{ $self->{concat_store}->{reggrp_data} } = map {
271 33         132 my $data = $_;
  99         161  
272             {
273             regexp => $data->{regexp},
274             store => sub {
275 3250     3250   59682 my ( $quote, $string, $rest ) = $_[0]->{match} =~ /^(['"`])(.*)(['"`])$/;
276 3250 100       8579 return $_[0]->{match} if ! $rest;
277              
278 3249         7876 return $string;
279             },
280             replacement => sub {
281 3250     3250   320756 my ( $quote, $string, $rest ) = $_[0]->{match} =~ /^(['"`])(.*)(['"`])$/;
282 3250 100       8761 return $_[0]->{match} if ! $rest;
283              
284 3249         13578 return sprintf( "%s$RESTORE_REPLACEMENT%s", $quote, $_[0]->{store_index}, $quote );
285             },
286              
287 99         702 };
288             } @$DATA[ 0, 1, 2 ];
289              
290 33         254 @{ $self->{concat}->{reggrp_data} } = map {
291 33         79 my $quote = $_;
  99         194  
292 99         157 my $restore_pattern = $RESTORE_PATTERN;
293 99         561 $restore_pattern =~ s/\(\\d\+\)/\\d\+/g;
294              
295 99         265 my $regexp = '(' . $quote . $restore_pattern . $quote . ')((?:\+' . $quote . $restore_pattern . $quote . ')+)';
296 99         5327 $regexp = qr/$regexp/;
297              
298             {
299             regexp => $regexp,
300             replacement => sub {
301 17     17   14858 my $submatches = $_[0]->{submatches};
302 17         39 my $ret = $submatches->[0];
303              
304 17         43 my $next_str = '^\+(' . $quote . $restore_pattern . $quote . ')';
305              
306 17         259 while ( my ( $next ) = $submatches->[1] =~ /$next_str/ ) {
307 21         47 chop( $ret );
308 21         49 $ret .= substr( $next, 1 );
309 21         297 $submatches->[1] =~ s/$next_str//;
310             }
311              
312 17         55 return $ret;
313             },
314 99         874 };
315             } ( '"', '\'', '\`' );
316              
317 33         131 @{ $self->{comments}->{reggrp_data} } = ( @$DATA[ 0, 1, 2, 4 ], @$COMMENTS );
  33         129  
318              
319             # single line comment
320             $self->{comments}->{reggrp_data}->[-2]->{replacement} = sub {
321 1847     1847   679814 my $submatches = $_[0]->{submatches};
322              
323 1847 100       6680 if ( $submatches->[0] eq ':' ) {
    100          
    100          
324 1         5 return sprintf( "://%s", $submatches->[2] );
325             }
326             elsif ( $submatches->[1] eq '#' ) {
327 6         10 my $cmnt = sprintf( "%s//%s%s__NEW_LINE__", @{$submatches}[0 .. 2] );
  6         27  
328 6         21 return $cmnt;
329             }
330             elsif ( $submatches->[1] eq '@' ) {
331 4         16 $reggrp_comments->exec( \$submatches->[2] );
332 4         141 $reggrp_clean->exec( \$submatches->[2] );
333 4         2455 $reggrp_whitespace->exec( \$submatches->[2] );
334              
335 4         2920 return sprintf( "%s//%s%s\n%s", @{$submatches}[0 .. 3] );
  4         27  
336             }
337 1836         4787 return sprintf( "\n%s", $submatches->[3] );
338 33         289 };
339              
340             # multi line comments
341             $self->{comments}->{reggrp_data}->[-1]->{replacement} = sub {
342 178     178   151556 my $submatches = $_[0]->{submatches};
343 178 100       779 if ( $submatches->[0] =~ /^\/\*\@(.*)\@\*\/$/sm ) {
344 7         24 my $cmnt = $1;
345              
346 7         24 $reggrp_comments->exec( \$cmnt );
347 7         325 $reggrp_clean->exec( \$cmnt );
348 7         4038 $reggrp_whitespace->exec( \$cmnt );
349              
350 7         8662 return sprintf( '/*@%s@*/ %s', $cmnt, $submatches->[1] );
351             }
352 171         552 return sprintf( " %s", $submatches->[1] );
353 33         229 };
354              
355 33         159 foreach my $reggrp ( @REGGRPS ) {
356 231         182741 my $reggrp_args = { reggrp => $self->{$reggrp}->{reggrp_data} };
357              
358 231 100 100     1441 $reggrp_args->{restore_pattern} = $RESTORE_PATTERN if ( $reggrp eq 'data_store' or $reggrp eq 'concat_store' );
359              
360 231         847 $self->{ '_reggrp_' . $reggrp } = Regexp::RegGrp->new( $reggrp_args );
361             }
362              
363 33         12871 $self->{block_data} = [];
364              
365 33         398 return $self;
366             }
367              
368             sub minify {
369 52     52 0 34416 my ( $self, $input, $opts );
370              
371 52 100 66     359 unless (ref( $_[0] )
372             and ref( $_[0] ) eq __PACKAGE__ )
373             {
374 9         42 $self = __PACKAGE__->init();
375              
376 9 50       46 shift( @_ ) unless ( ref( $_[0] ) );
377              
378 9         33 ( $input, $opts ) = @_;
379             }
380             else {
381 43         148 ( $self, $input, $opts ) = @_;
382             }
383              
384 52 50       196 if ( ref( $input ) ne 'SCALAR' ) {
385 0         0 carp( 'First argument must be a scalarref!' );
386 0         0 return undef;
387             }
388              
389 52         101 my $javascript = \'';
390 52         106 my $cont = 'void';
391              
392 52 100       162 if ( defined( wantarray ) ) {
393 5 50       8 my $tmp_input = ref( $input ) ? ${$input} : $input;
  5         8  
394              
395 5         7 $javascript = \$tmp_input;
396 5         7 $cont = 'scalar';
397             }
398             else {
399 47 50       154 $javascript = ref( $input ) ? $input : \$input;
400             }
401              
402 52 100       175 if ( ref( $opts ) eq 'HASH' ) {
403 37         114 foreach my $field ( @BOOLEAN_ACCESSORS ) {
404 74 100       261 $self->$field( $opts->{$field} ) if ( defined( $opts->{$field} ) );
405             }
406              
407 37         100 foreach my $field ( 'compress', 'copyright' ) {
408 74 100       308 $self->$field( $opts->{$field} ) if ( defined( $opts->{$field} ) );
409             }
410             }
411              
412             # (re)initialize variables used in the closures
413 52         163 $reggrp_comments = $self->reggrp_comments;
414 52         173 $reggrp_clean = $self->reggrp_clean;
415 52         144 $reggrp_whitespace = $self->reggrp_whitespace;
416              
417 52         114 my $copyright_comment = '';
418              
419 52 100       113 if ( ${$javascript} =~ /$COPYRIGHT_COMMENT/ism ) {
  52         1697  
420 3         22 $copyright_comment = $1;
421             }
422              
423             # Resets copyright_comment() if there is no copyright comment
424 52         226 $self->_copyright_comment( $copyright_comment );
425              
426 52 100 100     154 if ( not $self->no_compress_comment() and ${$javascript} =~ /$PACKER_COMMENT/ ) {
  47         1469  
427 1         5 my $compress = $1;
428 1 50       5 if ( $compress eq '_no_compress_' ) {
429 1 50       5 return ${$javascript} if ( $cont eq 'scalar' );
  0         0  
430 1         6 return;
431             }
432              
433 0         0 $self->compress( $compress );
434             }
435              
436 51         104 ${$javascript} =~ s/\r//gsm;
  51         175  
437 51         107 ${$javascript} .= "\n";
  51         784  
438 51         168 $self->reggrp_comments()->exec( $javascript );
439 51         26713 $self->reggrp_clean()->exec( $javascript );
440 51         32857 $self->reggrp_whitespace()->exec( $javascript );
441 51         633926 $self->reggrp_concat_store()->exec( $javascript );
442 51         8580 $self->reggrp_concat()->exec( $javascript );
443 51         39260 $self->reggrp_concat_store()->restore_stored( $javascript );
444              
445 51 100       27509 if ( $self->compress() ne 'clean' ) {
446 17         80 $self->reggrp_data_store()->exec( $javascript );
447              
448 17         3984 while ( ${$javascript} =~ /$SHRINK_VARS->{BLOCK}/ ) {
  44         3624  
449 27         64 ${$javascript} =~ s/$SHRINK_VARS->{BLOCK}/$self->_store_block_data( $1 )/egsm;
  27         3851  
  511         1191  
450             }
451              
452 17         94 $self->_restore_data( $javascript, 'block_data', $SHRINK_VARS->{ENCODED_BLOCK} );
453              
454 17         39 my %shrunk_vars = map { $_ => 1 } ( ${$javascript} =~ /$SHRINK_VARS->{SHRUNK}/g );
  1657         3861  
  17         1649  
455              
456 17         340 my $cnt = 0;
457 17         105 foreach my $shrunk_var ( sort keys( %shrunk_vars ) ) {
458 58         129 my $short_id;
459             do {
460 70         331 $short_id = $self->_encode52( $cnt++ );
461 58         101 } while ( ${$javascript} =~ /[^a-zA-Z0-9_\\x24\.]\Q$short_id\E[^a-zA-Z0-9_\\x24:]/ );
  70         6229  
462              
463 58         170 ${$javascript} =~ s/$shrunk_var/$short_id/g;
  58         2590  
464             }
465              
466 17         84 $self->reggrp_data_store()->restore_stored( $javascript );
467              
468 17         7321 $self->{block_data} = [];
469             }
470              
471 51 100 100     172 if ( $self->compress() eq 'obfuscate' or $self->compress() eq 'best' ) {
472 7         16 my $words = {};
473              
474 7         16 my @words = ${$javascript} =~ /$BASE62_VARS->{WORDS}/g;
  7         16082  
475              
476 7         84 my $idx = 0;
477              
478 7         25 foreach ( @words ) {
479 10898         25913 $words->{$_}->{count}++;
480             }
481              
482 7         17 WORD: foreach my $word ( sort { $words->{$b}->{count} <=> $words->{$a}->{count} } sort keys( %{$words} ) ) {
  10419         16078  
  7         1654  
483              
484 3122 100 66     6674 if ( exists( $words->{$word}->{encoded} ) and $words->{$word}->{encoded} eq $word ) {
485 151         317 next WORD;
486             }
487              
488 2971         4426 my $encoded = $self->_encode62( $idx );
489              
490 2971 100       5469 if ( exists( $words->{$encoded} ) ) {
491 209         286 my $next = 0;
492 209 100       427 if ( exists( $words->{$encoded}->{encoded} ) ) {
493 58         135 $words->{$word}->{encoded} = $words->{$encoded}->{encoded};
494 58         102 $words->{$word}->{index} = $words->{$encoded}->{index};
495 58         107 $words->{$word}->{minus} = length( $word ) - length( $words->{$word}->{encoded} );
496 58         83 $next = 1;
497             }
498 209         360 $words->{$encoded}->{encoded} = $encoded;
499 209         354 $words->{$encoded}->{index} = $idx;
500 209         323 $words->{$encoded}->{minus} = 0;
501 209         289 $idx++;
502 209 100       428 next WORD if ( $next );
503 151         284 redo WORD;
504             }
505              
506 2762         4177 $words->{$word}->{encoded} = $encoded;
507 2762         3619 $words->{$word}->{index} = $idx;
508 2762         3819 $words->{$word}->{minus} = length( $word ) - length( $encoded );
509              
510 2762         4343 $idx++;
511             }
512              
513 7         213 my $packed_length = length( ${$javascript} );
  7         39  
514              
515 7         28 my ( @pk, @pattern ) = ( (), () );
516              
517 7         16 foreach ( sort { $words->{$a}->{index} <=> $words->{$b}->{index} } sort keys( %{$words} ) ) {
  12076         21578  
  7         1623  
518 2971         4788 $packed_length -= ( $words->{$_}->{count} * $words->{$_}->{minus} );
519              
520 2971 100       4556 if ( $words->{$_}->{encoded} ne $_ ) {
521 2762         3278 push( @pk, $_ );
522 2762         4724 push( @pattern, $words->{$_}->{encoded} );
523             }
524             else {
525 209         274 push( @pk, '' );
526 209         361 push( @pattern, '' );
527             }
528             }
529              
530 7         142 my $size = scalar( @pattern );
531              
532 7 100       131 splice( @pattern, 62 ) if ( scalar( @pattern ) > 62 );
533              
534 7         44 my $pd = join( '|', @pattern );
535              
536 7         31 $self->reggrp_trim()->exec( \$pd );
537              
538 7 50       1491 unless ( $pd ) {
539 0         0 $pd = '^$';
540             }
541             else {
542 7         18 $pd = '[' . $pd . ']';
543              
544 7 100       24 if ( $size > 62 ) {
545 3         6 $pd = '(' . $pd . '|';
546              
547 3         11 my $enc = $self->_encode62( $size );
548              
549 3         13 my ( $c ) = $enc =~ /(^.)/;
550 3         6 my $ord = ord( $c );
551              
552 3         8 my $mul = length( $enc ) - 1;
553              
554 3         6 my $is62 = 0;
555              
556 3 100       16 if ( $ord >= 65 ) {
    100          
    50          
    50          
557 1 50       8 if ( $c eq 'Z' ) {
558 0         0 $mul += 1;
559 0         0 $is62 = 1;
560             }
561             else {
562 1         3 $pd .= '[0-9a';
563 1 50       6 if ( $ord > 97 ) {
    50          
    50          
564 0         0 $pd .= '-' . $c;
565             }
566             elsif ( $ord > 65 ) {
567 0         0 $pd .= '-zA-' . $c;
568             }
569             elsif ( $ord == 65 ) {
570 1         3 $pd .= '-zA';
571             }
572 1         2 $pd .= ']';
573             }
574             }
575             elsif ( $ord == 57 ) {
576 1         2 $pd .= '[0-9]';
577             }
578             elsif ( $ord == 50 ) {
579 0         0 $pd .= '[12]';
580             }
581             elsif ( $ord == 49 ) {
582 1         2 $pd .= '1';
583             }
584             else {
585 0         0 $pd .= '[0-' . ( $ord - 48 ) . ']';
586             }
587              
588 3 50       13 $pd .= '[0-9a-zA-Z]' . ( ( $mul > 1 ) ? '{' . $mul . '}' : '' );
589              
590 3 50       8 $mul-- if ( $is62 );
591              
592 3 50       7 if ( $mul > 1 ) {
593 0         0 for ( my $i = $mul; $i >= 2; $i-- ) {
594 0         0 $pd .= '|[0-9a-zA-Z]{' . $i . '}';
595             }
596             }
597              
598 3         7 $pd .= ')';
599             }
600             }
601 7         22 $packed_length += length( $pd );
602              
603 7         227 my $pk = join( '|', @pk );
604 7         541 $pk =~ s/(?>\|+)$//;
605 7         82 $packed_length += length( $pk );
606              
607 7 50       52 my $pc = length( $pk ) ? ( ( $pk =~ tr/|/|/ ) + 1 ) : 0;
608 7         21 $packed_length += length( $pc );
609              
610 7         18 my $pa = '[]';
611 7         17 $packed_length += length( $pa );
612              
613 7 50       42 my $pe = $BASE62_VARS->{ 'ENCODE' . ( $pc > 10 ? $pc > 36 ? 62 : 36 : 10 ) };
    100          
614 7         13 $packed_length += length( $pe );
615              
616 7         23 $packed_length += length( $BASE62_VARS->{UNPACK} );
617 7         126 $packed_length -= ( $BASE62_VARS->{UNPACK} =~ s/(%s|%d)/$1/g ) * 2;
618              
619 7         18 my ( @length_matches ) = ${$javascript} =~ s/((?>[\r\n]+))/$1/g;
  7         150  
620 7         26 foreach ( @length_matches ) {
621 7         29 $packed_length -= length( $_ ) - 3;
622             }
623              
624 7         12 $packed_length += ${$javascript} =~ tr/\\\'/\\\'/;
  7         81  
625              
626 7 100 100     31 if ( $self->compress() eq 'obfuscate' or $packed_length <= length( ${$javascript} ) ) {
  5         64  
627              
628 5         9 ${$javascript} =~ s/$BASE62_VARS->{WORDS}/sprintf( "%s", $words->{$1}->{encoded} )/eg;
  5         49  
  10865         22612  
629              
630 5         13 ${$javascript} =~ s/([\\'])/\\$1/g;
  5         388  
631 5         12 ${$javascript} =~ s/[\r\n]+/\\n/g;
  5         71  
632              
633 5         9 my $pp = ${$javascript};
  5         42  
634              
635 5         110 ${$javascript} = sprintf( $BASE62_VARS->{UNPACK}, $pe, $pd, $pp, $pa, $pc, $pk );
  5         2285  
636             }
637              
638             }
639              
640 51 100       230 if ( not $self->remove_copyright() ) {
641 50   100     154 ${$javascript} = ( $self->copyright() || $self->_copyright_comment() ) . ${$javascript};
  50         1373  
  50         110  
642             }
643              
644             # GH #9 bodge for sourceMappingURL
645 51         148 ${$javascript} =~ s/__NEW_LINE__/\n/xsmg;
  51         342  
646 51         93 ${$javascript} =~ s!//#sourceMappingURL!//# sourceMappingURL!g;
  51         628  
647 51         97 chomp( ${$javascript} );
  51         199  
648              
649 51 100       1245 return ${$javascript} if ( $cont eq 'scalar' );
  5         28  
650             }
651              
652             sub _restore_data {
653 379     379   773 my ( $self, $string_ref, $data_name, $pattern ) = @_;
654              
655 379         524 while ( ${$string_ref} =~ /$pattern/ ) {
  533         2453  
656 154         221 ${$string_ref} =~ s/$pattern/$self->{$data_name}->[$1]/egsm;
  154         709  
  511         2293  
657             }
658             }
659              
660             sub _store_block_data {
661 511     511   1379 my ( $self, $match ) = @_;
662              
663 511         3648 my ( undef, $prefix, $blocktype, $args, $block ) = $match =~ /$SHRINK_VARS->{BLOCK}/;
664              
665 511   100     1399 $prefix ||= '';
666 511   100     1028 $blocktype ||= '';
667 511   100     1042 $args ||= '';
668 511         739 my $replacement = '';
669 511 100       909 if ( $blocktype eq 'function' ) {
670              
671 181         551 $self->_restore_data( \$block, 'block_data', $SHRINK_VARS->{SCOPED} );
672              
673 181         1974 $args =~ s/\s*//g;
674              
675 181         441 $block = $args . $block;
676 181         1163 $prefix =~ s/$SHRINK_VARS->{BRACKETS}//;
677              
678 181         908 $args =~ s/^\(|\)$//g;
679              
680 181         720 while ( $args =~ /$SHRINK_VARS->{CALLER}/ ) {
681 0         0 $args =~ s/$SHRINK_VARS->{CALLER}//gsm;
682             }
683              
684 181         812 my @vars = grep( $_, split( /\s*,\s*/, $args ) );
685 181 100       454 my $do_shrink = grep( $_ eq '_no_shrink_', @vars ) ? 0 : 1;
686              
687 181         280 my %block_vars = ();
688 181 100       375 if ( $do_shrink ) {
689 180         2611 %block_vars = map { $_ => 1 } ( $block =~ /$SHRINK_VARS->{VARS}/g ), grep( $_ ne '$super', @vars );
  475         1315  
690             }
691              
692 181         649 $self->_restore_data( \$block, 'block_data', $SHRINK_VARS->{ENCODED_BLOCK} );
693              
694 181 100       370 if ( $do_shrink ) {
695              
696 180         260 my $cnt = 0;
697 180         668 foreach my $block_var ( sort keys( %block_vars ) ) {
698 468 50       1069 if ( length( $block_var ) ) {
699 468         5482 while ( $block =~ /$SHRINK_VARS->{PREFIX}\Q$cnt\E\b/ ) {
700 16         197 $cnt++;
701             }
702              
703 468         16961 while ( $block =~ /[^a-zA-Z0-9_\\x24\.]\Q$block_var\E[^a-zA-Z0-9_\\x24:]/ ) {
704 491         17848 $block =~ s/([^a-zA-Z0-9_\\x24\.])\Q$block_var\E([^a-zA-Z0-9_\\x24:])/sprintf( "%s\x02%d%s", $1, $cnt, $2 )/eg;
  1656         8487  
705             }
706              
707 468         10194 $block =~ s/([^{,a-zA-Z0-9_\\x24\.])\Q$block_var\E:/sprintf( "%s\x02%d:", $1, $cnt )/eg;
  1         7  
708              
709 468         1287 $cnt++;
710             }
711             }
712             }
713 181         371 $replacement = sprintf( "%s~%d~", $prefix, scalar( @{ $self->{block_data} } ) );
  181         633  
714              
715 181         316 push( @{ $self->{block_data} }, $block );
  181         1038  
716             }
717             else {
718 330         391 $replacement = sprintf( "~#%d~", scalar( @{ $self->{block_data} } ) );
  330         756  
719              
720 330         457 push( @{ $self->{block_data} }, $prefix . $block );
  330         808  
721             }
722              
723 511         21302 return $replacement;
724             }
725              
726             sub _encode52 {
727 70     70   177 my ( $self, $c ) = @_;
728              
729 70         138 my $m = $c % 52;
730              
731 70 50       245 my $ret = $m > 25 ? chr( $m + 39 ) : chr( $m + 97 );
732              
733 70 50       183 if ( $c >= 52 ) {
734 0         0 $ret = $self->_encode52( int( $c / 52 ) ) . $ret;
735             }
736              
737 70 50       189 $ret = substr( $ret, 1 ) . '0' if ( $ret =~ /^(do|if|in)$/ );
738              
739 70         231 return $ret;
740             }
741              
742             sub _encode62 {
743 5725     5725   7830 my ( $self, $c ) = @_;
744              
745 5725         6665 my $m = $c % 62;
746              
747 5725 100       10049 my $ret = $m > 35 ? chr( $m + 29 ) : $m > 9 ? chr( $m + 87 ) : $m;
    100          
748              
749 5725 100       8473 if ( $c >= 62 ) {
750 2751         4744 $ret = $self->_encode62( int( $c / 62 ) ) . $ret;
751             }
752              
753 5725         10351 return $ret;
754             }
755              
756             1;
757              
758             __END__