blib/lib/HTML/Object/DOM/Element/Input.pm untested

filter: Statementstmt 0.0% 0 / 179 Branchbran 0.0% 0 / 96 Conditioncond 0.0% 0 / 98 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 403 CC 49 SCAR 74.3
CC
49
Cov
0%
_set_up_down
2352.0
init
6.0
BEGIN
2.0
CRAP 1685.7
1 ##----------------------------------------------------------------------------
2 ## HTML Object - ~/lib/HTML/Object/DOM/Element/Input.pm
3 ## Version v0.2.2
4 ## Copyright(c) 2023 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2021/12/23
7 ## Modified 2023/11/06
8 ## All rights reserved.
9 ##
10 ##
11 ## This program is free software; you can redistribute it and/or modify it
12 ## under the same terms as Perl itself.
13 ##----------------------------------------------------------------------------
14 package HTML::Object::DOM::Element::Input;
15 BEGIN
16 {
17 use strict;
18 use warnings;
19 warnings::register_categories( 'HTML::Object' );
20 use parent qw( HTML::Object::DOM::Element );
21 use vars qw( $VERSION );
22 use HTML::Object::DOM::Element::Shared qw( :input );
23 use POSIX ();
24 our $VERSION = 'v0.2.2';
25 };
26  
27 use strict;
28 use warnings;
29  
30 sub init
31 {
32 my $self = shift( @_ );
33 $self->{_init_strict_use_sub} = 1;
34 $self->SUPER::init( @_ ) || return( $self->pass_error );
35 $self->{tag} = 'input' if( !CORE::length( "$self->{tag}" ) );
36 return( $self );
37 }
38  
39 # Note: property accept is inherited
40  
41 # Note: property accessKey is inherited
42  
43 # Note: deprecated property align is inherited
44  
45 # Note: property allowdirs is inherited
46  
47 # Note: property alt is inherited
48  
49 # Note: property autocapitalize is inherited
50  
51 # Note: property autocomplete is inherited
52  
53 # Note: property autofocus is inherited
54  
55 # Note: property checked is inherited
56  
57 # Note: property defaultChecked is inherited
58  
59 # Note: property defaultValue is inherited
60  
61 # Note: property dirName is inherited
62  
63 # Note: property disabled is inherited
64  
65 # Note: property files is inherited
66  
67 # Note: property form read-only is inherited
68  
69 # Note: property formAction is inherited
70  
71 # Note: property formEnctype is inherited
72  
73 # Note: property formMethod is inherited
74  
75 # Note: property formNoValidate is inherited
76  
77 # Note: property formTarget is inherited
78  
79 # Note: property height is inherited
80  
81 # Note: property indeterminate is inherited
82  
83 # Note: property inputmode is inherited
84  
85 # Note: property labels read-only is inherited
86  
87 # Note: property list read-only is inherited
88  
89 # Note: property max is inherited
90  
91 # Note: property maxLength is inherited
92  
93 # Note: property min is inherited
94  
95 # Note: property minLength is inherited
96  
97 sub mozGetFileNameArray { return; }
98  
99 sub mozSetFileArray { return; }
100  
101 # Note: property multiple is inherited
102  
103 # Note: property name is inherited
104  
105 sub oninput : lvalue { return( shift->on( 'input', @_ ) ); }
106  
107 sub oninvalid : lvalue { return( shift->on( 'invalid', @_ ) ); }
108  
109 sub onsearch : lvalue { return( shift->on( 'search', @_ ) ); }
110  
111 sub onselectionchange : lvalue { return( shift->on( 'selectionchange', @_ ) ); }
112  
113 # Note: property pattern is inherited
114  
115 # Note: property placeholder is inherited
116  
117 # Note: property readOnly is inherited
118  
119 # Note: property required is inherited
120  
121 # Note: property selectionDirection is inherited
122  
123 # Note: property selectionEnd is inherited
124  
125 # Note: property selectionStart is inherited
126  
127 # Note: property size is inherited
128  
129 # Note: property src is inherited
130  
131 # Note: property step is inherited
132  
133 sub stepDown { return( shift->_set_up_down( { direction => 'down' }, @_ ) ); }
134  
135 sub stepUp { return( shift->_set_up_down( { direction => 'up' }, @_ ) ); }
136  
137 # Note: property type is inherited
138  
139 # Note: deprecated property useMap is inherited
140  
141 # Note: property validationMessage read-only is inherited
142  
143 # Note: property validity read-only is inherited
144  
145 # Note: property value is inherited
146  
147 # Note: property valueAsDate is inherited
148  
149 # Note: property valueAsNumber is inherited
150  
151 # Note: property webkitEntries is inherited
152  
153 # Note: property webkitdirectory is inherited
154  
155 # Note: property width is inherited
156  
157 # Note: property willValidate read-only is inherited
158  
159 # date 1 (day) <input type="date" min="2019-12-25" step="1" />
160 # month 1 (month) <input type="month" min="2019-12" step="12" />
161 # week 1 (week) <input type="week" min="2019-W23" step="2" />
162 # time 60 (seconds) <input type="time" min="09:00" step="900" />
163 # datetime-local 1 (day) <input type="datetime-local" min="2019-12-25T19:30" step="7" />
164 # number 1 <input type="number" min="0" step="0.1" max="10" />
165 # range 1 <input type="range" min="0" step="2" max="10" />
166 #
167 # HTML::Object::InvalidStateError
168 sub _set_up_down
169 {
170 my $self = shift( @_ );
171 my $opts = shift( @_ );
172 my $incr = shift( @_ );
173 $incr = 1 if( !defined( $incr ) || !CORE::length( "$incr" ) );
174 return( $self->error( "First argument must be an hash reference of parameters." ) ) if( ref( $opts ) ne 'HASH' );
175 return( $self->error({
176 message => "Incremental value provided ($incr) is not a valid number.",
177 class => 'HTML::Object::SyntaxError',
178 }) ) if( !$self->_is_number( $incr ) );
179 # "If the value is a float, the value will increment as if Math.floor(stepDecrement) was passed. If the value is negative, the value will be incremented instead of decremented. "
180 # <https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/stepDown>
181 $incr = POSIX::floor( "$incr" );
182 my $dir = $opts->{direction} // 'down';
183 my $val = $self->value;
184 $self->_load_class( 'DateTime::Lite' ) || return( $self->pass_error );
185 $self->_load_class( 'DateTime::Format::Lite' ) || return( $self->pass_error );
186 $self->_load_class( 'DateTime::Lite::Duration' ) || return( $self->pass_error );
187 my $type2unit =
188 {
189 date => { unit => 'day', pattern => '%Y-%m-%d', default => '1970-01-01', step_re => qr/^\d+$/ },
190 month => { unit => 'month', pattern => '%Y-%m', default => '1970-01', step_re => qr/^\d+$/ },
191 week => { unit => 'week', pattern => '%Y-W%W', default => '1970-W1', step_re => qr/^\d+$/ },
192 'time' => { unit => 'second', pattern => ['%H:%M:%S','%H:%M'], default => '00:00:00', step_re => qr/^\d+$/, dt_format => '%02d:%02d', format => '%H:%M:%S' },
193 'datetime-local' => { unit => 'second', pattern => ['%Y-%m-%dT%H:%M:%S','%Y-%m-%dT%H:%M','%Y-%m-%dT%H'], default => '1970-01-01T00:00', step_re => qr/^\d+$/, format => '%Y-%m-%dT%H:%M:%S' },
194 number => { unit => 1, default => 0, step_re => qr/^\d+(?:\.\d+)?$/ },
195 range => { unit => 1, default => 0, step_re => qr/^\d+(?:\.\d+)?$/ },
196 };
197 my $type = $self->attr( 'type' ) || return( $self->error({
198 message => "This input type has no type attribute.",
199 class => 'HTML::Object::SyntaxError',
200 }) );
201 my $def = $type2unit->{ $type } || return( $self->error({
202 message => "This input type \"$type\" is unsupported for " . ( $dir eq 'up' ? 'stepUp' : 'stepDown' ),
203 class => 'HTML::Object::InvalidStateError',
204 }) );
205 my $min = $self->attr( 'min' );
206 my $max = $self->attr( 'max' );
207 my $step = $self->attr( 'step' );
208 $step = 1 if( !defined( $step ) );
209 $step = $step + 0;
210 $step = 1 if( !CORE::length( $step ) || lc( $step ) eq 'any' || $step !~ /$def->{step_re}/ );
211 return( $self->error({
212 message => "This input step value provided '" . ( $step // 'undef' ) . "' (" . $self->_str_val( $step // 'undef' ) . ") is not a proper number.",
213 class => 'HTML::Object::SyntaxError',
214 }) ) if( !$self->_is_number( $step ) );
215  
216 my $parse;
217 $parse = sub
218 {
219 my $this = shift( @_ );
220 return( $this ) if( $type eq 'week' || $type eq 'number' || $type eq 'range' );
221 my( $fmt, $dt );
222 if( ref( $def->{pattern} ) eq 'ARRAY' )
223 {
224 foreach my $pat ( @{$def->{pattern}} )
225 {
226 $fmt = DateTime::Format::Lite->new( pattern => $pat );
227 $dt = $fmt->parse_datetime( "$this" );
228 last if( $dt );
229 }
230 }
231 else
232 {
233 $fmt = DateTime::Format::Lite->new( pattern => $def->{pattern} );
234 $dt = $fmt->parse_datetime( "$this" );
235 }
236 if( !defined( $dt ) )
237 {
238 return( $self->error({
239 message => "Unable to parse $type value \"$this\"': " . $fmt->errmsg,
240 class => 'HTML::Object::InvalidStateError',
241 }) );
242 }
243  
244 if( $def->{format} )
245 {
246 $dt->set_formatter( DateTime::Format::Lite->new( pattern => $def->{format} ) );
247 }
248 else
249 {
250 $dt->set_formatter( $fmt );
251 }
252 return( $dt );
253 };
254  
255 $max //= '';
256 if( !defined( $val ) || !CORE::length( "$val" ) )
257 {
258 # If min is provided and we are told to go up, min is our starting value and we return immediately.
259 # However, the reverse is not true, i.e. if we go down and max is defined, we do not start at max value. We would start at 00:00
260 if( $dir eq 'up' && defined( $min ) && CORE::length( "$min" ) )
261 {
262 if( exists( $type2unit->{ $type } ) )
263 {
264 my $this = $parse->( $min );
265 # return( $self->error({
266 # message => "Unable to parse $type minimum value \"$min\"': " . $fmt->errmsg,
267 # class => 'HTML::Object::InvalidStateError',
268 # }) ) if( !defined( $this ) );
269 return( $self->pass_error ) if( !defined( $this ) );
270 $min = $this;
271 }
272 # $self->value = $min;
273 $self->value( $min );
274 return( $self );
275 }
276 elsif( $dir eq 'down' && defined( $max ) && CORE::length( "$max" ) )
277 {
278 if( exists( $type2unit->{ $type } ) )
279 {
280 my $this = $parse->( $max );
281 # return( $self->error({
282 # message => "Unable to parse $type maximum value \"$max\"': " . $fmt->errmsg,
283 # class => 'HTML::Object::InvalidStateError',
284 # }) ) if( !defined( $this ) );
285 return( $self->pass_error ) if( !defined( $this ) );
286 $max = $this;
287 }
288 # $self->value = $max;
289 $self->value( $max );
290 return( $self );
291 }
292 else
293 {
294 $val = $def->{default};
295 }
296 }
297  
298 # try-catch
299 my $rv = eval
300 {
301 my $new;
302 if( $type eq 'number' || $type eq 'range' )
303 {
304 $new = ( $dir eq 'up' )
305 ? ( $val + ( $step * $incr ) )
306 : ( $val - ( $step * $incr ) );
307 if( ( defined( $max ) && CORE::length( "$max" ) && $dir eq 'up' && $new > $max ) ||
308 ( defined( $min ) && CORE::length( "$min" ) && $dir eq 'down' && $new < $min ) )
309 {
310 return( $self );
311 }
312 }
313 # We manage it ourself, i.e. without using DateTime::Format::Lite, beecause
314 # the parser does not seem capable of recognising format like 2021-W23 even with
315 # the proper pattern
316 elsif( $type eq 'week' )
317 {
318 if( $val =~ /^(\d{1,})-W(\d{1,2})$/ )
319 {
320 my( $y, $w ) = ( $1, $2 );
321 return( $self->error({
322 message => "Week number found \"$w\" is higher than the maximum possible 52",
323 class => 'HTML::Object::InvalidStateError',
324 }) ) if( int( $w ) > 52 );
325 return( $self->error({
326 message => "Week number found \"$w\" is lower than the minimum possible 1",
327 class => 'HTML::Object::InvalidStateError',
328 }) ) if( int( $w ) < 1 );
329 my $dt = DateTime::Lite->from_day_of_year( year => $y, day_of_year => ( $w * 7 ) );
330 my $fmt = DateTime::Format::Lite->new( pattern => $def->{pattern} );
331 $dt->set_formatter( $fmt );
332  
333 if( $dir eq 'up' )
334 {
335 $dt->add( weeks => ( $step * $incr ) );
336 }
337 else
338 {
339 $dt->subtract( weeks => ( $step * $incr ) );
340 }
341  
342 if( $dir eq 'down' && defined( $min ) && CORE::length( "$min" ) )
343 {
344 my( $min_year, $min_week );
345 if( $min =~ /^(\d{1,})-W(\d{1,2})$/ )
346 {
347 ( $min_year, $min_week ) = ( $1, $2 );
348 return( $self->error({
349 message => "Minimum week number found \"$min_week\" is higher than the maximum possible 52",
350 class => 'HTML::Object::InvalidStateError',
351 }) ) if( int( $min_week ) > 52 );
352 return( $self->error({
353 message => "Minimum week number found \"$min_week\" is lower than the minimum possible 1",
354 class => 'HTML::Object::InvalidStateError',
355 }) ) if( int( $min_week ) < 1 );
356 my $dt_min = DateTime::Lite->from_day_of_year( year => $min_year, day_of_year => ( $min_week * 7 ) );
357 $dt_min->set_formatter( $fmt );
358 if( $dt < $dt_min )
359 {
360 # Silently refuse to comply, as browsers do
361 return( $self );
362 }
363 }
364 else
365 {
366 return( $self->error({
367 message => "Unable to parse $type minimum value \"$min\"': unsupported format. It should be 2021-W20 for example.",
368 class => 'HTML::Object::InvalidStateError',
369 }) );
370 }
371 }
372 elsif( $dir eq 'up' && defined( $max ) && CORE::length( "$max" ) )
373 {
374 my( $max_year, $max_week );
375 if( $max =~ /^(\d{1,})-W(\d{1,2})$/ )
376 {
377 ( $max_year, $max_week ) = ( $1, $2 );
378 return( $self->error({
379 message => "Maximum week number found \"$max_week\" is higher than the maximum possible 52",
380 class => 'HTML::Object::InvalidStateError',
381 }) ) if( int( $max_week ) > 52 );
382 return( $self->error({
383 message => "Maximum week number found \"$max_week\" is lower than the minimum possible 1",
384 class => 'HTML::Object::InvalidStateError',
385 }) ) if( int( $max_week ) < 1 );
386 my $dt_max = DateTime::Lite->from_day_of_year( year => $max_year, day_of_year => ( $max_week * 7 ) );
387 $dt_max->set_formatter( $fmt );
388 if( $dt > $dt_max )
389 {
390 # Silently refuse to comply, as browsers do
391 return( $self );
392 }
393 }
394 else
395 {
396 return( $self->error({
397 message => "Unable to parse $type maximum value \"$max\"': unsupported format. It should be 2021-W20 for example.",
398 class => 'HTML::Object::InvalidStateError',
399 }) );
400 }
401 }
402 $new = "$dt";
403 }
404 else
405 {
406 return( $self->error({
407 message => "Unable to parse $type value \"$val\"': unsupported format. It should be 2021-W20 for example.",
408 class => 'HTML::Object::InvalidStateError',
409 }) );
410 }
411 }
412 else
413 {
414 my $dt = $parse->( $val );
415 return( $self->pass_error ) if( !defined( $dt ) );
416 # We can pass negative number to duration
417 my $interval = DateTime::Lite::Duration->new( "$def->{unit}s" => ( $step * $incr ) );
418 if( $dir eq 'up' )
419 {
420 $dt->add_duration( $interval );
421 }
422 else
423 {
424 $dt->subtract_duration( $interval );
425 }
426  
427  
428 if( $dir eq 'down' && defined( $min ) && CORE::length( "$min" ) )
429 {
430 my $dt_min = $parse->( "$min" );
431 if( !defined( $dt_min ) )
432 {
433 # return( $self->error({
434 # message => "Unable to parse $type minimum value \"$min\"': " . $fmt->errmsg,
435 # class => 'HTML::Object::InvalidStateError',
436 # }) );
437 return( $self->pass_error );
438 }
439 # $dt_min->set_formatter( $fmt );
440 if( $dt < $dt_min )
441 {
442 # Silently refuse to comply, as browsers do
443 return( $self );
444 }
445 }
446 elsif( $dir eq 'up' && defined( $max ) && CORE::length( "$max" ) )
447 {
448 my $dt_max = $parse->( "$max" );
449 if( !defined( $dt_max ) )
450 {
451 # return( $self->error({
452 # message => "Unable to parse $type maximum value \"$max\"': " . $fmt->errmsg,
453 # class => 'HTML::Object::InvalidStateError',
454 # }) );
455 return( $self->pass_error );
456 }
457 # $dt_max->set_formatter( $fmt );
458 if( $dt > $dt_max )
459 {
460 # Silently refuse to comply, as browsers do
461 return( $self );
462 }
463 }
464 $new = "$dt";
465 }
466 # $self->value = $new;
467 $self->value( $new );
468 return( $self );
469 };
470 if( $@ )
471 {
472 return( $self->error({
473 message => "Error " . ( $opts->{direction} ? 'increasing' : 'decreasing' ) . " the input value: $@",
474 class => 'HTML::Object::InvalidStateError',
475 }) );
476 }
477 return( $rv );
478 }
479  
480 1;
481 # NOTE: POD
482 __END__