| 1 | ##---------------------------------------------------------------------------- | ||||||||||||
| 2 | ## HTML Object - ~/lib/HTML/Object/Event.pm | ||||||||||||
| 3 | ## Version v0.2.2 | ||||||||||||
| 4 | ## Copyright(c) 2023 DEGUEST Pte. Ltd. | ||||||||||||
| 5 | ## Author: Jacques Deguest <jack@deguest.jp> | ||||||||||||
| 6 | ## Created 2021/12/11 | ||||||||||||
| 7 | ## Modified 2026/07/04 | ||||||||||||
| 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::Event; | ||||||||||||
| 15 | BEGIN | ||||||||||||
| 16 | { | ||||||||||||
| 17 | 24 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 18 | 24 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 19 | 24 | ▶ | warnings::register_categories( 'HTML::Object' ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 20 | 24 | ▶ | use parent qw( Module::Generic ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 21 | 24 | ▶ | use vars qw( @EXPORT_OK %EXPORT_TAGS $VERSION ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 22 | 24 | ▶ | use Time::HiRes (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 23 | use constant { | ||||||||||||
| 24 | 24 | NONE => 0, | |||||||||||
| 25 | CAPTURING_PHASE => 1, | ||||||||||||
| 26 | AT_TARGET => 2, | ||||||||||||
| 27 | BUBBLING_PHASE => 3, | ||||||||||||
| 28 | |||||||||||||
| 29 | CANCEL_PROPAGATION => 1, | ||||||||||||
| 30 | CANCEL_IMMEDIATE_PROPAGATION => 2, | ||||||||||||
| 31 | |||||||||||||
| 32 | ABORT => 1, | ||||||||||||
| 33 | BLUR => 2, | ||||||||||||
| 34 | CLICK => 4, | ||||||||||||
| 35 | CHANGE => 16, | ||||||||||||
| 36 | DBLCLICK => 32, | ||||||||||||
| 37 | DRAGDDROP => 64, | ||||||||||||
| 38 | ERROR => 128, | ||||||||||||
| 39 | FOCUS => 256, | ||||||||||||
| 40 | KEYDOWN => 512, | ||||||||||||
| 41 | KEYPRESS => 1024, | ||||||||||||
| 42 | KEYUP => 2048, | ||||||||||||
| 43 | LOAD => 4096, | ||||||||||||
| 44 | MOUSEDOWN => 8192, | ||||||||||||
| 45 | MOUSEMOVE => 16384, | ||||||||||||
| 46 | MOUSEOUT => 32768, | ||||||||||||
| 47 | MOUSEOVER => 65536, | ||||||||||||
| 48 | MOUSEUP => 131072, | ||||||||||||
| 49 | MOVE => 262144, | ||||||||||||
| 50 | RESET => 524288, | ||||||||||||
| 51 | RESIZE => 1048576, | ||||||||||||
| 52 | SELECT => 2097152, | ||||||||||||
| 53 | SUBMIT => 4194304, | ||||||||||||
| 54 | UNLOAD => 8388608, | ||||||||||||
| 55 | 24 | ▶ | }; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 56 | 24 | our @EXPORT_OK = qw( | |||||||||||
| 57 | NONE CAPTURING_PHASE AT_TARGET BUBBLING_PHASE CANCEL_PROPAGATION CANCEL_IMMEDIATE_PROPAGATION | ||||||||||||
| 58 | ABORT BLUR CLICK CHANGE DBLCLICK DRAGDDROP ERROR FOCUS KEYDOWN KEYPRESS KEYUP LOAD | ||||||||||||
| 59 | MOUSEDOWN MOUSEMOVE MOUSEOUT MOUSEOVER MOUSEUP MOVE RESET RESIZE SELECT SUBMIT UNLOAD | ||||||||||||
| 60 | ); | ||||||||||||
| 61 | 24 | our %EXPORT_TAGS = ( | |||||||||||
| 62 | 'all' => [qw( | ||||||||||||
| 63 | NONE CAPTURING_PHASE AT_TARGET BUBBLING_PHASE | ||||||||||||
| 64 | CANCEL_PROPAGATION CANCEL_IMMEDIATE_PROPAGATION | ||||||||||||
| 65 | ABORT BLUR CLICK CHANGE DBLCLICK DRAGDDROP ERROR FOCUS KEYDOWN KEYPRESS KEYUP LOAD | ||||||||||||
| 66 | MOUSEDOWN MOUSEMOVE MOUSEOUT MOUSEOVER MOUSEUP MOVE RESET RESIZE SELECT SUBMIT UNLOAD | ||||||||||||
| 67 | )], | ||||||||||||
| 68 | 'events'=> [qw( | ||||||||||||
| 69 | ABORT BLUR CLICK CHANGE DBLCLICK DRAGDDROP ERROR FOCUS KEYDOWN KEYPRESS KEYUP LOAD | ||||||||||||
| 70 | MOUSEDOWN MOUSEMOVE MOUSEOUT MOUSEOVER MOUSEUP MOVE RESET RESIZE SELECT SUBMIT UNLOAD | ||||||||||||
| 71 | )], | ||||||||||||
| 72 | 'phase' => [qw( NONE CAPTURING_PHASE AT_TARGET BUBBLING_PHASE )] | ||||||||||||
| 73 | ); | ||||||||||||
| 74 | 24 | our $VERSION = 'v0.2.2'; | |||||||||||
| 75 | }; | ||||||||||||
| 76 | |||||||||||||
| 77 | 24 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 78 | 24 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 79 | |||||||||||||
| 80 | sub init | ||||||||||||
| 81 | { | ||||||||||||
| 82 | 198 | ▶ | my $self = shift( @_ ); | ||||||||||
|
Subroutine
init: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 83 | 198 | ▶ | my $type = shift( @_ ) || return( $self->error( "No event type was provided." ) ); | ||||||||||
|
Condition50%1/2
shift @_ || (return $self->error('No event type was provided.'))
MC/DC0%0/1
shift @_ || (return $self->error('No event type was provided.'))
shift @_
Truth table50%1/2
A
A= shift @_
| |||||||||||||
| 84 | 198 | $self->{bubbles} = 1; | |||||||||||
| 85 | 198 | $self->{cancelable} = 1; | |||||||||||
| 86 | # Property that, if set to true, will be checked to stop events handlers and propagation | ||||||||||||
| 87 | 198 | $self->{cancelled} = 0; | |||||||||||
| 88 | 198 | $self->{composed} = 0; | |||||||||||
| 89 | 198 | $self->{currentTarget} = undef; | |||||||||||
| 90 | 198 | $self->{defaultPrevented} = 0; | |||||||||||
| 91 | 198 | $self->{detail} = {}; | |||||||||||
| 92 | 198 | $self->{eventPhase} = NONE; | |||||||||||
| 93 | 198 | $self->{isTrusted} = 1; | |||||||||||
| 94 | # array of html element for which their event handler will be called | ||||||||||||
| 95 | 198 | $self->{path} = []; | |||||||||||
| 96 | 198 | $self->{target} = undef; | |||||||||||
| 97 | 198 | $self->{timeStamp} = Time::HiRes::time(); | |||||||||||
| 98 | 198 | $self->{type} = $type; | |||||||||||
| 99 | 198 | $self->{_init_strict_use_sub} = 1; | |||||||||||
| 100 | 198 | ▶ | $self->SUPER::init( @_ ) || return( $self->pass_error ); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 101 | # Our immediate caller is new() in Module::Generic, so we skip that | ||||||||||||
| 102 | 198 | my( $pack, $file, $line ) = caller(1); | |||||||||||
| 103 | 198 | my $sub = [caller(2)]->[3]; | |||||||||||
| 104 | 198 | $self->{package} = $pack; | |||||||||||
| 105 | 198 | $self->{file} = $file; | |||||||||||
| 106 | 198 | $self->{line} = $line; | |||||||||||
| 107 | 198 | $self->{subroutine} = $sub; | |||||||||||
| 108 | 198 | return( $self ); | |||||||||||
| 109 | } | ||||||||||||
| 110 | |||||||||||||
| 111 | # Note: Property | ||||||||||||
| 112 | 198 | ▶ | sub bubbles : lvalue { return( shift->_set_get_boolean( 'bubbles', @_ ) ); } | ||||||||||
|
Subroutine
bubbles: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 113 | |||||||||||||
| 114 | # Note: Property | ||||||||||||
| 115 | 210 | ▶ | sub cancelable : lvalue { return( shift->_set_get_boolean( 'cancelable', @_ ) ); } | ||||||||||
|
Subroutine
cancelable: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 116 | |||||||||||||
| 117 | # Note: Property | ||||||||||||
| 118 | 0 | ▶ | sub cancellable : lvalue { return( shift->_set_get_boolean( 'cancelable', @_ ) ); } | ||||||||||
|
Subroutine
cancellable: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 119 | |||||||||||||
| 120 | # Note: Property | ||||||||||||
| 121 | 0 | ▶ | sub canceled : lvalue { return( shift->_set_get_number( 'cancelled', @_ ) ); } | ||||||||||
|
Subroutine
canceled: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 122 | |||||||||||||
| 123 | # Note: Property | ||||||||||||
| 124 | 37 | ▶ | sub cancelled : lvalue { return( shift->_set_get_number( 'cancelled', @_ ) ); } | ||||||||||
|
Subroutine
cancelled: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 125 | |||||||||||||
| 126 | # Note: Property | ||||||||||||
| 127 | 1 | ▶ | sub composed : lvalue { return( shift->_set_get_boolean( 'composed', @_ ) ); } | ||||||||||
|
Subroutine
composed: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 128 | |||||||||||||
| 129 | sub composedPath | ||||||||||||
| 130 | { | ||||||||||||
| 131 | 3 | ▶ | my $self = shift( @_ ); | ||||||||||
|
Subroutine
composedPath: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||
| 132 | # already set | ||||||||||||
| 133 | 3 | ▶ | return( $self->path ) if( !$self->path->is_empty ); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 134 | 3 | my $target = $self->target; | |||||||||||
| 135 | 3 | ▶ | return( $self->error( "No target element is set yet!" ) ) if( !$target ); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 136 | 2 | my $original_target = $target; | |||||||||||
| 137 | 2 | my $path = $self->path; | |||||||||||
| 138 | 2 | ▶ | while( $target->parentNode ) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 139 | { | ||||||||||||
| 140 | 4 | $path->push( $target ); | |||||||||||
| 141 | 4 | $target = $target->parentNode; | |||||||||||
| 142 | } | ||||||||||||
| 143 | 2 | my $doc = $original_target->root; | |||||||||||
| 144 | 2 | ▶ | $path->push( $doc ) if( $doc ); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 145 | # Module::Generic::Array object | ||||||||||||
| 146 | 2 | return( $path ); | |||||||||||
| 147 | } | ||||||||||||
| 148 | |||||||||||||
| 149 | # Note: Property | ||||||||||||
| 150 | 25 | ▶ | sub currentTarget : lvalue { return( shift->_set_get_lvalue( 'currentTarget', @_ ) ); } | ||||||||||
|
Subroutine
currentTarget: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 151 | |||||||||||||
| 152 | # Note: Property | ||||||||||||
| 153 | 3 | ▶ | sub defaultPrevented : lvalue { return( shift->_set_get_boolean( 'defaultPrevented', @_ ) ); } | ||||||||||
|
Subroutine
defaultPrevented: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 154 | |||||||||||||
| 155 | # Note : Property from CustomEvent, but we add it here as a standard | ||||||||||||
| 156 | 210 | ▶ | sub detail : lvalue { return( shift->_set_get_hash_as_mix_object( 'detail', @_ ) ); } | ||||||||||
|
Subroutine
detail: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 157 | |||||||||||||
| 158 | sub dispatch | ||||||||||||
| 159 | { | ||||||||||||
| 160 | 0 | ▶ | my $self = shift( @_ ); | ||||||||||
|
Subroutine
dispatch: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||
| 161 | 0 | ▶ | my $elem = shift( @_ ) || return( $self->error({ | ||||||||||
|
Condition0%0/2
shift @_ || (return $self->error({'message', 'No element was provided to dispatch this event to.', 'class', 'HTML::Object::SyntaxError'}))
MC/DC0%0/1
shift @_ || (return $self->error({'message', 'No element was provided to dispatch this event to.', 'class', 'HTML::Object::SyntaxError'}))
shift @_
Truth table0%0/2
A
A= shift @_
| |||||||||||||
| 162 | message => "No element was provided to dispatch this event to.", | ||||||||||||
| 163 | class => 'HTML::Object::SyntaxError', | ||||||||||||
| 164 | }) ); | ||||||||||||
| 165 | 0 | ▶ | return( $self->error({ | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 166 | message => "Object provided is not a node object (HTML::Object::DOM::Node)", | ||||||||||||
| 167 | class => 'HTML::Object::SyntaxError', | ||||||||||||
| 168 | }) ) if( !$self->_is_a( $elem => 'HTML::Object::DOM::Node' ) ); | ||||||||||||
| 169 | 0 | return( $elem->dispatchEvent( $self ) ); | |||||||||||
| 170 | } | ||||||||||||
| 171 | |||||||||||||
| 172 | # Note: Property | ||||||||||||
| 173 | 15 | ▶ | sub eventPhase : lvalue { return( shift->_set_get_lvalue( 'eventPhase', @_ ) ); } | ||||||||||
|
Subroutine
eventPhase: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 174 | |||||||||||||
| 175 | 0 | ▶ | sub file { return( shift->_set_get_scalar( 'file', @_ ) ); } | ||||||||||
|
Subroutine
file: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 176 | |||||||||||||
| 177 | # Note: Property | ||||||||||||
| 178 | 1 | ▶ | sub isTrusted : lvalue { return( shift->_set_get_boolean( 'isTrusted', @_ ) ); } | ||||||||||
|
Subroutine
isTrusted: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 179 | |||||||||||||
| 180 | 0 | ▶ | sub line { return( shift->_set_get_scalar( 'line', @_ ) ); } | ||||||||||
|
Subroutine
line: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 181 | |||||||||||||
| 182 | 0 | ▶ | sub package { return( shift->_set_get_scalar( 'package', @_ ) ); } | ||||||||||
|
Subroutine
package: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 183 | |||||||||||||
| 184 | 5 | ▶ | sub path { return( shift->_set_get_array_as_object( 'path', @_ ) ); } | ||||||||||
|
Subroutine
path: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 185 | |||||||||||||
| 186 | # Note: preventDefault does nothing, except set defaultPrevented to true | ||||||||||||
| 187 | sub preventDefault | ||||||||||||
| 188 | { | ||||||||||||
| 189 | 1 | ▶ | my $self = shift( @_ ); | ||||||||||
|
Subroutine
preventDefault: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 190 | 1 | $self->defaultPrevented(1); | |||||||||||
| 191 | 1 | return( $self ); | |||||||||||
| 192 | } | ||||||||||||
| 193 | |||||||||||||
| 194 | sub setTimestamp | ||||||||||||
| 195 | { | ||||||||||||
| 196 | 0 | ▶ | my $self = shift( @_ ); | ||||||||||
|
Subroutine
setTimestamp: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||
| 197 | 0 | ▶ | my $now = scalar( @_ ) ? shift( @_ ) : Time::HiRes::time(); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 198 | 0 | ▶ | $self->_load_class( 'DateTime::Lite' ) || return( $self->pass_error ); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 199 | # try-catch | ||||||||||||
| 200 | 0 | local $@; | |||||||||||
| 201 | my $rv = eval | ||||||||||||
| 202 | 0 | { | |||||||||||
| 203 | 0 | my $dt; | |||||||||||
| 204 | 0 | ▶ | unless( $self->_is_a( $now => [qw( DateTime::Lite DateTime )] ) ) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 205 | { | ||||||||||||
| 206 | 0 | $dt = DateTime::Lite->from_epoch( epoch => $now ); | |||||||||||
| 207 | } | ||||||||||||
| 208 | 0 | ▶ | if( $self->_is_a( $now => 'DateTime' ) ) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 209 | { | ||||||||||||
| 210 | 0 | $dt = DateTime::Lite->from_object( object => $dt ); | |||||||||||
| 211 | } | ||||||||||||
| 212 | 0 | return( $self->_set_get_datetime( timeStamp => $dt ) ); | |||||||||||
| 213 | }; | ||||||||||||
| 214 | 0 | ▶ | if( $@ ) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 215 | { | ||||||||||||
| 216 | 0 | return( $self->error( "Error setting event timestamp: $@" ) ); | |||||||||||
| 217 | } | ||||||||||||
| 218 | 0 | return( $rv ); | |||||||||||
| 219 | } | ||||||||||||
| 220 | |||||||||||||
| 221 | sub stopImmediatePropagation | ||||||||||||
| 222 | { | ||||||||||||
| 223 | 1 | ▶ | my $self = shift( @_ ); | ||||||||||
|
Subroutine
stopImmediatePropagation: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 224 | 1 | $self->cancelled( CANCEL_IMMEDIATE_PROPAGATION ); | |||||||||||
| 225 | 1 | return( $self ); | |||||||||||
| 226 | } | ||||||||||||
| 227 | |||||||||||||
| 228 | sub stopPropagation | ||||||||||||
| 229 | { | ||||||||||||
| 230 | 1 | ▶ | my $self = shift( @_ ); | ||||||||||
|
Subroutine
stopPropagation: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 231 | 1 | $self->cancelled( CANCEL_PROPAGATION ); | |||||||||||
| 232 | 1 | return( $self ); | |||||||||||
| 233 | } | ||||||||||||
| 234 | |||||||||||||
| 235 | 0 | ▶ | sub subroutine { return( shift->_set_get_scalar( 'subroutine', @_ ) ); } | ||||||||||
|
Subroutine
subroutine: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 236 | |||||||||||||
| 237 | # Note: Property | ||||||||||||
| 238 | 203 | ▶ | sub target { return( shift->_set_get_object_without_init( 'target', 'HTML::Object::Element', @_ ) ); } | ||||||||||
|
Subroutine
target: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 239 | |||||||||||||
| 240 | # Note: Property | ||||||||||||
| 241 | 1 | ▶ | sub timeStamp : lvalue { return( shift->_set_get_datetime( 'timeStamp', @_ ) ); } | ||||||||||
|
Subroutine
timeStamp: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 242 | |||||||||||||
| 243 | # Note: Property | ||||||||||||
| 244 | 15 | ▶ | sub type : lvalue { return( shift->_set_get_lvalue( 'type', @_ ) ); } | ||||||||||
|
Subroutine
type: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 245 | |||||||||||||
| 246 | 24 | 1; | |||||||||||
| 247 | # NOTE: POD | ||||||||||||
| 248 | __END__ | ||||||||||||