| 1 | ##---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 2 | ## HTML Object - ~/lib/HTML/Object/Collection.pm | ||||||||||||||||||||||||||||
| 3 | ## Version v0.2.0 | ||||||||||||||||||||||||||||
| 4 | ## Copyright(c) 2021 DEGUEST Pte. Ltd. | ||||||||||||||||||||||||||||
| 5 | ## Author: Jacques Deguest <jack@deguest.jp> | ||||||||||||||||||||||||||||
| 6 | ## Created 2021/04/28 | ||||||||||||||||||||||||||||
| 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::Collection; | ||||||||||||||||||||||||||||
| 15 | BEGIN | ||||||||||||||||||||||||||||
| 16 | { | ||||||||||||||||||||||||||||
| 17 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 8 | ▶ | warnings::register_categories( 'HTML::Object' ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 8 | ▶ | use parent qw( HTML::Object::DOM::Element ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 8 | ▶ | use vars qw( $VERSION ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 8 | our $VERSION = 'v0.2.0'; | |||||||||||||||||||||||||||
| 23 | }; | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | # Purpose of this class is to serve as a distinctive class for search result from find() | ||||||||||||||||||||||||||||
| 29 | sub init | ||||||||||||||||||||||||||||
| 30 | { | ||||||||||||||||||||||||||||
| 31 | 45 | ▶ | my $self = shift( @_ ); | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=2 SCAR=7.5
Pod
documented
| |||||||||||||||||||||||||||||
| 32 | 45 | $self->{end} = ''; | |||||||||||||||||||||||||||
| 33 | 45 | $self->{tag} = '_collection'; | |||||||||||||||||||||||||||
| 34 | 45 | $self->{_init_strict_use_sub} = 1; | |||||||||||||||||||||||||||
| 35 | 45 | $self->{_exception_class} = 'HTML::Object::Exception'; | |||||||||||||||||||||||||||
| 36 | 45 | ▶ | $self->SUPER::init( @_ ) || return( $self->pass_error ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 45 | my $class = ref( $self ); | |||||||||||||||||||||||||||
| 38 | # To make collection exception also available as $HTML::Object::ERROR | ||||||||||||||||||||||||||||
| 39 | $self->{_error_handler} = sub | ||||||||||||||||||||||||||||
| 40 | { | ||||||||||||||||||||||||||||
| 41 | 0 | ▶ | my $ex = shift( @_ ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=10.9
| |||||||||||||||||||||||||||||
| 42 | # ${"${class}\::ERROR"} = $ex; | ||||||||||||||||||||||||||||
| 43 | 8 | ▶ | no warnings 'once'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | 0 | $HTML::Object::ERROR = $ex; | |||||||||||||||||||||||||||
| 45 | 0 | ▶ | warn( $ex ) if( $self->_is_warnings_enabled( 'HTML::Object' ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 45 | }; | |||||||||||||||||||||||||||
| 47 | 45 | return( $self ); | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | sub as_string | ||||||||||||||||||||||||||||
| 51 | { | ||||||||||||||||||||||||||||
| 52 | 6 | ▶ | my $self = shift( @_ ); | ||||||||||||||||||||||||||
|
Subroutine
as_string: called CC=6 SCAR=18.2
Pod
documented
| |||||||||||||||||||||||||||||
| 53 | 6 | my $opts = $self->_get_args_as_hash( @_ ); | |||||||||||||||||||||||||||
| 54 | 6 | ▶ | $opts->{all} = 0 if( !CORE::exists( $opts->{all} ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 6 | ▶ | $opts->{all} //= 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
$opts->{"all"} //= 0
MC/DC0%0/1
$opts->{"all"} //= 0
$opts->{"all"}
Truth table50%1/2
A
A= $opts->{"all"}
| |||||||||||||||||||||||||||||
| 56 | # In conformity with jQuery, we return the stringified version of the first object in our set | ||||||||||||||||||||||||||||
| 57 | # unless the 'all' option is provided and true. This is a divergence | ||||||||||||||||||||||||||||
| 58 | 6 | ▶ | if( $opts->{all} ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | { | ||||||||||||||||||||||||||||
| 60 | 4 | my $res = $self->new_array; | |||||||||||||||||||||||||||
| 61 | $self->children->foreach(sub | ||||||||||||||||||||||||||||
| 62 | { | ||||||||||||||||||||||||||||
| 63 | 21 | ▶ | my $child = shift( @_ ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 64 | # This will instruct the HTML::Object::Element to automatically close | ||||||||||||||||||||||||||||
| 65 | 21 | my $str = $_->as_string( inside_collection => 1 ); | |||||||||||||||||||||||||||
| 66 | 21 | $res->push( $str ); | |||||||||||||||||||||||||||
| 67 | 4 | }); | |||||||||||||||||||||||||||
| 68 | 4 | return( $res->join( '' ) ); | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | else | ||||||||||||||||||||||||||||
| 71 | { | ||||||||||||||||||||||||||||
| 72 | 2 | my $first = $self->children->first; | |||||||||||||||||||||||||||
| 73 | # If there is no element object in our collection, we return an empty scalar object | ||||||||||||||||||||||||||||
| 74 | # to avoid a potential perl error of "called on undefined value" | ||||||||||||||||||||||||||||
| 75 | # The user can then easily check if anything was provided with: | ||||||||||||||||||||||||||||
| 76 | # $e->as_string->length | ||||||||||||||||||||||||||||
| 77 | 2 | ▶ | return( $self->new_scalar ) if( !$first || !$self->_is_a( $first, 'HTML::Object::Element' ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$first and $self->_is_a($first, "HTML::Object::Element")
MC/DC0%0/2
$first and $self->_is_a($first, "HTML::Object::Element")
$first
$self->_is_a($first, "HTML::Object::Element")
Truth table33%1/3
A and B
A= $firstB= $self->_is_a($first, "HTML::Object::Element")
| |||||||||||||||||||||||||||||
| 78 | 2 | return( $first->as_string( inside_collection => 1 ) ); | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | sub end { return( shift->_set_get_object({ | ||||||||||||||||||||||||||||
| 83 | 46 | ▶ | field => 'end', | ||||||||||||||||||||||||||
|
Subroutine
end: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 84 | weaken => 1, | ||||||||||||||||||||||||||||
| 85 | }, 'HTML::Object::Element', @_ ) ); } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | # Note: Property | ||||||||||||||||||||||||||||
| 88 | 0 | ▶ | sub nodeValue { return; } | ||||||||||||||||||||||||||
|
Subroutine
nodeValue: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 8 | 1; | |||||||||||||||||||||||||||
| 91 | # NOTE: POD | ||||||||||||||||||||||||||||
| 92 | __END__ | ||||||||||||||||||||||||||||