File Coverage

lib/HTML/Object/DOM/Attribute.pm
Criterion Covered Total %
statement 29 79 36.7
branch 1 18 5.5
condition 0 3 0.0
subroutine 14 41 34.1
pod 34 34 100.0
total 78 175 44.5


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