| 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 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 7 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 7 | ▶ | warnings::register_categories( 'HTML::Object' ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 7 | ▶ | use parent qw( HTML::Object::DOM::Element ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 7 | ▶ | use vars qw( $VERSION ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 7 | our $VERSION = 'v0.2.0'; | |||||||||||||||||||||||||||
| 23 | }; | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 7 | ▶ | 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 | 7 | ▶ | 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 | 7 | 1; | |||||||||||||||||||||||||||
| 91 | # NOTE: POD | ||||||||||||||||||||||||||||
| 92 | __END__ | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | =encoding utf-8 | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | =head1 NAME | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | HTML::Object::Collection - HTML Object XQuery Collection Class | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | use HTML::Object::Collection; | ||||||||||||||||||||||||||||
| 103 | my $col = HTML::Object::Collection->new || | ||||||||||||||||||||||||||||
| 104 | die( HTML::Object::Collection->error, "\n" ); | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | =head1 VERSION | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | v0.2.0 | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | This module serves as a collection of L<HTML elements|HTML::Object::Element> and is used by L<XQuery|HTML::Object::XQuery> when a query is made with a selector or other criteria to group several L<HTML objects|HTML::Object::Element> just like jQuery does. | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | It inherits from L<HTML::Object::Document> and does not do much except for a handful methods. | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | =head1 INHERITANCE | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | +-----------------------+ +----------------------------+ +--------------------------+ | ||||||||||||||||||||||||||||
| 119 | | HTML::Object::Element | --> | HTML::Object::DOM::Element | --> | HTML::Object::Collection | | ||||||||||||||||||||||||||||
| 120 | +-----------------------+ +----------------------------+ +--------------------------+ | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | =head1 PROPERTIES | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | =head2 nodeValue | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | This returns or sets the value of the current node. | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | For document, element or collection, this returns C<undef> and for attribute, text or comment, this returns the objct value. | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeValue> | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | =head1 METHODS | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | =head2 as_string | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | When called, this will return the string representation of the first L<HTML element object|HTML::Object::Element> it contains similar to the behaviour of jQuery. However, as an added feature, if this method is called with C<all> set to 1 or a positive value then, it will return the string representation for all the L<HTML element objects|HTML::Object::Element> it holds. | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | =head2 end | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | Set or get the end L<element|HTML::Object::Element> | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | Jacques Deguest E<lt>F<jack@deguest.jp>E<gt> | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | L<HTML::Object>, L<HTML::Object::Attribute>, L<HTML::Object::Boolean>, L<HTML::Object::Closing>, L<HTML::Object::Collection>, L<HTML::Object::Comment>, L<HTML::Object::Declaration>, L<HTML::Object::Document>, L<HTML::Object::Element>, L<HTML::Object::Exception>, L<HTML::Object::Literal>, L<HTML::Object::Number>, L<HTML::Object::Root>, L<HTML::Object::Space>, L<HTML::Object::Text>, L<HTML::Object::XQuery> | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | Copyright (c) 2021 DEGUEST Pte. Ltd. | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | All rights reserved | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | =cut | ||||||||||||||||||||||||||||