blib/lib/JavaScript/XRay.pm

filter: Statementstmt 72.3% 149 / 206 Branchbran 57.8% 59 / 102 Conditioncond 70.1% 40 / 57 MC/DCmcdc 53.5% 15 / 28 Subroutinesub 88.8% 16 / 18 Podpod 100% 4 / 4 totaltotal 68.1% 283 / 415 SCAR 56.3
CC
78
Cov
68%
_get_external_javascript
132.0
inline_methods
72.0
_inline_javascript
31.0
CRAP 278.4
↑ 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  
8 3 our $VERSION = '0.99_5';
9 3 our $PACKAGE = __PACKAGE__;
10 3 our %SWITCHES = (
11 all => {
12 type => 'bool',
13 desc => 'filter all functions (default)',
14 },
15 none => {
16 type => 'bool',
17 desc => 'don\'t filter any functions',
18 },
19 anon => {
20 type => 'bool',
21 desc => 'filter anon functions (noisy)',
22 },
23 no_exec_count => {
24 type => 'bool',
25 desc => 'don\'t count function executions',
26 },
27 only => {
28 type => 'function1,function2,...',
29 desc => 'only filter listed functions (exact)',
30 ref_type => 'ARRAY'
31 },
32 skip => {
33 type => 'function1,function2,...',
34 desc => 'skip listed functions (exact)',
35 ref_type => 'ARRAY'
36 },
37 uncomment => {
38 type => 'string1,string2,...',
39 desc => 'uncomment lines prefixed with string (DEBUG1,DEBUG2)',
40 ref_type => 'ARRAY'
41 },
42 match => {
43 type => 'string',
44 desc => 'only filter functions that match string (/^string/)',
45 ref_type => 'Regexp'
46 },
47 );
48  
49 3 our @SWITCH_KEYS = keys %SWITCHES;
50  
51 sub new {
52 7 ▶ my ( $class, %args ) = @_;
Subroutine new: called CC=4 SCAR=13.9
Pod documented
53  
54 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
55 my $obj = {
56 alias => $alias,
57 abs_uri => $args{abs_uri},
58 iframe_height => $args{iframe_height} || 200,
59 css_inline => $args{css_inline},
60 css_external => $args{css_external},
61 verbose => $args{verbose},
62 7 ▶ js_log => '',
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_init => '',
64 };
65  
66 7 bless $obj, $class;
67  
68 7 $obj->_init_uri( $args{abs_uri} );
69 #$obj->auto_switches if $obj->can('auto_swtiches');
70 7 ▶ $obj->switches( %{$args{switches}} ) if $args{switches};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"switches"} 6 1 7
71  
72 7 return $obj;
73 }
74  
75 sub _init_uri {
76 7 ▶ my ( $self, $abs_uri ) = @_;
Subroutine _init_uri: called CC=3 SCAR=16.5
77 7 ▶ return unless $abs_uri;
Branch: 1 branch
BranchTrueFalseTotal
unless $abs_uri 7 0 7
78 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
79 0 return;
80 }
81  
82 sub switches {
83 6 ▶ my ( $self, %switches ) = @_;
Subroutine switches: called CC=8 SCAR=22.3
Pod documented
84 6 ▶ return $self->{switches} unless keys %switches;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %switches 0 6 6
85
86 # allow 'jsxray_uncomment' or just 'uncomment'
87 6 my $alias = $self->{alias};
88 %switches = map {
89 6 my $new_key = $_;
90 14 $new_key =~ s/^$alias\_//;
91 14 ( $new_key => $switches{$_} );
92 } keys %switches;
93  
94 6 for my $switch ( keys %switches ) {
95 14 ▶ unless ( $SWITCHES{$switch} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($JavaScript::XRay::SWITCHES{$switch}) 0 14 14
96 0 warn "invalid switch: $switch";
97 0 next;
98 }
99 14 my $ref_type = ref $switches{$switch};
100 $self->{switches}{$switch} =
101 $ref_type eq 'ARRAY' && $SWITCHES{$switch}{ref_type} eq 'ARRAY'
102 0 ? join(',', @{ $switches{$switch} })
103 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
104 }
105
106 # init other switches so we don't get warnings
107 6 for my $switch (@SWITCH_KEYS) {
108 $self->{switches}{$switch} = ''
109 48 ▶ unless $self->{switches}{$switch};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"switches"}{$switch} 35 13 48
110 }
111  
112 6 return %{ $self->{switches} };
113 }
114  
115 sub inline_methods {
116 0 ▶ my ( $self, @methods ) = @_;
Subroutine inline_methods: not called CC=8 SCAR=42.8
Pod documented
117  
118 0 ▶ if ( @methods ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@methods) 0 0 0
119 0 my @valid_methods = ();
120 0 for my $method (@methods) {
121 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
122 || $method eq 'HTTP_GET'
123 || ref $method eq 'CODE' )
124 {
125 0 warn 'inline methods may only be local server '
126 . 'directories, code references, or the special string '
127 . "HTTP_GET - invalid method: $method";
128 }
129 else {
130 0 push @valid_methods, $method;
131 }
132 }
133  
134 0 ▶ unless (@valid_methods) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@valid_methods) 0 0 0
135 0 warn 'inline_methods called without valid methods';
136 0 exit;
137 }
138
139 0 $self->{inline_methods} = \@valid_methods;
140 }
141  
142 0 ▶ return wantarray ? @{ $self->{inline_methods} } : $self->{inline_methods};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
143 }
144  
145 sub filter {
146 7 ▶ my ( $self, $html ) = @_;
Subroutine filter: called CC=7 SCAR=19.5
Pod documented
147  
148 7 my ( $alias, $switch ) = ( $self->{alias}, $self->{switches} );
149  
150 $self->_warn( 'Tracing anonymous functions' )
151 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
152  
153 $self->_warn( "Only tracing functions exactly matching: $switch->{only}" )
154 7 ▶ if $switch->{only};
Branch: 1 branch
BranchTrueFalseTotal
if $switch->{"only"} 3 4 7
155  
156 7 ▶ $self->_warn( "Skipping functions: $switch->{skip}" ) if $switch->{skip};
Branch: 1 branch
BranchTrueFalseTotal
if $switch->{"skip"} 1 6 7
157  
158 $self->_warn( "Tracing matching functions: /^$switch->{match}/" )
159 7 ▶ if $switch->{match};
Branch: 1 branch
BranchTrueFalseTotal
if $switch->{"match"} 1 6 7
160  
161 7 $html = $self->_filter($html);
162 6 $html = $self->_inline_javascript($html);
163  
164 6 ▶ $self->_uncomment( \$html ) if $switch->{uncomment};
Branch: 1 branch
BranchTrueFalseTotal
if $switch->{"uncomment"} 2 4 6
165 6 $self->_inject_console( \$html );
166 6 $self->_inject_js_css( \$html );
167  
168 6 return $html;
169 }
170  
171 sub _filter {
172 7 ▶ my ( $self, $work_html ) = @_;
Subroutine _filter: called CC=23 SCAR=31.4
173  
174 7 my ( $alias, $switch ) = ( $self->{alias}, $self->{switches} );
175  
176 7 my $new_html = '';
177 7 ▶ while (
Branch: 1 branch
BranchTrueFalseTotal
if ($work_html =~ / \G (.+?) ( function? \s* (?:\w|_)+? \s*? \( .+? \)? \s* \{ ) /cgimosx) 35 6 41
178 $work_html =~ m/
179 \G
180 (.+?)
181 (
182 function?
183 \s*
184 (?:\w|_)+?
185 \s*?
186 \(
187 .+?
188 \)?
189 \s*
190 \{
191 )
192 /cgimosx
193 )
194 {
195  
196 # build output page from input page
197 35 $new_html .= $1;
198  
199 # find the function name
200 35 my $function .= $2;
201 35 my ($name) = $function =~ m/function\s*(\w+?)?\s*?\(/gx;
202 35 ▶ $name = '' unless $name; # define it to supress warnings
Branch: 1 branch
BranchTrueFalseTotal
unless $name 7 28 35
203  
204 # don't want any recursive JavaScript loops
205 35 ▶ croak( "found function '$name', functions may "
Branch: 1 branch
BranchTrueFalseTotal
if $name eq $alias 1 34 35
206 . "not match alias: '$alias'" )
207 if $name eq $alias;
208  
209 # find the function arguments
210 34 my ($args) = $function =~ m/function\s*$name?\s*?\((.+?)\)/gx;
211 34 ▶ $name = 'ANON' unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 7 27 34
212  
213 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
51415
$name eq "ANON" and not $switch->{"anon"}
!ll&&!rl&&r
2333
MC/DC66%2/3
$switch->{"no_exec_count"} or $name eq "ANON" and not $switch->{"anon"}
$switch->{"no_exec_count"} $name eq "ANON" not $switch->{"anon"}
Truth table75%3/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
214 || ( $name eq 'ANON' && !$switch->{anon} ) )
215 {
216 26 $self->{js_log_init} .= "${alias}_exec_count['$name'] = 0;\n";
217 26 $function .= "${alias}_exec_count['$name']++;";
218 }
219  
220 my %only_function = $switch->{only}
221 23 ? map { $_ => 1 } split( /\,/, $switch->{only} )
222 34 ▶ : ();
Branch: 1 branch
BranchTrueFalseTotal
$switch->{'only'} ? : 14 20 34
223 my %skip_function = $switch->{skip}
224 5 ? map { $_ => 1 } split( /\,/, $switch->{skip} )
225 34 ▶ : ();
Branch: 1 branch
BranchTrueFalseTotal
$switch->{'skip'} ? : 5 29 34
226  
227 34 my $function_filter = '';
228 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
229 0 $function_filter = $switch->{match};
230 }
231 elsif ( $switch->{match} ) {
232 5 my $safe_filter = quotemeta $switch->{match};
233 5 $function_filter = qr/^$safe_filter/;
234 }
235  
236 # skip filter
237 # if none
238 # if anon and not filtering anon functions
239 # if switch 'only' used and function doesn't match
240 # if switch 'skip' used and function matches
241 # if switch 'filter' used and function doesn't match
242 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
243 || ( $name eq 'ANON' && !$switch->{anon} )
244 || ( $switch->{only} && !$only_function{$name} )
245 || ( $switch->{skip} && $skip_function{$name} )
246 || ( $switch->{match} && $name !~ m/$function_filter/x ) )
247 {
248 19 $new_html .= $function;
249 }
250 else {
251 15 $self->_warn("Found function '$name'");
252  
253 # build out function arguments - this is the cool part
254 # you also get to see the value of arguments passed to the
255 # function, _extremely_ handy
256 15 my $filtered_args = '';
257 15 ▶ if ($args) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args) 3 12 15
258 3 my @arg_list = split( /\,/, $args );
259 3 $filtered_args = '\'+' . join( '+\', \'+', @arg_list ) . '+\'';
260 }
261  
262 # insert the log call
263 $new_html
264 15 .= $function . "$alias('$name( $filtered_args )');";
265 }
266 }
267  
268 6 ▶ if ( $work_html =~ /\G(.*)/cgs ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($work_html =~ /\G(.*)/cgs) 6 0 6
269 6 $new_html .= $1;
270 }
271  
272 6 return $new_html;
273 }
274  
275 sub _inline_javascript {
276 6 ▶ my ( $self, $work_html ) = @_;
Subroutine _inline_javascript: called CC=11 SCAR=34.3
277  
278 6 my $new_html = '';
279  
280 # look through the HTML for script blocks
281 6 ▶ while (
Branch: 1 branch
BranchTrueFalseTotal
if ($work_html =~ m[ \G (.*?) ( <script .*? </script> ) ]cgimosx) 6 6 12
282 $work_html =~ m/
283 \G
284 (.*?)
285 (
286 <script
287 .*?
288 <\/script>
289 )
290 /cgimosx
291 )
292 {
293 6 $new_html .= $1;
294 6 my $script_block = $2;
295  
296 # pull out both script attributes and inner script
297 6 ▶ while (
Branch: 1 branch
BranchTrueFalseTotal
if ($script_block =~ m[ <script (.*?) \s*?> (.*?) </script> ]cgimosx) 6 6 12
298 $script_block =~ /
299 <script
300 (.*?)
301 \s*?>
302 (.*?)
303 <\/script>
304 /cgimosx
305 )
306 {
307 6 my ( $script_attrs, $inner_script ) = ( $1, $2 );
308 6 $script_attrs =~ s/\s*\=\s*/\=/g; # clean up white space
309  
310 # parse out name value pairs so we can rebuild the script
311 # element properly. (special case 'defer' is a boolean
312 # and has no value
313 6 my %attrs = ();
314 6 ▶ while (
Branch: 1 branch
BranchTrueFalseTotal
if ($script_attrs =~ / \G \s* (?: (defer) | (.+?) \s* \= \s* (?: [\"\'](.+?)[\"\'] | (\w+) ) ) /cgimosx) 0 6 6
315 $script_attrs =~ /
316 \G
317 \s*
318 (?: (defer) |
319 (.+?)
320 \s*
321 \=
322 \s*
323 (?: [\"\'](.+?)[\"\'] | (\w+) )
324 )
325 /cgimosx
326 )
327 {
328  
329 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
330 0 ▶ if ($defer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($defer) { } 0 0 0
331 0 $attrs{$defer} = 1;
332 }
333 else {
334 0 $attrs{$name} = $value;
335 }
336 }
337  
338 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
339 my @attrs = map {
340 0 ▶ $_ eq 'defer' ? $_ : "$_=\"$attrs{$_}\"";
Branch: 1 branch
BranchTrueFalseTotal
$_ eq 'defer' ? : 0 0 0
341 0 } grep { $_ ne 'src' } keys %attrs;
342  
343 0 my $js = $self->_get_external_javascript($attrs{src});
344
345 0 ▶ if ($js) {
Branch: 1 branch
BranchTrueFalseTotal
if ($js) { } 0 0 0
346 0 my $inline_javascript = '<script '
347 . join( ' ', @attrs ) . "><!--\n"
348 . $js
349 . "\n--></script>";
350  
351 0 $new_html .= "<!-- inline $attrs{src} -->\n";
352 0 $new_html .= $inline_javascript;
353 }
354 else {
355 0 warn 'failed to inline (or referenced URI is empty): '
356 . $script_block;
357 0 $new_html .= $script_block;
358 }
359 }
360 else {
361 6 $new_html .= $script_block;
362 }
363 }
364 }
365  
366 6 ▶ if ( $work_html =~ /\G(.*)/cgs ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($work_html =~ /\G(.*)/cgs) 6 0 6
367 6 $new_html .= $1;
368 }
369  
370 6 return $new_html;
371 }
372  
373 sub _get_external_javascript {
374 0 ▶ my ( $self, $src ) = @_;
Subroutine _get_external_javascript: not called CC=11 SCAR=48.8
375 0 my $js = '';
376  
377 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
378 0 warn 'unable to inline/filter external javascript files with an'
379 . 'absolute request uri: abs_uri not defined';
380 0 return $js;
381 }
382  
383 # FIXME not sure how the none HTTP URI works to get the complete file
384 # path in the case (but it does somehow) Need to unit test this
385 # code and figur out what's happening.
386 my $abs_js_uri =
387 $src =~ /^http/
388 ? URI->new( $src, $self->{abs_uri} )
389 0 ▶ : URI->new($src);
Branch: 1 branch
BranchTrueFalseTotal
$src =~ /^http/ ? : 0 0 0
390  
391 0 for my $method ( @{$self->{inline_methods}} ) {
392 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
393 0 $js = get( $abs_js_uri );
394 }
395 elsif ( -d $method ) {
396 0 my $possible_js_file = $method . $abs_js_uri->path;
397 0 ▶ if ( open( my $fh, 'r', $possible_js_file ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, 'r', $possible_js_file) { } 0 0 0
398 0 $js = do { local $/; $/ = undef; <$fh> };
399 0 close $fh;
400 }
401 else {
402 0 warn "failed to open: $possible_js_file: $!";
403 }
404 }
405 elsif ( ref $method eq 'CODE' ) {
406 0 $js = &$method( $src, $self->{abs_uri} );
407 }
408 0 ▶ last if $js;
Branch: 1 branch
BranchTrueFalseTotal
if $js 0 0 0
409 }
410  
411 0 ▶ if ($js) {
Branch: 1 branch
BranchTrueFalseTotal
if ($js) 0 0 0
412 0 $self->_warn("Inlining and Filtering $src");
413 0 $js = $self->_filter($js);
414 }
415  
416 0 return $js;
417 }
418  
419 sub _uncomment {
420 2 ▶ my ( $self, $html_ref ) = @_;
Subroutine _uncomment: called CC=4 SCAR=13.9
421 2 my $switch = $self->{switches};
422  
423 # uncomment nessesary tags
424 my @uncomment_strings
425 2 = map { quotemeta($_) } split( /\,/, $switch->{uncomment} );
426 2 for my $uncomment (@uncomment_strings) {
427 3 my $uncomment_count = $$html_ref =~ s/\/\/$uncomment//gs;
428 3 ▶ if ($uncomment_count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uncomment_count) 1 2 3
429 1 ▶ my $label = $uncomment_count > 1 ? 'instances' : 'instance';
Branch: 1 branch
BranchTrueFalseTotal
$uncomment_count > 1 ? : 0 1 1
430 1 $self->_warn( "$PACKAGE->filter uncommented $uncomment: "
431 . "Found $uncomment_count $label" );
432 }
433 }
434  
435 2 return;
436 }
437  
438 sub _inject_js_css {
439 6 ▶ my ( $self, $html_ref ) = @_;
Subroutine _inject_js_css: called CC=1 SCAR=0.0
440 6 my ( $alias, $switches ) = ( $self->{alias}, $self->{switches} );
441  
442 6 my $js_css = qq|<script><!--
443 var ${alias}_logging_on = true;
444 var ${alias}_doc = null;
445 var ${alias}_cont_div = null;
446 var ${alias}_last_div = null;
447 var ${alias}_count = 1;
448 var ${alias}_exec_count = [];
449 var ${alias}_date_start;
450 var ${alias}_time_start;
451 var ${alias}_pre_iframe_queue = [];
452 $self->{js_log}
453  
454 function ${alias}( msg ) {
455 if ( !${alias}_logging_on ) return;
456 if ( ${alias}_doc == null) {
457 if ( ! ${alias}_init( "Initialized" ) ) {
458 ${alias}_pre_iframe_queue.push(msg);
459 return;
460 }
461 else {
462 for( var x = 0; x < ${alias}_pre_iframe_queue.length; x++ ) {
463 ${alias}_log( ${alias}_pre_iframe_queue[x] );
464 }
465 ${alias}_pre_iframe_queue = [];
466 }
467 }
468 ${alias}_log( msg );
469 }
470  
471 function ${alias}_log ( msg ) {
472 // timing data
473 var ${alias}_date_now = new Date();
474 var ${alias}_time_since = ${alias}_date_now.getTime();
475 var ${alias}_elapsed_time =
476 ( ${alias}_time_since - ${alias}_time_start );
477 var ${alias}_time = ${alias}_date_format( ${alias}_date_now );
478 var ${alias}_div = ${alias}_doc.createElement( 'DIV' );
479  
480 ${alias}_div.className = "${alias}_desc";
481 ${alias}_doc.body.appendChild( ${alias}_div );
482 ${alias}_cont_div.insertBefore(${alias}_div, ${alias}_last_div);
483 ${alias}_div.innerHTML = "<span class='${alias}_loginfo'>[ "
484 + ${alias}_count + ' - ' + ${alias}_time + ' - '
485 + ${alias}_elapsed_time + "ms ]</span> " + msg;
486 ${alias}_count++;
487 ${alias}_last_div = ${alias}_div;
488 }
489  
490 function ${alias}_init(init_msg) {
491 $self->{js_log_init}
492 ${alias}_date_start = new Date();
493 ${alias}_time_start = ${alias}_date_start.getTime();
494 if (!window.frames.${alias}_iframe) return;
495 ${alias}_doc = window.frames.${alias}_iframe.document;
496 ${alias}_doc.open();
497 ${alias}_doc.write("<!DOCTYPE html PUBLIC -//W3C//DTD ");
498 ${alias}_doc.write("XHTML 1.0 Transitional//EN ");
499 ${alias}_doc.write(" http://www.w3.org/TR/xhtml1/DTD/");
500 ${alias}_doc.write("xhtml1/DTD/xhtml1-transitional.dtd>\\n\\n");
501 ${alias}_doc.write("<html><head><title>$PACKAGE v$VERSION");
502 ${alias}_doc.write("</title>\\n");
503 ${alias}_doc.write("</head>");
504 ${alias}_doc.write("|;
505 6 $js_css .= $self->_css(1);
506 6 $js_css .= qq|");
507 ${alias}_doc.write("<body style='");
508 ${alias}_doc.write("background-color:white; margin: 2px'></body>\\n");
509 ${alias}_doc.close();
510 ${alias}_cont_div = ${alias}_doc.createElement( 'DIV' );
511 ${alias}_doc.body.appendChild(${alias}_cont_div);
512 ${alias}_last_div = ${alias}_doc.createElement( 'DIV' );
513 ${alias}_last_div.className = "${alias}_desc";
514 ${alias}_last_div.innerHTML = "<span class='${alias}_loginfo'>[ "
515 + ${alias}_count
516 + " - "
517 + ${alias}_date_format( ${alias}_date_start )
518 + " - 0ms ]</span> $PACKAGE " + init_msg;
519 ${alias}_cont_div.appendChild(${alias}_last_div);
520 ${alias}_count++;
521  
522 return 1;
523 }
524  
525 function ${alias}_alert_counts() {
526 var msg = "";
527 var sort_array = new Array;
528 for ( var key in ${alias}_exec_count ) sort_array.push( key );
529 sort_array.sort( ${alias}_exec_key_sort );
530 for( var x = 0; x < sort_array.length; x++ ) {
531 if ( ${alias}_exec_count[sort_array[x]] != 0 ) {
532 msg += sort_array[x] + " = " + ${alias}_exec_count[sort_array[x]] + "\\n";
533 }
534 }
535 alert(msg);
536 }
537  
538 function ${alias}_exec_key_sort( a, b ) {
539 var x = ${alias}_exec_count[b];
540 var y = ${alias}_exec_count[a];
541 return ( ( x < y) ? -1 : ( (x > y) ? 1 : 0 ) );
542 }
543  
544 function ${alias}_date_format ( date ) {
545 var ${alias}_day = date.getDate();
546 var ${alias}_month = date.getMonth() + 1;
547 var ${alias}_hours = date.getHours();
548 var ${alias}_min = date.getMinutes();
549 var ${alias}_sec = date.getSeconds();
550 var ${alias}_ampm = "AM";
551  
552 if ( ${alias}_hours > 11 ) ${alias}_ampm = "PM";
553 if ( ${alias}_hours > 12 ) ${alias}_hours -= 12;
554 if ( ${alias}_hours == 0 ) ${alias}_hours = 12;
555 if ( ${alias}_min < 10 ) ${alias}_min = "0" + ${alias}_min;
556 if ( ${alias}_sec < 10 ) ${alias}_sec = "0" + ${alias}_sec;
557  
558 return ${alias}_month + '/' + ${alias}_day + ' '
559 + ${alias}_hours + ':' + ${alias}_min + ':'
560 + ${alias}_sec + ' ' + ${alias}_ampm;
561 }
562  
563 function ${alias}_toggle_info() {
564 var info = ${alias}_gel( '${alias}_info' )
565 if ( !info ) return;
566 var info_button = ${alias}_gel( '${alias}_info_button' )
567 if ( !info_button ) return;
568 if ( info.style.display == '' ) {
569 info.style.display = 'none';
570 info_button.value = "Show Info";
571 }
572 else {
573 info.style.display = '';
574 info_button.value = "Hide Info";
575 }
576 }
577  
578 function ${alias}_clear() {
579 if ( !confirm("Are you sure?") ) return;
580 ${alias}_count = 1;
581 ${alias}_init( "Console - Cleared" );
582 }
583  
584 function ${alias}_toggle_logging() {
585 var logging_button = ${alias}_gel( '${alias}_logging_button' )
586 if ( !logging_button ) return;
587 if ( ${alias}_logging_on ) {
588 ${alias}("$PACKAGE Console Stopped Logging");
589 logging_button.value = "Resume Logging";
590 ${alias}_logging_on = false;
591 }
592 else {
593 ${alias}_logging_on = true;
594 logging_button.value = "Stop Logging";
595 ${alias}("$PACKAGE Console - Resumed Logging");
596 }
597 }
598  
599 function ${alias}_gel( el ) {
600 return document.getElementById ? document.getElementById( el ) : null;
601 }
602  
603 -->
604 </script>\n|;
605 6 $js_css .= $self->_css;
606  
607 6 $$html_ref =~ s/(<head.*?>)/$1$js_css/is;
608  
609 6 return;
610 }
611  
612 sub _inject_console {
613 6 ▶ my ( $self, $html_ref ) = @_;
Subroutine _inject_console: called CC=4 SCAR=13.9
614  
615 6 my ( $alias, $switches ) = ( $self->{alias}, $self->{switches} );
616  
617 6 my $iframe .= qq|
618 <div class='${alias}_buttons' id='${alias}_buttons'>
619 <span class="${alias}_version"><a href="http://search.cpan.org/~jbisbee/JavaScript-XRay/" target="_blank">$PACKAGE</a> v$VERSION</span>
620 <input type="button" value="Stop Logging" id="${alias}_logging_button"
621 onClick="${alias}_toggle_logging()" class="${alias}_button">
622 <input type="button" value="Show Info" id="${alias}_info_button"
623 onClick="${alias}_toggle_info()" class="${alias}_button">
624 <input type="button" value="Clear" onClick="${alias}_clear()"
625 class="${alias}_button">|;
626  
627 $iframe .= qq| <input type="button" value="Execution Counts"
628 onClick="${alias}_alert_counts()" class="${alias}_button">|
629 6 ▶ unless $switches->{no_exec_count};
Branch: 1 branch
BranchTrueFalseTotal
unless $switches->{"no_exec_count"} 5 1 6
630  
631 6 $iframe .= qq|</div>
632 <div id="${alias}_info" class="${alias}_buttons" style='display:none'>
633 <center>
634 <table cellpadding=0 cellspacing=0 border=0>|;
635  
636 6 for my $switch ( @SWITCH_KEYS ) {
637 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
638 48 $iframe .= qq|<tr>
639 <td class='${alias}_desc'>${alias}_$switch</td>
640 <td>&nbsp;&nbsp;</td>
641 <td class='${alias}_value'>$value</td>
642 <td class='${alias}_desc'>$SWITCHES{$switch}{type}</td>
643 <td>&nbsp;&nbsp;</td>
644 <td class='${alias}_desc'>$SWITCHES{$switch}{desc}</td>
645 </tr>|;
646 }
647  
648 6 $iframe .= qq|
649 </table>
650 </center>
651 </div>
652 <div class="${alias}_iframe_padding">
653 <div class="${alias}_iframe_border">
654 <iframe id="${alias}_iframe" name="${alias}_iframe" class="${alias}_iframe"></iframe>
655 </div>
656 </div>|;
657  
658 6 $$html_ref =~ s/(<body.*?>)/$1$iframe/is;
659  
660 6 return;
661 }
662  
663 sub _css {
664 12 ▶ my ($self, $escape_bool) = @_;
Subroutine _css: called CC=4 SCAR=13.9
665  
666 12 my ($alias) = ($self->{alias});
667  
668 12 my $css = qq|<style>
669 .${alias}_desc, td.${alias}_value, .${alias}_loginfo, ${alias}_buttons {
670 font-family: arial,helvetica;
671 font-size: 12px;
672 background-color: white;
673 }
674 td.${alias}_desc, td.${alias}_value, .${alias}_buttons {
675 background-color: #D3D3D3
676 }
677 td.${alias}_desc, td.${alias}_value, .${alias}_version {
678 font-size: 12px;
679 }
680 .${alias}_buttons {
681 padding-top: 4px;
682 padding-left: 8px;
683 padding-bottom: 4px;
684 }
685 .${alias}_loginfo, .${alias}_version, .${alias}_buttons {
686 color: #727272
687 }
688 td.${alias}_value {
689 color: #5555FF;
690 padding-left:1em;
691 padding-right:1em;
692 }
693 .${alias}_version {
694 font-family: arial,helvetica;
695 float:right;
696 padding-right: 10px;
697 }
698 .${alias}_iframe_padding {
699 border-width: 0px 7px 7px 7px;
700 border-color: #D3D3D3;
701 border-style: solid;
702 }
703 .${alias}_iframe_border {
704 border-width: 1px;
705 border-style: groove;
706 }
707 .${alias}_iframe {
708 width: 100%;
709 height: $self->{iframe_height}px;
710 border: 0px;
711 }
712 input.${alias}_button {
713 background-color: #D3D3D3;
714 border-width: 1px;
715 border-color: #a9a9a9;
716 }|;
717  
718 # cat inline css
719 12 ▶ $css .= $self->{css_inline} if $self->{css_inline};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"css_inline"} 2 10 12
720 12 $css .= "\n</style>\n";
721  
722 # include external file
723 $css .= "<link href='$self->{css_external}' rel='stylesheet' "
724 . "type='text/css' />\n"
725 12 ▶ if $self->{css_external};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"css_external"} 2 10 12
726  
727 12 ▶ if ($escape_bool) {
Branch: 1 branch
BranchTrueFalseTotal
if ($escape_bool) 6 6 12
728 6 $css =~ s/\n/\\n/sg;
729 6 $css =~ s/\"/\\\"/g;
730 }
731  
732 12 return $css;
733 }
734  
735 sub _warn {
736 23 ▶ my ( $self, $msg ) = @_;
Subroutine _warn: called CC=2 SCAR=7.0
737 23 my $alias = $self->{alias};
738 23 ▶ warn "[$alias] $msg\n" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 23 23
739 23 $self->{js_log} .= qq| ${alias}_pre_iframe_queue.push(|
740 . qq|"${PACKAGE}-&gt;filter $msg");\n|;
741 23 return;
742 }
743  
744 3 1;
745  
746 __END__
↑ Summary