| 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
| |||||||||||||||||||||||||||||
| 19 | 47 | $content->SUPER::init(%param); | |||||||||||||||||||||||||||
| 20 | 47 | ▶ | if ($param{Body}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | 12 | $content->body($param{Body}); | |||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | 47 | ▶ | if ($param{Type}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
not $@ and $node
MC/DC50%1/2
not $@ and $node
not $@
$node
Truth table66%2/3
not(A) and B
A= not $@B= $node
| |||||||||||||||||||||||||||||
| 68 | 15 | $elem->appendChild($node); | |||||||||||||||||||||||||||
| 69 | 15 | ▶ | if ($content->version == 0.3) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 82 | 2 | $content->mode('escaped'); | |||||||||||||||||||||||||||
| 83 | } else { | ||||||||||||||||||||||||||||
| 84 | 1 | ▶ | $content->type($data =~ /^\s*</ ? 'html' : 'text'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | } else { | ||||||||||||||||||||||||||||
| 89 | 25 | ▶ | unless (exists $content->{__body}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 25 | my $mode; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 25 | ▶ | if ($content->version == 0.3) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 20 | ▶ | $mode = $content->mode || 'xml'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$content->mode || 'xml'
MC/DC100%1/1
$content->mode || 'xml'
$content->mode
Truth table100%2/2
A
A= $content->mode
| |||||||||||||||||||||||||||||
| 94 | } else { | ||||||||||||||||||||||||||||
| 95 | 5 | ▶ | $mode = | ||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 105 | 13 | my @children = grep ref($_) =~ /Element/, | |||||||||||||||||||||||||||
| 106 | LIBXML ? $elem->childNodes : $elem->getChildNodes; | ||||||||||||||||||||||||||||
| 107 | 13 | ▶ | if (@children) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 13 | ▶ | if (@children == 1 && $children[0]->getLocalName eq 'div') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@children == 1 and $children[0]->getLocalName eq "div"
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"
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 121 | 13 | ▶ | Encode::_utf8_off($content->{__body}) unless $XML::Atom::ForceUnicode; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
$content->type and $content->type =~ m[^text/]
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/]
| |||||||||||||||||||||||||||||
| 126 | 2 | ▶ | $content->{__body} = eval { Encode::decode("utf-8", $raw) } || $raw; | ||||||||||||||||||||||||||
|
Condition33%1/3
eval {
do {
&Encode::decode('utf-8', $raw)
}
} || $raw
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
| |||||||||||||||||||||||||||||
| 127 | 2 | ▶ | Encode::_utf8_off($content->{__body}) unless $XML::Atom::ForceUnicode; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 23 | ▶ | return ! $@ && $decoded =~ /^[\p{IsPrint}\s]*$/; | ||||||||||||||||||||||||||
|
Condition100%3/3
!$@ && $decoded =~ /^[\p{IsPrint}\s]*$/
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]*$/
| |||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 21 | 1; | |||||||||||||||||||||||||||