File Coverage

lib/HTML/Object/Text.pm
Criterion Covered Total %
statement 36 40 90.0
branch 2 4 50.0
condition n/a
subroutine 12 14 85.7
pod 6 6 100.0
total 56 64 87.5


line stmt bran cond sub pod time code
1             ##----------------------------------------------------------------------------
2             ## HTML Object - ~/lib/HTML/Object/Text.pm
3             ## Version v0.2.0
4             ## Copyright(c) 2021 DEGUEST Pte. Ltd.
5             ## Author: Jacques Deguest <jack@deguest.jp>
6             ## Created 2021/04/22
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::Text;
15             BEGIN
16             {
17 30     30   581 use strict;
  30         57  
  30         916  
18 30     30   153 use warnings;
  30         81  
  30         766  
19 30     30   156 use warnings::register;
  30         53  
  30         2946  
20 30     30   152 use parent qw( HTML::Object::Element );
  30         54  
  30         152  
21 30     30   1842 use vars qw( $VERSION );
  30         57  
  30         1283  
22 30     30   518 our $VERSION = 'v0.2.0';
23             };
24              
25 30     30   171 use strict;
  30         52  
  30         519  
26 30     30   129 use warnings;
  30         60  
  30         7099  
27              
28             sub init
29             {
30 131     131 1 1105 my $self = shift( @_ );
31 131         633 $self->{tag} = '_text';
32 131         414 $self->{value} = '';
33 131         394 $self->{is_empty} = 1;
34 131         327 $self->{_init_strict_use_sub} = 1;
35 131         449 $self->{_exception_class} = 'HTML::Object::Exception';
36 131 50       863 $self->SUPER::init( @_ ) || return( $self->pass_error );
37 131         935 return( $self );
38             }
39              
40             sub as_string
41             {
42 53     53 1 263 my $self = shift( @_ );
43             # We need to remove the reset flag, otherwise if there are any subsequent change that need to be propagated to this text parent, HTML::Object::Element::reset will not propagate it
44 53         559 $self->_remove_reset;
45 53 50       249 return( $self->value->length ? $self->value : $self->original );
46             }
47              
48             # There is nothing to encode because we have not touched the text in the first place.
49             # Especially in the script tag section !
50             # If the user wants to, he can and should go ahead, but it is way too dangerous for us to make assumptions
51 0     0 1 0 sub as_xml { return( shift->as_string( @_ ) ); }
52              
53             # added to provide element-like methods to text nodes, for use by cmp
54             sub lineage
55             {
56 0     0 1 0 my $node = shift( @_ );
57 0         0 my $parent = $node->parent;
58 0         0 return( $parent, $parent->lineage );
59             }
60              
61             sub set_checksum
62             {
63 131     131 1 641 my $self = shift( @_ );
64 131         668 return( $self->_get_md5_hash( $self->value->scalar ) );
65             }
66              
67 386     386 1 4449584 sub value : lvalue { return( shift->_set_get_scalar_as_object( 'value', @_ ) ); }
68              
69             1;
70             # NOTE: POD
71             __END__
72              
73             =encoding utf-8
74              
75             =head1 NAME
76              
77             HTML::Object::Text - HTML Object
78              
79             =head1 SYNOPSIS
80              
81             use HTML::Object::Text;
82             my $txt = HTML::Object::Text->new ||
83             die( HTML::Object::Text->error, "\n" );
84              
85             =head1 VERSION
86              
87             v0.2.0
88              
89             =head1 DESCRIPTION
90              
91             This module represents a text. It inherits from L<HTML::Object::Element>
92              
93             =head1 INHERITANCE
94              
95             +-----------------------+ +--------------------+
96             | HTML::Object::Element | --> | HTML::Object::Text |
97             +-----------------------+ +--------------------+
98              
99             =head1 PROPERTIES
100              
101             =head2 nodeValue
102              
103             This returns or sets the value of the current node.
104              
105             For document, element or collection, this returns C<undef> and for attribute, text or comment, this returns the objct value.
106              
107             See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeValue>
108              
109             =head1 METHODS
110              
111             =head2 as_string
112              
113             Returns the original value or the value assigned with L</value>
114              
115             =head2 as_xml
116              
117             This is an alias for L</as_string>
118              
119             =head2 isEqualNode
120              
121             Returns a boolean value which indicates whether or not two elements are of the same type and all their defining data points match.
122              
123             Two elements are equal when they have the same type, defining characteristics (this would be their ID, number of children, and so forth), its attributes match, and so on. The specific set of data points that must match varies depending on the types of the elements.
124              
125             See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Node/isEqualNode>
126              
127             =head2 lineage
128              
129             Returns an L<array object|Module::Generic::Array> of all the text element's ancestors.
130              
131             =head2 nodeValue
132              
133             This returns or sets the value of the current element.
134              
135             See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeValue>
136              
137             =head2 set_checksum
138              
139             Read-only method.
140              
141             This returns the md5 checksum for the current value.
142              
143             =head2 value
144              
145             Returns the current value as a L<scalar object|Module::Generic::Scalar>
146              
147             =head1 AUTHOR
148              
149             Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
150              
151             =head1 SEE ALSO
152              
153             L<https://html.spec.whatwg.org/multipage/syntax.html#the-doctype>
154              
155             L<https://developer.mozilla.org/en-US/docs/Web/HTML/Quirks_Mode_and_Standards_Mode>
156              
157             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>
158              
159             =head1 COPYRIGHT & LICENSE
160              
161             Copyright (c) 2021 DEGUEST Pte. Ltd.
162              
163             All rights reserved
164              
165             This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
166              
167             =cut