File Coverage

blib/lib/HTML/Parser.pm
Criterion Covered Total %
statement 53 55 96.3
branch 18 22 81.8
condition 5 6 83.3
subroutine 8 8 100.0
pod 3 5 60.0
total 87 96 90.6


line stmt bran cond sub pod time code
1             package HTML::Parser;
2              
3 49     49   278105 use strict;
  49         95  
  49         45546  
4              
5             our $VERSION = '3.85';
6              
7             require HTML::Entities;
8              
9             require XSLoader;
10             XSLoader::load('HTML::Parser', $VERSION);
11              
12             sub new
13             {
14 129     129 1 11988351 my $class = shift;
15 129         299 my $self = bless {}, $class;
16 129         491 return $self->init(@_);
17             }
18              
19              
20             sub init
21             {
22 129     129 0 207 my $self = shift;
23 129         1181 $self->_alloc_pstate;
24              
25 129         446 my %arg = @_;
26 129   66     771 my $api_version = delete $arg{api_version} || (@_ ? 3 : 2);
27 129 100       350 if ($api_version >= 4) {
28 1         6 require Carp;
29 1         160 Carp::croak("API version $api_version not supported " .
30             "by HTML::Parser $VERSION");
31             }
32              
33 128 100       310 if ($api_version < 3) {
34             # Set up method callbacks compatible with HTML-Parser-2.xx
35 47         436 $self->handler(text => "text", "self,text,is_cdata");
36 47         163 $self->handler(end => "end", "self,tagname,text");
37 47         141 $self->handler(process => "process", "self,token0,text");
38 47         185 $self->handler(start => "start",
39             "self,tagname,attr,attrseq,text");
40              
41             $self->handler(comment =>
42             sub {
43 59     59   2612 my($self, $tokens) = @_;
44 59         122 for (@$tokens) {
45 61         110 $self->comment($_);
46             }
47 47         260 }, "self,tokens");
48              
49             $self->handler(declaration =>
50             sub {
51 10     10   199 my $self = shift;
52 10         49 $self->declaration(substr($_[0], 2, -1));
53 47         179 }, "self,text");
54             }
55              
56 128 100       340 if (my $h = delete $arg{handlers}) {
57 3 50       13 $h = {@$h} if ref($h) eq "ARRAY";
58 3         10 while (my($event, $cb) = each %$h) {
59 3         38 $self->handler($event => @$cb);
60             }
61             }
62              
63             # In the end we try to assume plain attribute or handler
64 128         584 while (my($option, $val) = each %arg) {
65 101 100       609 if ($option =~ /^(\w+)_h$/) {
    50          
66 57         931 $self->handler($1 => @$val);
67             }
68             elsif ($option =~ /^(text|start|end|process|declaration|comment)$/) {
69 0         0 require Carp;
70 0         0 Carp::croak("Bad constructor option '$option'");
71             }
72             else {
73 44         294 $self->$option($val);
74             }
75             }
76              
77 128         626 return $self;
78             }
79              
80              
81             sub parse_file
82             {
83 18     18 1 62731 my($self, $file) = @_;
84 18         34 my $opened;
85 18 100 100     92 if (!ref($file) && ref(\$file) ne "GLOB") {
86             # Assume $file is a filename
87 10         27 local(*F);
88 10 50       409 open(F, "<", $file) || return undef;
89 10         31 binmode(F); # should we? good for byte counts
90 10         19 $opened++;
91 10         45 $file = *F;
92             }
93 18         32 my $chunk = '';
94 18         431 while (read($file, $chunk, 512)) {
95 6069 100       40462 $self->parse($chunk) || last;
96             }
97 18 100       249 close($file) if $opened;
98 18         178 $self->eof;
99             }
100              
101              
102             sub netscape_buggy_comment # legacy
103             {
104 1     1 0 2560 my $self = shift;
105 1         7 require Carp;
106 1         156 Carp::carp("netscape_buggy_comment() is deprecated. " .
107             "Please use the strict_comment() method instead");
108 1         12 my $old = !$self->strict_comment;
109 1 50       3 $self->strict_comment(!shift) if @_;
110 1         4 return $old;
111             }
112              
113             # set up method stubs
114       18569 1   sub text { }
115             *start = \&text;
116             *end = \&text;
117             *comment = \&text;
118             *declaration = \&text;
119             *process = \&text;
120              
121             1;
122              
123             __END__