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

filter: Statementstmt 0.0% 0 / 114 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 32 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 217 CC 21 SCAR 34.3
CC
21
Cov
0%
insertRow
90.0
deleteRow
42.0
init
12.0
CRAP 31.0
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 use strict;
18 use warnings;
19 warnings::register_categories( 'HTML::Object' );
20 use parent qw( HTML::Object::DOM::Element );
21 use vars qw( $VERSION );
22 use HTML::Object::DOM::Element::Shared qw( :tablesection );
23 our $VERSION = 'v0.2.0';
24 };
25  
26 use strict;
27 use warnings;
28  
29 sub init
30 {
31 my $self = shift( @_ );
32 # could also be tbody o tfoot
33 $self->{tag} = 'thead' if( !defined( $self->{tag} ) || !CORE::length( "$self->{tag}" ) );
34 $self->{_init_strict_use_sub} = 1;
35 $self->SUPER::init( @_ ) || return( $self->pass_error );
36 $self->{_section_reset} = 1;
37 my $callback = sub
38 {
39 my $def = shift( @_ );
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 unless( $def->{caller}->[0] eq ref( $self ) ||
43 $def->{caller}->[0] eq 'HTML::Object::DOM::Element::Table' )
44 {
45 $self->reset(1);
46 }
47 return(1);
48 };
49 $self->children->callback( add => $callback );
50 $self->children->callback( remove => $callback );
51 return( $self );
52 }
53  
54 # Note: deprecated property align is inherited
55  
56 # Note: deprecated property ch
57 sub ch : lvalue { return( shift->_set_get_property( 'ch', @_ ) ); }
58  
59 # Note: deprecated property chOff
60 sub chOff : lvalue { return( shift->_set_get_property( 'choff', @_ ) ); }
61  
62 sub deleteRow
63 {
64 my $self = shift( @_ );
65 my $pos = shift( @_ );
66 return( $self->error({
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 my $rows = $self->rows;
71 my $size = $rows->size;
72 return( $self->error({
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 return( $self->error({
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 $pos = ( $rows->length + $pos ) if( $pos < 0 );
81 my $elem = $rows->index( $pos );
82 my $children = $self->children;
83 my $kid_pos = $children->pos( $elem );
84 return( $self->error({
85 message => "Unable to find the row element among this table children!",
86 class => 'HTML::Object::HierarchyRequestError',
87 }) ) if( !defined( $kid_pos ) );
88 $children->splice( $kid_pos, 1 );
89 $elem->parent( undef );
90 $self->reset(1);
91 return( $elem );
92 }
93  
94 sub insertRow
95 {
96 my $self = shift( @_ );
97 my $pos = shift( @_ );
98 my $rows = $self->rows;
99 my $size = $rows->size;
100 if( defined( $pos ) )
101 {
102 return( $self->error({
103 message => "Value provided (" . overload::StrVal( $pos // '' ) . ") is not an integer.",
104 class => 'HTML::Object::IndexSizeError',
105 }) ) if( !$self->_is_integer( $pos ) );
106 return( $self->error({
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 return( $self->error({
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 $pos = ( $rows->length + $pos ) if( $pos < 0 );
115 }
116 $self->_load_class( 'HTML::Object::DOM::Element::TableRow' ) || return( $self->pass_error );
117 my $children = $self->children;
118 my $row = HTML::Object::DOM::Element::TableRow->new( @_ ) ||
119 return( $self->pass_error( HTML::Object::DOM::Element::TableRow->error ) );
120 $row->close;
121 # A position was provided
122 if( defined( $pos ) )
123 {
124 # ..., but there are no rows yet
125 if( $rows->is_empty )
126 {
127 $children->push( $row );
128 }
129 else
130 {
131 my $elem = $rows->index( $pos );
132 my $kid_pos = $children->pos( $elem );
133 if( !defined( $kid_pos ) )
134 {
135 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 $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 $children->push( $row );
147 }
148 $row->parent( $self );
149 $self->reset(1);
150 return( $row );
151 }
152  
153 sub reset
154 {
155 my $self = shift( @_ );
156 if( scalar( @_ ) )
157 {
158 $self->_reset_section;
159 return( $self->SUPER::reset( @_ ) );
160 }
161 return( $self );
162 }
163  
164 # Note: property rows read-only
165 sub rows
166 {
167 my $self = shift( @_ );
168 return( $self->{_section_rows} ) if( $self->{_section_rows} && !$self->_is_section_reset );
169 my $list = $self->children->grep(sub{ $self->_is_a( $_ => 'HTML::Object::DOM::Element::TableRow' ) });
170 unless( $self->{_section_rows} )
171 {
172 $self->_load_class( 'HTML::Object::DOM::Collection' ) || return( $self->pass_error );
173 $self->{_section_rows} = HTML::Object::DOM::Collection->new ||
174 return( $self->pass_error( HTML::Object::DOM::Collection->error ) );
175 }
176 $self->{_section_rows}->set( $list );
177 $self->_remove_section_reset;
178 return( $self->{_section_rows} );
179 }
180  
181 # Note: deprecated property vAlign
182 sub vAlign : lvalue { return( shift->_set_get_property( 'valign', @_ ) ); }
183  
184 sub _is_section_reset { return( CORE::length( shift->{_section_reset} ) ); }
185  
186 sub _remove_section_reset { return( CORE::delete( shift->{_section_reset} ) ); }
187  
188 sub _reset_section
189 {
190 my $self = shift( @_ );
191 $self->{_section_reset}++;
192 # Force cells object update
193 $self->rows;
194 if( my $parent = $self->parent )
195 {
196 $parent->_reset_table( $self->{tag} ) if( $parent->can( '_reset_table' ) );
197 }
198 return( $self );
199 }
200  
201 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