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__