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

filter: Statementstmt 0.0% 0 / 54 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 7 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 86 CC 5 SCAR 19.5
CC
5
Cov
0%
as_string
20.0
init
6.0
BEGIN
2.0
CRAP 7.0
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 use strict;
18 use warnings;
19 warnings::register_categories( 'HTML::Object' );
20 use parent qw( HTML::Object::DOM::Element );
21 use vars qw( $VERSION );
22 our $VERSION = 'v0.2.0';
23 };
24  
25 use strict;
26 use warnings;
27  
28 # Purpose of this class is to serve as a distinctive class for search result from find()
29 sub init
30 {
31 my $self = shift( @_ );
32 $self->{end} = '';
33 $self->{tag} = '_collection';
34 $self->{_init_strict_use_sub} = 1;
35 $self->{_exception_class} = 'HTML::Object::Exception';
36 $self->SUPER::init( @_ ) || return( $self->pass_error );
37 my $class = ref( $self );
38 # To make collection exception also available as $HTML::Object::ERROR
39 $self->{_error_handler} = sub
40 {
41 my $ex = shift( @_ );
42 # ${"${class}\::ERROR"} = $ex;
43 no warnings 'once';
44 $HTML::Object::ERROR = $ex;
45 warn( $ex ) if( $self->_is_warnings_enabled( 'HTML::Object' ) );
46 };
47 return( $self );
48 }
49  
50 sub as_string
51 {
52 my $self = shift( @_ );
53 my $opts = $self->_get_args_as_hash( @_ );
54 $opts->{all} = 0 if( !CORE::exists( $opts->{all} ) );
55 $opts->{all} //= 0;
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 if( $opts->{all} )
59 {
60 my $res = $self->new_array;
61 $self->children->foreach(sub
62 {
63 my $child = shift( @_ );
64 # This will instruct the HTML::Object::Element to automatically close
65 my $str = $_->as_string( inside_collection => 1 );
66 $res->push( $str );
67 });
68 return( $res->join( '' ) );
69 }
70 else
71 {
72 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 return( $self->new_scalar ) if( !$first || !$self->_is_a( $first, 'HTML::Object::Element' ) );
78 return( $first->as_string( inside_collection => 1 ) );
79 }
80 }
81  
82 sub end { return( shift->_set_get_object({
83 field => 'end',
84 weaken => 1,
85 }, 'HTML::Object::Element', @_ ) ); }
86  
87 # Note: Property
88 sub nodeValue { return; }
89  
90 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