blib/lib/JavaScript/Packer.pm

filter: Statementstmt 89.3% 334 / 374 Branchbran 72.5% 103 / 142 Conditioncond 79.4% 27 / 34 MC/DCmcdc 61.9% 13 / 21 Subroutinesub 75.7% 25 / 33 Podpod 33.3% 1 / 3 totaltotal 82.8% 503 / 607 CC 90 SCAR 37.4
CC
90
Cov
84%
minify
86.0
_store_block_data
13.0
compress
5.2
CRAP 42.0
↑ Summary
1 package JavaScript::Packer;
2  
3 3 ▶use 5.008009;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Regexp::RegGrp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # =========================================================================== #
10  
11 3 our $VERSION = '1.005_001';
12  
13 3 our @BOOLEAN_ACCESSORS = (
14 'no_compress_comment',
15 'remove_copyright'
16 );
17  
18 3 our @COPYRIGHT_ACCESSORS = (
19 'copyright',
20 'copyright_comment'
21 );
22  
23 3 our @COMPRESS_OPTS = ( 'clean', 'obfuscate', 'shrink', 'best' );
24 3 our $DEFAULT_COMPRESS = 'clean';
25  
26 3 our $PACKER_COMMENT = '\/\*\s*JavaScript::Packer\s*(\w+)\s*\*\/';
27 3 our $COPYRIGHT_COMMENT = '\/\*((?>[^\*]|\*[^\/])*copyright(?>[^\*]|\*[^\/])*)\*\/';
28  
29 3 our $SHRINK_VARS = {
30 BLOCK => qr/(((catch|do|if|while|with|function)\b[^~{};]*(\(\s*[^{};]*\s*\))\s*)?(\{[^{}]*\}))/, # function ( arg ) { ... }
31 ENCODED_BLOCK => qr/~#?(\d+)~/,
32 CALLER => qr/((?>[a-zA-Z0-9_\x24\.]+)\s*\([^\(\)]*\))(?=[,\)])/, # do_something( arg1, arg2 ) as argument of another function call
33 BRACKETS => qr/\{[^{}]*\}|\[[^\[\]]*\]|\([^\(\)]*\)|~[^~]+~/,
34 IDENTIFIER => qr~[a-zA-Z_\x24][a-zA-Z_0-9\\x24]*~,
35 SCOPED => qr/~#(\d+)~/,
36 VARS => qr~\b(?:var|function)\s+((?>[a-zA-Z0-9_\x24]+))~, # var x, funktion blah
37 PREFIX => qr~\x02~,
38 SHRUNK => qr~\x02\d+\b~
39 };
40  
41 3 our $BASE62_VARS = {
42 WORDS => qr~(\b[0-9a-zA-Z]\b|(?>[a-zA-Z0-9_]{2,}))~,
43 ENCODE10 => 'String',
44 ENCODE36 => 'function(c){return c.toString(36)}',
45 ENCODE62 => q~function(c){return(c<62?'':e(parseInt(c/62)))+((c=c%62)>35?String.fromCharCode(c+29):c.toString(36))}~,
46 UNPACK => 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,{}))~
47 };
48  
49 3 our $DICTIONARY = {
50 STRING1 => qr~"(?>(?:(?>[^"\\]+)|\\.|\\")*)"~,
51 STRING2 => qr~'(?>(?:(?>[^'\\]+)|\\.|\\')*)'~,
52 REGEXP => qr~\/(\\[\/\\]|[^*\/])(\\.|[^\/\n\\])*\/[gim]*~,
53 OPERATOR => qr'return|typeof|[\[(\^=,{}:;&|!*?]',
54 CONDITIONAL => qr~\/\*\@\w*|\w*\@\*\/|\/\/\@\w*|\@(?>\w+)~,
55 COMMENT1 => qr~\/\/(\@)?([^\n]*)?\n~,
56 COMMENT2 => qr~\/\*[^*]*\*+(?:[^\/][^*]*\*+)*\/~
57 };
58  
59 our $DATA = [
60 {
61 regexp => $DICTIONARY->{STRING1}
62 },
63 {
64 regexp => $DICTIONARY->{STRING2}
65 },
66 {
67 regexp => $DICTIONARY->{CONDITIONAL}
68 },
69 {
70 regexp => '(' . $DICTIONARY->{OPERATOR} . ')\s*(' . $DICTIONARY->{REGEXP} . ')',
71 replacement => sub {
72 0 ▶ return sprintf( "%s%s", $_[0]->{submatches}->[0], $_[0]->{submatches}->[1] );
Subroutine __ANON__: not called CC=1 SCAR=6.9
73 }
74 }
75 3 ];
76  
77 our $COMMENTS = [
78 {
79 regexp => ';;;[^\n]*\n',
80 replacement => ''
81 },
82 {
83 regexp => $DICTIONARY->{COMMENT1} . '\s*(' . $DICTIONARY->{REGEXP} . ')?'
84 },
85 {
86 3 regexp => '(' . $DICTIONARY->{COMMENT2} . ')\s*(' . $DICTIONARY->{REGEXP} . ')?'
87 }
88 ];
89  
90 our $CLEAN = [
91 {
92 regexp => '\(\s*([^;)]*)\s*;\s*([^;)]*)\s*;\s*([^;)]*)\)',
93 0 ▶ replacement => sub { return sprintf( "(%s;%s;%s)", @{$_[0]->{submatches}} ); }
Subroutine __ANON__: not called CC=1 SCAR=6.9
94 },
95 {
96 regexp => 'throw[^};]+[};]'
97 },
98 {
99 regexp => ';+\s*([};])',
100 29 ▶ replacement => sub { return $_[0]->{submatches}->[0]; }
Subroutine __ANON__: called CC=1 SCAR=0.0
101 }
102 3 ];
103  
104 our $WHITESPACE = [
105 {
106 regexp => '\/\/@[^\n]*\n'
107 },
108 {
109 regexp => '@\s+\b',
110 replacement => '@ '
111 },
112 {
113 regexp => '\b\s+@',
114 replacement => ' @'
115 },
116 {
117 regexp => '(\d)\s+(\.\s*[a-z\x24_\[(])',
118 0 ▶ replacement => sub { return sprintf( "%s %s", @{$_[0]->{submatches}} ); }
Subroutine __ANON__: not called CC=1 SCAR=6.9
119 },
120 {
121 regexp => '([+-])\s+([+-])',
122 0 ▶ replacement => sub { return sprintf( "%s %s", @{$_[0]->{submatches}} ); }
Subroutine __ANON__: not called CC=1 SCAR=6.9
123 },
124 {
125 regexp => '(?>\s+)(\x24)(?>\s+)',
126 0 ▶ replacement => sub { return sprintf( " %s ", $_[0]->{submatches}->[0] ); }
Subroutine __ANON__: not called CC=1 SCAR=6.9
127 },
128 {
129 regexp => '(\x24)(?>\s+)(?!=)',
130 0 ▶ replacement => sub { return sprintf( "%s ", $_[0]->{submatches}->[0] ); }
Subroutine __ANON__: not called CC=1 SCAR=6.9
131 },
132 {
133 regexp => '(?<!=)(?>\s+)(\x24)',
134 3 ▶ replacement => sub { return sprintf( " %s", $_[0]->{submatches}->[0] ); }
Subroutine __ANON__: called CC=1 SCAR=0.0
135 },
136 {
137 3 regexp => '\b\s+\b',
138 replacement => ' '
139 },
140 {
141 regexp => '\s+',
142 replacement => ''
143 }
144 ];
145  
146 our $TRIM = [
147 {
148 regexp => '(\d)(?:\|\d)+\|(\d)',
149 3 ▶ replacement => sub { return sprintf( "%d-%d", $_[0]->{submatches}->[0] || 0, $_[0]->{submatches}->[1] || 0 ); }
Subroutine __ANON__: called CC=3 SCAR=12.7
Condition50%2/4
$_[0]{'submatches'}[0] || 0
l!l
30
$_[0]{'submatches'}[1] || 0
l!l
30
MC/DC0%0/1
$_[0]{'submatches'}[0] || 0
$_[0]{'submatches'}[0]
MC/DC0%0/1
$_[0]{'submatches'}[1] || 0
$_[0]{'submatches'}[1]
Truth table50%1/2
A
A= $_[0]{'submatches'}[0]
Aresult
11
00
Truth table50%1/2
A
A= $_[0]{'submatches'}[1]
Aresult
11
00
150 },
151 {
152 regexp => '([a-z])(?:\|[a-z])+\|([a-z])',
153 1 ▶ replacement => sub { return sprintf( "%s-%s", $_[0]->{submatches}->[0], $_[0]->{submatches}->[1] ); }
Subroutine __ANON__: called CC=1 SCAR=0.0
154 },
155 {
156 regexp => '([A-Z])(?:\|[A-Z])+\|([A-Z])',
157 1 ▶ replacement => sub { return sprintf( "%s-%s", $_[0]->{submatches}->[0], $_[0]->{submatches}->[1] ); }
Subroutine __ANON__: called CC=1 SCAR=0.0
158 },
159 {
160 3 regexp => '\|',
161 replacement => ''
162 }
163 ];
164  
165 3 our @REGGRPS = ( 'comments', 'clean', 'whitespace', 'concat', 'trim', 'data_store' );
166  
167 # --------------------------------------------------------------------------- #
168  
169 {
170 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
171  
172 3 foreach my $field ( @BOOLEAN_ACCESSORS ) {
173 6 ▶ next if defined *{ __PACKAGE__ . '::' . $field }{CODE};
Branch: 1 branch
BranchTrueFalseTotal
if defined *{'JavaScript::Packer::' . $field;}{"CODE"} 0 6 6
174  
175 6 *{ __PACKAGE__ . '::' . $field} = sub {
176 59 ▶ my ( $self, $value ) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.0
177  
178 59 ▶ $self->{'_' . $field} = $value ? 1 : undef if ( defined( $value ) );
Branch: 2 branches
BranchTrueFalseTotal
if defined $value 8 51 59
$value ? : 5 3 8
179  
180 59 return $self->{'_' . $field};
181 6 };
182 }
183  
184 3 foreach my $field ( @COPYRIGHT_ACCESSORS ) {
185 6 ▶ $field = '_' . $field if ( $field eq 'copyright_comment' );
Branch: 1 branch
BranchTrueFalseTotal
if $field eq "copyright_comment" 3 3 6
186 6 ▶ next if defined *{ __PACKAGE__ . '::' . $field }{CODE};
Branch: 1 branch
BranchTrueFalseTotal
if defined *{'JavaScript::Packer::' . $field;}{"CODE"} 0 6 6
187  
188 6 *{ __PACKAGE__ . '::' . $field} = sub {
189 66 ▶ my ( $self, $value ) = @_;
Subroutine __ANON__: called CC=4 SCAR=13.9
190  
191 66 ▶ if ( defined( $value ) and not ref( $value ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and not ref $value) 26 40 66
Condition66%2/3
defined $value and not ref $value
!ll&&!rl&&r
40026
MC/DC50%1/2
defined $value and not ref $value
defined $value not ref $value
Truth table66%2/3
A and not(B)
A= defined $valueB= not ref $value
ABresult
0X0
100
111
192 26 $value =~ s/^\s*|\s*$//gs;
193 26 $self->{'_' . $field} = $value;
194 }
195  
196 66 my $ret = '';
197  
198 66 ▶ if ( $self->{'_' . $field} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$self{'_' . $field}) 12 54 66
199 12 $ret = '/* ' . $self->{'_' . $field} . ' */' . "\n";
200 }
201  
202 66 return $ret;
203 6 };
204 }
205  
206 3 foreach my $reggrp ( @REGGRPS ) {
207 18 ▶ next if defined *{ __PACKAGE__ . '::reggrp_' . $reggrp }{CODE};
Branch: 1 branch
BranchTrueFalseTotal
if defined *{'JavaScript::Packer::reggrp_' . $reggrp;}{"CODE"} 0 18 18
208  
209 18 *{ __PACKAGE__ . '::reggrp_' . $reggrp } = sub {
210 144 ▶ my ( $self ) = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
211  
212 144 return $self->{ '_reggrp_' . $reggrp };
213 18 };
214 }
215 }
216  
217 sub compress {
218 88 ▶ my ( $self, $value ) = @_;
Subroutine compress: called CC=5 SCAR=16.4
Pod documented
219  
220 88 ▶ if ( defined( $value ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 16 72 88
221 16 ▶ if ( grep( $value eq $_, @COMPRESS_OPTS ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (grep(($value eq $_), @JavaScript::Packer::COMPRESS_OPTS)) { } 15 1 16
elsif (not $value) { } 0 1 1
222 15 $self->{_compress} = $value;
223 }
224 elsif ( ! $value ) {
225 0 $self->{_compress} = undef;
226 }
227 }
228  
229 88 ▶ $self->{_compress} ||= $DEFAULT_COMPRESS;
Condition66%2/3
$self->{"_compress"} ||= $JavaScript::Packer::DEFAULT_COMPRESS
l!l&&r!l&&!r
8440
MC/DC0%0/2
$self->{"_compress"} ||= $JavaScript::Packer::DEFAULT_COMPRESS
$self->{"_compress"} $JavaScript::Packer::DEFAULT_COMPRESS
Truth table66%2/3
A ||= B
A= $self->{"_compress"}B= $JavaScript::Packer::DEFAULT_COMPRESS
ABresult
1X1
011
000
230  
231 88 return $self->{_compress};
232 }
233  
234 sub init {
235 13 ▶ my $class = shift;
Subroutine init: called CC=2 SCAR=6.9
Pod undocumented
236 13 my $self = {};
237  
238 13 bless( $self, $class );
239  
240 13 @{$self->{clean}->{reggrp_data}} = ( @$DATA, @$CLEAN );
241 13 @{$self->{whitespace}->{reggrp_data}} = ( @$DATA[ 0, 1, 3 ], @$WHITESPACE );
242 13 $self->{trim}->{reggrp_data} = $TRIM;
243  
244 13 @{$self->{data_store}->{reggrp_data}} = map {
245 13 {
246 regexp => $_->{regexp},
247 55 ▶ store => sub { return sprintf( "%s", $_[0]->{match} ); }
Subroutine __ANON__: called CC=1 SCAR=0.0
248 }
249 52 } @$DATA;
250  
251 $self->{data_store}->{reggrp_data}->[-1]->{replacement} = sub {
252 0 ▶ return sprintf( "%s\x01%d\x01", $_[0]->{submatches}->[0], $_[0]->{store_index} );
Subroutine __ANON__: not called CC=1 SCAR=6.9
253 13 };
254  
255 $self->{data_store}->{reggrp_data}->[-1]->{store} = sub {
256 0 ▶ return $_[0]->{submatches}->[1];
Subroutine __ANON__: not called CC=1 SCAR=6.9
257 13 };
258  
259 13 @{$self->{concat}->{reggrp_data}} = map {
260 13 my $data = $_;
261 {
262 regexp => '(' . $data->{regexp} . ')((?:\+' . $data->{regexp} . ')+)',
263 replacement => sub {
264 3 ▶ my $submatches = $_[0]->{submatches};
Subroutine __ANON__: called CC=2 SCAR=6.9
265 3 my $ret = $submatches->[0];
266  
267 3 my $next_str = '^\+(' . $data->{regexp} . ')';
268  
269 3 ▶ while ( my ( $next ) = $submatches->[1] =~ /$next_str/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($next) = $submatches->[1] =~ /$next_str/) 6 3 9
270 6 chop( $ret );
271 6 $ret .= substr( $next, 1 );
272 6 $submatches->[1] =~ s/$next_str//;
273 }
274  
275 3 return $ret;
276 }
277 26 };
278 } @$DATA[ 0, 1 ];
279  
280 13 @{$self->{comments}->{reggrp_data}} = ( @$DATA[ 0, 1, 3 ], @$COMMENTS );
281  
282 $self->{comments}->{reggrp_data}->[-2]->{replacement} = sub {
283 15 ▶ my $submatches = $_[0]->{submatches};
Subroutine __ANON__: called CC=2 SCAR=6.9
284 15 ▶ if ( $submatches->[0] eq '@' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($submatches->[0] eq "\@") 4 11 15
285 4 $self->reggrp_comments()->exec( \$submatches->[1] );
286 4 $self->reggrp_clean()->exec( \$submatches->[1] );
287 4 $self->reggrp_whitespace()->exec( \$submatches->[1] );
288  
289 4 return sprintf( "//%s%s\n%s", @{$submatches} );
290 }
291 11 return sprintf( "\n%s", $submatches->[2] );
292 13 };
293  
294 $self->{comments}->{reggrp_data}->[-1]->{replacement} = sub {
295 21 ▶ my $submatches = $_[0]->{submatches};
Subroutine __ANON__: called CC=2 SCAR=6.9
296 21 ▶ if ( $submatches->[0] =~ /^\/\*\@(.*)\@\*\/$/sm ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($submatches->[0] =~ m[^/\*\@(.*)\@\*/$]ms) 7 14 21
297 7 my $cmnt = $1;
298  
299 7 $self->reggrp_comments()->exec( \$cmnt );
300 7 $self->reggrp_clean()->exec( \$cmnt );
301 7 $self->reggrp_whitespace()->exec( \$cmnt );
302  
303 7 return sprintf( '/*@%s@*/ %s', $cmnt, $submatches->[1] );
304 }
305 14 return sprintf( " %s", $submatches->[1] );
306 13 };
307  
308 13 foreach ( @REGGRPS ) {
309 78 $self->{ '_reggrp_' . $_ } = Regexp::RegGrp->new( { reggrp => $self->{$_}->{reggrp_data} } );
310 }
311  
312 13 $self->{block_data} = [];
313  
314 13 return $self;
315 }
316  
317 sub minify {
318 22 ▶ my ( $self, $input, $opts );
Subroutine minify: called CC=56 SCAR=44.5
Pod undocumented
319  
320 22 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] and ref $_[0] eq __PACKAGE__) { } 22 0 22
Condition33%1/3
ref $_[0] and ref $_[0] eq __PACKAGE__
!ll&&!rl&&r
0022
MC/DC0%0/2
ref $_[0] and ref $_[0] eq __PACKAGE__
ref $_[0] ref $_[0] eq __PACKAGE__
Truth table33%1/3
A and B
A= ref $_[0]B= ref $_[0] eq __PACKAGE__
ABresult
0X0
100
111
321 ref( $_[0] ) and
322 ref( $_[0] ) eq __PACKAGE__
323 ) {
324 0 $self = __PACKAGE__->init();
325  
326 0 ▶ shift( @_ ) unless ( ref( $_[0] ) );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] 0 0 0
327  
328 0 ( $input, $opts ) = @_;
329 }
330 else {
331 22 ( $self, $input, $opts ) = @_;
332 }
333  
334 22 ▶ if ( ref( $input ) ne 'SCALAR' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $input ne "SCALAR") 0 22 22
335 0 carp( 'First argument must be a scalarref!' );
336 0 return undef;
337 }
338  
339 22 my $javascript = \'';
340 22 my $cont = 'void';
341  
342 22 ▶ if ( defined( wantarray ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) { } 0 22 22
343 0 ▶ my $tmp_input = ref( $input ) ? ${$input} : $input;
Branch: 1 branch
BranchTrueFalseTotal
ref $input ? : 0 0 0
344  
345 0 $javascript = \$tmp_input;
346 0 $cont = 'scalar';
347 }
348 else {
349 22 ▶ $javascript = ref( $input ) ? $input : \$input;
Branch: 1 branch
BranchTrueFalseTotal
ref $input ? : 22 0 22
350 }
351  
352 22 ▶ if ( ref( $opts ) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts eq "HASH") 14 8 22
353 14 foreach my $field ( @BOOLEAN_ACCESSORS ) {
354 28 ▶ $self->$field( $opts->{$field} ) if ( defined( $opts->{$field} ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{$field} 4 24 28
355 }
356  
357 14 foreach my $field ( 'compress', 'copyright' ) {
358 28 ▶ $self->$field( $opts->{$field} ) if ( defined( $opts->{$field} ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{$field} 10 18 28
359 }
360 }
361  
362 22 my $copyright_comment = '';
363  
364 22 ▶ if ( ${$javascript} =~ /$COPYRIGHT_COMMENT/ism ) {
Branch: 1 branch
BranchTrueFalseTotal
if (${$javascript;} =~ /$COPYRIGHT_COMMENT/ism) 2 20 22
365 2 $copyright_comment = $1;
366 }
367 # Resets copyright_comment() if there is no copyright comment
368 22 $self->_copyright_comment( $copyright_comment );
369  
370 22 ▶ if ( not $self->no_compress_comment() and ${$javascript} =~ /$PACKER_COMMENT/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->no_compress_comment and ${$javascript;} =~ /$PACKER_COMMENT/) 1 21 22
Condition100%3/3
not $self->no_compress_comment and ${$javascript;} =~ /$PACKER_COMMENT/
!ll&&!rl&&r
3181
MC/DC100%2/2
not $self->no_compress_comment and ${$javascript;} =~ /$PACKER_COMMENT/
not $self->no_compress_comment ${$javascript;} =~ /$PACKER_COMMENT/
Truth table100%3/3
not(A) and B
A= not $self->no_compress_commentB= ${$javascript;} =~ /$PACKER_COMMENT/
ABresult
0X0
100
111
371 1 my $compress = $1;
372 1 ▶ if ( $compress eq '_no_compress_' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($compress eq "_no_compress_") 1 0 1
373 1 ▶ return ${$javascript} if ( $cont eq 'scalar' );
Branch: 1 branch
BranchTrueFalseTotal
if $cont eq "scalar" 0 1 1
374 1 return;
375 }
376  
377 0 $self->compress( $compress );
378 }
379  
380 21 ${$javascript} =~ s/\r//gsm;
381 21 ${$javascript} .= "\n";
382  
383 21 $self->reggrp_comments()->exec( $javascript );
384 21 $self->reggrp_clean()->exec( $javascript );
385 21 $self->reggrp_whitespace()->exec( $javascript );
386 21 $self->reggrp_concat()->exec( $javascript );
387  
388 21 ▶ if ( $self->compress() ne 'clean' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->compress ne "clean") 11 10 21
389 11 $self->reggrp_data_store()->exec( $javascript );
390  
391 11 ▶ while( ${$javascript} =~ /$SHRINK_VARS->{BLOCK}/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (${$javascript;} =~ /$JavaScript::Packer::SHRINK_VARS->{"BLOCK"}/) 17 11 28
392 17 ${$javascript} =~ s/$SHRINK_VARS->{BLOCK}/$self->_store_block_data( $1 )/egsm;
393 }
394  
395 11 $self->_restore_data( $javascript, 'block_data', $SHRINK_VARS->{ENCODED_BLOCK} );
396  
397 11 my %shrunk_vars = map { $_ => 1 } ( ${$javascript} =~ /$SHRINK_VARS->{SHRUNK}/g );
398  
399 11 my $cnt = 0;
400 11 foreach my $shrunk_var ( keys( %shrunk_vars ) ) {
401 34 my $short_id;
402 do {
403 34 $short_id = $self->_encode52( $cnt++ );
404 } while (
405 34 ▶ ${$javascript} =~ /[^a-zA-Z0-9_\\x24\.]\Q$short_id\E[^a-zA-Z0-9_\\x24:]/
Branch: 1 branch
BranchTrueFalseTotal
${$javascript;} =~ /[^a-zA-Z0-9_\\x24\.]\Q$short_id\E[^a-zA-Z0-9_\\x24:]/ and do { do { $short_id = $self->_encode52($cnt++) } } 0 34 34
406 );
407  
408 34 ${$javascript} =~ s/$shrunk_var/$short_id/g;
409 }
410  
411 11 $self->reggrp_data_store()->restore_stored( $javascript );
412  
413 11 $self->{block_data} = [];
414 }
415  
416 21 ▶ if ( $self->compress() eq 'obfuscate' or $self->compress() eq 'best' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->compress eq "obfuscate" or $self->compress eq "best") 5 16 21
Condition100%3/3
$self->compress eq "obfuscate" or $self->compress eq "best"
l!l&&r!l&&!r
2316
MC/DC100%2/2
$self->compress eq "obfuscate" or $self->compress eq "best"
$self->compress eq "obfuscate" $self->compress eq "best"
Truth table100%3/3
A or B
A= $self->compress eq "obfuscate"B= $self->compress eq "best"
ABresult
1X1
011
000
417 5 my $words = {};
418  
419 5 my @words = ${$javascript} =~ /$BASE62_VARS->{WORDS}/g;
420  
421 5 my $idx = 0;
422  
423 5 foreach ( @words ) {
424 371 $words->{$_}->{count}++;
425 }
426  
427 5 WORD: foreach my $word ( sort { $words->{$b}->{count} <=> $words->{$a}->{count} } keys( %{$words} ) ) {
428  
429 138 ▶ if ( exists( $words->{$word}->{encoded} ) and $words->{$word}->{encoded} eq $word ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $words->{$word}{"encoded"} and $words->{$word}{"encoded"} eq $word) 14 124 138
Condition66%2/3
exists $words->{$word}{"encoded"} and $words->{$word}{"encoded"} eq $word
!ll&&!rl&&r
124014
MC/DC50%1/2
exists $words->{$word}{"encoded"} and $words->{$word}{"encoded"} eq $word
exists $words->{$word}{"encoded"} $words->{$word}{"encoded"} eq $word
Truth table66%2/3
A and B
A= exists $words->{$word}{"encoded"}B= $words->{$word}{"encoded"} eq $word
ABresult
0X0
100
111
430 14 next WORD;
431 }
432  
433 124 my $encoded = $self->_encode62( $idx );
434  
435 124 ▶ if ( exists( $words->{$encoded} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $words->{$encoded}) 27 97 124
436 27 my $next = 0;
437 27 ▶ if ( exists( $words->{$encoded}->{encoded} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $words->{$encoded}{"encoded"}) 13 14 27
438 13 $words->{$word}->{encoded} = $words->{$encoded}->{encoded};
439 13 $words->{$word}->{index} = $words->{$encoded}->{index};
440 13 $words->{$word}->{minus} = length( $word ) - length( $words->{$word}->{encoded} );
441 13 $next = 1;
442 }
443 27 $words->{$encoded}->{encoded} = $encoded;
444 27 $words->{$encoded}->{index} = $idx;
445 27 $words->{$encoded}->{minus} = 0;
446 27 $idx++;
447 27 ▶ next WORD if ( $next );
Branch: 1 branch
BranchTrueFalseTotal
if $next 13 14 27
448 14 redo WORD;
449 }
450  
451 97 $words->{$word}->{encoded} = $encoded;
452 97 $words->{$word}->{index} = $idx;
453 97 $words->{$word}->{minus} = length( $word ) - length( $encoded );
454  
455 97 $idx++;
456 }
457  
458 5 my $packed_length = length( ${$javascript} );
459  
460 5 my ( @pk, @pattern ) = ( (), () );
461  
462 5 foreach (
463 sort {
464 $words->{$a}->{index} <=> $words->{$b}->{index}
465 507 } keys( %{$words} )
466 ) {
467 124 $packed_length -= ( $words->{$_}->{count} * $words->{$_}->{minus} );
468  
469 124 ▶ if ( $words->{$_}->{encoded} ne $_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($words->{$_}{'encoded'} ne $_) { } 97 27 124
470 97 push( @pk, $_ );
471 97 push( @pattern, $words->{$_}->{encoded} );
472 }
473 else {
474 27 push( @pk, '' );
475 27 push( @pattern, '' );
476 }
477 }
478  
479 5 my $size = scalar( @pattern );
480  
481 5 ▶ splice( @pattern, 62 ) if ( scalar( @pattern ) > 62 );
Branch: 1 branch
BranchTrueFalseTotal
if scalar @pattern > 62 1 4 5
482  
483 5 my $pd = join( '|', @pattern );
484  
485 5 $self->reggrp_trim()->exec( \$pd );
486  
487 5 ▶ unless ( $pd ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pd) { } 5 0 5
488 0 $pd = '^$';
489 }
490 else {
491 5 $pd = '[' . $pd . ']';
492  
493 5 ▶ if ( $size > 62 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size > 62) 1 4 5
494 1 $pd = '(' . $pd . '|';
495  
496 1 my $enc = $self->_encode62( $size );
497  
498 1 my ( $c ) = $enc =~ /(^.)/;
499 1 my $ord = ord( $c );
500  
501 1 my $mul = length( $enc ) - 1;
502  
503 1 my $is62 = 0;
504  
505 1 ▶ if ( $ord >= 65 ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($ord >= 65) { } 0 1 1
elsif ($ord == 57) { } 0 1 1
elsif ($ord == 50) { } 0 1 1
elsif ($ord == 49) { } 1 0 1
506 0 ▶ if ( $c eq 'Z' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq 'Z') { } 0 0 0
507 0 $mul += 1;
508 0 $is62 = 1;
509 }
510 else {
511 0 $pd .= '[0-9a';
512 0 ▶ if ( $ord > 97 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ord > 97) { } 0 0 0
elsif ($ord > 65) { } 0 0 0
513 0 $pd .= '-' . $c;
514 }
515 elsif ( $ord > 65 ) {
516 0 $pd .= '-zA-' . $c;
517 }
518 0 $pd .= ']';
519 }
520 }
521 elsif ( $ord == 57 ) {
522 0 $pd .= '0-9';
523 }
524 elsif ( $ord == 50 ) {
525 0 $pd .= '[12]';
526 }
527 elsif ( $ord == 49 ) {
528 1 $pd .= '1';
529 }
530 else {
531 0 $pd .= '[0-' . ( $ord - 48 ) . ']';
532 }
533  
534 1 ▶ $pd .= '[0-9a-zA-Z]' . ( ( $mul > 1 ) ? '{' . $mul . '}' : '' );
Branch: 1 branch
BranchTrueFalseTotal
$mul > 1 ? : 0 1 1
535  
536 1 ▶ $mul-- if ( $is62 );
Branch: 1 branch
BranchTrueFalseTotal
if $is62 0 1 1
537  
538 1 ▶ if ( $mul > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mul > 1) 0 1 1
539 0 ▶ for ( my $i = $mul; $i >= 2; $i-- ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 2) 0 0 0
540 0 $pd .= '|[0-9a-zA-Z]{' . $i . '}';
541 }
542 }
543  
544 1 $pd .= ')';
545 }
546 }
547 5 $packed_length += length( $pd );
548  
549 5 my $pk = join( '|', @pk );
550 5 $pk =~ s/(?>\|+)$//;
551 5 $packed_length += length( $pk );
552  
553 5 ▶ my $pc = length( $pk ) ? ( ( $pk =~ tr/|/|/ ) + 1 ) : 0;
Branch: 1 branch
BranchTrueFalseTotal
length $pk ? : 5 0 5
554 5 $packed_length += length( $pc );
555  
556 5 my $pa = '[]';
557 5 $packed_length += length( $pa );
558  
559 5 ▶ my $pe = $BASE62_VARS->{'ENCODE' . ( $pc > 10 ? $pc > 36 ? 62 : 36 : 10 )};
Branch: 2 branches
BranchTrueFalseTotal
$pc > 10 ? : 1 4 5
$pc > 36 ? : 1 0 1
560 5 $packed_length += length( $pe );
561  
562  
563 5 $packed_length += length( $BASE62_VARS->{UNPACK} );
564 5 $packed_length -= ( $BASE62_VARS->{UNPACK} =~ s/(%s|%d)/$1/g ) * 2;
565  
566 5 my ( @length_matches ) = ${$javascript} =~ s/((?>[\r\n]+))/$1/g;
567 5 foreach ( @length_matches ) {
568 5 $packed_length -= length( $_ ) - 3;
569 }
570  
571 5 $packed_length += ${$javascript} =~ tr/\\\'/\\\'/;
572  
573 5 ▶ if ( $self->compress() eq 'obfuscate' or $packed_length <= length( ${$javascript} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->compress eq "obfuscate" or $packed_length <= length ${$javascript;}) 3 2 5
Condition100%3/3
$self->compress eq "obfuscate" or $packed_length <= length ${$javascript;}
l!l&&r!l&&!r
212
MC/DC100%2/2
$self->compress eq "obfuscate" or $packed_length <= length ${$javascript;}
$self->compress eq "obfuscate" $packed_length <= length ${$javascript;}
Truth table100%3/3
A or B
A= $self->compress eq "obfuscate"B= $packed_length <= length ${$javascript;}
ABresult
1X1
011
000
574  
575 3 ${$javascript} =~ s/$BASE62_VARS->{WORDS}/sprintf( "%s", $words->{$1}->{encoded} )/eg;
576  
577 3 ${$javascript} =~ s/([\\'])/\\$1/g;
578 3 ${$javascript} =~ s/[\r\n]+/\\n/g;
579  
580 3 my $pp = ${$javascript};
581  
582 3 ${$javascript} = sprintf( $BASE62_VARS->{UNPACK}, $pe, $pd, $pp, $pa, $pc, $pk );
583 }
584  
585 }
586  
587 21 ▶ if ( not $self->remove_copyright() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->remove_copyright) 20 1 21
588 20 ▶ ${$javascript} = ( $self->copyright() || $self->_copyright_comment() ) . ${$javascript};
Condition100%3/3
$self->copyright || $self->_copyright_comment
l!l&&r!l&&!r
4115
MC/DC100%2/2
$self->copyright || $self->_copyright_comment
$self->copyright $self->_copyright_comment
Truth table100%3/3
A || B
A= $self->copyrightB= $self->_copyright_comment
ABresult
1X1
011
000
589 }
590  
591 21 ▶ return ${$javascript} if ( $cont eq 'scalar' );
Branch: 1 branch
BranchTrueFalseTotal
if $cont eq "scalar" 0 21 21
592 }
593  
594 sub _restore_data {
595 63 ▶ my ( $self, $string_ref, $data_name, $pattern ) = @_;
Subroutine _restore_data: called CC=2 SCAR=6.9
596  
597 63 ▶ while ( ${$string_ref} =~ /$pattern/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (${$string_ref;} =~ /$pattern/) 30 63 93
598 30 ${$string_ref} =~ s/$pattern/$self->{$data_name}->[$1]/egsm;
599 }
600 }
601  
602 sub _store_block_data {
603 46 ▶ my ( $self, $match ) = @_;
Subroutine _store_block_data: called CC=13 SCAR=25.7
604  
605 46 my ( undef, $prefix, $blocktype, $args, $block ) = $match =~ /$SHRINK_VARS->{BLOCK}/;
606  
607 46 ▶ $prefix ||= '';
Condition100%2/2
$prefix ||= ""
l!l
3412
MC/DC100%1/1
$prefix ||= ""
$prefix
Truth table100%2/2
A
A= $prefix
Aresult
11
00
608 46 ▶ $blocktype ||= '';
Condition100%2/2
$blocktype ||= ""
l!l
3412
MC/DC100%1/1
$blocktype ||= ""
$blocktype
Truth table100%2/2
A
A= $blocktype
Aresult
11
00
609 46 ▶ $args ||= '';
Condition100%2/2
$args ||= ""
l!l
3412
MC/DC100%1/1
$args ||= ""
$args
Truth table100%2/2
A
A= $args
Aresult
11
00
610 46 my $replacement = '';
611 46 ▶ if ( $blocktype eq 'function' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($blocktype eq 'function') { } 26 20 46
612  
613 26 $self->_restore_data( \$block, 'block_data', $SHRINK_VARS->{SCOPED} );
614  
615 26 $args =~ s/\s*//g;
616  
617 26 $block = $args . $block;
618 26 $prefix =~ s/$SHRINK_VARS->{BRACKETS}//;
619  
620 26 $args =~ s/^\(|\)$//g;
621  
622 26 ▶ while( $args =~ /$SHRINK_VARS->{CALLER}/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args =~ /$JavaScript::Packer::SHRINK_VARS->{"CALLER"}/) 0 26 26
623 0 $args =~ s/$SHRINK_VARS->{CALLER}//gsm;
624 }
625  
626 26 my @vars = grep( $_, split( /\s*,\s*/, $args ) );
627 26 ▶ my $do_shrink = grep( $_ eq '_no_shrink_', @vars ) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
grep(($_ eq '_no_shrink_'), @vars) ? : 1 25 26
628  
629 26 my %block_vars = ();
630 26 ▶ if ( $do_shrink ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_shrink) 25 1 26
631 25 %block_vars = map { $_ => 1 } ( $block =~ /$SHRINK_VARS->{VARS}/g ), grep( $_ ne '$super', @vars );
632 }
633  
634 26 $self->_restore_data( \$block, 'block_data', $SHRINK_VARS->{ENCODED_BLOCK} );
635  
636 26 ▶ if ( $do_shrink ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_shrink) 25 1 26
637  
638 25 my $cnt = 0;
639 25 foreach my $block_var ( keys( %block_vars ) ) {
640 50 ▶ if ( length( $block_var ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $block_var) 50 0 50
641 50 ▶ while ( $block =~ /$SHRINK_VARS->{PREFIX}\Q$cnt\E\b/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block =~ /$JavaScript::Packer::SHRINK_VARS->{"PREFIX"}\Q$cnt\E\b/) 14 50 64
642 14 $cnt++;
643 }
644  
645 50 ▶ while ( $block =~ /[^a-zA-Z0-9_\\x24\.]\Q$block_var\E[^a-zA-Z0-9_\\x24:]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block =~ /[^a-zA-Z0-9_\\x24\.]\Q$block_var\E[^a-zA-Z0-9_\\x24:]/) 52 50 102
646 52 $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;
647 }
648  
649 50 $block =~ s/([^{,a-zA-Z0-9_\\x24\.])\Q$block_var\E:/sprintf( "%s\x02%d:", $1, $cnt )/eg;
650  
651 50 $cnt++;
652 }
653 }
654 }
655 26 $replacement = sprintf( "%s~%d~", $prefix, scalar( @{$self->{block_data}} ) );
656  
657 26 push( @{$self->{block_data}}, $block );
658 }
659 else {
660 20 $replacement = sprintf( "~#%d~", scalar( @{$self->{block_data}} ) );
661  
662 20 push( @{$self->{block_data}}, $prefix . $block );
663 }
664  
665 46 return $replacement;
666 };
667  
668 sub _encode52 {
669 34 ▶ my ( $self, $c ) = @_;
Subroutine _encode52: called CC=4 SCAR=15.0
670  
671 34 my $m = $c % 52;
672  
673 34 ▶ my $ret = $m > 25 ? chr( $m + 39 ) : chr( $m + 97 );
Branch: 1 branch
BranchTrueFalseTotal
$m > 25 ? : 0 34 34
674  
675 34 ▶ if ( $c >= 52 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c >= 52) 0 34 34
676 0 $ret = $self->_encode52( int( $c / 52 ) ) . $ret;
677 }
678  
679 34 ▶ $ret = substr( $ret, 1 ) . '0' if ( $ret =~ /^(do|if|in)$/ );
Branch: 1 branch
BranchTrueFalseTotal
if $ret =~ /^(do|if|in)$/ 0 34 34
680  
681 34 return $ret;
682 };
683  
684  
685 sub _encode62 {
686 151 ▶ my ( $self, $c ) = @_;
Subroutine _encode62: called CC=4 SCAR=13.9
687  
688 151 my $m = $c % 62;
689  
690 151 ▶ my $ret = $m > 35 ? chr( $m + 29 ) : $m > 9 ? chr( $m + 87 ) : $m;
Branch: 2 branches
BranchTrueFalseTotal
$m > 35 ? : 26 125 151
$m > 9 ? : 53 72 125
691  
692 151 ▶ if ( $c >= 62 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c >= 62) 26 125 151
693 26 $ret = $self->_encode62( int( $c / 62 ) ) . $ret;
694 }
695  
696 151 return $ret;
697 };
698  
699 3 1;
700  
701 __END__
↑ Summary