blib/lib/HTML/HeadParser.pm

filter: Statementstmt 95.5% 85 / 89 Branchbran 82.7% 48 / 58 Conditioncond 64.2% 9 / 14 MC/DCmcdc 44.4% 4 / 9 Subroutinesub 90.9% 10 / 11 Podpod 71.4% 5 / 7 totaltotal 85.6% 161 / 188 CC 36 SCAR 24.4
CC
36
Cov
88%
start
21.6
text
7.0
flush_text
6.0
CRAP 11.5
↑ Summary blib/lib/HTML/PullParser.pm »
1 package HTML::HeadParser;
2  
3 =head1 NAME
4  
5 HTML::HeadParser - Parse <HEAD> 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 <title>....</title>
14 $p->header('Content-Base') # to access <base href="http://...">
15 $p->header('Foo') # to access <meta http-equiv="Foo" content="...">
16 $p->header('X-Meta-Author') # to access <meta name="author" content="...">
17 $p->header('X-Meta-Charset') # to access <meta charset="...">
18  
19 =head1 DESCRIPTION
20  
21 The C<HTML::HeadParser> is a specialized (and lightweight)
22 C<HTML::Parser> that will only parse the E<lt>HEAD>...E<lt>/HEAD>
23 section of an HTML document. The parse() method
24 will return a FALSE value as soon as some E<lt>BODY> element or body
25 text are found, and should not be called again after this.
26  
27 Note that the C<HTML::HeadParser> 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<HTML::HeadParser> keeps a reference to a header object, and the
32 parser will update this header object as the various elements of the
33 E<lt>HEAD> 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<Content-Base> header is initialized from the E<lt>base
41 href="..."> element.
42  
43 =item Title:
44  
45 The I<Title> header is initialized from the E<lt>title>...E<lt>/title>
46 element.
47  
48 =item Isindex:
49  
50 The I<Isindex> header will be added if there is a E<lt>isindex>
51 element in the E<lt>head>. The header value is initialized from the
52 I<prompt> attribute if it is present. If no I<prompt> attribute is
53 given it will have '?' as the value.
54  
55 =item X-Meta-Foo:
56  
57 All E<lt>meta> elements containing a C<name> attribute will result in
58 headers using the prefix C<X-Meta-> appended with the value of the
59 C<name> attribute as the name of the header, and the value of the
60 C<content> attribute as the pushed header value.
61  
62 E<lt>meta> elements containing a C<http-equiv> attribute will result
63 in headers as in above, but without the C<X-Meta-> prefix in the
64 header name.
65  
66 E<lt>meta> elements containing a C<charset> attribute will result in
67 an C<X-Meta-Charset> header, using the value of the C<charset>
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 2 require HTML::Parser;
87 2 our @ISA = qw(HTML::Parser);
88  
89 2 ▶use HTML::Entities ();
Subroutine BEGIN: called CC=1 SCAR=0.0
90  
91 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
92 2 our $DEBUG;
93 #$DEBUG = 1;
94 2 our $VERSION = '3.87';
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<HTTP::Headers> class. Normally it will be
103 of some class that is a or delegates to the C<HTTP::Headers> class.
104  
105 If no $header is given C<HTML::HeadParser> will create an
106 C<HTTP::Headers> object by itself (initially empty).
107  
108 =cut
109  
110 sub new
111 8 ▶{
Subroutine new: called CC=2 SCAR=6.9
Pod documented
112 8 my($class, $header) = @_;
113 8 ▶ unless ($header) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($header) 1 7 8
114 1 require HTTP::Headers;
115 1 $header = HTTP::Headers->new;
116 }
117  
118 8 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 $self->{'header'} = $header;
125 8 $self->{'tag'} = ''; # name of active element that takes textual content
126 8 $self->{'text'} = ''; # the accumulated text associated with the element
127 8 $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-E<gt>header-E<gt>header($key)>.
138  
139 =cut
140  
141 sub header
142 14 ▶{
Subroutine header: called CC=2 SCAR=6.9
Pod documented
143 14 my $self = shift;
144 14 ▶ return $self->{'header'} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 13 14
145 13 $self->{'header'}->header(@_);
146 }
147  
148 sub as_string # legacy
149 3 ▶{
Subroutine as_string: called CC=1 SCAR=0.0
Pod undocumented
150 3 my $self = shift;
151 3 $self->{'header'}->as_string;
152 }
153  
154 sub flush_text # internal
155 13 ▶{
Subroutine flush_text: called CC=6 SCAR=17.9
Pod undocumented
156 13 my $self = shift;
157 13 my $tag = $self->{'tag'};
158 13 my $text = $self->{'text'};
159 13 $text =~ s/^\s+//;
160 13 $text =~ s/\s+$//;
161 13 $text =~ s/\s+/ /g;
162 13 ▶ print "FLUSH $tag => '$text'\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::HeadParser::DEBUG 0 13 13
163 13 ▶ if ($tag eq 'title') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq "title") 7 6 13
164 7 my $decoded;
165 7 ▶ $decoded = utf8::decode($text) if $self->utf8_mode && defined &utf8::decode;
Branch: 1 branch
BranchTrueFalseTotal
if $self->utf8_mode and defined &utf8::decode 2 5 7
Condition66%2/3
$self->utf8_mode and defined &utf8::decode
!ll&&!rl&&r
502
MC/DC50%1/2
$self->utf8_mode and defined &utf8::decode
$self->utf8_mode defined &utf8::decode
Truth table66%2/3
A and B
A= $self->utf8_modeB= defined &utf8::decode
ABresult
0X0
100
111
166 7 HTML::Entities::decode($text);
167 7 ▶ utf8::encode($text) if $decoded;
Branch: 1 branch
BranchTrueFalseTotal
if $decoded 2 5 7
168 7 $self->{'header'}->push_header(Title => $text);
169 }
170 13 $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 <HEAD>...</HEAD>. Also note that the
175 # <HEAD> tags themselves might be missing:
176 #
177 # <!ENTITY % head.content "TITLE & ISINDEX? & BASE? & STYLE? &
178 # SCRIPT* & META* & LINK*">
179 #
180 # <!ELEMENT HEAD O O (%head.content)>
181 #
182 # From HTML 4.01:
183 #
184 # <!ENTITY % head.misc "SCRIPT|STYLE|META|LINK|OBJECT">
185 # <!ENTITY % head.content "TITLE & BASE?">
186 # <!ELEMENT HEAD O O (%head.content;) +(%head.misc;)>
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 47 ▶{
Subroutine start: called CC=19 SCAR=30.7
Pod documented
195 47 my($self, $tag, $attr) = @_; # $attr is reference to a HASH
196 47 ▶ print "START[$tag]\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::HeadParser::DEBUG 0 47 47
197 47 ▶ $self->flush_text if $self->{'tag'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"tag"} 3 44 47
198 47 ▶ if ($tag eq 'meta') {
Branch: 6 branches
BranchTrueFalseTotal
if ($tag eq 'meta') { } 20 27 47
elsif ($tag eq 'base') { } 4 23 27
elsif ($tag eq 'isindex') { } 0 23 23
elsif ($tag =~ /^(?:title|noscript|object|command)$/) { } 13 10 23
elsif ($tag eq 'link') { } 6 4 10
elsif ($tag eq 'head' or $tag eq 'html') { } 4 0 4
199 20 my $key = $attr->{'http-equiv'};
200 20 ▶ if (!defined($key) || !length($key)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $key and length $key) 13 7 20
Condition66%2/3
defined $key and length $key
!ll&&!rl&&r
1307
MC/DC50%1/2
defined $key and length $key
defined $key length $key
Truth table66%2/3
A and B
A= defined $keyB= length $key
ABresult
0X0
100
111
201 13 ▶ if ($attr->{name}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($attr->{'name'}) { } 10 3 13
elsif ($attr->{'charset'}) { } 3 0 3
202 10 $key = "X-Meta-\u$attr->{name}";
203 } elsif ($attr->{charset}) { # HTML 5 <meta charset="...">
204 3 $key = "X-Meta-Charset";
205 3 $self->{header}->push_header($key => $attr->{charset});
206 3 return;
207 } else {
208 0 return;
209 }
210 }
211 17 $key =~ s/:/-/g;
212 17 $self->{'header'}->push_header($key => $attr->{content});
213 } elsif ($tag eq 'base') {
214 4 ▶ return unless exists $attr->{href};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $attr->{"href"} 0 4 4
215 4 (my $base = $attr->{href}) =~ s/^\s+//; $base =~ s/\s+$//; # HTML5
216 4 $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 ▶ $self->{'header'}->push_header(Isindex => $attr->{prompt} || '?');
Condition0%0/2
$attr->{'prompt'} || '?'
l!l
00
MC/DC0%0/1
$attr->{'prompt'} || '?'
$attr->{'prompt'}
Truth table0%0/2
A
A= $attr->{'prompt'}
Aresult
11
00
221 } elsif ($tag =~ /^(?:title|noscript|object|command)$/) {
222 # Just remember tag. Initialize header when we see the end tag.
223 13 $self->{'tag'} = $tag;
224 } elsif ($tag eq 'link') {
225 6 ▶ return unless exists $attr->{href};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $attr->{"href"} 0 6 6
226 # <link href="http:..." rel="xxx" rev="xxx" title="xxx">
227 6 my $href = delete($attr->{href});
228 6 $href =~ s/^\s+//; $href =~ s/\s+$//; # HTML5
229 6 my $h_val = "<$href>";
230 6 for (sort keys %{$attr}) {
231 16 ▶ next if $_ eq "/"; # XHTML junk
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "/" 3 13 16
232 13 $h_val .= qq(; $_="$attr->{$_}");
233 }
234 6 $self->{'header'}->push_header(Link => $h_val);
235 ▶ } elsif ($tag eq 'head' || $tag eq 'html') {
Condition66%2/3
$tag eq "head" or $tag eq "html"
l!l&&r!l&&!r
220
MC/DC0%0/2
$tag eq "head" or $tag eq "html"
$tag eq "head" $tag eq "html"
Truth table66%2/3
A or B
A= $tag eq "head"B= $tag eq "html"
ABresult
1X1
011
000
236 # ignore
237 } else {
238 # stop parsing
239 0 $self->eof;
240 }
241 }
242  
243 sub end
244 12 ▶{
Subroutine end: called CC=4 SCAR=13.9
Pod documented
245 12 my($self, $tag) = @_;
246 12 ▶ print "END[$tag]\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::HeadParser::DEBUG 0 12 12
247 12 ▶ $self->flush_text if $self->{'tag'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"tag"} 10 2 12
248 12 ▶ $self->eof if $tag eq 'head';
Branch: 1 branch
BranchTrueFalseTotal
if $tag eq "head" 1 11 12
249 }
250  
251 sub text
252 71 ▶{
Subroutine text: called CC=7 SCAR=19.5
Pod documented
253 71 my($self, $text) = @_;
254 71 ▶ print "TEXT[$text]\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::HeadParser::DEBUG 0 71 71
255 71 ▶ unless ($self->{first_chunk}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"first_chunk"}) 8 63 71
256 # drop Unicode BOM if found
257 8 ▶ if ($self->utf8_mode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->utf8_mode) { } 2 6 8
258 2 $text =~ s/^\xEF\xBB\xBF//;
259 }
260 else {
261 6 $text =~ s/^\x{FEFF}//;
262 }
263 8 $self->{first_chunk}++;
264 }
265 71 my $tag = $self->{tag};
266 71 ▶ if (!$tag && $text =~ /\S/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $tag and $text =~ /\S/) 5 66 71
Condition100%3/3
not $tag and $text =~ /\S/
!ll&&!rl&&r
16505
MC/DC100%2/2
not $tag and $text =~ /\S/
not $tag $text =~ /\S/
Truth table100%3/3
not(A) and B
A= not $tagB= $text =~ /\S/
ABresult
0X0
100
111
267 # Normal text means start of body
268 5 $self->eof;
269 5 return;
270 }
271 66 ▶ return if $tag ne 'title';
Branch: 1 branch
BranchTrueFalseTotal
if $tag ne "title" 59 7 66
272 7 $self->{'text'} .= $text;
273 }
274  
275 2 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=9.2
276 0 ▶ *utf8_mode = sub { 1 } unless HTML::Entities::UNICODE_SUPPORT;
Subroutine __ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
BranchTrueFalseTotal
unless &HTML::Entities::UNICODE_SUPPORT() 0 2 2
277 }
278  
279 2 1;
280  
281 __END__
↑ Summary blib/lib/HTML/PullParser.pm »