blib/lib/HTML/Object/DOM/List.pm untested

filter: Statementstmt 0.0% 0 / 83 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 46 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 185 CC 17 SCAR 47.4
CC
17
Cov
0%
addEventListener
240.0
AUTOLOAD
12.0
BEGIN
2.0
CRAP 114.5
1 ##----------------------------------------------------------------------------
2 ## HTML Object - ~/lib/HTML/Object/DOM/List.pm
3 ## Version v0.3.0
4 ## Copyright(c) 2022 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2021/12/28
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::List;
15 BEGIN
16 {
17 use strict;
18 use warnings;
19 warnings::register_categories( 'HTML::Object' );
20 use parent qw( HTML::Object::EventTarget );
21 use vars qw( $VERSION );
22 use HTML::Object::Event;
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->{children} = [];
34 $self->{_init_strict_use_sub} = 1;
35 $self->SUPER::init( @_ ) || return( $self->pass_error );
36 return( $self );
37 }
38  
39 # This method is called from the package inheriting from us
40 # $def is a dictionary hash reference providing specifics in a crisp way about the types of events and related properties concerned
41 # Example:
42 # {
43 # addtrack => {
44 # add => { property => 'children', type => 'add', event => 'addtrack' },
45 # },
46 # change => {
47 # add => { property => 'selected', type => 'add', event => 'change' },
48 # remove => { property => 'selected', type => 'remove', event => 'change' },
49 # },
50 # removetrack => {
51 # remove => { property => 'children', type => 'remove', event => 'removetrack' },
52 # },
53 # }
54 sub addEventListener
55 {
56 my $self = shift( @_ );
57 return( $self->error( "I was expecting 3 arguments, but got only ", scalar( @_ ), "." ) ) if( scalar( @_ ) < 3 );
58 my $def = shift( @_ );
59 my $type = shift( @_ );
60 my $code = shift( @_ );
61 return( $self->error( "Dictionary provided is not an hash reference." ) ) if( !defined( $def ) || ref( $def ) ne 'HASH' );
62 return( $self->error( "Event type provided contains illegal characters." ) ) if( !defined( $type ) || $type !~ /^\w+#/ );
63 return( $self->error( "Callback is not a code reference." ) ) if( !defined( $code ) || !length( "$code" ) || ref( $code ) ne 'CODE' );
64 # Before we enable the event listener we must make sure we are listening on events on relevant array or scalar
65 if( CORE::exists( $def->{ $type } ) )
66 {
67 my $ref = $def->{ $type };
68 # add or remove
69 OP: foreach my $op ( keys( %$ref ) )
70 {
71 my $this = $ref->{ $op };
72 for( qw( property type ) )
73 {
74 if( !CORE::exists( $this->{ $_ } ) || !defined( $this->{ $_ } ) || !CORE::length( $this->{ $_ } ) )
75 {
76 warn( "Dictionary property \"$_\" is missing or empty.\n" ) if( $self->_is_warnings_enabled( 'HTML::Object' ) );
77 next OP;
78 }
79 elsif( $_ ne 'add' && $_ ne 'remove' )
80 {
81 warnings::warn( "Unknown data listener type \"$_\"\n" );
82 next OP;
83 }
84 }
85
86 my $subref = $self->can( $this->{property} );
87 if( !defined( $subref ) )
88 {
89 warn( "This object class \"" . ( ref( $self ) || $self ) . "\" does not support method \"" . $this->{property} . "\".\n" ) if( $self->_is_warnings_enabled( 'HTML::Object' ) );
90 next;
91 }
92 my $data = $subref->( $self );
93 if( !$self->_is_object( $data ) || !$data->can( 'callback' ) )
94 {
95 warn( "Object from class \"" . ( ref( $data ) || $data ) . "\" does not have a \"callback\" method.\n" ) if( $self->_is_warnings_enabled( 'HTML::Object' ) );
96 next;
97 }
98 my $cb = $data->callback( $this->{type} );
99 # Callback already exists
100 next if( defined( $cb ) && ref( $cb ) );
101 $data->callback( $this->{type} => sub
102 {
103 my $hash = shift( @_ );
104 my $event = $self->_make_event( $type );
105 if( CORE::exists( $this->{callback} ) && ref( $this->{callback} ) eq 'CODE' )
106 {
107 $this->{callback}->( $self, { event => $event, added => $hash->{added}, removed => $hash->{removed}, type => $hash->{type} });
108 }
109 $self->dispatchEvent( $event );
110 });
111 }
112 }
113 return( $self->SUPER::addEventListener( $type => $code ) );
114 }
115  
116 sub forEach { return( shift->children->foreach( @_ ) ); }
117  
118 sub length { return( shift->children->length ); }
119  
120 sub push { return( shift->children->push( @_ ) ); }
121  
122 sub _make_event
123 {
124 my $self = shift( @_ );
125 my $type = shift( @_ );
126 my $event = HTML::Object::Event->new( $type,
127 bubbles => 0,
128 cancellable => 0,
129 target => $self,
130 ) || return( $self->pass_error );
131 return( $event );
132 }
133  
134 sub AUTOLOAD
135 {
136 my( $name ) = our $AUTOLOAD =~ /([^:]+)$/;
137 my $self = shift( @_ );
138 my $children = $self->children;
139 die( "No method \"\$name\" in class \"", ( ref( $self ) || $self ), "\".\n" ) if( !$children );
140 my $code = $children->can( $name );
141 die( "No method \"\$name\" in class \"", ( ref( $children ) || $children ), "\".\n" ) if( !$code );
142 eval( "sub $name { return( shift->children->$name( \@_ ) ); }\n\n" );
143 return( $code->( $children, @_ ) );
144 }
145  
146 1;
147 # NOTE: POD
148 __END__
149  
150 =encoding utf-8
151  
152 =head1 NAME
153  
154 HTML::Object::DOM::List - HTML Object DOM List Abstract Class
155  
156 =head1 SYNOPSIS
157  
158 package HTML::Object::DOM::VideoTrackList;
159 use parent qw( HTML::Object::DOM::List );
160
161 my $list = HTML::Object::DOM::VideoTrackList->new || d
162 ie( HTML::Object::DOM::VideoTrackList->error, "\n" );
163  
164 =head1 VERSION
165  
166 v0.3.0
167  
168 =head1 DESCRIPTION
169  
170 This is an abstract class designed to be inherited by L<HTML::Object::DOM::TextTrackCueList>, L<HTML::Object::DOM::TextTrackList> and L<HTML::Object::DOM::VideoTrackList>
171  
172 It inherits from L<HTML::Object::EventTarget>
173  
174 =head1 INHERITANCE
175  
176 +-----------------------+ +---------------------------+ +-------------------------+
177 | HTML::Object::Element | --> | HTML::Object::EventTarget | --> | HTML::Object::DOM::List |
178 +-----------------------+ +---------------------------+ +-------------------------+
179  
180 =head1 METHODS
181  
182 =head2 addEventListener
183  
184 This is a variant from the usual C<addEventListener> from L<HTML::Object::EventTarget>. This method takes 3 arguments:
185  
186 =over 4
187  
188 =item 1. A dictionary hash reference
189  
190 This dictionary contains a key for each event name and another hash reference as their value. That hash reference contains the following properties:
191  
192 =over 8
193  
194 =item callback
195  
196 Optional property whose value is a code reference to be called after creating the event and before dispatching it with L<HTML::Object::EventTarget/dispatchEvent>. The purpose is to give it a chance to add some property value to the event like C<track> for L<HTML::Object::DOM::TrackEvent> fired by L<HTML::Object::DOM::TextTrackCueList>, L<HTML::Object::DOM::TextTrackList> and L<HTML::Object::DOM::VideoTrackList>
197  
198 =item event
199  
200 The event name.
201  
202 =item property
203  
204 The module property or method name.
205  
206 =item type
207  
208 The type of callback to set for this property. Possible values are: C<add> or C<remove>
209  
210 =back
211  
212 =item 2. An event type
213  
214 =item 3. An event handler callback
215  
216 =back
217  
218 When C<addEventListener> is called, it will check if, for the given even type passed, there is an entry in the dictionary, and if there is it will enable an internal callback on the associated module property when there is any change to its underlying value.
219  
220 This relies on L<Module::Generic::Array/callback> and L<Module::Generic::Scalar/callback>
221  
222 That internal callback will be called when a change occurs, and will create an L<event|HTML::Object::Event> of type C<type> and call L<HTML::Object::EventTarget/dispatchEvent> passing it the newly created event.
223  
224 If a C<callback> was specified in the dictionary for this event type, the callback code will be executed, and the whatever value added or removed will be passed to the callback as an hash reference and an hash property C<added> or C<removed> depending if the operation was to add or remove a value.
225  
226 After having set this internal callback to monitor change, if any, this will call its parent L<addEventListener> to register the event listener.
227  
228 =head2 forEach
229  
230 Calls C<foreach> on the array object returned by L<HTML::Object::Element/children> method.
231  
232 =head2 length
233  
234 Returns the size of the list, starting from C<1>.
235  
236 =head2 push
237  
238 Provided with some data and they will be appended to this list object.
239  
240 =head1 AUTHOR
241  
242 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
243  
244 =head1 SEE ALSO
245  
246 L<HTML::Object::EventTarget>, L<HTML::Object::Event>, L<HTML::Object::DOM::TextTrackCueList>, L<HTML::Object::DOM::TextTrackCueList>, L<HTML::Object::DOM::VideoTrackList>
247  
248 =head1 COPYRIGHT & LICENSE
249  
250 Copyright(c) 2021 DEGUEST Pte. Ltd.
251  
252 All rights reserved
253  
254 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
255  
256 =cut