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__