blib/lib/HTML/ListScraper/Interactive.pm

filter: Statementstmt 97.7% 88 / 90 Branchbran 77.7% 28 / 36 Conditioncond 95.4% 21 / 22 MC/DCmcdc 92.8% 13 / 14 Subroutinesub 100% 9 / 9 Podpod 50.0% 2 / 4 totaltotal 92.0% 161 / 175 CC 30 SCAR 30.0
CC
30
Cov
93%
format_tags
27.4
canonicalize_tags
4.1
BEGIN
1.0
CRAP 20.1
↑ Summary scrape »
1 package HTML::ListScraper::Interactive;
2  
3 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 ▶use HTML::Entities;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 7 require Exporter;
9  
10 7 ▶use vars qw(@ISA @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 7 @ISA = qw(Exporter);
13 7 @EXPORT_OK = qw(format_tags canonicalize_tags);
14  
15 7 ▶use Class::Generate qw(class);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 7 class 'HTML::ListScraper::FormTag' => {
18 name => { type => '$', required => 1 },
19 index => { type => '$', required => 1, readonly => 1 },
20 link => { type => '$', required => 1, readonly => 1 },
21 text => { type => '$', required => 1, readonly => 1 },
22 '&close_name' => q{ $name .= '/'; }
23 };
24  
25 907 ▶sub is_opening {
Subroutine is_opening: called CC=1 SCAR=0.0
Pod undocumented
26 907 my $tag = shift;
27  
28 907 return $tag !~ m~\/~;
29 }
30  
31 907 ▶sub is_closing {
Subroutine is_closing: called CC=1 SCAR=0.0
Pod undocumented
32 907 my $tag = shift;
33  
34 907 return $tag =~ m~^\/~;
35 }
36  
37 53 ▶sub format_tags {
Subroutine format_tags: called CC=27 SCAR=33.1
Pod documented
38 53 my ($scraper, $tags, $incl) = @_;
39  
40 53 my $incl_attr;
41 my $incl_text;
42 53 my $incl_index;
43 53 ▶ if (ref($incl)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $incl) 50 3 53
44 50 $incl_attr = $incl->{attr};
45 50 $incl_text = $incl->{text};
46 50 $incl_index = $incl->{index};
47 }
48  
49 53 my @buffer;
50 my @stack;
51 53 foreach my $td (@$tags) {
52 960 my $name = $td->name;
53 960 my $tag = $name;
54 960 $tag =~ s~^\/~~;
55  
56 960 ▶ my $text = $td->text || '';
Condition100%2/2
$td->text || ''
l!l
560400
MC/DC100%1/1
$td->text || ''
$td->text
Truth table100%2/2
A
A= $td->text
Aresult
11
00
57 960 $text =~ s/[\s[:cntrl:]]+/ /g;
58  
59 960 ▶ my $link = $td->link || '';
Condition100%2/2
$td->link || ''
l!l
109851
MC/DC100%1/1
$td->link || ''
$td->link
Truth table100%2/2
A
A= $td->link
Aresult
11
00
60 960 $link =~ s/[\s[:cntrl:]]+//g;
61  
62 960 ▶ if ($name eq $tag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq $tag) { } 491 469 960
63 491 push @stack, [ $tag, scalar(@buffer) ];
64 491 push @buffer, HTML::ListScraper::FormTag->new(name => $name,
65 index => $td->index, link => $link, text => $text);
66 } else {
67 469 ▶ while (scalar(@stack) &&
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @stack and $stack[scalar(@stack) - 1][0] ne $tag) 20 469 489
Condition66%2/3
scalar @stack and $stack[scalar(@stack) - 1][0] ne $tag
!ll&&!rl&&r
046920
MC/DC50%1/2
scalar @stack and $stack[scalar(@stack) - 1][0] ne $tag
scalar @stack $stack[scalar(@stack) - 1][0] ne $tag
Truth table66%2/3
A and B
A= scalar @stackB= $stack[scalar(@stack) - 1][0] ne $tag
ABresult
0X0
100
111
68 ($stack[scalar(@stack) - 1]->[0] ne $tag)) {
69 20 ▶ if ($scraper->is_unclosed_tag(
Branch: 1 branch
BranchTrueFalseTotal
if ($scraper->is_unclosed_tag($stack[scalar(@stack) - 1][0])) { } 20 0 20
70 $stack[scalar(@stack) - 1]->[0])) {
71 20 my $pair = pop @stack;
72  
73 20 $buffer[$pair->[1]]->close_name();
74 } else {
75 0 last;
76 }
77 }
78  
79 469 ▶ if (scalar(@stack)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @stack) 469 0 469
80 469 pop @stack;
81 }
82  
83 469 push @buffer, HTML::ListScraper::FormTag->new(name => $name,
84 index => $td->index, link => $link, text => $text);
85 }
86 }
87  
88 53 ▶ while (scalar(@stack)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @stack) 2 53 55
89 2 my $pair = pop @stack;
90 2 $buffer[$pair->[1]]->close_name();
91 }
92  
93 53 my @out;
94 my $prev;
95 53 my $prev_index;
96 53 my $depth = 0;
97 53 foreach my $ft (@buffer) {
98 960 my $name = $ft->name;
99 960 ▶ if (defined($prev)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prev) 907 53 960
100 907 my $op = is_opening($prev);
101 907 my $cl = is_closing($name);
102 907 ▶ if ($op && !$cl) {
Branch: 2 branches
BranchTrueFalseTotal
if ($op and not $cl) { } 284 623 907
elsif (not $op and $cl) { } 284 524 808
Condition100%3/3
$op and not $cl
!ll&&!rl&&r
438185284
MC/DC100%2/2
$op and not $cl
$op not $cl
Truth table100%3/3
A and not(B)
A= $opB= not $cl
ABresult
0X0
100
111
103 284 ++$depth;
104 ▶ } elsif (!$op && $cl) {
Condition100%3/3
not $op and $cl
!ll&&!rl&&r
185154284
MC/DC100%2/2
not $op and $cl
not $op $cl
Truth table100%3/3
not(A) and B
A= not $opB= $cl
ABresult
0X0
100
111
105 284 ▶ if ($depth > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth > 0) 284 0 284
106 284 --$depth;
107 }
108 }
109 }
110  
111 960 my $indent = ' ' x (2 * $depth);
112  
113 960 my $attr = '';
114 960 ▶ if ($incl_attr && $ft->link) {
Branch: 1 branch
BranchTrueFalseTotal
if ($incl_attr and $ft->link) 109 851 960
Condition100%3/3
$incl_attr and $ft->link
!ll&&!rl&&r
50801109
MC/DC100%2/2
$incl_attr and $ft->link
$incl_attr $ft->link
Truth table100%3/3
A and B
A= $incl_attrB= $ft->link
ABresult
0X0
100
111
115 109 $attr = ' href="' . encode_entities($ft->link, '"') . '"';
116 }
117  
118 960 my $lncol = '';
119 960 ▶ if ($incl_index) {
Branch: 1 branch
BranchTrueFalseTotal
if ($incl_index) 0 960 960
120 0 $lncol = $ft->index . "\t";
121 }
122  
123 960 ▶ if (defined($prev_index) && (($ft->index - $prev_index) != 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prev_index and $ft->index - $prev_index != 1) 26 934 960
Condition100%3/3
defined $prev_index and $ft->index - $prev_index != 1
!ll&&!rl&&r
5388126
MC/DC100%2/2
defined $prev_index and $ft->index - $prev_index != 1
defined $prev_index $ft->index - $prev_index != 1
Truth table100%3/3
A and B
A= defined $prev_indexB= $ft->index - $prev_index != 1
ABresult
0X0
100
111
124 26 push @out, "\n";
125 }
126  
127 960 push @out, "$lncol$indent<$name$attr>\n";
128
129 960 ▶ if ($incl_text && ($ft->text !~ /^[\s\r\n]*$/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($incl_text and not $ft->text =~ /^[\s\r\n]*$/) 64 896 960
Condition100%3/3
$incl_text and not $ft->text =~ /^[\s\r\n]*$/
!ll&&!rl&&r
73815864
MC/DC100%2/2
$incl_text and not $ft->text =~ /^[\s\r\n]*$/
$incl_text not $ft->text =~ /^[\s\r\n]*$/
Truth table100%3/3
A and not(B)
A= $incl_textB= not $ft->text =~ /^[\s\r\n]*$/
ABresult
0X0
100
111
130 64 ▶ $lncol = $incl_index ? "\t" : "";
Branch: 1 branch
BranchTrueFalseTotal
$incl_index ? : 0 64 64
131 64 push @out, $lncol . $indent . encode_entities($ft->text, "<>&") . "\n";
132 }
133  
134 960 $prev = $name;
135 960 $prev_index = $ft->index;
136 }
137  
138 53 ▶ return wantarray ? @out : \@out;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 53 0 53
139 }
140  
141 3 ▶sub canonicalize_tags {
Subroutine canonicalize_tags: called CC=4 SCAR=14.0
Pod documented
142 3 my @out;
143 3 foreach (@_) {
144 48 my $ln = lc $_;
145 48 $ln =~ s/^\s*<//;
146 48 $ln =~ s/\/?>[\s\r\n]*$//;
147  
148 48 ▶ if ($ln) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ln) 48 0 48
149 48 push @out, $ln;
150 }
151 }
152  
153 3 ▶ return wantarray ? @out : \@out;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 0 3
154 }
155  
156 7 1;
157  
158 __END__
↑ Summary scrape »