lib/HTML/Object/DOM/Attribute.pm

filter: Statementstmt 38.2% 31 / 81 Branchbran 5.5% 1 / 18 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 34.1% 14 / 41 Podpod 100% 34 / 34 totaltotal 44.6% 80 / 179 CC 11 SCAR 13.8
CC
11
Cov
31%
getAttributes
12.0
getNextSibling
12.0
getPreviousSibling
12.0
CRAP 4.0
« lib/HTML/Object/DOM/Element/UList.pm ↑ Summary lib/HTML/Object/DOM/Element/Option.pm »
1 ##----------------------------------------------------------------------------
2 ## HTML Object - ~/lib/HTML/Object/DOM/Attribute.pm
3 ## Version v0.2.0
4 ## Copyright(c) 2021 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2021/12/13
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::DOM::Attribute;
15 BEGIN
16 {
17 11 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 11 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 11 ▶ warnings::register_categories( 'HTML::Object' );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 11 ▶ use parent qw( HTML::Object::Attribute HTML::Object::DOM::Node );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 11 ▶ use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 11 our $VERSION = 'v0.2.0';
23 };
24  
25 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 31 ▶sub init { return( shift->HTML::Object::Attribute::init( @_ ) ); }
Subroutine init: called CC=1 SCAR=0.0
Pod documented
29  
30 sub getAttributes
31 {
32 0 ▶ my $self = shift( @_ );
Subroutine getAttributes: not called CC=3 SCAR=24.8
Pod documented
33 0 my $elem = $self->element;
34 0 my $a = $self->new_array;
35 0 ▶ return( wantarray() ? $a->list : $a ) if( !$elem );
Branch: 2 branches
BranchTrueFalseTotal
unless $elem 0 0 0
wantarray ? : 0 0 0
36 0 return( $elem->getAttributes );
37 }
38  
39 0 ▶sub getChildNodes { return( wantarray() ? () : [] ); }
Subroutine getChildNodes: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
40  
41 0 ▶sub getElementById { return; }
Subroutine getElementById: not called CC=1 SCAR=6.9
Pod documented
42  
43 0 ▶sub getFirstChild { return; }
Subroutine getFirstChild: not called CC=1 SCAR=6.9
Pod documented
44  
45 0 ▶sub getLastChild { return; }
Subroutine getLastChild: not called CC=1 SCAR=6.9
Pod documented
46  
47 sub getLocalName
48 {
49 0 ▶ my $self = shift( @_ );
Subroutine getLocalName: not called CC=1 SCAR=6.9
Pod documented
50 0 ( my $name = $self->name ) =~ s{^.*:}{};
51 0 return( $name );
52 }
53  
54 26 ▶sub getName { return( shift->name ); }
Subroutine getName: called CC=1 SCAR=0.0
Pod documented
55  
56 sub getNextSibling
57 {
58 0 ▶ my $self = shift( @_ );
Subroutine getNextSibling: not called CC=3 SCAR=24.8
Pod documented
59 0 my $elem = $self->element;
60 0 ▶ return if( !$elem );
Branch: 1 branch
BranchTrueFalseTotal
unless $elem 0 0 0
61 0 my $pos = $elem->attributes_sequence->pos( $self->name );
62 0 ▶ return if( $pos == $elem->attributes_sequence->size );
Branch: 1 branch
BranchTrueFalseTotal
if $pos == $elem->attributes_sequence->size 0 0 0
63 0 my $key = $elem->attributes_sequence->get( $pos - 1 );
64 0 my $val = $elem->attributes->get( $key );
65 0 return( $self->new(
66 element => $elem,
67 name => $key,
68 rank => ( $pos + 1 ),
69 value => $val,
70 ) );
71 }
72  
73 0 ▶sub getParentNode { return( shift->element ); }
Subroutine getParentNode: not called CC=1 SCAR=6.9
Pod documented
74  
75 # awfully inefficient, but hopefully this is called only for weird (read test-case) queries
76 sub getPreviousSibling
77 {
78 0 ▶ my $self = shift( @_ );
Subroutine getPreviousSibling: not called CC=3 SCAR=24.8
Pod documented
79 0 my $elem = $self->element;
80 0 ▶ return if( !$elem );
Branch: 1 branch
BranchTrueFalseTotal
unless $elem 0 0 0
81 0 my $pos = $elem->attributes_sequence->pos( $self->name );
82 0 ▶ return if( !$pos );
Branch: 1 branch
BranchTrueFalseTotal
unless $pos 0 0 0
83 0 my $key = $elem->attributes_sequence->get( $pos - 1 );
84 0 my $val = $elem->attributes->get( $key );
85 0 return( $self->new(
86 element => $elem,
87 name => $key,
88 rank => ( $pos - 1 ),
89 value => $val,
90 ) );
91 }
92  
93 0 ▶sub getRootNode { return( shift->parent->getRootNode ); }
Subroutine getRootNode: not called CC=1 SCAR=6.9
Pod documented
94  
95 0 ▶sub getValue { return( shift->value ); }
Subroutine getValue: not called CC=1 SCAR=6.9
Pod documented
96  
97 0 ▶sub isAttributeNode { 1 }
Subroutine isAttributeNode: not called CC=1 SCAR=6.9
Pod documented
98  
99 0 ▶sub isCommentNode { return(0); }
Subroutine isCommentNode: not called CC=1 SCAR=6.9
Pod documented
100  
101 0 ▶sub isElementNode { return(0); }
Subroutine isElementNode: not called CC=1 SCAR=6.9
Pod documented
102  
103 0 ▶sub isNamespaceNode { return(0); }
Subroutine isNamespaceNode: not called CC=1 SCAR=6.9
Pod documented
104  
105 0 ▶sub isPINode { return(0); }
Subroutine isPINode: not called CC=1 SCAR=6.9
Pod documented
106  
107 0 ▶sub isProcessingInstructionNode { return(0); }
Subroutine isProcessingInstructionNode: not called CC=1 SCAR=6.9
Pod documented
108  
109 0 ▶sub isTextNode { return(0); }
Subroutine isTextNode: not called CC=1 SCAR=6.9
Pod documented
110  
111 sub is_inside
112 {
113 0 ▶ my( $self, $node ) = @_;
Subroutine is_inside: not called CC=2 SCAR=17.9
Pod documented
114 0 my $e = $self->element;
115 0 ▶ return( ( $e == $node ) || $e->is_inside( $node ) );
Condition0%0/3
$e == $node || $e->is_inside($node)
l!l&&r!l&&!r
000
MC/DC0%0/2
$e == $node || $e->is_inside($node)
$e == $node $e->is_inside($node)
Truth table0%0/3
A || B
A= $e == $nodeB= $e->is_inside($node)
ABresult
1X1
011
000
116 }
117  
118 # Updated version from HTML::Object::Element's one because HTML::Object::DOM::Attribute
119 # is considered a child of its associated element
120 # This is used in HTML::Object::DOM::Node->compareDocumentPosition
121 sub lineage
122 {
123 1 ▶ my $self = shift( @_ );
Subroutine lineage: called CC=2 SCAR=7.0
Pod documented
124 1 my $e = $self->element;
125 1 ▶ return( $self->new_array ) if( !$e );
Branch: 1 branch
BranchTrueFalseTotal
unless $e 0 1 1
126 1 return( $e->lineage->unshift( $e ) );
127 }
128  
129 0 ▶sub localName { return( shift->getName ); }
Subroutine localName: not called CC=1 SCAR=6.9
Pod documented
130  
131 0 ▶sub namespaceURI { return; }
Subroutine namespaceURI: not called CC=1 SCAR=6.9
Pod documented
132  
133 # "The value of Attr.name, that is the qualified name of the attribute."
134 # <https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeName>
135 0 ▶sub nodeName { return( shift->name ); }
Subroutine nodeName: not called CC=1 SCAR=6.9
Pod documented
136  
137 # Note: Property
138 1 ▶sub nodeValue : lvalue { return( shift->_set_get_lvalue( 'value', @_ ) ); }
Subroutine nodeValue: called CC=1 SCAR=0.0
Pod documented
139  
140 3 ▶sub ownerElement { return( shift->element ); }
Subroutine ownerElement: called CC=1 SCAR=0.0
Pod documented
141  
142 2 ▶sub parent { return( shift->element ); }
Subroutine parent: called CC=1 SCAR=0.0
Pod documented
143  
144 0 ▶sub prefix { return; }
Subroutine prefix: not called CC=1 SCAR=6.9
Pod documented
145  
146 45 ▶sub string_value { return( shift->value ); }
Subroutine string_value: called CC=1 SCAR=0.0
Pod documented
147  
148 0 ▶sub to_boolean { return( shift->value->scalar ? HTML::Object::Boolean->True : HTML::Object::Boolean->False ); }
Subroutine to_boolean: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
(shift())->value->scalar ? : 0 0 0
149  
150 0 ▶sub to_literal { return( HTML::Object::Literal->new( shift->value ) ); }
Subroutine to_literal: not called CC=1 SCAR=6.9
Pod documented
151  
152 0 ▶sub to_number { return( HTML::Object::Number->new( shift->value ) ); }
Subroutine to_number: not called CC=1 SCAR=6.9
Pod documented
153  
154 sub toString
155 {
156 0 ▶ my $self = shift( @_ );
Subroutine toString: not called CC=1 SCAR=6.9
Pod documented
157 0 return( sprintf( '%s="%s"', $self->name, $self->value ) );
158 }
159  
160 11 1;
161 # NOTE: POD
162 __END__
163  
164 =encoding utf-8
165  
166 =head1 NAME
167  
168 HTML::Object::DOM::Attribute - HTML Object
169  
170 =head1 SYNOPSIS
171  
172 use HTML::Object::DOM::Attribute;
173 my $this = HTML::Object::DOM::Attribute->new || die( HTML::Object::DOM::Attribute->error, "\n" );
174  
175 =head1 VERSION
176  
177 v0.2.0
178  
179 =head1 DESCRIPTION
180  
181 This module implements a DOM node attribute. It inherits from L<HTML::Object::Attribute> and from L<HTML::Object::DOM::Node>
182  
183 =head1 INHERITANCE
184  
185 +-----------------------+ +---------------------------+ +-------------------------+ +----------------------------+
186 | HTML::Object::Element | --> | HTML::Object::EventTarget | --> | HTML::Object::DOM::Node | --> | HTML::Object::DOM::Closing |
187 +-----------------------+ +---------------------------+ +-------------------------+ +----------------------------+
188 | ^
189 | |
190 v |
191 +-----------------------+ |
192 | HTML::Object::Closing | ------------------------------------------------------------------------+
193 +-----------------------+
194  
195 =head1 PROPERTIES
196  
197 =head2 nodeValue
198  
199 Returns the attribute value as a L<scalar object|Module::Generic::Scalar>
200  
201 =head1 METHODS
202  
203 =head2 getAttributes
204  
205 If this attribute has no associated element, this returns an empty list in list context or an empty array reference in scalar context.
206  
207 Otherwise, this returns an L<array object|Module::Generic::Array> of attribute objects for the associated element.
208  
209 =head2 getChildNodes
210  
211 Returns an empty list in list context, or an empty array reference in scalar context.
212  
213 =head2 getElementById
214  
215 Returns an empty list in list context, or C<undef> in scalar context.
216  
217 =head2 getFirstChild
218  
219 Returns an empty list in list context, or C<undef> in scalar context.
220  
221 =head2 getLastChild
222  
223 Returns an empty list in list context, or C<undef> in scalar context.
224  
225 =head2 getLocalName
226  
227 Returns the attribute name.
228  
229 =head2 getName
230  
231 Returns the attribute name.
232  
233 =head2 getNextSibling
234  
235 Returns the next attribute object for the associated element, or C<undef> if there are no associated element, or if this attribute is the last one of the associated element.
236  
237 =head2 getParentNode
238  
239 Returns the associated element, if any.
240  
241 =head2 getPreviousSibling
242  
243 Returns the previous attribute object for the associated element, or C<undef> if there are no associated element, or if this attribute is the first one of the associated element.
244  
245 =head2 getRootNode
246  
247 Returns the root node by calling C<getRootNode> on this attribute parent.
248  
249 =head2 getValue
250  
251 Returns the attribute value.
252  
253 =head2 isAttributeNode
254  
255 Returns true.
256  
257 =head2 isCommentNode
258  
259 Returns false.
260  
261 =head2 isElementNode
262  
263 Returns false.
264  
265 =head2 isEqualNode
266  
267 Provided with another attribute object, and this returns true if both attribute object have the same value, or false otherwise.
268  
269 =head2 isNamespaceNode
270  
271 Returns false.
272  
273 =head2 isPINode
274  
275 Returns false.
276  
277 =head2 isProcessingInstructionNode
278  
279 Returns false.
280  
281 =head2 isTextNode
282  
283 Returns false.
284  
285 =head2 is_inside
286  
287 Provided with a node and this returns true if this attribute associated element is the same, or if the associated element is inside the provided element.
288  
289 =head2 lineage
290  
291 Returns the value provided by calling C<lineage> on the associated element and adding it to the lineage, or an empty L<array object> if there is no associated element.
292  
293 =head2 localName
294  
295 This is just an alias for L</getName>
296  
297 =head2 namespaceURI
298  
299 Always returns C<undef>
300  
301 =head2 nodeName
302  
303 Returns the qualified name of the attribute. This actually calls L<HTML::Object::Attribute/name>
304  
305 =head2 ownerElement
306  
307 This returns the attribute associated element, if any.
308  
309 =head2 parent
310  
311 This returns the attribute associated element, if any.
312  
313 =head2 prefix
314  
315 Always returns C<undef>
316  
317 =head2 string_value
318  
319 Returns the attribute value.
320  
321 =head2 to_boolean
322  
323 Returns a L<true boolean object|HTML::Object::Boolean> if the attribute value is true, or a L<false boolean object|HTML::Object::Boolean> otherwise.
324  
325 =head2 to_literal
326  
327 Returns the attribute value.
328  
329 =head2 to_number
330  
331 Returns a new L<HTML::Object::Number> object based on the attribute value.
332  
333 =head2 toString
334  
335 Returns a string, such as: C<name="value">
336  
337 =head1 AUTHOR
338  
339 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
340  
341 =head1 SEE ALSO
342  
343 L<Mozilla reference|https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes>
344  
345 L<W3C standard on attributes|https://html.spec.whatwg.org/multipage/syntax.html#attributes-2>
346  
347 =head1 COPYRIGHT & LICENSE
348  
349 Copyright(c) 2021 DEGUEST Pte. Ltd.
350  
351 All rights reserved
352  
353 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
354  
355 =cut
« lib/HTML/Object/DOM/Element/UList.pm ↑ Summary lib/HTML/Object/DOM/Element/Option.pm »