File Coverage

blib/lib/HTML/PullParser.pm
Criterion Covered Total %
statement 56 58 96.5
branch 18 22 81.8
condition 11 14 78.5
subroutine 5 6 83.3
pod 4 4 100.0
total 94 104 90.3


line stmt bran cond sub pod time code
1             package HTML::PullParser;
2              
3 2     2   7237 use strict;
  2         30  
  2         143  
4              
5             require HTML::Parser;
6             our @ISA = qw(HTML::Parser);
7             our $VERSION = '3.83';
8              
9 2     2   17 use Carp ();
  2         4  
  2         1365  
10              
11             sub new
12             {
13 11     11 1 432717 my($class, %cnf) = @_;
14              
15             # Construct argspecs for the various events
16 11         15 my %argspec;
17 11         24 for (qw(start end text declaration comment process default)) {
18 77         93 my $tmp = delete $cnf{$_};
19 77 100       117 next unless defined $tmp;
20 63         89 $argspec{$_} = $tmp;
21             }
22 11 50       25 Carp::croak("Info not collected for any events")
23             unless %argspec;
24              
25 11         17 my $file = delete $cnf{file};
26 11         18 my $doc = delete $cnf{doc};
27 11 50 66     35 Carp::croak("Can't parse from both 'doc' and 'file' at the same time")
28             if defined($file) && defined($doc);
29 11 50 66     54 Carp::croak("No 'doc' or 'file' given to parse from")
30             unless defined($file) || defined($doc);
31              
32             # Create object
33 11         17 $cnf{api_version} = 3;
34 11         54 my $self = $class->SUPER::new(%cnf);
35              
36 11         31 my $accum = $self->{pullparser_accum} = [];
37 11         29 while (my($event, $argspec) = each %argspec) {
38 63         267 $self->SUPER::handler($event => $accum, $argspec);
39             }
40              
41 11 100       22 if (defined $doc) {
42 5 100       19 $self->{pullparser_str_ref} = ref($doc) ? $doc : \$doc;
43 5         19 $self->{pullparser_str_pos} = 0;
44             }
45             else {
46 6 100 66     18 if (!ref($file) && ref(\$file) ne "GLOB") {
47 4         522 require IO::File;
48 4   100     7471 $file = IO::File->new($file, "r") || return;
49             }
50              
51 5         282 $self->{pullparser_file} = $file;
52             }
53 10         43 $self;
54             }
55              
56              
57             sub handler
58             {
59 0     0 1 0 Carp::croak("Can't set handlers for HTML::PullParser");
60             }
61              
62              
63             sub get_token
64             {
65 8296     8296 1 28870 my $self = shift;
66 8296   100     10102 while (!@{$self->{pullparser_accum}} && !$self->{pullparser_eof}) {
  8431         15704  
67 135 100       452 if (my $f = $self->{pullparser_file}) {
    50          
68             # must try to parse more from the file
69 8         8 my $buf;
70 8 100       108 if (read($f, $buf, 512)) {
71 5         361 $self->parse($buf);
72             } else {
73 3         23 $self->eof;
74 3         5 $self->{pullparser_eof}++;
75 3         17 delete $self->{pullparser_file};
76             }
77             }
78             elsif (my $sref = $self->{pullparser_str_ref}) {
79             # must try to parse more from the scalar
80 127         226 my $pos = $self->{pullparser_str_pos};
81 127         316 my $chunk = substr($$sref, $pos, 512);
82 127         11226 $self->parse($chunk);
83 127         382 $pos += length($chunk);
84 127 100       257 if ($pos < length($$sref)) {
85 122         276 $self->{pullparser_str_pos} = $pos;
86             }
87             else {
88 5         36 $self->eof;
89 5         17 $self->{pullparser_eof}++;
90 5         11 delete $self->{pullparser_str_ref};
91 5         12 delete $self->{pullparser_str_pos};
92             }
93             }
94             else {
95 0         0 die;
96             }
97             }
98 8296         10317 shift @{$self->{pullparser_accum}};
  8296         14844  
99             }
100              
101              
102             sub unget_token
103             {
104 9     9 1 2221 my $self = shift;
105 9         16 unshift @{$self->{pullparser_accum}}, @_;
  9         20  
106 9         19 $self;
107             }
108              
109             1;
110              
111              
112             __END__