| 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
Condition33%1/3
defined $tokens and CORE::length $tokens
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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.'))
MC/DC0%0/1
shift @_ || (return $self->error('No anonymous subroutine was provided.'))
shift @_
Truth table0%0/2
A
A= shift @_
| |||||||||||||||||||||||||||||
| 70 | 0 | ▶ | return( $self->error( "Callback provided is not an anonymous subroutine" ) ) if( ref( $code ) ne 'CODE' ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 74 | 0 | $self->items->foreach( $code ); | |||||||||||||||||||||||||||
| 75 | 0 | ▶ | if( $elem ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | { | ||||||||||||||||||||||||||||
| 77 | 0 | my $after = $self->items->join( ',' )->scalar; | |||||||||||||||||||||||||||
| 78 | 0 | ▶ | $elem->_reset(1) if( $before ne $after ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
$attr and $elem
MC/DC0%0/2
$attr and $elem
$attr
$elem
Truth table33%1/3
A and B
A= $attrB= $elem
| |||||||||||||||||||||||||||||
| 100 | { | ||||||||||||||||||||||||||||
| 101 | 5 | my $elem_tokens = $elem->attr( $attr ); | |||||||||||||||||||||||||||
| 102 | 5 | ▶ | if( $elem_tokens eq $self->tokens ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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.'))
MC/DC0%0/1
shift @_ || (return $self->error('No token was provided to toggle.'))
shift @_
Truth table0%0/2
A
A= shift @_
| |||||||||||||||||||||||||||||
| 148 | 0 | $token = _trim( $token ); | |||||||||||||||||||||||||||
| 149 | 0 | my $rv; | |||||||||||||||||||||||||||
| 150 | 0 | ▶ | if( $self->items->has( $token ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | { | ||||||||||||||||||||||||||||
| 152 | 0 | ▶ | $rv = $self->items->remove( $token ) ? $self->true : $self->false; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 169 | { | ||||||||||||||||||||||||||||
| 170 | 2 | ▶ | if( scalar( @_ ) == 1 && !defined( $_[0] ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
scalar @_ == 1 and not defined $_[0]
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]
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 197 | 1 | ▶ | my $attr = $self->attribute || return( $self ); | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->attribute || (return $self)
MC/DC0%0/1
$self->attribute || (return $self)
$self->attribute
Truth table50%1/2
A
A= $self->attribute
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||