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 LibXML/Node.pm »
1 package HTML::TreeBuilder::LibXML;
2 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 23 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 23 our $VERSION = '0.28';
5 23 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 23 ▶use base 'HTML::TreeBuilder::LibXML::Node';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 23 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 23 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 44 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
11 44 my $class = shift;
12 44 bless {}, $class;
13 }
14  
15 29 ▶sub new_from_content {
Subroutine new_from_content: called CC=2 SCAR=6.9
Pod undocumented
16 29 my $class = shift;
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 1 ▶sub new_from_file {
Subroutine new_from_file: called CC=1 SCAR=0.0
Pod undocumented
27 1 my $class = shift;
28 1 my $self = $class->new;
29 1 $self->parse_file(@_);
30 1 return $self;
31 }
32  
33 23 my $PARSER;
34 43 ▶sub _parser {
Subroutine _parser: called CC=2 SCAR=6.9
35 43 ▶ unless ($PARSER) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($PARSER) 19 24 43
36 19 $PARSER = XML::LibXML->new();
37 19 $PARSER->recover(1);
38 19 $PARSER->recover_silently(1);
39 19 $PARSER->keep_blanks(1);
40 19 $PARSER->expand_entities(1);
41 19 $PARSER->no_network(1);
42 }
43 43 $PARSER;
44 }
45  
46 45 ▶sub parse {
Subroutine parse: called CC=1 SCAR=0.0
Pod undocumented
47 45 my ($self, $html) = @_;
48 45 $self->{_content} .= $html;
49 }
50  
51 1 ▶sub parse_content {
Subroutine parse_content: called CC=1 SCAR=0.0
Pod undocumented
52 1 my $self = shift;
53 1 $self->parse($_[0]);
54 1 $self->eof;
55 }
56  
57 1 ▶sub parse_file {
Subroutine parse_file: called CC=2 SCAR=7.0
Pod undocumented
58 1 my $self = shift;
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" 1 0 1
60 1 my $content = do { local $/; <$fh> };
61 1 $self->parse_content($content);
62 }
63  
64 43 ▶sub eof {
Subroutine eof: called CC=5 SCAR=16.1
Pod undocumented
65 43 my ($self, ) = @_;
66 43 ▶ $self->{_content} = ' ' if defined $self->{_content} && $self->{_content} eq ''; # HACK
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_content"} and $self->{"_content"} eq "" 1 42 43
Condition66%2/3
defined $self->{"_content"} and $self->{"_content"} eq ""
!ll&&!rl&&r
0421
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 43 ▶ $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 33 10 43
68 43 ▶ $self->{_implicit_doctype} = 1 unless $self->{_content} =~ /<!DOCTYPE/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_content"} =~ /<!DOCTYPE/i 39 4 43
69 43 my $doc = $self->_parser->parse_html_string($self->{_content});
70 43 $self->{node} = $self->_documentElement($doc);
71 }
72  
73 43 ▶sub _documentElement {
Subroutine _documentElement: called CC=2 SCAR=7.0
74 43 my($self, $doc) = @_;
75 43 ▶ 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
4030
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 1 ▶sub elementify {
Subroutine elementify: called CC=1 SCAR=0.0
Pod undocumented
83 1 bless shift, 'HTML::TreeBuilder::LibXML::Node';
84 }
85  
86 27 ▶sub guts {
Subroutine guts: called CC=13 SCAR=26.1
Pod undocumented
87 27 my ($self, $destructive) = @_;
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 7 ▶sub disembowel {
Subroutine disembowel: called CC=1 SCAR=0.0
Pod undocumented
123 7 my ($self) = @_;
124 7 $self->guts(1);
125 }
126  
127 2 ▶sub replace_original {
Subroutine replace_original: called CC=2 SCAR=6.9
Pod undocumented
128 2 require HTML::TreeBuilder::XPath;
129  
130 2 my $orig = HTML::TreeBuilder::XPath->can('new');
131  
132 23 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
133 2 ▶ *HTML::TreeBuilder::XPath::new = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
134 2 HTML::TreeBuilder::LibXML->new();
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 1 ▶sub store_comments { }
Subroutine store_comments: called CC=1 SCAR=0.0
Pod undocumented
146 0 ▶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 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
152 1 my ( $class, $callback ) = @_;
153 1 bless { cb => $callback }, $class;
154 }
155  
156 1 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
157 1 my $self = shift;
158 1 $self->{cb}->();
159 }
160  
161 23 1;
162 __END__
↑ Summary LibXML/Node.pm »