blib/lib/JavaScript/XRay.pm

filter: Statementstmt 74.7% 169 / 226 Branchbran 60.3% 70 / 116 Conditioncond 64.6% 42 / 65 MC/DCmcdc 53.1% 17 / 32 Subroutinesub 90.0% 18 / 20 Podpod 100% 4 / 4 totaltotal 69.1% 320 / 463 CC 89 SCAR 37.8
CC
89
Cov
69%
_get_external_javascript
210.0
inline_methods
72.0
_inline_javascript
31.0
CRAP 44.0
↑ Summary
1 package JavaScript::XRay;
2 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use LWP::Simple qw(get);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use constant IFRAME_DEFAULT_HEIGHT => 200;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 our $VERSION = '1.22';
10 3 our $PACKAGE = __PACKAGE__;
11 3 our %SWITCHES = (
12 all => {
13 type => 'bool',
14 desc => 'filter all functions (default)',
15 },
16 none => {
17 type => 'bool',
18 desc => 'don\'t filter any functions',
19 },
20 anon => {
21 type => 'bool',
22 desc => 'filter anon functions (noisy)',
23 },
24 no_exec_count => {
25 type => 'bool',
26 desc => 'don\'t count function executions',
27 },
28 only => {
29 type => 'function1,function2,...',
30 desc => 'only filter listed functions (exact)',
31 ref_type => 'ARRAY',
32 },
33 skip => {
34 type => 'function1,function2,...',
35 desc => 'skip listed functions (exact)',
36 ref_type => 'ARRAY'
37 },
38 uncomment => {
39 type => 'string1,string2,...',
40 desc => 'uncomment lines prefixed with string (DEBUG1,DEBUG2)',
41 ref_type => 'ARRAY'
42 },
43 match => {
44 type => 'string',
45 desc => 'only filter functions that match string (/^string/)',
46 ref_type => 'Regexp'
47 },
48 );
49  
50 3 our @SWITCH_KEYS = keys %SWITCHES;
51  
52 7 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
53 7 my ( $class, %args ) = @_;
54  
55 7 ▶ my $alias = $args{alias} || 'jsxray';
Condition100%2/2
$args{'alias'} || 'jsxray'
l!l
34
MC/DC100%1/1
$args{'alias'} || 'jsxray'
$args{'alias'}
Truth table100%2/2
A
A= $args{'alias'}
Aresult
11
00
56 my $obj = {
57 alias => $alias,
58 iframe_height => $args{iframe_height} || IFRAME_DEFAULT_HEIGHT,
59 css_inline => $args{css_inline},
60 css_external => $args{css_external},
61 verbose => $args{verbose},
62 7 ▶ inline_methods => ['HTTP_GET'],
Condition100%2/2
$args{'iframe_height'} || 200
l!l
25
MC/DC100%1/1
$args{'iframe_height'} || 200
$args{'iframe_height'}
Truth table100%2/2
A
A= $args{'iframe_height'}
Aresult
11
00
63 js_log => '',
64 js_log_init => '',
65 js_switches => '',
66 js_function_names => '',
67 };
68  
69 7 bless $obj, $class;
70  
71 7 $obj->_init_uri( $args{abs_uri} );
72 7 ▶ $obj->switches( %{$args{switches}} ) if $args{switches};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"switches"} 6 1 7
73  
74 7 return $obj;
75 }
76  
77 7 ▶sub _init_uri {
Subroutine _init_uri: called CC=3 SCAR=16.5
78 7 my ( $self, $abs_uri ) = @_;
79 7 ▶ return unless $abs_uri;
Branch: 1 branch
BranchTrueFalseTotal
unless $abs_uri 7 0 7
80 0 ▶ $self->{abs_uri} = ref $abs_uri eq 'URI' ? $abs_uri : URI->new($abs_uri);
Branch: 1 branch
BranchTrueFalseTotal
ref $abs_uri eq 'URI' ? : 0 0 0
81 0 return;
82 }
83  
84 6 ▶sub switches {
Subroutine switches: called CC=8 SCAR=22.2
Pod documented
85 6 my ( $self, %switches ) = @_;
86 6 ▶ return $self->{switches} unless keys %switches;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %switches 0 6 6
87  
88 # allow 'jsxray_uncomment' or just 'uncomment'
89 6 my $alias = $self->{alias};
90 %switches = map {
91 6 my $new_key = $_;
92 14 $new_key =~ s/^$alias\_//;
93 14 ( $new_key => $switches{$_} );
94 } keys %switches;
95  
96 6 for my $switch ( keys %switches ) {
97 14 ▶ unless ( $SWITCHES{$switch} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($JavaScript::XRay::SWITCHES{$switch}) 0 14 14
98 0 warn "invalid switch: $switch";
99 0 next;
100 }
101 14 my $ref_type = ref $switches{$switch};
102 $self->{switches}{$switch} =
103 $ref_type eq 'ARRAY' && $SWITCHES{$switch}{ref_type} eq 'ARRAY'
104 0 ? join(',', @{ $switches{$switch} })
105 14 ▶ : $switches{$switch};
Branch: 1 branch
BranchTrueFalseTotal
$ref_type eq 'ARRAY' && $JavaScript::XRay::SWITCHES{$switch}{'ref_type'} eq 'ARRAY' ? : 0 14 14
Condition33%1/3
$ref_type eq "ARRAY" and $JavaScript::XRay::SWITCHES{$switch}{"ref_type"} eq "ARRAY"
!ll&&!rl&&r
1400
MC/DC0%0/2
$ref_type eq "ARRAY" and $JavaScript::XRay::SWITCHES{$switch}{"ref_type"} eq "ARRAY"
$ref_type eq "ARRAY" $JavaScript::XRay::SWITCHES{$switch}{"ref_type"} eq "ARRAY"
Truth table33%1/3
A and B
A= $ref_type eq "ARRAY"B= $JavaScript::XRay::SWITCHES{$switch}{"ref_type"} eq "ARRAY"
ABresult
0X0
100
111
106  
107 14 $self->{js_switches} .= qq|${alias}_switches.push("${alias}_${switch}");\n|;
108 }
109  
110 # init other switches so we don't get warnings
111 6 for my $switch (@SWITCH_KEYS) {
112 $self->{switches}{$switch} = ''
113 48 ▶ unless $self->{switches}{$switch};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"switches"}{$switch} 35 13 48
114 }
115  
116 6 return %{ $self->{switches} };
117 }
118  
119 0 ▶sub inline_methods {
Subroutine inline_methods: not called CC=8 SCAR=42.8
Pod documented
120 0 my ( $self, @methods ) = @_;
121  
122 0 ▶ if ( @methods ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@methods) 0 0 0
123 0 my @valid_methods = ();
124 0 for my $method (@methods) {
125 0 ▶ unless ( -d $method
Branch: 1 branch
BranchTrueFalseTotal
if (-d $method or $method eq 'HTTP_GET' or ref $method eq 'CODE') { } 0 0 0
Condition0%0/6
-d $method or $method eq "HTTP_GET" or ref $method eq "CODE"
l!l&&r!l&&!r
000
-d $method or $method eq "HTTP_GET"
l!l&&r!l&&!r
000
MC/DC0%0/3
-d $method or $method eq "HTTP_GET" or ref $method eq "CODE"
-d $method $method eq "HTTP_GET" ref $method eq "CODE"
Truth table0%0/4
A or B or C
A= -d $methodB= $method eq "HTTP_GET"C= ref $method eq "CODE"
ABCresult
1XX1
01X1
0011
0000
126 || $method eq 'HTTP_GET'
127 || ref $method eq 'CODE' )
128 {
129 0 warn 'inline methods may only be local server '
130 . 'directories, code references, or the special string '
131 . "HTTP_GET - invalid method: $method";
132 }
133 else {
134 0 push @valid_methods, $method;
135 }
136 }
137  
138 0 ▶ unless (@valid_methods) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@valid_methods) 0 0 0
139 0 warn 'inline_methods called without valid methods';
140 0 exit;
141 }
142
143 0 $self->{inline_methods} = \@valid_methods;
144 }
145  
146 0 ▶ return wantarray ? @{ $self->{inline_methods} } : $self->{inline_methods};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
147 }
148  
149 7 ▶sub filter {
Subroutine filter: called CC=7 SCAR=19.5
Pod documented
150 7 my ( $self, $html ) = @_;
151  
152 7 my ( $alias, $switch ) = ( $self->{alias}, $self->{switches} );
153  
154 $self->_warn( 'Tracing anonymous functions' )
155 7 ▶ if $switch->{anon} && !$switch->{only};
Branch: 1 branch
BranchTrueFalseTotal
if $switch->{"anon"} and not $switch->{"only"} 2 5 7
Condition100%3/3
$switch->{"anon"} and not $switch->{"only"}
!ll&&!rl&&r
322
MC/DC100%2/2
$switch->{"anon"} and not $switch->{"only"}
$switch->{"anon"} not $switch->{"only"}
Truth table100%3/3
A and not(B)
A= $switch->{"anon"}B= not $switch->{"only"}
ABresult
0X0
100
111
156  
157 $self->_warn( "Only tracing functions exactly matching: $switch->{only}" )
158 7 ▶ if $switch->{only};
Branch: 1 branch
BranchTrueFalseTotal
if $switch->{"only"} 3 4 7
159  
160 7 ▶ $self->_warn( "Skipping functions: $switch->{skip}" ) if $switch->{skip};
Branch: 1 branch
BranchTrueFalseTotal
if $switch->{"skip"} 1 6 7
161  
162 $self->_warn( "Tracing matching functions: /^$switch->{match}/" )
163 7 ▶ if $switch->{match};
Branch: 1 branch
BranchTrueFalseTotal
if $switch->{"match"} 1 6 7
164  
165 7 $html = $self->_filter($html);
166 6 $html = $self->_inline_javascript($html);
167  
168 6 ▶ $self->_uncomment( \$html ) if $switch->{uncomment};
Branch: 1 branch
BranchTrueFalseTotal
if $switch->{"uncomment"} 2 4 6
169 6 $self->_inject_console( \$html );
170  
171 6 $self->_inject_js_css( \$html );
172  
173 6 return $html;
174 }
175  
176 7 ▶sub _filter {
Subroutine _filter: called CC=24 SCAR=31.8
177 7 my ( $self, $work_html ) = @_;
178  
179 7 my ( $alias, $switch ) = ( $self->{alias}, $self->{switches} );
180  
181 7 my $new_html = '';
182 7 ▶ while (
Branch: 1 branch
BranchTrueFalseTotal
if ($work_html =~ / \G (.+?) ( function? \s* (?:\w|_)+? \s*? [(] .+? [)]? \s* \{ ) /cgimosx) 35 6 41
183 $work_html =~ m{
184 \G
185 (.+?)
186 (
187 function?
188 \s*
189 (?:\w|_)+?
190 \s*?
191 [(]
192 .+?
193 [)]?
194 \s*
195 \{
196 )
197 }cgimosx
198 )
199 {
200  
201 # build output page from input page
202 35 $new_html .= $1;
203  
204 # find the function name
205 35 my $function .= $2;
206 35 my ($name) = $function =~ m/function\s*(\w+?)?\s*?\(/gx;
207 35 ▶ $name = '' unless $name; # define it to supress warnings
Branch: 1 branch
BranchTrueFalseTotal
unless $name 7 28 35
208  
209 # don't want any recursive JavaScript loops
210 35 ▶ croak( "found function '$name', functions may "
Branch: 1 branch
BranchTrueFalseTotal
if $name eq $alias 1 34 35
211 . "not match alias: '$alias'" )
212 if $name eq $alias;
213  
214 # find the function arguments
215 34 my ($args) = $function =~ m/function\s*$name?\s*?[(](.+?)[)]/gx;
216 34 ▶ $name = 'ANON' unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 7 27 34
217  
218 34 ▶ unless ( $switch->{no_exec_count}
Branch: 1 branch
BranchTrueFalseTotal
unless ($switch->{"no_exec_count"} or $name eq "ANON" and not $switch->{"anon"}) 26 8 34
Condition100%6/6
$switch->{"no_exec_count"} or $name eq "ANON" and not $switch->{"anon"}
l!l&&r!l&&!r
5326
$name eq "ANON" and not $switch->{"anon"}
!ll&&!rl&&r
2333
MC/DC100%3/3
$switch->{"no_exec_count"} or $name eq "ANON" and not $switch->{"anon"}
$switch->{"no_exec_count"} $name eq "ANON" not $switch->{"anon"}
Truth table100%4/4
A or B and not(C)
A= $switch->{"no_exec_count"}B= $name eq "ANON"C= not $switch->{"anon"}
ABCresult
1XX1
0111
00X0
0100
219 || ( $name eq 'ANON' && !$switch->{anon} ) )
220 {
221 26 $self->{js_log_init} .= "${alias}_exec_count['$name'] = 0;\n";
222 26 $function .= "${alias}_exec_count['$name']++;";
223 }
224  
225 # functions for use in form to select query parameters
226 34 ▶ $self->_switch_function_options($name) if ($name ne 'ANON');
Branch: 1 branch
BranchTrueFalseTotal
if $name ne "ANON" 27 7 34
227
228 my %only_function = $switch->{only}
229 23 ? map { $_ => 1 } split( /\,/, $switch->{only} )
230 34 ▶ : ();
Branch: 1 branch
BranchTrueFalseTotal
$switch->{'only'} ? : 14 20 34
231 my %skip_function = $switch->{skip}
232 5 ? map { $_ => 1 } split( /\,/, $switch->{skip} )
233 34 ▶ : ();
Branch: 1 branch
BranchTrueFalseTotal
$switch->{'skip'} ? : 5 29 34
234  
235 34 my $function_filter = '';
236 34 ▶ if (ref $switch->{match} eq 'Regexp') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $switch->{'match'} eq 'Regexp') { } 0 34 34
elsif ($switch->{'match'}) { } 5 29 34
237 0 $function_filter = $switch->{match};
238 }
239 elsif ( $switch->{match} ) {
240 5 my $safe_filter = quotemeta $switch->{match};
241 5 $function_filter = qr/^$safe_filter/;
242 }
243  
244 # skip filter
245 # if none
246 # if anon and not filtering anon functions
247 # if switch 'only' used and function doesn't match
248 # if switch 'skip' used and function matches
249 # if switch 'filter' used and function doesn't match
250 34 ▶ if ( ( $switch->{none} )
Branch: 1 branch
BranchTrueFalseTotal
if ($switch->{'none'} or $name eq 'ANON' and not $switch->{'anon'} or $switch->{'only'} and not $only_function{$name} or $switch->{'skip'} and $skip_function{$name} or $switch->{'match'} and not $name =~ /$function_filter/x) { } 19 15 34
Condition95%23/24
$switch->{"none"} or $name eq "ANON" and not $switch->{"anon"} or $switch->{"only"} and not $only_function{$name} or $switch->{"skip"} and $skip_function{$name} or $switch->{"match"} and not $name =~ /$function_filter/x
l!l&&r!l&&!r
16315
$switch->{"none"} or $name eq "ANON" and not $switch->{"anon"} or $switch->{"only"} and not $only_function{$name} or $switch->{"skip"} and $skip_function{$name}
l!l&&r!l&&!r
15118
$switch->{"none"} or $name eq "ANON" and not $switch->{"anon"} or $switch->{"only"} and not $only_function{$name}
l!l&&r!l&&!r
31219
$switch->{"none"} or $name eq "ANON" and not $switch->{"anon"}
l!l&&r!l&&!r
0331
$name eq "ANON" and not $switch->{"anon"}
!ll&&!rl&&r
2743
$switch->{"only"} and not $only_function{$name}
!ll&&!rl&&r
18112
$switch->{"skip"} and $skip_function{$name}
!ll&&!rl&&r
1441
$switch->{"match"} and not $name =~ /$function_filter/x
!ll&&!rl&&r
1413
MC/DC88%8/9
$switch->{"none"} or $name eq "ANON" and not $switch->{"anon"} or $switch->{"only"} and not $only_function{$name} or $switch->{"skip"} and $skip_function{$name} or $switch->{"match"} and not $name =~ /$function_filter/x
$switch->{"none"} $name eq "ANON" not $switch->{"anon"} $switch->{"only"} not $only_function{$name} $switch->{"skip"} $skip_function{$name} $switch->{"match"} not $name =~ /$function_filter/x
Truth table34%11/32
A or B and not(C) or D and not(E) or F and G or H and not(I)
A= $switch->{"none"}B= $name eq "ANON"C= not $switch->{"anon"}D= $switch->{"only"}E= not $only_function{$name}F= $switch->{"skip"}G= $skip_function{$name}H= $switch->{"match"}I= not $name =~ /$function_filter/x
ABCDEFGHIresult
1XXXXXXXX1
011XXXXXX1
00X11XXXX1
01011XXXX1
00X0X11XX1
00X1011XX1
0100X11XX1
0101011XX1
00X0X0X111
00X0X10111
00X100X111
00X1010111
0100X0X111
0100X10111
010100X111
0101010111
00X0X0X0X0
00X0X0X100
00X0X100X0
00X0X10100
00X100X0X0
00X100X100
00X10100X0
00X1010100
0100X0X0X0
0100X0X100
0100X100X0
0100X10100
010100X0X0
010100X100
01010100X0
0101010100
251 || ( $name eq 'ANON' && !$switch->{anon} )
252 || ( $switch->{only} && !$only_function{$name} )
253 || ( $switch->{skip} && $skip_function{$name} )
254 || ( $switch->{match} && $name !~ m/$function_filter/x ) )
255 {
256 19 $new_html .= $function;
257 }
258 else {
259 15 $self->_warn("Found function '$name'");
260  
261 # build out function arguments - this is the cool part
262 # you also get to see the value of arguments passed to the
263 # function, _extremely_ handy
264 15 my $filtered_args = '';
265 15 ▶ if ($args) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args) 3 12 15
266 3 my @arg_list = split( /\,/, $args );
267 3 $filtered_args = '\'+' . join( '+\', \'+', @arg_list ) . '+\'';
268 }
269  
270 # insert the log call
271 $new_html
272 15 .= $function . "$alias('$name( $filtered_args )');";
273 }
274 }
275  
276 6 ▶ if ( $work_html =~ /\G(.*)/cgs ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($work_html =~ /\G(.*)/cgs) 6 0 6
277 6 $new_html .= $1;
278 }
279  
280 6 return $new_html;
281 }
282  
283 6 ▶sub _inline_javascript {
Subroutine _inline_javascript: called CC=11 SCAR=34.3
284 6 my ( $self, $work_html ) = @_;
285  
286 6 my $new_html = '';
287  
288 # look through the HTML for script blocks
289 6 ▶ while (
Branch: 1 branch
BranchTrueFalseTotal
if ($work_html =~ m[ \G (.*?) ( <script .*? <\/script> ) ]cgimosx) 6 6 12
290 $work_html =~ m{
291 \G
292 (.*?)
293 (
294 <script
295 .*?
296 <\/script>
297 )
298 }cgimosx
299 )
300 {
301 6 $new_html .= $1;
302 6 my $script_block = $2;
303  
304 # pull out both script attributes and inner script
305 6 ▶ while (
Branch: 1 branch
BranchTrueFalseTotal
if ($script_block =~ m[ <script (.*?) \s*?> (.*?) <\/script> ]cgimosx) 6 6 12
306 $script_block =~ m{
307 <script
308 (.*?)
309 \s*?>
310 (.*?)
311 <\/script>
312 }cgimosx
313 )
314 {
315 6 my ( $script_attrs, $inner_script ) = ( $1, $2 );
316 6 $script_attrs =~ s/\s*\=\s*/\=/g; # clean up white space
317  
318 # parse out name value pairs so we can rebuild the script
319 # element properly. (special case 'defer' is a boolean
320 # and has no value
321 6 my %attrs = ();
322 6 ▶ while (
Branch: 1 branch
BranchTrueFalseTotal
if ($script_attrs =~ / \G \s* (?: (defer) | (.+?) \s* \= \s* (?: [\"\'](.+?)[\"\'] | (\w+) ) ) /cgimosx) 0 6 6
323 $script_attrs =~ m{
324 \G
325 \s*
326 (?: (defer) |
327 (.+?)
328 \s*
329 \=
330 \s*
331 (?: [\"\'](.+?)[\"\'] | (\w+) )
332 )
333 }cgimosx
334 )
335 {
336  
337 0 ▶ my ( $defer, $name, $value ) = ( $1, $2, $3 || $4 );
Condition0%0/3
$3 || $4
l!l&&r!l&&!r
000
MC/DC0%0/2
$3 || $4
$3 $4
Truth table0%0/3
A || B
A= $3B= $4
ABresult
1X1
011
000
338 0 ▶ if ($defer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($defer) { } 0 0 0
339 0 $attrs{$defer} = 1;
340 }
341 else {
342 0 $attrs{$name} = $value;
343 }
344 }
345  
346 6 ▶ if ( keys %attrs && $attrs{src} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %attrs and $attrs{'src'}) { } 0 6 6
Condition33%1/3
keys %attrs and $attrs{"src"}
!ll&&!rl&&r
600
MC/DC0%0/2
keys %attrs and $attrs{"src"}
keys %attrs $attrs{"src"}
Truth table33%1/3
A and B
A= keys %attrsB= $attrs{"src"}
ABresult
0X0
100
111
347 my @attrs = map {
348 0 ▶ $_ eq 'defer' ? $_ : "$_=\"$attrs{$_}\"";
Branch: 1 branch
BranchTrueFalseTotal
$_ eq 'defer' ? : 0 0 0
349 0 } grep { $_ ne 'src' } keys %attrs;
350  
351 0 my $js = $self->_get_external_javascript($attrs{src});
352
353 0 ▶ if ($js) {
Branch: 1 branch
BranchTrueFalseTotal
if ($js) { } 0 0 0
354 0 my $inline_javascript = '<script '
355 . join( ' ', @attrs ) . "><!--\n"
356 . $js
357 . "\n--></script>";
358  
359 0 $new_html .= "<!-- inline $attrs{src} -->\n";
360 0 $new_html .= $inline_javascript;
361 }
362 else {
363 0 warn 'failed to inline (or referenced URI is empty): '
364 . $script_block;
365 0 $new_html .= $script_block;
366 }
367 }
368 else {
369 6 $new_html .= $script_block;
370 }
371 }
372 }
373  
374 6 ▶ if ( $work_html =~ /\G(.*)/cgs ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($work_html =~ /\G(.*)/cgs) 6 0 6
375 6 $new_html .= $1;
376 }
377  
378 6 return $new_html;
379 }
380  
381 0 ▶sub _get_external_javascript {
Subroutine _get_external_javascript: not called CC=14 SCAR=53.5
382 0 my ( $self, $src ) = @_;
383 0 my $js = '';
384  
385 0 ▶ if ( $src !~ /^http/i && !$self->{abs_uri} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($src =~ /^http/i or $self->{"abs_uri"}) 0 0 0
Condition0%0/3
$src =~ /^http/i or $self->{"abs_uri"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$src =~ /^http/i or $self->{"abs_uri"}
$src =~ /^http/i $self->{"abs_uri"}
Truth table0%0/3
A or B
A= $src =~ /^http/iB= $self->{"abs_uri"}
ABresult
1X1
011
000
386 0 warn 'unable to inline/filter external javascript files with an'
387 . 'absolute request uri: abs_uri not defined';
388 0 return $js;
389 }
390  
391 # if true its an absolute uri so no need to call new_abs
392 my $abs_js_uri =
393 $src =~ /^http/ || ( $src =~ /^\// && $self->{abs_uri} =~ /^\// )
394 ? URI->new($src)
395 0 ▶ : URI->new_abs( $src, $self->{abs_uri} );
Branch: 1 branch
BranchTrueFalseTotal
$src =~ /^http/ || $src =~ m[^/] && $self->{'abs_uri'} =~ m[^/] ? : 0 0 0
Condition0%0/6
$src =~ /^http/ or $src =~ m[^/] and $self->{"abs_uri"} =~ m[^/]
l!l&&r!l&&!r
000
$src =~ m[^/] and $self->{"abs_uri"} =~ m[^/]
!ll&&!rl&&r
000
MC/DC0%0/3
$src =~ /^http/ or $src =~ m[^/] and $self->{"abs_uri"} =~ m[^/]
$src =~ /^http/ $src =~ m[^/] $self->{"abs_uri"} =~ m[^/]
Truth table0%0/4
A or B and C
A= $src =~ /^http/B= $src =~ m[^/]C= $self->{"abs_uri"} =~ m[^/]
ABCresult
1XX1
0111
00X0
0100
396  
397 0 for my $method ( @{$self->{inline_methods}} ) {
398 0 ▶ if ($method eq 'HTTP_GET') {
Branch: 3 branches
BranchTrueFalseTotal
if ($method eq 'HTTP_GET') { } 0 0 0
elsif (-d $method) { } 0 0 0
elsif (ref $method eq 'CODE') { } 0 0 0
399 $self->_warn("attempting to fetch: $abs_js_uri")
400 0 ▶ if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
401 0 $js = get( $abs_js_uri );
402 }
403 elsif ( -d $method ) {
404 0 my $possible_js_file = URI->new_abs( $src, $method );
405 0 ▶ if ( open( my $fh, '<', $possible_js_file ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, '<', $possible_js_file) { } 0 0 0
406 0 $js = do { local $/ = undef; <$fh> };
407 0 close $fh;
408 }
409 else {
410 0 warn "failed to open: $possible_js_file: $!";
411 }
412 }
413 elsif ( ref $method eq 'CODE' ) {
414 0 $js = &$method( $src, $self->{abs_uri} );
415 }
416 0 ▶ last if $js;
Branch: 1 branch
BranchTrueFalseTotal
if $js 0 0 0
417 }
418  
419 0 ▶ if ($js) {
Branch: 1 branch
BranchTrueFalseTotal
if ($js) 0 0 0
420 0 $self->_warn("Inlining and Filtering $src");
421 0 $js = $self->_filter($js);
422 }
423  
424 0 return $js;
425 }
426  
427 2 ▶sub _uncomment {
Subroutine _uncomment: called CC=4 SCAR=13.9
428 2 my ( $self, $html_ref ) = @_;
429 2 my $switch = $self->{switches};
430  
431 # uncomment nessesary tags
432 my @uncomment_strings
433 2 = map { quotemeta($_) } split( /\,/, $switch->{uncomment} );
434 2 for my $uncomment (@uncomment_strings) {
435 3 my $uncomment_count = $$html_ref =~ s/\/\/$uncomment//gs;
436 3 ▶ if ($uncomment_count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uncomment_count) 1 2 3
437 1 ▶ my $label = $uncomment_count > 1 ? 'instances' : 'instance';
Branch: 1 branch
BranchTrueFalseTotal
$uncomment_count > 1 ? : 0 1 1
438 1 $self->_warn( "$PACKAGE->filter uncommented $uncomment: "
439 . "Found $uncomment_count $label" );
440 }
441 }
442  
443 2 return;
444 }
445  
446 6 ▶sub _inject_js_css {
Subroutine _inject_js_css: called CC=1 SCAR=0.0
447 6 my ( $self, $html_ref ) = @_;
448 6 my ( $alias, $switches ) = ( $self->{alias}, $self->{switches} );
449  
450 6 my $js_css = qq|<script><!--
451 var ${alias}_logging_on = true;
452 var ${alias}_doc = null;
453 var ${alias}_cont_div = null;
454 var ${alias}_last_div = null;
455 var ${alias}_count = 1;
456 var ${alias}_exec_count = [];
457 var ${alias}_date_start;
458 var ${alias}_time_start;
459 var ${alias}_pre_iframe_queue = [];
460 $self->{js_log}
461  
462 function ${alias}( msg ) {
463 if ( !${alias}_logging_on ) return;
464 if ( ${alias}_doc == null) {
465 if ( ! ${alias}_init( "Initialized" ) ) {
466 ${alias}_pre_iframe_queue.push(msg);
467 return;
468 }
469 else {
470 for( var x = 0; x < ${alias}_pre_iframe_queue.length; x++ ) {
471 ${alias}_log( ${alias}_pre_iframe_queue[x] );
472 }
473 ${alias}_pre_iframe_queue = [];
474 }
475 }
476 ${alias}_log( msg );
477 }
478  
479 function ${alias}_log ( msg ) {
480 // timing data
481 var ${alias}_date_now = new Date();
482 var ${alias}_time_since = ${alias}_date_now.getTime();
483 var ${alias}_elapsed_time =
484 ( ${alias}_time_since - ${alias}_time_start );
485 var ${alias}_time = ${alias}_date_format( ${alias}_date_now );
486 var ${alias}_div = ${alias}_doc.createElement( 'DIV' );
487  
488 ${alias}_div.className = "${alias}_desc";
489 ${alias}_doc.body.appendChild( ${alias}_div );
490 ${alias}_cont_div.insertBefore(${alias}_div, ${alias}_last_div);
491 ${alias}_div.innerHTML = "<span class='${alias}_loginfo'>[ "
492 + ${alias}_count + ' - ' + ${alias}_time + ' - '
493 + ${alias}_elapsed_time + "ms ]</span> " + msg;
494 ${alias}_count++;
495 ${alias}_last_div = ${alias}_div;
496 }
497  
498 function ${alias}_init(init_msg) {
499 $self->{js_log_init}
500 ${alias}_date_start = new Date();
501 ${alias}_time_start = ${alias}_date_start.getTime();
502 if (!window.frames.${alias}_iframe) return;
503 ${alias}_doc = window.frames.${alias}_iframe.document;
504 ${alias}_doc.open();
505 ${alias}_doc.write("<!DOCTYPE html PUBLIC -//W3C//DTD ");
506 ${alias}_doc.write("XHTML 1.0 Transitional//EN ");
507 ${alias}_doc.write(" http://www.w3.org/TR/xhtml1/DTD/");
508 ${alias}_doc.write("xhtml1/DTD/xhtml1-transitional.dtd>\\n\\n");
509 ${alias}_doc.write("<html><head><title>$PACKAGE v$VERSION");
510 ${alias}_doc.write("</title>\\n");
511 ${alias}_doc.write("</head>");
512 ${alias}_doc.write("|;
513 6 $js_css .= $self->_css(1);
514 6 $js_css .= qq|");
515 ${alias}_doc.write("<body style='");
516 ${alias}_doc.write("background-color:white; margin: 2px'></body>\\n");
517 ${alias}_doc.close();
518 ${alias}_cont_div = ${alias}_doc.createElement( 'DIV' );
519 ${alias}_doc.body.appendChild(${alias}_cont_div);
520 ${alias}_last_div = ${alias}_doc.createElement( 'DIV' );
521 ${alias}_last_div.className = "${alias}_desc";
522 ${alias}_last_div.innerHTML = "<span class='${alias}_loginfo'>[ "
523 + ${alias}_count
524 + " - "
525 + ${alias}_date_format( ${alias}_date_start )
526 + " - 0ms ]</span> $PACKAGE " + init_msg;
527 ${alias}_cont_div.appendChild(${alias}_last_div);
528 ${alias}_count++;
529  
530 return 1;
531 }
532  
533 function ${alias}_alert_counts() {
534 var msg = "";
535 var sort_array = new Array;
536 for ( var key in ${alias}_exec_count ) sort_array.push( key );
537 sort_array.sort( ${alias}_exec_key_sort );
538 for( var x = 0; x < sort_array.length; x++ ) {
539 if ( ${alias}_exec_count[sort_array[x]] != 0 ) {
540 msg += sort_array[x] + " = " + ${alias}_exec_count[sort_array[x]] + "\\n";
541 }
542 }
543 alert(msg);
544 }
545  
546 function ${alias}_exec_key_sort( a, b ) {
547 var x = ${alias}_exec_count[b];
548 var y = ${alias}_exec_count[a];
549 return ( ( x < y) ? -1 : ( (x > y) ? 1 : 0 ) );
550 }
551  
552 function ${alias}_date_format ( date ) {
553 var ${alias}_day = date.getDate();
554 var ${alias}_month = date.getMonth() + 1;
555 var ${alias}_hours = date.getHours();
556 var ${alias}_min = date.getMinutes();
557 var ${alias}_sec = date.getSeconds();
558 var ${alias}_ampm = "AM";
559  
560 if ( ${alias}_hours > 11 ) ${alias}_ampm = "PM";
561 if ( ${alias}_hours > 12 ) ${alias}_hours -= 12;
562 if ( ${alias}_hours == 0 ) ${alias}_hours = 12;
563 if ( ${alias}_min < 10 ) ${alias}_min = "0" + ${alias}_min;
564 if ( ${alias}_sec < 10 ) ${alias}_sec = "0" + ${alias}_sec;
565  
566 return ${alias}_month + '/' + ${alias}_day + ' '
567 + ${alias}_hours + ':' + ${alias}_min + ':'
568 + ${alias}_sec + ' ' + ${alias}_ampm;
569 }
570  
571 function ${alias}_toggle_switch() {
572 var obj = ${alias}_gel( '${alias}_switch' )
573 if ( !obj ) return;
574 var switch_button = ${alias}_gel( '${alias}_switch_button' )
575 if ( !switch_button ) return;
576 if ( obj.style.display == '' ) {
577 obj.style.display = 'none';
578 }
579 else {
580 obj.style.display = '';
581 }
582 }
583
584 function ${alias}_toggle_info() {
585 var info = ${alias}_gel( '${alias}_info' )
586 if ( !info ) return;
587 var info_button = ${alias}_gel( '${alias}_info_button' )
588 if ( !info_button ) return;
589 if ( info.style.display == '' ) {
590 info.style.display = 'none';
591 info_button.value = "Show Info";
592 }
593 else {
594 info.style.display = '';
595 info_button.value = "Hide Info";
596 }
597 }
598  
599 function ${alias}_clear() {
600 if ( !confirm("Are you sure?") ) return;
601 ${alias}_count = 1;
602 ${alias}_init( "Console - Cleared" );
603 }
604  
605 function ${alias}_toggle_logging() {
606 var logging_button = ${alias}_gel( '${alias}_logging_button' )
607 if ( !logging_button ) return;
608 if ( ${alias}_logging_on ) {
609 ${alias}("$PACKAGE Console Stopped Logging");
610 logging_button.value = "Resume Logging";
611 ${alias}_logging_on = false;
612 }
613 else {
614 ${alias}_logging_on = true;
615 logging_button.value = "Stop Logging";
616 ${alias}("$PACKAGE Console - Resumed Logging");
617 }
618 }
619  
620 // Parameter switches
621 ${alias}_switches = [];
622 $self->{js_switches};
623
624 // Reload url object
625 var ${alias}_reload = {};
626 ${alias}_reload.params = [];
627
628 // Initialize switches parameters
629 ${alias}_reload.params_init = function() {
630 ${alias}_reload.params = [];
631 }
632
633 // Add to switches parameters
634 ${alias}_reload.params_add = function (id, value) {
635 var query_str = id + "=" + value;
636 ${alias}_reload.params.push(query_str);
637 return;
638 }
639
640 // Assemble url to reload page in after form submission
641 ${alias}_reload.params_final = function() {
642 var base_url = document.URL;
643 // Eliminate params for switches from url, keep others intact
644 var idx = document.URL.indexOf('?');
645 if (idx != -1) {
646 var switches_str = ${alias}_switches.join('~');
647 switches_str = '~'+switches_str+'~';
648 var url_params = document.URL.split('?');
649 base_url = url_params[0];
650 var pairs = url_params[1].split('&');
651 var n = pairs.length
652 for (var i=0; i<n; i++) {
653 name_value = pairs[i].split('=');
654 if (!switches_str.match("~"+name_value[0]+"~")) {
655 ${alias}_reload.params.push(name_value[0]+'='+name_value[1]);
656 }
657 }
658 }
659
660 var switch_params = ${alias}_reload.params.join("&");
661 var reload_url = base_url + "?" + switch_params;
662 location.replace(reload_url);
663 }
664
665 function ${alias}_reload_console(form) {
666 // initialize params array
667 ${alias}_reload.params_init();
668
669 for (var i=0; i < form.length; i++) {
670 var form_el = form.elements[i];
671 var id = form_el.id;
672 // form element type: checkbox, text
673 var element_type = form_el.type;
674 switch (element_type) {
675 case "text": //string values
676 var val = form_el.value;
677 if (val != '') {
678 ${alias}_reload.params_add(id, val);
679 }
680 break;
681 case "checkbox": //bool values
682 // only checked values are added as params
683 if (form_el.checked) {
684 var val = '1';
685 ${alias}_reload.params_add(id, val);
686 }
687 break;
688 case "select-multiple": //string values
689 var sel = [];
690 for (var j=0; j < form_el.options.length; j++) {
691 var val = form_el.options[j].value;
692 if (form_el.options[j].selected == true && val != '') {
693 sel.push(val);
694 }
695 }
696 if (sel.length > 0) {
697 var str = sel.join(',');
698 ${alias}_reload.params_add(id, str);
699 }
700 break;
701 }
702
703 //last form element, go ahead and assemble final url
704 if (i == (form.length - 1)) {
705 ${alias}_reload.params_final();
706 }
707 }
708 }
709
710 function ${alias}_reset_console(form) {
711 for (var i=0; i < form.length; i++) {
712 var form_el = form.elements[i];
713 // form element type: checkbox, text
714 var element_type = form_el.type;
715 switch (element_type) {
716 case "text": //string values
717 form_el.value = "";
718 break;
719 case "checkbox": //bool values
720 if (form_el.checked) {
721 form_el.checked = false;
722 }
723 break;
724 case "select-multiple": //string values
725 for (var j=0; j < form_el.options.length; j++) {
726 form_el.options[j].selected = false;
727 }
728 break;
729 }
730 }
731 }
732
733 function ${alias}_gel( el ) {
734 return document.getElementById ? document.getElementById( el ) : null;
735 }
736  
737 -->
738 </script>\n|;
739 6 $js_css .= $self->_css;
740  
741 6 $$html_ref =~ s/(<head.*?>)/$1$js_css/is;
742  
743 6 return;
744 }
745  
746 6 ▶sub _inject_console {
Subroutine _inject_console: called CC=11 SCAR=24.0
747 6 my ( $self, $html_ref ) = @_;
748  
749 6 my ( $alias, $switches ) = ( $self->{alias}, $self->{switches} );
750  
751 6 my $iframe .= qq|
752 <div class='${alias}_buttons' id='${alias}_buttons'>
753 <span class="${alias}_version"><a href="http://search.cpan.org/~jbisbee/JavaScript-XRay/" target="_blank">$PACKAGE</a> v$VERSION</span>
754 <input type="button" value="Stop Logging" id="${alias}_logging_button"
755 onClick="${alias}_toggle_logging()" class="${alias}_button">
756 <input type="button" value="Show Info" id="${alias}_info_button"
757 onClick="${alias}_toggle_info()" class="${alias}_button">
758 <input type="button" value="Clear" onClick="${alias}_clear()"
759 class="${alias}_button">
760 <input type="button" value="Change Switches" id="${alias}_switch_button"
761 onClick="${alias}_toggle_switch()" class="${alias}_button">|;
762  
763 $iframe .= qq| <input type="button" value="Execution Counts"
764 onClick="${alias}_alert_counts()" class="${alias}_button">|
765 6 ▶ unless $switches->{no_exec_count};
Branch: 1 branch
BranchTrueFalseTotal
unless $switches->{"no_exec_count"} 5 1 6
766  
767 6 $iframe .= qq|</div>
768 <div id="${alias}_info" class="${alias}_buttons" style='display:none'>
769 <center>
770 <table cellpadding=0 cellspacing=0 border=0>|;
771  
772 6 for my $switch ( @SWITCH_KEYS ) {
773 48 ▶ my $value = $switches->{$switch} || '';
Condition100%2/2
$switches->{$switch} || ''
l!l
1137
MC/DC100%1/1
$switches->{$switch} || ''
$switches->{$switch}
Truth table100%2/2
A
A= $switches->{$switch}
Aresult
11
00
774 48 $iframe .= qq|<tr>
775 <td class='${alias}_desc'>${alias}_$switch</td>
776 <td>&nbsp;&nbsp;</td>
777 <td class='${alias}_value'>$value</td>
778 <td class='${alias}_desc'>$SWITCHES{$switch}{type}</td>
779 <td>&nbsp;&nbsp;</td>
780 <td class='${alias}_desc'>$SWITCHES{$switch}{desc}</td>
781 </tr>|;
782 }
783  
784 6 $iframe .= qq|
785 </table>
786 </center>
787 </div>
788  
789
790 <div id="${alias}_switch" class="${alias}_buttons" style='display:none;'>
791 <div class="${alias}_form_border">
792 <table width="100%" cellpadding="1" cellspacing="1" border="0">
793 <tr><td align="right"><div class="${alias}_closebutton" onClick="${alias}_toggle_switch()">X</div></td></tr>
794 <tr><td>Use 'Ctrl' key to choose function names in multiple selection boxes. Click on "Reload Console" for new switches to take effect.</td></tr></table>
795 <br>
796 <table cellpadding=1 cellspacing=0 border=0>
797 <form name="switch_console" id="switch_console" method="get" action="">|;
798
799 6 for my $switch (@SWITCH_KEYS) {
800 48 ▶ next if ( $switch eq 'all' );
Branch: 1 branch
BranchTrueFalseTotal
if $switch eq "all" 6 42 48
801 42 ▶ my $value = $switches->{$switch} || '';
Condition100%2/2
$switches->{$switch} || ''
l!l
1131
MC/DC100%1/1
$switches->{$switch} || ''
$switches->{$switch}
Truth table100%2/2
A
A= $switches->{$switch}
Aresult
11
00
802 42 my $form_element;
803 42 ▶ if ( $SWITCHES{$switch}{type} eq 'bool' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($JavaScript::XRay::SWITCHES{$switch}{'type'} eq 'bool') { } 18 24 42
elsif ($JavaScript::XRay::SWITCHES{$switch}{'type'} =~ /string/) { } 12 12 24
elsif ($JavaScript::XRay::SWITCHES{$switch}{'type'} =~ /function/) { } 12 0 12
804 18 ▶ my $checkbox_value = $value ? ' checked' : '';
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 5 13 18
805 18 $form_element = qq|<input type="checkbox" value=""$checkbox_value id="${alias}_$switch">|;
806 }
807 elsif ( $SWITCHES{$switch}{type} =~ /string/ ) {
808 12 $form_element = qq|<input type="text" size="40" value="$value" id="${alias}_$switch">|;
809 }
810 elsif ( $SWITCHES{$switch}{type} =~ /function/ ) {
811 12 $form_element = qq|<select multiple size="5" id="${alias}_$switch"><option value="">-- None -- $self->{js_function_names}</select>|;
812 }
813 42 $iframe .= qq|<tr class="${alias}_desc" valign="top"><td align="right">${alias}_$switch:&nbsp;</td><td>$form_element</td></tr>|;
814 }
815
816 6 $iframe .= qq|
817 <tr><td colspan="2">&nbsp;</td></tr>
818 <tr align="center"><td colspan="2"><input type="button" value="Reload Console" onClick="${alias}_reload_console(this.form);" class="${alias}_button"> &nbsp; <input type="button" value="Reset" onClick="${alias}_reset_console(this.form);" class="${alias}_button"></tr>
819 </form>
820 </table>
821 </div>
822 </div>
823
824 <div class="${alias}_iframe_padding">
825 <div class="${alias}_iframe_border">
826 <iframe id="${alias}_iframe" name="${alias}_iframe" class="${alias}_iframe"></iframe>
827 </div>
828 </div>|;
829  
830 6 $$html_ref =~ s/(<body.*?>)/$1$iframe/is;
831  
832 6 return;
833 }
834  
835 12 ▶sub _css {
Subroutine _css: called CC=4 SCAR=13.9
836 12 my ($self, $escape_bool) = @_;
837  
838 12 my ($alias) = ($self->{alias});
839  
840 12 my $css = qq|<style>
841 .${alias}_desc, td.${alias}_value, .${alias}_loginfo, ${alias}_buttons {
842 font-family: arial,helvetica;
843 font-size: 12px;
844 background-color: white;
845 }
846 tr.${alias}_desc, td.${alias}_desc, td.${alias}_value, .${alias}_buttons {
847 background-color: #D3D3D3
848 }
849 tr.${alias}_desc, td.${alias}_desc, td.${alias}_value, .${alias}_version {
850 font-size: 12px;
851 }
852 .${alias}_buttons {
853 padding-top: 4px;
854 padding-left: 8px;
855 padding-bottom: 4px;
856 }
857 .${alias}_loginfo, .${alias}_version, .${alias}_buttons {
858 color: #727272
859 }
860 td.${alias}_value {
861 color: #5555FF;
862 padding-left:1em;
863 padding-right:1em;
864 }
865 .${alias}_version {
866 font-family: arial,helvetica;
867 float:right;
868 padding-right: 10px;
869 }
870 .${alias}_iframe_padding {
871 border-width: 0px 7px 7px 7px;
872 border-color: #D3D3D3;
873 border-style: solid;
874 }
875 .${alias}_iframe_border {
876 border-width: 1px;
877 border-style: groove;
878 }
879 .${alias}_iframe {
880 width: 100%;
881 height: $self->{iframe_height}px;
882 border: 0px;
883 }
884 input.${alias}_button {
885 background-color: #D3D3D3;
886 border-width: 1px;
887 border-color: #a9a9a9;
888 }
889 .${alias}_form_border {
890 border: 1px dashed #5e5e5e;
891 padding: 5px;
892 width: 50%;
893 }
894 .${alias}_closebutton {
895 border:1px solid #5e5e5e;
896 width:10px; padding:1px;
897 font:10px arial,sans-serif;
898 text-align:center;
899 color:#5e5e5e;
900 vertical-align:middle;
901 cursor:pointer;
902 cursor:hand;
903 }|;
904  
905 # cat inline css
906 12 ▶ $css .= $self->{css_inline} if $self->{css_inline};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"css_inline"} 2 10 12
907 12 $css .= "\n</style>\n";
908  
909 # include external file
910 $css .= "<link href='$self->{css_external}' rel='stylesheet' "
911 . "type='text/css' />\n"
912 12 ▶ if $self->{css_external};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"css_external"} 2 10 12
913  
914 12 ▶ if ($escape_bool) {
Branch: 1 branch
BranchTrueFalseTotal
if ($escape_bool) 6 6 12
915 6 $css =~ s/\n/\\n/sg;
916 6 $css =~ s/\"/\\\"/g;
917 }
918  
919 12 return $css;
920 }
921  
922 23 ▶sub _warn {
Subroutine _warn: called CC=2 SCAR=7.0
923 23 my ( $self, $msg ) = @_;
924 23 my $alias = $self->{alias};
925 23 ▶ warn "[$alias] $msg\n" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 23 23
926 23 $self->{js_log} .= qq| ${alias}_pre_iframe_queue.push(|
927 . qq|"${PACKAGE}-&gt;filter $msg");\n|;
928
929 23 return;
930 }
931  
932 27 ▶sub _switch_function_options {
Subroutine _switch_function_options: called CC=1 SCAR=0.0
933 27 my ( $self, $msg ) = @_;
934 27 my $alias = $self->{alias};
935 27 $self->{js_function_names} .= qq|<option value="$msg">$msg|;
936 27 return;
937 }
938  
939  
940 3 1;
941  
942 __END__
↑ Summary