Tags/Utils.pm

filter: Statementstmt 95.9% 47 / 49 Branchbran 83.3% 10 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod 100% 3 / 3 totaltotal 94.5% 69 / 73 CC 9 SCAR 12.2
CC
9
Cov
93%
encode_attr_entities
5.0
encode_char_entities
5.0
BEGIN
1.0
CRAP 3.4
« Tags/Output/Raw.pm ↑ Summary Tags/Utils/Preserve.pm »
1 package Tags::Utils;
2  
3 27 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 27 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 27 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 27 ▶use Error::Pure qw(err);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 27 ▶use HTML::Entities;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 27 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # Constants.
12 27 Readonly::Array our @EXPORT_OK => qw(encode_newline encode_attr_entities
13 encode_char_entities);
14 27 Readonly::Scalar my $ATTR_CHARS => q{<&"};
15 27 Readonly::Scalar my $CHAR_CHARS => q{<&\240};
16 27 Readonly::Scalar my $EMPTY_STR => q{};
17  
18 27 our $VERSION = 0.16;
19  
20 # Encode newline in data to '\n' in output.
21 1 ▶sub encode_newline {
Subroutine encode_newline: called CC=1 SCAR=0.0
Pod documented
22 1 my $string = shift;
23  
24 1 $string =~ s/\n/\\n/gms;
25  
26 1 return $string;
27 }
28  
29 # Encode '<&"' attribute entities.
30 92 ▶sub encode_attr_entities {
Subroutine encode_attr_entities: called CC=5 SCAR=16.2
Pod documented
31 92 my $data_r = shift;
32  
33 92 ▶ if (ref $data_r eq 'SCALAR') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $data_r eq 'SCALAR') { } 62 30 92
elsif (ref $data_r eq 'ARRAY') { } 29 1 30
elsif (ref $data_r eq $EMPTY_STR) { } 1 0 1
34 62 ${$data_r} = encode_entities(decode_entities(${$data_r}),
35 $ATTR_CHARS);
36 } elsif (ref $data_r eq 'ARRAY') {
37 29 foreach my $one_data (@{$data_r}) {
38 61 encode_attr_entities(\$one_data);
39 }
40 } elsif (ref $data_r eq $EMPTY_STR) {
41 1 return encode_entities(decode_entities($data_r), $ATTR_CHARS);
42 } else {
43 0 err 'Reference \''.(ref $data_r).'\' doesn\'t supported.';
44 }
45 91 return;
46 }
47  
48 # Encode '<&NBSP' char entities.
49 77 ▶sub encode_char_entities {
Subroutine encode_char_entities: called CC=5 SCAR=16.2
Pod documented
50 77 my $data_r = shift;
51  
52 77 ▶ if (ref $data_r eq 'SCALAR') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $data_r eq 'SCALAR') { } 42 35 77
elsif (ref $data_r eq 'ARRAY') { } 34 1 35
elsif (ref $data_r eq $EMPTY_STR) { } 1 0 1
53 42 ${$data_r} = encode_entities(decode_entities(${$data_r}),
54 $CHAR_CHARS);
55 } elsif (ref $data_r eq 'ARRAY') {
56 34 foreach my $one_data (@{$data_r}) {
57 41 encode_char_entities(\$one_data);
58 }
59 } elsif (ref $data_r eq $EMPTY_STR) {
60 1 return encode_entities(decode_entities($data_r), $CHAR_CHARS);
61 } else {
62 0 err 'Reference \''.(ref $data_r).'\' doesn\'t supported.';
63 }
64 76 return;
65 }
66  
67 27 1;
68  
69 __END__
« Tags/Output/Raw.pm ↑ Summary Tags/Utils/Preserve.pm »