blib/lib/JavaScript/Packer.pm

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