TableContent/Parser.pm

filter: Statementstmt 94.3% 117 / 124 Branchbran 100% 44 / 44 Conditioncond 83.3% 5 / 6 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 87.5% 21 / 24 Podpod 53.8% 7 / 13 totaltotal 91.6% 197 / 215 CC 28 SCAR 14.5
CC
28
Cov
95%
start
10.0
_add_table
5.0
text
5.0
CRAP 4.3
« TableContent.pm ↑ Summary TableContent/Table/Row.pm »
1 package HTML::TableContent::Parser;
2  
3 18 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 18 our $VERSION = '1.00';
6  
7 18 extends 'HTML::Parser';
8  
9 18 ▶use HTML::TableContent::Table;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 has [qw(current_tables nested caption_selectors)] => (
12 is => 'rw',
13 lazy => 1,
14 clearer => 1,
15 323 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
16 18 );
17  
18 18 has [qw(current_table current_element selected)] => (
19 is => 'rw',
20 lazy => 1,
21 clearer => 1,
22 );
23  
24 18 has options => (
25 is => 'ro',
26 lazy => 1,
27 builder => 1,
28 );
29  
30 2938 ▶sub has_caption_selector { return scalar @{ $_[0]->caption_selectors } ? 1 : 0 }
Subroutine has_caption_selector: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
scalar @{$_[0]->caption_selectors;} ? : 2352 586 2938
31  
32 8679 ▶sub count_nested { return scalar @{ $_[0]->nested }; }
Subroutine count_nested: called CC=1 SCAR=0.0
Pod undocumented
33  
34 7809 ▶sub has_nested { return $_[0]->count_nested ? 1 : 0; }
Subroutine has_nested: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->count_nested ? : 898 6911 7809
35  
36 789 ▶sub get_last_nested { return $_[0]->nested->[ $_[0]->count_nested - 1 ]; }
Subroutine get_last_nested: called CC=1 SCAR=0.0
Pod undocumented
37  
38 sub clear_last_nested {
39 81 ▶ return delete $_[0]->nested->[ $_[0]->count_nested - 1 ];
Subroutine clear_last_nested: called CC=1 SCAR=0.0
Pod undocumented
40 }
41  
42 0 ▶sub all_current_tables { return @{ $_[0]->current_tables }; }
Subroutine all_current_tables: not called CC=1 SCAR=6.9
Pod documented
43  
44 0 ▶sub count_current_tables { return scalar @{ $_[0]->current_tables }; }
Subroutine count_current_tables: not called CC=1 SCAR=6.9
Pod documented
45  
46 sub current_or_nested {
47 7433 ▶ return $_[0]->has_nested ? $_[0]->get_last_nested : $_[0]->current_table;
Subroutine current_or_nested: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->has_nested ? : 789 6644 7433
48 }
49  
50 sub parse {
51 435 ▶ my ( $self, $data ) = @_;
Subroutine parse: called CC=1 SCAR=0.0
Pod documented
52  
53 435 $self->SUPER::parse($data);
54  
55 435 return $self->current_tables;
56 }
57  
58 sub parse_file {
59 61 ▶ my ( $self, $file ) = @_;
Subroutine parse_file: called CC=1 SCAR=0.0
Pod documented
60  
61 61 $self->SUPER::parse_file($file);
62  
63 61 return $self->current_tables;
64 }
65  
66 sub start {
67 6659 ▶ my ( $self, $tag, $attr, $attrseq, $origtext ) = @_;
Subroutine start: called CC=10 SCAR=23.0
Pod documented
68  
69 6659 ▶ if ($self->current_element && $attr->{href}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->current_element and $attr->{"href"}) 262 6397 6659
Condition100%3/3
$self->current_element and $attr->{"href"}
!ll&&!rl&&r
15384859262
MC/DC100%2/2
$self->current_element and $attr->{"href"}
$self->current_element $attr->{"href"}
Truth table100%3/3
A and B
A= $self->current_elementB= $attr->{"href"}
ABresult
0X0
100
111
70 262 push @{ $self->current_element->links }, $attr->{href};
71 }
72  
73 6659 $tag = lc $tag;
74 6659 ▶ if ( my $option = $self->options->{$tag} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $option = $self->options->{$tag}) 3721 2938 6659
75 3721 my $table = $self->current_or_nested;
76 3721 my $action = $option->{add};
77 3721 my $element = $self->$action($attr, $table);
78 3721 return $self->current_element($element);
79 }
80  
81 2938 ▶ if ( $self->has_caption_selector ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_caption_selector) 2352 586 2938
82 2352 foreach my $selector ( @{ $self->caption_selectors }) {
83 3069 ▶ if ( $selector eq $tag ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($selector eq $tag) 38 3031 3069
84 38 return $self->selected($attr);
85 }
86
87 3031 for my $field (qw/id class/) {
88 6060 my $val = $attr->{$field};
89 6060 ▶ next unless $val;
Branch: 1 branch
BranchTrueFalseTotal
unless $val 4167 1893 6060
90
91 1893 ▶ if ( $val =~ m/$selector/ixms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /$selector/imsx) 2 1891 1893
92 2 return $self->selected($attr);
93 }
94 }
95 }
96 }
97  
98 2898 return;
99 }
100  
101 sub text {
102 8204 ▶ my ( $self, $text ) = @_;
Subroutine text: called CC=5 SCAR=16.1
Pod documented
103  
104 8204 ▶ if ( my $elem = $self->current_element ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $elem = $self->current_element) 6418 1786 8204
105 6418 ▶ if ( $text =~ m{\S+}xms ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\S+/msx) 2728 3690 6418
106 2728 $text =~ s{^\s+|\s+$}{}g;
107 2728 push @{ $elem->data }, $text;
108 }
109 }
110 8204 ▶ if ( my $selected = $self->selected) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $selected = $self->selected) 370 7834 8204
111 370 ▶ if ( $text =~ m{\S+}xms ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\S+/msx) 317 53 370
112 317 $selected->{text} = $text;
113 317 $self->selected($selected);
114 }
115 }
116  
117 8204 return;
118 }
119  
120 sub end {
121 6526 ▶ my ( $self, $tag, $origtext ) = @_;
Subroutine end: called CC=3 SCAR=11.0
Pod documented
122  
123 6526 $tag = lc $tag;
124  
125 6526 ▶ if ( my $option = $self->options->{$tag} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $option = $self->options->{$tag}) 3712 2814 6526
126 3712 my $table = $self->current_or_nested;
127 3712 ▶ if ( my $action = $option->{close} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $action = $option->{"close"}) 1352 2360 3712
128 1352 my $element = $self->$action($table);
129 }
130 }
131  
132 6526 return;
133 }
134  
135 sub _build_options {
136 return {
137 122 ▶ table => {
Subroutine _build_options: called CC=1 SCAR=0.0
138 add => '_add_table',
139 close => '_close_table',
140 },
141 th => {
142 add => '_add_header',
143 },
144 tr => {
145 add => '_add_row',
146 close => '_close_row',
147 },
148 td => {
149 add => '_add_cell',
150 },
151 caption => {
152 add => '_add_caption'
153 }
154 };
155 }
156  
157 sub _add_header {
158 547 ▶ my ($self, $attr, $table) = @_;
Subroutine _add_header: called CC=1 SCAR=0.0
159  
160 547 my $header = $table->add_header($attr);
161 547 $table->get_last_row->header($header);
162 547 return $header;
163 }
164  
165 sub _add_row {
166 1062 ▶ my ($self, $attr, $table) = @_;
Subroutine _add_row: called CC=1 SCAR=0.0
167  
168 1062 my $row = $table->add_row($attr);
169 1062 return $row;
170 }
171  
172 sub _add_cell {
173 1816 ▶ my ($self, $attr, $table) = @_;
Subroutine _add_cell: called CC=1 SCAR=0.0
174  
175 1816 my $cell = $table->get_last_row->add_cell($attr);
176 1816 $table->parse_to_column($cell);
177 1816 return $cell;
178 }
179  
180 sub _add_caption {
181 0 ▶ my ($self, $attr, $table) = @_;
Subroutine _add_caption: not called CC=1 SCAR=6.9
182  
183 0 my $caption = $table->add_caption($attr);
184 0 return $caption;
185 }
186  
187 sub _add_table {
188 296 ▶ my ($self, $attr, $table) = @_;
Subroutine _add_table: called CC=5 SCAR=16.1
189  
190 296 my $element = HTML::TableContent::Table->new($attr);
191  
192 296 ▶ if ( defined $table && $table->isa('HTML::TableContent::Table') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $table and $table->isa('HTML::TableContent::Table')) { } 81 215 296
Condition66%2/3
defined $table and $table->isa("HTML::TableContent::Table")
!ll&&!rl&&r
215081
MC/DC50%1/2
defined $table and $table->isa("HTML::TableContent::Table")
defined $table $table->isa("HTML::TableContent::Table")
Truth table66%2/3
A and B
A= defined $tableB= $table->isa("HTML::TableContent::Table")
ABresult
0X0
100
111
193 81 ▶ if ( $self->has_nested ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_nested) 28 53 81
194 28 push @{ $self->current_table->nested }, $element;
195 }
196 81 push @{ $self->nested }, $element;
197 81 push @{ $table->nested }, $element;
198 81 push @{ $table->get_last_row->get_last_cell->nested }, $element;
199 }
200 else {
201 215 ▶ if ( my $caption = $self->selected ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $caption = $self->selected) 18 197 215
202 18 $element->add_caption($caption);
203 18 $self->clear_selected;
204 }
205 215 $self->current_table($element);
206 }
207 }
208  
209 sub _close_table {
210 295 ▶ my ($self, $table) = @_;
Subroutine _close_table: called CC=2 SCAR=6.9
211  
212 295 ▶ if ( $self->has_nested ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_nested) { } 81 214 295
213 81 return $self->clear_last_nested;
214 }
215 else {
216 214 push @{ $self->current_tables }, $self->current_table;
217 214 $self->clear_current_element;
218 214 return $self->clear_current_table;
219 }
220 }
221  
222 sub _close_row {
223 1057 ▶ my ($self, $table) = @_;
Subroutine _close_row: called CC=5 SCAR=16.1
224  
225 1057 my $row = $table->get_last_row;
226  
227 1057 ▶ if ( $row->header ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($row->header) { } 247 810 1057
elsif ($row->cell_count == 0) { } 10 800 810
228 247 $table->clear_last_row;
229  
230 247 my $index = 0;
231 247 foreach my $cell ( $row->all_cells ) {
232 39 my $row = $table->rows->[$index];
233 39 ▶ if ( defined $row ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $row) { } 26 13 39
234 26 push @{ $row->cells }, $cell;
235 }
236 else {
237 13 my $new_row = $table->add_row({});
238 13 push @{ $new_row->cells }, $cell;
239 }
240 39 $index++;
241 }
242 }
243 elsif ( $row->cell_count == 0 ) {
244 10 $table->clear_last_row;
245 }
246  
247 1057 return;
248 }
249  
250 18 1;
251  
252 __END__
« TableContent.pm ↑ Summary TableContent/Table/Row.pm »