lib/HTML/Object/TokenList.pm

filter: Statementstmt 62.9% 78 / 124 Branchbran 28.1% 9 / 32 Conditioncond 26.6% 4 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 61.2% 19 / 31 Podpod 100% 21 / 21 totaltotal 56.4% 131 / 232 CC 24 SCAR 21.6
CC
24
Cov
53%
forEach
42.0
toggle
20.0
items
7.3
CRAP 8.7
« lib/HTML/Object/XPath/Function.pm ↑ Summary blib/lib/HTML/Object/TokenList.pm »
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 2 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶ warnings::register_categories( 'HTML::Object' );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶ use parent qw( Module::Generic );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶ use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 our $VERSION = 'v0.2.0';
23 };
24  
25 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 sub init
29 {
30 1 ▶ my $self = shift( @_ );
Subroutine init: called CC=4 SCAR=14.2
Pod documented
31 1 my $tokens = shift( @_ );
32 1 $self->{attribute} = undef;
33 1 $self->{items} = [];
34 1 $self->{element} = undef;
35 1 $self->{tokens} = undef;
36 1 ▶ if( defined( $tokens ) && CORE::length( $tokens ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tokens and CORE::length $tokens) 1 0 1
Condition33%1/3
defined $tokens and CORE::length $tokens
!ll&&!rl&&r
001
MC/DC0%0/2
defined $tokens and CORE::length $tokens
defined $tokens CORE::length $tokens
Truth table33%1/3
A and B
A= defined $tokensB= CORE::length $tokens
ABresult
0X0
100
111
37 {
38 1 $self->{items} = $self->_string2array( $tokens )->unique(1);
39 1 $self->{tokens} = $tokens;
40 }
41 1 $self->{_init_strict_use_sub} = 1;
42 1 ▶ $self->SUPER::init( @_ ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init(@_) or return $self->pass_error 0 1 1
43 1 return( $self );
44 }
45  
46 sub add
47 {
48 1 ▶ my $self = shift( @_ );
Subroutine add: called CC=1 SCAR=0.0
Pod documented
49 1 my $a = $self->_string2array( @_ );
50 1 $self->items->push( $a->list );
51 1 $self->items->unique(1);
52 1 $self->_reset;
53 1 return( $self );
54 }
55  
56 1 ▶sub as_string { return( shift->items->join( ' ' )->scalar ); }
Subroutine as_string: called CC=1 SCAR=0.0
Pod documented
57  
58 7 ▶sub attribute { return( shift->_set_get_scalar_as_object( 'attribute', @_ ) ); }
Subroutine attribute: called CC=1 SCAR=0.0
Pod documented
59  
60 2 ▶sub contains { return( shift->items->has( _trim( @_ ) ) ); }
Subroutine contains: called CC=1 SCAR=0.0
Pod documented
61  
62 7 ▶sub element { return( shift->_set_get_object_without_init( 'element', 'HTML::Object::Element', @_ ) ); }
Subroutine element: called CC=1 SCAR=0.0
Pod documented
63  
64 0 ▶sub entries { return; }
Subroutine entries: not called CC=1 SCAR=6.9
Pod documented
65  
66 sub forEach
67 {
68 0 ▶ my $self = shift( @_ );
Subroutine forEach: not called CC=6 SCAR=37.4
Pod documented
69 0 ▶ my $code = shift( @_ ) || return( $self->error( "No anonymous subroutine was provided." ) );
Condition0%0/2
shift @_ || (return $self->error('No anonymous subroutine was provided.'))
l!l
00
MC/DC0%0/1
shift @_ || (return $self->error('No anonymous subroutine was provided.'))
shift @_
Truth table0%0/2
A
A= shift @_
Aresult
11
00
70 0 ▶ return( $self->error( "Callback provided is not an anonymous subroutine" ) ) if( ref( $code ) ne 'CODE' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $code ne "CODE" 0 0 0
71 0 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 0 ▶ my $before = $self->items->join( ',' )->scalar if( $elem );
Branch: 1 branch
BranchTrueFalseTotal
if $elem 0 0 0
74 0 $self->items->foreach( $code );
75 0 ▶ if( $elem )
Branch: 1 branch
BranchTrueFalseTotal
if ($elem) 0 0 0
76 {
77 0 my $after = $self->items->join( ',' )->scalar;
78 0 ▶ $elem->_reset(1) if( $before ne $after );
Branch: 1 branch
BranchTrueFalseTotal
if $before ne $after 0 0 0
79 }
80 0 return( $self );
81 }
82  
83 0 ▶sub item { return( shift->items->index( shift( @_ ) ) ); }
Subroutine item: not called CC=1 SCAR=6.9
Pod documented
84  
85 sub items
86 {
87 5 ▶ my $self = shift( @_ );
Subroutine items: called CC=5 SCAR=19.9
Pod documented
88 5 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 5 5
89 {
90 0 my $ref = $self->_set_get_array_as_object( 'items', @_ );
91 0 $self->_reset;
92 0 return( $ref );
93 }
94 else
95 {
96 # Check if the value has changed on the element so we keep in sync
97 5 my $attr = $self->attribute;
98 5 my $elem = $self->element;
99 5 ▶ if( $attr && $elem )
Branch: 1 branch
BranchTrueFalseTotal
if ($attr and $elem) 5 0 5
Condition33%1/3
$attr and $elem
!ll&&!rl&&r
005
MC/DC0%0/2
$attr and $elem
$attr $elem
Truth table33%1/3
A and B
A= $attrB= $elem
ABresult
0X0
100
111
100 {
101 5 my $elem_tokens = $elem->attr( $attr );
102 5 ▶ if( $elem_tokens eq $self->tokens )
Branch: 1 branch
BranchTrueFalseTotal
if ($elem_tokens eq $self->tokens) { } 5 0 5
103 {
104 5 return( $self->_set_get_array_as_object( 'items' ) );
105 }
106 # Element attribute value takes precedence over us
107 else
108 {
109 0 return( $self->{items} = $self->_string2array( $elem_tokens ) );
110 }
111 }
112 0 return( $self->_set_get_array_as_object( 'items' ) );
113 }
114 }
115  
116 0 ▶sub keys { return; }
Subroutine keys: not called CC=1 SCAR=6.9
Pod documented
117  
118 # Property
119 0 ▶sub length { return( shift->items->length ); }
Subroutine length: not called CC=1 SCAR=6.9
Pod documented
120  
121 0 ▶sub remove { return( shift->items->remove( _trim( @_ )->_reset ) ); }
Subroutine remove: not called CC=1 SCAR=6.9
Pod documented
122  
123 sub replace
124 {
125 0 ▶ my $self = shift( @_ );
Subroutine replace: not called CC=2 SCAR=17.9
Pod documented
126 0 ▶ my $ok = $self->items->replace( @_ ) ? $self->true : $self->false;
Branch: 1 branch
BranchTrueFalseTotal
$self->items->replace(@_) ? : 0 0 0
127 0 $self->items->unique(1);
128 # Reset the associated element cache, if any.
129 0 $self->_reset;
130 0 return( $ok );
131 }
132  
133 sub reset
134 {
135 0 ▶ my $self = shift( @_ );
Subroutine reset: not called CC=1 SCAR=6.9
Pod documented
136 0 $self->items->reset;
137 0 $self->tokens->reset;
138 0 $self->_reset;
139 0 return( $self );
140 }
141  
142 0 ▶sub supports { return( shift->true ); }
Subroutine supports: not called CC=1 SCAR=6.9
Pod documented
143  
144 sub toggle
145 {
146 0 ▶ my $self = shift( @_ );
Subroutine toggle: not called CC=4 SCAR=30.0
Pod documented
147 0 ▶ my $token = shift( @_ ) || return( $self->error( "No token was provided to toggle." ) );
Condition0%0/2
shift @_ || (return $self->error('No token was provided to toggle.'))
l!l
00
MC/DC0%0/1
shift @_ || (return $self->error('No token was provided to toggle.'))
shift @_
Truth table0%0/2
A
A= shift @_
Aresult
11
00
148 0 $token = _trim( $token );
149 0 my $rv;
150 0 ▶ if( $self->items->has( $token ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->items->has($token)) { } 0 0 0
151 {
152 0 ▶ $rv = $self->items->remove( $token ) ? $self->true : $self->false;
Branch: 1 branch
BranchTrueFalseTotal
$self->items->remove($token) ? : 0 0 0
153 }
154 else
155 {
156 0 $self->items->push( $token );
157 0 $rv = $self->true;
158 }
159 0 $self->_reset;
160 0 return( $rv );
161 }
162  
163 8 ▶sub tokens { return( shift->_set_get_scalar_as_object( 'tokens', @_ ) ); }
Subroutine tokens: called CC=1 SCAR=0.0
Pod documented
164  
165 sub update
166 {
167 2 ▶ my $self = shift( @_ );
Subroutine update: called CC=4 SCAR=15.8
Pod documented
168 2 ▶ if( scalar( @_ ) )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_) 2 0 2
169 {
170 2 ▶ if( scalar( @_ ) == 1 && !defined( $_[0] ) )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 1 and not defined $_[0]) { } 0 2 2
Condition33%1/3
scalar @_ == 1 and not defined $_[0]
!ll&&!rl&&r
020
MC/DC0%0/2
scalar @_ == 1 and not defined $_[0]
scalar @_ == 1 not defined $_[0]
Truth table33%1/3
A and not(B)
A= scalar @_ == 1B= not defined $_[0]
ABresult
0X0
100
111
171 {
172 0 $self->tokens->reset;
173 0 $self->items->reset;
174 }
175 else
176 {
177 2 my $items = $self->_string2array( @_ );
178 2 $self->tokens( $items->join( ' ' )->scalar );
179 2 $self->{items} = $items;
180 }
181 }
182 2 return( $self );
183 }
184  
185 # Property
186 0 ▶sub value { return( shift->as_string ); }
Subroutine value: not called CC=1 SCAR=6.9
Pod documented
187  
188 0 ▶sub values { return; }
Subroutine values: not called CC=1 SCAR=6.9
Pod documented
189  
190 sub _reset
191 {
192 1 ▶ my $self = shift( @_ );
Subroutine _reset: called CC=3 SCAR=11.1
193 1 my $elem = $self->element;
194 1 my $tokens = $self->as_string;
195 1 $self->tokens( $tokens );
196 1 ▶ return( $self ) if( !ref( $elem ) );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $elem 0 1 1
197 1 ▶ my $attr = $self->attribute || return( $self );
Condition50%1/2
$self->attribute || (return $self)
l!l
10
MC/DC0%0/1
$self->attribute || (return $self)
$self->attribute
Truth table50%1/2
A
A= $self->attribute
Aresult
11
00
198 1 $elem->attr( $attr => $tokens );
199 1 $elem->reset(1);
200 1 return( $self );
201 }
202  
203 sub _string2array
204 {
205 4 ▶ my $self = shift( @_ );
Subroutine _string2array: called CC=3 SCAR=11.3
206 4 my @tokens = ();
207 4 for( @_ )
208 {
209 4 ▶ if( $self->_is_array( $_ ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_array($_)) { } 0 4 4
210 {
211 0 push( @tokens, @$_ );
212 }
213 # space-delimited tokens
214 else
215 {
216 4 push( @tokens, split( /[[:blank:]\h]+/, _trim( @_ ) ) );
217 }
218 }
219 4 return( $self->new_array( \@tokens )->unique(1) );
220 }
221  
222 sub _trim
223 {
224 6 ▶ my $str = shift( @_ );
Subroutine _trim: called CC=1 SCAR=0.0
225 6 $str =~ s/^[[:blank:]\h]+|[[:blank:]\h]+$//g;
226 6 $str =~ s/[[:blank:]\h]+/ /g;
227 6 return( $str );
228 }
229  
230 2 1;
231 # NOTE: POD
232 __END__
« lib/HTML/Object/XPath/Function.pm ↑ Summary blib/lib/HTML/Object/TokenList.pm »