| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package HTML::HeadParser; |
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=head1 NAME |
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
HTML::HeadParser - Parse section of a HTML document |
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
require HTML::HeadParser; |
|
10
|
|
|
|
|
|
|
$p = HTML::HeadParser->new; |
|
11
|
|
|
|
|
|
|
$p->parse($text) and print "not finished"; |
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
$p->header('Title') # to access .... |
|
14
|
|
|
|
|
|
|
$p->header('Content-Base') # to access |
|
15
|
|
|
|
|
|
|
$p->header('Foo') # to access |
|
16
|
|
|
|
|
|
|
$p->header('X-Meta-Author') # to access |
|
17
|
|
|
|
|
|
|
$p->header('X-Meta-Charset') # to access |
|
18
|
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
20
|
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
The C is a specialized (and lightweight) |
|
22
|
|
|
|
|
|
|
C that will only parse the EHEAD>...E/HEAD> |
|
23
|
|
|
|
|
|
|
section of an HTML document. The parse() method |
|
24
|
|
|
|
|
|
|
will return a FALSE value as soon as some EBODY> element or body |
|
25
|
|
|
|
|
|
|
text are found, and should not be called again after this. |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
Note that the C might get confused if raw undecoded |
|
28
|
|
|
|
|
|
|
UTF-8 is passed to the parse() method. Make sure the strings are |
|
29
|
|
|
|
|
|
|
properly decoded before passing them on. |
|
30
|
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
The C keeps a reference to a header object, and the |
|
32
|
|
|
|
|
|
|
parser will update this header object as the various elements of the |
|
33
|
|
|
|
|
|
|
EHEAD> section of the HTML document are recognized. The following |
|
34
|
|
|
|
|
|
|
header fields are affected: |
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=over 4 |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=item Content-Base: |
|
39
|
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
The I header is initialized from the Ebase |
|
41
|
|
|
|
|
|
|
href="..."> element. |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=item Title: |
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
The I header is initialized from the Etitle>...E/title> |
|
46
|
|
|
|
|
|
|
element. |
|
47
|
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=item Isindex: |
|
49
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
The I header will be added if there is a Eisindex> |
|
51
|
|
|
|
|
|
|
element in the Ehead>. The header value is initialized from the |
|
52
|
|
|
|
|
|
|
I attribute if it is present. If no I attribute is |
|
53
|
|
|
|
|
|
|
given it will have '?' as the value. |
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=item X-Meta-Foo: |
|
56
|
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
All Emeta> elements containing a C attribute will result in |
|
58
|
|
|
|
|
|
|
headers using the prefix C appended with the value of the |
|
59
|
|
|
|
|
|
|
C attribute as the name of the header, and the value of the |
|
60
|
|
|
|
|
|
|
C attribute as the pushed header value. |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
Emeta> elements containing a C attribute will result |
|
63
|
|
|
|
|
|
|
in headers as in above, but without the C prefix in the |
|
64
|
|
|
|
|
|
|
header name. |
|
65
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
Emeta> elements containing a C attribute will result in |
|
67
|
|
|
|
|
|
|
an C header, using the value of the C |
|
68
|
|
|
|
|
|
|
attribute as the pushed header value. |
|
69
|
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
The ':' character can't be represented in header field names, so |
|
71
|
|
|
|
|
|
|
if the meta element contains this char it's substituted with '-' |
|
72
|
|
|
|
|
|
|
before forming the field name. |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=back |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=head1 METHODS |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
The following methods (in addition to those provided by the |
|
79
|
|
|
|
|
|
|
superclass) are available: |
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=over 4 |
|
82
|
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=cut |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
require HTML::Parser; |
|
87
|
|
|
|
|
|
|
our @ISA = qw(HTML::Parser); |
|
88
|
|
|
|
|
|
|
|
|
89
|
2
|
|
|
2
|
|
2575
|
use HTML::Entities (); |
|
|
2
|
|
|
|
|
7
|
|
|
|
2
|
|
|
|
|
67
|
|
|
90
|
|
|
|
|
|
|
|
|
91
|
2
|
|
|
2
|
|
13
|
use strict; |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
2463
|
|
|
92
|
|
|
|
|
|
|
our $DEBUG; |
|
93
|
|
|
|
|
|
|
#$DEBUG = 1; |
|
94
|
|
|
|
|
|
|
our $VERSION = '3.81'; |
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=item $hp = HTML::HeadParser->new |
|
97
|
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=item $hp = HTML::HeadParser->new( $header ) |
|
99
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
The object constructor. The optional $header argument should be a |
|
101
|
|
|
|
|
|
|
reference to an object that implement the header() and push_header() |
|
102
|
|
|
|
|
|
|
methods as defined by the C class. Normally it will be |
|
103
|
|
|
|
|
|
|
of some class that is a or delegates to the C class. |
|
104
|
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
If no $header is given C will create an |
|
106
|
|
|
|
|
|
|
C object by itself (initially empty). |
|
107
|
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=cut |
|
109
|
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
sub new |
|
111
|
|
|
|
|
|
|
{ |
|
112
|
8
|
|
|
8
|
1
|
3408
|
my($class, $header) = @_; |
|
113
|
8
|
100
|
|
|
|
28
|
unless ($header) { |
|
114
|
1
|
|
|
|
|
509
|
require HTTP::Headers; |
|
115
|
1
|
|
|
|
|
6649
|
$header = HTTP::Headers->new; |
|
116
|
|
|
|
|
|
|
} |
|
117
|
|
|
|
|
|
|
|
|
118
|
8
|
|
|
|
|
69
|
my $self = $class->SUPER::new(api_version => 3, |
|
119
|
|
|
|
|
|
|
start_h => ["start", "self,tagname,attr"], |
|
120
|
|
|
|
|
|
|
end_h => ["end", "self,tagname"], |
|
121
|
|
|
|
|
|
|
text_h => ["text", "self,text"], |
|
122
|
|
|
|
|
|
|
ignore_elements => [qw(script style)], |
|
123
|
|
|
|
|
|
|
); |
|
124
|
8
|
|
|
|
|
34
|
$self->{'header'} = $header; |
|
125
|
8
|
|
|
|
|
16
|
$self->{'tag'} = ''; # name of active element that takes textual content |
|
126
|
8
|
|
|
|
|
16
|
$self->{'text'} = ''; # the accumulated text associated with the element |
|
127
|
8
|
|
|
|
|
60
|
$self; |
|
128
|
|
|
|
|
|
|
} |
|
129
|
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=item $hp->header; |
|
131
|
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
Returns a reference to the header object. |
|
133
|
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=item $hp->header( $key ) |
|
135
|
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
Returns a header value. It is just a shorter way to write |
|
137
|
|
|
|
|
|
|
C<$hp-Eheader-Eheader($key)>. |
|
138
|
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=cut |
|
140
|
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
sub header |
|
142
|
|
|
|
|
|
|
{ |
|
143
|
14
|
|
|
14
|
1
|
5166
|
my $self = shift; |
|
144
|
14
|
100
|
|
|
|
50
|
return $self->{'header'} unless @_; |
|
145
|
13
|
|
|
|
|
34
|
$self->{'header'}->header(@_); |
|
146
|
|
|
|
|
|
|
} |
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
sub as_string # legacy |
|
149
|
|
|
|
|
|
|
{ |
|
150
|
3
|
|
|
3
|
0
|
370
|
my $self = shift; |
|
151
|
3
|
|
|
|
|
10
|
$self->{'header'}->as_string; |
|
152
|
|
|
|
|
|
|
} |
|
153
|
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
sub flush_text # internal |
|
155
|
|
|
|
|
|
|
{ |
|
156
|
13
|
|
|
13
|
0
|
23
|
my $self = shift; |
|
157
|
13
|
|
|
|
|
19
|
my $tag = $self->{'tag'}; |
|
158
|
13
|
|
|
|
|
18
|
my $text = $self->{'text'}; |
|
159
|
13
|
|
|
|
|
36
|
$text =~ s/^\s+//; |
|
160
|
13
|
|
|
|
|
47
|
$text =~ s/\s+$//; |
|
161
|
13
|
|
|
|
|
43
|
$text =~ s/\s+/ /g; |
|
162
|
13
|
50
|
|
|
|
30
|
print "FLUSH $tag => '$text'\n" if $DEBUG; |
|
163
|
13
|
100
|
|
|
|
30
|
if ($tag eq 'title') { |
|
164
|
7
|
|
|
|
|
10
|
my $decoded; |
|
165
|
7
|
100
|
66
|
|
|
33
|
$decoded = utf8::decode($text) if $self->utf8_mode && defined &utf8::decode; |
|
166
|
7
|
|
|
|
|
41
|
HTML::Entities::decode($text); |
|
167
|
7
|
100
|
|
|
|
20
|
utf8::encode($text) if $decoded; |
|
168
|
7
|
|
|
|
|
27
|
$self->{'header'}->push_header(Title => $text); |
|
169
|
|
|
|
|
|
|
} |
|
170
|
13
|
|
|
|
|
124
|
$self->{'tag'} = $self->{'text'} = ''; |
|
171
|
|
|
|
|
|
|
} |
|
172
|
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
# This is an quote from the HTML3.2 DTD which shows which elements |
|
174
|
|
|
|
|
|
|
# that might be present in a .... Also note that the |
|
175
|
|
|
|
|
|
|
# tags themselves might be missing: |
|
176
|
|
|
|
|
|
|
# |
|
177
|
|
|
|
|
|
|
#
|
|
178
|
|
|
|
|
|
|
# SCRIPT* & META* & LINK*"> |
|
179
|
|
|
|
|
|
|
# |
|
180
|
|
|
|
|
|
|
# |
|
181
|
|
|
|
|
|
|
# |
|
182
|
|
|
|
|
|
|
# From HTML 4.01: |
|
183
|
|
|
|
|
|
|
# |
|
184
|
|
|
|
|
|
|
# |
|
185
|
|
|
|
|
|
|
# |
|
186
|
|
|
|
|
|
|
# |
|
187
|
|
|
|
|
|
|
# |
|
188
|
|
|
|
|
|
|
# From HTML 5 as of WD-html5-20090825: |
|
189
|
|
|
|
|
|
|
# |
|
190
|
|
|
|
|
|
|
# One or more elements of metadata content, [...] |
|
191
|
|
|
|
|
|
|
# => base, command, link, meta, noscript, script, style, title |
|
192
|
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
sub start |
|
194
|
|
|
|
|
|
|
{ |
|
195
|
47
|
|
|
47
|
1
|
1441
|
my($self, $tag, $attr) = @_; # $attr is reference to a HASH |
|
196
|
47
|
50
|
|
|
|
88
|
print "START[$tag]\n" if $DEBUG; |
|
197
|
47
|
100
|
|
|
|
88
|
$self->flush_text if $self->{'tag'}; |
|
198
|
47
|
100
|
66
|
|
|
191
|
if ($tag eq 'meta') { |
|
|
|
100
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
199
|
20
|
|
|
|
|
33
|
my $key = $attr->{'http-equiv'}; |
|
200
|
20
|
100
|
66
|
|
|
56
|
if (!defined($key) || !length($key)) { |
|
201
|
13
|
100
|
|
|
|
28
|
if ($attr->{name}) { |
|
|
|
50
|
|
|
|
|
|
|
202
|
10
|
|
|
|
|
29
|
$key = "X-Meta-\u$attr->{name}"; |
|
203
|
|
|
|
|
|
|
} elsif ($attr->{charset}) { # HTML 5 |
|
204
|
3
|
|
|
|
|
6
|
$key = "X-Meta-Charset"; |
|
205
|
3
|
|
|
|
|
10
|
$self->{header}->push_header($key => $attr->{charset}); |
|
206
|
3
|
|
|
|
|
40
|
return; |
|
207
|
|
|
|
|
|
|
} else { |
|
208
|
0
|
|
|
|
|
0
|
return; |
|
209
|
|
|
|
|
|
|
} |
|
210
|
|
|
|
|
|
|
} |
|
211
|
17
|
|
|
|
|
53
|
$key =~ s/:/-/g; |
|
212
|
17
|
|
|
|
|
46
|
$self->{'header'}->push_header($key => $attr->{content}); |
|
213
|
|
|
|
|
|
|
} elsif ($tag eq 'base') { |
|
214
|
4
|
50
|
|
|
|
11
|
return unless exists $attr->{href}; |
|
215
|
4
|
|
|
|
|
16
|
(my $base = $attr->{href}) =~ s/^\s+//; $base =~ s/\s+$//; # HTML5 |
|
|
4
|
|
|
|
|
14
|
|
|
216
|
4
|
|
|
|
|
12
|
$self->{'header'}->push_header('Content-Base' => $base); |
|
217
|
|
|
|
|
|
|
} elsif ($tag eq 'isindex') { |
|
218
|
|
|
|
|
|
|
# This is a non-standard header. Perhaps we should just ignore |
|
219
|
|
|
|
|
|
|
# this element |
|
220
|
0
|
|
0
|
|
|
0
|
$self->{'header'}->push_header(Isindex => $attr->{prompt} || '?'); |
|
221
|
|
|
|
|
|
|
} elsif ($tag =~ /^(?:title|noscript|object|command)$/) { |
|
222
|
|
|
|
|
|
|
# Just remember tag. Initialize header when we see the end tag. |
|
223
|
13
|
|
|
|
|
75
|
$self->{'tag'} = $tag; |
|
224
|
|
|
|
|
|
|
} elsif ($tag eq 'link') { |
|
225
|
6
|
50
|
|
|
|
24
|
return unless exists $attr->{href}; |
|
226
|
|
|
|
|
|
|
# |
|
227
|
6
|
|
|
|
|
14
|
my $href = delete($attr->{href}); |
|
228
|
6
|
|
|
|
|
16
|
$href =~ s/^\s+//; $href =~ s/\s+$//; # HTML5 |
|
|
6
|
|
|
|
|
14
|
|
|
229
|
6
|
|
|
|
|
19
|
my $h_val = "<$href>"; |
|
230
|
6
|
|
|
|
|
9
|
for (sort keys %{$attr}) { |
|
|
6
|
|
|
|
|
32
|
|
|
231
|
16
|
100
|
|
|
|
31
|
next if $_ eq "/"; # XHTML junk |
|
232
|
13
|
|
|
|
|
41
|
$h_val .= qq(; $_="$attr->{$_}"); |
|
233
|
|
|
|
|
|
|
} |
|
234
|
6
|
|
|
|
|
17
|
$self->{'header'}->push_header(Link => $h_val); |
|
235
|
|
|
|
|
|
|
} elsif ($tag eq 'head' || $tag eq 'html') { |
|
236
|
|
|
|
|
|
|
# ignore |
|
237
|
|
|
|
|
|
|
} else { |
|
238
|
|
|
|
|
|
|
# stop parsing |
|
239
|
0
|
|
|
|
|
0
|
$self->eof; |
|
240
|
|
|
|
|
|
|
} |
|
241
|
|
|
|
|
|
|
} |
|
242
|
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
sub end |
|
244
|
|
|
|
|
|
|
{ |
|
245
|
12
|
|
|
12
|
1
|
66
|
my($self, $tag) = @_; |
|
246
|
12
|
50
|
|
|
|
26
|
print "END[$tag]\n" if $DEBUG; |
|
247
|
12
|
100
|
|
|
|
40
|
$self->flush_text if $self->{'tag'}; |
|
248
|
12
|
100
|
|
|
|
61
|
$self->eof if $tag eq 'head'; |
|
249
|
|
|
|
|
|
|
} |
|
250
|
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
sub text |
|
252
|
|
|
|
|
|
|
{ |
|
253
|
71
|
|
|
71
|
1
|
1122
|
my($self, $text) = @_; |
|
254
|
71
|
50
|
|
|
|
132
|
print "TEXT[$text]\n" if $DEBUG; |
|
255
|
71
|
100
|
|
|
|
136
|
unless ($self->{first_chunk}) { |
|
256
|
|
|
|
|
|
|
# drop Unicode BOM if found |
|
257
|
8
|
100
|
|
|
|
30
|
if ($self->utf8_mode) { |
|
258
|
2
|
|
|
|
|
9
|
$text =~ s/^\xEF\xBB\xBF//; |
|
259
|
|
|
|
|
|
|
} |
|
260
|
|
|
|
|
|
|
else { |
|
261
|
6
|
|
|
|
|
27
|
$text =~ s/^\x{FEFF}//; |
|
262
|
|
|
|
|
|
|
} |
|
263
|
8
|
|
|
|
|
19
|
$self->{first_chunk}++; |
|
264
|
|
|
|
|
|
|
} |
|
265
|
71
|
|
|
|
|
100
|
my $tag = $self->{tag}; |
|
266
|
71
|
100
|
100
|
|
|
290
|
if (!$tag && $text =~ /\S/) { |
|
267
|
|
|
|
|
|
|
# Normal text means start of body |
|
268
|
5
|
|
|
|
|
36
|
$self->eof; |
|
269
|
5
|
|
|
|
|
22
|
return; |
|
270
|
|
|
|
|
|
|
} |
|
271
|
66
|
100
|
|
|
|
433
|
return if $tag ne 'title'; |
|
272
|
7
|
|
|
|
|
39
|
$self->{'text'} .= $text; |
|
273
|
|
|
|
|
|
|
} |
|
274
|
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
BEGIN { |
|
276
|
2
|
50
|
|
2
|
|
79
|
*utf8_mode = sub { 1 } unless HTML::Entities::UNICODE_SUPPORT; |
|
|
0
|
|
|
|
|
0
|
|
|
277
|
|
|
|
|
|
|
} |
|
278
|
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
1; |
|
280
|
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
__END__ |