blib/lib/HTML/TreeBuilder/LibXML.pm

filter: Statementstmt 99.0% 102 / 103 Branchbran 88.4% 23 / 26 Conditioncond 60.0% 9 / 15 MC/DCmcdc 30.0% 3 / 10 Subroutinesub 96.1% 25 / 26 Podpod 0.0% 0 / 13 totaltotal 83.9% 162 / 193 CC 22 SCAR 16.3
CC
22
Cov
93%
guts
13.6
eof
5.0
parse_file
2.0
CRAP 5.1
↑ Summary blib/lib/HTML/TreeBuilder/LibXML/Node.pm »
1 package HTML::TreeBuilder::LibXML;
2 22 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 22 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 22 our $VERSION = '0.26';
5 22 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 22 ▶use base 'HTML::TreeBuilder::LibXML::Node';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 22 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 22 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 sub new {
11 43 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
12 43 bless {}, $class;
13 }
14  
15 sub new_from_content {
16 29 ▶ my $class = shift;
Subroutine new_from_content: called CC=2 SCAR=6.9
Pod undocumented
17 29 my $self = $class->new;
18 29 for my $content (@_) {
19 31 $self->parse($content);
20 }
21 29 $self->eof;
22  
23 29 return $self;
24 }
25  
26 sub new_from_file {
27 1 ▶ my $class = shift;
Subroutine new_from_file: called CC=1 SCAR=0.0
Pod undocumented
28 1 my $self = $class->new;
29 1 $self->parse_file(@_);
30 1 return $self;
31 }
32  
33 22 my $PARSER;
34 sub _parser {
35 42 ▶ unless ($PARSER) {
Subroutine _parser: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless ($PARSER) 18 24 42
36 18 $PARSER = XML::LibXML->new();
37 18 $PARSER->recover(1);
38 18 $PARSER->recover_silently(1);
39 18 $PARSER->keep_blanks(0);
40 18 $PARSER->expand_entities(1);
41 18 $PARSER->no_network(1);
42 }
43 42 $PARSER;
44 }
45  
46 sub parse {
47 44 ▶ my ($self, $html) = @_;
Subroutine parse: called CC=1 SCAR=0.0
Pod undocumented
48 44 $self->{_content} .= $html;
49 }
50  
51 sub parse_content {
52 1 ▶ my $self = shift;
Subroutine parse_content: called CC=1 SCAR=0.0
Pod undocumented
53 1 $self->parse($_[0]);
54 1 $self->eof;
55 }
56  
57 sub parse_file {
58 1 ▶ my $self = shift;
Subroutine parse_file: called CC=2 SCAR=7.0
Pod undocumented
59 1 ▶ open (my $fh, '<', $_[0]) or die "Can't open $_[0]: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $_[0] or die "Can't open $_[0]: $!\n" 0 1 1
60 1 my $content = do { local $/; <$fh> };
61 1 $self->parse_content($content);
62 }
63  
64 sub eof {
65 42 ▶ my ($self, ) = @_;
Subroutine eof: called CC=5 SCAR=16.1
Pod undocumented
66 42 ▶ $self->{_content} = ' ' if defined $self->{_content} && $self->{_content} eq ''; # HACK
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_content"} and $self->{"_content"} eq "" 1 41 42
Condition66%2/3
defined $self->{"_content"} and $self->{"_content"} eq ""
!ll&&!rl&&r
0411
MC/DC50%1/2
defined $self->{"_content"} and $self->{"_content"} eq ""
defined $self->{"_content"} $self->{"_content"} eq ""
Truth table66%2/3
A and B
A= defined $self->{"_content"}B= $self->{"_content"} eq ""
ABresult
0X0
100
111
67 42 ▶ $self->{_implicit_html} = 1 unless $self->{_content} =~ /<html/i; # TODO find a better way to know that implicit <html> was inserted
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_content"} =~ /<html/i 32 10 42
68 42 ▶ $self->{_implicit_doctype} = 1 unless $self->{_content} =~ /<!DOCTYPE/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_content"} =~ /<!DOCTYPE/i 38 4 42
69 42 my $doc = $self->_parser->parse_html_string($self->{_content});
70 42 $self->{node} = $self->_documentElement($doc);
71 }
72  
73 sub _documentElement {
74 42 ▶ my($self, $doc) = @_;
Subroutine _documentElement: called CC=2 SCAR=7.0
75 42 ▶ return $doc->documentElement || do {
Condition66%2/3
$doc->documentElement || do { my $elem = $doc->createElement('html'); $elem->appendChild($doc->createElement('body')); $elem }
l!l&&r!l&&!r
3930
MC/DC0%0/2
$doc->documentElement || do { my $elem = $doc->createElement('html'); $elem->appendChild($doc->createElement('body')); $elem }
$doc->documentElement do { my $elem = $doc->createElement('html'); $elem->appendChild($doc->createElement('body')); $elem }
Truth table66%2/3
A || B
A= $doc->documentElementB= do { my $elem = $doc->createElement('html'); $elem->appendChild($doc->createElement('body')); $elem }
ABresult
1X1
011
000
76 3 my $elem = $doc->createElement("html");
77 3 $elem->appendChild($doc->createElement("body"));
78 3 $elem;
79 };
80 }
81  
82 sub elementify {
83 1 ▶ bless shift, 'HTML::TreeBuilder::LibXML::Node';
Subroutine elementify: called CC=1 SCAR=0.0
Pod undocumented
84 }
85  
86 sub guts {
87 27 ▶ my ($self, $destructive) = @_;
Subroutine guts: called CC=13 SCAR=26.1
Pod undocumented
88  
89 28 my @out = $self->{_implicit_html} ? map { $_->nonBlankChildNodes } $self->{node}->findnodes('/html/head | /html/body')
90 27 ▶ : $self->{node};
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_implicit_html'} ? : 26 1 27
91  
92 27 ▶ if ($destructive && @out > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($destructive and @out > 0) 8 19 27
Condition66%2/3
$destructive and @out > 0
!ll&&!rl&&r
1908
MC/DC50%1/2
$destructive and @out > 0
$destructive @out > 0
Truth table66%2/3
A and B
A= $destructiveB= @out > 0
ABresult
0X0
100
111
93 8 my $doc = XML::LibXML->createDocument;
94 8 ▶ if (!$self->{_implicit_doctype} && (my $dtd = $out[0]->ownerDocument->internalSubset)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"_implicit_doctype"} and my $dtd = $out[0]->ownerDocument->internalSubset) 0 8 8
Condition33%1/3
not $self->{"_implicit_doctype"} and my $dtd = $out[0]->ownerDocument->internalSubset
!ll&&!rl&&r
800
MC/DC0%0/2
not $self->{"_implicit_doctype"} and my $dtd = $out[0]->ownerDocument->internalSubset
not $self->{"_implicit_doctype"} my $dtd = $out[0]->ownerDocument->internalSubset
Truth table33%1/3
not(A) and B
A= not $self->{"_implicit_doctype"}B= my $dtd = $out[0]->ownerDocument->internalSubset
ABresult
0X0
100
111
95 0 $doc->createInternalSubset( $dtd->getName, $dtd->publicId, $dtd->systemId );
96 }
97 8 $doc->setDocumentElement($out[0]); # 1st child
98 8 $out[0]->addSibling($_) foreach @out[1..$#out];
99 }
100
101 27 ▶ return map { HTML::TreeBuilder::LibXML::Node->new($_) } @out if wantarray; # one simple normal case.
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 19 8 27
102 8 ▶ return unless @out;
Branch: 1 branch
BranchTrueFalseTotal
unless @out 0 8 8
103
104 8 my $doc = XML::LibXML->createDocument;
105 8 ▶ if (!$self->{_implicit_doctype} && (my $dtd = $out[0]->ownerDocument->internalSubset)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"_implicit_doctype"} and my $dtd = $out[0]->ownerDocument->internalSubset) 1 7 8
Condition66%2/3
not $self->{"_implicit_doctype"} and my $dtd = $out[0]->ownerDocument->internalSubset
!ll&&!rl&&r
701
MC/DC50%1/2
not $self->{"_implicit_doctype"} and my $dtd = $out[0]->ownerDocument->internalSubset
not $self->{"_implicit_doctype"} my $dtd = $out[0]->ownerDocument->internalSubset
Truth table66%2/3
not(A) and B
A= not $self->{"_implicit_doctype"}B= my $dtd = $out[0]->ownerDocument->internalSubset
ABresult
0X0
100
111
106 1 $doc->createInternalSubset( $dtd->getName, $dtd->publicId, $dtd->systemId );
107 }
108  
109 8 ▶ if (@out == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@out == 1) 5 3 8
110 5 $doc->adoptNode($out[0]);
111 5 $doc->setDocumentElement($out[0]);
112 5 return HTML::TreeBuilder::LibXML::Node->new($out[0]);
113 }
114
115 3 my $div = $doc->createElement('div'); # TODO put the _implicit flag somewhere, to be compatible with HTML::TreeBuilders
116 3 $doc->setDocumentElement($div);
117 3 $div->appendChild($_) for @out;
118
119 3 return HTML::TreeBuilder::LibXML::Node->new($div);
120 }
121  
122 sub disembowel {
123 7 ▶ my ($self) = @_;
Subroutine disembowel: called CC=1 SCAR=0.0
Pod undocumented
124 7 $self->guts(1);
125 }
126  
127 sub replace_original {
128 2 ▶ require HTML::TreeBuilder::XPath;
Subroutine replace_original: called CC=2 SCAR=6.9
Pod undocumented
129  
130 2 my $orig = HTML::TreeBuilder::XPath->can('new');
131  
132 22 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
133 *HTML::TreeBuilder::XPath::new = sub {
134 2 ▶ HTML::TreeBuilder::LibXML->new();
Subroutine __ANON__: called CC=1 SCAR=0.0
135 2 };
136  
137 2 ▶ if (defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) 1 1 2
138 return HTML::TreeBuilder::LibXML::Destructor->new(
139 1 ▶ sub { *HTML::TreeBuilder::XPath::new = $orig } );
Subroutine __ANON__: called CC=1 SCAR=0.0
140 }
141 1 return;
142 }
143  
144 # The HTML::TreeBuilder has this method and it is needed to us for web-scraper module
145 ▶sub store_comments { }
Subroutine store_comments: called CC=1 SCAR=0.0
Pod undocumented
146 ▶sub ignore_unknown { }
Subroutine ignore_unknown: not called CC=1 SCAR=0.0
Pod undocumented
147  
148 package # hide from cpan
149 HTML::TreeBuilder::LibXML::Destructor;
150  
151 sub new {
152 1 ▶ my ( $class, $callback ) = @_;
Subroutine new: called CC=1 SCAR=0.0
153 1 bless { cb => $callback }, $class;
154 }
155  
156 sub DESTROY {
157 1 ▶ my $self = shift;
Subroutine DESTROY: called CC=1 SCAR=0.0
158 1 $self->{cb}->();
159 }
160  
161 22 1;
162 __END__
↑ Summary blib/lib/HTML/TreeBuilder/LibXML/Node.pm »