lib/HTML/Object/DOM/List.pm

filter: Statementstmt 32.9% 27 / 82 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 44 MC/DCmcdc 0.0% 0 / 27 Subroutinesub 52.9% 9 / 17 Podpod 100% 5 / 5 totaltotal 19.6% 41 / 209 CC 35 SCAR 60.0
CC
35
Cov
17%
addEventListener
756.0
AUTOLOAD
30.0
__ANON__
12.0
CRAP 404.6
« lib/HTML/Object/XQuery.pm ↑ Summary blib/lib/HTML/Object/DOM/Node.pm »
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 1 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶ warnings::register_categories( 'HTML::Object' );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶ use parent qw( HTML::Object::EventTarget );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶ use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶ use HTML::Object::Event;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶ use Wanted;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 our $VERSION = 'v0.3.0';
25 };
26  
27 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 sub init
31 {
32 0 ▶ my $self = shift( @_ );
Subroutine init: not called CC=2 SCAR=17.9
Pod documented
33 0 $self->{children} = [];
34 0 $self->{_init_strict_use_sub} = 1;
35 0 ▶ $self->SUPER::init( @_ ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init(@_) or return $self->pass_error 0 0 0
36 0 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 0 ▶ my $self = shift( @_ );
Subroutine addEventListener: not called CC=27 SCAR=66.3
Pod documented
57 0 ▶ return( $self->error( "I was expecting 3 arguments, but got only ", scalar( @_ ), "." ) ) if( scalar( @_ ) < 3 );
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ < 3 0 0 0
58 0 my $def = shift( @_ );
59 0 my $type = shift( @_ );
60 0 my $code = shift( @_ );
61 0 ▶ return( $self->error( "Dictionary provided is not an hash reference." ) ) if( !defined( $def ) || ref( $def ) ne 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $def or ref $def ne "HASH" 0 0 0
Condition0%0/3
not defined $def or ref $def ne "HASH"
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $def or ref $def ne "HASH"
not defined $def ref $def ne "HASH"
Truth table0%0/3
not(A) or B
A= not defined $defB= ref $def ne "HASH"
ABresult
1X1
011
000
62 0 ▶ return( $self->error( "Event type provided contains illegal characters." ) ) if( !defined( $type ) || $type !~ /^\w+#/ );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type and $type =~ /^\w+#/ 0 0 0
Condition0%0/3
defined $type and $type =~ /^\w+#/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $type and $type =~ /^\w+#/
defined $type $type =~ /^\w+#/
Truth table0%0/3
A and B
A= defined $typeB= $type =~ /^\w+#/
ABresult
0X0
100
111
63 0 ▶ return( $self->error( "Callback is not a code reference." ) ) if( !defined( $code ) || !length( "$code" ) || ref( $code ) ne 'CODE' );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $code && CORE::length "$code" or ref $code ne "CODE" 0 0 0
Condition0%0/6
not defined $code && CORE::length "$code" or ref $code ne "CODE"
l!l&&r!l&&!r
000
defined $code && CORE::length "$code"
!ll&&!rl&&r
000
MC/DC0%0/3
not defined $code && CORE::length "$code" or ref $code ne "CODE"
defined $code CORE::length "$code" ref $code ne "CODE"
Truth table0%0/4
not(A && B) or C
A= defined $codeB= CORE::length "$code"C= ref $code ne "CODE"
ABCresult
0XX1
10X1
1111
1100
64 # Before we enable the event listener we must make sure we are listening on events on relevant array or scalar
65 0 ▶ if( CORE::exists( $def->{ $type } ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $def->{$type}) 0 0 0
66 {
67 0 my $ref = $def->{ $type };
68 # add or remove
69 0 OP: foreach my $op ( keys( %$ref ) )
70 {
71 0 my $this = $ref->{ $op };
72 0 for( qw( property type ) )
73 {
74 0 ▶ if( !CORE::exists( $this->{ $_ } ) || !defined( $this->{ $_ } ) || !CORE::length( $this->{ $_ } ) )
Branch: 2 branches
BranchTrueFalseTotal
if (not exists $this->{$_} && defined $this->{$_} && CORE::length $this->{$_}) { } 0 0 0
elsif ($_ ne 'add' and $_ ne 'remove') { } 0 0 0
Condition0%0/6
exists $this->{$_} && defined $this->{$_} && CORE::length $this->{$_}
!ll&&!rl&&r
000
exists $this->{$_} and defined $this->{$_}
!ll&&!rl&&r
000
MC/DC0%0/3
exists $this->{$_} && defined $this->{$_} && CORE::length $this->{$_}
exists $this->{$_} defined $this->{$_} CORE::length $this->{$_}
Truth table0%0/4
A and B && C
A= exists $this->{$_}B= defined $this->{$_}C= CORE::length $this->{$_}
ABCresult
0XX0
10X0
1100
1111
75 {
76 0 ▶ warn( "Dictionary property \"$_\" is missing or empty.\n" ) if( $self->_is_warnings_enabled( 'HTML::Object' ) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_warnings_enabled("HTML::Object") 0 0 0
77 0 next OP;
78 }
79 ▶ elsif( $_ ne 'add' && $_ ne 'remove' )
Condition0%0/3
$_ ne "add" and $_ ne "remove"
!ll&&!rl&&r
000
MC/DC0%0/2
$_ ne "add" and $_ ne "remove"
$_ ne "add" $_ ne "remove"
Truth table0%0/3
A and B
A= $_ ne "add"B= $_ ne "remove"
ABresult
0X0
100
111
80 {
81 0 warnings::warn( "Unknown data listener type \"$_\"\n" );
82 0 next OP;
83 }
84 }
85
86 0 my $subref = $self->can( $this->{property} );
87 0 ▶ if( !defined( $subref ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $subref) 0 0 0
88 {
89 0 ▶ warn( "This object class \"" . ( ref( $self ) || $self ) . "\" does not support method \"" . $this->{property} . "\".\n" ) if( $self->_is_warnings_enabled( 'HTML::Object' ) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_warnings_enabled("HTML::Object") 0 0 0
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
90 0 next;
91 }
92 0 my $data = $subref->( $self );
93 0 ▶ if( !$self->_is_object( $data ) || !$data->can( 'callback' ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_object($data) and $data->can("callback")) 0 0 0
Condition0%0/3
$self->_is_object($data) and $data->can("callback")
!ll&&!rl&&r
000
MC/DC0%0/2
$self->_is_object($data) and $data->can("callback")
$self->_is_object($data) $data->can("callback")
Truth table0%0/3
A and B
A= $self->_is_object($data)B= $data->can("callback")
ABresult
0X0
100
111
94 {
95 0 ▶ warn( "Object from class \"" . ( ref( $data ) || $data ) . "\" does not have a \"callback\" method.\n" ) if( $self->_is_warnings_enabled( 'HTML::Object' ) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_warnings_enabled("HTML::Object") 0 0 0
Condition0%0/3
ref $data || $data
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $data || $data
ref $data $data
Truth table0%0/3
A || B
A= ref $dataB= $data
ABresult
1X1
011
000
96 0 next;
97 }
98 0 my $cb = $data->callback( $this->{type} );
99 # Callback already exists
100 0 ▶ next if( defined( $cb ) && ref( $cb ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $cb and ref $cb 0 0 0
Condition0%0/3
defined $cb and ref $cb
!ll&&!rl&&r
000
MC/DC0%0/2
defined $cb and ref $cb
defined $cb ref $cb
Truth table0%0/3
A and B
A= defined $cbB= ref $cb
ABresult
0X0
100
111
101 $data->callback( $this->{type} => sub
102 {
103 0 ▶ my $hash = shift( @_ );
Subroutine __ANON__: not called CC=3 SCAR=24.8
104 0 my $event = $self->_make_event( $type );
105 0 ▶ if( CORE::exists( $this->{callback} ) && ref( $this->{callback} ) eq 'CODE' )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $this->{"callback"} and ref $this->{"callback"} eq "CODE") 0 0 0
Condition0%0/3
exists $this->{"callback"} and ref $this->{"callback"} eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
exists $this->{"callback"} and ref $this->{"callback"} eq "CODE"
exists $this->{"callback"} ref $this->{"callback"} eq "CODE"
Truth table0%0/3
A and B
A= exists $this->{"callback"}B= ref $this->{"callback"} eq "CODE"
ABresult
0X0
100
111
106 {
107 0 $this->{callback}->( $self, { event => $event, added => $hash->{added}, removed => $hash->{removed}, type => $hash->{type} });
108 }
109 0 $self->dispatchEvent( $event );
110 0 });
111 }
112 }
113 0 return( $self->SUPER::addEventListener( $type => $code ) );
114 }
115  
116 0 ▶sub forEach { return( shift->children->foreach( @_ ) ); }
Subroutine forEach: not called CC=1 SCAR=6.9
Pod documented
117  
118 0 ▶sub length { return( shift->children->length ); }
Subroutine length: not called CC=1 SCAR=6.9
Pod documented
119  
120 0 ▶sub push { return( shift->children->push( @_ ) ); }
Subroutine push: not called CC=1 SCAR=6.9
Pod documented
121  
122 sub _make_event
123 {
124 0 ▶ my $self = shift( @_ );
Subroutine _make_event: not called CC=2 SCAR=17.9
125 0 my $type = shift( @_ );
126 0 ▶ my $event = HTML::Object::Event->new( $type,
Condition0%0/2
'HTML::Object::Event'->new($type, 'bubbles', 0, 'cancellable', 0, 'target', $self) || (return $self->pass_error)
l!l
00
MC/DC0%0/1
'HTML::Object::Event'->new($type, 'bubbles', 0, 'cancellable', 0, 'target', $self) || (return $self->pass_error)
'HTML::Object::Event'->new($type, 'bubbles', 0, 'cancellable', 0, 'target', $self)
Truth table0%0/2
A
A= 'HTML::Object::Event'->new($type, 'bubbles', 0, 'cancellable', 0, 'target', $self)
Aresult
11
00
127 bubbles => 0,
128 cancellable => 0,
129 target => $self,
130 ) || return( $self->pass_error );
131 0 return( $event );
132 }
133  
134 sub AUTOLOAD
135 {
136 0 ▶ my( $name ) = our $AUTOLOAD =~ /([^:]+)$/;
Subroutine AUTOLOAD: not called CC=5 SCAR=34.0
137 0 my $self = shift( @_ );
138 0 my $children = $self->children;
139 0 ▶ die( "No method \"\$name\" in class \"", ( ref( $self ) || $self ), "\".\n" ) if( !$children );
Branch: 1 branch
BranchTrueFalseTotal
unless $children 0 0 0
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
140 0 my $code = $children->can( $name );
141 0 ▶ die( "No method \"\$name\" in class \"", ( ref( $children ) || $children ), "\".\n" ) if( !$code );
Branch: 1 branch
BranchTrueFalseTotal
unless $code 0 0 0
Condition0%0/3
ref $children || $children
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $children || $children
ref $children $children
Truth table0%0/3
A || B
A= ref $childrenB= $children
ABresult
1X1
011
000
142 0 eval( "sub $name { return( shift->children->$name( \@_ ) ); }\n\n" );
143 0 return( $code->( $children, @_ ) );
144 }
145  
146 1 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
« lib/HTML/Object/XQuery.pm ↑ Summary blib/lib/HTML/Object/DOM/Node.pm »