blib/lib/HTML/Object/TokenList.pm untested

filter: Statementstmt 0.0% 0 / 125 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 17 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 31 Podpod 0.0% 0 / 25 totaltotal 0.0% 0 / 228 CC 16 SCAR 21.5
CC
16
Cov
0%
forEach
30.0
items
20.0
toggle
12.0
CRAP 8.6
1 ##----------------------------------------------------------------------------
2 ## HTML Object - ~/lib/HTML/Object/TokenList.pm
3 ## Version v0.2.0
4 ## Copyright(c) 2021 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2021/12/09
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::TokenList;
15 BEGIN
16 {
17 use strict;
18 use warnings;
19 warnings::register_categories( 'HTML::Object' );
20 use parent qw( Module::Generic );
21 use vars qw( $VERSION );
22 our $VERSION = 'v0.2.0';
23 };
24  
25 use strict;
26 use warnings;
27  
28 sub init
29 {
30 my $self = shift( @_ );
31 my $tokens = shift( @_ );
32 $self->{attribute} = undef;
33 $self->{items} = [];
34 $self->{element} = undef;
35 $self->{tokens} = undef;
36 if( defined( $tokens ) && CORE::length( $tokens ) )
37 {
38 $self->{items} = $self->_string2array( $tokens )->unique(1);
39 $self->{tokens} = $tokens;
40 }
41 $self->{_init_strict_use_sub} = 1;
42 $self->SUPER::init( @_ ) || return( $self->pass_error );
43 return( $self );
44 }
45  
46 sub add
47 {
48 my $self = shift( @_ );
49 my $a = $self->_string2array( @_ );
50 $self->items->push( $a->list );
51 $self->items->unique(1);
52 $self->_reset;
53 return( $self );
54 }
55  
56 sub as_string { return( shift->items->join( ' ' )->scalar ); }
57  
58 sub attribute { return( shift->_set_get_scalar_as_object( 'attribute', @_ ) ); }
59  
60 sub contains { return( shift->items->has( _trim( @_ ) ) ); }
61  
62 sub element { return( shift->_set_get_object_without_init( 'element', 'HTML::Object::Element', @_ ) ); }
63  
64 sub entries { return; }
65  
66 sub forEach
67 {
68 my $self = shift( @_ );
69 my $code = shift( @_ ) || return( $self->error( "No anonymous subroutine was provided." ) );
70 return( $self->error( "Callback provided is not an anonymous subroutine" ) ) if( ref( $code ) ne 'CODE' );
71 my $elem = $self->element;
72 # We do not sort on purpose. Change in the order will trigger a cache reset on the associated element
73 my $before = $self->items->join( ',' )->scalar if( $elem );
74 $self->items->foreach( $code );
75 if( $elem )
76 {
77 my $after = $self->items->join( ',' )->scalar;
78 $elem->_reset(1) if( $before ne $after );
79 }
80 return( $self );
81 }
82  
83 sub item { return( shift->items->index( shift( @_ ) ) ); }
84  
85 sub items
86 {
87 my $self = shift( @_ );
88 if( @_ )
89 {
90 my $ref = $self->_set_get_array_as_object( 'items', @_ );
91 $self->_reset;
92 return( $ref );
93 }
94 else
95 {
96 # Check if the value has changed on the element so we keep in sync
97 my $attr = $self->attribute;
98 my $elem = $self->element;
99 if( $attr && $elem )
100 {
101 my $elem_tokens = $elem->attr( $attr );
102 if( $elem_tokens eq $self->tokens )
103 {
104 return( $self->_set_get_array_as_object( 'items' ) );
105 }
106 # Element attribute value takes precedence over us
107 else
108 {
109 return( $self->{items} = $self->_string2array( $elem_tokens ) );
110 }
111 }
112 return( $self->_set_get_array_as_object( 'items' ) );
113 }
114 }
115  
116 sub keys { return; }
117  
118 # Property
119 sub length { return( shift->items->length ); }
120  
121 sub remove { return( shift->items->remove( _trim( @_ )->_reset ) ); }
122  
123 sub replace
124 {
125 my $self = shift( @_ );
126 my $ok = $self->items->replace( @_ ) ? $self->true : $self->false;
127 $self->items->unique(1);
128 # Reset the associated element cache, if any.
129 $self->_reset;
130 return( $ok );
131 }
132  
133 sub reset
134 {
135 my $self = shift( @_ );
136 $self->items->reset;
137 $self->tokens->reset;
138 $self->_reset;
139 return( $self );
140 }
141  
142 sub supports { return( shift->true ); }
143  
144 sub toggle
145 {
146 my $self = shift( @_ );
147 my $token = shift( @_ ) || return( $self->error( "No token was provided to toggle." ) );
148 $token = _trim( $token );
149 my $rv;
150 if( $self->items->has( $token ) )
151 {
152 $rv = $self->items->remove( $token ) ? $self->true : $self->false;
153 }
154 else
155 {
156 $self->items->push( $token );
157 $rv = $self->true;
158 }
159 $self->_reset;
160 return( $rv );
161 }
162  
163 sub tokens { return( shift->_set_get_scalar_as_object( 'tokens', @_ ) ); }
164  
165 sub update
166 {
167 my $self = shift( @_ );
168 if( scalar( @_ ) )
169 {
170 if( scalar( @_ ) == 1 && !defined( $_[0] ) )
171 {
172 $self->tokens->reset;
173 $self->items->reset;
174 }
175 else
176 {
177 my $items = $self->_string2array( @_ );
178 $self->tokens( $items->join( ' ' )->scalar );
179 $self->{items} = $items;
180 }
181 }
182 return( $self );
183 }
184  
185 # Property
186 sub value { return( shift->as_string ); }
187  
188 sub values { return; }
189  
190 sub _reset
191 {
192 my $self = shift( @_ );
193 my $elem = $self->element;
194 my $tokens = $self->as_string;
195 $self->tokens( $tokens );
196 return( $self ) if( !ref( $elem ) );
197 my $attr = $self->attribute || return( $self );
198 $elem->attr( $attr => $tokens );
199 $elem->reset(1);
200 return( $self );
201 }
202  
203 sub _string2array
204 {
205 my $self = shift( @_ );
206 my @tokens = ();
207 for( @_ )
208 {
209 if( $self->_is_array( $_ ) )
210 {
211 push( @tokens, @$_ );
212 }
213 # space-delimited tokens
214 else
215 {
216 push( @tokens, split( /[[:blank:]\h]+/, _trim( @_ ) ) );
217 }
218 }
219 return( $self->new_array( \@tokens )->unique(1) );
220 }
221  
222 sub _trim
223 {
224 my $str = shift( @_ );
225 $str =~ s/^[[:blank:]\h]+|[[:blank:]\h]+$//g;
226 $str =~ s/[[:blank:]\h]+/ /g;
227 return( $str );
228 }
229  
230 1;
231 # NOTE: POD
232 __END__
233  
234 =encoding utf-8
235  
236 =head1 NAME
237  
238 HTML::Object::TokenList - HTML Object Token List Class
239  
240 =head1 SYNOPSIS
241  
242 use HTML::Object::TokenList;
243 # standalone, i.e. without connection to an element
244 my $list = HTML::Object::TokenList->new( 'some class to edit' ) ||
245 die( HTML::Object::TokenList->error, "\n" );
246  
247 Or
248  
249 use HTML::Object::Element;
250 my $e = HTML::Object::Element->new( tag => 'div' );
251 my $list = $e->classList;
252  
253 $list->add( 'another-class' );
254 $list->remove( 'edit' );
255 $list->length;
256 $list->value;
257 $list->as_string;
258 $list->contains( 'some' );
259 $list->forEach(sub
260 {
261 my $c = shift( @_ ); # also available as $_
262 # do something
263 });
264 $list->item(3); # 'edit'
265 $list->replace( 'to' = 'other' );
266 $list->toggle( 'visible' ); # activate it
267 $list->toggle( 'visible' ); # now remove it
268  
269 =head1 VERSION
270  
271 v0.2.0
272  
273 =head1 DESCRIPTION
274  
275 The C<TokenList> interface represents a set of space-separated tokens. Such a set is returned by L<HTML::Object::DOM::Element/classList> or L<HTML::Object::DOM::AnchorElement/relList>.
276  
277 A C<TokenList> is indexed beginning with 0 as with perl array. C<TokenList> is always case-sensitive.
278  
279 This module can be used independently or be instantiated by an L<element|HTML::Object::DOM::Element>, and in which case, any modification made will be reflected in the associated element's attribute.
280  
281 =head1 PROPERTIES
282  
283 =head2 length
284  
285 Read-only. This returns an L<integer|Module::Generic::Number> representing the number of objects stored in the object.
286  
287 =head2 value
288  
289 A stringifier property that returns the value of the list as a string. See also L</as_string>
290  
291 =head1 METHODS
292  
293 =head2 add
294  
295 Adds the specified tokens to the list. Returns the current object for chaining.
296  
297 The tokens can be provided either as a list of string, an array reference of strings, or a space-delimited string of tokens.
298  
299 =head2 as_string
300  
301 A stringifier property that returns the value of the list as a string. See also L</value>
302  
303 =head2 attribute
304  
305 Set or get the element attribute to which C<TokenList> is bound. For example a C<class> attribute or a C<rel> attribute
306  
307 This is optional if you want to use this class independently from any element, or if you want to set the element later.
308  
309 =head2 contains
310  
311 Returns true if the list contains the given token, otherwise false.
312  
313 =head2 element
314  
315 Set or get the L<element|HTML::Object::Element>
316  
317 This is optional if you want to use this class independently from any element, or if you want to set the element later.
318  
319 =head2 entries
320  
321 This does nothing.
322  
323 Normally, under JavaScript, this would return an iterator, allowing you to go through all key/value pairs contained in this object.
324  
325 =head2 forEach
326  
327 Executes a provided callback function once for each DOMTokenList element.
328  
329 =head2 item
330  
331 Returns the item in the list by its index, or undefined if the index is greater than or equal to the list's length.
332  
333 =head2 items
334  
335 Sets or gets the list of token items. It returns the L<array object|Module::Generic::Array> containing all the tokens.
336  
337 =head2 keys
338  
339 This does nothing.
340  
341 Normally, under JavaScript, this would return an iterator, allowing you to go through all keys of the key/value pairs contained in this object.
342  
343 =head2 remove
344  
345 Removes the specified tokens from the list.
346  
347 =head2 replace
348  
349 Replaces the token with another one.
350  
351 It returns a boolean value, which is true if the old entry was successfully replaced, or false if not.
352  
353 See the L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/DOMTokenList/replace> for more information.
354  
355 =head2 reset
356  
357 Reset the tokens list to an empty list and, of course, propagate that change to the associated element's attribute, if any was set.
358  
359 Returns the current object.
360  
361 =head2 supports
362  
363 Returns true if the given token is in the associated attribute's supported tokens.
364  
365 For the purpose of the perl environment, this actually always returns true.
366  
367 =head2 toggle
368  
369 Removes the token from the list if it exists, or adds it to the list if it does not. Returns a boolean indicating whether the token is in the list after the operation.
370  
371 =head2 tokens
372  
373 Sets or get the L<array object|Module::Generic::Array> of tokens.
374  
375 =head2 update
376  
377 This method is called by an internal callback in L<HTML::Object::Element> when the value of an registered attribute has been changed. It does not propagate the change back to the element since it is triggered by the element itself.
378  
379 If C<undef> is provided as its sole argument, this will empty the tokens list, otherwise it will set the new tokens list with a space-delimited string of tokens, a list or array reference of tokens.
380  
381 Returns the current object.
382  
383 =head2 values
384  
385 This does nothing.
386  
387 Normally, under JavaScript, this would return an iterator, allowing you to go through all values of the key/value pairs contained in this object.
388  
389 =head1 EXAMPLES
390  
391 In the following simple example, we retrieve the list of classes set on a <p> element as a L<HTML::Object::TokenList> using L<HTML::Object::Element/classList>, add a class using L<HTML::Object::TokenList/add>, and then update the C<textContent> of the <p> to equal the L<HTML::Object::TokenList>.
392  
393 <p class="a b c"></p>
394  
395 my $para = $doc->querySelector("p");
396 my $classes = $para->classList;
397 $para->classList->add("d");
398 $para->textContent = qq{paragraph classList is "${classes}"};
399  
400 would yield:
401  
402 paragraph classList is "a b c d"
403  
404 =head1 WHITESPACE AND DUPLICATES
405  
406 Methods that modify the TokenList (such as L<HTML::Object::TokenList/add>) automatically trim any excess whitespace and remove duplicate values from the list. For example:
407  
408 <span class=" d d e f"></span>
409  
410 my $span = $doc->querySelector("span");
411 my $classes = $span->classList;
412 $span->classList->add("x");
413 $span->textContent = qq{span classList is "${classes}"};
414  
415 would yield:
416  
417 span classList is "d e f x"
418  
419 =head1 AUTHOR
420  
421 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
422  
423 =head1 SEE ALSO
424  
425 L<HTML::Object::Element>
426  
427 L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/DOMTokenList>
428  
429 =head1 COPYRIGHT & LICENSE
430  
431 Copyright(c) 2021 DEGUEST Pte. Ltd.
432  
433 All rights reserved
434  
435 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
436  
437 =cut