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

filter: Statementstmt 0.0% 0 / 130 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 36 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 25 Podpod 0.0% 0 / 18 totaltotal 0.0% 0 / 253 CC 23 SCAR 34.8
CC
23
Cov
0%
insertCell
110.0
deleteCell
42.0
init
12.0
CRAP 32.3
1 ##----------------------------------------------------------------------------
2 ## HTML Object - ~/lib/HTML/Object/DOM/Element/TableRow.pm
3 ## Version v0.2.0
4 ## Copyright(c) 2022 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2022/01/07
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::TableRow;
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( :tablerow );
23 our $VERSION = 'v0.2.0';
24 };
25  
26 use strict;
27 use warnings;
28  
29 sub init
30 {
31 my $self = shift( @_ );
32 $self->{_init_strict_use_sub} = 1;
33 $self->SUPER::init( @_ ) || return( $self->pass_error );
34 $self->{tag} = 'tr' if( !defined( $self->{tag} ) || !CORE::length( "$self->{tag}" ) );
35 $self->{_row_reset} = 1;
36 my $callback = sub
37 {
38 my $def = shift( @_ );
39 # Our children were modified from outside our package.
40 # We need to check if it affects our rows and reset the cache accordingly
41 unless( $def->{caller}->[0] eq ref( $self ) ||
42 $def->{caller}->[0] eq 'HTML::Object::DOM::Element::Table' )
43 {
44 $self->reset(1);
45 }
46 return(1);
47 };
48 $self->children->callback( add => $callback );
49 $self->children->callback( remove => $callback );
50 return( $self );
51 }
52  
53 # Note: deprecated property align is inherited
54  
55 # Note: deprecated property bgColor
56 sub bgColor : lvalue { return( shift->_set_get_property( 'bgcolor', @_ ) ); }
57  
58 sub bgcolor : lvalue { return( shift->bgColor( @_ ) ); }
59  
60 # Note: property cells read-only
61 sub cells
62 {
63 my $self = shift( @_ );
64 return( $self->{_row_cells} ) if( $self->{_row_cells} && !$self->_is_row_reset );
65 my $list = $self->children->grep(sub{ $self->_is_a( $_ => 'HTML::Object::DOM::Element::TableCell' ) });
66 # The content of the collection is refreshed, but the collection object itself does not change, so the user can poll it
67 unless( $self->{_row_cells} )
68 {
69 $self->_load_class( 'HTML::Object::DOM::Collection' ) || return( $self->pass_error );
70 $self->{_row_cells} = HTML::Object::DOM::Collection->new ||
71 return( $self->pass_error( HTML::Object::DOM::Collection->error ) );
72 }
73 $self->{_row_cells}->set( $list );
74 $self->_remove_row_reset;
75 return( $self->{_row_cells} );
76 }
77  
78 # Note: deprecated property ch
79 sub ch : lvalue { return( shift->_set_get_property( 'ch', @_ ) ); }
80  
81 # Note: deprecated property chOff
82 sub chOff : lvalue { return( shift->_set_get_property( 'choff', @_ ) ); }
83  
84 sub choff : lvalue { return( shift->chOff( @_ ) ); }
85  
86 sub deleteCell
87 {
88 my $self = shift( @_ );
89 my $pos = shift( @_ );
90 return( $self->error({
91 message => "Value provided (" . overload::StrVal( $pos // '' ) . ") is not an integer.",
92 class => 'HTML::Object::IndexSizeError',
93 }) ) if( !defined( $pos ) || !$self->_is_integer( $pos ) );
94 my $cells = $self->cells;
95 my $size = $cells->cells;
96 return( $self->error({
97 message => "Value provided ($pos) is greater than the zero-based number of cells available (${size}).",
98 class => 'HTML::Object::IndexSizeError',
99 }) ) if( $pos > $size );
100 return( $self->error({
101 message => "Value provided ($pos) is lower than the zero-based number of cells available (${size}). If you want to specify a negative index, it must be between -1 and -${size}",
102 class => 'HTML::Object::IndexSizeError',
103 }) ) if( $pos < 0 && abs( $pos ) > $size );
104 $pos = ( $cells->length + $pos ) if( $pos < 0 );
105 my $elem = $cells->index( $pos );
106 my $children = $self->children;
107 my $kid_pos = $children->pos( $elem );
108 return( $self->error({
109 message => "Unable to find the cell element among this row children!",
110 class => 'HTML::Object::HierarchyRequestError',
111 }) ) if( !defined( $kid_pos ) );
112 $children->splice( $kid_pos, 1 );
113 $elem->parent( undef );
114 $self->reset(1);
115 return( $elem );
116 }
117  
118 sub insertCell
119 {
120 my $self = shift( @_ );
121 my $pos = shift( @_ );
122 my $cells = $self->cells;
123 my $size = $cells->size;
124 if( defined( $pos ) )
125 {
126 return( $self->error({
127 message => "Value provided (" . overload::StrVal( $pos // '' ) . ") is not an integer.",
128 class => 'HTML::Object::IndexSizeError',
129 }) ) if( !$self->_is_integer( $pos ) );
130 return( $self->error({
131 message => "Value provided ($pos) is greater than the zero-based number of cells available (${size}).",
132 class => 'HTML::Object::IndexSizeError',
133 }) ) if( $pos > $size );
134 return( $self->error({
135 message => "Value provided ($pos) is lower than the zero-based number of cells available (${size}). If you want to specify a negative index, it must be between -1 and -${size}",
136 class => 'HTML::Object::IndexSizeError',
137 }) ) if( $pos < 0 && abs( $pos ) > $size );
138 $pos = ( $cells->length + $pos ) if( $pos < 0 );
139 }
140 $self->_load_class( 'HTML::Object::DOM::Element::TableCell' ) || return( $self->pass_error );
141 my $children = $self->children;
142 my $cell = HTML::Object::DOM::Element::TableCell->new( @_ ) ||
143 return( $self->pass_error( HTML::Object::DOM::Element::TableCell->error ) );
144 $cell->close;
145 # A position was provided
146 if( defined( $pos ) )
147 {
148 # ..., but there are no cells yet
149 if( $cells->is_empty )
150 {
151 $children->push( $cell );
152 }
153 else
154 {
155 my $elem = $cells->index( $pos );
156 return( $self->error({
157 message => "No element could be found at row index $pos",
158 class => 'HTML::Object::HierarchyRequestError',
159 }) ) if( !defined( $elem ) );
160 my $kid_pos = $children->pos( $elem );
161 if( !defined( $kid_pos ) )
162 {
163 return( $self->error({
164 message => "Could not find a value at offset $pos (translated to $kid_pos among the row children) amazingly enough.",
165 class => 'HTML::Object::HierarchyRequestError',
166 }) );
167 }
168 $children->splice( $kid_pos, 0, $cell );
169 }
170 }
171 # otherwise, there are already other cells directly under <tr> and the new cell is just added at the end of the row.
172 else
173 {
174 $children->push( $cell );
175 }
176 $cell->parent( $self );
177 $self->reset(1);
178 return( $cell );
179 }
180  
181 sub reset
182 {
183 my $self = shift( @_ );
184 if( scalar( @_ ) )
185 {
186 $self->_reset_row;
187 return( $self->SUPER::reset( @_ ) );
188 }
189 return( $self );
190 }
191  
192 # Note: property rowIndex read-only
193 sub rowIndex
194 {
195 my $self = shift( @_ );
196 my $parent = $self->parent || return;
197 my $siblings = $parent->children;
198 my $pos = $siblings->pos( $self );
199 return( $pos );
200 }
201  
202 # Note: property sectionRowIndex read-only
203 sub sectionRowIndex
204 {
205 my $self = shift( @_ );
206 my $parent = $self->parent || return;
207 if( $self->_is_a( $parent => 'HTML::Object::DOM::Element::TableSection' ) )
208 {
209 return( $parent->children->pos( $self ) );
210 }
211 return;
212 }
213  
214 # Note: deprecated property vAlign
215 sub vAlign : lvalue { return( shift->_set_get_property( 'valign', @_ ) ); }
216  
217 sub valign : lvalue { return( shift->vAlign( @_ ) ); }
218  
219 sub _is_row_reset { return( CORE::length( shift->{_row_reset} ) ); }
220  
221 sub _remove_row_reset { return( CORE::delete( shift->{_row_reset} ) ); }
222  
223 sub _reset_row
224 {
225 my $self = shift( @_ );
226 $self->{_row_reset}++;
227 # Force it to recompute
228 $self->cells;
229 if( my $parent = $self->parent )
230 {
231 $parent->_reset_section if( $parent->can( '_reset_section' ) );
232 }
233 return( $self );
234 }
235  
236 1;
237 # NOTE: POD
238 __END__
239  
240 =encoding utf-8
241  
242 =head1 NAME
243  
244 HTML::Object::DOM::Element::TableRow - HTML Object DOM TableRow Class
245  
246 =head1 SYNOPSIS
247  
248 use HTML::Object::DOM::Element::TableRow;
249 my $tablerow = HTML::Object::DOM::Element::TableRow->new ||
250 die( HTML::Object::DOM::Element::TableRow->error, "\n" );
251  
252 =head1 VERSION
253  
254 v0.2.0
255  
256 =head1 DESCRIPTION
257  
258 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 rows (i.e. C<tr>) in an HTML table.
259  
260 =head1 INHERITANCE
261  
262 +-----------------------+ +---------------------------+ +-------------------------+ +----------------------------+ +--------------------------------------+
263 | HTML::Object::Element | --> | HTML::Object::EventTarget | --> | HTML::Object::DOM::Node | --> | HTML::Object::DOM::Element | --> | HTML::Object::DOM::Element::TableRow |
264 +-----------------------+ +---------------------------+ +-------------------------+ +----------------------------+ +--------------------------------------+
265  
266 =head1 PROPERTIES
267  
268 Inherits properties from its parent L<HTML::Object::DOM::Element>
269  
270 =head2 cells
271  
272 Read-only.
273  
274 Returns a L<collection|HTML::Object::DOM::Collection> containing the cells in the row, but only the ones directly under the row element.
275  
276 Note that for performance improvement, the collection is cached until changes are made that would affect the results.
277  
278 Example:
279  
280 <table id="myTable1">
281 <tr>
282 <td>
283 <table id="myTable2">
284 <tr><td></td></tr>
285 </table>
286 </td>
287 <td></td>
288 </tr>
289 </table>
290  
291 say getElementById('myTable1')->rows->[0]->cells->length; # 2
292 say getElementById('myTable2')->rows->[0]->cells->length; # 1
293  
294 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableRowElement/cells>
295  
296 =head2 rowIndex
297  
298 Read-only.
299  
300 Returns a long value which gives the logical position of the row within the entire table. If the row is not part of a table, returns C<undef> (C<-1> under JavaScript).
301  
302 Example:
303  
304 <table>
305 <thead>
306 <tr><th>Item</th> <th>Price</th></tr>
307 </thead>
308 <tbody>
309 <tr><td>Bananas</td> <td>$2</td></tr>
310 <tr><td>Oranges</td> <td>$8</td></tr>
311 <tr><td>Top Sirloin</td> <td>$20</td></tr>
312 </tbody>
313 <tfoot>
314 <tr><td>Total</td> <td>$30</td></tr>
315 </tfoot>
316 </table>
317  
318 Another example:
319  
320 my $rows = $doc->querySelectorAll('tr');
321  
322 $rows->forEach(sub
323 {
324 my $row = shift( @_ );
325 my $z = $doc->createElement('td');
326 $z->textContent = "(row #" . $row->rowIndex . ")";
327 row->appendChild($z);
328 });
329  
330 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableRowElement/rowIndex>
331  
332 =head2 sectionRowIndex
333  
334 Read-only.
335  
336 Returns a long value which gives the logical position of the row within the table section it belongs to. If the row is not part of a section, returns C<undef> (C<-1> under JavaScript).
337  
338 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableRowElement/sectionRowIndex>
339  
340 =head1 METHODS
341  
342 Inherits methods from its parent L<HTML::Object::DOM::Element>
343  
344 =head2 deleteCell
345  
346 Removes the cell corresponding to the C<index> given in parameter. If the C<index> value is C<-1> the last cell is removed; if it smaller than C<-1> or greater than the amount of cells in the collection, an C<HTML::Object::IndexSizeError> is returned.
347  
348 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableRowElement/deleteCell>
349  
350 =head2 insertCell
351  
352 Returns an L<HTML::Object::DOM::Element::TableCell> representing a new cell of the table. It inserts it in the L<cells collection|/cells> immediately before a C<td> or C<th> element at the given C<index> position, if any was provided.
353  
354 If the C<index> is not given or is C<-1>, the new cell is appended to the collection. If the C<index> is smaller than C<-1>, it will start that far back from the end of the collection array. If C<index> is greater than the number of cells in the collection, an C<HTML::Object::IndexSizeError> error is returned.
355  
356 Example:
357  
358 <table id="my-table">
359 <tr><td>Row 1</td></tr>
360 <tr><td>Row 2</td></tr>
361 <tr><td>Row 3</td></tr>
362 </table>
363  
364 sub addRow
365 {
366 my $tableID = shift( @_ );
367 # Get a reference to the table
368 my $tableRef = $doc->getElementById( $tableID );
369  
370 # Insert a row at the end of the table
371 my $newRow = $tableRef->insertRow( -1 );
372  
373 # Insert a cell in the row at index 0
374 my $newCell = $newRow->insertCell( 0 );
375  
376 # Append a text node to the cell
377 my $newText = $doc->createTextNode('New bottom row');
378 $newCell->appendChild( $newText );
379 }
380  
381 # Call addRow() with the table's ID
382 addRow( 'my-table' );
383  
384 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableRowElement/insertCell>
385  
386 =head1 DEPRECATED PROPERTIES
387  
388 =head2 align
389  
390 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".
391  
392 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableRowElement/align>
393  
394 =head2 bgColor
395  
396 Is a string containing the background color of the cells. It reflects the obsolete bgcolor attribute.
397  
398 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableRowElement/bgColor>
399  
400 =head2 bgcolor
401  
402 Alias for L</bgColor>
403  
404 =head2 ch
405  
406 Is a string containing one single character. 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.
407  
408 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableRowElement/ch>
409  
410 =head2 chOff
411  
412 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.
413  
414 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableRowElement/chOff>
415  
416 =head2 choff
417  
418 Alias for L</chOff>
419  
420 =head2 reset
421  
422 When called, this will set a boolean value indicating the cached data must be recomputed. This allow the saving of computational power,
423  
424 =head2 vAlign
425  
426 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".
427  
428 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableRowElement/vAlign>
429  
430 =head2 valign
431  
432 Alias for L</vAlign>
433  
434 =head1 AUTHOR
435  
436 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
437  
438 =head1 SEE ALSO
439  
440 L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableRowElement>, L<Mozilla documentation on tablerow element|https://developer.mozilla.org/en-US/docs/Web/HTML/Element/tablerow>
441  
442 =head1 COPYRIGHT & LICENSE
443  
444 Copyright(c) 2022 DEGUEST Pte. Ltd.
445  
446 All rights reserved
447  
448 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
449  
450 =cut