Object/EventTarget.pm

filter: Statementstmt 76.7% 188 / 245 Branchbran 44.5% 66 / 148 Conditioncond 40.5% 49 / 121 MC/DCmcdc 7.2% 5 / 69 Subroutinesub 88.4% 23 / 26 Podpod 100% 9 / 9 totaltotal 55.0% 340 / 618 CC 125 SCAR 36.7
CC
125
Cov
59%
_trigger_event_for
110.0
addEventListener
78.7
removeEventListener
57.0
CRAP 39.2
« Object/DOM/Element/Table.pm ↑ Summary Object/DOM/TextTrackCueList.pm »
1 ##----------------------------------------------------------------------------
2 ## HTML Object - ~/lib/HTML/Object/EventTarget.pm
3 ## Version v0.3.0
4 ## Copyright(c) 2022 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2021/12/11
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::EventTarget;
15 BEGIN
16 36 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
17 36 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 36 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 36 warnings::register_categories( 'HTML::Object' );
20 # Changed inheritance from Module::Generic to HTML::Object::Element, because I need modules like
21 # HTML::Object::DOM::TextTrack that inherits from EventTarget to also have the parent method provided
22 # by the core module HTML::Object::Element
23 36 ▶ use parent qw( HTML::Object::Element );
Subroutine BEGIN: called CC=1 SCAR=0.0
24 36 ▶ use vars qw( $PACK_SUB_RE $SIGNALS $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
25 36 ▶ use HTML::Object::EventListener;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 36 ▶ use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
27 36 ▶ use Wanted;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 36 our $PACK_SUB_RE = qr/^(((?<pack>[a-zA-Z\_]\w*(?:\:\:\w+)*)\:\:)?(?<sub>\w+))$/;
29 # Hash reference of signal to array of object to remove their listeners
30 36 our $SIGNALS = {};
31 36 our $VERSION = 'v0.3.0';
32 };
33  
34 36 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 36 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 sub init
38 127 ▶{
Subroutine init: called CC=4 SCAR=14.3
Pod documented
39 127 my $self = shift( @_ );
40 127 $self->{tag} = 'eventtarget';
41 127 ▶ $self->{_init_strict_use_sub} = 1 unless( CORE::exists( $self->{_init_strict_use_sub} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_init_strict_use_sub"} 0 127 127
42 127 ▶ $self->{_exception_class} = 'HTML::Object::Exception' unless( CORE::exists( $self->{_exception_class} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_exception_class"} 127 0 127
43 127 ▶ $self->SUPER::init( @_ ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init(@_) or return $self->pass_error 127 0 127
44 127 $self->{event_listeners} = {};
45 127 return( $self );
46 }
47  
48 sub addEventListener
49 5 ▶{
Subroutine addEventListener: called CC=26 SCAR=43.7
Pod documented
50 5 my $self = shift( @_ );
51 5 ▶ my $type = shift( @_ ) || return( $self->error( "No event listener type was provided." ) );
Condition50%1/2
shift @_ || (return $self->error('No event listener type was provided.'))
l!l
50
MC/DC0%0/1
shift @_ || (return $self->error('No event listener type was provided.'))
shift @_
Truth table50%1/2
A
A= shift @_
Aresult
11
00
52 5 ▶ my $callback = shift( @_ ) || return( $self->error( "No event listener callback was provided." ) );
Condition50%1/2
shift @_ || (return $self->error('No event listener callback was provided.'))
l!l
50
MC/DC0%0/1
shift @_ || (return $self->error('No event listener callback was provided.'))
shift @_
Truth table50%1/2
A
A= shift @_
Aresult
11
00
53 5 ▶ return( $self->error( "Event listener type \"$type\" contains illegal characters. It should contain only alphanumeric and _ characters." ) ) if( $type !~ /^\w+$/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $type =~ /^\w+$/ 0 5 5
54 5 $type = lc( $type );
55 5 ▶ if( !ref( $callback ) && $callback =~ /$PACK_SUB_RE/ )
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $callback and $callback =~ /$HTML::Object::EventTarget::PACK_SUB_RE/) 0 5 5
Condition33%1/3
not ref $callback and $callback =~ /$HTML::Object::EventTarget::PACK_SUB_RE/
!ll&&!rl&&r
500
MC/DC0%0/2
not ref $callback and $callback =~ /$HTML::Object::EventTarget::PACK_SUB_RE/
not ref $callback $callback =~ /$HTML::Object::EventTarget::PACK_SUB_RE/
Truth table33%1/3
not(A) and B
A= not ref $callbackB= $callback =~ /$HTML::Object::EventTarget::PACK_SUB_RE/
ABresult
0X0
100
111
56 {
57 0 my( $pack, $sub ) = @+{qw( pack sub )};
58 0 ▶ $pack ||= caller;
Condition0%0/3
$pack ||= caller
l!l&&r!l&&!r
000
MC/DC0%0/2
$pack ||= caller
$pack caller
Truth table0%0/3
A ||= B
A= $packB= caller
ABresult
1X1
011
000
59 0 ▶ if( my $ref = $pack->can( $sub ) )
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = $pack->can($sub)) { } 0 0 0
60 {
61 0 $callback = $ref;
62 }
63 else
64 {
65 0 return( $self->error( "Unknown subroutine \"$callback\" in package \"$pack\"." ) );
66 }
67 }
68 5 ▶ return( $self->error( "Event listener callback is not a code reference." ) ) if( ref( $callback ) ne 'CODE' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $callback ne "CODE" 0 5 5
69 5 my $opts = $self->_get_args_as_hash( @_ );
70 5 my @ok_params = qw( capture once passive signal debug );
71 5 my $params = {};
72 5 @$params{ @ok_params } = CORE::delete( @$opts{ @ok_params } );
73 5 my $post_processing;
74 5 ▶ $post_processing = CORE::delete( $opts->{post_processing} ) if( CORE::exists( $opts->{post_processing} ) && ref( $opts->{post_processing} ) eq 'CODE' );
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts->{"post_processing"} and ref $opts->{"post_processing"} eq "CODE" 0 5 5
Condition33%1/3
exists $opts->{"post_processing"} and ref $opts->{"post_processing"} eq "CODE"
!ll&&!rl&&r
500
MC/DC0%0/2
exists $opts->{"post_processing"} and ref $opts->{"post_processing"} eq "CODE"
exists $opts->{"post_processing"} ref $opts->{"post_processing"} eq "CODE"
Truth table33%1/3
A and B
A= exists $opts->{"post_processing"}B= ref $opts->{"post_processing"} eq "CODE"
ABresult
0X0
100
111
75 5 ▶ if( scalar( keys( %$opts ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %$opts) 0 5 5
76 {
77 0 ▶ warn( "Unrecognised options: '" . join( "', '", sort( keys( %$opts ) ) ) . "'\n" ) if( $self->_is_warnings_enabled( 'HTML::Object' ) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_warnings_enabled("HTML::Object") 0 0 0
78 }
79 5 ▶ $params->{capture} //= 0;
Condition100%2/2
$params->{"capture"} //= 0
l!l
41
MC/DC100%1/1
$params->{"capture"} //= 0
$params->{"capture"}
Truth table100%2/2
A
A= $params->{"capture"}
Aresult
11
00
80 5 ▶ $params->{once} //= 0;
Condition50%1/2
$params->{"once"} //= 0
l!l
05
MC/DC0%0/1
$params->{"once"} //= 0
$params->{"once"}
Truth table50%1/2
A
A= $params->{"once"}
Aresult
11
00
81 5 ▶ $params->{passive} //= 0;
Condition50%1/2
$params->{"passive"} //= 0
l!l
05
MC/DC0%0/1
$params->{"passive"} //= 0
$params->{"passive"}
Truth table50%1/2
A
A= $params->{"passive"}
Aresult
11
00
82 5 my $key = join( ';', $type, Scalar::Util::refaddr( $callback ), $params->{capture} );
83 5 ▶ $self->{event_listeners} = {} if( !CORE::exists( $self->{event_listeners} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"event_listeners"} 3 2 5
84 5 my $repo = $self->{event_listeners};
85 5 ▶ my $debug = CORE::delete( $params->{debug} ) || 0;
Condition50%1/2
delete $params->{'debug'} || 0
l!l
05
MC/DC0%0/1
delete $params->{'debug'} || 0
delete $params->{'debug'}
Truth table50%1/2
A
A= delete $params->{'debug'}
Aresult
11
00
86 5 ▶ my $eh = HTML::Object::EventListener->new(
Condition50%1/2
'HTML::Object::EventListener'->new('type', $type, 'code', $callback, 'options', $params, 'element', $self, 'debug', $debug) || (return $self->pass_error('HTML::Object::EventListener'->error))
l!l
50
MC/DC0%0/1
'HTML::Object::EventListener'->new('type', $type, 'code', $callback, 'options', $params, 'element', $self, 'debug', $debug) || (return $self->pass_error('HTML::Object::EventListener'->error))
'HTML::Object::EventListener'->new('type', $type, 'code', $callback, 'options', $params, 'element', $self, 'debug', $debug)
Truth table50%1/2
A
A= 'HTML::Object::EventListener'->new('type', $type, 'code', $callback, 'options', $params, 'element', $self, 'debug', $debug)
Aresult
11
00
87 type => $type,
88 code => $callback,
89 options => $params,
90 element => $self,
91 debug => $debug,
92 ) || return( $self->pass_error( HTML::Object::EventListener->error ) );
93 5 ▶ if( $params->{signal} && $params->{signal} =~ /^\w+$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($params->{"signal"} and $params->{"signal"} =~ /^\w+$/) 0 5 5
Condition33%1/3
$params->{"signal"} and $params->{"signal"} =~ /^\w+$/
!ll&&!rl&&r
500
MC/DC0%0/2
$params->{"signal"} and $params->{"signal"} =~ /^\w+$/
$params->{"signal"} $params->{"signal"} =~ /^\w+$/
Truth table33%1/3
A and B
A= $params->{"signal"}B= $params->{"signal"} =~ /^\w+$/
ABresult
0X0
100
111
94 {
95 0 $SIG{ $params->{signal} } = \&_signal_remove_listeners;
96 0 ▶ $SIGNALS->{ $params->{signal} } = [] if( !CORE::exists( $SIGNALS->{ $params->{signal} } ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$HTML::Object::EventTarget::SIGNALS{$params->{"signal"}} 0 0 0
97 0 push( @{$SIGNALS->{ $params->{signal} }}, $eh );
98 }
99 5 ▶ $repo->{ $type } = {} if( !CORE::exists( $repo->{ $type } ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $repo->{$type} 3 2 5
100 5 ▶ $repo->{ $type }->{sequence} = $self->new_array if( !CORE::exists( $repo->{ $type }->{sequence} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $repo->{$type}{"sequence"} 3 2 5
101 5 $repo->{ $type }->{ $key } = $eh;
102 5 ▶ if( $repo->{ $type }->{sequence}->has( $key ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($repo->{$type}{"sequence"}->has($key)) 0 5 5
103 {
104 0 $repo->{ $type }->{sequence}->remove( $key );
105 }
106 5 $repo->{ $type }->{sequence}->push( $key );
107 # Call any post-processing callback if necessary.
108 # Those are used so that event monitoring can be enabled upon setting event handlers and not before, on some data like array or scalar
109 # See HTML::Object::LDOM::List for example
110 5 ▶ if( defined( $post_processing ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $post_processing) 0 5 5
111 {
112 0 $post_processing->( $eh );
113 }
114 5 return( $eh );
115 }
116  
117 sub dispatchEvent
118 2 ▶{
Subroutine dispatchEvent: called CC=11 SCAR=28.5
Pod documented
119 2 my $self = shift( @_ );
120 2 ▶ my $event = shift( @_ ) || return( $self->error( "No event object was provided." ) );
Condition50%1/2
shift @_ || (return $self->error('No event object was provided.'))
l!l
20
MC/DC0%0/1
shift @_ || (return $self->error('No event object was provided.'))
shift @_
Truth table50%1/2
A
A= shift @_
Aresult
11
00
121 2 ▶ return( $self->error( "Event object provided ($event) is not an HTML::Object::Event" ) ) if( !$self->_is_a( $event => 'HTML::Object::Event' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_a($event, "HTML::Object::Event") 0 2 2
122 2 $event->target( $self );
123  
124 2 ▶ my $type = $event->type || return( $self->error( "The event has no type associated with it!" ) );
Condition50%1/2
$event->type || (return $self->error('The event has no type associated with it!'))
l!l
20
MC/DC0%0/1
$event->type || (return $self->error('The event has no type associated with it!'))
$event->type
Truth table50%1/2
A
A= $event->type
Aresult
11
00
125 2 $type = lc( $type );
126 2 my $can_cancel = $event->cancelable;
127 2 ▶ return( $self ) if( $can_cancel && $event->cancelled );
Branch: 1 branch
BranchTrueFalseTotal
if $can_cancel and $event->cancelled 0 2 2
Condition33%1/3
$can_cancel and $event->cancelled
!ll&&!rl&&r
020
MC/DC0%0/2
$can_cancel and $event->cancelled
$can_cancel $event->cancelled
Truth table33%1/3
A and B
A= $can_cancelB= $event->cancelled
ABresult
0X0
100
111
128 # from current element to top one
129 2 my $path = $event->composedPath;
130 2 $event->eventPhase( $event->CAPTURING_PHASE );
131 # Go from top to our element, i.e. reverse
132 $path->reverse->foreach(sub
133 6 ▶ {
Subroutine __ANON__: called CC=5 SCAR=20.9
134 6 my $node = shift( @_ );
135 6 $event->currentTarget( $node );
136 $node->handleEvent( $event ) || do
137 6 ▶ {
Branch: 1 branch
BranchTrueFalseTotal
$node->handleEvent($event) or do { warn "Error handling event: ", $node->error if $self->_is_warnings_enabled("HTML::Object") } 6 0 6
138 0 ▶ warn( "Error handling event: ", $node->error ) if( $self->_is_warnings_enabled( 'HTML::Object' ) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_warnings_enabled("HTML::Object") 0 0 0
139 };
140 6 ▶ if( $can_cancel && $event->cancelled )
Branch: 1 branch
BranchTrueFalseTotal
if ($can_cancel and $event->cancelled) 0 6 6
Condition33%1/3
$can_cancel and $event->cancelled
!ll&&!rl&&r
060
MC/DC0%0/2
$can_cancel and $event->cancelled
$can_cancel $event->cancelled
Truth table33%1/3
A and B
A= $can_cancelB= $event->cancelled
ABresult
0X0
100
111
141 {
142 0 return(0);
143 }
144 # Make sure to return true to keep looping
145 6 return(1);
146 2 });
147 2 ▶ return( $self ) if( $can_cancel && $event->cancelled >= $event->CANCEL_IMMEDIATE_PROPAGATION );
Branch: 1 branch
BranchTrueFalseTotal
if $can_cancel and $event->cancelled >= $event->CANCEL_IMMEDIATE_PROPAGATION 0 2 2
Condition33%1/3
$can_cancel and $event->cancelled >= $event->CANCEL_IMMEDIATE_PROPAGATION
!ll&&!rl&&r
020
MC/DC0%0/2
$can_cancel and $event->cancelled >= $event->CANCEL_IMMEDIATE_PROPAGATION
$can_cancel $event->cancelled >= $event->CANCEL_IMMEDIATE_PROPAGATION
Truth table33%1/3
A and B
A= $can_cancelB= $event->cancelled >= $event->CANCEL_IMMEDIATE_PROPAGATION
ABresult
0X0
100
111
148 2 $event->eventPhase( $event->AT_TARGET );
149 2 $event->currentTarget( $self );
150 2 $self->handleEvent( $event );
151 2 ▶ return( $self ) if( $can_cancel && $event->cancelled >= $event->CANCEL_PROPAGATION );
Branch: 1 branch
BranchTrueFalseTotal
if $can_cancel and $event->cancelled >= $event->CANCEL_PROPAGATION 0 2 2
Condition33%1/3
$can_cancel and $event->cancelled >= $event->CANCEL_PROPAGATION
!ll&&!rl&&r
020
MC/DC0%0/2
$can_cancel and $event->cancelled >= $event->CANCEL_PROPAGATION
$can_cancel $event->cancelled >= $event->CANCEL_PROPAGATION
Truth table33%1/3
A and B
A= $can_cancelB= $event->cancelled >= $event->CANCEL_PROPAGATION
ABresult
0X0
100
111
152 # This event does not bubble, so we do nothing more
153 2 ▶ return( $self ) if( !$event->bubbles );
Branch: 1 branch
BranchTrueFalseTotal
unless $event->bubbles 0 2 2
154 2 $event->eventPhase( $event->BUBBLING_PHASE );
155 # Now, go from our element to the top one
156 $path->for(sub
157 6 ▶ {
Subroutine __ANON__: called CC=6 SCAR=21.6
158 6 my( $i, $node ) = @_;
159 # Skip the first one which is us.
160 6 ▶ return(1) if( $i == 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $i == 0 2 4 6
161 4 $event->currentTarget( $node );
162 $node->handleEvent( $event ) || do
163 4 ▶ {
Branch: 1 branch
BranchTrueFalseTotal
$node->handleEvent($event) or do { warn "Error handling event: ", $node->error if $self->_is_warnings_enabled("HTML::Object") } 4 0 4
164 0 ▶ warn( "Error handling event: ", $node->error ) if( $self->_is_warnings_enabled( 'HTML::Object' ) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_warnings_enabled("HTML::Object") 0 0 0
165 };
166 4 ▶ if( $can_cancel && $event->cancelled )
Branch: 1 branch
BranchTrueFalseTotal
if ($can_cancel and $event->cancelled) 0 4 4
Condition33%1/3
$can_cancel and $event->cancelled
!ll&&!rl&&r
040
MC/DC0%0/2
$can_cancel and $event->cancelled
$can_cancel $event->cancelled
Truth table33%1/3
A and B
A= $can_cancelB= $event->cancelled
ABresult
0X0
100
111
167 {
168 0 return(0);
169 }
170 # Make sure to return true to keep looping
171 4 return(1);
172 2 });
173 2 return( $self );
174 }
175  
176 0 ▶sub event_listeners { return( shift->_set_get_hash( 'event_listeners', @_ ) ); }
Subroutine event_listeners: not called CC=1 SCAR=6.9
Pod documented
177  
178 sub getEventListeners
179 195 ▶{
Subroutine getEventListeners: called CC=7 SCAR=20.6
Pod documented
180 195 my $self = shift( @_ );
181 195 my $type = shift( @_ );
182 195 ▶ return( $self->error( "No event type propoded to get its event listeners." ) ) if( !defined( $type ) || !CORE::length( $type ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type and length $type 0 195 195
Condition33%1/3
defined $type and length $type
!ll&&!rl&&r
00195
MC/DC0%0/2
defined $type and length $type
defined $type length $type
Truth table33%1/3
A and B
A= defined $typeB= length $type
ABresult
0X0
100
111
183 195 ▶ $self->{event_listeners} = {} if( !CORE::exists( $self->{event_listeners} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"event_listeners"} 64 131 195
184 195 my $repo = $self->{event_listeners};
185 195 ▶ return if( !scalar( keys( %$repo ) ) );
Branch: 1 branch
BranchTrueFalseTotal
unless scalar keys %$repo 192 3 195
186 3 ▶ $repo->{ $type } = {} if( !CORE::exists( $repo->{ $type } ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $repo->{$type} 0 3 3
187 3 ▶ $repo->{ $type }->{sequence} = $self->new_array if( !CORE::exists( $repo->{ $type }->{sequence} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $repo->{$type}{"sequence"} 0 3 3
188 3 my $results = $self->new_array;
189 $repo->{ $type }->{sequence}->foreach(sub
190 3 ▶ {
Subroutine __ANON__: called CC=3 SCAR=13.1
191 3 my $key = shift( @_ );
192 3 ▶ $results->push( $repo->{ $type }->{ $key } ) if( CORE::exists( $repo->{ $type }->{ $key } ) && $self->_is_a( $repo->{ $type }->{ $key } => 'HTML::Object::EventListener' ) );
Branch: 1 branch
BranchTrueFalseTotal
if exists $repo->{$type}{$key} and $self->_is_a($repo->{$type}{$key}, "HTML::Object::EventListener") 3 0 3
Condition33%1/3
exists $repo->{$type}{$key} and $self->_is_a($repo->{$type}{$key}, "HTML::Object::EventListener")
!ll&&!rl&&r
003
MC/DC0%0/2
exists $repo->{$type}{$key} and $self->_is_a($repo->{$type}{$key}, "HTML::Object::EventListener")
exists $repo->{$type}{$key} $self->_is_a($repo->{$type}{$key}, "HTML::Object::EventListener")
Truth table33%1/3
A and B
A= exists $repo->{$type}{$key}B= $self->_is_a($repo->{$type}{$key}, "HTML::Object::EventListener")
ABresult
0X0
100
111
193 3 });
194 3 return( $results );
195 }
196  
197 sub handleEvent
198 12 ▶{
Subroutine handleEvent: called CC=24 SCAR=36.5
Pod documented
199 12 my $self = shift( @_ );
200 12 ▶ my $evt = shift( @_ ) || return( $self->error({
Condition50%1/2
shift @_ || (return $self->error({'message', 'No event was provided to handle.', 'class', 'HTML::Object::SyntaxError'}))
l!l
120
MC/DC0%0/1
shift @_ || (return $self->error({'message', 'No event was provided to handle.', 'class', 'HTML::Object::SyntaxError'}))
shift @_
Truth table50%1/2
A
A= shift @_
Aresult
11
00
201 message => "No event was provided to handle.",
202 class => 'HTML::Object::SyntaxError',
203 }) );
204 12 ▶ return( $self->error({
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_a($evt, "HTML::Object::Event") 0 12 12
205 message => "Event object provided is not an event of class HTML::Object::Event or its descendants.",
206 class => 'HTML::Object::TypeError',
207 }) ) if( !$self->_is_a( $evt => 'HTML::Object::Event' ) );
208 12 ▶ return( $self ) if( $evt->cancelled );
Branch: 1 branch
BranchTrueFalseTotal
if $evt->cancelled 0 12 12
209 12 my $can_cancel = $evt->cancelable;
210  
211 12 my $repo = $self->{event_listeners};
212 12 ▶ my $type = $evt->type || return( $self->error({
Condition50%1/2
$evt->type || (return $self->error({'message', 'No event type was provided.', 'class', 'HTML::Object::SyntaxError'}))
l!l
120
MC/DC0%0/1
$evt->type || (return $self->error({'message', 'No event type was provided.', 'class', 'HTML::Object::SyntaxError'}))
$evt->type
Truth table50%1/2
A
A= $evt->type
Aresult
11
00
213 message => "No event type was provided.",
214 class => 'HTML::Object::SyntaxError',
215 }) );
216 12 ▶ return( $self ) if( !CORE::exists( $repo->{ $type } ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $repo->{$type} 4 8 12
217 return( $self->error({
218 message => "Repository of event listener of type '$type' is not an hash reference!",
219 class => 'HTML::Object::TypeError',
220 8 ▶ }) ) if( ref( $repo->{ $type } ) ne 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $repo->{$type} ne "HASH" 0 8 8
221  
222 return( $self->error({
223 message => "Could not find the 'sequence' property in the repository of event listeners.",
224 class => 'HTML::Object::SyntaxError',
225 8 ▶ }) ) if( !CORE::exists( $repo->{ $type }->{sequence} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $repo->{$type}{"sequence"} 0 8 8
226 return( $self->error({
227 message => "Sequence property of event listeners for type '$type' is not a Module::Generic::Array object",
228 class => 'HTML::Object::TypeError',
229 8 ▶ }) ) if( !$self->_is_a( $repo->{ $type }->{sequence} => 'Module::Generic::Array' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_a($repo->{$type}{"sequence"}, "Module::Generic::Array") 0 8 8
230  
231 8 $evt->currentTarget( $self );
232 8 my $eventPhase = $evt->eventPhase;
233 8 foreach my $key ( @{$repo->{ $type }->{sequence}} )
234 {
235 8 ▶ return( $self->error({
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key and length $key 0 8 8
Condition33%1/3
defined $key and length $key
!ll&&!rl&&r
008
MC/DC0%0/2
defined $key and length $key
defined $key length $key
Truth table33%1/3
A and B
A= defined $keyB= length $key
ABresult
0X0
100
111
236 message => "Empty key found in sequence repository.",
237 class => 'HTML::Object::SyntaxError',
238 }) ) if( !defined( $key ) || !CORE::length( $key ) );
239 8 ▶ if( !CORE::exists( $repo->{ $type }->{ $key } ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $repo->{$type}{$key}) 0 8 8
240 {
241 0 return( $self->error({
242 message => "Found an event listener of type '$type' with key '$key', but could not find its associated entry in the event listener repository.",
243 class => 'HTML::Object::SyntaxError',
244 }) );
245 }
246 8 my $listener = $repo->{ $type }->{ $key };
247 8 ▶ if( !$self->_is_a( $listener => 'HTML::Object::EventListener' ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_a($listener, "HTML::Object::EventListener")) 0 8 8
248 {
249 0 return( $self->error({
250 message => "The event listener of type '$type' found is not an HTML::Object::EventListener object.",
251 class => 'HTML::Object::TypeError',
252 }) );
253 }
254  
255 # check we are in the right phase
256 8 ▶ if( ( $eventPhase eq $evt->AT_TARGET && $evt->target ne $self ) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($eventPhase eq $evt->AT_TARGET and $evt->target ne $self or $eventPhase eq $evt->CAPTURING_PHASE and not $listener->capture or $eventPhase eq $evt->BUBBLING_PHASE and $listener->capture) 4 4 8
Condition86%13/15
$eventPhase eq $evt->AT_TARGET and $evt->target ne $self or $eventPhase eq $evt->CAPTURING_PHASE and not $listener->capture or $eventPhase eq $evt->BUBBLING_PHASE and $listener->capture
l!l&&r!l&&!r
314
$eventPhase eq $evt->AT_TARGET and $evt->target ne $self or $eventPhase eq $evt->CAPTURING_PHASE and not $listener->capture
l!l&&r!l&&!r
035
$eventPhase eq $evt->AT_TARGET and $evt->target ne $self
!ll&&!rl&&r
620
$eventPhase eq $evt->CAPTURING_PHASE and not $listener->capture
!ll&&!rl&&r
413
$eventPhase eq $evt->BUBBLING_PHASE and $listener->capture
!ll&&!rl&&r
311
MC/DC50%3/6
$eventPhase eq $evt->AT_TARGET and $evt->target ne $self or $eventPhase eq $evt->CAPTURING_PHASE and not $listener->capture or $eventPhase eq $evt->BUBBLING_PHASE and $listener->capture
$eventPhase eq $evt->AT_TARGET $evt->target ne $self $eventPhase eq $evt->CAPTURING_PHASE not $listener->capture $eventPhase eq $evt->BUBBLING_PHASE $listener->capture
Truth table33%5/15
A and B or C and not(D) or E and F
A= $eventPhase eq $evt->AT_TARGETB= $evt->target ne $selfC= $eventPhase eq $evt->CAPTURING_PHASED= not $listener->captureE= $eventPhase eq $evt->BUBBLING_PHASEF= $listener->capture
ABCDEFresult
11XXXX1
0X11XX1
1011XX1
0X0X111
0X10111
100X111
1010111
0X0X0X0
0X0X100
0X100X0
0X10100
100X0X0
100X100
10100X0
1010100
257 ( $eventPhase eq $evt->CAPTURING_PHASE && !$listener->capture ) ||
258 ( $eventPhase eq $evt->BUBBLING_PHASE && $listener->capture ) )
259 {
260 4 next;
261 }
262  
263 4 my $code = $listener->code;
264 4 ▶ if( ref( $code ) ne 'CODE' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $code ne "CODE") 0 4 4
265 {
266 # return( $self->error({
267 # message => "The handler for the event listener of type '$type' is not a code reference.",
268 # class => 'HTML::Object::TypeError',
269 # }) );
270 0 ▶ warn( "Warning only: the handler for the event listener of type '$type' is not a code reference." ) if( $self->_is_warnings_enabled( 'HTML::Object' ) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_warnings_enabled("HTML::Object") 0 0 0
271 0 next;
272 }
273 4 local $_ = $self;
274 # Note: Should we catch a die, or let it die, if that were the case?
275 4 $code->( $evt );
276 4 ▶ last if( $can_cancel && $evt->cancelled == $evt->CANCEL_IMMEDIATE_PROPAGATION );
Branch: 1 branch
BranchTrueFalseTotal
if $can_cancel and $evt->cancelled == $evt->CANCEL_IMMEDIATE_PROPAGATION 0 4 4
Condition33%1/3
$can_cancel and $evt->cancelled == $evt->CANCEL_IMMEDIATE_PROPAGATION
!ll&&!rl&&r
040
MC/DC0%0/2
$can_cancel and $evt->cancelled == $evt->CANCEL_IMMEDIATE_PROPAGATION
$can_cancel $evt->cancelled == $evt->CANCEL_IMMEDIATE_PROPAGATION
Truth table33%1/3
A and B
A= $can_cancelB= $evt->cancelled == $evt->CANCEL_IMMEDIATE_PROPAGATION
ABresult
0X0
100
111
277 }
278 8 return( $self );
279 }
280  
281 sub hasEventListener
282 2 ▶{
Subroutine hasEventListener: called CC=9 SCAR=28.5
Pod documented
283 2 my $self = shift( @_ );
284 2 my $type = shift( @_ );
285 2 ▶ $self->{event_listeners} = {} if( !CORE::exists( $self->{event_listeners} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"event_listeners"} 0 2 2
286 2 my $repo = $self->{event_listeners};
287 2 ▶ return( $self->new_number(0) ) if( !scalar( keys( %$repo ) ) );
Branch: 1 branch
BranchTrueFalseTotal
unless scalar keys %$repo 0 2 2
288 2 ▶ if( defined( $type ) && CORE::length( $type ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type and length $type) 0 2 2
Condition33%1/3
defined $type and length $type
!ll&&!rl&&r
200
MC/DC0%0/2
defined $type and length $type
defined $type length $type
Truth table33%1/3
A and B
A= defined $typeB= length $type
ABresult
0X0
100
111
289 {
290 0 ▶ $repo->{ $type } = {} if( !CORE::exists( $repo->{ $type } ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $repo->{$type} 0 0 0
291 0 my $n = scalar( keys( %{$repo->{ $type }} ) );
292 0 ▶ $n-- if( CORE::exists( $repo->{ $type }->{sequence} ) );
Branch: 1 branch
BranchTrueFalseTotal
if exists $repo->{$type}{"sequence"} 0 0 0
293 0 return( $self->new_number( $n ) );
294 }
295 2 my $n = 0;
296 2 foreach my $t ( keys( %$repo ) )
297 {
298 2 $n += scalar( keys( %{$repo->{ $t }} ) );
299 2 ▶ $n-- if( CORE::exists( $repo->{ $t }->{sequence} ) );
Branch: 1 branch
BranchTrueFalseTotal
if exists $repo->{$t}{"sequence"} 2 0 2
300 }
301 2 return( $self->new_number( $n ) );
302 }
303  
304 196 ▶sub on : lvalue
Subroutine on: called CC=1 SCAR=0.3
Pod documented
305 {
306 196 my $self = shift( @_ );
307 196 my $event = shift( @_ );
308  
309 return( $self->_set_get_callback({
310 get => sub
311 195 ▶ {
Subroutine __ANON__: called CC=4 SCAR=14.6
312 195 my $self = shift( @_ );
313 195 ▶ return( $self->error( "No event provided to set event handler." ) ) if( !defined( $event ) || !CORE::length( $event ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $event and length $event 0 195 195
Condition33%1/3
defined $event and length $event
!ll&&!rl&&r
00195
MC/DC0%0/2
defined $event and length $event
defined $event length $event
Truth table33%1/3
A and B
A= defined $eventB= length $event
ABresult
0X0
100
111
314 195 ▶ my $listeners = $self->getEventListeners( $event ) || return;
Condition100%2/2
$self->getEventListeners($event) || (return)
l!l
3192
MC/DC100%1/1
$self->getEventListeners($event) || (return)
$self->getEventListeners($event)
Truth table100%2/2
A
A= $self->getEventListeners($event)
Aresult
11
00
315 3 return( $listeners->first );
316 },
317 set => sub
318 1 ▶ {
Subroutine __ANON__: called CC=5 SCAR=18.4
319 1 my $self = shift( @_ );
320 1 my $arg = shift( @_ );
321 1 ▶ return( $self->error( "No event provided to set event handler." ) ) if( !defined( $event ) || !CORE::length( $event ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $event and length $event 0 1 1
Condition33%1/3
defined $event and length $event
!ll&&!rl&&r
001
MC/DC0%0/2
defined $event and length $event
defined $event length $event
Truth table33%1/3
A and B
A= defined $eventB= length $event
ABresult
0X0
100
111
322 # Argument provided is a code reference
323 1 ▶ if( ref( $arg ) ne 'CODE' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg ne "CODE") 0 1 1
324 {
325 0 return( $self->error( "Value provided is not a code reference." ) );
326 }
327 1 ▶ my $eh = $self->addEventListener( $event => $arg ) || return( $self->pass_error );
Condition50%1/2
$self->addEventListener($event, $arg) || (return $self->pass_error)
l!l
10
MC/DC0%0/1
$self->addEventListener($event, $arg) || (return $self->pass_error)
$self->addEventListener($event, $arg)
Truth table50%1/2
A
A= $self->addEventListener($event, $arg)
Aresult
11
00
328 1 return( $eh );
329 }
330 196 }, @_ ) );
331 }
332  
333 sub removeEventListener
334 2 ▶{
Subroutine removeEventListener: called CC=19 SCAR=40.4
Pod documented
335 2 my $self = shift( @_ );
336 2 ▶ my $type = shift( @_ ) || return( $self->error( "No event listener type was provided." ) );
Condition50%1/2
shift @_ || (return $self->error('No event listener type was provided.'))
l!l
20
MC/DC0%0/1
shift @_ || (return $self->error('No event listener type was provided.'))
shift @_
Truth table50%1/2
A
A= shift @_
Aresult
11
00
337 2 my( $callback, $eh );
338 2 ▶ if( $self->_is_a( $_[0] => 'HTML::Object::EventListener' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_a($_[0], 'HTML::Object::EventListener')) { } 0 2 2
339 {
340 0 $eh = shift( @_ );
341 0 $callback = $eh->code;
342 }
343 else
344 {
345 2 ▶ $callback = shift( @_ ) || return( $self->error( "No event listener callback was provided." ) );
Condition50%1/2
shift @_ || (return $self->error('No event listener callback was provided.'))
l!l
20
MC/DC0%0/1
shift @_ || (return $self->error('No event listener callback was provided.'))
shift @_
Truth table50%1/2
A
A= shift @_
Aresult
11
00
346 }
347 2 ▶ return( $self->error( "Event listener type \"$type\" contains illegal characters. It should contain only alphanumeric and _ characters." ) ) if( $type !~ /^\w+$/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $type =~ /^\w+$/ 0 2 2
348 2 ▶ if( !ref( $callback ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $callback and $callback =~ /$HTML::Object::EventTarget::PACK_SUB_RE/) 0 2 2
Condition33%1/3
not ref $callback and $callback =~ /$HTML::Object::EventTarget::PACK_SUB_RE/
!ll&&!rl&&r
200
MC/DC0%0/2
not ref $callback and $callback =~ /$HTML::Object::EventTarget::PACK_SUB_RE/
not ref $callback $callback =~ /$HTML::Object::EventTarget::PACK_SUB_RE/
Truth table33%1/3
not(A) and B
A= not ref $callbackB= $callback =~ /$HTML::Object::EventTarget::PACK_SUB_RE/
ABresult
0X0
100
111
349 $callback =~ /$PACK_SUB_RE/ )
350 {
351 0 my( $pack, $sub ) = @+{qw( pack sub )};
352 0 ▶ $pack ||= caller;
Condition0%0/3
$pack ||= caller
l!l&&r!l&&!r
000
MC/DC0%0/2
$pack ||= caller
$pack caller
Truth table0%0/3
A ||= B
A= $packB= caller
ABresult
1X1
011
000
353 0 ▶ if( my $ref = $pack->can( $sub ) )
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = $pack->can($sub)) { } 0 0 0
354 {
355 0 $callback = $ref;
356 }
357 else
358 {
359 0 return( $self->error( "Unknown subroutine \"$callback\" in package \"$pack\"." ) );
360 }
361 }
362 2 ▶ return( $self->error( "Event listener callback is not a code reference. You can provide either an event listener object (HTML::Object::EventListener, or a reference to a subroutine, or a subroutine name such as MyPackage::my_sub." ) ) if( defined( $callback ) && ref( $callback ) ne 'CODE' );
Branch: 1 branch
BranchTrueFalseTotal
if defined $callback and ref $callback ne "CODE" 0 2 2
Condition33%1/3
defined $callback and ref $callback ne "CODE"
!ll&&!rl&&r
020
MC/DC0%0/2
defined $callback and ref $callback ne "CODE"
defined $callback ref $callback ne "CODE"
Truth table33%1/3
A and B
A= defined $callbackB= ref $callback ne "CODE"
ABresult
0X0
100
111
363 2 my $opts = $self->_get_args_as_hash( @_ );
364 2 ▶ if( defined( $eh ) && !CORE::exists( $opts->{capture} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $eh and not exists $opts->{"capture"}) 0 2 2
Condition33%1/3
defined $eh and not exists $opts->{"capture"}
!ll&&!rl&&r
200
MC/DC0%0/2
defined $eh and not exists $opts->{"capture"}
defined $eh not exists $opts->{"capture"}
Truth table33%1/3
A and not(B)
A= defined $ehB= not exists $opts->{"capture"}
ABresult
0X0
100
111
365 {
366 0 $opts->{capture} = $eh->options->{capture};
367 }
368 2 my @ok_params = qw( capture );
369 2 my $params = {};
370 2 @$params{ @ok_params } = CORE::delete( @$opts{ @ok_params } );
371 2 ▶ if( scalar( keys( %$opts ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %$opts) 0 2 2
372 {
373 0 ▶ warn( "Unrecognised options: '" . join( "', '", sort( keys( %$opts ) ) ) . "'\n" ) if( $self->_is_warnings_enabled( 'HTML::Object' ) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_warnings_enabled("HTML::Object") 0 0 0
374 }
375 2 ▶ $params->{capture} //= 0;
Condition50%1/2
$params->{"capture"} //= 0
l!l
20
MC/DC0%0/1
$params->{"capture"} //= 0
$params->{"capture"}
Truth table50%1/2
A
A= $params->{"capture"}
Aresult
11
00
376 2 my $key = join( ';', $type, Scalar::Util::refaddr( $callback ), $params->{capture} );
377 2 ▶ $self->{event_listeners} = {} if( !CORE::exists( $self->{event_listeners} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"event_listeners"} 0 2 2
378 2 my $repo = $self->{event_listeners};
379 2 ▶ $repo->{ $type } = {} if( !CORE::exists( $repo->{ $type } ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $repo->{$type} 0 2 2
380 2 $eh = CORE::delete( $repo->{ $type }->{ $key } );
381 2 ▶ $repo->{ $type }->{sequence} = $self->new_array if( !CORE::exists( $repo->{ $type }->{sequence} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $repo->{$type}{"sequence"} 0 2 2
382 2 $repo->{ $type }->{sequence}->remove( $key );
383 2 return( $eh );
384 }
385  
386 sub _signal_remove_listeners
387 0 ▶{
Subroutine _signal_remove_listeners: not called CC=5 SCAR=34.0
388 my $sig = shift( @_ ) ||
389 do
390 0 ▶ {
Condition0%0/3
shift @_ || do { warn "Warning only: no signal was provided in HTML::Object::EventTarget::_signal_remove_listeners\n" if warnings::enabled('HTML::Object'); return }
l!l&&r!l&&!r
000
MC/DC0%0/2
shift @_ || do { warn "Warning only: no signal was provided in HTML::Object::EventTarget::_signal_remove_listeners\n" if warnings::enabled('HTML::Object'); return }
shift @_ do { warn "Warning only: no signal was provided in HTML::Object::EventTarget::_signal_remove_listeners\n" if warnings::enabled('HTML::Object'); return }
Truth table0%0/3
A || B
A= shift @_B= do { warn "Warning only: no signal was provided in HTML::Object::EventTarget::_signal_remove_listeners\n" if warnings::enabled('HTML::Object'); return }
ABresult
1X1
011
000
391 0 ▶ warn( "Warning only: no signal was provided in HTML::Object::EventTarget::_signal_remove_listeners\n" ) if( warnings::enabled( 'HTML::Object' ) );
Branch: 1 branch
BranchTrueFalseTotal
if warnings::enabled("HTML::Object") 0 0 0
392 0 return;
393 };
394 0 my $all = $SIGNALS->{ $sig };
395 0 ▶ if( ref( $all ) eq 'ARRAY' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $all eq "ARRAY") 0 0 0
396 {
397 0 $_->remove for( @$all );
398 }
399 }
400  
401 sub _trigger_event_for
402 0 ▶{
Subroutine _trigger_event_for: not called CC=10 SCAR=47.0
403 0 my $self = shift( @_ );
404 0 my $type = shift( @_ );
405 0 my $class;
406 0 ▶ if( index( $_[0], '::' ) != -1 )
Branch: 1 branch
BranchTrueFalseTotal
if (index($_[0], "::") != -1) 0 0 0
407 {
408 0 $class = shift( @_ );
409 }
410 0 my $opts = $self->_get_args_as_hash( @_ );
411 0 ▶ $opts->{class} //= '';
Condition0%0/2
$opts->{"class"} //= ""
l!l
00
MC/DC0%0/1
$opts->{"class"} //= ""
$opts->{"class"}
Truth table0%0/2
A
A= $opts->{"class"}
Aresult
11
00
412 0 ▶ $class //= $opts->{class} || 'HTML::Object::Event';
Condition0%0/4
$class //= $opts->{"class"} || "HTML::Object::Event"
l!l
00
$opts->{'class'} || 'HTML::Object::Event'
l!l
00
MC/DC0%0/1
$class //= $opts->{"class"} || "HTML::Object::Event"
$class
Truth table0%0/2
A
A= $class
Aresult
11
00
413 0 ▶ $self->_load_class( $class ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class($class) or return $self->pass_error 0 0 0
414 0 ▶ my $event = $class->new( $type, @_ ) || return( $self->error( $class->error ) );
Condition0%0/2
$class->new($type, @_) || (return $self->error($class->error))
l!l
00
MC/DC0%0/1
$class->new($type, @_) || (return $self->error($class->error))
$class->new($type, @_)
Truth table0%0/2
A
A= $class->new($type, @_)
Aresult
11
00
415 0 ▶ if( CORE::exists( $opts->{callback} ) && ref( $opts->{callback} ) eq 'CODE' )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{"callback"} and ref $opts->{"callback"} eq "CODE") 0 0 0
Condition0%0/3
exists $opts->{"callback"} and ref $opts->{"callback"} eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
exists $opts->{"callback"} and ref $opts->{"callback"} eq "CODE"
exists $opts->{"callback"} ref $opts->{"callback"} eq "CODE"
Truth table0%0/3
A and B
A= exists $opts->{"callback"}B= ref $opts->{"callback"} eq "CODE"
ABresult
0X0
100
111
416 {
417 0 local $_ = $event;
418 0 $opts->{callback}->( $event );
419 }
420 0 ▶ $self->dispatchEvent( $event ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->dispatchEvent($event) or return $self->pass_error 0 0 0
421 0 return( $event );
422 }
423  
424 36 1;
425 # NOTE: POD
426 __END__
« Object/DOM/Element/Table.pm ↑ Summary Object/DOM/TextTrackCueList.pm »