blib/lib/XML/Atom/Content.pm

filter: Statementstmt 97.6% 84 / 86 Branchbran 81.4% 44 / 54 Conditioncond 70.5% 12 / 17 MC/DCmcdc 54.5% 6 / 11 Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 3 totaltotal 86.1% 155 / 180 SCAR 36.0
CC
35
Cov
89%
body
33.0
init
4.0
_is_printable
3.0
CRAP 36.6
« blib/lib/XML/Atom/Thing.pm ↑ Summary blib/lib/XML/Atom/Util.pm »
1 # $Id$
2  
3 package XML::Atom::Content;
4 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 20 ▶use base qw( XML::Atom::Base );
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 20 __PACKAGE__->mk_attr_accessors(qw( type mode ));
8 20 __PACKAGE__->mk_xml_attr_accessors(qw( lang base ));
9  
10 20 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 20 ▶use XML::Atom;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 20 ▶use MIME::Base64 qw( encode_base64 decode_base64 );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 22 ▶sub element_name { 'content' }
Subroutine element_name: called CC=1 SCAR=0.0
Pod undocumented
15  
16 sub init {
17 29 ▶ my $content = shift;
Subroutine init: called CC=4 SCAR=13.9
Pod undocumented
18 29 ▶ my %param = @_ == 1 ? (Body => $_[0]) : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 1 28 29
19 29 $content->SUPER::init(%param);
20 29 ▶ if ($param{Body}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param{"Body"}) 10 19 29
21 10 $content->body($param{Body});
22 }
23 29 ▶ if ($param{Type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param{"Type"}) 1 28 29
24 1 $content->type($param{Type});
25 }
26 29 return $content;
27 }
28  
29 sub body {
30 39 ▶ my $content = shift;
Subroutine body: called CC=30 SCAR=35.0
Pod undocumented
31 39 my $elem = $content->elem;
32 39 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 21 18 39
33 21 my $data = shift;
34 21 if (LIBXML) {
35 21 $elem->removeChildNodes;
36 } else {
37 $elem->removeChild($_) for $elem->getChildNodes;
38 }
39 21 ▶ if (!_is_printable($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not _is_printable($data)) { } 4 17 21
40 4 Encode::_utf8_off($data);
41 4 if (LIBXML) {
42 4 $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 4 ▶ if ($content->version == 0.3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content->version == "0.3") 3 1 4
48 3 $content->mode('base64');
49 }
50 } else {
51 17 my $copy = '<div xmlns="http://www.w3.org/1999/xhtml">' .
52 $data .
53 '</div>';
54 17 my $node;
55 17 eval {
56 17 if (LIBXML) {
57 17 my $parser = XML::Atom->libxml_parser;
58 17 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 17 ▶ if (!$@ && $node) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $@ and $node) { } 15 2 17
Condition66%2/3
not $@ and $node
!ll&&!rl&&r
2015
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 2 if (LIBXML) {
76 2 $elem->appendChild(XML::LibXML::Text->new($data));
77 } else {
78 $elem->appendChild(XML::XPath::Node::Text->new($data));
79 }
80  
81 2 ▶ if ($content->version == 0.3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content->version == 0.3) { } 1 1 2
82 1 $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 18 ▶ unless (exists $content->{__body}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $content->{"__body"}) 18 0 18
90 18 my $mode;
91  
92 18 ▶ if ($content->version == 0.3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content->version == 0.3) { } 13 5 18
93 13 ▶ $mode = $content->mode || 'xml';
Condition100%2/2
$content->mode || 'xml'
l!l
112
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 18 ▶ if ($mode eq 'xml') {
Branch: 3 branches
BranchTrueFalseTotal
if ($mode eq 'xml') { } 10 8 18
elsif ($mode eq 'base64') { } 5 3 8
elsif ($mode eq 'escaped') { } 3 0 3
105 10 my @children = grep ref($_) =~ /Element/,
106 LIBXML ? $elem->childNodes : $elem->getChildNodes;
107 10 ▶ if (@children) {
Branch: 1 branch
BranchTrueFalseTotal
if (@children) { } 10 0 10
108 10 ▶ if (@children == 1 && $children[0]->getLocalName eq 'div') {
Branch: 1 branch
BranchTrueFalseTotal
if (@children == 1 and $children[0]->getLocalName eq "div") 10 0 10
Condition33%1/3
@children == 1 and $children[0]->getLocalName eq "div"
!ll&&!rl&&r
0010
MC/DC0%0/2
@children == 1 and $children[0]->getLocalName eq "div"
@children == 1 $children[0]->getLocalName eq "div"
Truth table33%1/3
A and B
A= @children == 1B= $children[0]->getLocalName eq "div"
ABresult
0X0
100
111
109 10 @children =
110 LIBXML ? $children[0]->childNodes :
111 $children[0]->getChildNodes
112 }
113 10 $content->{__body} = '';
114 10 for my $n (@children) {
115 12 $content->{__body} .= $n->toString(LIBXML ? 1 : 0);
116 }
117 } else {
118 0 $content->{__body} = LIBXML ? $elem->textContent : $elem->string_value;
119 }
120 10 ▶ if ($] >= 5.008) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= "5.008") 10 0 10
121 10 ▶ Encode::_utf8_off($content->{__body}) unless $XML::Atom::ForceUnicode;
Branch: 1 branch
BranchTrueFalseTotal
unless $XML::Atom::ForceUnicode 10 0 10
122 }
123 } elsif ($mode eq 'base64') {
124 5 my $raw = decode_base64(LIBXML ? $elem->textContent : $elem->string_value);
125 5 ▶ if ($content->type && $content->type =~ m!^text/!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content->type and $content->type =~ m[^text/]) { } 2 3 5
Condition100%3/3
$content->type and $content->type =~ m[^text/]
!ll&&!rl&&r
122
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 { || $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 3 $content->{__body} = $raw;
130 }
131 } elsif ($mode eq 'escaped') {
132 3 $content->{__body} = LIBXML ? $elem->textContent : $elem->string_value;
133 } else {
134 0 $content->{__body} = undef;
135 }
136 }
137 }
138 39 $content->{__body};
139 }
140  
141 sub _is_printable {
142 21 ▶ my $data = shift;
Subroutine _is_printable: called CC=3 SCAR=11.0
143  
144 21 local $@;
145 # try decoding this $data with UTF-8
146 my $decoded =
147 ( Encode::is_utf8($data)
148 ? $data
149 21 ▶ : eval { Encode::decode("utf-8", $data, Encode::FB_CROAK) } );
Branch: 1 branch
BranchTrueFalseTotal
&Encode::is_utf8($data) ? : 3 18 21
150  
151 21 ▶ return ! $@ && $decoded =~ /^[\p{IsPrint}\s]*$/;
Condition100%3/3
!$@ && $decoded =~ /^[\p{IsPrint}\s]*$/
!ll&&!rl&&r
2217
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 20 1;
« blib/lib/XML/Atom/Thing.pm ↑ Summary blib/lib/XML/Atom/Util.pm »