DOM/EventTarget.pm

filter: Statementstmt 95.7% 135 / 141 Branchbran 85.3% 70 / 82 Conditioncond 66.1% 82 / 124 MC/DCmcdc 40.5% 30 / 74 Subroutinesub 94.7% 18 / 19 Podpod 85.7% 6 / 7 totaltotal 76.2% 341 / 447 SCAR 48.5
CC
88
Cov
83%
_dispatch_event
119.6
trigger_event
11.1
get_event_listeners
7.0
CRAP 128.0
« DOM/Element.pm ↑ Summary DOM/Node.pm »
1 package HTML::DOM::EventTarget;
2  
3 28 our $VERSION = '0.057';
4  
5  
6 28 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 28 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 28 ▶no warnings qw ' utf8 parenthesis ';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 28 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 28 ▶use HTML::DOM::Event;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 28 ▶use HTML::DOM::Exception qw 'UNSPECIFIED_EVENT_TYPE_ERR';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 28 ▶use Scalar::Util qw'refaddr blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 28 ▶use HTML::DOM::_FieldHash;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 28 fieldhashes \my(
17 %evh, # event handlers
18 %cevh, # capturing event handlers
19 %aevh, # attribute event handlers
20 );
21  
22 =head1 NAME
23  
24 HTML::DOM::EventTarget - Perl implementation of the DOM EventTarget interface
25  
26 =head1 VERSION
27  
28 Version 0.057
29  
30 =head1 SYNOPSIS
31  
32 use HTML::DOM;
33 $doc = HTML::DOM->new;
34 $doc->isa('HTML::DOM::EventTarget'); # true
35  
36 $event = $doc->createEvent('MouseEvents');
37 $event->initEvent('click',1,1);
38  
39 $doc->trigger_event('click');
40 $doc->dispatchEvent($event);
41 # etc
42  
43 =head1 DESCRIPTION
44  
45 This class provides the W3C's EventTarget DOM interface. It serves as a
46 base class for L<HTML::DOM::Node> and L<HTML::DOM::Attr>, but any class you
47 write can inherit from it.
48  
49 This class provides the methods listed under L</METHODS>, but will also use
50 a few
51 others
52 defined by subclasses, if they are present:
53  
54 =over
55  
56 =item parentNode
57  
58 =item event_parent
59  
60 These are used to determine the 'ancestry' of the event target, through
61 which the event will be dispatched. For each object, starting with the
62 target, the C<parentNode> method is called; if it doesn't exist or returns
63 false, the C<event_parent> method is tried. If that fails, then the object
64 is taken to be the topmost object.
65  
66 =item error_handler
67  
68 The return value of this method, if it exists and returns one, is presumed
69 to be a code ref, and is called whenever an event handler (listener) dies.
70 If there is no C<error_handler> method that returns true, then
71 C<< $target->ownerDocument->error_handler >> is used instead. If that
72 fails, then errors are ignored.
73  
74 =item event_listeners_enabled
75  
76 If this method exists and returns false, then event handlers are not
77 called.
78 If there is no C<event_listeners_enabled> method,
79 then
80 C<< $target->ownerDocument->event_listeners_enabled >> is used instead.
81  
82 =item ownerDocument
83  
84 See C<error_handler> and C<event_listeners_enabled>.
85  
86 =back
87  
88 =head1 METHODS
89  
90 If a subclass needs to store event handlers and listeners elsewhere (e.g.,
91 associating them with another object), it can override C<addEventListener>,
92 C<removeEventListener>, C<event_handler> and C<get_event_listeners>.
93  
94 =over
95  
96 =item addEventListener($event_name, $listener, $capture)
97  
98 The C<$listener> should be either a coderef or an object with a
99 C<handleEvent> method. (HTML::DOM does not implement any such object since
100 it would just be a wrapper around a coderef anyway, but has support for
101 them.) An object with C<&{}> overloading will also do.
102  
103 C<$capture> is a boolean indicating whether this is to be triggered during
104 the 'capture' phase.
105  
106 =cut
107  
108 sub addEventListener {
109 885 ▶ my ($self,$name,$listener, $capture) = @_;
Subroutine addEventListener: called CC=1 SCAR=0.0
Pod documented
110 (\(%cevh, %evh))[!$capture]->{$self}
111 885 {lc $name}{refaddr $listener} = $listener;
112 885 return;
113 }
114  
115  
116 =item removeEventListener($event_name, $listener, $capture)
117  
118 The C<$listener> should be the same reference passed to
119 C<addEventListener>.
120  
121 =cut
122  
123 sub removeEventListener {
124 99 ▶ my ($self,$name,$listener, $capture) = @_;
Subroutine removeEventListener: called CC=3 SCAR=11.0
Pod documented
125 99 $name = lc $name;
126 99 my $h = (\(%cevh, %evh))[!$capture];
127 exists $h->{$self}
128 and exists $$h{$self}{$name}
129 99 ▶ and delete $$h{$self}{$name}{refaddr $listener};
Branch: 1 branch
BranchTrueFalseTotal
exists $h->{$self} and exists $h->{$self}{$name} and delete $h->{$self}{$name}{builtin::refaddr($listener)} 95 4 99
Condition66%2/3
exists $h->{$self} and exists $h->{$self}{$name}
!ll&&!rl&&r
0495
MC/DC50%1/2
exists $h->{$self} and exists $h->{$self}{$name}
exists $h->{$self} exists $h->{$self}{$name}
Truth table66%2/3
A and B
A= exists $h->{$self}B= exists $h->{$self}{$name}
ABresult
0X0
100
111
130 99 return;
131 }
132  
133  
134 =item on* (onthis, onthat, onclick, onfoo, etc.)
135  
136 This applies to any all-lowercase method beginning with C<on>. Basically,
137 C<< $target->onclick(\&sub) >> is equivalent to
138 C<< $target->addEventListener('click', \&sub, 0) >>, except that it
139 replaces any event handler already assigned via C<onclick>, returning it.
140 C<< $target->onclick >> (without arguments) returns the event handler
141 previously assigned to C<onclick> if there is one.
142  
143 =cut
144  
145 sub AUTOLOAD {
146 3446 ▶ my($pack,$meth) = our $AUTOLOAD =~ /(.*)::(.*)/s;
Subroutine AUTOLOAD: called CC=2 SCAR=6.9
147 3446 ▶ $meth =~ /^on([a-z]+)\z/
Branch: 1 branch
BranchTrueFalseTotal
$meth =~ /^on([a-z]+)\z/ or die qq[Can't locate object method "$meth" via package ] . qq["$pack" at ] . join(" line ", (caller)[1, 2]) . "\n" 3433 13 3446
148 or die "Can't locate object method \"$meth\" via package "
149 . qq'"$pack" at '.join' line ',(caller)[1,2]
150 ,. "\n";
151 13 shift->event_handler($1, @_);
152 }
153 ▶sub DESTROY{}
Subroutine DESTROY: not called CC=1 SCAR=0.0
154  
155 =item event_handler ( $name )
156  
157 =item event_handler ( $name, $new_value )
158  
159 This is an accessor method for event listeners created by HTML or DOM
160 attributes beginning with 'on'. This is used internally by the C<on*>
161 methods. You can use it directly for efficiency's sake.
162  
163 This method used to be called C<attr_event_listener>, but that was a
164 mistake, as there is a distinction between handlers and listeners. The old
165 name is still available but will be removed in a future release. It simply
166 calls C<event_handler>.
167  
168 =cut
169  
170 sub event_handler {
171 25 ▶ my ($self,$name) = (shift,shift);
Subroutine event_handler: called CC=5 SCAR=16.1
Pod documented
172 25 $name = lc $name;
173 my $old = exists $aevh{$self} && exists $aevh{$self}{$name}
174 25 ▶ && $aevh{$self}{$name};
Condition83%5/6
exists $aevh{$self} && exists $aevh{$self}{$name} && $aevh{$self}{$name}
!ll&&!rl&&r
10015
exists $aevh{$self} and exists $aevh{$self}{$name}
!ll&&!rl&&r
7315
MC/DC66%2/3
exists $aevh{$self} && exists $aevh{$self}{$name} && $aevh{$self}{$name}
exists $aevh{$self} exists $aevh{$self}{$name} $aevh{$self}{$name}
Truth table75%3/4
A and B && C
A= exists $aevh{$self}B= exists $aevh{$self}{$name}C= $aevh{$self}{$name}
ABCresult
0XX0
10X0
1100
1111
175 25 ▶ @_ and $aevh{$self}{$name} = shift;
Branch: 1 branch
BranchTrueFalseTotal
@_ and $aevh{$self}{$name} = shift() 17 8 25
176 25 ▶ $old ||();
Branch: 1 branch
BranchTrueFalseTotal
$old or () 10 15 25
177 }
178 2 ▶sub attr_event_listener { shift->event_handler(@_) }
Subroutine attr_event_listener: called CC=1 SCAR=0.0
Pod undocumented
179  
180  
181 =item get_event_listeners($event_name, $capture)
182  
183 This is not a DOM method (hence the underscores in the name). It returns a
184 list of all event listeners for the given event name. C<$capture> is a
185 boolean that indicates which list to return, either 'capture' listeners or
186 normal ones.
187  
188 If there is an event handler for this event (and C<$capture> is false),
189 then C<get_event_listeners> tacks a wrapper for the event handler on to the
190 end of the list it returns.
191  
192 =for comment
193 This is no longer true. But we may need a similar warning in case other packages install listeners that must not be removed.
194 B<Warning:> This method is intended mostly for internal use, but you can
195 go ahead and use it if you like. Just beware that some of the event
196 handlers returned may have been installed automatically by HTML::DOM, and
197 are necessary for its internal workings, so don't go passing those to
198 C<removeEventListener> and expect all to go well.
199  
200 =cut
201  
202 sub get_event_listeners { # uses underscores because it is not a DOM method
203 6389 ▶ my($self,$name,$capture) = @_;
Subroutine get_event_listeners: called CC=7 SCAR=19.5
Pod documented
204 6389 $name = lc $name;
205 6389 my $h = (\(%cevh, %evh))[!$capture]->{$self};
206 my @ret = $h && exists $$h{$name}
207 6389 ▶ ? values %{$$h{$name}}
Branch: 1 branch
BranchTrueFalseTotal
$h && exists $h->{$name} ? : 1087 5302 6389
Condition100%3/3
$h and exists $h->{$name}
!ll&&!rl&&r
51421601087
MC/DC100%2/2
$h and exists $h->{$name}
$h exists $h->{$name}
Truth table100%3/3
A and B
A= $hB= exists $h->{$name}
ABresult
0X0
100
111
208 : ();
209 6389 ▶ if(!$capture && exists $aevh{$self} && exists $aevh{$self}{$name}
Branch: 1 branch
BranchTrueFalseTotal
if (not $capture and exists $aevh{$self} and exists $aevh{$self}{$name} and defined(my $aevh = $aevh{$self}{$name})) { } 13 6376 6389
Condition88%8/9
not $capture and exists $aevh{$self} and exists $aevh{$self}{$name} and defined(my $aevh = $aevh{$self}{$name})
!ll&&!rl&&r
6376013
not $capture and exists $aevh{$self} and exists $aevh{$self}{$name}
!ll&&!rl&&r
63373913
not $capture and exists $aevh{$self}
!ll&&!rl&&r
2949338852
MC/DC75%3/4
not $capture and exists $aevh{$self} and exists $aevh{$self}{$name} and defined(my $aevh = $aevh{$self}{$name})
not $capture exists $aevh{$self} exists $aevh{$self}{$name} defined(my $aevh = $aevh{$self}{$name})
Truth table80%4/5
not(A) and B and C and D
A= not $captureB= exists $aevh{$self}C= exists $aevh{$self}{$name}D= defined(my $aevh = $aevh{$self}{$name})
ABCDresult
0XXX0
10XX0
110X0
11100
11111
210 and defined (my $aevh = $aevh{$self}{$name})) {
211 @ret, sub {
212 10 ▶ my $ret =
Subroutine __ANON__: called CC=6 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
defined builtin::blessed($aevh) && $aevh->can('call_with') ? : 2 8 10
Condition66%2/3
defined builtin::blessed($aevh) and $aevh->can("call_with")
!ll&&!rl&&r
802
MC/DC50%1/2
defined builtin::blessed($aevh) and $aevh->can("call_with")
defined builtin::blessed($aevh) $aevh->can("call_with")
Truth table66%2/3
A and B
A= defined builtin::blessed($aevh)B= $aevh->can("call_with")
ABresult
0X0
100
111
213 defined blessed $aevh && $aevh->can('call_with')
214 ? call_with $aevh $_[0]->currentTarget, $_[0]
215 : &$aevh($_[0]);
216 10 ▶ defined $ret
Branch: 2 branches
BranchTrueFalseTotal
defined $ret and $name eq "mouseover" ? $ret : !$ret and $_[0]->preventDefault 2 8 10
$name eq 'mouseover' ? : 1 7 8
Condition100%3/3
defined $ret and $name eq "mouseover" ? $ret : !$ret
!ll&&!rl&&r
262
MC/DC100%2/2
defined $ret and $name eq "mouseover" ? $ret : !$ret
defined $ret $name eq "mouseover" ? $ret : !$ret
Truth table100%3/3
A and B
A= defined $retB= $name eq "mouseover" ? $ret : !$ret
ABresult
0X0
100
111
217 && ($name eq 'mouseover' ? $ret : !$ret)
218 && $_[0]->preventDefault;
219 }
220 13 }
221 6376 else { @ret }
222 }
223  
224 =item dispatchEvent($event_object)
225  
226 $event_object is an object returned by HTML::DOM's C<createEvent> method,
227 or any object that implements the interface documented in
228 L<HTML::DOM::Event>.
229  
230 C<dispatchEvent> does not automatically call the handler passed to the
231 document's C<default_event_handler>. It is expected that the code that
232 calls this method will do that (see also L</trigger_event>).
233  
234 The return value is a boolean indicating whether the default action
235 should be taken (i.e., whether preventDefault was I<not> called).
236  
237 =for comment
238 Actually, it's the event object itself (unless it was called in
239 auto-vivacious mode and the event was never auto-vivved); but that’s an
240 implementation detail that’s subject to change willy-nilly.
241  
242 =cut
243  
244 sub dispatchEvent {
245 1307 ▶ _dispatch_event(shift, 1, shift);
Subroutine dispatchEvent: called CC=1 SCAR=0.0
Pod documented
246 }
247  
248 sub _dispatch_event { # This is where all the work is.
249 # We accept two different types of arg lists:
250 # 1) $target->...($yes_it_is_an_event_object, $event_obj)
251 # 2) $target->...($no_it's_not_an_event_object,
252 # $event_category, \&arg_maker, %more_args)
253 # The second is for autovivving the event object, as we do with
254 # attr modifications, to avoid creating an attr node unnecessarily.
255 # We init an event with (%more_args, &arg_maker).
256  
257 1891 ▶ my ($target, $event) = (shift,shift);
Subroutine _dispatch_event: called CC=60 SCAR=47.8
258 1891 ▶ $event &&= shift or my ($cat, $args, %args) = @_;;
Branch: 1 branch
BranchTrueFalseTotal
$event &&= shift() or my($cat, $args, %args) = 584 1307 1891
Condition66%2/3
$event &&= shift()
!ll&&!rl&&r
58401307
MC/DC50%1/2
$event &&= shift()
$event shift()
Truth table66%2/3
A &&= B
A= $eventB= shift()
ABresult
0X0
100
111
259 1891 ▶ my $name = $event ? $event->type : $args{type};
Branch: 1 branch
BranchTrueFalseTotal
$event ? : 1307 584 1891
260  
261 1891 ▶ die HTML::DOM::Exception->new(UNSPECIFIED_EVENT_TYPE_ERR,
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and length $name 6 1885 1891
Condition100%3/3
defined $name and length $name
!ll&&!rl&&r
421885
MC/DC100%2/2
defined $name and length $name
defined $name length $name
Truth table100%3/3
A and B
A= defined $nameB= length $name
ABresult
0X0
100
111
262 'The type of event has not been specified')
263 unless defined $name and length $name;
264  
265 1885 ▶ $event->_set_target($target) if $event && !$event->target;
Branch: 1 branch
BranchTrueFalseTotal
if $event and not $event->target 1299 586 1885
Condition100%3/3
$event and not $event->target
!ll&&!rl&&r
58421299
MC/DC100%2/2
$event and not $event->target
$event not $event->target
Truth table100%3/3
A and not(B)
A= $eventB= not $event->target
ABresult
0X0
100
111
266  
267 1885 local *@;
268  
269 # Check to see whether we are supposed to skip event handlers, and
270 # short-circuit if that’s the case:
271 Foo: {
272 1885 my $doc;
273 my $sub = $target->can('event_listeners_enabled')
274 1885 ▶ || (eval{$doc = $target->ownerDocument}||next Foo)
Condition85%6/7
$target->can('event_listeners_enabled') || (eval { do { $doc = $target->ownerDocument } } || (next Foo))->can('event_listeners_enabled') || (last Foo)
l!l
18650
$target->can("event_listeners_enabled") or (eval { do { $doc = $target->ownerDocument } } || (next Foo))->can("event_listeners_enabled")
l!l&&r!l&&!r
1618681
eval { do { $doc = $target->ownerDocument } } || (next Foo)
l!l
184920
MC/DC0%0/2
$target->can('event_listeners_enabled') || (eval { do { $doc = $target->ownerDocument } } || (next Foo))->can('event_listeners_enabled') || (last Foo)
$target->can("event_listeners_enabled") (eval { do { $doc = $target->ownerDocument } } || (next Foo))->can("event_listeners_enabled")
MC/DC100%1/1
eval { do { $doc = $target->ownerDocument } } || (next Foo)
eval { do { $doc = $target->ownerDocument } }
Truth table66%2/3
A or B
A= $target->can("event_listeners_enabled")B= (eval { do { $doc = $target->ownerDocument } } || (next Foo))->can("event_listeners_enabled")
ABresult
1X1
011
000
Truth table100%2/2
A
A= eval { do { $doc = $target->ownerDocument } }
Aresult
11
00
275 ->can('event_listeners_enabled')
276 || last Foo;
277 1865 ▶ &$sub($doc||$target) or return $event||1
Branch: 1 branch
BranchTrueFalseTotal
&$sub($doc || $target) or return $event || 1 4 1861 1865
Condition60%3/5
$doc || $target
l!l&&r!l&&!r
1849160
$event || 1
l!l
40
MC/DC0%0/2
$doc || $target
$doc $target
MC/DC0%0/1
$event || 1
$event
Truth table66%2/3
A || B
A= $docB= $target
ABresult
1X1
011
000
Truth table50%1/2
A
A= $event
Aresult
11
00
278 }
279
280 # Basic event flow is as follows:
281 # 1. The 'capturing' phase: Go through the node's ancestors,
282 # starting from the top of the tree. For each one, trigger any
283 # capture events it might have.
284 # 2. Trigger events on the $target.
285 # 3. 'Bubble-blowing' phase: Trigger events on the target's ances-
286 # tors in reverse order (top last).
287  
288 1881 my $eh = eval{$target->error_handler}
289 1881 ▶ ||eval{$target->ownerDocument->error_handler};
Condition100%3/3
eval { do { $target->error_handler } } || eval { do { $target->ownerDocument->error_handler } }
l!l&&r!l&&!r
111879
MC/DC50%1/2
eval { do { $target->error_handler } } || eval { do { $target->ownerDocument->error_handler } }
eval { do { $target->error_handler } } eval { do { $target->ownerDocument->error_handler } }
Truth table66%2/3
A || B
A= eval { do { $target->error_handler } }B= eval { do { $target->ownerDocument->error_handler } }
ABresult
1X1
011
000
290  
291 1881 my @lineage = $target;
292 {
293 1881 push @lineage, eval{$lineage[-1]->parentNode}
294 4788 ▶ ||eval{$lineage[-1]->event_parent}
Condition100%5/5
eval { do { $lineage[-1]->parentNode } } || eval { do { $lineage[-1]->event_parent } } || (last)
l!l
29071881
eval { do { $lineage[-1]->parentNode } } or eval { do { $lineage[-1]->event_parent } }
l!l&&r!l&&!r
2885221881
MC/DC100%2/2
eval { do { $lineage[-1]->parentNode } } || eval { do { $lineage[-1]->event_parent } } || (last)
eval { do { $lineage[-1]->parentNode } } eval { do { $lineage[-1]->event_parent } }
Truth table100%3/3
A or B
A= eval { do { $lineage[-1]->parentNode } }B= eval { do { $lineage[-1]->event_parent } }
ABresult
1X1
011
000
295 ||last;
296 redo
297 2907 }
298 1881 shift @lineage; # shouldn’t include the target
299 # $lineage[-1] is the root, by the way
300  
301 1881 my $initted;
302  
303 1881 for (reverse @lineage) { # root first
304 2907 my @l = $_->get_event_listeners($name, 1);
305 2907 ▶ if(@l and !$initted++) {
Branch: 1 branch
BranchTrueFalseTotal
if (@l and not $initted++) 10 2897 2907
Condition66%2/3
@l and not $initted++
!ll&&!rl&&r
2897010
MC/DC50%1/2
@l and not $initted++
@l not $initted++
Truth table66%2/3
A and not(B)
A= @lB= not $initted++
ABresult
0X0
100
111
306 # ~~~ This occurs three times; it probably ought to
307 # go it its own sub
308 10 ▶ $event ||= do {
Condition33%1/3
$event ||= do { (my $e = ($target->ownerDocument || $target)->createEvent($cat))->init(%args, &$args); $e->_set_target($target) unless $e->target; $e }
l!l&&r!l&&!r
1000
MC/DC0%0/2
$event ||= do { (my $e = ($target->ownerDocument || $target)->createEvent($cat))->init(%args, &$args); $e->_set_target($target) unless $e->target; $e }
$event do { (my $e = ($target->ownerDocument || $target)->createEvent($cat))->init(%args, &$args); $e->_set_target($target) unless $e->target; $e }
Truth table33%1/3
A ||= B
A= $eventB= do { (my $e = ($target->ownerDocument || $target)->createEvent($cat))->init(%args, &$args); $e->_set_target($target) unless $e->target; $e }
ABresult
1X1
011
000
309 0 ▶ (my $e =
Condition0%0/3
$target->ownerDocument || $target
l!l&&r!l&&!r
000
MC/DC0%0/2
$target->ownerDocument || $target
$target->ownerDocument $target
Truth table0%0/3
A || B
A= $target->ownerDocumentB= $target
ABresult
1X1
011
000
310 ($target->ownerDocument||$target)
311 ->createEvent($cat)
312 )->init(
313 %args, &$args
314 );
315 0 ▶ $e->_set_target($target) unless $e->target;
Branch: 1 branch
BranchTrueFalseTotal
unless $e->target 0 0 0
316 0 $e;
317 };
318 10 $event->_set_eventPhase(
319 HTML::DOM::Event::CAPTURING_PHASE);
320 }
321 2907 ▶ $event-> _set_currentTarget($_) if @l;
Branch: 1 branch
BranchTrueFalseTotal
if @l 10 2897 2907
322 2907 for(@l) {
323 18 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { defined builtin::blessed($_) && $_->can("handleEvent") ? $_->handleEvent($event) : &$_($event); 1 } } or $eh and &$eh() 0 18 18
Condition33%1/3
eval { do { defined builtin::blessed($_) && $_->can('handleEvent') ? $_->handleEvent($event) : &$_($event); 1 } } || $eh && &$eh()
l!l&&r!l&&!r
1800
MC/DC0%0/2
eval { do { defined builtin::blessed($_) && $_->can('handleEvent') ? $_->handleEvent($event) : &$_($event); 1 } } || $eh && &$eh()
eval { do { defined builtin::blessed($_) && $_->can('handleEvent') ? $_->handleEvent($event) : &$_($event); 1 } } $eh && &$eh()
Truth table33%1/3
A || B
A= eval { do { defined builtin::blessed($_) && $_->can('handleEvent') ? $_->handleEvent($event) : &$_($event); 1 } }B= $eh && &$eh()
ABresult
1X1
011
000
324 18 ▶ defined blessed $_ && $_->can('handleEvent') ?
Branch: 1 branch
BranchTrueFalseTotal
defined builtin::blessed($_) && $_->can('handleEvent') ? : 0 18 18
Condition33%1/3
defined builtin::blessed($_) and $_->can("handleEvent")
!ll&&!rl&&r
1800
MC/DC0%0/2
defined builtin::blessed($_) and $_->can("handleEvent")
defined builtin::blessed($_) $_->can("handleEvent")
Truth table33%1/3
A and B
A= defined builtin::blessed($_)B= $_->can("handleEvent")
ABresult
0X0
100
111
325 $_->handleEvent($event) : &$_($event);
326 18 ▶ 1
Condition0%0/3
$eh and &$eh()
!ll&&!rl&&r
000
MC/DC0%0/2
$eh and &$eh()
$eh &$eh()
Truth table0%0/3
A and B
A= $ehB= &$eh()
ABresult
0X0
100
111
327 } or $eh and &$eh();
328 }
329 2907 ▶ return !cancelled $event if
Branch: 1 branch
BranchTrueFalseTotal
if ($event || (next))->propagation_stopped 2 2660 2662
Condition100%2/2
$event || (next)
l!l
2662245
MC/DC100%1/1
$event || (next)
$event
Truth table100%2/2
A
A= $event
Aresult
11
00
330 ($event||next)->propagation_stopped;
331 }
332  
333 1879 my @l = $target->get_event_listeners($name);
334 1879 ▶ if(@l) {
Branch: 1 branch
BranchTrueFalseTotal
if (@l) 768 1111 1879
335 768 ▶ unless ($initted++) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($initted++) 760 8 768
336 760 ▶ $event ||= do {
Condition66%2/3
$event ||= do { (my $e = ($target->ownerDocument || $target)->createEvent($cat))->init(%args, &$args); $e->_set_target($target) unless $e->target; $e }
l!l&&r!l&&!r
5312290
MC/DC0%0/2
$event ||= do { (my $e = ($target->ownerDocument || $target)->createEvent($cat))->init(%args, &$args); $e->_set_target($target) unless $e->target; $e }
$event do { (my $e = ($target->ownerDocument || $target)->createEvent($cat))->init(%args, &$args); $e->_set_target($target) unless $e->target; $e }
Truth table66%2/3
A ||= B
A= $eventB= do { (my $e = ($target->ownerDocument || $target)->createEvent($cat))->init(%args, &$args); $e->_set_target($target) unless $e->target; $e }
ABresult
1X1
011
000
337 229 ▶ (my $e =
Condition33%1/3
$target->ownerDocument || $target
l!l&&r!l&&!r
22900
MC/DC0%0/2
$target->ownerDocument || $target
$target->ownerDocument $target
Truth table33%1/3
A || B
A= $target->ownerDocumentB= $target
ABresult
1X1
011
000
338 ($target->ownerDocument||$target)
339 ->createEvent($cat)
340 )->init(
341 %args, &$args
342 );
343 229 ▶ $e->_set_target($target) unless $e->target;
Branch: 1 branch
BranchTrueFalseTotal
unless $e->target 229 0 229
344 229 $e;
345 };
346 };
347 768 $event->_set_eventPhase(HTML::DOM::Event::AT_TARGET);
348 768 $event->_set_currentTarget($target);
349 }
350 eval {
351 780 ▶ defined blessed $_ && $_->can('handleEvent') ?
Branch: 1 branch
BranchTrueFalseTotal
defined builtin::blessed($_) && $_->can('handleEvent') ? : 0 780 780
Condition33%1/3
defined builtin::blessed($_) and $_->can("handleEvent")
!ll&&!rl&&r
78000
MC/DC0%0/2
defined builtin::blessed($_) and $_->can("handleEvent")
defined builtin::blessed($_) $_->can("handleEvent")
Truth table33%1/3
A and B
A= defined builtin::blessed($_)B= $_->can("handleEvent")
ABresult
0X0
100
111
352 $_->handleEvent($event) : &$_($event);
353 775 ▶ 1
Condition66%2/3
$eh and &$eh()
!ll&&!rl&&r
320
MC/DC0%0/2
$eh and &$eh()
$eh &$eh()
Truth table66%2/3
A and B
A= $ehB= &$eh()
ABresult
0X0
100
111
354 1879 ▶ } or $eh and &$eh() for @l;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { defined builtin::blessed($_) && $_->can("handleEvent") ? $_->handleEvent($event) : &$_($event); 1 } } or $eh and &$eh() 5 775 780
Condition66%2/3
eval { do { defined builtin::blessed($_) && $_->can('handleEvent') ? $_->handleEvent($event) : &$_($event); 1 } } || $eh && &$eh()
l!l&&r!l&&!r
77550
MC/DC0%0/2
eval { do { defined builtin::blessed($_) && $_->can('handleEvent') ? $_->handleEvent($event) : &$_($event); 1 } } || $eh && &$eh()
eval { do { defined builtin::blessed($_) && $_->can('handleEvent') ? $_->handleEvent($event) : &$_($event); 1 } } $eh && &$eh()
Truth table66%2/3
A || B
A= eval { do { defined builtin::blessed($_) && $_->can('handleEvent') ? $_->handleEvent($event) : &$_($event); 1 } }B= $eh && &$eh()
ABresult
1X1
011
000
355 return +($event) x !cancelled $event if
356 $event
357 ? $event->propagation_stopped || !$event->bubbles
358 1879 ▶ : !$args{propagates_up};
Branch: 2 branches
BranchTrueFalseTotal
if $event ? $event->propagation_stopped || !$event->bubbles : !$args{"propagates_up"} 385 1494 1879
$event ? : 1524 355 1879
Condition100%3/3
$event->propagation_stopped or not $event->bubbles
l!l&&r!l&&!r
23831139
MC/DC100%2/2
$event->propagation_stopped or not $event->bubbles
$event->propagation_stopped not $event->bubbles
Truth table100%3/3
A or not(B)
A= $event->propagation_stoppedB= not $event->bubbles
ABresult
1X1
011
000
359  
360 1494 my $initted2;
361 1494 for (@lineage) { # root last
362 1265 my @l = $_->get_event_listeners($name);
363 1265 ▶ if(@l){
Branch: 1 branch
BranchTrueFalseTotal
if (@l) 217 1048 1265
364 217 ▶ unless($initted++) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($initted++) 19 198 217
365 19 ▶ $event ||= do {
Condition33%1/3
$event ||= do { (my $e = ($target->ownerDocument || $target)->createEvent($cat))->init(%args, &$args); $e->_set_target($target) unless $e->target; $e }
l!l&&r!l&&!r
1900
MC/DC0%0/2
$event ||= do { (my $e = ($target->ownerDocument || $target)->createEvent($cat))->init(%args, &$args); $e->_set_target($target) unless $e->target; $e }
$event do { (my $e = ($target->ownerDocument || $target)->createEvent($cat))->init(%args, &$args); $e->_set_target($target) unless $e->target; $e }
Truth table33%1/3
A ||= B
A= $eventB= do { (my $e = ($target->ownerDocument || $target)->createEvent($cat))->init(%args, &$args); $e->_set_target($target) unless $e->target; $e }
ABresult
1X1
011
000
366 0 ▶ (my $e =
Condition0%0/3
$target->ownerDocument || $target
l!l&&r!l&&!r
000
MC/DC0%0/2
$target->ownerDocument || $target
$target->ownerDocument $target
Truth table0%0/3
A || B
A= $target->ownerDocumentB= $target
ABresult
1X1
011
000
367 ($target->ownerDocument||$target)
368 ->createEvent($cat)
369 )->init(
370 %args, &$args
371 );
372 0 ▶ $e->_set_target($target)
Branch: 1 branch
BranchTrueFalseTotal
unless $e->target 0 0 0
373 unless $e->target;
374 0 $e;
375 };
376 }
377 217 ▶ unless ($initted2++) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($initted2++) 121 96 217
378 121 $event->_set_eventPhase(
379 HTML::DOM::Event::BUBBLING_PHASE);
380 }
381 }
382 1265 ▶ $event-> _set_currentTarget($_) if @l;
Branch: 1 branch
BranchTrueFalseTotal
if @l 217 1048 1265
383 eval {
384 223 ▶ defined blessed $_ && $_->can('handleEvent') ?
Branch: 1 branch
BranchTrueFalseTotal
defined builtin::blessed($_) && $_->can('handleEvent') ? : 0 223 223
Condition33%1/3
defined builtin::blessed($_) and $_->can("handleEvent")
!ll&&!rl&&r
22300
MC/DC0%0/2
defined builtin::blessed($_) and $_->can("handleEvent")
defined builtin::blessed($_) $_->can("handleEvent")
Truth table33%1/3
A and B
A= defined builtin::blessed($_)B= $_->can("handleEvent")
ABresult
0X0
100
111
385 $_->handleEvent($event) : &$_($event);
386 223 ▶ 1
Condition0%0/3
$eh and &$eh()
!ll&&!rl&&r
000
MC/DC0%0/2
$eh and &$eh()
$eh &$eh()
Truth table0%0/3
A and B
A= $ehB= &$eh()
ABresult
0X0
100
111
387 1265 ▶ } or $eh and &$eh() for(@l);
Branch: 1 branch
BranchTrueFalseTotal
eval { do { defined builtin::blessed($_) && $_->can("handleEvent") ? $_->handleEvent($event) : &$_($event); 1 } } or $eh and &$eh() 0 223 223
Condition33%1/3
eval { do { defined builtin::blessed($_) && $_->can('handleEvent') ? $_->handleEvent($event) : &$_($event); 1 } } || $eh && &$eh()
l!l&&r!l&&!r
22300
MC/DC0%0/2
eval { do { defined builtin::blessed($_) && $_->can('handleEvent') ? $_->handleEvent($event) : &$_($event); 1 } } || $eh && &$eh()
eval { do { defined builtin::blessed($_) && $_->can('handleEvent') ? $_->handleEvent($event) : &$_($event); 1 } } $eh && &$eh()
Truth table33%1/3
A || B
A= eval { do { defined builtin::blessed($_) && $_->can('handleEvent') ? $_->handleEvent($event) : &$_($event); 1 } }B= $eh && &$eh()
ABresult
1X1
011
000
388 1265 ▶ return +($event) x !cancelled $event
Branch: 1 branch
BranchTrueFalseTotal
if ($event || (next))->propagation_stopped 2 1030 1032
Condition100%2/2
$event || (next)
l!l
1032233
MC/DC100%1/1
$event || (next)
$event
Truth table100%2/2
A
A= $event
Aresult
11
00
389 if ($event||next)->propagation_stopped;
390 }
391 1492 ▶ return +($event) x !($event||return 1)->cancelled ;
Condition100%2/2
$event || (return 1)
l!l
1137355
MC/DC100%1/1
$event || (return 1)
$event
Truth table100%2/2
A
A= $event
Aresult
11
00
392 }
393  
394 =item trigger_event($event, ...)
395  
396 Here is another non-DOM method. C<$event> can be an event object or simply
397 an event name. This method triggers an
398 event for real, first calling C<dispatchEvent> and then running the default
399 action for the event unless an event listener cancels it.
400  
401 It can take named args following the C<$event> arg. These are passed to the
402 event object's C<init> method. Any
403 omitted args will be filled in with reasonable defaults. These are
404 completely ignored if C<$event> is an event object.
405  
406 Also, you can use the C<default> arg to provide a coderef that will be
407 called as the default event handler. L<HTML::DOM::Node> overrides it to do
408 just that, so you shouldn't need to use this arg except on a custom
409 subclass of EventTarget.
410  
411 When C<$event> is an event name, C<trigger_event> automatically chooses the
412 right event class and a set of default args for that event name, so you can
413 supply just a few. E.g.,
414  
415 $elem->trigger_event('click', shift => 1, button => 1);
416  
417 =begin comment
418  
419 Internal-only features:
420  
421 The interface for this is very clunky, so I’m keeping it private for now.
422 It only exists for the sake of the implementation, anyway.
423  
424 The named args can contain DOMActivate_default => \&sub to specify a
425 default handler for an event type. We don't use default =>
426 { DOMActivate => \&sub } as I originally intended, because that would make
427 it harder for multiple classes
428 to say SUPER::trigger_event($evnt, ..._default => ) without clobbering each
429 other.
430  
431 And there's the 'create event object on demand' interface, which is as
432 follows:
433  
434 $thing->trigger_event('DOMAttrModified', auto_viv => \&arg_maker);
435  
436 This does not automatically supply the view.
437  
438 =end comment
439  
440 =cut
441  
442 sub trigger_event { # non-DOM method
443 1864 ▶ my ($target, $event, %args) = @_;
Subroutine trigger_event: called CC=11 SCAR=24.1
Pod documented
444 1864 ▶ if($args{auto_viv}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"auto_viv"}) 584 1280 1864
445 # For efficiency’s sake, we skip creating the event object
446 # here, and have _dispatch_event create the object on
447 # demand, using the code ref that we pass to it.
448 584 my ($cat, @init_args) = HTML'DOM'Event'defaults($event);
449 584 unshift @init_args, type => $event;
450 584 ▶ if(my $rv = _dispatch_event(
Branch: 1 branch
BranchTrueFalseTotal
if (my $rv = _dispatch_event($target, 0, $cat, $args{"auto_viv"}, @init_args)) 584 0 584
451 $target, 0, $cat, $args{auto_viv},
452 @init_args
453 )) {
454 my $def =
455 $args{"$event\_default"} ||
456 584 ▶ $args{"default"}
Condition80%4/5
$args{"${event}_default"} || $args{'default'} || (return)
l!l
3581
$args{"${event}_default"} or $args{"default"}
l!l&&r!l&&!r
03581
MC/DC50%1/2
$args{"${event}_default"} || $args{'default'} || (return)
$args{"${event}_default"} $args{"default"}
Truth table66%2/3
A or B
A= $args{"${event}_default"}B= $args{"default"}
ABresult
1X1
011
000
457 || return;
458 3 ▶ unless (ref $rv) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $rv) 3 0 3
459 ($rv =
460 HTML'DOM'Event'create_event($cat)
461 )->init(my @args =
462 3 @init_args, &{$args{auto_viv}}
463 );
464 3 $rv->_set_target($target);
465 }
466 3 &$def($rv);
467 }
468 3 return;
469 }
470 1280 my $type;
471 defined blessed $event && $event->isa('HTML::DOM::Event')
472 ? $type = $event->type
473 1280 ▶ : do {
Branch: 1 branch
BranchTrueFalseTotal
defined builtin::blessed($event) && $event->isa('HTML::DOM::Event') ? : 4 1276 1280
Condition66%2/3
defined builtin::blessed($event) and $event->isa("HTML::DOM::Event")
!ll&&!rl&&r
127604
MC/DC50%1/2
defined builtin::blessed($event) and $event->isa("HTML::DOM::Event")
defined builtin::blessed($event) $event->isa("HTML::DOM::Event")
Truth table66%2/3
A and B
A= defined builtin::blessed($event)B= $event->isa("HTML::DOM::Event")
ABresult
0X0
100
111
474 1276 $type = $event;
475 1276 $event = HTML'DOM'Event'create_event((
476 my (undef, @init_args) =
477 HTML'DOM'Event'defaults($type)
478 )[0]);
479 1276 $event->init(
480 type=>$type,
481 @init_args,
482 %args
483 );
484 };
485  
486 $target->dispatchEvent($event) and &{
487 1280 ▶ $args{"$type\_default"} ||
Branch: 1 branch
BranchTrueFalseTotal
$target->dispatchEvent($event) and &{$args{"${type}_default"} or $args{"default"} or return;}($event) 1276 4 1280
488 $args{default}
489 || return
490 1276 ▶ }($event);
Branch: 1 branch
BranchTrueFalseTotal
$args{"${type}_default"} or $args{"default"} or return 1158 118 1276
Condition100%3/3
$args{"${type}_default"} or $args{"default"}
l!l&&r!l&&!r
95231158
MC/DC100%2/2
$args{"${type}_default"} or $args{"default"}
$args{"${type}_default"} $args{"default"}
Truth table100%3/3
A or B
A= $args{"${type}_default"}B= $args{"default"}
ABresult
1X1
011
000
491 122 return;
492 }
493  
494  
495 =back
496  
497 =cut
498  
499 28 1;
500 __END__
« DOM/Element.pm ↑ Summary DOM/Node.pm »