lib/HTML/Object/DOM/Element/TableSection.pm

filter: Statementstmt 71.1% 74 / 104 Branchbran 36.9% 17 / 46 Conditioncond 34.6% 9 / 26 MC/DCmcdc 25.0% 4 / 16 Subroutinesub 80.9% 17 / 21 Podpod 100% 8 / 8 totaltotal 58.3% 129 / 221 CC 34 SCAR 34.2
CC
34
Cov
57%
deleteRow
90.0
insertRow
57.3
rows
6.0
CRAP 30.5
« blib/lib/HTML/Object/DOM/Element/TableSection.pm ↑ Summary lib/HTML/Object/DOM/Element/TableRow.pm »
1 ##----------------------------------------------------------------------------
2 ## HTML Object - ~/lib/HTML/Object/DOM/Element/TableSection.pm
3 ## Version v0.2.0
4 ## Copyright(c) 2021 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2021/12/23
7 ## Modified 2022/09/18
8 ## All rights reserved
9 ##
10 ##
11 ## This program is free software; you can redistribute it and/or modify it
12 ## under the same terms as Perl itself.
13 ##----------------------------------------------------------------------------
14 package HTML::Object::DOM::Element::TableSection;
15 BEGIN
16 {
17 2 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶ warnings::register_categories( 'HTML::Object' );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶ use parent qw( HTML::Object::DOM::Element );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶ use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶ use HTML::Object::DOM::Element::Shared qw( :tablesection );
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 our $VERSION = 'v0.2.0';
24 };
25  
26 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 sub init
30 {
31 6 ▶ my $self = shift( @_ );
Subroutine init: called CC=4 SCAR=14.0
Pod documented
32 # could also be tbody o tfoot
33 6 ▶ $self->{tag} = 'thead' if( !defined( $self->{tag} ) || !CORE::length( "$self->{tag}" ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"tag"} and length qq[$self->{"tag"}] 6 0 6
Condition33%1/3
defined $self->{"tag"} and length qq[$self->{"tag"}]
!ll&&!rl&&r
600
MC/DC0%0/2
defined $self->{"tag"} and length qq[$self->{"tag"}]
defined $self->{"tag"} length qq[$self->{"tag"}]
Truth table33%1/3
A and B
A= defined $self->{"tag"}B= length qq[$self->{"tag"}]
ABresult
0X0
100
111
34 6 $self->{_init_strict_use_sub} = 1;
35 6 ▶ $self->SUPER::init( @_ ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init(@_) or return $self->pass_error 0 6 6
36 6 $self->{_section_reset} = 1;
37 my $callback = sub
38 {
39 15 ▶ my $def = shift( @_ );
Subroutine __ANON__: called CC=3 SCAR=11.0
40 # Our children were modified from outside our package.
41 # We need to check if it affects our rows and reset the cache accordingly
42 15 ▶ unless( $def->{caller}->[0] eq ref( $self ) ||
Branch: 1 branch
BranchTrueFalseTotal
unless ($def->{"caller"}[0] eq ref $self or $def->{"caller"}[0] eq "HTML::Object::DOM::Element::Table") 13 2 15
Condition100%3/3
$def->{"caller"}[0] eq ref $self or $def->{"caller"}[0] eq "HTML::Object::DOM::Element::Table"
l!l&&r!l&&!r
1113
MC/DC100%2/2
$def->{"caller"}[0] eq ref $self or $def->{"caller"}[0] eq "HTML::Object::DOM::Element::Table"
$def->{"caller"}[0] eq ref $self $def->{"caller"}[0] eq "HTML::Object::DOM::Element::Table"
Truth table100%3/3
A or B
A= $def->{"caller"}[0] eq ref $selfB= $def->{"caller"}[0] eq "HTML::Object::DOM::Element::Table"
ABresult
1X1
011
000
43 $def->{caller}->[0] eq 'HTML::Object::DOM::Element::Table' )
44 {
45 13 $self->reset(1);
46 }
47 15 return(1);
48 6 };
49 6 $self->children->callback( add => $callback );
50 6 $self->children->callback( remove => $callback );
51 6 return( $self );
52 }
53  
54 # Note: deprecated property align is inherited
55  
56 # Note: deprecated property ch
57 0 ▶sub ch : lvalue { return( shift->_set_get_property( 'ch', @_ ) ); }
Subroutine ch: not called CC=1 SCAR=6.9
Pod documented
58  
59 # Note: deprecated property chOff
60 0 ▶sub chOff : lvalue { return( shift->_set_get_property( 'choff', @_ ) ); }
Subroutine chOff: not called CC=1 SCAR=6.9
Pod documented
61  
62 sub deleteRow
63 {
64 0 ▶ my $self = shift( @_ );
Subroutine deleteRow: not called CC=9 SCAR=45.0
Pod documented
65 0 my $pos = shift( @_ );
66 0 ▶ return( $self->error({
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pos and $self->_is_integer($pos) 0 0 0
Condition0%0/5
defined $pos and $self->_is_integer($pos)
!ll&&!rl&&r
000
$pos // ""
l!l
00
MC/DC0%0/2
defined $pos and $self->_is_integer($pos)
defined $pos $self->_is_integer($pos)
MC/DC0%0/1
$pos // ""
$pos
Truth table0%0/3
A and B
A= defined $posB= $self->_is_integer($pos)
ABresult
0X0
100
111
Truth table0%0/2
A
A= $pos
Aresult
11
00
67 message => "Value provided (" . overload::StrVal( $pos // '' ) . ") is not an integer.",
68 class => 'HTML::Object::IndexSizeError',
69 }) ) if( !defined( $pos ) || !$self->_is_integer( $pos ) );
70 0 my $rows = $self->rows;
71 0 my $size = $rows->size;
72 0 ▶ return( $self->error({
Branch: 1 branch
BranchTrueFalseTotal
if $pos > $size 0 0 0
73 message => "Value provided ($pos) is greater than the zero-based number of rows available (" . $rows->size . ").",
74 class => 'HTML::Object::IndexSizeError',
75 }) ) if( $pos > $size );
76 0 ▶ return( $self->error({
Branch: 1 branch
BranchTrueFalseTotal
if $pos < 0 and abs $pos > $size 0 0 0
Condition0%0/3
$pos < 0 and abs $pos > $size
!ll&&!rl&&r
000
MC/DC0%0/2
$pos < 0 and abs $pos > $size
$pos < 0 abs $pos > $size
Truth table0%0/3
A and B
A= $pos < 0B= abs $pos > $size
ABresult
0X0
100
111
77 message => "Value provided ($pos) is lower than the zero-based number of rows available (" . $rows->size . "). If you want to specify a negative index, it must be between -1 and -${size}",
78 class => 'HTML::Object::IndexSizeError',
79 }) ) if( $pos < 0 && abs( $pos ) > $size );
80 0 ▶ $pos = ( $rows->length + $pos ) if( $pos < 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $pos < 0 0 0 0
81 0 my $elem = $rows->index( $pos );
82 0 my $children = $self->children;
83 0 my $kid_pos = $children->pos( $elem );
84 0 ▶ return( $self->error({
Branch: 1 branch
BranchTrueFalseTotal
unless defined $kid_pos 0 0 0
85 message => "Unable to find the row element among this table children!",
86 class => 'HTML::Object::HierarchyRequestError',
87 }) ) if( !defined( $kid_pos ) );
88 0 $children->splice( $kid_pos, 1 );
89 0 $elem->parent( undef );
90 0 $self->reset(1);
91 0 return( $elem );
92 }
93  
94 sub insertRow
95 {
96 1 ▶ my $self = shift( @_ );
Subroutine insertRow: called CC=13 SCAR=40.5
Pod documented
97 1 my $pos = shift( @_ );
98 1 my $rows = $self->rows;
99 1 my $size = $rows->size;
100 1 ▶ if( defined( $pos ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pos) 0 1 1
101 {
102 0 ▶ return( $self->error({
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_integer($pos) 0 0 0
Condition0%0/2
$pos // ""
l!l
00
MC/DC0%0/1
$pos // ""
$pos
Truth table0%0/2
A
A= $pos
Aresult
11
00
103 message => "Value provided (" . overload::StrVal( $pos // '' ) . ") is not an integer.",
104 class => 'HTML::Object::IndexSizeError',
105 }) ) if( !$self->_is_integer( $pos ) );
106 0 ▶ return( $self->error({
Branch: 1 branch
BranchTrueFalseTotal
if $pos > $size 0 0 0
107 message => "Value provided ($pos) is greater than the zero-based number of rows available (" . $rows->size . ").",
108 class => 'HTML::Object::IndexSizeError',
109 }) ) if( $pos > $size );
110 0 ▶ return( $self->error({
Branch: 1 branch
BranchTrueFalseTotal
if $pos < 0 and abs $pos > $size 0 0 0
Condition0%0/3
$pos < 0 and abs $pos > $size
!ll&&!rl&&r
000
MC/DC0%0/2
$pos < 0 and abs $pos > $size
$pos < 0 abs $pos > $size
Truth table0%0/3
A and B
A= $pos < 0B= abs $pos > $size
ABresult
0X0
100
111
111 message => "Value provided ($pos) is lower than the zero-based number of rows available (" . $rows->size . "). If you want to specify a negative index, it must be between -1 and -${size}",
112 class => 'HTML::Object::IndexSizeError',
113 }) ) if( $pos < 0 && abs( $pos ) > $size );
114 0 ▶ $pos = ( $rows->length + $pos ) if( $pos < 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $pos < 0 0 0 0
115 }
116 1 ▶ $self->_load_class( 'HTML::Object::DOM::Element::TableRow' ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("HTML::Object::DOM::Element::TableRow") or return $self->pass_error 0 1 1
117 1 my $children = $self->children;
118 1 ▶ my $row = HTML::Object::DOM::Element::TableRow->new( @_ ) ||
Condition50%1/2
'HTML::Object::DOM::Element::TableRow'->new(@_) || (return $self->pass_error('HTML::Object::DOM::Element::TableRow'->error))
l!l
10
MC/DC0%0/1
'HTML::Object::DOM::Element::TableRow'->new(@_) || (return $self->pass_error('HTML::Object::DOM::Element::TableRow'->error))
'HTML::Object::DOM::Element::TableRow'->new(@_)
Truth table50%1/2
A
A= 'HTML::Object::DOM::Element::TableRow'->new(@_)
Aresult
11
00
119 return( $self->pass_error( HTML::Object::DOM::Element::TableRow->error ) );
120 1 $row->close;
121 # A position was provided
122 1 ▶ if( defined( $pos ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pos) { } 0 1 1
123 {
124 # ..., but there are no rows yet
125 0 ▶ if( $rows->is_empty )
Branch: 1 branch
BranchTrueFalseTotal
if ($rows->is_empty) { } 0 0 0
126 {
127 0 $children->push( $row );
128 }
129 else
130 {
131 0 my $elem = $rows->index( $pos );
132 0 my $kid_pos = $children->pos( $elem );
133 0 ▶ if( !defined( $kid_pos ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $kid_pos) 0 0 0
134 {
135 0 return( $self->error({
136 message => "Could not find a value at offset $pos (translated to $kid_pos among the table children) amazingly enough.",
137 class => 'HTML::Object::HierarchyRequestError',
138 }) );
139 }
140 0 $children->splice( $kid_pos, 0, $row );
141 }
142 }
143 # otherwise, there are already other rows directly under <table> and the new row is just added at the end of the table, even if there is a <tfoot> element.
144 else
145 {
146 1 $children->push( $row );
147 }
148 1 $row->parent( $self );
149 1 $self->reset(1);
150 1 return( $row );
151 }
152  
153 sub reset
154 {
155 337 ▶ my $self = shift( @_ );
Subroutine reset: called CC=2 SCAR=6.9
Pod documented
156 337 ▶ if( scalar( @_ ) )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_) 63 274 337
157 {
158 63 $self->_reset_section;
159 63 return( $self->SUPER::reset( @_ ) );
160 }
161 274 return( $self );
162 }
163  
164 # Note: property rows read-only
165 sub rows
166 {
167 149 ▶ my $self = shift( @_ );
Subroutine rows: called CC=6 SCAR=18.0
Pod documented
168 149 ▶ return( $self->{_section_rows} ) if( $self->{_section_rows} && !$self->_is_section_reset );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_section_rows"} and not $self->_is_section_reset 40 109 149
Condition100%3/3
$self->{"_section_rows"} and not $self->_is_section_reset
!ll&&!rl&&r
610340
MC/DC100%2/2
$self->{"_section_rows"} and not $self->_is_section_reset
$self->{"_section_rows"} not $self->_is_section_reset
Truth table100%3/3
A and not(B)
A= $self->{"_section_rows"}B= not $self->_is_section_reset
ABresult
0X0
100
111
169 109 ▶ my $list = $self->children->grep(sub{ $self->_is_a( $_ => 'HTML::Object::DOM::Element::TableRow' ) });
Subroutine __ANON__: called CC=1 SCAR=0.0
170 109 ▶ unless( $self->{_section_rows} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_section_rows"}) 6 103 109
171 {
172 6 ▶ $self->_load_class( 'HTML::Object::DOM::Collection' ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("HTML::Object::DOM::Collection") or return $self->pass_error 0 6 6
173 6 ▶ $self->{_section_rows} = HTML::Object::DOM::Collection->new ||
Condition50%1/2
'HTML::Object::DOM::Collection'->new || (return $self->pass_error('HTML::Object::DOM::Collection'->error))
l!l
60
MC/DC0%0/1
'HTML::Object::DOM::Collection'->new || (return $self->pass_error('HTML::Object::DOM::Collection'->error))
'HTML::Object::DOM::Collection'->new
Truth table50%1/2
A
A= 'HTML::Object::DOM::Collection'->new
Aresult
11
00
174 return( $self->pass_error( HTML::Object::DOM::Collection->error ) );
175 }
176 109 $self->{_section_rows}->set( $list );
177 109 $self->_remove_section_reset;
178 109 return( $self->{_section_rows} );
179 }
180  
181 # Note: deprecated property vAlign
182 0 ▶sub vAlign : lvalue { return( shift->_set_get_property( 'valign', @_ ) ); }
Subroutine vAlign: not called CC=1 SCAR=6.9
Pod documented
183  
184 143 ▶sub _is_section_reset { return( CORE::length( shift->{_section_reset} ) ); }
Subroutine _is_section_reset: called CC=1 SCAR=0.0
185  
186 109 ▶sub _remove_section_reset { return( CORE::delete( shift->{_section_reset} ) ); }
Subroutine _remove_section_reset: called CC=1 SCAR=0.0
187  
188 sub _reset_section
189 {
190 109 ▶ my $self = shift( @_ );
Subroutine _reset_section: called CC=3 SCAR=11.0
191 109 $self->{_section_reset}++;
192 # Force cells object update
193 109 $self->rows;
194 109 ▶ if( my $parent = $self->parent )
Branch: 1 branch
BranchTrueFalseTotal
if (my $parent = $self->parent) 89 20 109
195 {
196 89 ▶ $parent->_reset_table( $self->{tag} ) if( $parent->can( '_reset_table' ) );
Branch: 1 branch
BranchTrueFalseTotal
if $parent->can("_reset_table") 89 0 89
197 }
198 109 return( $self );
199 }
200  
201 2 1;
202 # NOTE: POD
203 __END__
204  
205 =encoding utf-8
206  
207 =head1 NAME
208  
209 HTML::Object::DOM::Element::TableSection - HTML Object DOM TableSection Class
210  
211 =head1 SYNOPSIS
212  
213 use HTML::Object::DOM::Element::TableSection;
214 my $section = HTML::Object::DOM::Element::TableSection->new ||
215 die( HTML::Object::DOM::Element::TableSection->error, "\n" );
216  
217 =head1 VERSION
218  
219 v0.2.0
220  
221 =head1 DESCRIPTION
222  
223 This interface provides special properties and methods (beyond the L<HTML::Object::DOM::Element> interface it also has available to it by inheritance) for manipulating the layout and presentation of sections, that is headers, footers and bodies, in an HTML table.
224  
225 =head1 INHERITANCE
226  
227 +-----------------------+ +---------------------------+ +-------------------------+ +----------------------------+ +------------------------------------------+
228 | HTML::Object::Element | --> | HTML::Object::EventTarget | --> | HTML::Object::DOM::Node | --> | HTML::Object::DOM::Element | --> | HTML::Object::DOM::Element::TableSection |
229 +-----------------------+ +---------------------------+ +-------------------------+ +----------------------------+ +------------------------------------------+
230  
231 =head1 PROPERTIES
232  
233 Inherits properties from its parent L<HTML::Object::DOM::Element>
234  
235 =head2 rows
236  
237 Read-only.
238  
239 Returns a live L<collection|HTML::Object::DOM::Collection> containing the rows in the section. The L<collection|HTML::Object::DOM::Collection> is live and is automatically updated when rows are added or removed.
240  
241 Note that for performance improvement, the collection is cached until changes are made that would affect the results.
242  
243 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableSectionElement/rows>
244  
245 =head1 METHODS
246  
247 Inherits methods from its parent L<HTML::Object::DOM::Element>
248  
249 =head2 deleteRow
250  
251 Removes the row, corresponding to the index given in parameter, in the section. If the index value is C<-1> the last row is removed; if it smaller than C<-1> or greater than the amount of rows in the collection, an C<HTML::Object::IndexSizeError> is returned.
252  
253 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableSectionElement/deleteRow>
254  
255 =head2 insertRow
256  
257 Returns an L<HTML::Object::DOM::Element::TableRow> representing a new row of the table. It inserts it in the rows collection immediately before the C<tr> element at the given index position, if any was provided.
258  
259 If the index is not given or is C<-1>, the new row is appended to the collection. If the index is smaller than C<-1>, it will start that far back from the end of the collection array. If index is greater than the number of rows in the collection, an C<HTML::Object::IndexSizeError> error is returned.
260  
261 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableSectionElement/insertRow>
262  
263 =head2 reset
264  
265 Reset the cache flag so that some data will be recomputed. The cache is design to avoid doing useless computing repeatedly when there is no change of data.
266  
267 =head1 DEPRECATED PROPERTIES
268  
269 =head2 align
270  
271 Is a string containing an enumerated value reflecting the align attribute. It indicates the alignment of the element's contents with respect to the surrounding context. The possible values are "left", "right", and "center".
272  
273 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableSectionElement/align>
274  
275 =head2 ch
276  
277 Is a string containing one single chararcter. This character is the one to align all the cell of a column on. It reflects the char and default to the decimal points associated with the language, e.g. '.' for English, or ',' for French. This property was optional and was not very well supported.
278  
279 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableSectionElement/ch>
280  
281 =head2 chOff
282  
283 Is a string containing a integer indicating how many characters must be left at the right (for left-to-right scripts; or at the left for right-to-left scripts) of the character defined by L<HTML::Object::DOM::Element::C<TableRow>>.ch. This property was optional and was not very well supported.
284  
285 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableSectionElement/chOff>
286  
287 =head2 vAlign
288  
289 Is a string representing an enumerated value indicating how the content of the cell must be vertically aligned. It reflects the valign attribute and can have one of the following values: "top", "middle", "bottom", or "baseline".
290  
291 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableSectionElement/vAlign>
292  
293 =head1 AUTHOR
294  
295 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
296  
297 =head1 SEE ALSO
298  
299 L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableSectionElement>, L<Mozilla documentation on tbody element|https://developer.mozilla.org/en-US/docs/Web/HTML/Element/tbody>, L<Mozilla documentation on thead element|https://developer.mozilla.org/en-US/docs/Web/HTML/Element/thead>, L<Mozilla documentation on tfoot element|https://developer.mozilla.org/en-US/docs/Web/HTML/Element/tfoot>, L<W3C specifications|https://html.spec.whatwg.org/multipage/tables.html>
300  
301 =head1 COPYRIGHT & LICENSE
302  
303 Copyright(c) 2021 DEGUEST Pte. Ltd.
304  
305 All rights reserved
306  
307 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
308  
309 =cut
« blib/lib/HTML/Object/DOM/Element/TableSection.pm ↑ Summary lib/HTML/Object/DOM/Element/TableRow.pm »