JavaScript.pm

filter: Statementstmt 92.3% 109 / 118 Branchbran 72.9% 35 / 48 Conditioncond 60.0% 9 / 15 MC/DCmcdc 40.0% 4 / 10 Subroutinesub 90.9% 10 / 11 Podpod 100% 2 / 2 totaltotal 82.8% 169 / 204 CC 34 SCAR 24.1
CC
34
Cov
85%
import
16.1
__jsdump
14.5
hjsdump
6.0
CRAP 11.1
↑ Summary example.pl »
1 package Data::JavaScript; ## no critic (PodSpelling)
2  
3 9 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use Scalar::Util 'reftype';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 9 our $VERSION = '1.16';
8  
9 # Exporter
10 9 Readonly our @EXPORT => qw(jsdump hjsdump);
11 9 Readonly our @EXPORT_OK => '__quotemeta';
12 9 Readonly our %EXPORT_TAGS => (
13 all => [ @EXPORT, @EXPORT_OK ],
14 compat => [@EXPORT],
15 );
16  
17 # Magic numbers
18 9 Readonly my $MIN_ENCODE_REQUIRE_BREAKPOINT => 5.007;
19 9 Readonly my $JSCOMPAT_DEFAULT_VERSION => 1.3;
20 9 Readonly my $JSCOMPAT_UNDEFINED_MISSING => 1.2;
21  
22 # This is a context variable which holds on to configs.
23 9 my %opt = ( JS => $JSCOMPAT_DEFAULT_VERSION ); # TODO: This is super out-dated.
24  
25 9 ▶if ( $] >= $MIN_ENCODE_REQUIRE_BREAKPOINT ) { require Encode; }
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= $MIN_ENCODE_REQUIRE_BREAKPOINT) 9 0 9
26  
27 9 ▶sub import {
Subroutine import: called CC=15 SCAR=27.8
28 9 my ( $package, @args ) = @_;
29  
30 # Let's get the stuff we're going to import
31 9 my @explicit_imports = ();
32 9 my @import = ();
33 9 my %allowable = map { $_ => 1 } ( @EXPORT, @EXPORT_OK );
34  
35 # This is the madness for the JS version
36 9 for my $arg (@args) {
37 8 ▶ if ( ref $arg eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $arg eq 'HASH') { } 2 6 8
elsif (not ref $arg) { } 6 0 6
38 2 ▶ if ( exists $arg->{JS} ) { $opt{JS} = $arg->{JS}; }
Branch: 1 branch
BranchTrueFalseTotal
if (exists $arg->{"JS"}) 1 1 2
39 2 ▶ if ( exists $arg->{UNDEF} ) { $opt{UNDEF} = $arg->{UNDEF}; }
Branch: 1 branch
BranchTrueFalseTotal
if (exists $arg->{"UNDEF"}) 1 1 2
40 }
41 elsif ( not ref $arg ) {
42 6 push @explicit_imports, $arg;
43 }
44 }
45 9 ▶ $opt{UNDEF} ||= $opt{JS} > $JSCOMPAT_UNDEFINED_MISSING ? 'undefined' : q('');
Branch: 1 branch
BranchTrueFalseTotal
$opt{'JS'} > $JSCOMPAT_UNDEFINED_MISSING ? : 8 1 9
Condition33%1/3
$opt{"UNDEF"} ||= $opt{"JS"} > $JSCOMPAT_UNDEFINED_MISSING ? "undefined" : "''"
l!l&&r!l&&!r
090
MC/DC0%0/2
$opt{"UNDEF"} ||= $opt{"JS"} > $JSCOMPAT_UNDEFINED_MISSING ? "undefined" : "''"
$opt{"UNDEF"} $opt{"JS"} > $JSCOMPAT_UNDEFINED_MISSING ? "undefined" : "''"
Truth table33%1/3
A ||= B
A= $opt{"UNDEF"}B= $opt{"JS"} > $JSCOMPAT_UNDEFINED_MISSING ? "undefined" : "''"
ABresult
1X1
011
000
46  
47 #use (); #imports nothing, as package is not supplied
48 9 ▶ if ( defined $package ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $package) 9 0 9
49  
50 9 ▶ if ( scalar @explicit_imports ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @explicit_imports) { } 6 3 9
51  
52 # Run through the explicitly exported symbols
53 6 for my $explicit_import (@explicit_imports) {
54  
55 # Looks like a tag
56 6 ▶ if ( substr( $explicit_import, 0, 1 ) eq q/:/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if (substr($explicit_import, 0, 1) eq ':') { } 6 0 6
elsif (exists $allowable{$explicit_import}) { } 0 0 0
57 6 my $tag = substr $explicit_import, 1;
58  
59 # Only do things for the actually exported tags.
60 0 ▶ if ( not exists $EXPORT_TAGS{$tag} ) { next; }
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $Data::JavaScript::EXPORT_TAGS{$tag}) 0 6 6
61 6 push @import, @{ $EXPORT_TAGS{$tag} };
62 }
63  
64 # Not a tag
65 elsif ( exists $allowable{$explicit_import} ) {
66  
67 #only user-specfied subset of @EXPORT, @EXPORT_OK
68 0 push @import, $explicit_import;
69 }
70 }
71 }
72 else {
73 3 @import = @EXPORT;
74 }
75  
76 9 my $caller = caller;
77 9 ▶ no strict 'refs'; ## no critic (ProhibitNoStrict)
Subroutine BEGIN: called CC=1 SCAR=0.0
78 9 for my $func (@import) {
79 23 *{"$caller\::$func"} = \&{$func};
80 }
81 9 ▶ use strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
82 }
83  
84 9 return;
85 }
86  
87 0 ▶sub hjsdump {
Subroutine hjsdump: not called CC=2 SCAR=17.9
Pod documented
88 0 my @input = @_;
89  
90 0 my @res = (
91 qq(<script type="text/javascript" language="JavaScript$opt{JS}" />),
92 '<!--', jsdump(@input), '// -->', '</script>',
93 );
94 0 ▶ return wantarray ? @res : join qq/\n/, @res, q//;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
95 }
96  
97 21 ▶sub jsdump {
Subroutine jsdump: called CC=3 SCAR=11.0
Pod documented
98 21 my ( $sym, @input ) = @_;
99  
100 21 ▶ return "var $sym;\n" if ( not scalar @input );
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @input 0 21 21
101 21 my ( $elem, $undef ) = @input;
102 21 my %dict = ();
103 21 my @res = __jsdump( $sym, $elem, \%dict, $undef );
104 21 $res[0] = qq/var $res[0]/;
105 21 ▶ return wantarray ? @res : join qq/\n/, @res, q//;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 18 3 21
106 }
107  
108 31 ▶sub __quotemeta {
Subroutine __quotemeta: called CC=4 SCAR=13.9
109 31 my ($input) = @_;
110  
111 ## ENCODER!
112 31 ▶ if ( $] < $MIN_ENCODE_REQUIRE_BREAKPOINT ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] < $MIN_ENCODE_REQUIRE_BREAKPOINT) { } 0 31 31
113 0 $input =~ s{
114 ([^ \x21-\x5B\x5D-\x7E]+)
115 }{
116 0 sprintf(join('', '\x%02X' x length$1), unpack'C*',$1)
117 }gexsm;
118 }
119 else {
120 31 ▶ if ( $opt{JS} >= $JSCOMPAT_DEFAULT_VERSION && Encode::is_utf8($input) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"JS"} >= $JSCOMPAT_DEFAULT_VERSION and &Encode::is_utf8($input)) 4 27 31
Condition100%3/3
$opt{"JS"} >= $JSCOMPAT_DEFAULT_VERSION and &Encode::is_utf8($input)
!ll&&!rl&&r
3244
MC/DC100%2/2
$opt{"JS"} >= $JSCOMPAT_DEFAULT_VERSION and &Encode::is_utf8($input)
$opt{"JS"} >= $JSCOMPAT_DEFAULT_VERSION &Encode::is_utf8($input)
Truth table100%3/3
A and B
A= $opt{"JS"} >= $JSCOMPAT_DEFAULT_VERSIONB= &Encode::is_utf8($input)
ABresult
0X0
100
111
121 4 $input =~ s{
122 ([\x{0080}-\x{fffd}]+)
123 }{
124 7 sprintf '\u%0*v4X', '\u', $1
125 }gexms;
126 }
127  
128 {
129 9 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
130 31 $input =~ s{
131 ((?:[^ \x21-\x7E]|(?:\\(?!u)))+)
132 }{
133 12 sprintf '\x%0*v2X', '\x', $1
134 }gexms;
135 }
136  
137 }
138  
139 #This is kind of ugly/inconsistent output for munged UTF-8
140 #tr won't work because we need the escaped \ for JS output
141 31 $input =~ s/\\x09/\\t/gxms;
142 31 $input =~ s/\\x0A/\\n/gxms;
143 31 $input =~ s/\\x0D/\\r/gxms;
144 31 $input =~ s/"/\\"/gxms;
145 31 $input =~ s/\\x5C/\\\\/gxms;
146  
147 #Escape </script> for stupid browsers that stop parsing
148 31 $input =~ s{</script>}{\\x3C\\x2Fscript\\x3E}gxms;
149  
150 31 return $input;
151 }
152  
153 48 ▶sub __jsdump {
Subroutine __jsdump: called CC=14 SCAR=26.8
154 48 my ( $sym, $elem, $dict, $undef ) = @_;
155 48 my $ref = ref $elem;
156  
157 48 ▶ if ( not $ref ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ref) 38 10 48
158 38 ▶ if ( not defined $elem ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $elem) 4 34 38
159 4 ▶ return qq($sym = @{[defined($undef) ? $undef : $opt{UNDEF}]};);
Branch: 1 branch
BranchTrueFalseTotal
defined $undef ? : 1 3 4
160 }
161  
162 #Translated from $Regexp::Common::RE{num}{real}
163 34 ▶ if ( $elem ne '.' &&
Branch: 1 branch
BranchTrueFalseTotal
if ($elem ne "." and $elem =~ /^[+-]?(?:(?=\d|[.])\d*(?:[.]\d{0,})?)(?:[eE][+-]?\d+)?$/msux) 13 21 34
Condition66%2/3
$elem ne "." and $elem =~ /^[+-]?(?:(?=\d|[.])\d*(?:[.]\d{0,})?)(?:[eE][+-]?\d+)?$/msux
!ll&&!rl&&r
02113
MC/DC50%1/2
$elem ne "." and $elem =~ /^[+-]?(?:(?=\d|[.])\d*(?:[.]\d{0,})?)(?:[eE][+-]?\d+)?$/msux
$elem ne "." $elem =~ /^[+-]?(?:(?=\d|[.])\d*(?:[.]\d{0,})?)(?:[eE][+-]?\d+)?$/msux
Truth table66%2/3
A and B
A= $elem ne "."B= $elem =~ /^[+-]?(?:(?=\d|[.])\d*(?:[.]\d{0,})?)(?:[eE][+-]?\d+)?$/msux
ABresult
0X0
100
111
164 $elem =~ /^[+-]?(?:(?=\d|[.])\d*(?:[.]\d{0,})?)(?:[eE][+-]?\d+)?$/xsm ) {
165  
166 13 ▶ if( $elem =~ /^0\d+$/xsm ){
Branch: 1 branch
BranchTrueFalseTotal
if ($elem =~ /^0\d+$/msux) 1 12 13
167 1 return qq($sym = "$elem";) }
168 12 return qq($sym = $elem;);
169 }
170  
171 #Fall-back to quoted string
172 21 return qq($sym = ") . __quotemeta($elem) . '";';
173 }
174  
175 #Circular references
176 10 ▶ if( $dict->{$elem} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($dict->{$elem}) 0 10 10
177 0 return qq($sym = $dict->{$elem};) }
178 10 $dict->{$elem} = $sym;
179  
180 #isa over ref in case we're given objects
181 10 ▶ if ( $ref eq 'ARRAY' || reftype $elem eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq 'ARRAY' or builtin::reftype($elem) eq 'ARRAY') { } 6 4 10
elsif ($ref eq 'HASH' or builtin::reftype($elem) eq 'HASH') { } 4 0 4
Condition66%2/3
$ref eq "ARRAY" or builtin::reftype($elem) eq "ARRAY"
l!l&&r!l&&!r
604
MC/DC50%1/2
$ref eq "ARRAY" or builtin::reftype($elem) eq "ARRAY"
$ref eq "ARRAY" builtin::reftype($elem) eq "ARRAY"
Truth table66%2/3
A or B
A= $ref eq "ARRAY"B= builtin::reftype($elem) eq "ARRAY"
ABresult
1X1
011
000
182 6 my @list = ("$sym = new Array;");
183 6 my $n = 0;
184 6 foreach my $one ( @{$elem} ) {
185 18 my $newsym = "$sym\[$n]";
186 18 push @list, __jsdump( $newsym, $one, $dict, $undef );
187 18 $n++;
188 }
189 6 return @list;
190 }
191 ▶ elsif ( $ref eq 'HASH' || reftype $elem eq 'HASH' ) {
Condition33%1/3
$ref eq "HASH" or builtin::reftype($elem) eq "HASH"
l!l&&r!l&&!r
400
MC/DC0%0/2
$ref eq "HASH" or builtin::reftype($elem) eq "HASH"
$ref eq "HASH" builtin::reftype($elem) eq "HASH"
Truth table33%1/3
A or B
A= $ref eq "HASH"B= builtin::reftype($elem) eq "HASH"
ABresult
1X1
011
000
192 4 my @list = ("$sym = new Object;");
193 4 foreach my $k ( sort keys %{$elem} ) {
194 9 my $old_k;
195 9 $k = __quotemeta( $old_k = $k );
196 9 my $newsym = qq($sym\["$k"]);
197 9 push @list, __jsdump( $newsym, $elem->{$old_k}, $dict, $undef );
198 }
199 4 return @list;
200 }
201 else {
202 0 return "//Unknown reference: $sym=$ref";
203 }
204 }
205  
206 9 1;
207 ## no critic (RequirePodSections)
208 __END__
↑ Summary example.pl »