line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# |
2
|
|
|
|
|
|
|
# This file is part of HTML-Zoom-Parser-HTML-Parser |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# This software is copyright (c) 2013 by Matthew Phillips. |
5
|
|
|
|
|
|
|
# |
6
|
|
|
|
|
|
|
# This is free software; you can redistribute it and/or modify it under |
7
|
|
|
|
|
|
|
# the same terms as the Perl 5 programming language system itself. |
8
|
|
|
|
|
|
|
# |
9
|
|
|
|
|
|
|
package HTML::Zoom::Parser::HTML::Parser; |
10
|
|
|
|
|
|
|
{ |
11
|
|
|
|
|
|
|
$HTML::Zoom::Parser::HTML::Parser::VERSION = '1.130810'; |
12
|
|
|
|
|
|
|
} |
13
|
|
|
|
|
|
|
# ABSTRACT: Glue to power HTML::Zoom with HTML::Parser |
14
|
|
|
|
|
|
|
|
15
|
14
|
|
|
14
|
|
153136
|
use strictures 1; |
|
14
|
|
|
|
|
106
|
|
|
14
|
|
|
|
|
440
|
|
16
|
14
|
|
|
14
|
|
1027
|
use base qw(HTML::Zoom::SubObject); |
|
14
|
|
|
|
|
25
|
|
|
14
|
|
|
|
|
1988
|
|
17
|
|
|
|
|
|
|
|
18
|
14
|
|
|
14
|
|
21489
|
use HTML::TokeParser; |
|
14
|
|
|
|
|
184903
|
|
|
14
|
|
|
|
|
476
|
|
19
|
14
|
|
|
14
|
|
130
|
use HTML::Entities; |
|
14
|
|
|
|
|
31
|
|
|
14
|
|
|
|
|
8888
|
|
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub html_to_events { |
23
|
81
|
|
|
81
|
0
|
201940
|
my ($self, $text) = @_; |
24
|
81
|
|
|
|
|
127
|
my @events; |
25
|
|
|
|
|
|
|
_toke_parser($text => sub { |
26
|
1073
|
|
|
1073
|
|
2157
|
push @events, $_[0]; |
27
|
81
|
|
|
|
|
431
|
}); |
28
|
81
|
|
|
|
|
1614
|
return \@events; |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub html_to_stream { |
32
|
23
|
|
|
23
|
0
|
61427
|
my ($self, $text) = @_; |
33
|
23
|
|
|
|
|
202
|
return $self->_zconfig->stream_utils |
34
|
23
|
|
|
|
|
77
|
->stream_from_array(@{$self->html_to_events($text)}); |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub _toke_parser { |
38
|
81
|
|
|
81
|
|
140
|
my ($text, $handler) = @_; |
39
|
|
|
|
|
|
|
|
40
|
81
|
50
|
|
|
|
395
|
my $parser = HTML::TokeParser->new(\$text) or return $!; |
41
|
|
|
|
|
|
|
# HTML::Parser downcases by default |
42
|
|
|
|
|
|
|
|
43
|
81
|
|
|
|
|
11784
|
while (my $token = $parser->get_token) { |
44
|
1041
|
|
|
|
|
18171
|
my $type = shift @$token; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
# we break down what we emit to stream handler by type |
47
|
|
|
|
|
|
|
# start tag |
48
|
1041
|
100
|
|
|
|
2321
|
if ($type eq 'S') { |
49
|
313
|
|
|
|
|
724
|
my ($tag, $attr, $attrseq, $text) = @$token; |
50
|
313
|
|
|
|
|
456
|
my $in_place = delete $attr->{'/'}; # val will be '/' if in place |
51
|
313
|
100
|
|
|
|
589
|
$attrseq = [ grep { $_ ne '/' } @$attrseq ] if $in_place; |
|
33
|
|
|
|
|
106
|
|
52
|
313
|
100
|
|
|
|
701
|
if (substr($tag, -1) eq '/') { |
53
|
1
|
|
|
|
|
2
|
$in_place = '/'; |
54
|
1
|
|
|
|
|
2
|
chop $tag; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
313
|
|
|
|
|
1932
|
$handler->({ |
58
|
|
|
|
|
|
|
type => 'OPEN', |
59
|
|
|
|
|
|
|
name => $tag, |
60
|
|
|
|
|
|
|
attrs => $attr, |
61
|
|
|
|
|
|
|
is_in_place_close => $in_place, |
62
|
|
|
|
|
|
|
attr_names => $attrseq, |
63
|
|
|
|
|
|
|
raw => $text, |
64
|
|
|
|
|
|
|
}); |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
# if attr '/' exists, assume an inplace close, and emit a CLOSE as well |
67
|
313
|
100
|
|
|
|
803
|
if ($in_place) { |
68
|
32
|
|
|
|
|
135
|
$handler->({ |
69
|
|
|
|
|
|
|
type => 'CLOSE', |
70
|
|
|
|
|
|
|
name => $tag, |
71
|
|
|
|
|
|
|
raw => '', # don't emit $text for raw, match builtin behavior |
72
|
|
|
|
|
|
|
is_in_place_close => 1, |
73
|
|
|
|
|
|
|
}); |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
# end tag |
78
|
1041
|
100
|
|
|
|
2087
|
if ($type eq 'E') { |
79
|
281
|
|
|
|
|
506
|
my ($tag, $text) = @$token; |
80
|
281
|
|
|
|
|
1063
|
$handler->({ |
81
|
|
|
|
|
|
|
type => 'CLOSE', |
82
|
|
|
|
|
|
|
name => $tag, |
83
|
|
|
|
|
|
|
raw => $text, |
84
|
|
|
|
|
|
|
# is_in_place_close => 1 for br/> ?? |
85
|
|
|
|
|
|
|
}); |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
# text |
89
|
1041
|
100
|
|
|
|
2272
|
if ($type eq 'T') { |
90
|
446
|
|
|
|
|
790
|
my ($text, $is_data) = @$token; |
91
|
446
|
|
|
|
|
1408
|
$handler->({ |
92
|
|
|
|
|
|
|
type => 'TEXT', |
93
|
|
|
|
|
|
|
raw => $text |
94
|
|
|
|
|
|
|
}); |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
# comment |
98
|
1041
|
50
|
|
|
|
1804
|
if ($type eq 'C') { |
99
|
0
|
|
|
|
|
0
|
my ($text) = @$token; |
100
|
0
|
|
|
|
|
0
|
$handler->({ |
101
|
|
|
|
|
|
|
type => 'SPECIAL', |
102
|
|
|
|
|
|
|
raw => $text |
103
|
|
|
|
|
|
|
}); |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
# declaration |
107
|
1041
|
100
|
|
|
|
2023
|
if ($type eq 'D') { |
108
|
1
|
|
|
|
|
3
|
my ($text) = @$token; |
109
|
1
|
|
|
|
|
8
|
$handler->({ |
110
|
|
|
|
|
|
|
type => 'SPECIAL', |
111
|
|
|
|
|
|
|
raw => $text |
112
|
|
|
|
|
|
|
}); |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
# process instructions |
116
|
1041
|
50
|
|
|
|
4371
|
if ($type eq 'PI') { |
117
|
0
|
|
|
|
|
0
|
my ($token0, $text) = @$token; |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
81
|
|
|
81
|
0
|
156289
|
sub html_escape { encode_entities($_[1]) } |
123
|
|
|
|
|
|
|
|
124
|
0
|
|
|
0
|
0
|
|
sub html_unescape { decode_entities($_[1]) } |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
1; |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
__END__ |