lib/HTML/Object/EventListener.pm

filter: Statementstmt 97.7% 43 / 44 Branchbran 50.0% 2 / 4 Conditioncond 50.0% 4 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 93.3% 14 / 15 Podpod 100% 8 / 8 totaltotal 85.5% 71 / 83 CC 7 SCAR 8.7
CC
7
Cov
88%
remove
5.4
init
3.0
handleEvent
2.0
CRAP 2.4
« blib/lib/HTML/Object/DOM/Element/Image.pm ↑ Summary blib/lib/HTML/Object/DOM/DocumentFragment.pm »
1 ##----------------------------------------------------------------------------
2 ## HTML Object - ~/lib/HTML/Object/EventListener.pm
3 ## Version v0.2.0
4 ## Copyright(c) 2021 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2021/12/11
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::EventListener;
15 BEGIN
16 {
17 33 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 33 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 33 ▶ warnings::register_categories( 'HTML::Object' );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 33 ▶ use parent qw( Module::Generic );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 33 ▶ use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 33 our $VERSION = 'v0.2.0';
23 };
24  
25 33 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 33 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 sub init
29 {
30 5 ▶ my $self = shift( @_ );
Subroutine init: called CC=3 SCAR=11.1
Pod documented
31 5 $self->{element} = undef;
32 5 $self->{type} = undef;
33 5 $self->{code} = undef;
34 5 $self->{options} = {};
35 5 $self->{_init_strict_use_sub} = 1;
36 5 ▶ $self->SUPER::init( @_ ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init(@_) or return $self->pass_error 0 5 5
37 5 ▶ return( $self->error( "No callback has been set for this event listener." ) ) if( !$self->{code} );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"code"} 0 5 5
38 5 return( $self );
39 }
40  
41 6 ▶sub capture { return( shift->options->{capture} ); }
Subroutine capture: called CC=1 SCAR=0.0
Pod documented
42  
43 14 ▶sub code { return( shift->_set_get_code( 'code', @_ ) ); }
Subroutine code: called CC=1 SCAR=0.0
Pod documented
44  
45 7 ▶sub element { return( shift->_set_get_object_without_init( 'element', 'HTML::Object::Element', @_ ) ); }
Subroutine element: called CC=1 SCAR=0.0
Pod documented
46  
47 0 ▶sub handleEvent { return( shift->code( @_ ) ); }
Subroutine handleEvent: not called CC=1 SCAR=6.9
Pod documented
48  
49 13 ▶sub options { return( shift->_set_get_hash_as_mix_object( 'options', @_ ) ); }
Subroutine options: called CC=1 SCAR=0.0
Pod documented
50  
51 sub remove
52 {
53 2 ▶ my $self = shift( @_ );
Subroutine remove: called CC=5 SCAR=16.8
Pod documented
54 2 ▶ my $elem = $self->element ||
Condition50%1/2
$self->element || (return $self->error({'message', 'No element object found in our event listener!', 'class', 'HTML::Object::SyntaxError'}))
l!l
20
MC/DC0%0/1
$self->element || (return $self->error({'message', 'No element object found in our event listener!', 'class', 'HTML::Object::SyntaxError'}))
$self->element
Truth table50%1/2
A
A= $self->element
Aresult
11
00
55 return( $self->error({
56 message => "No element object found in our event listener!",
57 class => 'HTML::Object::SyntaxError',
58 }) );
59 2 ▶ my $type = $self->type ||
Condition50%1/2
$self->type || (return $self->error({'message', 'No event type found in our event listener!', 'class', 'HTML::Object::SyntaxError'}))
l!l
20
MC/DC0%0/1
$self->type || (return $self->error({'message', 'No event type found in our event listener!', 'class', 'HTML::Object::SyntaxError'}))
$self->type
Truth table50%1/2
A
A= $self->type
Aresult
11
00
60 return( $self->error({
61 message => "No event type found in our event listener!",
62 class => 'HTML::Object::SyntaxError',
63 }) );
64 2 ▶ my $code = $self->code ||
Condition50%1/2
$self->code || (return $self->error({'message', 'No event callabck found in our event listener!', 'class', 'HTML::Object::SyntaxError'}))
l!l
20
MC/DC0%0/1
$self->code || (return $self->error({'message', 'No event callabck found in our event listener!', 'class', 'HTML::Object::SyntaxError'}))
$self->code
Truth table50%1/2
A
A= $self->code
Aresult
11
00
65 return( $self->error({
66 message => "No event callabck found in our event listener!",
67 class => 'HTML::Object::SyntaxError',
68 }) );
69 2 my $opts = $self->options;
70 2 ▶ $opts->{capture} //= 0;
Condition50%1/2
$opts->{"capture"} //= 0
l!l
02
MC/DC0%0/1
$opts->{"capture"} //= 0
$opts->{"capture"}
Truth table50%1/2
A
A= $opts->{"capture"}
Aresult
11
00
71 2 $elem->removeEventListener( $type, $code, { capture => $opts->{capture} });
72 2 return( $self );
73 }
74  
75 7 ▶sub type { return( shift->_set_get_scalar_as_object( 'type', @_ ) ); }
Subroutine type: called CC=1 SCAR=0.0
Pod documented
76  
77 33 1;
78 # NOTE: POD
79 __END__
80  
81 =encoding utf-8
82  
83 =head1 NAME
84  
85 HTML::Object::EventListener - HTML Object Event Listener Class
86  
87 =head1 SYNOPSIS
88  
89 use HTML::Object::EventListener;
90 my $handler = HTML::Object::EventListener->new ||
91 die( HTML::Object::EventListener->error, "\n" );
92  
93 =head1 VERSION
94  
95 v0.2.0
96  
97 =head1 DESCRIPTION
98  
99 This module implements an L<HTML::Object> event listener. It is instantiated by the L<addEventListener|HTML::Object::EventTarget/addEventListener> method
100  
101 =head1 CONSTRUCTOR
102  
103 =head2 new
104  
105 Provided with an hash or hash reference of options and this will instantiate a new even listener and return the object.
106  
107 It takes the same options as the methods listed below.
108  
109 =head1 METHODS
110  
111 =head2 capture
112  
113 Sets or get the C<capture> boolean value for this event listener.
114  
115 =head2 code
116  
117 Same as L</handleEvent>
118  
119 =head2 element
120  
121 Sets or gets the L<HTML::Object::Element> object to which is attached this event listener.
122  
123 =head2 handleEvent
124  
125 Set or get an anonymous subroutine or a reference to a subroutine that is called whenever an event of the specified type occurs.
126  
127 =head2 options
128  
129 Set or get an hash reference of options for this event handler. Those options are the same as the ones passed to L<HTML::Object::EventTarget/addEventListener>
130  
131 =head2 remove
132  
133 A convenient alternative method to L<HTML::Object::EventTarget/removeEventListener>. It takes no parameter and calls L<HTML::Object::EventTarget/removeEventListener> with all the necessary parameters, hassle free.
134  
135 It returns the current event listener object upon success, and C<undef> upon error and then set an L<error|Module::Generic/error>
136  
137 =head2 type
138  
139 Set or get the type of event this object is for.
140  
141 =head1 AUTHOR
142  
143 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
144  
145 =head1 SEE ALSO
146  
147 L<HTML::Object::EventTarget>, L<HTML::Object::Event>, L<HTML::Object::EventListener>
148  
149 L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/Events/Event_handlers>
150  
151 =head1 COPYRIGHT & LICENSE
152  
153 Copyright(c) 2021 DEGUEST Pte. Ltd.
154  
155 All rights reserved
156  
157 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
158  
159 =cut
« blib/lib/HTML/Object/DOM/Element/Image.pm ↑ Summary blib/lib/HTML/Object/DOM/DocumentFragment.pm »