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
|
|
13699
|
use strict; |
|
14
|
|
|
|
|
38
|
|
|
14
|
|
|
|
|
523
|
|
18
|
14
|
|
|
14
|
|
100
|
use warnings; |
|
14
|
|
|
|
|
36
|
|
|
14
|
|
|
|
|
670
|
|
19
|
14
|
|
|
14
|
|
82
|
use parent qw( HTML::Object::DOM::Element ); |
|
14
|
|
|
|
|
31
|
|
|
14
|
|
|
|
|
146
|
|
20
|
14
|
|
|
14
|
|
1348
|
use vars qw( $VERSION ); |
|
14
|
|
|
|
|
56
|
|
|
14
|
|
|
|
|
822
|
|
21
|
14
|
|
|
14
|
|
94
|
use Want; |
|
14
|
|
|
|
|
39
|
|
|
14
|
|
|
|
|
1272
|
|
22
|
14
|
|
|
14
|
|
464
|
our $VERSION = 'v0.2.1'; |
23
|
|
|
|
|
|
|
}; |
24
|
|
|
|
|
|
|
|
25
|
14
|
|
|
14
|
|
104
|
use strict; |
|
14
|
|
|
|
|
39
|
|
|
14
|
|
|
|
|
390
|
|
26
|
14
|
|
|
14
|
|
82
|
use warnings; |
|
14
|
|
|
|
|
53
|
|
|
14
|
|
|
|
|
9751
|
|
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub init |
29
|
|
|
|
|
|
|
{ |
30
|
18
|
|
|
18
|
1
|
1583
|
my $self = shift( @_ ); |
31
|
18
|
|
|
|
|
2004
|
$self->{_init_strict_use_sub} = 1; |
32
|
18
|
50
|
|
|
|
186
|
$self->SUPER::init( @_ ) || return( $self->pass_error ); |
33
|
18
|
50
|
|
|
|
101
|
$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
|
|
|
|
|
228
|
$self->{_initial_text} = 1; |
36
|
18
|
|
|
|
|
78
|
return( $self ); |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
# Note: property text |
40
|
|
|
|
|
|
|
sub text : lvalue { return( shift->_set_get_callback({ |
41
|
|
|
|
|
|
|
get => sub |
42
|
|
|
|
|
|
|
{ |
43
|
4
|
|
|
4
|
|
2505
|
my $self = shift( @_ ); |
44
|
4
|
100
|
66
|
|
|
55
|
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
|
|
|
|
9
|
if( $self->{_initial_text} ) |
49
|
|
|
|
|
|
|
{ |
50
|
2
|
|
|
|
|
11
|
my $children = $self->children; |
51
|
2
|
|
|
|
|
161
|
my $val; |
52
|
2
|
|
|
|
|
23
|
$val = $self->as_text; |
53
|
2
|
100
|
|
|
|
8
|
if( $self->looks_like_it_has_html( "$val" ) ) |
54
|
|
|
|
|
|
|
{ |
55
|
1
|
|
|
|
|
46
|
my $p = $self->new_parser; |
56
|
1
|
|
|
|
|
9
|
my $doc = $p->parse_data( $val ); |
57
|
1
|
|
|
|
|
6
|
my $kids = $doc->children; |
58
|
1
|
|
|
|
|
68
|
$_->parent( $self ) for( @$kids ); |
59
|
1
|
|
|
|
|
35
|
$children->set( $kids ); |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
else |
62
|
|
|
|
|
|
|
{ |
63
|
|
|
|
|
|
|
} |
64
|
2
|
|
|
|
|
518
|
CORE::delete( $self->{_initial_text} ); |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
2
|
|
|
|
|
13
|
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
|
|
|
483
|
if( $self->_is_a( $_ => 'HTML::Object::DOM::Text' ) || |
73
|
|
|
|
|
|
|
$self->_is_a( $_ => 'HTML::Object::DOM::Space' ) ) |
74
|
|
|
|
|
|
|
{ |
75
|
3
|
|
|
|
|
104
|
my $v = $_->value; |
76
|
3
|
|
|
|
|
2383
|
$result->push( "$v" ); |
77
|
|
|
|
|
|
|
} |
78
|
2
|
|
|
|
|
69
|
}); |
79
|
2
|
|
|
|
|
361
|
$self->{_title_text} = $result->join( '' )->scalar; |
80
|
2
|
|
|
|
|
94
|
$self->_remove_reset; |
81
|
|
|
|
|
|
|
} |
82
|
4
|
|
|
|
|
35
|
my $text = $self->{_title_text}; |
83
|
4
|
|
|
|
|
20
|
return( $text ); |
84
|
|
|
|
|
|
|
}, |
85
|
|
|
|
|
|
|
set => sub |
86
|
|
|
|
|
|
|
{ |
87
|
1
|
|
|
1
|
|
331
|
my $self = shift( @_ ); |
88
|
1
|
|
|
|
|
3
|
my $arg = shift( @_ ); |
89
|
1
|
|
|
|
|
25
|
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
|
|
|
|
4
|
if( !defined( $nodes ) ) |
95
|
|
|
|
|
|
|
{ |
96
|
0
|
|
|
|
|
0
|
return( $self->pass_error ); |
97
|
|
|
|
|
|
|
} |
98
|
1
|
|
|
|
|
2
|
my $ok = 1; |
99
|
1
|
|
|
|
|
5
|
for( @$nodes ) |
100
|
|
|
|
|
|
|
{ |
101
|
2
|
50
|
66
|
|
|
52
|
if( !$self->_is_a( $_ => 'HTML::Object::DOM::Text' ) && |
102
|
|
|
|
|
|
|
!$self->_is_a( $_ => 'HTML::Object::DOM::Space' ) ) |
103
|
|
|
|
|
|
|
{ |
104
|
1
|
|
|
|
|
82
|
$ok = 0, last; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
} |
107
|
1
|
50
|
|
|
|
3
|
if( !$ok ) |
108
|
|
|
|
|
|
|
{ |
109
|
1
|
|
|
|
|
20
|
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
|
41349
|
}, @_ ) ); } |
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 |