File Coverage

lib/HTML/Object/DOM/Element/Title.pm
Criterion Covered Total %
statement 61 67 91.0
branch 12 18 66.6
condition 6 9 66.6
subroutine 12 12 100.0
pod 2 2 100.0
total 93 108 86.1


line stmt bran cond sub pod time code
1             ##----------------------------------------------------------------------------
2             ## HTML Object - ~/lib/HTML/Object/DOM/Element/Title.pm
3             ## Version v0.2.1
4             ## Copyright(c) 2022 DEGUEST Pte. Ltd.
5             ## Author: Jacques Deguest <jack@deguest.jp>
6             ## Created 2022/01/09
7             ## Modified 2022/09/20
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::Element::Title;
15             BEGIN
16             {
17 14     14   12306 use strict;
  14         31  
  14         514  
18 14     14   84 use warnings;
  14         31  
  14         550  
19 14     14   79 use parent qw( HTML::Object::DOM::Element );
  14         30  
  14         118  
20 14     14   1234 use vars qw( $VERSION );
  14         35  
  14         692  
21 14     14   88 use Want;
  14         32  
  14         1239  
22 14     14   307 our $VERSION = 'v0.2.1';
23             };
24              
25 14     14   81 use strict;
  14         34  
  14         335  
26 14     14   83 use warnings;
  14         31  
  14         8904  
