blib/lib/HTML/AutoTag.pm

filter: Statementstmt 100% 64 / 64 Branchbran 90.6% 29 / 32 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 7 / 7 Podpod 100% 2 / 2 totaltotal 95.3% 103 / 108 SCAR 30.5
CC
21
Cov
96%
tag
16.0
new
6.0
BEGIN
1.0
CRAP 21.0
↑ Summary
1 package HTML::AutoTag;
2 8 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 our $VERSION = '1.06';
6  
7 8 ▶use HTML::Entities;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use Tie::Hash::Attribute;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 8 our( $INDENT, $NEWLINE, $LEVEL, $ENCODE, $ENCODES, $SORTED );
11  
12 sub new {
13 15 ▶ my $self = shift;
Subroutine new: called CC=6 SCAR=17.9
Pod documented
14 15 my $args = {@_};
15 15 ▶ $ENCODE = defined $args->{encode} ? $args->{encode} : exists $args->{encodes};
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{'encode'} ? : 5 10 15
16 15 ▶ $ENCODES = defined $args->{encodes} ? $args->{encodes} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{'encodes'} ? : 5 10 15
17 15 ▶ $INDENT = defined $args->{indent} ? $args->{indent} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{'indent'} ? : 5 10 15
18 15 ▶ $NEWLINE = defined $args->{indent} ? "\n" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{'indent'} ? : 5 10 15
19 15 ▶ $LEVEL = $args->{level} || 0;
Condition50%1/2
$args->{'level'} || 0
l!l
015
MC/DC0%0/1
$args->{'level'} || 0
$args->{'level'}
Truth table50%1/2
A
A= $args->{'level'}
Aresult
11
00
20 15 $SORTED = $args->{sorted};
21 15 bless {}, $self;
22 }
23  
24 sub tag {
25 267 ▶ my $self = shift;
Subroutine tag: called CC=16 SCAR=27.7
Pod documented
26 267 my %args = @_;
27 267 my $attr = $args{attr};
28  
29 267 ▶ return '' unless defined $args{tag};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"tag"} 0 267 267
30  
31 267 my $attr_str;
32 267 ▶ if (grep ref($_), values %$attr) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep ref($_), values %$attr) { } 47 220 267
33 # complex attrs use a tied hash
34 47 ▶ unless (grep /^-/, keys %$attr) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep /^-/, keys %$attr) 47 0 47
35 47 tie my %attr, 'Tie::Hash::Attribute', sorted => $SORTED;
36 47 %attr = %$attr;
37 47 $attr = \%attr;
38 }
39 } else {
40 # simple attrs can bypass being tied
41 220 $attr_str = '';
42 220 ▶ my @keys = $SORTED ? sort keys %$attr : keys %$attr;
Branch: 1 branch
BranchTrueFalseTotal
$HTML::AutoTag::SORTED ? : 178 42 220
43 220 for my $key (@keys) {
44 79 $attr_str .= ' ' . Tie::Hash::Attribute::_key( $key ) . '="' . Tie::Hash::Attribute::_val( $attr->{$key} ) . '"';
45 }
46 }
47  
48 # emtpy tag
49 267 ▶ unless (defined $args{cdata}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"cdata"}) 35 232 267
50 35 ▶ return ( $INDENT x $LEVEL )
Branch: 1 branch
BranchTrueFalseTotal
defined $attr_str ? : 35 0 35
51 . "<$args{tag}"
52 . ( defined( $attr_str ) ? $attr_str : scalar( %$attr ) )
53 . " />$NEWLINE"
54 ;
55 }
56  
57 232 my $cdata;
58 my $no_post_indent;
59 232 ▶ if (ref($args{cdata}) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $args{'cdata'} eq 'ARRAY') { } 12 220 232
elsif (ref $args{'cdata'} eq 'HASH') { } 14 206 220
60  
61 12 ▶ if (ref($args{cdata}[0]) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args{'cdata'}[0] eq 'HASH') { } 4 8 12
62  
63 4 $LEVEL++;
64 4 $cdata = $NEWLINE;
65 4 for (0 .. $#{ $args{cdata} }) {
66 18 $cdata .= $self->tag( %{ $args{cdata}[$_] } );
67 }
68 4 $LEVEL--;
69  
70 } else {
71 8 my $str = '';
72 8 for (@{ $args{cdata} }) {
73 36 $str .= $self->tag( tag => $args{tag}, attr => $attr, cdata => $_);
74 }
75 8 return $str;
76 }
77  
78 } elsif (ref($args{cdata}) eq 'HASH') {
79 14 $LEVEL++;
80 14 $cdata = $NEWLINE . $self->tag( %{ $args{cdata} } );
81 14 $LEVEL--;
82  
83 } else {
84 206 ▶ $cdata = $ENCODE ? HTML::Entities::encode_entities( $args{cdata}, $ENCODES ) : $args{cdata};
Branch: 1 branch
BranchTrueFalseTotal
$HTML::AutoTag::ENCODE ? : 5 201 206
85 206 $no_post_indent = 1;
86 }
87  
88 224 ▶ return ( $INDENT x $LEVEL )
Branch: 2 branches
BranchTrueFalseTotal
defined $attr_str ? : 183 41 224
$no_post_indent ? : 206 18 224
89 . "<$args{tag}" . ( defined( $attr_str ) ? $attr_str : scalar( %$attr ) )
90 . ">$cdata"
91 . ( $no_post_indent ? '' : ( $INDENT x $LEVEL ) )
92 . "</$args{tag}>$NEWLINE"
93 ;
94 }
95  
96 8 1;
97  
98 __END__
↑ Summary