blib/lib/HTML/TableExtract.pm

filter: Statementstmt 76.5% 528 / 690 Branchbran 58.9% 204 / 346 Conditioncond 56.8% 62 / 109 MC/DCmcdc 35.9% 23 / 64 Subroutinesub 73.8% 65 / 88 Podpod 100% 16 / 16 totaltotal 68.3% 898 / 1313 SCAR 76.0
CC
247
Cov
69%
_check_htrigger
57.0
__ANON__
49.6
table
30.0
CRAP 2004.5
↑ Summary
1 package HTML::TableExtract;
2  
3 # This package extracts tables from HTML. Tables of interest may be
4 # specified using header information, depth, order in a depth, table tag
5 # attributes, or some combination of the four. See the POD for more
6 # information.
7 #
8 # Author: Matthew P. Sisk. See the POD for copyright information.
9  
10 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 11 ▶use vars qw($VERSION @ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 11 $VERSION = '2.13';
16  
17 11 ▶use HTML::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 11 @ISA = qw(HTML::Parser);
19  
20 11 ▶use HTML::Entities;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 # trickery for subclassing from HTML::TreeBuilder rather than the
23 # default HTML::Parser. (use HTML::TableExtract qw(tree);) Also installs
24 # a mode constant TREE().
25  
26 11 ▶BEGIN { *TREE = sub { 0 } }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
27  
28 sub import {
29 10 ▶ my $class = shift;
Subroutine import: called CC=6 SCAR=26.7
30 11 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 10 ▶ *TREE = @_ ? sub { 1 } : sub { 0 };
Subroutine __ANON__: called CC=1 SCAR=0.1
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 10 10
32 10 ▶ return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 10 0 10
33 0 my $mode = shift;
34 0 ▶ croak "Unknown mode '$mode'\n" unless $mode eq 'tree';
Branch: 1 branch
BranchTrueFalseTotal
unless $mode eq "tree" 0 0 0
35 0 eval "use HTML::TreeBuilder";
36 0 ▶ croak "Problem loading HTML::TreeBuilder : $@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
37 0 eval "use HTML::ElementTable 1.17";
38 0 ▶ croak "problem loading HTML::ElementTable : $@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
39 0 @ISA = qw(HTML::TreeBuilder);
40 0 $class;
41 }
42  
43 # Backwards compatibility for deprecated methods
44 11 *table_state = *table;
45 11 *table_states = *tables;
46 11 *first_table_state_found = *first_table_found;
47  
48 ###
49  
50 11 my %Defaults = (
51 headers => undef,
52 depth => undef,
53 count => undef,
54 attribs => undef,
55 subtables => undef,
56 gridmap => 1,
57 decode => 1,
58 automap => 1,
59 slice_columns => 1,
60 keep_headers => 0,
61 br_translate => 1,
62 error_handle => \*STDOUT,
63 debug => 0,
64 keep_html => 0,
65 strip_html_on_match => 1,
66 );
67 11 my $Dpat = join('|', sort keys %Defaults);
68  
69 ### Constructor
70  
71 sub new {
72 23 ▶ my $that = shift;
Subroutine new: called CC=11 SCAR=24.9
Pod documented
73 23 ▶ my $class = ref($that) || $that;
Condition33%1/3
ref $that || $that
l!l&&r!l&&!r
0230
MC/DC0%0/2
ref $that || $that
ref $that $that
Truth table33%1/3
A || B
A= ref $thatB= $that
ABresult
1X1
011
000
74  
75 23 my(%pass, %parms, $k, $v);
76 23 ▶ while (($k,$v) = splice(@_, 0, 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 31 23 54
77 31 ▶ if ($k eq 'headers') {
Branch: 2 branches
BranchTrueFalseTotal
if ($k eq 'headers') { } 12 19 31
elsif ($k =~ /^$Dpat$/) { } 19 0 19
78 12 ▶ ref $v eq 'ARRAY'
Branch: 1 branch
BranchTrueFalseTotal
ref $v eq "ARRAY" or croak("Param '${k}' must be passed in ref to array\n") 0 12 12
79 or croak "Param '$k' must be passed in ref to array\n";
80 12 $parms{$k} = $v;
81 }
82 elsif ($k =~ /^$Dpat$/) {
83 19 $parms{$k} = $v;
84 }
85 else {
86 0 $pass{$k} = $v;
87 }
88 }
89  
90 23 my $self = $class->SUPER::new(%pass);
91 23 bless $self, $class;
92 23 foreach (keys %parms, keys %Defaults) {
93 $self->{$_} = exists $parms{$_} && defined $parms{$_} ?
94 376 ▶ $parms{$_} : $Defaults{$_};
Branch: 1 branch
BranchTrueFalseTotal
exists $parms{$_} && defined $parms{$_} ? : 62 314 376
Condition66%2/3
exists $parms{$_} and defined $parms{$_}
!ll&&!rl&&r
314062
MC/DC50%1/2
exists $parms{$_} and defined $parms{$_}
exists $parms{$_} defined $parms{$_}
Truth table66%2/3
A and B
A= exists $parms{$_}B= defined $parms{$_}
ABresult
0X0
100
111
95 }
96 23 ▶ if ($self->{headers}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"headers"}) 12 11 23
97 0 $self->_emsg("TE here, headers: ", join(',', @{$self->{headers}}), "\n")
98 12 ▶ if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 12 12
99 12 $self->{gridmap} = 1;
100 }
101  
102 # Initialize counts and containers
103 23 $self->_reset_state;
104  
105 23 $self;
106 }
107  
108 ### HTML::Parser overrides
109  
110 sub start {
111 10650 ▶ my $self = shift;
Subroutine start: called CC=16 SCAR=28.7
Pod documented
112 10650 my @res;
113  
114 10650 ▶ @res = $self->SUPER::start(@_) if TREE();
Branch: 1 branch
BranchTrueFalseTotal
if TREE() 0 10650 10650
115  
116 # Create a new table state if entering a table.
117 10650 ▶ if ($_[0] eq 'table') {
Branch: 2 branches
BranchTrueFalseTotal
if ($_[0] eq 'table') { } 111 10539 10650
elsif ($self->{'_in_a_table'}) { } 10389 150 10539
118 111 my $ts = $self->_enter_table(@_);
119 111 ▶ $ts->tree($res[0]) if @res;
Branch: 1 branch
BranchTrueFalseTotal
if @res 0 111 111
120 }
121 elsif ($self->{_in_a_table}) {
122 # Rows and cells are next.
123 10389 my $ts = $self->current_table;
124 10389 ▶ if ($_[0] eq 'tr') {
Branch: 3 branches
BranchTrueFalseTotal
if ($_[0] eq 'tr') { } 984 9405 10389
elsif ($_[0] eq 'td' or $_[0] eq 'th') { } 9312 93 9405
elsif (not TREE() and $ts->{'in_cell'}) { } 93 0 93
125 984 $ts->_enter_row;
126 }
127 ▶ elsif ($_[0] eq 'td' || $_[0] eq 'th') {
Condition66%2/3
$_[0] eq "td" or $_[0] eq "th"
l!l&&r!l&&!r
9312093
MC/DC50%1/2
$_[0] eq "td" or $_[0] eq "th"
$_[0] eq "td" $_[0] eq "th"
Truth table66%2/3
A or B
A= $_[0] eq "td"B= $_[0] eq "th"
ABresult
1X1
011
000
128 9312 $ts->_enter_cell(@_);
129 9312 ▶ my %attrs = ref $_[1] ? %{$_[1]} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 9312 0 9312
130 9312 ▶ my $rspan = $attrs{rowspan} || 1;
Condition100%2/2
$attrs{'rowspan'} || 1
l!l
69306
MC/DC100%1/1
$attrs{'rowspan'} || 1
$attrs{'rowspan'}
Truth table100%2/2
A
A= $attrs{'rowspan'}
Aresult
11
00
131 9312 ▶ my $cspan = $attrs{colspan} || 1;
Condition100%2/2
$attrs{'colspan'} || 1
l!l
99303
MC/DC100%1/1
$attrs{'colspan'} || 1
$attrs{'colspan'}
Truth table100%2/2
A
A= $attrs{'colspan'}
Aresult
11
00
132 9312 $ts->_rasterizer->($ts->row_count, $rspan, $cspan);
133 9312 $ts->_anchor_item(@res);
134 }
135 ▶ elsif (! TREE() && $ts->{in_cell}) {
Condition33%1/3
not TREE() and $ts->{"in_cell"}
!ll&&!rl&&r
0093
MC/DC0%0/2
not TREE() and $ts->{"in_cell"}
not TREE() $ts->{"in_cell"}
Truth table33%1/3
not(A) and B
A= not TREE()B= $ts->{"in_cell"}
ABresult
0X0
100
111
136 93 ▶ if ($self->{keep_html}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'keep_html'}) { } 0 93 93
elsif ($_[0] eq 'br' and $self->{'br_translate'}) { } 3 180 183
137 # capture full text of tag
138 0 $self->text($_[3]);
139 }
140 ▶ elsif ($_[0] eq 'br' && $self->{br_translate}) {
Condition66%2/3
$_[0] eq "br" and $self->{"br_translate"}
!ll&&!rl&&r
9003
MC/DC50%1/2
$_[0] eq "br" and $self->{"br_translate"}
$_[0] eq "br" $self->{"br_translate"}
Truth table66%2/3
A and B
A= $_[0] eq "br"B= $self->{"br_translate"}
ABresult
0X0
100
111
141 # Replace <br> with newlines if requested
142 3 $self->text("\n");
143 }
144 }
145 }
146  
147 10650 @res;
148 } # end start
149  
150 sub end {
151 10647 ▶ my $self = shift;
Subroutine end: called CC=10 SCAR=24.0
Pod documented
152 10647 my @res;
153
154 10647 ▶ @res = $self->SUPER::end(@_) if TREE();
Branch: 1 branch
BranchTrueFalseTotal
if TREE() 0 10647 10647
155  
156 10647 ▶ if ($self->{_in_a_table}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_in_a_table"}) 10497 150 10647
157 10497 my $ts = $self->current_table;
158 10497 ▶ if ($_[0] eq 'td' || $_[0] eq 'th') {
Branch: 4 branches
BranchTrueFalseTotal
if ($_[0] eq 'td' or $_[0] eq 'th') { } 9312 1185 10497
elsif ($_[0] eq 'tr') { } 984 201 1185
elsif ($_[0] eq 'table') { } 111 90 201
elsif (not TREE()) { } 90 0 90
Condition66%2/3
$_[0] eq "td" or $_[0] eq "th"
l!l&&r!l&&!r
931201185
MC/DC50%1/2
$_[0] eq "td" or $_[0] eq "th"
$_[0] eq "td" $_[0] eq "th"
Truth table66%2/3
A or B
A= $_[0] eq "td"B= $_[0] eq "th"
ABresult
1X1
011
000
159 9312 $ts->_exit_cell;
160 }
161 elsif ($_[0] eq 'tr') {
162 984 $ts->_exit_row;
163 }
164 elsif ($_[0] eq 'table') {
165 111 $self->_exit_table;
166 }
167 elsif (! TREE()) {
168 90 ▶ if ($self->{keep_html} && $ts->{in_cell}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"keep_html"} and $ts->{"in_cell"}) 0 90 90
Condition33%1/3
$self->{"keep_html"} and $ts->{"in_cell"}
!ll&&!rl&&r
9000
MC/DC0%0/2
$self->{"keep_html"} and $ts->{"in_cell"}
$self->{"keep_html"} $ts->{"in_cell"}
Truth table33%1/3
A and B
A= $self->{"keep_html"}B= $ts->{"in_cell"}
ABresult
0X0
100
111
169 # capture full text of tag
170 0 $self->text($_[1]);
171 }
172 }
173 }
174  
175 10647 @res;
176 } # end end
177  
178 sub text {
179 9756 ▶ my $self = shift;
Subroutine text: called CC=6 SCAR=19.2
Pod documented
180 9756 my @res;
181  
182 9756 ▶ if (TREE()) {
Branch: 2 branches
BranchTrueFalseTotal
if (TREE()) { } 0 9756 9756
elsif ($self->{'_in_a_table'}) { } 9449 307 9756
183 0 @res = $self->SUPER::text(@_);
184 }
185 elsif ($self->{_in_a_table}) {
186 9449 my $ts = $self->current_table;
187 9449 ▶ if ($ts->{in_cell}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ts->{"in_cell"}) 9322 127 9449
188 9322 ▶ if ($self->{decode} && !$self->{keep_html}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'decode'} and not $self->{'keep_html'}) { } 9322 0 9322
Condition33%1/3
$self->{"decode"} and not $self->{"keep_html"}
!ll&&!rl&&r
009322
MC/DC0%0/2
$self->{"decode"} and not $self->{"keep_html"}
$self->{"decode"} not $self->{"keep_html"}
Truth table33%1/3
A and not(B)
A= $self->{"decode"}B= not $self->{"keep_html"}
ABresult
0X0
100
111
189 9322 $ts->_add_text(decode_entities($_[0]));
190 }
191 else {
192 0 $ts->_add_text($_[0]);
193 }
194 }
195 }
196  
197 9756 @res;
198 } # end text
199  
200 sub parse {
201 333 ▶ my $self = shift;
Subroutine parse: called CC=3 SCAR=11.0
Pod documented
202 333 ▶ $self->_reset_state unless $self->{_parsing};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_parsing"} 23 310 333
203 333 ▶ $self->{_parsing} ||= 1;
Condition100%2/2
$self->{"_parsing"} ||= 1
l!l
31023
MC/DC100%1/1
$self->{"_parsing"} ||= 1
$self->{"_parsing"}
Truth table100%2/2
A
A= $self->{"_parsing"}
Aresult
11
00
204 333 $self->SUPER::parse(@_);
205 }
206  
207 sub eof {
208 23 ▶ my $self = shift;
Subroutine eof: called CC=1 SCAR=0.0
Pod documented
209 23 $self->{_parsing} = 0;
210 23 $self->SUPER::eof(@_);
211 }
212  
213 ### End HTML::Parser overrides
214  
215 ### Report Methods
216  
217 sub depths {
218 # Return all depths where valid tables were located.
219 0 ▶ my $self = shift;
Subroutine depths: not called CC=2 SCAR=17.9
Pod documented
220 0 ▶ return () unless ref $self->{_tables};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->{"_tables"} 0 0 0
221 0 sort { $a <=> $b } keys %{$self->{_tables}};
222 }
223  
224 sub counts {
225 # Given a depth, return the counts of all valid tables found therein.
226 0 ▶ my($self, $depth) = @_;
Subroutine counts: not called CC=3 SCAR=24.8
Pod documented
227 0 ▶ defined $depth or croak "Depth required\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $depth or croak("Depth required\n") 0 0 0
228 0 ▶ return () unless exists $self->{_tables}{$depth};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_tables"}{$depth} 0 0 0
229 0 sort { $a <=> $b } keys %{$self->{_tables}{$depth}};
230 }
231  
232 sub table {
233 # Return the table state for a particular depth and count
234 0 ▶ my($self, $depth, $count) = @_;
Subroutine table: not called CC=5 SCAR=34.0
Pod documented
235 0 ▶ defined $depth or croak "Depth required\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $depth or croak("Depth required\n") 0 0 0
236 0 ▶ defined $count or croak "Count required\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $count or croak("Count required\n") 0 0 0
237 0 ▶ if (! $self->{_tables}{$depth} || ! $self->{_tables}{$depth}{$count}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_tables"}{$depth} and $self->{"_tables"}{$depth}{$count}) 0 0 0
Condition0%0/3
$self->{"_tables"}{$depth} and $self->{"_tables"}{$depth}{$count}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"_tables"}{$depth} and $self->{"_tables"}{$depth}{$count}
$self->{"_tables"}{$depth} $self->{"_tables"}{$depth}{$count}
Truth table0%0/3
A and B
A= $self->{"_tables"}{$depth}B= $self->{"_tables"}{$depth}{$count}
ABresult
0X0
100
111
238 0 return undef;
239 }
240 0 $self->{_tables}{$depth}{$count};
241 }
242  
243 sub first_table_found {
244 10 ▶ my $self = shift;
Subroutine first_table_found: called CC=2 SCAR=7.2
Pod documented
245 10 ▶ ref $self->{_ts_sequential}[0] ? $self->{_ts_sequential}[0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $self->{'_ts_sequential'}[0] ? : 10 0 10
246 }
247  
248 0 ▶sub rows { shift->first_table_found->rows(@_) }
Subroutine rows: not called CC=1 SCAR=6.9
Pod documented
249  
250 sub tables {
251 # Return all valid table records found, in the order that they
252 # were seen.
253 14 ▶ my $self = shift;
Subroutine tables: called CC=3 SCAR=15.0
Pod documented
254 14 ▶ while ($self->{_in_a_table}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_in_a_table"}) 0 14 14
255 0 my $ts = $self->current_table;
256 $self->_emsg("Mangled HTML in table ($ts->{depth},$ts->{count}), inferring closing table tag.\n")
257 0 ▶ if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
258 0 $self->_exit_table;
259 }
260 14 @{$self->{_ts_sequential}};
261 }
262  
263 # in tree mode, we already are an HTML::TreeBuilder, which is an
264 # HTML::Element structure after parsing...but we provide this for
265 # consistency with the table object method for accessing the tree
266 # structures.
267  
268 0 ▶sub tree { shift }
Subroutine tree: not called CC=1 SCAR=6.9
Pod documented
269  
270 sub tables_report {
271 # Print out a summary of extracted tables, including depth/count
272 0 ▶ my $self = shift;
Subroutine tables_report: not called CC=2 SCAR=17.9
Pod documented
273 0 my $str;
274 0 foreach my $ts ($self->tables) {
275 0 $str .= $ts->report(@_);
276 }
277 0 $str;
278 }
279  
280 sub tables_dump {
281 0 ▶ my $self = shift;
Subroutine tables_dump: not called CC=1 SCAR=6.9
Pod documented
282 0 $self->_emsg($self->tables_report(@_));
283 }
284  
285 # for testing/debugging
286 sub _attribute_purge {
287 0 ▶ my $self = shift;
Subroutine _attribute_purge: not called CC=2 SCAR=17.9
288 0 foreach (keys %Defaults) {
289 0 delete $self->{$_};
290 }
291 }
292  
293 ### Runtime
294  
295 sub _enter_table {
296 111 ▶ my($self, @args) = @_;
Subroutine _enter_table: called CC=9 SCAR=22.0
297  
298 111 ++$self->{_cdepth};
299 111 ++$self->{_in_a_table};
300  
301 111 my $depth = $self->{_cdepth};
302  
303 # Table tag attributes, if present
304 111 ▶ my $attribs = $args[1] || {};
Condition50%1/2
$args[1] || {}
l!l
1110
MC/DC0%0/1
$args[1] || {}
$args[1]
Truth table50%1/2
A
A= $args[1]
Aresult
11
00
305  
306 # Table states can come and go on the stack...here we retrieve the
307 # table state for the table surrounding the current table tag (parent
308 # table state). If the current table tag belongs to a top level table,
309 # then this will be undef.
310 111 my $pts = $self->current_table;
311  
312 # Counts are tracked for each depth.
313 111 my $counts = $self->{_counts};
314 111 ▶ $counts->[$depth] = -1 unless defined $counts->[$depth];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $counts->[$depth] 45 66 111
315 111 ++$counts->[$depth];
316 111 my $count = $counts->[$depth];
317  
318 $self->_emsg("TABLE: cdepth $depth, ccount $count, it: $self->{_in_a_table}\n")
319 111 ▶ if $self->{debug} >= 2;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} >= 2 0 111 111
320  
321 # Umbrella status means that this current table and all of its
322 # descendant tables will be harvested.
323 111 my $umbrella = 0;
324 111 ▶ if (! defined $self->{depth} && ! defined $self->{count} &&
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"depth"} or defined $self->{"count"} or $self->{"attribs"} or $self->{"headers"}) 30 81 111
Condition100%9/9
defined $self->{"depth"} or defined $self->{"count"} or $self->{"attribs"} or $self->{"headers"}
l!l&&r!l&&!r
354630
defined $self->{"depth"} or defined $self->{"count"} or $self->{"attribs"}
l!l&&r!l&&!r
201576
defined $self->{"depth"} or defined $self->{"count"}
l!l&&r!l&&!r
15591
MC/DC100%4/4
defined $self->{"depth"} or defined $self->{"count"} or $self->{"attribs"} or $self->{"headers"}
defined $self->{"depth"} defined $self->{"count"} $self->{"attribs"} $self->{"headers"}
Truth table100%5/5
A or B or C or D
A= defined $self->{"depth"}B= defined $self->{"count"}C= $self->{"attribs"}D= $self->{"headers"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
325 ! $self->{attribs} && ! $self->{headers}) {
326 30 ++$umbrella;
327 }
328  
329 # Basic parameters for the soon-to-be-created table state.
330 my %tsparms = (
331 depth => $depth,
332 count => $count,
333 attribs => $attribs,
334 umbrella => $umbrella,
335 automap => $self->{automap},
336 slice_columns => $self->{slice_columns},
337 keep_headers => $self->{keep_headers},
338 counts => $counts,
339 error_handle => $self->{error_handle},
340 debug => $self->{debug},
341 keep_html => $self->{keep_html},
342 strip_html_on_match => $self->{strip_html_on_match},
343 111 parent_table => $pts,
344 );
345  
346 # Target constraints. There is no point in passing any of these along
347 # if we are under an umbrella. Notice that with table states, "depth"
348 # and "count" are absolute coordinates recording where this table was
349 # created, whereas "tdepth" and "tcount" are the target constraints.
350 # Headers have "absolute" meaning, therefore are passed by the
351 # same name.
352 111 ▶ if (!$umbrella) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($umbrella) 81 30 111
353 81 $tsparms{tdepth} = $self->{depth};
354 81 $tsparms{tcount} = $self->{count};
355 81 $tsparms{tattribs} = $self->{attribs};
356 81 $tsparms{headers} = $self->{headers};
357 }
358  
359 # Abracadabra
360 111 my $ts = HTML::TableExtract::Table->new(%tsparms);
361  
362 # Push the newly created and configured table state onto the stack.
363 # This will now be the current_table().
364 111 push(@{$self->{_tablestack}}, $ts);
365  
366 111 $ts;
367 }
368  
369 sub _exit_table {
370 111 ▶ my $self = shift;
Subroutine _exit_table: called CC=8 SCAR=23.6
371 111 my $ts = $self->current_table;
372  
373 # Last ditch fix for HTML mangle
374 111 ▶ if ($ts->{in_cell}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ts->{"in_cell"}) 0 111 111
375 0 ▶ $self->_emsg("Mangled HTML in table ($self->{depth},$self->{count}), forcing exit of cell ($ts->{rc},$ts->{cc}) due to table exit\n") if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
376 0 $ts->_exit_cell;
377 }
378 111 ▶ if ($ts->{in_row}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ts->{"in_row"}) 0 111 111
379 0 ▶ $self->_emsg("Mangled HTML in table ($self->{depth},$self->{count}), forcing exit of row $ts->{rc} due to table exit\n") if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
380 0 $ts->_exit_row;
381 }
382  
383 # transform from tree to grid using our rasterized template
384 111 $ts->_grid_map();
385  
386 111 ▶ $self->_capture_table($ts) if $ts->_check_triggers;
Branch: 1 branch
BranchTrueFalseTotal
if $ts->_check_triggers 83 28 111
387  
388 # Restore last table state
389 111 pop(@{$self->{_tablestack}});
390 111 --$self->{_in_a_table};
391 111 my $lts = $self->current_table;
392 111 ▶ if (ref $lts) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $lts) { } 54 57 111
393 54 $self->{_cdepth} = $lts->{depth};
394 }
395 else {
396 # Back to the top level
397 57 $self->{_cdepth} = -1;
398 }
399 $self->_emsg("LEAVE: cdepth: $self->{_cdepth}, ccount: $ts->{count}, it: $self->{_in_a_table}\n")
400 111 ▶ if $self->{debug} >= 2;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} >= 2 0 111 111
401 }
402  
403 sub _capture_table {
404 89 ▶ my($self, $ts, $type) = @_;
Subroutine _capture_table: called CC=8 SCAR=22.4
405 89 ▶ croak "Table state ref required\n" unless ref $ts;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ts 0 89 89
406 89 ▶ if ($self->{debug} >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"} >= 2) 0 89 89
407 0 my $msg = "Captured table (" . $ts->depth . ',' . $ts->count . ")";
408 0 ▶ $msg .= " ($type)" if $type;
Branch: 1 branch
BranchTrueFalseTotal
if $type 0 0 0
409 0 $msg .= "\n";
410 0 $self->_emsg($msg);
411 }
412 89 ▶ $ts->tree(HTML::ElementTable->new_from_tree($ts->tree)) if TREE();
Branch: 1 branch
BranchTrueFalseTotal
if TREE() 0 89 89
413 89 ▶ if ($self->{subtables}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"subtables"}) 11 78 89
414 11 foreach my $child (@{$ts->{children}}) {
415 8 ▶ next if $child->{captured};
Branch: 1 branch
BranchTrueFalseTotal
if $child->{"captured"} 2 6 8
416 6 $self->_capture_table($child, 'subtable');
417 6 $child->{slice_columns} = 0;
418 6 $child->{keep_headers} = 1;
419 6 $child->{headers} = '';
420 }
421 }
422 89 $ts->{captured} = 1;
423 89 $self->{_tables}{$ts->{depth}}{$ts->{count}} = $ts;
424 89 push(@{$self->{_ts_sequential}}, $ts);
425 }
426  
427 sub current_table {
428 30668 ▶ my $self = shift;
Subroutine current_table: called CC=1 SCAR=0.0
Pod documented
429 30668 $self->{_tablestack}[$#{$self->{_tablestack}}];
430 }
431  
432 sub _reset_state {
433 46 ▶ my $self = shift;
Subroutine _reset_state: called CC=1 SCAR=0.0
434 46 $self->{_cdepth} = -1;
435 46 $self->{_tablestack} = [];
436 46 $self->{_tables} = {};
437 46 $self->{_ts_sequential} = [];
438 46 $self->{_counts} = [];
439 46 $self->{_in_a_table} = 0;
440 46 $self->{_parsing} = 0;
441 }
442  
443 sub _emsg {
444 0 ▶ my $self = shift;
Subroutine _emsg: not called CC=2 SCAR=17.9
445 0 my $fh = $self->{error_handle};
446 0 ▶ return unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 0 0 0
447 0 print $fh @_;
448 }
449  
450 ##########
451  
452 {
453  
454 package HTML::TableExtract::Table;
455  
456 11 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
457 11 ▶ use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
458  
459 11 *TREE = *HTML::TableExtract::TREE;
460  
461 sub new {
462 111 ▶ my $that = shift;
Subroutine new: called CC=8 SCAR=21.2
463 111 ▶ my $class = ref($that) || $that;
Condition33%1/3
ref $that || $that
l!l&&r!l&&!r
01110
MC/DC0%0/2
ref $that || $that
ref $that $that
Truth table33%1/3
A || B
A= ref $thatB= $that
ABresult
1X1
011
000
464 # Note:
465 # - 'depth' and 'count' are where this table were found.
466 # - 'tdepth' and 'tcount' are target constraints on which to trigger.
467 # - 'headers' represent a target constraint, location independent.
468 # - 'attribs' represent target table tag constraints
469 111 my $self = {
470 umbrella => 0,
471 in_row => 0,
472 in_cell => 0,
473 rc => -1,
474 cc => -1,
475 grid => [],
476 translation => [],
477 hrow => [],
478 order => [],
479 children => [],
480 captured => 0,
481 debug => 0,
482 };
483  
484 111 $self->{_rastamon} = HTML::TableExtract::Rasterize->make_rasterizer();
485 111 bless $self, $class;
486  
487 111 my %parms = @_;
488  
489 # Depth and Count -- this is the absolute address of the table.
490 111 ▶ croak "Absolute depth required\n" unless defined $parms{depth};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $parms{"depth"} 0 111 111
491 111 ▶ croak "Count required\n" unless defined $parms{count};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $parms{"count"} 0 111 111
492 111 ▶ croak "Counts required\n" unless defined $parms{counts};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $parms{"counts"} 0 111 111
493  
494 111 foreach (keys %parms) {
495 1767 $self->{$_} = $parms{$_};
496 }
497  
498 # Register lineage
499 111 my $pts = $self->{parent_table};
500 111 ▶ $self->lineage($pts || undef);
Condition100%2/2
$pts || undef
l!l
5457
MC/DC100%1/1
$pts || undef
$pts
Truth table100%2/2
A
A= $pts
Aresult
11
00
501 111 ▶ push(@{$pts->{children}}, $self) if ($pts);
Branch: 1 branch
BranchTrueFalseTotal
if $pts 54 57 111
502 111 delete $self->{parent_table};
503  
504 111 $self;
505 }
506  
507 sub _anchor_item {
508 # anchor the reference to a cell in our grid -- in TREE mode this is
509 # a reference to a data element, otherwise it's a reference to an
510 # empty scalar in which we will collect our text.
511 9312 ▶ my($self, @res) = @_;
Subroutine _anchor_item: called CC=3 SCAR=11.8
512 9312 my $row = $self->{grid}[-1];
513 9312 my $item;
514 9312 ▶ if (@res && ref $res[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (@res and ref $res[0]) { } 0 9312 9312
Condition33%1/3
@res and ref $res[0]
!ll&&!rl&&r
931200
MC/DC0%0/2
@res and ref $res[0]
@res ref $res[0]
Truth table33%1/3
A and B
A= @resB= ref $res[0]
ABresult
0X0
100
111
515 0 $item = $res[0];
516 }
517 else {
518 9312 my $scalar_ref;
519 9312 $item = \$scalar_ref;
520 }
521 9312 push(@$row, $item);
522 }
523  
524 sub _gridalias {
525 3 ▶ my $self = shift;
Subroutine _gridalias: called CC=2 SCAR=7.1
526 3 ▶ $self->{gridalias} ||= $self->_make_gridalias;
Condition66%2/3
$self->{"gridalias"} ||= $self->_make_gridalias
l!l&&r!l&&!r
120
MC/DC0%0/2
$self->{"gridalias"} ||= $self->_make_gridalias
$self->{"gridalias"} $self->_make_gridalias
Truth table66%2/3
A ||= B
A= $self->{"gridalias"}B= $self->_make_gridalias
ABresult
1X1
011
000
527 }
528  
529 sub _grid_map {
530 # using our rasterized template, flesh out our captured items which
531 # are still in 'tree' format
532 111 ▶ my $self = shift;
Subroutine _grid_map: called CC=13 SCAR=30.3
533 111 my $template = $self->_rasterizer->();
534 111 my $grid = $self->{grid};
535 # drop empty rows
536 111 ▶ if ($self->{debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 111 111
537 0 foreach (0 .. $#$grid) {
538 0 ▶ next if @{$grid->[$_]};
Branch: 1 branch
BranchTrueFalseTotal
if @{$grid->[$_];} 0 0 0
539 0 $self->_emsg("Dropping empty row $_\n");
540 }
541 }
542 111 @$grid = grep(@$_, @$grid);
543 111 foreach my $r (0 .. $#$template) {
544 984 my $row = $grid->[$r];
545 984 my $trow = $template->[$r];
546 984 ▶ $self->_emsg("Flesh row $r ($#$row) to $#$trow\n") if $self->{debug} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} > 1 0 984 984
547 984 foreach my $c (0 .. $#$trow) {
548 9342 ▶ print STDERR $trow->[$c] ? '1' : '0' if $self->{debug} > 1;
Branch: 2 branches
BranchTrueFalseTotal
if $self->{"debug"} > 1 0 9342 9342
$trow->[$c] ? : 0 0 0
549 9342 ▶ if ($trow->[$c]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($trow->[$c]) { } 9312 30 9342
550 9312 ▶ if (! defined $row->[$c]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $row->[$c]) 0 9312 9312
551 0 $row->[$c] = \undef;
552 }
553 9312 next;
554 }
555 else {
556 30 my $scalar;
557 30 splice(@$row, $c, 0, \$scalar);
558 }
559 }
560 984 ▶ print STDERR "\n" if $self->{debug} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} > 1 0 984 984
561 984 ▶ croak "row $r splice mismatch: $#$row vs $#$trow\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $row->$#* == $trow->$#* 0 984 984
562 unless $#$row == $#$trow;
563 }
564 111 $grid;
565 }
566  
567 sub _make_gridalias {
568 # our aliased grid will have references in masked cells to the same
569 # cell that is covering it via spanning.
570 2 ▶ my $self = shift;
Subroutine _make_gridalias: called CC=7 SCAR=19.5
571 2 my $grid = $self->{grid};
572 2 my $template = $self->_rasterizer->();
573 2 my(@gridalias, @translation);
574 2 $gridalias[$_] = [@{$grid->[$_]}] foreach 0 .. $#$grid;
575 2 foreach my $r (0 .. $#gridalias) {
576 14 my $row = $gridalias[$r];
577 14 foreach my $c (0 .. $#$row) {
578 56 ▶ my $tcell = $template->[$r][$c] || next;
Condition100%2/2
$template->[$r][$c] || (next)
l!l
3620
MC/DC100%1/1
$template->[$r][$c] || (next)
$template->[$r][$c]
Truth table100%2/2
A
A= $template->[$r][$c]
Aresult
11
00
579 36 my($rspan, $cspan) = @$tcell;
580 36 foreach my $rs (0 .. $rspan-1) {
581 42 foreach my $cs (0 .. $cspan-1) {
582 56 $gridalias[$r + $rs][$c + $cs] = $grid->[$r][$c];
583 56 $translation[$r + $rs][$c + $cs] = "$r,$c";
584 }
585 }
586 }
587 }
588 2 $self->{translation} = \@translation;
589 2 $self->{gridalias} = \@gridalias;
590 }
591  
592 ### Constraint tests
593  
594 sub _check_dtrigger {
595 # depth
596 81 ▶ my $self = shift;
Subroutine _check_dtrigger: called CC=3 SCAR=11.0
597 81 ▶ return 1 unless defined $self->{tdepth};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"tdepth"} 66 15 81
598 15 ▶ $self->{tdepth} == $self->{depth} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'tdepth'} == $self->{'depth'} ? : 8 7 15
599 }
600  
601 sub _check_ctrigger {
602 # count
603 74 ▶ my $self = shift;
Subroutine _check_ctrigger: called CC=4 SCAR=13.9
604 74 ▶ return 1 unless defined $self->{tcount};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"tcount"} 63 11 74
605 return 1 if (exists $self->{counts}[$self->{depth}] &&
606 11 ▶ $self->{tcount} == $self->{counts}[$self->{depth}]);
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"counts"}[$self->{"depth"}] and $self->{"tcount"} == $self->{"counts"}[$self->{"depth"}] 4 7 11
Condition66%2/3
exists $self->{"counts"}[$self->{"depth"}] and $self->{"tcount"} == $self->{"counts"}[$self->{"depth"}]
!ll&&!rl&&r
074
MC/DC50%1/2
exists $self->{"counts"}[$self->{"depth"}] and $self->{"tcount"} == $self->{"counts"}[$self->{"depth"}]
exists $self->{"counts"}[$self->{"depth"}] $self->{"tcount"} == $self->{"counts"}[$self->{"depth"}]
Truth table66%2/3
A and B
A= exists $self->{"counts"}[$self->{"depth"}]B= $self->{"tcount"} == $self->{"counts"}[$self->{"depth"}]
ABresult
0X0
100
111
607 7 return 0;
608 }
609  
610 sub _check_atrigger {
611 # attributes
612 67 ▶ my $self = shift;
Subroutine _check_atrigger: called CC=9 SCAR=22.1
613 67 ▶ return 1 unless scalar keys %{$self->{tattribs}};
Branch: 1 branch
BranchTrueFalseTotal
unless scalar keys %{$$self{"tattribs"};} 52 15 67
614 15 ▶ return 0 unless scalar keys %{$self->{attribs}};
Branch: 1 branch
BranchTrueFalseTotal
unless scalar keys %{$$self{"attribs"};} 0 15 15
615 15 my $a_hit = 1;
616 15 foreach my $attrib (keys %{$self->{tattribs}}) {
617 15 ▶ if (! defined $self->{attribs}{$attrib}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"attribs"}{$attrib}) 8 7 15
618 8 $a_hit = 0; last;
619 }
620 7 ▶ if (! defined $self->{tattribs}{$attrib}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"tattribs"}{$attrib}) 1 6 7
621 # undefined, but existing, target attribs are wildcards
622 1 next;
623 }
624 6 ▶ if ($self->{tattribs}{$attrib} ne $self->{attribs}{$attrib}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"tattribs"}{$attrib} ne $self->{"attribs"}{$attrib}) 2 4 6
625 2 $a_hit = 0; last;
626 }
627 }
628 15 ▶ $self->_emsg("Matched attributes\n") if $self->{debug} > 3 && $a_hit;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} > 3 and $a_hit 0 15 15
Condition33%1/3
$self->{"debug"} > 3 and $a_hit
!ll&&!rl&&r
1500
MC/DC0%0/2
$self->{"debug"} > 3 and $a_hit
$self->{"debug"} > 3 $a_hit
Truth table33%1/3
A and B
A= $self->{"debug"} > 3B= $a_hit
ABresult
0X0
100
111
629 15 $a_hit;
630 }
631  
632 sub _check_htrigger {
633 # headers
634 57 ▶ my $self = shift;
Subroutine _check_htrigger: called CC=25 SCAR=40.4
635 57 ▶ return 1 if $self->{umbrella};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"umbrella"} 0 57 57
636 57 ▶ return 1 unless $self->{headers};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"headers"} 11 46 57
637 46 ROW: foreach my $r (0 .. $#{$self->{grid}}) {
638 70 $self->_reset_hits;
639 70 my $hpat = $self->_header_pattern;
640 70 my @hits;
641 70 foreach my $c (0 .. $#{$self->{grid}[$r]}) {
642 631 my $ref = $self->{grid}[$r][$c];
643 631 my $target = '';
644 631 my $ref_type = ref $ref;
645 631 ▶ if ($ref_type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref_type) 631 0 631
646 631 ▶ if ($ref_type eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref_type eq 'SCALAR') { } 631 0 631
647 631 my $item = $$ref;
648 631 ▶ if ($self->{keep_html} && $self->{strip_html_on_match}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'keep_html'} and $self->{'strip_html_on_match'}) { } 0 631 631
Condition33%1/3
$self->{"keep_html"} and $self->{"strip_html_on_match"}
!ll&&!rl&&r
63100
MC/DC0%0/2
$self->{"keep_html"} and $self->{"strip_html_on_match"}
$self->{"keep_html"} $self->{"strip_html_on_match"}
Truth table33%1/3
A and B
A= $self->{"keep_html"}B= $self->{"strip_html_on_match"}
ABresult
0X0
100
111
649 0 my $stripper = HTML::TableExtract::StripHTML->new;
650 0 $target = $stripper->strip($item);
651 }
652 else {
653 631 $target = $item;
654 }
655 }
656 else {
657 0 ▶ if (($self->{keep_html} || TREE()) &&
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'keep_html'} || TREE() and $self->{'strip_html_on_match'}) { } 0 0 0
Condition0%0/6
$self->{"keep_html"} || TREE() and $self->{"strip_html_on_match"}
!ll&&!rl&&r
000
$self->{"keep_html"} or TREE()
l!l&&r!l&&!r
000
MC/DC0%0/3
$self->{"keep_html"} || TREE() and $self->{"strip_html_on_match"}
$self->{"keep_html"} TREE() $self->{"strip_html_on_match"}
Truth table0%0/5
A or B and C
A= $self->{"keep_html"}B= TREE()C= $self->{"strip_html_on_match"}
ABCresult
00X0
1X00
0100
1X11
0111
658 $self->{strip_html_on_match}) {
659 0 $target = $ref->as_text;
660 }
661 else {
662 0 $target = $ref->as_HTML;
663 }
664 }
665 }
666 631 ▶ $target = defined $target ? $target : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $target ? : 631 0 631
667 $self->_emsg("attempt match on $target ($hpat): ")
668 631 ▶ if $self->{debug} >= 5;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} >= 5 0 631 631
669 631 ▶ if ($target =~ $hpat) {
Branch: 2 branches
BranchTrueFalseTotal
if ($target =~ /$hpat/) { } 180 451 631
elsif ($self->{'debug'} >= 5) { } 0 451 451
670 180 my $hit = $1;
671 180 ▶ $self->_emsg("($hit)\n") if $self->{debug} >= 5;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} >= 5 0 180 180
672 # Get rid of the header segment that matched so we can tell
673 # when we're through with all header patterns.
674 180 my $real_hit;
675 180 foreach (sort _header_string_sort keys %{$self->{hits_left}}) {
676 228 ▶ if ($hit =~ /$_/im) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hit =~ /$_/im) 180 48 228
677 180 delete $self->{hits_left}{$_};
678 180 $real_hit = $_;
679 180 $hpat = $self->_header_pattern;
680 180 last;
681 }
682 }
683 180 ▶ if (defined $real_hit) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $real_hit) 180 0 180
684 180 ▶ if ($self->{debug} >= 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"} >= 4) 0 180 180
685 0 ▶ my $str = $ref_type eq 'SCALAR' ? $$ref : $ref->as_HTML;
Branch: 1 branch
BranchTrueFalseTotal
$ref_type eq 'SCALAR' ? : 0 0 0
686 0 $self->_emsg("HIT on '$hit' ($real_hit) in $str ($r,$c)\n");
687 }
688 180 push(@hits, $hit);
689 #
690 180 $self->{hits}{$c} = $real_hit;
691 180 push(@{$self->{order}}, $c);
692 180 ▶ if (!%{$self->{hits_left}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%{$self->{"hits_left"};}) 42 138 180
693 # Successful header row match
694 42 ++$self->{head_found};
695 42 $self->{hrow_index} = $r;
696 42 $self->{hrow} = $self->{grid}[$r];
697 42 last ROW;
698 }
699 }
700 }
701 elsif ($self->{debug} >= 5) {
702 0 $self->_emsg("0\n");
703 }
704 }
705 28 ▶ if ($self->{debug} && @hits) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"} and @hits) 0 28 28
Condition33%1/3
$self->{"debug"} and @hits
!ll&&!rl&&r
2800
MC/DC0%0/2
$self->{"debug"} and @hits
$self->{"debug"} @hits
Truth table33%1/3
A and B
A= $self->{"debug"}B= @hits
ABresult
0X0
100
111
706 0 my $str = "Incomplete header match ";
707 0 $str .= "(left: " . join(', ', sort keys %{$self->{hits_left}}) . ") ";
708 0 $str .= "in row $r, resetting scan";
709 0 $str .= "\n";
710 0 $self->_emsg($str);
711 }
712 }
713 46 $self->{head_found};
714 }
715  
716 sub _check_triggers {
717 111 ▶ my $self = shift;
Subroutine _check_triggers: called CC=5 SCAR=16.1
718 111 ▶ return 1 if $self->{umbrella};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"umbrella"} 30 81 111
719 81 ▶ $self->_check_dtrigger &&
Branch: 1 branch
BranchTrueFalseTotal
$self->_check_dtrigger and $self->_check_ctrigger and $self->_check_atrigger and $self->_check_htrigger 57 24 81
Condition100%6/6
$self->_check_dtrigger and $self->_check_ctrigger and $self->_check_atrigger
!ll&&!rl&&r
141057
$self->_check_dtrigger and $self->_check_ctrigger
!ll&&!rl&&r
7767
MC/DC100%3/3
$self->_check_dtrigger and $self->_check_ctrigger and $self->_check_atrigger
$self->_check_dtrigger $self->_check_ctrigger $self->_check_atrigger
Truth table100%4/4
A and B and C
A= $self->_check_dtriggerB= $self->_check_ctriggerC= $self->_check_atrigger
ABCresult
0XX0
10X0
1100
1111
720 $self->_check_ctrigger &&
721 $self->_check_atrigger &&
722 $self->_check_htrigger;
723 }
724  
725 ### Maintain table context
726  
727 sub _enter_row {
728 984 ▶ my $self = shift;
Subroutine _enter_row: called CC=3 SCAR=13.0
729 984 ▶ if ($self->{in_row}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"in_row"}) 0 984 984
730 0 ▶ $self->_emsg("Mangled HTML in table ($self->{depth},$self->{count}), forcing exit of row $self->{rc} due to new row\n") if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
731 0 $self->_exit_row;
732 }
733 984 ++$self->{rc};
734 984 ++$self->{in_row};
735 984 push(@{$self->{grid}}, [])
736 }
737  
738 sub _exit_row {
739 984 ▶ my $self = shift;
Subroutine _exit_row: called CC=5 SCAR=22.5
740 984 ▶ if ($self->{in_row}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'in_row'}) { } 984 0 984
741 984 ▶ if ($self->{in_cell}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"in_cell"}) 0 984 984
742 0 ▶ $self->_emsg("Mangled HTML in table ($self->{depth},$self->{count}), forcing exit of cell ($self->{rc}, $self->{cc}) due to new row\n") if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
743 0 $self->_exit_cell;
744 }
745 984 $self->{in_row} = 0;
746 984 $self->{cc} = -1;
747 }
748 else {
749 $self->_emsg("Mangled HTML in table ($self->{depth},$self->{count}), extraneous </TR> ignored after row $self->{rc}\n")
750 0 ▶ if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
751 }
752 }
753  
754 sub _enter_cell {
755 9312 ▶ my $self = shift;
Subroutine _enter_cell: called CC=8 SCAR=25.1
756 9312 ▶ if ($self->{in_cell}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"in_cell"}) 0 9312 9312
757 0 ▶ $self->_emsg("Mangled HTML in table ($self->{depth},$self->{count}), forcing exit of cell ($self->{rc},$self->{cc}) due to new cell\n") if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
758 0 $self->_exit_cell;
759 }
760 9312 ▶ if (!$self->{in_row}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"in_row"}) 0 9312 9312
761 # Go ahead and try to recover from mangled HTML, because we care.
762 $self->_emsg("Mangled HTML in table ($self->{depth},$self->{count}), inferring <TR> as row $self->{rc}\n")
763 0 ▶ if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
764 0 $self->_enter_row;
765 }
766 9312 ++$self->{cc};
767 9312 ++$self->{in_cell};
768 9312 ▶ my %attrs = ref $_[1] ? %{$_[1]} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 9312 0 9312
769 9312 ▶ my $rspan = $attrs{rowspan} || 1;
Condition100%2/2
$attrs{'rowspan'} || 1
l!l
69306
MC/DC100%1/1
$attrs{'rowspan'} || 1
$attrs{'rowspan'}
Truth table100%2/2
A
A= $attrs{'rowspan'}
Aresult
11
00
770 9312 ▶ my $cspan = $attrs{colspan} || 1;
Condition100%2/2
$attrs{'colspan'} || 1
l!l
99303
MC/DC100%1/1
$attrs{'colspan'} || 1
$attrs{'colspan'}
Truth table100%2/2
A
A= $attrs{'colspan'}
Aresult
11
00
771 }
772  
773 sub _exit_cell {
774 9312 ▶ my $self = shift;
Subroutine _exit_cell: called CC=3 SCAR=14.2
775 9312 ▶ if ($self->{in_cell}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'in_cell'}) { } 9312 0 9312
776 9312 $self->{in_cell} = 0;
777 }
778 else {
779 $self->_emsg("Mangled HTML in table ($self->{depth},$self->{count}), extraneous </TD> ignored in row $self->{rc}\n")
780 0 ▶ if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
781 }
782 }
783  
784 # Header stuff
785  
786 sub _header_pattern {
787 250 ▶ my($self, @headers) = @_;
Subroutine _header_pattern: called CC=2 SCAR=7.0
788 my $str = join('|',
789 map("($_)",
790 250 sort _header_string_sort keys %{$self->{hits_left}}
791 ));
792 250 my $hpat = qr/($str)/im;
793 250 ▶ $self->_emsg("HPAT: /$hpat/\n") if $self->{debug} >= 2;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} >= 2 0 250 250
794 250 $self->{hpat} = $hpat;
795 }
796  
797 sub _header_string_sort {
798 # this ensures that supersets appear before subsets in our header
799 # search pattern, eg, '10' appears before '1' and 'hubbabubba'
800 # appears before 'hubba'.
801 1112 ▶ if ($a =~ /^$b/) {
Subroutine _header_string_sort: called CC=3 SCAR=14.2
Branch: 2 branches
BranchTrueFalseTotal
if ($a =~ /^$b/) { } 0 1112 1112
elsif ($b =~ /^$a/) { } 0 1112 1112
802 0 return -1;
803 }
804 elsif ($b =~ /^$a/) {
805 0 return 1;
806 }
807 else {
808 1112 return $b cmp $a;
809 }
810 }
811  
812 # Report methods
813  
814 54 ▶ sub depth { shift->{depth} }
Subroutine depth: called CC=1 SCAR=0.0
815 54 ▶ sub count { shift->{count} }
Subroutine count: called CC=1 SCAR=0.0
816 sub coords {
817 0 ▶ my $self = shift;
Subroutine coords: not called CC=1 SCAR=6.9
818 0 ($self->depth, $self->count);
819 }
820  
821 9312 ▶ sub row_count { shift->{rc} }
Subroutine row_count: called CC=1 SCAR=0.0
822 0 ▶ sub col_count { shift->{cc} }
Subroutine col_count: not called CC=1 SCAR=6.9
823  
824 sub tree {
825 0 ▶ my $self = shift;
Subroutine tree: not called CC=2 SCAR=17.9
826 0 ▶ @_ ? $self->{_tree_ref} = shift : $self->{_tree_ref};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
827 }
828  
829 sub lineage {
830 165 ▶ my $self = shift;
Subroutine lineage: called CC=5 SCAR=16.1
831 165 ▶ $self->{lineage} ||= [];
Condition100%2/2
$self->{"lineage"} ||= []
l!l
54111
MC/DC100%1/1
$self->{"lineage"} ||= []
$self->{"lineage"}
Truth table100%2/2
A
A= $self->{"lineage"}
Aresult
11
00
832 165 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 111 54 165
833 111 my $pts = shift;
834 111 my(@lineage, $pcoords);
835 111 ▶ if ($pts) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pts) 54 57 111
836 54 foreach my $pcoord ($pts->lineage) {
837 20 push(@lineage, [@$pcoord]);
838 }
839 54 $pcoords = [$pts->depth, $pts->count, $pts->{rc}, $pts->{cc}];
840 54 push(@lineage, $pcoords);
841 }
842 111 $self->{lineage} = \@lineage;
843 }
844 165 @{$self->{lineage}};
845 }
846  
847 36 ▶ sub rows { shift->_rows(0) }
Subroutine rows: called CC=1 SCAR=0.0
848  
849 sub space_rows {
850 0 ▶ my $self = shift;
Subroutine space_rows: not called CC=1 SCAR=6.9
851 0 $self->_rows(1);
852 }
853  
854 sub _rows {
855 36 ▶ my $self = shift;
Subroutine _rows: called CC=4 SCAR=14.0
856 36 my $alias = shift;
857 36 my @ri = $self->row_indices;
858 36 my @rows;
859 36 ▶ my $grid = $alias ? $self->_gridalias : $self->{grid};
Branch: 1 branch
BranchTrueFalseTotal
$alias ? : 0 36 36
860 36 foreach ($self->row_indices) {
861 218 push(@rows, scalar $self->_slice_and_normalize_row($grid->[$_]));
862 }
863 36 ▶ wantarray ? @rows : \@rows;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 36 0 36
864 }
865  
866 sub columns {
867 0 ▶ my $self = shift;
Subroutine columns: not called CC=4 SCAR=30.0
868 0 my @cols;
869 0 my @rows = $self->rows;
870 0 foreach my $row (@rows) {
871 0 foreach my $c (0 .. $#$row) {
872 0 ▶ $cols[$c] ||= [];
Condition0%0/2
$cols[$c] ||= []
l!l
00
MC/DC0%0/1
$cols[$c] ||= []
$cols[$c]
Truth table0%0/2
A
A= $cols[$c]
Aresult
11
00
873 0 push(@{$cols[$c]}, $row->[$c]);
874 }
875 }
876 0 @cols;
877 }
878  
879 sub row_indices {
880 78 ▶ my $self = shift;
Subroutine row_indices: called CC=3 SCAR=11.0
881 78 my $start_index = 0;
882 78 ▶ if ($self->{headers}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"headers"}) 38 40 78
883 38 $start_index = $self->hrow_index;
884 38 ▶ $start_index += 1 unless $self->{keep_headers};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"keep_headers"} 29 9 38
885 }
886 78 $start_index .. $#{$self->{grid}};
887 }
888  
889 sub col_indices {
890 0 ▶ my $self = shift;
Subroutine col_indices: not called CC=1 SCAR=6.9
891 0 my $row = $self->{grid}[0];
892 0 0 .. $#$row;
893 }
894  
895 sub row {
896 6 ▶ my $self = shift;
Subroutine row: called CC=3 SCAR=11.6
897 6 my $r = shift;
898 6 ▶ $r <= $#{$self->{grid}}
Branch: 1 branch
BranchTrueFalseTotal
$r <= $#{$$self{"grid"};} or croak(qq[row $r out of range ($#{$$self{"grid"};})\n]) 0 6 6
899 0 or croak "row $r out of range ($#{$self->{grid}})\n";
900 my @row = $self->_slice_and_normalize_row(
901 6 $self->{grid}[($self->row_indices)[$r]]
902 );
903 6 ▶ wantarray ? @row : \@row;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 6 6
904 }
905  
906 sub _slice_and_normalize_row {
907 224 ▶ my $self = shift;
Subroutine _slice_and_normalize_row: called CC=4 SCAR=13.9
908 224 my $rowref = shift;
909 224 my @row;
910 224 ▶ if ($self->{automap} && $self->_map_makes_a_difference) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'automap'} and $self->_map_makes_a_difference) { } 128 96 224
Condition66%2/3
$self->{"automap"} and $self->_map_makes_a_difference
!ll&&!rl&&r
096128
MC/DC50%1/2
$self->{"automap"} and $self->_map_makes_a_difference
$self->{"automap"} $self->_map_makes_a_difference
Truth table66%2/3
A and B
A= $self->{"automap"}B= $self->_map_makes_a_difference
ABresult
0X0
100
111
911 128 @row = @{$rowref}[$self->column_map];
912 }
913 else {
914 96 @row = @$rowref;
915 }
916 224 @row = map($self->_cell_to_content($_), @row);
917 224 ▶ wantarray ? @row : \@row;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 6 218 224
918 }
919  
920 sub column {
921 0 ▶ my $self = shift;
Subroutine column: not called CC=3 SCAR=24.8
922 0 my $c = shift;
923 0 my @column;
924 0 foreach my $row ($self->rows) {
925 0 push(@column, $self->cell($row, $c));
926 }
927 0 ▶ wantarray ? @column : \@column;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
928 }
929  
930 sub cell {
931 4 ▶ my $self = shift;
Subroutine cell: called CC=2 SCAR=7.0
932 4 my($r, $c) = @_;
933 4 my $row = $self->row($r);
934 4 ▶ $c <= $#$row or croak "Column $c out of range ($#$row)\n";
Branch: 1 branch
BranchTrueFalseTotal
$c <= $row->$#* or croak("Column $c out of range ($row->$#*)\n") 0 4 4
935 4 $self->_cell_to_content($row->[$c]);
936 }
937  
938 sub _cell_to_content {
939 877 ▶ my $self = shift;
Subroutine _cell_to_content: called CC=4 SCAR=14.0
940 877 ▶ @_ or croak "cell item required\n";
Branch: 1 branch
BranchTrueFalseTotal
@_ or croak("cell item required\n") 0 877 877
941 877 my $cell = shift;
942 877 ▶ return $cell unless ref $cell;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cell 4 873 877
943 873 ▶ return $cell if TREE();
Branch: 1 branch
BranchTrueFalseTotal
if TREE() 0 873 873
944 873 return $$cell;
945 }
946  
947 sub space {
948 2 ▶ my $self = shift;
Subroutine space: called CC=3 SCAR=11.2
949 2 my($r, $c) = @_;
950 2 my $gridalias = $self->_gridalias;
951 2 ▶ $r <= $#$gridalias
Branch: 1 branch
BranchTrueFalseTotal
$r <= $gridalias->$#* or croak("row $r out of range ($gridalias->$#*)\n") 0 2 2
952 or croak "row $r out of range ($#$gridalias)\n";
953 2 my $row = $gridalias->[$r];
954 2 ▶ $c <= $#$row or croak "Column $c out of range ($#$row)\n";
Branch: 1 branch
BranchTrueFalseTotal
$c <= $row->$#* or croak("Column $c out of range ($row->$#*)\n") 0 2 2
955 2 $self->_cell_to_content($row->[$c]);
956 }
957  
958 sub source_coords {
959 28 ▶ my $self = shift;
Subroutine source_coords: called CC=3 SCAR=11.4
960 28 my($r, $c) = @_;
961 28 ▶ $r <= $#{$self->{translation}}
Branch: 1 branch
BranchTrueFalseTotal
$r <= $#{$$self{"translation"};} or croak(qq[row $r out of range ($#{$$self{"translation"};})\n]) 0 28 28
962 0 or croak "row $r out of range ($#{$self->{translation}})\n";
963 28 my $row = $self->{translation}[$r];
964 28 ▶ $c <= $#$row or croak "Column $c out of range ($#$row)\n";
Branch: 1 branch
BranchTrueFalseTotal
$c <= $row->$#* or croak("Column $c out of range ($row->$#*)\n") 0 28 28
965 28 split(/,/, $self->{translation}[$r][$c]);
966 }
967  
968 sub hrow_index {
969 41 ▶ my $self = shift;
Subroutine hrow_index: called CC=1 SCAR=0.0
970 41 $self->{hrow_index};
971 }
972  
973 sub hrow {
974 3 ▶ my $self = shift;
Subroutine hrow: called CC=5 SCAR=21.7
975 3 ▶ if ($self->{automap} && $self->_map_makes_a_difference) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'automap'} and $self->_map_makes_a_difference) { } 3 0 3
Condition33%1/3
$self->{"automap"} and $self->_map_makes_a_difference
!ll&&!rl&&r
003
MC/DC0%0/2
$self->{"automap"} and $self->_map_makes_a_difference
$self->{"automap"} $self->_map_makes_a_difference
Truth table33%1/3
A and B
A= $self->{"automap"}B= $self->_map_makes_a_difference
ABresult
0X0
100
111
976 3 ▶ return map(ref $_ ? $$_ : $_, @{$self->{hrow}}[$self->column_map]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 15 0 15
977 }
978 else {
979 0 ▶ return map(ref $_ ? $$_ : $_, @{$self->{hrow}});
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 0 0
980 }
981 }
982  
983 sub column_map {
984 # Return the column numbers of this table in the same order as the
985 # provided headers.
986 419 ▶ my $self = shift;
Subroutine column_map: called CC=3 SCAR=11.0
987 419 ▶ if ($self->{headers}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'headers'}) { } 344 75 419
988 # First we order the original column counts by taking a hash slice
989 # based on the original header order. The resulting original
990 # column numbers are mapped to the actual content indices since
991 # we could have a sparse slice.
992 344 my %order;
993 344 foreach (keys %{$self->{hits}}) {
994 1026 $order{$self->{hits}{$_}} = $_;
995 }
996 344 return @order{@{$self->{headers}}};
997 }
998 else {
999 75 return 0 .. $#{$self->{grid}[0]};
1000 }
1001 }
1002  
1003 sub _map_makes_a_difference {
1004 227 ▶ my $self = shift;
Subroutine _map_makes_a_difference: called CC=6 SCAR=17.9
1005 227 ▶ return 0 unless $self->{slice_columns};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"slice_columns"} 2 225 227
1006 225 my $diff = 0;
1007 225 my @order = $self->column_map;
1008 225 my @sorder = sort { $a <=> $b } @order;
1009 225 ▶ ++$diff if $#order != $#sorder;
Branch: 1 branch
BranchTrueFalseTotal
if $#order != $#sorder 0 225 225
1010 225 ▶ ++$diff if $#sorder != $#{$self->{grid}[0]};
Branch: 1 branch
BranchTrueFalseTotal
if $#sorder != $#{$$self{"grid"}[0];} 131 94 225
1011 225 foreach (0 .. $#order) {
1012 597 ▶ if ($order[$_] != $sorder[$_]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($order[$_] != $sorder[$_]) 131 466 597
1013 131 ++$diff;
1014 131 last;
1015 }
1016 }
1017 225 $diff;
1018 }
1019  
1020 sub _add_text {
1021 9322 ▶ my($self, $txt) = @_;
Subroutine _add_text: called CC=1 SCAR=0.0
1022 9322 my $r = $self->{rc};
1023 9322 my $c = $self->{cc};
1024 9322 my $row = $self->{grid}[$r];
1025 9322 ${$row->[$c]} .= $txt;
1026 9322 $txt;
1027 }
1028  
1029 sub _reset_hits {
1030 70 ▶ my $self = shift;
Subroutine _reset_hits: called CC=3 SCAR=11.0
1031 70 ▶ return unless $self->{headers};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"headers"} 0 70 70
1032 70 $self->{hits} = {};
1033 70 $self->{order} = [];
1034 70 foreach (@{$self->{headers}}) {
1035 254 ++$self->{hits_left}{$_};
1036 }
1037 70 1;
1038 }
1039  
1040 9425 ▶ sub _rasterizer { shift->{_rastamon} }
Subroutine _rasterizer: called CC=1 SCAR=0.0
1041  
1042 sub report {
1043 # Print out a summary of this table, including depth/count
1044 0 ▶ my($self, $include_content, $col_sep) = @_;
Subroutine report: not called CC=4 SCAR=30.0
1045 0 ▶ $col_sep ||= ':';
Condition0%0/2
$col_sep ||= ":"
l!l
00
MC/DC0%0/1
$col_sep ||= ":"
$col_sep
Truth table0%0/2
A
A= $col_sep
Aresult
11
00
1046 0 my $str;
1047 0 $str .= "TABLE(" . $self->depth . ", " . $self->count . ')';
1048 0 ▶ if ($include_content) {
Branch: 1 branch
BranchTrueFalseTotal
if ($include_content) { } 0 0 0
1049 0 $str .= ":\n";
1050 0 foreach my $row ($self->rows) {
1051 0 $str .= join($col_sep, @$row) . "\n";
1052 }
1053 }
1054 else {
1055 0 $str .= "\n";
1056 }
1057 0 $str;
1058 }
1059  
1060 sub dump {
1061 0 ▶ my $self = shift;
Subroutine dump: not called CC=1 SCAR=6.9
1062 0 $self->_emsg($self->report(@_));
1063 }
1064  
1065 sub _emsg {
1066 0 ▶ my $self = shift;
Subroutine _emsg: not called CC=1 SCAR=6.9
1067 0 my $fh = $self->{error_handle};
1068 0 print $fh @_;
1069 }
1070  
1071 }
1072  
1073 ##########
1074  
1075 {
1076  
1077 11 package HTML::TableExtract::Rasterize;
1078  
1079 # Provide a closure that will rasterize (turn into a grid) a table
1080 # from a tree structure based on repeated data element calls with
1081 # rowspan and colspan information. Not as straight forward as it
1082 # seems...see test cases for an example bugaboo.
1083  
1084 11 my $DEBUG = 0;
1085  
1086 sub make_rasterizer {
1087 111 ▶ my $pkg = shift;
Subroutine make_rasterizer: called CC=1 SCAR=0.1
1088 111 my(@grid, @row_spinner, @col_spinner);
1089 111 my $empty_row_offset = 0;
1090 sub {
1091 9425 ▶ return \@grid unless @_;
Subroutine __ANON__: called CC=33 SCAR=39.0
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 113 9312 9425
1092 9312 my($row_num, $rspan, $cspan) = @_;
1093 9312 ▶ $rspan = 1 unless $rspan > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $rspan > 1 9306 6 9312
1094 9312 ▶ $cspan = 1 unless $cspan > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $cspan > 1 9303 9 9312
1095 9312 my($rspin_propogate, $row_added);
1096 9312 my $trigger = $#grid + $empty_row_offset;
1097 9312 ▶ if ($row_num > $trigger) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row_num > $trigger) 984 8328 9312
1098 # adjust for having been handed a row that skips a prior row,
1099 # otherwise the next cell will land in a wrong row. Hopefully
1100 # this doesn't happen too often but I've seen it in the wild!
1101 984 ▶ if ($row_num - $trigger > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row_num - $trigger > 1) 0 984 984
1102 0 $empty_row_offset += $row_num - $trigger - 1;
1103 }
1104 # add new row
1105 984 $row_added = 1;
1106 984 my @new_row;
1107 # first add new row spinner
1108 984 ▶ if ($row_spinner[-1] && $col_spinner[-1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row_spinner[-1] and $col_spinner[-1]) { } 0 984 984
Condition33%1/3
$row_spinner[-1] and $col_spinner[-1]
!ll&&!rl&&r
98400
MC/DC0%0/2
$row_spinner[-1] and $col_spinner[-1]
$row_spinner[-1] $col_spinner[-1]
Truth table33%1/3
A and B
A= $row_spinner[-1]B= $col_spinner[-1]
ABresult
0X0
100
111
1109 0 push(@row_spinner, $row_spinner[-1]);
1110 0 $rspin_propogate = 1;
1111 }
1112 else {
1113 984 push(@row_spinner, $cspan - 1);
1114 }
1115 # spin columns
1116 984 foreach (@col_spinner) {
1117 8382 ▶ if ($_) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_) { } 15 8367 8382
1118 15 push(@new_row, 0);
1119 15 --$_;
1120 }
1121 else {
1122 8367 push(@new_row, undef);
1123 }
1124 }
1125 984 ▶ @new_row = (undef) unless @new_row;
Branch: 1 branch
BranchTrueFalseTotal
unless @new_row 111 873 984
1126 984 push(@grid, \@new_row);
1127 }
1128 9312 my $current_row = $grid[-1];
1129 # locate next available cell in row
1130 9312 my $col;
1131 9312 foreach my $ci (0 .. $#$current_row) {
1132 49821 ▶ if (! defined $current_row->[$ci]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $current_row->[$ci]) 8463 41358 49821
1133 8463 $col = $ci;
1134 8463 last;
1135 }
1136 }
1137 9312 ▶ if (! defined $col) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $col) 849 8463 9312
1138 849 ▶ ADDCOL: while (! defined $col) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $col) 849 0 849
1139 # if no cells were available, add a column
1140 849 foreach my $ri (0 .. $#grid) {
1141 849 my $row = $grid[$ri];
1142 849 my $cspan_count = $row_spinner[$ri];
1143 849 ▶ if (!$cspan_count) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $cspan_count) { } 849 0 849
1144 849 push(@$row, undef);
1145 }
1146 else {
1147 0 push(@$row, 0);
1148 0 --$row_spinner[$ri];
1149 }
1150 }
1151 849 push(@col_spinner, $col_spinner[-1]);
1152 849 foreach my $ci (0 .. $#$current_row) {
1153 4989 ▶ if (! defined $current_row->[$ci]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $current_row->[$ci]) 849 4140 4989
1154 849 $col = $ci;
1155 849 last ADDCOL;
1156 }
1157 }
1158 }
1159 849 ▶ $col_spinner[-1] = $rspan - 1 if $col == $#$current_row;
Branch: 1 branch
BranchTrueFalseTotal
if $col == $current_row->$#* 849 0 849
1160 849 $row_spinner[$#grid] = $cspan - 1;
1161 }
1162  
1163 # we now have correct coordinates for this element
1164 9312 $current_row->[$col] = [$rspan, $cspan];
1165 9312 $col_spinner[$col] = $rspan - 1;
1166  
1167 # if this is an embedded placement (not a trailing element), use up
1168 # the cspan
1169 9312 ▶ if ($col < $#$current_row) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col < $current_row->$#*) 7482 1830 9312
1170 7482 my $offset = 1;
1171 7482 my $row_span = $col_spinner[$col];
1172 7482 ▶ if ($col + $row_spinner[-1] < $#$current_row &&
Branch: 1 branch
BranchTrueFalseTotal
if ($col + $row_spinner[-1] < $current_row->$#* and $row_added and not $rspin_propogate) 870 6612 7482
Condition83%5/6
$col + $row_spinner[-1] < $current_row->$#* and $row_added and not $rspin_propogate
!ll&&!rl&&r
66120870
$col + $row_spinner[-1] < $current_row->$#* and $row_added
!ll&&!rl&&r
36609870
MC/DC66%2/3
$col + $row_spinner[-1] < $current_row->$#* and $row_added and not $rspin_propogate
$col + $row_spinner[-1] < $current_row->$#* $row_added not $rspin_propogate
Truth table75%3/4
A and B and not(C)
A= $col + $row_spinner[-1] < $current_row->$#*B= $row_addedC= not $rspin_propogate
ABCresult
0XX0
10X0
1100
1111
1173 $row_added && !$rspin_propogate) {
1174 # cell is spun out -- clear spinner unless it inherited cspan
1175 # from a cell above
1176 870 $row_spinner[-1] = 0;
1177 }
1178 7482 ▶ while ($offset < $cspan) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < $cspan) 15 7479 7494
1179 15 my $cursor = $col + $offset;
1180 15 $current_row->[$cursor] = 0;
1181 15 $col_spinner[$cursor] = $row_span;
1182 15 ++$offset;
1183 15 ▶ if ($col + $offset > $#$current_row) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col + $offset > $current_row->$#*) 3 12 15
1184 3 $row_spinner[-1] = $cspan - $offset;
1185 3 last;
1186 }
1187 }
1188 }
1189  
1190 9312 ▶ if ($DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DEBUG) 0 9312 9312
1191 0 foreach my $r (0 .. $#grid) {
1192 0 my $row = $grid[$r];
1193 0 foreach my $c (0 .. $#$row) {
1194 0 ▶ if (defined $row->[$c]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $row->[$c]) { } 0 0 0
1195 0 ▶ print STDERR $row->[$c] ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$row->[$c] ? : 0 0 0
1196 }
1197 else {
1198 0 print STDERR '?';
1199 }
1200 }
1201 0 print STDERR " $row_spinner[$r]\n";
1202 }
1203 0 print STDERR "\n";
1204 0 foreach (@col_spinner) {
1205 0 ▶ print STDERR defined $_ ? $_ : '?';
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
1206 }
1207 0 print STDERR "\n\n-----\n\n";
1208 }
1209  
1210 9312 return \@grid;
1211 }
1212 111 }
1213  
1214 }
1215  
1216 ##########
1217  
1218 {
1219  
1220 11 package HTML::TableExtract::StripHTML;
1221  
1222 11 ▶ use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
1223  
1224 11 ▶ use HTML::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
1225 11 @ISA = qw(HTML::Parser);
1226  
1227 sub tag {
1228 0 ▶ my($self, $tag, $num) = @_;
Subroutine tag: not called CC=1 SCAR=6.9
1229 0 $self->{_htes_inside}{$tag} += $num;
1230 }
1231  
1232 sub text {
1233 0 ▶ my $self = shift;
Subroutine text: not called CC=3 SCAR=24.8
1234 0 ▶ return if $self->{_htes_inside}{script} || $self->{_htes_inside}{style};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_htes_inside"}{"script"} or $self->{"_htes_inside"}{"style"} 0 0 0
Condition0%0/3
$self->{"_htes_inside"}{"script"} or $self->{"_htes_inside"}{"style"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"_htes_inside"}{"script"} or $self->{"_htes_inside"}{"style"}
$self->{"_htes_inside"}{"script"} $self->{"_htes_inside"}{"style"}
Truth table0%0/3
A or B
A= $self->{"_htes_inside"}{"script"}B= $self->{"_htes_inside"}{"style"}
ABresult
1X1
011
000
1235 0 $self->{_htes_tidbit} .= $_[0];
1236 }
1237  
1238 sub new {
1239 0 ▶ my $class = shift;
Subroutine new: not called CC=1 SCAR=6.9
1240 0 my $self = HTML::Parser->new(
1241 api_version => 3,
1242 handlers => [start => [\&tag, "self, tagname, '+1'"],
1243 end => [\&tag, "self, tagname, '-1'"],
1244 text => [\&text, "self, dtext"],
1245 ],
1246 marked_sections => 1,
1247 );
1248 0 bless $self, $class;
1249 }
1250  
1251 sub strip {
1252 0 ▶ my $self = shift;
Subroutine strip: not called CC=1 SCAR=6.9
1253 0 $self->parse(shift);
1254 0 $self->eof;
1255 0 $self->{_htes_tidbit};
1256 }
1257  
1258 }
1259  
1260 11 1;
1261  
1262 __END__
↑ Summary