27              
28             sub init
29             {
30 18     18 1 1536 my $self = shift( @_ );
31 18         1818 $self->{_init_strict_use_sub} = 1;
32 18 50       163 $self->SUPER::init( @_ ) || return( $self->pass_error );
33 18 50       100 $self->{tag} = 'title' if( !CORE::length( "$self->{tag}" ) );
34             # We set this boolean to true to indicate we have not yet looked into the text inside the <title></title>
35 18         205 $self->{_initial_text} = 1;
36 18         75 return( $self );
37             }
38              
39             # Note: property text
40             sub text : lvalue { return( shift->_set_get_callback({
41             get => sub
42             {
43 4     4   2045 my $self = shift( @_ );
44 4 100 66     35 if( !$self->{_title_text} || $self->_is_reset )
45             {
46             # We set this boolean to true to indicate we have not yet looked into the text inside the <title></title>
47             # The HTML::Parser sets the title value to anything within <title></title> no matter if there are any tag embedded, so we need to parse it further, but only once, hence this boolean value
48 2 50       8 if( $self->{_initial_text} )
49             {
50 2         6 my $children = $self->children;
51 2         94 my $val;
52 2         26 $val = $self->as_text;
53 2 100       18 if( $self->looks_like_it_has_html( "$val" ) )
54             {
55 1         9 my $p = $self->new_parser;
56 1         5 my $doc = $p->parse_data( $val );
57 1         4 my $kids = $doc->children;
58 1         52 $_->parent( $self ) for( @$kids );
59 1         24 $children->set( $kids );
60             }
61             else
62             {
63             }
64 2         353 CORE::delete( $self->{_initial_text} );
65             }
66            
67 2         10 my $result = $self->new_array;
68             # We purposively skip anything that is neither a space nor a text.
69             # This is what web browser do, notwithstanding any tag that may exist in the <title> tag
70             $self->children->foreach(sub
71             {
72 4 100 66     335 if( $self->_is_a( $_ => 'HTML::Object::DOM::Text' ) ||
73             $self->_is_a( $_ => 'HTML::Object::DOM::Space' ) )
74             {
75 3         75 my $v = $_->value;
76 3         2122 $result->push( "$v" );
77             }
78 2         39 });
79 2         246 $self->{_title_text} = $result->join( '' )->scalar;
80 2         63 $self->_remove_reset;
81             }
82 4         21 my $text = $self->{_title_text};
83 4         12 return( $text );
84             },
85             set => sub
86             {
87 1     1   325 my $self = shift( @_ );
88 1         2 my $arg = shift( @_ );
89 1         9 my $nodes = $self->_get_from_list_of_elements_or_html( $arg );
90             # for( my $i = 0; $i < scalar( @$nodes ); $i++ )
91             # {
92             # }
93            
94 1 50       3 if( !defined( $nodes ) )
95             {
96 0         0 return( $self->pass_error );
97             }
98 1         3 my $ok = 1;
99 1         3 for( @$nodes )
100             {
101 2 50 66     53 if( !$self->_is_a( $_ => 'HTML::Object::DOM::Text' ) &&
102             !$self->_is_a( $_ => 'HTML::Object::DOM::Space' ) )
103             {
104 1         63 $ok = 0, last;
105             }
106             }
107 1 50       4 if( !$ok )
108             {
109 1         12 return( $self->error( 'Values provided for title text contains data other tan text or space. You can provide text, space including HTML::Object::DOM::Text and HTML::Object::DOM::Space objects' ) );
110             }
111 0         0 $_->parent( $self ) for( @$nodes );
112 0         0 my $children = $self->children;
113 0         0 $children->set( $nodes );
114 0         0 $self->reset(1);
115 0         0 return(1);
116             }
117 5     5 1 35612 }, @_ ) ); }
118              
119             1;
120             # NOTE: POD
121             __END__
122              
123             =encoding utf-8
124              
125             =head1 NAME
126              
127             HTML::Object::DOM::Element::Title - HTML Object DOM Title Class
128              
129             =head1 SYNOPSIS
130              
131             use HTML::Object::DOM::Element::Title;
132             my $title = HTML::Object::DOM::Element::Title->new ||
133             die( HTML::Object::DOM::Element::Title->error, "\n" );
134              
135             =head1 VERSION
136              
137             v0.2.1
138              
139             =head1 DESCRIPTION
140              
141             This interface contains the title for a document. This element inherits all of the properties and methods of the L<HTML::Object::DOM::Element> interface.
142              
143             =head1 INHERITANCE
144              
145             +-----------------------+ +---------------------------+ +-------------------------+ +----------------------------+ +-----------------------------------+
146             | HTML::Object::Element | --> | HTML::Object::EventTarget | --> | HTML::Object::DOM::Node | --> | HTML::Object::DOM::Element | --> | HTML::Object::DOM::Element::Title |
147             +-----------------------+ +---------------------------+ +-------------------------+ +----------------------------+ +-----------------------------------+
148              
149             =head1 PROPERTIES
150              
151             Inherits properties from its parent L<HTML::Object::DOM::Element>
152              
153             =head2 text
154              
155             Is a string representing the text of the document's title, and only the text part. For example, consider this:
156              
157             <!doctype html>
158             <html>
159             <head>
160             <title>Hello world! <span class="highlight">Isn't this wonderful</span> really?</title>
161             </head>
162             <body></body>
163             </html>
164              
165             my $title = $doc->getElementsByTagName( 'title' )->[0];
166             say $title->text;
167             # Hello world! really?
168              
169             As you can see, the tag C<span> and its content was skipped.
170              
171             Also, do not confuse:
172              
173             $doc->title;
174              
175             with:
176              
177             $doc->getElementsByTagName( 'title' )->[0];
178              
179             The former is just a setter/getter method to set or get the inner text value of the document title, while the latter is the L<HTML::Object::DOM::Element::Title> object. So you cannot write:
180              
181             $doc->title->text = "Hello world!";
182              
183             See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLTitleElement/text>
184              
185             =head1 METHODS
186              
187             Inherits methods from its parent L<HTML::Object::DOM::Element>
188              
189             =head1 AUTHOR
190              
191             Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
192              
193             =head1 SEE ALSO
194              
195             L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLTitleElement>, L<Mozilla documentation on title element|https://developer.mozilla.org/en-US/docs/Web/HTML/Element/title>
196              
197             =head1 COPYRIGHT & LICENSE
198              
199             Copyright(c) 2022 DEGUEST Pte. Ltd.
200              
201             All rights reserved
202              
203             This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
204              
205             =cut