Atom/Content.pm

filter: Statementstmt 97.6% 84 / 86 Branchbran 83.3% 45 / 54 Conditioncond 76.4% 13 / 17 MC/DCmcdc 63.6% 7 / 11 Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 3 totaltotal 87.7% 158 / 180 CC 35 SCAR 31.4
CC
35
Cov
90%
body
32.1
init
4.0
_is_printable
3.0
CRAP 23.1
« Atom/Thing.pm ↑ Summary Atom/Feed.pm »
1 # $Id$
2  
3 package XML::Atom::Content;
4 21 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 21 ▶use base qw( XML::Atom::Base );
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 21 __PACKAGE__->mk_attr_accessors(qw( type mode ));
8 21 __PACKAGE__->mk_xml_attr_accessors(qw( lang base ));
9  
10 21 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 21 ▶use XML::Atom;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 21 ▶use MIME::Base64 qw( encode_base64 decode_base64 );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 24 ▶sub element_name { 'content' }
Subroutine element_name: called CC=1 SCAR=0.0
Pod undocumented
15  
16 47 ▶sub init {
Subroutine init: called CC=4 SCAR=13.9
Pod undocumented
17 47 my $content = shift;
18 47 ▶ my %param = @_ == 1 ? (Body => $_[0]) : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 1 46 47
19 47 $content->SUPER::init(%param);
20 47 ▶ if ($param{Body}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param{"Body"}) 12 35 47
21 12 $content->body($param{Body});
22 }
23 47 ▶ if ($param{Type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param{"Type"}) 1 46 47
24 1 $content->type($param{Type});
25 }
26 47 return $content;
27 }
28  
29 48 ▶sub body {
Subroutine body: called CC=30 SCAR=34.7
Pod undocumented
30 48 my $content = shift;
31 48 my $elem = $content->elem;
32 48 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 23 25 48
33 23 my $data = shift;
34 23 if (LIBXML) {
35 23 $elem->removeChildNodes;
36 } else {
37 $elem->removeChild($_) for $elem->getChildNodes;
38 }
39 23 ▶ if (!_is_printable($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not _is_printable($data)) { } 5 18 23
40 5 Encode::_utf8_off($data);
41 5 if (LIBXML) {
42 5 $elem->appendChild(XML::LibXML::Text->new(encode_base64($data, '')));
43 } else {
44 $elem->appendChild(XML::XPath::Node::Text->new(encode_base64($data, '')));
45 }
46  
47 5 ▶ if ($content->version == 0.3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content->version == "0.3") 4 1 5
48 4 $content->mode('base64');
49 }
50 } else {
51 18 my $copy = '<div xmlns="http://www.w3.org/1999/xhtml">' .
52 $data .
53 '</div>';
54 18 my $node;
55 18 eval {
56 18 if (LIBXML) {
57 18 my $parser = XML::Atom->libxml_parser;
58 18 my $tree = $parser->parse_string($copy);
59 15 $node = $tree->getDocumentElement;
60 } else {
61 my $parser = XML::Atom->expat_parser;
62 my $xp = XML::XPath->new(xml => $copy, parser => $parser);
63 $node = (($xp->find('/')->get_nodelist)[0]->getChildNodes)[0]
64 if $xp;
65 }
66 };
67 18 ▶ if (!$@ && $node) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $@ and $node) { } 15 3 18
Condition66%2/3
not $@ and $node
!ll&&!rl&&r
3015
MC/DC50%1/2
not $@ and $node
not $@ $node
Truth table66%2/3
not(A) and B
A= not $@B= $node
ABresult
0X0
100
111
68 15 $elem->appendChild($node);
69 15 ▶ if ($content->version == 0.3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content->version == 0.3) { } 11 4 15
70 11 $content->mode('xml');
71 } else {
72 4 $content->type('xhtml');
73 }
74 } else {
75 3 if (LIBXML) {
76 3 $elem->appendChild(XML::LibXML::Text->new($data));
77 } else {
78 $elem->appendChild(XML::XPath::Node::Text->new($data));
79 }
80  
81 3 ▶ if ($content->version == 0.3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content->version == 0.3) { } 2 1 3
82 2 $content->mode('escaped');
83 } else {
84 1 ▶ $content->type($data =~ /^\s*</ ? 'html' : 'text');
Branch: 1 branch
BranchTrueFalseTotal
$data =~ /^\s*</ ? : 1 0 1
85 }
86 }
87 }
88 } else {
89 25 ▶ unless (exists $content->{__body}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $content->{"__body"}) 25 0 25
90 25 my $mode;
91  
92 25 ▶ if ($content->version == 0.3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content->version == 0.3) { } 20 5 25
93 20 ▶ $mode = $content->mode || 'xml';
Condition100%2/2
$content->mode || 'xml'
l!l
164
MC/DC100%1/1
$content->mode || 'xml'
$content->mode
Truth table100%2/2
A
A= $content->mode
Aresult
11
00
94 } else {
95 5 ▶ $mode =
Branch: 5 branches
BranchTrueFalseTotal
$content->type eq 'xhtml' ? : 2 3 5
$content->type =~ m[[/\+]xml$] ? : 0 3 3
$content->type eq 'html' ? : 1 2 3
$content->type eq 'text' ? : 1 1 2
$content->type =~ m[^text/] ? : 0 1 1
96 $content->type eq 'xhtml' ? 'xml'
97 : $content->type =~ m![/\+]xml$! ? 'xml'
98 : $content->type eq 'html' ? 'escaped'
99 : $content->type eq 'text' ? 'escaped'
100 : $content->type =~ m!^text/! ? 'escaped'
101 : 'base64';
102 }
103  
104 25 ▶ if ($mode eq 'xml') {
Branch: 3 branches
BranchTrueFalseTotal
if ($mode eq 'xml') { } 13 12 25
elsif ($mode eq 'base64') { } 7 5 12
elsif ($mode eq 'escaped') { } 5 0 5
105 13 my @children = grep ref($_) =~ /Element/,
106 LIBXML ? $elem->childNodes : $elem->getChildNodes;
107 13 ▶ if (@children) {
Branch: 1 branch
BranchTrueFalseTotal
if (@children) { } 13 0 13
108 13 ▶ if (@children == 1 && $children[0]->getLocalName eq 'div') {
Branch: 1 branch
BranchTrueFalseTotal
if (@children == 1 and $children[0]->getLocalName eq "div") 12 1 13
Condition66%2/3
@children == 1 and $children[0]->getLocalName eq "div"
!ll&&!rl&&r
1012
MC/DC50%1/2
@children == 1 and $children[0]->getLocalName eq "div"
@children == 1 $children[0]->getLocalName eq "div"
Truth table66%2/3
A and B
A= @children == 1B= $children[0]->getLocalName eq "div"
ABresult
0X0
100
111
109 12 @children =
110 LIBXML ? $children[0]->childNodes :
111 $children[0]->getChildNodes
112 }
113 13 $content->{__body} = '';
114 13 for my $n (@children) {
115 17 $content->{__body} .= $n->toString(LIBXML ? 1 : 0);
116 }
117 } else {
118 0 $content->{__body} = LIBXML ? $elem->textContent : $elem->string_value;
119 }
120 13 ▶ if ($] >= 5.008) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= "5.008") 13 0 13
121 13 ▶ Encode::_utf8_off($content->{__body}) unless $XML::Atom::ForceUnicode;
Branch: 1 branch
BranchTrueFalseTotal
unless $XML::Atom::ForceUnicode 13 0 13
122 }
123 } elsif ($mode eq 'base64') {
124 7 my $raw = decode_base64(LIBXML ? $elem->textContent : $elem->string_value);
125 7 ▶ if ($content->type && $content->type =~ m!^text/!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content->type and $content->type =~ m[^text/]) { } 2 5 7
Condition100%3/3
$content->type and $content->type =~ m[^text/]
!ll&&!rl&&r
142
MC/DC100%2/2
$content->type and $content->type =~ m[^text/]
$content->type $content->type =~ m[^text/]
Truth table100%3/3
A and B
A= $content->typeB= $content->type =~ m[^text/]
ABresult
0X0
100
111
126 2 ▶ $content->{__body} = eval { Encode::decode("utf-8", $raw) } || $raw;
Condition33%1/3
eval { do { &Encode::decode('utf-8', $raw) } } || $raw
l!l&&r!l&&!r
200
MC/DC0%0/2
eval { do { &Encode::decode('utf-8', $raw) } } || $raw
eval { do { &Encode::decode('utf-8', $raw) } } $raw
Truth table33%1/3
A || B
A= eval { do { &Encode::decode('utf-8', $raw) } }B= $raw
ABresult
1X1
011
000
127 2 ▶ Encode::_utf8_off($content->{__body}) unless $XML::Atom::ForceUnicode;
Branch: 1 branch
BranchTrueFalseTotal
unless $XML::Atom::ForceUnicode 2 0 2
128 } else {
129 5 $content->{__body} = $raw;
130 }
131 } elsif ($mode eq 'escaped') {
132 5 $content->{__body} = LIBXML ? $elem->textContent : $elem->string_value;
133 } else {
134 0 $content->{__body} = undef;
135 }
136 }
137 }
138 48 $content->{__body};
139 }
140  
141 23 ▶sub _is_printable {
Subroutine _is_printable: called CC=3 SCAR=11.0
142 23 my $data = shift;
143  
144 23 local $@;
145 # try decoding this $data with UTF-8
146 my $decoded =
147 ( Encode::is_utf8($data)
148 ? $data
149 23 ▶ : eval { Encode::decode("utf-8", $data, Encode::FB_CROAK) } );
Branch: 1 branch
BranchTrueFalseTotal
&Encode::is_utf8($data) ? : 3 20 23
150  
151 23 ▶ return ! $@ && $decoded =~ /^[\p{IsPrint}\s]*$/;
Condition100%3/3
!$@ && $decoded =~ /^[\p{IsPrint}\s]*$/
!ll&&!rl&&r
2318
MC/DC100%2/2
!$@ && $decoded =~ /^[\p{IsPrint}\s]*$/
!$@ $decoded =~ /^[\p{IsPrint}\s]*$/
Truth table100%3/3
not(A) && B
A= !$@B= $decoded =~ /^[\p{IsPrint}\s]*$/
ABresult
0X0
100
111
152 }
153  
154 21 1;
« Atom/Thing.pm ↑ Summary Atom/Feed.pm »