blib/lib/XML/Driver/HTML.pm

filter: Statementstmt 95.1% 78 / 82 Branchbran 71.4% 40 / 56 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 8 / 8 Podpod 66.6% 2 / 3 totaltotal 81.7% 130 / 159 CC 35 SCAR 27.3
CC
35
Cov
83%
dumptree
18.9
parse
18.0
new
3.6
CRAP 15.4
↑ Summary
1 # XML::Driver::HTML
2 #
3 # Copyright (c) 2000 Michael Koehne <kraehe@copyleft.de>
4 #
5 # XML::Driver::HTML is free software. You can use and redistribute
6 # this copy under terms of the GNU General Public License.
7  
8 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶no warnings 'utf8' ;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 package XML::Driver::HTML;
12  
13 1 ▶use HTML::TreeBuilder;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use vars qw($VERSION $METHODS);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 $VERSION = '0.06';
18 1 $METHODS = {
19 start_document => 1,
20 end_document => 1,
21 start_element => 1,
22 end_element => 1,
23 characters => 1,
24 comment => 1
25 };
26  
27 1 $HTML::TreeBuilder::Debug = 0; # default debug level
28  
29 1 ▶sub new {
Subroutine new: called CC=3 SCAR=12.7
Pod documented
30 1 my $proto = shift;
31 0 ▶ my $self = ($#_ == 0) ? { %{ (shift) } } : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
$#_ == 0 ? : 0 1 1
32 1 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
33  
34 1 bless($self, $class);
35 }
36  
37 3 ▶sub parse {
Subroutine parse: called CC=16 SCAR=28.9
Pod documented
38 3 my $self = shift;
39 0 ▶ my $args = ($#_ == 0) ? { %{ (shift) } } : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
$#_ == 0 ? : 0 3 3
40 3 my $file;
41 3 my $result = undef;
42  
43 3 ▶ $self->{'Source'} = $args->{'Source'} if $args->{'Source'};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"Source"} 2 1 3
44 3 ▶ $self->{'Handler'} = $args->{'Handler'} if $args->{'Handler'};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"Handler"} 0 3 3
45  
46 3 ▶ die "no Source defined" unless $self->{'Source'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"Source"} 0 3 3
47 3 ▶ die "no Handler defined" unless $self->{'Handler'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"Handler"} 0 3 3
48  
49 3 my $h = HTML::TreeBuilder->new;
50 3 $h->ignore_unknown(1);
51 3 $h->warn(0);
52 3 $h->{'_store_comments'}=1;
53 3 $h->{'_store_declarations'}=1;
54  
55 3 ▶ if ($self->{'Source'}{'ByteStream'}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->{'Source'}{'ByteStream'}) { } 1 2 3
elsif ($self->{'Source'}{'String'}) { } 1 1 2
elsif ($self->{'Source'}{'SystemId'}) { } 1 0 1
56 1 $h->parse_file($self->{'Source'}{'ByteStream'});
57 } elsif ($self->{'Source'}{'String'}) {
58 1 $h->parse($self->{'Source'}{'String'});
59 1 $h->eof();
60 } elsif ($self->{'Source'}{'SystemId'}) {
61 1 $h->parse_file($self->{'Source'}{'SystemId'});
62 } else {
63 0 die "no Source defined";
64 }
65  
66 3 $self->{'Methods'} = {};
67 3 foreach (keys %$METHODS) {
68 18 ▶ $self->{'Methods'}{$_} = 1 if $self->{'Handler'}->can($_);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Handler"}->can($_) 18 0 18
69 }
70  
71 3 delete $self->{'Recode'};
72 3 ▶ $self->{'Recode'} = 1 if lc($self->{'Source'}{'Encoding'}) eq "iso-8859-1";
Branch: 1 branch
BranchTrueFalseTotal
if lc $self->{"Source"}{"Encoding"} eq "iso-8859-1" 1 2 3
73 3 ▶ $self->{'Recode'} = 1 if lc($self->{'Source'}{'Encoding'}) eq "iso88591";
Branch: 1 branch
BranchTrueFalseTotal
if lc $self->{"Source"}{"Encoding"} eq "iso88591" 0 3 3
74 3 ▶ $self->{'Recode'} = 1 if lc($self->{'Source'}{'Encoding'}) eq "latin1";
Branch: 1 branch
BranchTrueFalseTotal
if lc $self->{"Source"}{"Encoding"} eq "latin1" 0 3 3
75  
76 $self->{'Handler'}->start_document()
77 3 ▶ if $self->{'Methods'}{'start_document'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Methods"}{"start_document"} 3 0 3
78 3 $self->dumptree($h);
79 $result = $self->{'Handler'}->end_document()
80 3 ▶ if $self->{'Methods'}{'end_document'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Methods"}{"end_document"} 3 0 3
81  
82 3 $h = $h->delete(); # nuke it!
83  
84 3 return $result;
85 }
86  
87 165 ▶sub dumptree {
Subroutine dumptree: called CC=18 SCAR=29.4
Pod undocumented
88 165 my ($self,$element) = @_;
89  
90 165 my $tag = $element->{'_tag'};
91 165 my $cont = $element->{'_content'};
92 165 my $attr = {};
93 165 my $value;
94  
95 165 ▶ return if $tag eq "style";
Branch: 1 branch
BranchTrueFalseTotal
if $tag eq "style" 1 164 165
96  
97 164 ▶ if ($tag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag) 164 0 164
98 164 ▶ if ($self->{'Recode'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'Recode'}) { } 45 119 164
99 45 foreach (keys %$element) {
100 187 ▶ if ($_ !~ '^_') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_ =~ /^_/) 42 145 187
101 42 $value = $element->{$_};
102 42 $value =~
103 0 s/([\x80-\xFF])/chr(0xC0|ord($1)>>6).chr(0x80|ord($1)&0x3F)/eg;
104 42 $attr->{$_} = $value;
105 }
106 }
107 } else {
108 119 foreach (keys %$element) {
109 425 ▶ $attr->{$_} = $element->{$_}
Branch: 1 branch
BranchTrueFalseTotal
unless $_ =~ /^_/ 50 375 425
110 if $_ !~ '^_';
111 }
112 }
113  
114 164 ▶ if ($tag !~ /^\~/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tag =~ /^\~/) 161 3 164
115 $self->{'Handler'}->start_element( {
116 'Name' => $tag, 'Attributes' => $attr
117 } )
118 161 ▶ if $self->{'Methods'}{'start_element'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Methods"}{"start_element"} 161 0 161
119  
120 161 foreach (@$cont) {
121 315 ▶ if (ref $_ eq 'HTML::Element') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_ eq 'HTML::Element') { } 162 153 315
122 162 $self->dumptree($_)
123 } else {
124 10 s/([\x80-\xFF])/chr(0xC0|ord($1)>>6).chr(0x80|ord($1)&0x3F)/eg
125 153 ▶ if $self->{'Recode'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Recode"} 31 122 153
126 $self->{'Handler'}->characters( { 'Data' => $_ } )
127 153 ▶ if ($self->{'Methods'}{'characters'} && (lc($_) !~ "^[ \t]*<!"));
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Methods"}{"characters"} and not lc($_) =~ /^[ ]*<!/ 153 0 153
Condition33%1/3
$self->{"Methods"}{"characters"} and not lc($_) =~ /^[ ]*<!/
!ll&&!rl&&r
00153
MC/DC0%0/2
$self->{"Methods"}{"characters"} and not lc($_) =~ /^[ ]*<!/
$self->{"Methods"}{"characters"} not lc($_) =~ /^[ ]*<!/
Truth table33%1/3
A and not(B)
A= $self->{"Methods"}{"characters"}B= not lc($_) =~ /^[ ]*<!/
ABresult
0X0
100
111
128 }
129 }
130  
131 $self->{'Handler'}->end_element( { 'Name' => $tag } )
132 161 ▶ if $self->{'Methods'}{'end_element'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Methods"}{"end_element"} 161 0 161
133 }
134 164 ▶ if ($tag eq "~comment") {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq "~comment") 3 161 164
135 $self->{'Handler'}->comment( { 'Data' => $attr->{'text'} } )
136 3 ▶ if $self->{'Methods'}{'comment'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Methods"}{"comment"} 3 0 3
137 }
138 }
139 }
140  
141 1 1;
142 __END__
↑ Summary