« blib/lib/HTML/Object/DOM/Element/TableRow.pm
↑ Summary
lib/HTML/Object/DOM/Element/TableSection.pm »
| 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__ |