lib/XHTML/Util.pm

filter: Statementstmt 89.1% 263 / 295 Branchbran 79.8% 107 / 134 Conditioncond 65.7% 23 / 35 MC/DCmcdc 59.0% 13 / 22 Subroutinesub 83.6% 41 / 49 Podpod 100% 22 / 22 totaltotal 84.2% 469 / 557 CC 95 SCAR 24.6
CC
95
Cov
85%
_enpara_this_nodes_content
32.2
traverse
20.0
new
13.9
CRAP 11.7
↑ Summary
1 package XHTML::Util;
2 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 14 ▶no warnings "uninitialized";
Subroutine BEGIN: called CC=1 SCAR=0.0
5 14 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 14 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 14 ▶use HTML::Tagset 3.02 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 14 ▶use HTML::Entities qw( encode_entities decode_entities );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 14 ▶use HTML::Selector::XPath ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 14 ▶use HTML::DTD;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 14 ▶use Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 14 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 14 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 14 ▶use HTML::TokeParser::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 14 ▶use XML::Normalize::LibXML qw( xml_normalize );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 14 ▶use LWP::Simple ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 14 ▶use overload q{""} => sub { +shift->as_string }, fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
18  
19 14 our $VERSION = "0.99_08";
20 14 our $AUTHORITY = 'cpan:ASHLEY';
21 14 our $TITLE_ATTR = join("/", __PACKAGE__, $VERSION);
22  
23 14 our $FRAGMENT_SELECTOR = "div[title='$TITLE_ATTR']";
24 14 our $FRAGMENT_XPATH = HTML::Selector::XPath::selector_to_xpath($FRAGMENT_SELECTOR);
25  
26 14 my $isKnown = { %HTML::Tagset::isKnown }; # We modify this one.
27 14 my $emptyElement = \%HTML::Tagset::emptyElement;
28 14 my $isBodyElement = \%HTML::Tagset::isBodyElement;
29 14 my $isPhraseMarkup = \%HTML::Tagset::isPhraseMarkup;
30 #my $canTighten = \%HTML::Tagset::canTighten;
31 #my $isHeadElement = \%HTML::Tagset::isHeadElement;
32 #my $isHeadOrBodyElement = \%HTML::Tagset::isHeadOrBodyElement;
33 #my $isList = \%HTML::Tagset::isList;
34 #my $isTableElement = \%HTML::Tagset::isTableElement;
35 14 my $isFormElement = \%HTML::Tagset::isFormElement;
36 #my $p_closure_barriers = \@HTML::Tagset::p_closure_barriers;
37  
38 # Accommodate HTML::TokeParser's idea of a "tag."
39 14 $isKnown->{"$_/"} = 1 for keys %{$emptyElement};
40 700 my $isBlockLevel = { map {; $_ => 1 }
41 1302 ▶ grep { ! ( $isPhraseMarkup->{$_} || $isFormElement->{$_} ) }
Condition100%3/3
$isPhraseMarkup->{$_} || $isFormElement->{$_}
l!l&&r!l&&!r
50498700
MC/DC100%2/2
$isPhraseMarkup->{$_} || $isFormElement->{$_}
$isPhraseMarkup->{$_} $isFormElement->{$_}
Truth table100%3/3
A || B
A= $isPhraseMarkup->{$_}B= $isFormElement->{$_}
ABresult
1X1
011
000
42 14 keys %{$isBodyElement}
43 };
44 # use YAML; die YAML::Dump($isBlockLevel);
45  
46 2 ▶sub tags {
Subroutine tags: called CC=1 SCAR=0.0
Pod documented
47 2 grep { ! /\W/ }
48 sort keys %HTML::Tagset::isKnown;
49 }
50  
51 26 ▶sub new {
Subroutine new: called CC=9 SCAR=26.3
Pod documented
52 26 my $class = shift;
53 26 ▶ my $arg = shift or croak "new requires an argument";
Branch: 1 branch
BranchTrueFalseTotal
my $arg = shift() or croak("new requires an argument") 25 1 26
54 25 my $self = bless {}, $class;
55  
56 25 ▶ if ( ref($arg) eq "SCALAR" )
Branch: 6 branches
BranchTrueFalseTotal
if (ref $arg eq 'SCALAR') { } 21 4 25
elsif (builtin::blessed($arg) eq 'Path::Class::File') { } 0 4 4
elsif (builtin::blessed($arg) eq __PACKAGE__) { } 0 4 4
elsif (builtin::blessed($arg) =~ /\AURI::https?/) { } 0 4 4
elsif (builtin::blessed($arg) and $arg->can('getlines')) { } 0 4 4
elsif (ref $arg eq 'GLOB') { } 0 4 4
57 {
58 21 $self->_parse( $$arg );
59 }
60 elsif ( blessed($arg) eq "Path::Class::File" )
61 {
62 0 $self->_parse( scalar $arg->slurp );
63 }
64 elsif ( blessed($arg) eq __PACKAGE__ )
65 {
66 0 $self->_parse( $arg->as_string ); # Cloning.
67 }
68 elsif ( blessed($arg) =~ /\AURI::https?/ )
69 {
70 0 $self->_parse( LWP::Simple::get($arg) );
71 }
72 ▶ elsif ( blessed($arg) and $arg->can("getlines") )
Condition33%1/3
builtin::blessed($arg) and $arg->can("getlines")
!ll&&!rl&&r
400
MC/DC0%0/2
builtin::blessed($arg) and $arg->can("getlines")
builtin::blessed($arg) $arg->can("getlines")
Truth table33%1/3
A and B
A= builtin::blessed($arg)B= $arg->can("getlines")
ABresult
0X0
100
111
73 {
74 0 $self->_parse( join("", $arg->getlines) );
75 }
76 elsif ( ref($arg) eq 'GLOB' )
77 {
78 # $self->_parse( scalar Path::Class::File->new($arg)->slurp );
79 }
80 else
81 {
82 4 $self->_parse( scalar Path::Class::File->new($arg)->slurp );
83 }
84 25 $self;
85 }
86  
87 42 ▶sub debug {
Subroutine debug: called CC=3 SCAR=11.0
Pod documented
88 42 my $self = shift;
89 42 ▶ $self->{_debug} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 41 42
90 42 ▶ $self->{_debug} || 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_debug"} or 0 8 34 42
91 }
92  
93 48 ▶sub as_string {
Subroutine as_string: called CC=7 SCAR=20.4
Pod documented
94 48 my $self = shift;
95 48 ▶ my @args = @_ ? @_ : ( 1, "UTF-8" );
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 48 48
96 48 ▶ if ( $self->is_document )
Branch: 2 branches
BranchTrueFalseTotal
if ($self->is_document) { } 3 45 48
elsif ($self->is_fragment) { } 45 0 45
97 {
98 3 return _trim( Encode::decode_utf8( $self->doc->serialize(@args) ) );
99 }
100 elsif ( $self->is_fragment )
101 {
102 45 ▶ croak "No root in document\n", $self->doc->serialize
Branch: 1 branch
BranchTrueFalseTotal
unless $self->root 0 45 45
103 unless $self->root;
104  
105 45 my ( $fragment ) = $self->root->findnodes($FRAGMENT_XPATH);
106  
107 45 ▶ croak "No fragment...?\n", $self->doc->serialize
Branch: 1 branch
BranchTrueFalseTotal
unless $fragment 0 45 45
108 unless $fragment;
109  
110 45 my $out = "";
111 45 $out .= $_->serialize(@args) for $fragment->childNodes;
112  
113 45 return Encode::decode_utf8( _trim($out) );
114 }
115 else
116 {
117 0 die "No type was found, internal issue :(";
118 }
119 }
120  
121 48 ▶sub is_document {
Subroutine is_document: called CC=1 SCAR=0.0
Pod documented
122 48 +shift->{_type} eq "document";
123 }
124  
125 56 ▶sub is_fragment {
Subroutine is_fragment: called CC=1 SCAR=0.0
Pod documented
126 56 +shift->{_type} eq "fragment";
127 }
128  
129 25 ▶sub _parse {
Subroutine _parse: called CC=2 SCAR=6.9
130 25 my $self = shift;
131 $self->{_sanitized} =
132 25 $self->_sanitize( $self->{_original_string} = shift );
133  
134 25 ▶ if ( $self->{_original_string} =~ /\A(?:<\W[^>]+>|\s+)*<html/i )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_original_string'} =~ /\A(?:<\W[^>]+>|\s+)*<html/i) { } 3 22 25
135 {
136 3 $self->{_type} = "document";
137 3 $self->{_doc} = $self->parser->parse_html_string($self->{_sanitized});
138 # Special case, doc contains ONLY 1 p and its first and last
139 # child of body then we should replace it with the FRAGMENT
140 # holder div.
141 }
142 else
143 {
144 # SHOULD we sanitize first?
145 22 $self->{_type} = "fragment";
146  
147 $self->{_doc} = $self->parser
148 ->parse_html_string(join("\n",
149 "<html><head><title></title></head><body>",
150 sprintf('<div title="%s">',
151 $TITLE_ATTR
152 ),
153 $self->{_sanitized},
154 #Encode::encode_utf8($self->{_sanitized}),
155 22 '</div></body></html>')
156 );
157 }
158  
159 25 $self->root->normalize;
160 25 $self->doc;
161 }
162  
163 130 ▶sub root {
Subroutine root: called CC=1 SCAR=0.0
Pod documented
164 130 +shift->doc->getDocumentElement;
165 }
166  
167 202 ▶sub doc {
Subroutine doc: called CC=1 SCAR=0.0
Pod documented
168 202 +shift->{_doc};
169 }
170  
171 0 ▶sub text {
Subroutine text: not called CC=1 SCAR=6.9
Pod documented
172 0 +shift->doc->getDocumentElement->textContent;
173 }
174  
175 29 ▶sub parser {
Subroutine parser: called CC=2 SCAR=6.9
Pod documented
176 29 my $self = shift;
177 29 ▶ return $self->{_parser} if $self->{_parser};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_parser"} 4 25 29
178 25 $self->{_parser} = XML::LibXML->new;
179 25 $self->{_parser}->recover_silently(1);
180 25 $self->{_parser}->keep_blanks(1);
181 25 $self->{_parser};
182 }
183  
184 10 ▶sub is_valid {
Subroutine is_valid: called CC=4 SCAR=14.0
Pod documented
185 10 my $self = shift;
186 10 ▶ return 1 if $self->doc->is_valid;
Branch: 1 branch
BranchTrueFalseTotal
if $self->doc->is_valid 0 10 10
187 # 321 debug about which DTD is being used.
188 10 ▶ my $dtd_name = shift || "xhtml1-transitional";
Condition50%1/2
shift() || 'xhtml1-transitional'
l!l
010
MC/DC0%0/1
shift() || 'xhtml1-transitional'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
189 10 my $dtd_string = HTML::DTD->get_dtd("$dtd_name.dtd");
190 10 $self->{_dtd} = XML::LibXML::Dtd->parse_string($dtd_string);
191 10 ▶ return $self->doc->is_valid($self->{_dtd}) ? $self : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->doc->is_valid($self->{'_dtd'}) ? : 3 7 10
192 }
193  
194 3 ▶sub validate {
Subroutine validate: called CC=2 SCAR=6.9
Pod documented
195 3 my $self = shift;
196 3 ▶ return 1 if $self->is_valid;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_valid 1 2 3
197 2 return $self->doc->validate($self->{_dtd});
198 }
199  
200 0 ▶sub _original_string {
Subroutine _original_string: not called CC=2 SCAR=17.9
201 0 my $self = shift;
202 0 ▶ $self->{_original_string} ||= shift;
Condition0%0/3
$self->{"_original_string"} ||= shift()
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"_original_string"} ||= shift()
$self->{"_original_string"} shift()
Truth table0%0/3
A ||= B
A= $self->{"_original_string"}B= shift()
ABresult
1X1
011
000
203 # $self->{_original_string} ||= Encode::encode_utf8( shift ); #321
204 0 $self->{_original_string};
205 }
206  
207 13 ▶sub _return {
Subroutine _return: called CC=2 SCAR=6.9
208 13 my $self = shift; # 321 ARGS for serialize.
209 13 xml_normalize( $self->doc );
210 13 my $callers_wantarray = [ caller(1) ]->[5];
211 13 ▶ return unless defined $callers_wantarray; # Void context.
Branch: 1 branch
BranchTrueFalseTotal
unless defined $callers_wantarray 2 11 13
212 11 return $self; # Should always return self?
213 }
214  
215 3 ▶sub fix {
Subroutine fix: called CC=5 SCAR=16.1
Pod documented
216 3 my $self = shift;
217 3 ▶ return $self->_return if $self->is_valid;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_valid 1 2 3
218  
219 2 for my $fixable ( qw( img ) )
220 {
221 2 my $method = "_fix_$fixable";
222 2 for my $node ( $self->root->findnodes("//$fixable") )
223 {
224 5 $self->$method($node);
225 }
226 }
227  
228 $self->is_valid()
229 2 ▶ or carp "Could not fix the problems with this document";
Branch: 1 branch
BranchTrueFalseTotal
$self->is_valid or carp("Could not fix the problems with this document") 1 1 2
230 2 $self->validate();
231 1 $self->_return;
232 }
233  
234 25 ▶sub _sanitize {
Subroutine _sanitize: called CC=11 SCAR=24.0
235 25 my $self = shift;
236 25 ▶ my $fragment = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $fragment = shift() or return 25 0 25
237 #$fragment = Encode::decode_utf8($fragment);
238 25 my $p = HTML::TokeParser::Simple->new(\$fragment);
239 25 my $renew = "";
240 25 my $in_body = 0;
241 TOKEN:
242 25 ▶ while ( my $token = $p->get_token )
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = $p->get_token) 638 25 663
243 {
244 #warn sprintf("%10s %10s %s\n", $token->[-1], $token->get_tag, blessed($token));
245 #no warnings "uninitialized";
246 638 ▶ if ( $isKnown->{$token->get_tag} )
Branch: 2 branches
BranchTrueFalseTotal
if ($$isKnown{$token->get_tag}) { } 351 287 638
elsif ($token->is_declaration or $token->is_pi) { } 6 281 287
247 {
248 351 ▶ if ( $token->is_start_tag )
Branch: 1 branch
BranchTrueFalseTotal
if ($token->is_start_tag) { } 195 156 351
249 {
250 195 my @pair;
251 195 for my $attr ( @{ $token->get_attrseq } )
252 {
253 237 ▶ next if $attr eq "/";
Branch: 1 branch
BranchTrueFalseTotal
if $attr eq "/" 30 207 237
254 207 my $value = encode_entities(decode_entities($token->get_attr($attr)));
255 207 push @pair, join("=",
256 $attr,
257 qq{"$value"});
258 }
259 195 $renew .= "<" . join(" ", $token->get_tag, @pair);
260 195 ▶ $renew .= ( $token->get_attr("/") || $emptyElement->{$token->get_tag} ) ? "/>" : ">";
Branch: 1 branch
BranchTrueFalseTotal
$token->get_attr('/') || $$emptyElement{$token->get_tag} ? : 33 162 195
Condition100%3/3
$token->get_attr("/") or $$emptyElement{$token->get_tag}
l!l&&r!l&&!r
303162
MC/DC100%2/2
$token->get_attr("/") or $$emptyElement{$token->get_tag}
$token->get_attr("/") $$emptyElement{$token->get_tag}
Truth table100%3/3
A or B
A= $token->get_attr("/")B= $$emptyElement{$token->get_tag}
ABresult
1X1
011
000
261 }
262 else
263 {
264 156 $renew .= $token->as_is;
265 }
266 }
267 ▶ elsif ( $token->is_declaration or $token->is_pi )
Condition100%3/3
$token->is_declaration or $token->is_pi
l!l&&r!l&&!r
33281
MC/DC100%2/2
$token->is_declaration or $token->is_pi
$token->is_declaration $token->is_pi
Truth table100%3/3
A or B
A= $token->is_declarationB= $token->is_pi
ABresult
1X1
011
000
268 {
269 6 $renew .= $token->as_is;
270 }
271 else
272 {
273 281 $renew .= encode_entities(decode_entities($token->as_is),'<>"&');
274 }
275 }
276 25 return $renew;
277 }
278  
279 0 ▶sub body {
Subroutine body: not called CC=1 SCAR=6.9
Pod documented
280 0 [ shift->doc->findnodes("//body") ]->[0];
281 }
282  
283 0 ▶sub head {
Subroutine head: not called CC=1 SCAR=6.9
Pod documented
284 0 [ shift->doc->findnodes("//head") ]->[0];
285 }
286  
287 0 ▶sub as_fragment {
Subroutine as_fragment: not called CC=3 SCAR=24.8
Pod documented
288 0 my $self = shift;
289 0 my ( $fragment ) = $self->doc->findnodes($FRAGMENT_XPATH);
290 0 ▶ $fragment ||= $self->body;
Condition0%0/3
$fragment ||= $self->body
l!l&&r!l&&!r
000
MC/DC0%0/2
$fragment ||= $self->body
$fragment $self->body
Truth table0%0/3
A ||= B
A= $fragmentB= $self->body
ABresult
1X1
011
000
291 0 my $out = "";
292 0 $out .= $_->serialize(1,"UTF-8") for $fragment->childNodes;
293 0 return $out;
294 }
295  
296 5 ▶sub _make_selector {
Subroutine _make_selector: called CC=5 SCAR=16.2
297 5 my $self = shift;
298 5 my $selector = shift;
299 5 ▶ unless ( $selector )
Branch: 1 branch
BranchTrueFalseTotal
unless ($selector) 4 1 5
300 {
301 4 ▶ my $base = $self->is_fragment ? $FRAGMENT_SELECTOR : "body";
Branch: 1 branch
BranchTrueFalseTotal
$self->is_fragment ? : 4 0 4
302 4 $selector = "$base, $base *";
303 }
304 5 ▶ warn "Selector: $selector" if $self->debug > 2;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug > 2 1 4 5
305 5 ▶ $selector =~ m,\A/, ?
Branch: 1 branch
BranchTrueFalseTotal
$selector =~ m[\A/] ? : 0 5 5
306 $selector :
307 HTML::Selector::XPath::selector_to_xpath($selector);
308 }
309  
310 0 ▶sub traverse {
Subroutine traverse: not called CC=4 SCAR=30.0
Pod documented
311 0 my $self = shift;
312 0 ▶ my $xpath = $self->_make_selector(+shift) if @_ == 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 2 0 0 0
313 0 my $code = shift;
314  
315 0 ▶ if ( $xpath )
Branch: 1 branch
BranchTrueFalseTotal
if ($xpath) { } 0 0 0
316 {
317 0 for my $node ( $self->root->findnodes("$xpath") )
318 {
319 0 $code->($node);
320 }
321 }
322 else
323 {
324 0 $code->($self->root);
325 }
326 0 $self->_return;
327 }
328  
329  
330 5 ▶sub enpara {
Subroutine enpara: called CC=6 SCAR=17.9
Pod documented
331 5 my $self = shift;
332 5 my $xpath = $self->_make_selector(+shift);
333 5 my $root = $self->root;
334 5 my $doc = $self->doc;
335  
336 NODE:
337 5 for my $designated_enpara ( $root->findnodes("$xpath") )
338 {
339 # warn "FOUND ", $designated_enpara->nodeName, $/;
340 # warn "*********", $designated_enpara->toString if $self->debug > 2;
341 29 ▶ next unless $designated_enpara->nodeType == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $designated_enpara->nodeType == 1 0 29 29
342 29 ▶ next NODE if $designated_enpara->nodeName eq 'p';
Branch: 1 branch
BranchTrueFalseTotal
if $designated_enpara->nodeName eq "p" 5 24 29
343 24 ▶ if ( $designated_enpara->nodeName eq 'pre' ) # I don't think so, honky.
Branch: 1 branch
BranchTrueFalseTotal
if ($designated_enpara->nodeName eq "pre") 1 23 24
344 {
345 # Expand or leave it alone? or ->validate it...?
346 1 carp "It makes no sense to enpara within a <pre/>; skipping";
347 1 next NODE;
348 }
349 23 ▶ next unless $isBlockLevel->{$designated_enpara->nodeName};
Branch: 1 branch
BranchTrueFalseTotal
unless $$isBlockLevel{$designated_enpara->nodeName} 4 19 23
350  
351 19 $self->_enpara_this_nodes_content($designated_enpara, $doc);
352 }
353 5 $self->_enpara_this_nodes_content($root, $doc);
354 5 $self->_return;
355 }
356  
357 24 ▶sub _enpara_this_nodes_content {
Subroutine _enpara_this_nodes_content: called CC=32 SCAR=34.7
358 24 my ( $self, $parent, $doc ) = @_;
359 24 my $lastChild = $parent->lastChild;
360 24 my @naked_block;
361 24 for my $node ( $parent->childNodes )
362 {
363 62 ▶ if ( $isBlockLevel->{$node->nodeName}
Branch: 3 branches
BranchTrueFalseTotal
if ($$isBlockLevel{$node->nodeName} or $node->nodeName eq 'a' and grep {$_->nodeName eq 'img';} $node->childNodes) { } 20 42 62
elsif ($node->nodeType == 3 and $node->nodeValue =~ /(?:[^\S\n]*\n){2,}/) { } 13 29 42
elsif (not $node->nodeName =~ /\Ahead|body\z/) { } 19 10 29
Condition50%3/6
$$isBlockLevel{$node->nodeName} or $node->nodeName eq "a" and grep {$_->nodeName eq "img";} $node->childNodes
l!l&&r!l&&!r
20042
$node->nodeName eq "a" and grep {$_->nodeName eq "img";} $node->childNodes
!ll&&!rl&&r
4200
MC/DC33%1/3
$$isBlockLevel{$node->nodeName} or $node->nodeName eq "a" and grep {$_->nodeName eq "img";} $node->childNodes
$$isBlockLevel{$node->nodeName} $node->nodeName eq "a" grep {$_->nodeName eq "img";} $node->childNodes
Truth table50%2/4
A or B and C
A= $$isBlockLevel{$node->nodeName}B= $node->nodeName eq "a"C= grep {$_->nodeName eq "img";} $node->childNodes
ABCresult
1XX1
0111
00X0
0100
364 or
365 $node->nodeName eq "a" # special case block level, so IGNORE
366 and
367 0 grep { $_->nodeName eq "img" } $node->childNodes
368 )
369 {
370 20 ▶ next unless @naked_block; # nothing to enblock
Branch: 1 branch
BranchTrueFalseTotal
unless @naked_block 15 5 20
371 5 my $p = $doc->createElement("p");
372 5 $p->setAttribute("enpara","enpara");
373 5 ▶ $p->setAttribute("line",__LINE__) if $self->debug > 4;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug > 4 0 5 5
374 5 $p->appendChild($_) for @naked_block;
375 5 ▶ $parent->insertBefore( $p, $node )
Branch: 1 branch
BranchTrueFalseTotal
if $p->textContent =~ /\S/ 2 3 5
376 if $p->textContent =~ /\S/;
377 5 @naked_block = ();
378 }
379 ▶ elsif ( $node->nodeType == 3
Condition100%3/3
$node->nodeType == 3 and $node->nodeValue =~ /(?:[^\S\n]*\n){2,}/
!ll&&!rl&&r
141513
MC/DC100%2/2
$node->nodeType == 3 and $node->nodeValue =~ /(?:[^\S\n]*\n){2,}/
$node->nodeType == 3 $node->nodeValue =~ /(?:[^\S\n]*\n){2,}/
Truth table100%3/3
A and B
A= $node->nodeType == 3B= $node->nodeValue =~ /(?:[^\S\n]*\n){2,}/
ABresult
0X0
100
111
380 and
381 $node->nodeValue =~ /(?:[^\S\n]*\n){2,}/
382 )
383 {
384 13 my $text = $node->nodeValue;
385 13 my @text_part = map { $doc->createTextNode($_) }
386 split /([^\S\n]*\n){2,}/, $text;
387  
388 13 my @new_node;
389 13 ▶ for ( my $x = 0; $x < @text_part; $x++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($x < @text_part) 43 13 56
390 {
391 43 ▶ if ( $text_part[$x]->nodeValue =~ /\S/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($text_part[$x]->nodeValue =~ /\S/) { } 15 28 43
392 {
393 15 push @naked_block, $text_part[$x];
394 }
395 else # it's a blank newline node so _STOP_
396 {
397 28 ▶ next unless @naked_block;
Branch: 1 branch
BranchTrueFalseTotal
unless @naked_block 14 14 28
398 14 my $p = $doc->createElement("p");
399 14 $p->setAttribute("enpara","enpara");
400 14 ▶ $p->setAttribute("line",__LINE__) if $self->debug > 4;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug > 4 0 14 14
401 14 $p->appendChild($_) for @naked_block;
402 14 @naked_block = ();
403 14 push @new_node, $p;
404 }
405 }
406 13 ▶ if ( @new_node )
Branch: 1 branch
BranchTrueFalseTotal
if (@new_node) 8 5 13
407 {
408 8 $parent->insertAfter($new_node[0], $node);
409 8 ▶ for ( my $x = 1; $x < @new_node; $x++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($x < @new_node) 6 8 14
410 {
411 6 $parent->insertAfter($new_node[$x], $new_node[$x-1]);
412 }
413 }
414 13 $node->unbindNode;
415 }
416 elsif ( $node->nodeName !~ /\Ahead|body\z/ ) # Hack? Fix real reason? 321
417 {
418 19 push @naked_block, $node; # if $node->nodeValue =~ /\S/;
419 }
420  
421 47 ▶ if ( $node->isSameNode( $lastChild )
Branch: 1 branch
BranchTrueFalseTotal
if ($node->isSameNode($lastChild) and @naked_block) 11 36 47
Condition100%3/3
$node->isSameNode($lastChild) and @naked_block
!ll&&!rl&&r
29711
MC/DC100%2/2
$node->isSameNode($lastChild) and @naked_block
$node->isSameNode($lastChild) @naked_block
Truth table100%3/3
A and B
A= $node->isSameNode($lastChild)B= @naked_block
ABresult
0X0
100
111
422 and @naked_block )
423 {
424 11 my $p = $doc->createElement("p");
425 11 $p->setAttribute("enpara","enpara");
426 11 ▶ $p->setAttribute("line",__LINE__) if $self->debug > 4;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug > 4 0 11 11
427 11 $p->appendChild($_) for ( @naked_block );
428 11 ▶ $parent->appendChild($p) if $p->textContent =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
if $p->textContent =~ /\S/ 5 6 11
429 }
430 }
431  
432 24 my $newline = $doc->createTextNode("\n");
433 24 my $br = $doc->createElement("br");
434  
435 24 for my $p ( $parent->findnodes('//p[@enpara="enpara"]') )
436 {
437 21 $p->removeAttribute("enpara");
438 21 $parent->insertBefore( $newline->cloneNode, $p );
439 21 $parent->insertAfter( $newline->cloneNode, $p );
440  
441 21 my $frag = $doc->createDocumentFragment();
442  
443 21 my @kids = $p->childNodes();
444 21 ▶ for ( my $i = 0; $i < @kids; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @kids) 25 21 46
445 {
446 25 my $kid = $kids[$i];
447 25 ▶ next unless $kid->nodeName eq "#text";
Branch: 1 branch
BranchTrueFalseTotal
unless $kid->nodeName eq "#text" 4 21 25
448 21 my $text = $kid->nodeValue;
449 21 ▶ $text =~ s/\A\r?\n// if $i == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $i == 0 19 2 21
450 21 ▶ $text =~ s/\r?\n\z// if $i == $#kids;
Branch: 1 branch
BranchTrueFalseTotal
if $i == $#kids 19 2 21
451  
452 21 my @lines = map { $doc->createTextNode($_) }
453 split /(\r?\n)/, $text;
454  
455 21 ▶ for ( my $i = 0; $i < @lines; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @lines) 33 21 54
456 {
457 33 $frag->appendChild($lines[$i]);
458 33 ▶ unless ( $i == $#lines
Branch: 1 branch
BranchTrueFalseTotal
unless ($i == $#lines or $lines[$i]->nodeValue =~ /\A\r?\n\z/) 6 27 33
Condition100%3/3
$i == $#lines or $lines[$i]->nodeValue =~ /\A\r?\n\z/
l!l&&r!l&&!r
2166
MC/DC100%2/2
$i == $#lines or $lines[$i]->nodeValue =~ /\A\r?\n\z/
$i == $#lines $lines[$i]->nodeValue =~ /\A\r?\n\z/
Truth table100%3/3
A or B
A= $i == $#linesB= $lines[$i]->nodeValue =~ /\A\r?\n\z/
ABresult
1X1
011
000
459 or
460 $lines[$i]->nodeValue =~ /\A\r?\n\z/ )
461 {
462 6 $frag->appendChild($br->cloneNode);
463 }
464 }
465 21 $kid->replaceNode($frag);
466 }
467 }
468 }
469  
470 56 ▶sub _trim {
Subroutine _trim: called CC=3 SCAR=11.4
471 56 s/\A\s+|\s+\z//g for @_;
472 56 ▶ wantarray ? @_ : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 56 56
473 }
474  
475 5 ▶sub _fix_img {
Subroutine _fix_img: called CC=3 SCAR=11.3
476 5 my ( $self, $img ) = @_;
477 5 ▶ unless ( $img->hasAttribute("src") )
Branch: 1 branch
BranchTrueFalseTotal
unless ($img->hasAttribute("src")) 0 5 5
478 {
479 0 croak "There is no way to fix an image without a source";
480 }
481 5 ▶ unless ( $img->hasAttribute("alt") )
Branch: 1 branch
BranchTrueFalseTotal
unless ($img->hasAttribute("alt")) 1 4 5
482 {
483 1 $img->setAttribute("alt", $img->getAttribute("src"));
484 }
485 }
486  
487 0 ▶sub _fix_center {
Subroutine _fix_center: not called CC=1 SCAR=6.9
488 0 my ( $self, $center ) = @_;
489 # <center> --> <div style="text-align:center">
490 0 die "Unimplemented";
491 }
492  
493 6 ▶sub _make_selector_xpath {
Subroutine _make_selector_xpath: called CC=3 SCAR=11.2
494 6 my $self = shift;
495 6 my $selector = shift;
496 6 ▶ my $base = $self->is_fragment ? $FRAGMENT_SELECTOR : "body";
Branch: 1 branch
BranchTrueFalseTotal
$self->is_fragment ? : 6 0 6
497 6 my $xpath = HTML::Selector::XPath::selector_to_xpath("$base $selector");
498 6 ▶ warn "XPATH: $xpath\n" if $self->debug >= 5;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug >= 5 0 6 6
499 6 return $xpath;
500 }
501  
502 4 ▶sub remove {
Subroutine remove: called CC=2 SCAR=6.9
Pod documented
503 4 my $self = shift;
504 4 my $xpath = $self->_make_selector_xpath(@_);
505 4 for my $node ( $self->root->findnodes($xpath) )
506 {
507 5 $node->parentNode->removeChild($node);
508 }
509 4 $self->_return;
510 }
511  
512 2 ▶sub strip_tags {
Subroutine strip_tags: called CC=3 SCAR=11.0
Pod documented
513 2 my $self = shift;
514 2 my $xpath = $self->_make_selector_xpath(@_);
515  
516 2 for my $node ( $self->root->findnodes($xpath) )
517 {
518 2 my $fragment = $self->doc->createDocumentFragment;
519 2 for my $n ( $node->childNodes )
520 {
521 2 $fragment->appendChild($n);
522 }
523 2 $node->replaceNode($fragment);
524 }
525 2 $self->_return;
526 }
527  
528 1 ▶sub same_same {
Subroutine same_same: called CC=3 SCAR=11.1
Pod documented
529 1 my $self = shift;
530 1 my $other = shift;
531 1 ▶ my $self2 = blessed($other) eq __PACKAGE__ ?
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($other) eq __PACKAGE__ ? : 1 0 1
532 $other : __PACKAGE__->new($other);
533  
534 1 $self->parser->keep_blanks(0);
535  
536 1 my $one = $self->parser->parse_string($self->root->serialize(0))->serialize(0);
537 1 my $two = $self->parser->parse_string($self2->root->serialize(0))->serialize(0);
538  
539 1 $self->parser->keep_blanks(1);
540  
541 1 ▶ $one eq $two or die "$one\n\n$two"
Branch: 1 branch
BranchTrueFalseTotal
$one eq $two or die "$one\n\n$two" 1 0 1
542 }
543  
544 0 ▶sub clone {
Subroutine clone: not called CC=1 SCAR=6.9
Pod documented
545 0 my $self = shift;
546 0 my $class = blessed($self);
547 0 $class->new($self);
548 }
549  
550 14 1;
551  
552 __END__
↑ Summary