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

filter: Statementstmt 0.0% 0 / 317 Branchbran 0.0% 0 / 102 Conditioncond 0.0% 0 / 111 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 41 Podpod 0.0% 0 / 33 totaltotal 0.0% 0 / 604 CC 52 SCAR 40.5
CC
52
Cov
0%
insertRow
210.0
_create_tsection
110.0
_set_get_section
56.0
CRAP 57.6
1 ##----------------------------------------------------------------------------
2 ## HTML Object - ~/lib/HTML/Object/DOM/Element/Table.pm
3 ## Version v0.3.0
4 ## Copyright(c) 2022 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2021/12/23
7 ## Modified 2025/10/16
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::Table;
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( :table );
23 use Wanted;
24 our $VERSION = 'v0.3.0';
25 };
26  
27 use strict;
28 use warnings;
29  
30 sub init
31 {
32 my $self = shift( @_ );
33 $self->{_init_strict_use_sub} = 1;
34 $self->SUPER::init( @_ ) || return( $self->pass_error );
35 $self->{tag} = 'table' if( !defined( $self->{tag} ) || !CORE::length( "$self->{tag}" ) );
36 $self->{_table_reset_caption} = 1;
37 $self->{_table_reset_tbody} = 1;
38 $self->{_table_reset_tfoot} = 1;
39 $self->{_table_reset_thead} = 1;
40 $self->{_table_reset_rows} = 1;
41 $self->{_reset_fields} = [qw( _table_reset_caption _table_reset_tbody _table_reset_tfoot _table_reset_thead _table_reset_rows )];
42 my $callback = sub
43 {
44 my $def = shift( @_ );
45 # my $info = [caller(5)];
46 # print( STDERR ref( $self ), "::children->callback(add): called from package ", $info->[0], " at line ", $info->[2], " for '", $def->{added}->[0], "'\n" );
47
48 # Our children were modified from outside our package.
49 # We need to check if it affects our rows and reset the cache accordingly
50 unless( $def->{caller}->[0] eq ref( $self ) )
51 {
52 # my $has_row = 0;
53 # for( @{$def->{data}} )
54 # {
55 # $has_row++, last if( $self->_is_a( $_ => 'HTML::Object::DOM::Element::TableRow' ) );
56 # }
57 # $self->_reset_table( 'rows' ) if( $has_row );
58 $self->_reset_table( 'all' );
59 }
60 return(1);
61 };
62 $self->children->callback( add => $callback );
63 $self->children->callback( remove => $callback );
64 return( $self );
65 }
66  
67 # Note: deprecated property align is inherited
68  
69 # Note: deprecated property bgColor
70 sub bgColor : lvalue { return( shift->_set_get_property( 'bgcolor', @_ ) ); }
71  
72 # Note: deprecated property border
73 sub border : lvalue { return( shift->_set_get_property( 'border', @_ ) ); }
74  
75 # Note: property caption
76 sub caption : lvalue { return( shift->_set_get_section( caption => 'HTML::Object::DOM::Element::TableCaption', @_ ) ); }
77  
78 # Note: deprecated property cellPadding
79 sub cellPadding : lvalue { return( shift->_set_get_property( 'cellpadding', @_ ) ); }
80  
81 # Note: deprecated property cellSpacing
82 sub cellSpacing : lvalue { return( shift->_set_get_property( 'cellspacing', @_ ) ); }
83  
84 sub createCaption
85 {
86 my $self = shift( @_ );
87 if( $self->{_table_captions} && !$self->_is_table_reset( 'caption' ) && !$self->{_table_captions}->is_empty )
88 {
89 return( $self->{_table_captions}->first );
90 }
91  
92 my $list = $self->children->grep(sub{ $self->_is_a( $_ => 'HTML::Object::DOM::Element::TableCaption' ) });
93 my $capt = $list->first;
94 if( !$capt )
95 {
96 $self->_load_class( 'HTML::Object::DOM::Element::TableCaption' ) ||
97 return( $self->pass_error );
98 $capt = HTML::Object::DOM::Element::TableCaption->new( @_ ) ||
99 return( $self->pass_error( HTML::Object::DOM::Element::TableCaption->error ) );
100 $capt->close;
101 $capt->parent( $self );
102 $self->children->unshift( $capt );
103 $list->unshift( $capt );
104 $self->reset(1);
105 $self->_load_class( 'HTML::Object::DOM::Collection' ) ||
106 return( $self->pass_error );
107 $self->{_table_captions} = HTML::Object::DOM::Collection->new( $list );
108 $self->_remove_table_reset( 'caption' );
109 }
110 return( $capt );
111 }
112  
113 sub createTBody { return( shift->_create_tsection( tbody => @_ ) ); }
114  
115 sub createTFoot { return( shift->_create_tsection( tfoot => @_ ) ); }
116  
117 sub createTHead { return( shift->_create_tsection( thead => @_ ) ); }
118  
119 sub deleteCaption { return( shift->_delete_first_element( caption => @_ ) ); }
120  
121 sub deleteRow
122 {
123 my $self = shift( @_ );
124 my $pos = shift( @_ );
125 return( $self->error({
126 message => "Value provided (" . overload::StrVal( $pos // '' ) . ") is not an integer.",
127 class => 'HTML::Object::IndexSizeError',
128 }) ) if( !defined( $pos ) || !$self->_is_integer( $pos ) );
129 my $rows = $self->rows;
130 my $size = $rows->size;
131 return( $self->error({
132 message => "Value provided ($pos) is greater than the zero-based number of rows available (" . $rows->size . ").",
133 class => 'HTML::Object::IndexSizeError',
134 }) ) if( $pos > $size );
135 return( $self->error({
136 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}",
137 class => 'HTML::Object::IndexSizeError',
138 }) ) if( $pos < 0 && abs( $pos ) > $size );
139 $pos = ( $rows->length + $pos ) if( $pos < 0 );
140 my $elem = $rows->index( $pos );
141 my $parent = $elem->parent;
142 my $children = $parent->children;
143 my $kid_pos = $children->pos( $elem );
144 return( $self->error({
145 message => "Unable to find the row element for index $pos",
146 class => 'HTML::Object::HierarchyRequestError',
147 }) ) if( !defined( $kid_pos ) );
148 my $rv = $children->splice( $kid_pos, 1 );
149 $elem->parent( undef );
150 $parent->reset(1);
151 $self->_reset_table( 'rows' );
152 return( $elem );
153 }
154  
155 sub deleteTFoot { return( shift->_delete_first_element( tfoot => @_ ) ); }
156  
157 sub deleteTHead { return( shift->_delete_first_element( thead => @_ ) ); }
158  
159 # Note: deprecated property frame
160 sub frame : lvalue { return( shift->_set_get_property( 'frame', @_ ) ); }
161  
162 sub insertRow
163 {
164 my $self = shift( @_ );
165 my $pos = shift( @_ );
166 my $rows = $self->rows;
167 my $size = $rows->size;
168 if( defined( $pos ) )
169 {
170 return( $self->error({
171 message => "Value provided (" . overload::StrVal( $pos // '' ) . ") is not an integer.",
172 class => 'HTML::Object::IndexSizeError',
173 }) ) if( !$self->_is_integer( $pos ) );
174 return( $self->error({
175 message => "Value provided ($pos) is greater than the zero-based number of rows available (" . $rows->size . ").",
176 class => 'HTML::Object::IndexSizeError',
177 }) ) if( $pos > $size );
178 return( $self->error({
179 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}",
180 class => 'HTML::Object::IndexSizeError',
181 }) ) if( $pos < 0 && abs( $pos ) > $size );
182 $pos = ( $rows->length + $pos ) if( $pos < 0 );
183 }
184 # "If a table has multiple <tbody> elements, by default, the new row is inserted into the last <tbody>."
185 my $body = $self->tbodies->last;
186 # If there is no tbody and no rows yet, we create a tbody
187 if( !$rows->length && !$body )
188 {
189 $body = $self->createTBody();
190 }
191 $self->_load_class( 'HTML::Object::DOM::Element::TableRow' ) || return( $self->pass_error );
192 my $row;
193 # A position was provided
194 if( defined( $pos ) )
195 {
196 # ..., but there are no rows yet
197 if( $rows->is_empty )
198 {
199 # if we have a tbody, we add the new row there
200 if( $body )
201 {
202 $row = $body->insertRow();
203 }
204 # otherwise, we just add as the last child of the table.
205 # However, this should not happen, because if there are no rows and no tbody, oen is created and this condition is never reached
206 else
207 {
208 $self->children->push( $row );
209 $row->parent( $self );
210 $self->reset(1);
211 }
212 }
213 else
214 {
215 my $elem = $rows->index( $pos );
216 return( $self->error({
217 message => "No element could be found at row index $pos",
218 class => 'HTML::Object::HierarchyRequestError',
219 }) ) if( !defined( $elem ) );
220 my $parent = $elem->parent;
221 my $children = $parent->children;
222 return( $self->error({
223 message => "Element at row index $pos has no parent!",
224 class => 'HTML::Object::HierarchyRequestError',
225 }) ) if( !$parent );
226 my $real_pos = $children->pos( $elem );
227 return( $self->error({
228 message => "Unable to find the row element for index $pos",
229 class => 'HTML::Object::HierarchyRequestError',
230 }) ) if( !defined( $real_pos ) );
231 $row = HTML::Object::DOM::Element::TableRow->new( @_ ) ||
232 return( $self->pass_error( HTML::Object::DOM::Element::TableRow->error ) );
233 $row->close;
234 $row->parent( $parent );
235 $children->splice( $real_pos, 0, $row );
236 $parent->reset(1);
237 }
238 }
239 # If there is already a tbody, the new row will be added there
240 elsif( $body )
241 {
242 $row = $body->insertRow();
243 }
244 # 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.
245 else
246 {
247 $row = HTML::Object::DOM::Element::TableRow->new( @_ ) ||
248 return( $self->pass_error( HTML::Object::DOM::Element::TableRow->error ) );
249 $row->close;
250 $self->children->push( $row );
251 $row->parent( $self );
252 $self->reset(1);
253 }
254 $self->_reset_table( 'rows' );
255 return( $row );
256 }
257  
258 # Note: property rows read-only
259 sub rows
260 {
261 my $self = shift( @_ );
262 return( $self->{_table_rows} ) if( $self->{_table_rows} && !$self->_is_table_reset( qw( rows tbody tfoot thead ) ) );
263 my $results = $self->new_array;
264 my $children = $self->children;
265 $children->foreach(sub
266 {
267 my $tag = $_->tag;
268 if( $tag eq 'tr' )
269 {
270 $results->push( $_ );
271 }
272 elsif( $tag eq 'tbody' || $tag eq 'tfoot' || $tag eq 'thead' )
273 {
274 my $rows = $_->rows;
275 $results->push( $rows->list ) if( !$rows->is_empty );
276 }
277 return(1);
278 });
279 unless( $self->{_table_rows} )
280 {
281 $self->_load_class( 'HTML::Object::DOM::Collection' ) ||
282 return( $self->pass_error );
283 $self->{_table_rows} = HTML::Object::DOM::Collection->new ||
284 return( $self->pass_error( HTML::Object::DOM::Collection->error ) );
285 }
286 # Re-use the same object, so that we can update the object the user may have retrieved
287 # e.g.:
288 # my $rows = $table->rows
289 # then do some row changes, and
290 # say $rows->length; # shows updated number of rows. No need to redo $table->rows
291 $self->{_table_rows}->set( $results );
292 $self->_remove_table_reset( 'rows' );
293 return( $self->{_table_rows} );
294 }
295  
296 # Note: deprecated property rules
297 sub rules : lvalue { return( shift->_set_get_property( 'rules', @_ ) ); }
298  
299 # Note: deprecated property summary
300 sub summary : lvalue { return( shift->_set_get_property( 'summary', @_ ) ); }
301  
302 # Note: property tBodies read-only
303 sub tBodies { return( shift->_get_tsection_collection( 'tbody' ) ); }
304  
305 sub tbodies { return( shift->tBodies( @_ ) ); }
306  
307 # Note: property tFoot
308 sub tFoot : lvalue { return( shift->_set_get_section( tfoot => 'HTML::Object::DOM::Element::TableSection', @_ ) ); }
309  
310 sub tfoot : lvalue { return( shift->tFoot( @_ ) ); }
311  
312 # Note: property tHead
313 sub tHead : lvalue { return( shift->_set_get_section( thead => 'HTML::Object::DOM::Element::TableSection', @_ ) ); }
314  
315 sub thead : lvalue { return( shift->tHead( @_ ) ); }
316  
317 # Note: deprecated property width is inherited
318  
319 # Common for tbody, thead and tfoot
320 sub _create_tsection
321 {
322 my $self = shift( @_ );
323 my $tag = shift( @_ ) || return( $self->error( "No tag name was provided for this table section." ) );
324 my $opts = $self->_get_args_as_hash( @_ );
325 $self->_load_class( 'HTML::Object::DOM::Element::TableSection' ) ||
326 return( $self->pass_error );
327 $opts->{tag} = $tag;
328 my $children = $self->children;
329 my $elem = HTML::Object::DOM::Element::TableSection->new( %$opts ) ||
330 return( $self->pass_error( HTML::Object::DOM::Element::TableSection->error ) );
331 $elem->close;
332 $elem->parent( $self );
333 my $list = $children->grep(sub{ $self->_is_a( $_ => 'HTML::Object::DOM::Element::TableSection' ) && $_->tag eq $tag });
334 if( $tag eq 'tbody' )
335 {
336 my $last_elem = $list->last;
337 if( $last_elem )
338 {
339 $last_elem->after( $elem );
340 }
341 else
342 {
343 $children->push( $elem );
344 }
345 $self->reset(1);
346 $self->_reset_table( $tag );
347 }
348 elsif( $tag eq 'tfoot' )
349 {
350 if( $list->is_empty )
351 {
352 $children->push( $elem );
353 $self->reset(1);
354 $self->_reset_table( $tag );
355 }
356 else
357 {
358 return( $list->first );
359 }
360 }
361 elsif( $tag eq 'thead' )
362 {
363 if( $list->is_empty )
364 {
365 my $len = $children->length;
366 my $pos;
367 for( my $i = 0; $i < $len; $i++ )
368 {
369 my $e = $children->[$i];
370 next if( !$e->_is_a( 'HTML::Object::DOM::Node' ) );
371 my $tag = $e->tag;
372 if( $tag ne 'caption' && $tag ne 'colgroup' )
373 {
374 $pos = $i;
375 $children->splice( $i, 0, $elem );
376 $self->reset(1);
377 $self->_reset_table( $tag );
378 last;
379 }
380 }
381 if( !defined( $pos ) )
382 {
383 $children->push( $elem );
384 $self->reset(1);
385 $self->_reset_table( $tag );
386 }
387 }
388 else
389 {
390 return( $list->first );
391 }
392 }
393 return( $elem );
394 }
395  
396 sub _delete_first_element
397 {
398 my $self = shift( @_ );
399 my $tag = shift( @_ ) || return( $self->error( "No tag was provided." ) );
400 $tag = lc( $tag );
401 my $children = $self->children;
402 my $len = $children->length;
403 for( my $i = 0; $i < $len; $i++ )
404 {
405 if( $self->_is_a( $children->[$i] => 'HTML::Object::Element' ) &&
406 $children->[$i]->tag eq $tag )
407 {
408 my $elem = $children->[$i];
409 $children->splice( $i, 1 );
410 $elem->parent( undef );
411 $self->reset(1);
412 $self->_reset_table( $tag );
413 return( $elem );
414 }
415 }
416 return;
417 }
418  
419 sub _get_tsection_collection
420 {
421 my $self = shift( @_ );
422 my $tag = shift( @_ ) || return( $self->error( "No tag name was provided for this table section." ) );
423 my $cache_name = "_table_collection_${tag}";
424 return( $self->{ $cache_name } ) if( $self->{ $cache_name } && !$self->_is_table_reset( $tag ) );
425 my $results = $self->new_array;
426 my $children = $self->children;
427 $children->foreach(sub
428 {
429 # if( $_->tag eq $tag && !$self->_is_a( $_ => 'HTML::Object::DOM::Closing' ) )
430 if( $_->tag eq $tag )
431 {
432 $results->push( $_ );
433 }
434 });
435 $self->_load_class( 'HTML::Object::DOM::Collection' ) || return( $self->pass_error );
436 my $col = HTML::Object::DOM::Collection->new( $results ) ||
437 return( $self->pass_error( HTML::Object::DOM::Collection->error ) );
438 $self->{ $cache_name } = $col;
439 $self->_remove_table_reset( $tag );
440 return( $col );
441 }
442  
443 sub _is_table_reset
444 {
445 my $self = shift( @_ );
446 my @types = @_;
447 my $type = shift( @_ );
448 if( scalar( @types ) )
449 {
450 foreach my $type ( @types )
451 {
452 if( defined( $type ) && CORE::length( $type ) )
453 {
454 return(1) if( CORE::length( $self->{ "_table_reset_${type}" } ) );
455 }
456 }
457 }
458 else
459 {
460 for( @{$self->{_reset_fields}} )
461 {
462 return(1) if( CORE::exists( $self->{ $_ } ) && CORE::length( $self->{ $_ } ) );
463 }
464 }
465 return(0);
466 }
467  
468 sub _remove_table_reset
469 {
470 my $self = shift( @_ );
471 my @types = @_;
472 for( @types )
473 {
474 CORE::delete( $self->{ "_table_reset_${_}" } );
475 }
476 return( $self );
477 }
478  
479 sub _reset_table
480 {
481 my $self = shift( @_ );
482 my $type = shift( @_ );
483 if( $type eq 'all' )
484 {
485 for( @{$self->{_reset_fields}} )
486 {
487 $self->{ $_ } = 1;
488 }
489 }
490 else
491 {
492 $self->{ "_table_reset_${type}" } = 1;
493 }
494 $self->rows if( $type eq 'rows' || $type eq 'all' );
495 $self->reset(1);
496 return( $self );
497 }
498  
499 sub _set_get_section : lvalue
500 {
501 my $self = shift( @_ );
502 my $tag = shift( @_ );
503 my $class = shift( @_ );
504  
505 return( $self->_set_get_callback({
506 get => sub
507 {
508 my $self = shift( @_ );
509 my $list = $self->_get_tsection_collection( $tag );
510 return( $list->first );
511 },
512 set => sub
513 {
514 my $self = shift( @_ );
515 my $new = shift( @_ );
516 if( !$self->_is_a( $new => $class ) || $new->tag ne $tag )
517 {
518 return( $self->error({ message => "New ${tag} object provided is not an ${class} object", class => 'HTML::Object::HierarchyRequestError' }) );
519 }
520 $new->detach;
521 my( $old, $pos );
522 my $children = $self->children;
523 my $len = $children->length;
524 for( my $i = 0; $i < $len; $i++ )
525 {
526 my $e = $children->[$i];
527 my $e_tag = $e->tag;
528 if( $e_tag eq $tag )
529 {
530 $old = $e;
531 $pos = $i;
532 last;
533 }
534 # Find out the default position we would insert the new element if there is no previous ($old) element
535 elsif( !defined( $pos ) &&
536 (
537 ( $tag eq 'thead' && $e_tag ne 'caption' && $e_tag ne 'colgroup' ) ||
538 ( $tag eq 'tfoot' && $e_tag ne 'caption' && $e_tag ne 'colgroup' && $e_tag ne 'thead' )
539 ) )
540 {
541 $pos = $i;
542 }
543 }
544
545 if( !defined( $pos ) )
546 {
547 if( $tag eq 'caption' )
548 {
549 $pos = 0;
550 }
551 # For thead, or tfoot, put them as the last element
552 else
553 {
554 $pos = $len;
555 }
556 }
557
558 $self->reset(1);
559 $self->_reset_table( $tag );
560 $new->parent( $self );
561 if( defined( $old ) )
562 {
563 $children->splice( $pos, 1, $new );
564 return( $old );
565 }
566 else
567 {
568 $children->splice( $pos, 0, $new );
569 return;
570 }
571 },
572 }, @_ ) );
573 }
574  
575 1;
576 # NOTE: POD
577 __END__
578  
579 =encoding utf-8
580  
581 =head1 NAME
582  
583 HTML::Object::DOM::Element::Table - HTML Object DOM Table Class
584  
585 =head1 SYNOPSIS
586  
587 use HTML::Object::DOM::Element::Table;
588 my $table = HTML::Object::DOM::Element::Table->new ||
589 die( HTML::Object::DOM::Element::Table->error, "\n" );
590  
591 =head1 VERSION
592  
593 v0.3.0
594  
595 =head1 DESCRIPTION
596  
597 This interface provides special properties and methods (beyond the regular L<HTML::Object::DOM::Element> object interface it also has available to it by inheritance) for manipulating the layout and presentation of tables in an HTML document.
598  
599 Tables can have, in this order:
600  
601 =over 4
602  
603 =item 1. an optional L<caption|HTML::Object::DOM::Element::TableCaption> element,
604  
605 =item 2. zero or more L<colgroup|HTML::Object::DOM::Element::TableCol> elements,
606  
607 =item 3. an optional L<thead|HTML::Object::DOM::Element::TableSection> element,
608  
609 =item 4. either one of the following:
610  
611 =over 8
612  
613 =item * zero or more L<tbody|HTML::Object::DOM::Element::TableSection> elements
614  
615 =item * one or more L<tr|HTML::Object::DOM::Element::TableRow> elements
616  
617 =back
618  
619 =item 5. an optional L<tfoot|HTML::Object::DOM::Element::TableSection> element
620  
621 =back
622  
623 The above is for reference only and is not enforced by this interface.
624  
625 =head1 INHERITANCE
626  
627 +-----------------------+ +---------------------------+ +-------------------------+ +----------------------------+ +-----------------------------------+
628 | HTML::Object::Element | --> | HTML::Object::EventTarget | --> | HTML::Object::DOM::Node | --> | HTML::Object::DOM::Element | --> | HTML::Object::DOM::Element::Table |
629 +-----------------------+ +---------------------------+ +-------------------------+ +----------------------------+ +-----------------------------------+
630  
631 =head1 PROPERTIES
632  
633 Inherits properties from its parent L<HTML::Object::DOM::Element>
634  
635 =head2 caption
636  
637 Is a L<table caption element|HTML::Object::DOM::Element::TableCaption> representing the first L<caption|HTML::Object::DOM::Element::TableCaption> that is a child of the element, or C<undef> if none is found.
638  
639 When set, if the object does not represent a L<caption|HTML::Object::DOM::Element::TableCaption>, a C<HTML::Object::HierarchyRequestError> error is returned. If a correct object is given, it is inserted in the tree as the first child of this element and the first L<caption|HTML::Object::DOM::Element::TableCaption> that is a child of this element is removed from the tree, if any, and returned.
640  
641 Example:
642  
643 my $table = $doc->getElementsByTagName( 'table' )->first;
644 my $old_caption = $table->caption( $new_caption );
645 # or
646 $table->caption = $new_caption;
647  
648 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableElement/caption>
649  
650 =head2 rows
651  
652 Read-only.
653  
654 Returns a L<HTML Collection|HTML::Object::DOM::Collection> containing all the rows of the element, that is all C<tr> that are a child of the element, or a child of one of its C<thead>, C<tbody> and C<tfoot> children. The rows members of a C<thead> appear first, in tree order, and those members of a C<tbody> last, also in tree order.
655  
656 Note that for performance improvement, the collection is cached until changes are made that would affect the results.
657  
658 Example:
659  
660 <table id="myTable">
661 <tr></tr>
662 <tbody>
663 <tr></tr>
664 </tbody>
665 </table>
666  
667 my $rows = $doc->getElementById( 'myTable' )->rows;
668 say $rows->length; # 2
669  
670 <table id="myTable2">
671 <tr>
672 <td>
673 <table>
674 <tr></tr>
675 </table>
676 </td>
677 </tr>
678 <tbody>
679 <tr></tr>
680 </tbody>
681 </table>
682  
683 my $rows = $doc->getElementById( 'myTable2' )->rows;
684 say $rows->length; # Still 2
685  
686 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableElement/rows>
687  
688 =head2 tBodies
689  
690 Read-only.
691  
692 Returns a L<HTML Collection|HTML::Object::DOM::Collection> containing all the L<tbody|HTML::Object::DOM::Element::TableSection> of the element.
693  
694 Note that for performance improvement, the collection is cached until changes are made that would affect the results.
695  
696 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableElement/tBodies>
697  
698 =head2 tbodies
699  
700 Alias for L</tBodies>
701  
702 =head2 tFoot
703  
704 Is a L<HTML::Object::DOM::Element::TableSection> representing the first L<tfoot|HTML::Object::DOM::Element::TableSection> that is a child of the element, or C<undef> if none is found.
705  
706 When set, if the object does not represent a L<tfoot|HTML::Object::DOM::Element::TableSection>, a C<HTML::Object::HierarchyRequestError> error is returned.
707  
708 If a correct object is given, it is inserted in the tree immediately before the first element that is neither a L<caption|HTML::Object::DOM::Element::TableCaption>, a L<colgroup|HTML::Object::DOM::Element::TableCol>, nor a L<thead|HTML::Object::DOM::Element::TableSection>, or as the last child if there is no such element, and the first L<tfoot|HTML::Object::DOM::Element::TableSection> that is a child of this element is removed from the tree, if any.
709  
710 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableElement/tFoot>
711  
712 =head2 tHead
713  
714 Is a L<HTML::Object::DOM::Element::TableSection> representing the first L<thead|HTML::Object::DOM::Element::TableSection> that is a child of the element, or C<undef> if none is found.
715  
716 When set, if the object does not represent a L<thead|HTML::Object::DOM::Element::TableSection>, a C<HTML::Object::HierarchyRequestError> error is returned.
717  
718 If a correct object is given, it is inserted in the tree immediately before the first element that is neither a L<caption|HTML::Object::DOM::Element::TableCaption>, nor a L<colgroup|HTML::Object::DOM::Element::TableCol>, or as the last child if there is no such element, and the first L<thead|HTML::Object::DOM::Element::TableSection> that is a child of this element is removed from the tree, if any.
719  
720 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableElement/tHead>
721  
722 =head1 METHODS
723  
724 Inherits methods from its parent L<HTML::Object::DOM::Element>
725  
726 =head2 createCaption
727  
728 Returns an L<HTML::Object::DOM::Element> representing the first L<caption|HTML::Object::DOM::Element::TableCaption> that is a child of the element. If none is found, a new one is created and inserted in the tree as the first child of the C<table> element.
729  
730 Example:
731  
732 <table>
733 <tr><td>Cell 1.1</td><td>Cell 1.2</td><td>Cell 1.3</td></tr>
734 <tr><td>Cell 2.1</td><td>Cell 2.2</td><td>Cell 2.3</td></tr>
735 </table>
736  
737 my $table = $doc->querySelector('table');
738 my $caption = $table->createCaption();
739 $caption->textContent = 'This caption was created by JavaScript!';
740  
741 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableElement/createCaption>
742  
743 =head2 createTBody
744  
745 Returns a L<HTML::Object::DOM::Element::TableSection> representing a new L<tbody|HTML::Object::DOM::Element::TableSection> that is a child of the element. It is inserted in the tree after the last element that is a L<tbody|HTML::Object::DOM::Element::TableSection>, or as the last child if there is no such element.
746  
747 Example:
748  
749 my $mybody = $mytable->createTBody();
750 # Now this should be true: $mybody == mytable->tBodies->item( $mytable->tBodies->length - 1 )
751  
752 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableElement/createTBody>
753  
754 =head2 createTFoot
755  
756 Returns an L<HTML::Object::DOM::Element::TableSection> representing the first L<tfoot|HTML::Object::DOM::Element::TableSection> that is a child of the element. If none is found, a new one is created and inserted in the tree as the last child.
757  
758 Example:
759  
760 my $myfoot = $mytable->createTFoot();
761 # Now this should be true: $myfoot == $mytable->tFoot
762  
763 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableElement/createTFoot>
764  
765 =head2 createTHead
766  
767 Returns an L<HTML::Object::DOM::Element::TableSection> representing the first L<thead|HTML::Object::DOM::Element::TableSection> that is a child of the element. If none is found, a new one is created and inserted in the tree immediately before the first element that is neither a L<caption|HTML::Object::DOM::Element::TableCaption>, nor a L<colgroup|HTML::Object::DOM::Element::TableCol>, or as the last child if there is no such element.
768  
769 Example:
770  
771 my $myhead = mytable->createTHead();
772 # Now this should be true: $myhead == mytable->tHead
773  
774 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableElement/createTHead>
775  
776 =head2 deleteCaption
777  
778 Removes the first L<caption|HTML::Object::DOM::Element::TableCaption> that is a child of the element and returns the object of the caption element.
779  
780 Example:
781  
782 <table>
783 <caption>This caption will be deleted!</caption>
784 <tr><td>Cell 1.1</td><td>Cell 1.2</td></tr>
785 <tr><td>Cell 2.1</td><td>Cell 2.2</td></tr>
786 </table>
787  
788 my $table = $doc->querySelector('table');
789 $table->deleteCaption();
790  
791 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableElement/deleteCaption>
792  
793 =head2 deleteRow
794  
795 Removes the row corresponding to the index given in parameter. 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.
796  
797 Example:
798  
799 <table>
800 <tr><td>Cell 1.1</td><td>Cell 1.2</td><td>Cell 1.3</td></tr>
801 <tr><td>Cell 2.1</td><td>Cell 2.2</td><td>Cell 2.3</td></tr>
802 <tr><td>Cell 3.1</td><td>Cell 3.2</td><td>Cell 3.3</td></tr>
803 </table>
804  
805 my $table = $doc->querySelector('table');
806 # Delete second row
807 $table->deleteRow(1);
808  
809 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableElement/deleteRow>
810  
811 =head2 deleteTFoot
812  
813 Removes the first L<tfoot|HTML::Object::DOM::Element::TableSection> that is a child of the element and returns the object of the caption element.
814  
815 Example:
816  
817 <table>
818 <thead><th>Name</th><th>Score</th></thead>
819 <tr><td>Bob</td><td>541</td></tr>
820 <tr><td>Jim</td><td>225</td></tr>
821 <tfoot><th>Average</th><td>383</td></tfoot>
822 </table>
823  
824 my $table = $doc->querySelector('table');
825 $table->deleteTFoot();
826  
827 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableElement/deleteTFoot>
828  
829 =head2 deleteTHead
830  
831 Removes the first L<thead|HTML::Object::DOM::Element::TableSection> that is a child of the element and returns the object of the caption element.
832  
833 Example:
834  
835 <table>
836 <thead><th>Name</th><th>Occupation</th></thead>
837 <tr><td>Bob</td><td>Plumber</td></tr>
838 <tr><td>Jim</td><td>Roofer</td></tr>
839 </table>
840  
841 my $table = $doc->querySelector('table');
842 $table->deleteTHead();
843  
844 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableElement/deleteTHead>
845  
846 =head2 insertRow
847  
848 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. If there are no existing rows yet, a L<tbody|HTML::Object::DOM::Element::TableSection> is created and the new row inserted into it. If a table has multiple C<tbody> elements, by default, the new row is inserted into the last C<tbody>.
849  
850 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.
851  
852 Example:
853  
854 <table></table>
855  
856 $doc->getElementsByTagName('table')->[0]->insertRow();
857  
858 Table is now:
859  
860 <table>
861 <tbody>
862 <tr></tr>
863 </tbody>
864 </table>
865  
866 But if there are already existing rows and no tbody, the new row will merely be added as the las child of the table.
867  
868 <table>
869 <tr></tr>
870 </table>
871  
872 $doc->getElementsByTagName('table')->[0]->insertRow();
873  
874 Table is now:
875  
876 <table>
877 <tr></tr>
878 <tr></tr>
879 </table>
880  
881 Even if there is a C<tfoot>, the new row will be added after:
882  
883 <table>
884 <tr></tr>
885 <tfoot></tfoot>
886 </table>
887  
888 $doc->getElementsByTagName('table')->[0]->insertRow();
889  
890 Table is now:
891  
892 <table>
893 <tr></tr>
894 <tfoot></tfoot>
895 <tr></tr>
896 </table>
897  
898 If an index is negative, the new row will be added that far back from the end:
899  
900 <table>
901 <tr id="one"></tr>
902 <tr id="two"></tr>
903 <tr id="three"></tr>
904 </table>
905  
906 $doc->getElementsByTagName('table')->[0]->insertRow(-2);
907  
908 Table is now:
909  
910 <table>
911 <tr id="one"></tr>
912 <tr></tr>
913 <tr id="two"></tr>
914 <tr id="three"></tr>
915 </table>
916  
917 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableElement/insertRow>
918  
919 =head2 DEPRECATED PROPERTIES
920  
921 =head2 align
922  
923 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".
924  
925 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableElement/align>
926  
927 =head2 bgColor
928  
929 A string containing the background color of the table. It reflects the obsolete bgcolor attribute.
930  
931 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableElement/bgColor>
932  
933 =head2 border
934  
935 Is a string containing the width in pixels of the border of the table. It reflects the obsolete border attribute.
936  
937 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableElement/border>
938  
939 =head2 cellPadding
940  
941 Is a string containing the width in pixels of the horizontal and vertical sapce between cell content and cell borders. It reflects the obsolete cellpadding attribute.
942  
943 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableElement/cellPadding>
944  
945 =head2 cellSpacing
946  
947 Is a string containing the width in pixels of the horizontal and vertical separation between cells. It reflects the obsolete cellspacing attribute.
948  
949 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableElement/cellSpacing>
950  
951 =head2 frame
952  
953 Is a string containing the type of the external borders of the table. It reflects the obsolete frame attribute and can take one of the following values: C<void>, C<above>, C<below>, C<hsides>, C<vsides>, C<lhs>, C<rhs>, C<box>, or C<border>.
954  
955 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableElement/frame>
956  
957 =head2 rules
958  
959 Is a string containing the type of the internal borders of the table. It reflects the obsolete rules attribute and can take one of the following values: C<none>, C<groups>, C<rows>, C<cols>, or C<all>.
960  
961 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableElement/rules>
962  
963 =head2 summary
964  
965 Is a string containing a description of the purpose or the structure of the table. It reflects the obsolete summary attribute.
966  
967 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableElement/summary>
968  
969 =head2 width
970  
971 Is a string containing the length in pixels or in percentage of the desired width fo the entire table. It reflects the obsolete width attribute.
972  
973 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableElement/width>
974  
975 =head1 AUTHOR
976  
977 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
978  
979 =head1 SEE ALSO
980  
981 L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableElement>, L<Mozilla documentation on table element|https://developer.mozilla.org/en-US/docs/Web/HTML/Element/table>
982  
983 =head1 COPYRIGHT & LICENSE
984  
985 Copyright(c) 2021 DEGUEST Pte. Ltd.
986  
987 All rights reserved
988  
989 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
990  
991 =cut