blib/lib/XML/Atom/Syndication/Text.pm | |||
---|---|---|---|
Criterion | Covered | Total | % |
statement | 38 | 61 | 62.3 |
branch | 10 | 24 | 41.6 |
condition | 3 | 5 | 60.0 |
subroutine | 4 | 4 | 100.0 |
pod | 1 | 2 | 50.0 |
total | 56 | 96 | 58.3 |
line | stmt | bran | cond | sub | pod | time | code |
---|---|---|---|---|---|---|---|
1 | package XML::Atom::Syndication::Text; | ||||||
2 | 11 | 11 | 370 | use strict; | |||
11 | 14 | ||||||
11 | 328 | ||||||
3 | |||||||
4 | 11 | 11 | 41 | use base qw( XML::Atom::Syndication::Object ); | |||
11 | 40 | ||||||
11 | 6504 | ||||||
5 | |||||||
6 | XML::Atom::Syndication::Text->mk_accessors('attribute', 'type'); | ||||||
7 | |||||||
8 | sub init { | ||||||
9 | 50 | 50 | 0 | 79 | my $text = shift; | ||
10 | 50 | 50 | 182 | my %param = @_ == 1 ? (Body => $_[0]) : @_; # escaped text is assumed. | |||
11 | 50 | 195 | $text->SUPER::init(%param); | ||||
12 | 50 | 198 | my $e = $text->elem; | ||||
13 | 50 | 50 | 143 | if ($param{Body}) { | |||
14 | 0 | 0 | $text->body($param{Body}); | ||||
15 | } | ||||||
16 | 50 | 50 | 85 | if ($param{Type}) { | |||
17 | 0 | 0 | $text->type($param{Type}); | ||||
18 | } | ||||||
19 | 50 | 150 | $text; | ||||
20 | } | ||||||
21 | |||||||
22 | sub body { | ||||||
23 | 48 | 48 | 1 | 14326 | my $text = shift; | ||
24 | 48 | 127 | my $elem = $text->elem; | ||||
25 | 48 | 100 | 120 | my $type = $elem->attributes->{'{}type'} || 'text'; | |||
26 | 48 | 50 | 390 | if (@_) { # set | |||
27 | 0 | 0 | my $data = shift; | ||||
28 | 0 | 0 | 0 | if ($type eq 'xhtml') { | |||
29 | 0 | 0 | my $node = $data; | ||||
30 | 0 | 0 | 0 | unless (ref $node) { | |||
31 | 0 | 0 | my $copy = | ||||
32 | ' ' . $data |
||||||
33 | . ''; | ||||||
34 | 0 | 0 | eval { | ||||
35 | 0 | 0 | require XML::Elemental; | ||||
36 | 0 | 0 | my $parser = XML::Elemental->parser; | ||||
37 | 0 | 0 | my $xml = $parser->parse_string($copy); | ||||
38 | 0 | 0 | $node = $xml->contents->[0]; | ||||
39 | }; | ||||||
40 | 0 | 0 | 0 | return $text->error( | |||
41 | "Error parsing content body string as XML: $@") | ||||||
42 | if $@; | ||||||
43 | } | ||||||
44 | 0 | 0 | $node->parent($elem); | ||||
45 | 0 | 0 | $elem->contents([$node]); | ||||
46 | } else { # is text or html | ||||||
47 | 0 | 0 | my $text = XML::Elemental::Characters->new; | ||||
48 | 0 | 0 | $text->data($data); | ||||
49 | 0 | 0 | $text->parent($elem); | ||||
50 | 0 | 0 | $elem->contents([$text]); | ||||
51 | } | ||||||
52 | 0 | 0 | $text->{__body} = undef; | ||||
53 | 0 | 0 | 1; | ||||
54 | } else { # get | ||||||
55 | 48 | 50 | 98 | unless (defined $text->{__body}) { | |||
56 | 48 | 100 | 89 | if ($type eq 'xhtml') { | |||
57 | my @children = | ||||||
58 | 14 | 94 | grep { ref($_) eq 'XML::Elemental::Element' } | ||||
59 | 14 | 22 | @{$elem->contents}; | ||||
14 | 33 | ||||||
60 | 14 | 50 | 28 | if (@children) { | |||
61 | 14 | 38 | my ($local) = | ||||
62 | $children[0]->name =~ /{.*}(.+)/; # process name | ||||||
63 | 14 | 50 | 33 | 186 | @children = @{$children[0]->contents} | ||
14 | 33 | ||||||
64 | if (@children == 1 && $local eq 'div'); | ||||||
65 | 14 | 72 | $text->{__body} = ' '; |
||||
66 | 14 | 97 | my $w = XML::Atom::Syndication::Writer->new; | ||||
67 | 14 | 41 | $w->set_prefix('', 'http://www.w3.org/1999/xhtml'); | ||||
68 | 14 | 18 | map { $text->{__body} .= $w->as_xml($_) } @children; | ||||
21 | 65 | ||||||
69 | 14 | 132 | $text->{__body} .= ''; | ||||
70 | } else { | ||||||
71 | 0 | 0 | $text->{__body} = $elem->text_content; | ||||
72 | } | ||||||
73 | 14 | 50 | 48 | if ($] >= 5.008) { | |||
74 | 14 | 42 | Encode::_utf8_on($text->{__body}); | ||||
75 | 14 | 36 | $text->{__body} =~ s/(\w{4});/chr(hex($1))/eg; | ||||
0 | 0 | ||||||
76 | 14 | 34 | Encode::_utf8_off($text->{__body}); | ||||
77 | } | ||||||
78 | } else { # escaped | ||||||
79 | 34 | 85 | $text->{__body} = $elem->text_content; | ||||
80 | } | ||||||
81 | } | ||||||
82 | 48 | 748 | $text->{__body}; | ||||
83 | } | ||||||
84 | } | ||||||
85 | |||||||
86 | 1; | ||||||
87 | |||||||
88 | __END__ |