blib/lib/HTML/LinkExtractor.pm

filter: Statementstmt 90.9% 111 / 122 Branchbran 80.0% 56 / 70 Conditioncond 39.1% 9 / 23 MC/DCmcdc 18.1% 2 / 11 Subroutinesub 100% 14 / 14 Podpod 100% 4 / 4 totaltotal 80.3% 196 / 244 CC 43 SCAR 33.5
CC
43
Cov
82%
_parsola
43.2
new
4.0
strip
3.0
CRAP 28.6
↑ Summary
1 package HTML::LinkExtractor;
2
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4
5 2 ▶use HTML::TokeParser 2; # use HTML::TokeParser::Simple 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use URI 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
8
9 2 ▶use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 $VERSION = '0.13';
11
12 ## The html tags which might have URLs
13 # the master list of tagolas and required attributes (to constitute a link)
14 2 ▶use vars qw( %TAGS );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 %TAGS = (
16 a => [qw( href )],
17 applet => [qw( archive code codebase src )],
18 area => [qw( href )],
19 base => [qw( href )],
20 bgsound => [qw( src )],
21 blockquote => [qw( cite )],
22 body => [qw( background )],
23 del => [qw( cite )],
24 div => [qw( src )], # IE likes it, but don't know where it's documented
25 embed => [qw( pluginspage pluginurl src )],
26 form => [qw( action )],
27 frame => [qw( src longdesc )],
28 iframe => [qw( src )],
29 ilayer => [qw( background src )],
30 img => [qw( dynsrc longdesc lowsrc src usemap )],
31 input => [qw( dynsrc lowsrc src )],
32 ins => [qw( cite )],
33 isindex => [qw( action )], # real oddball
34 layer => [qw( src )],
35 link => [qw( src href )],
36 object => [qw( archive classid code codebase data usemap )],
37 q => [qw( cite )],
38 script => [qw( src )], # HTML::Tagset has 'for' ~ it's WRONG!
39 sound => [qw( src )],
40 table => [qw( background )],
41 td => [qw( background )],
42 th => [qw( background )],
43 tr => [qw( background )],
44 ## the exotic cases
45 meta => undef,
46 '!doctype' => [qw( url )], # is really a process instruction
47 );
48
49 ## tags which contain <.*?> STUFF TO GET </\w+>
50 2 ▶use vars qw( @TAGS_IN_NEED );
Subroutine BEGIN: called CC=1 SCAR=0.0
51 1 @TAGS_IN_NEED = qw(
52 a
53 blockquote
54 del
55 ins
56 q
57 );
58
59 2 ▶use vars qw( @VALID_URL_ATTRIBUTES );
Subroutine BEGIN: called CC=1 SCAR=0.0
60 1 @VALID_URL_ATTRIBUTES = qw(
61 action
62 archive
63 background
64 cite
65 classid
66 code
67 codebase
68 data
69 dynsrc
70 href
71 longdesc
72 lowsrc
73 pluginspage
74 pluginurl
75 src
76 usemap
77 );
78
79
80 5 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
81 5 my($class, $cb, $base, $strip) = @_;
82 5 my $self = bless {}, $class;
83
84
85 5 ▶ $self->{_cb} = $cb if defined $cb;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cb 2 3 5
86 5 ▶ $self->{_base} = URI->new($base) if defined $base;
Branch: 1 branch
BranchTrueFalseTotal
if defined $base 3 2 5
87 5 ▶ $self->strip( $strip || 0 );
Condition100%2/2
$strip || 0
l!l
41
MC/DC100%1/1
$strip || 0
$strip
Truth table100%2/2
A
A= $strip
Aresult
11
00
88
89 5 return $self;
90 }
91
92 17 ▶sub strip {
Subroutine strip: called CC=3 SCAR=11.0
Pod documented
93 17 my( $self, $on ) = @_;
94 17 ▶ return $self->{_strip} unless defined $on;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $on 11 6 17
95 6 ▶ return $self->{_strip} = $on ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$on ? : 5 1 6
96 }
97
98 ## $p= HTML::TokeParser->new($filename || FILEHANDLE ||\$filecontents); # ## $p= HTML::TokeParser::Simple->new($filename || FILEHANDLE ||\$filecontents);
99
100 5 ▶sub parse {
Subroutine parse: called CC=2 SCAR=7.1
Pod documented
101 5 my( $this, $hmmm ) = @_;
102 5 my $tp = new HTML::TokeParser( $hmmm ); # my $tp = new HTML::TokeParser::Simple( $hmmm );
103
104 5 ▶ unless($tp) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tp) 0 5 5
105 0 croak qq[ Couldn't create a HTML::TokeParser object: $!]; # croak qq[ Couldn't create a HTML::TokeParser::Simple object: $!];
106 }
107
108 5 $this->{_tp} = $tp;
109
110 5 $this->_parsola();
111 5 return();
112 }
113
114 5 ▶sub _parsola {
Subroutine _parsola: called CC=36 SCAR=37.7
115 5 my $self = shift;
116
117 ## a stack of links for keeping track of TEXT
118 ## which is all of "<a href>text</a>"
119 5 my @TEXT = ();
120 5 $self->{_LINKS} = [];
121
122
123 # ["S", $tag, $attr, $attrseq, $text]
124 # ["E", $tag, $text]
125 # ["T", $text, $is_data]
126 # ["C", $text]
127 # ["D", $text]
128 # ["PI", $token0, $text]
129
130 5 ▶ while (my $T = $self->{_tp}->get_token() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $T = $self->{"_tp"}->get_token) 51 5 56
131 51 my $NL; #NewLink
132 51 my $Tag = $T->[1]; # my $Tag = $T->return_tag;
133 51 my $got_TAGS_IN_NEED=0;
134 ## Start tag?
135 51 ▶ if($T->[0] eq 'S' ) { # if($T->is_start_tag) {
Branch: 4 branches
BranchTrueFalseTotal
if ($T->[0] eq 'S') { } 15 36 51
elsif ($T->[0] eq 'T') { } 26 10 36
elsif ($T->[0] eq 'D') { } 1 9 10
elsif ($T->[0] eq 'E') { } 9 0 9
136 15 ▶ next unless exists $TAGS{$Tag};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $HTML::LinkExtractor::TAGS{$Tag} 1 14 15
137
138 ## Do we have a tag for which we want to capture text?
139 14 $got_TAGS_IN_NEED = 0;
140 14 $got_TAGS_IN_NEED = grep { /^\Q$Tag\E$/i } @TAGS_IN_NEED;
141
142 ## then check to see if we got things besides META :)
143 14 ▶ if(defined $TAGS{ $Tag }) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $HTML::LinkExtractor::TAGS{$Tag}) { } 13 1 14
elsif ($Tag eq 'meta') { } 1 0 1
144
145 13 for my $Btag(@{$TAGS{$Tag}}) {
146 ## and we check if they do have one with a value
147 25 ▶ if(exists $T->[2]->{ $Btag }) { # if(exists $T->return_attr()->{ $Btag }) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $T->[2]{$Btag}) 13 12 25
148
149 13 $NL = $T->[2]; # $NL = $T->return_attr();
150 ## TAGS_IN_NEED are tags in deed (start capturing the <a>STUFF</a>)
151 13 ▶ if($got_TAGS_IN_NEED) {
Branch: 1 branch
BranchTrueFalseTotal
if ($got_TAGS_IN_NEED) 9 4 13
152 9 push @TEXT, $NL;
153 9 $NL->{_TEXT} = "";
154 }
155 }
156 }
157 }elsif($Tag eq 'meta') {
158 1 $NL = $T->[2]; # $NL = $T->return_attr();
159
160 1 ▶ if(defined $$NL{content} and length $$NL{content} and (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $NL->{"content"} and length $NL->{"content"} and defined $NL->{"http-equiv"} && $NL->{"http-equiv"} =~ /refresh/i || defined $NL->{"name"} && $NL->{"name"} =~ /refresh/i) 1 0 1
Condition26%4/15
defined $NL->{"content"} and length $NL->{"content"} and defined $NL->{"http-equiv"} && $NL->{"http-equiv"} =~ /refresh/i || defined $NL->{"name"} && $NL->{"name"} =~ /refresh/i
!ll&&!rl&&r
001
defined $NL->{"content"} and length $NL->{"content"}
!ll&&!rl&&r
001
defined $NL->{"http-equiv"} and $NL->{"http-equiv"} =~ /refresh/i or defined $NL->{"name"} and $NL->{"name"} =~ /refresh/i
l!l&&r!l&&!r
100
defined $NL->{"http-equiv"} and $NL->{"http-equiv"} =~ /refresh/i
!ll&&!rl&&r
001
defined $NL->{"name"} and $NL->{"name"} =~ /refresh/i
!ll&&!rl&&r
000
MC/DC0%0/6
defined $NL->{"content"} and length $NL->{"content"} and defined $NL->{"http-equiv"} && $NL->{"http-equiv"} =~ /refresh/i || defined $NL->{"name"} && $NL->{"name"} =~ /refresh/i
defined $NL->{"content"} length $NL->{"content"} defined $NL->{"http-equiv"} $NL->{"http-equiv"} =~ /refresh/i defined $NL->{"name"} $NL->{"name"} =~ /refresh/i
Truth table11%1/9
A and B and C and D or E and F
A= defined $NL->{"content"}B= length $NL->{"content"}C= defined $NL->{"http-equiv"}D= $NL->{"http-equiv"} =~ /refresh/iE= defined $NL->{"name"}F= $NL->{"name"} =~ /refresh/i
ABCDEFresult
0XXXXX0
10XXXX0
110X0X0
110X100
11100X0
1110100
1111XX1
110X111
1110111
161 defined $$NL{'http-equiv'} && $$NL{'http-equiv'} =~ /refresh/i
162 or
163 defined $$NL{'name'} && $$NL{'name'} =~ /refresh/i
164 ) ) {
165
166 1 my( $timeout, $url ) = split m{;\s*?URL=}i, $$NL{content},2;
167 1 my $base = $self->{_base};
168 1 ▶ $$NL{url} = URI->new_abs( $url, $base ) if $base;
Branch: 1 branch
BranchTrueFalseTotal
if $base 0 1 1
169 1 ▶ $$NL{url} = $url unless exists $$NL{url};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $NL->{"url"} 1 0 1
170 1 ▶ $$NL{timeout} = $timeout if $timeout;
Branch: 1 branch
BranchTrueFalseTotal
if $timeout 1 0 1
171 }
172 }
173
174 ## In case we got nested tags
175 14 ▶ if(@TEXT) {
Branch: 1 branch
BranchTrueFalseTotal
if (@TEXT) 12 2 14
176 12 $TEXT[-1]->{_TEXT} .= $T->[-1] ; # $TEXT[-1]->{_TEXT} .= $T->as_is;
177 }
178
179 ## Text?
180 }elsif($T->[0] eq 'T' ) { # }elsif($T->is_text) {
181 26 ▶ $TEXT[-1]->{_TEXT} .= $T->[-2] if @TEXT; # $TEXT[-1]->{_TEXT} .= $T->as_is if @TEXT;
Branch: 1 branch
BranchTrueFalseTotal
if @TEXT 9 17 26
182 ## Declaration?
183 }elsif($T->[0] eq 'D' ) { # }elsif($T->is_declaration) {
184 ## We look at declarations, to get anly custom .dtd's (tis linky)
185 1 my $text = $T->[-1] ; # my $text = $T->as_is;
186 1 ▶ if( $text =~ m{ SYSTEM \s \" ( [^\"]* ) \" > $ }ix ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ / SYSTEM \s \" ( [^\"]* ) \" > $ /xi) 1 0 1
187 1 $NL = { raw => $text, url => $1, tag => '!doctype' };
188 }
189 ## End tag?
190 }elsif($T->[0] eq 'E' ){ # }elsif($T->is_end_tag){
191 ## these be ignored (maybe not in between <a...></a> tags
192 ## unless we're stacking (bug #5723)
193 9 ▶ if(@TEXT and exists $TAGS{$Tag}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@TEXT and exists $HTML::LinkExtractor::TAGS{$Tag}) 9 0 9
Condition33%1/3
@TEXT and exists $HTML::LinkExtractor::TAGS{$Tag}
!ll&&!rl&&r
009
MC/DC0%0/2
@TEXT and exists $HTML::LinkExtractor::TAGS{$Tag}
@TEXT exists $HTML::LinkExtractor::TAGS{$Tag}
Truth table33%1/3
A and B
A= @TEXTB= exists $HTML::LinkExtractor::TAGS{$Tag}
ABresult
0X0
100
111
194 9 $TEXT[-1]->{_TEXT} .= $T->[-1] ; # $TEXT[-1]->{_TEXT} .= $T->as_is;
195 9 my $pop = pop @TEXT;
196 9 ▶ $TEXT[-1]->{_TEXT} .= $pop->{_TEXT} if @TEXT;
Branch: 1 branch
BranchTrueFalseTotal
if @TEXT 1 8 9
197 9 ▶ $pop->{_TEXT} = _stripHTML( \$pop->{_TEXT} ) if $self->strip;
Branch: 1 branch
BranchTrueFalseTotal
if $self->strip 4 5 9
198 9 ▶ $self->{_cb}->($self, $pop) if exists $self->{_cb};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"_cb"} 2 7 9
199 }
200 }
201
202 50 ▶ if(defined $NL) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $NL) 15 35 50
203 15 $$NL{tag} = $Tag;
204
205 15 my $base = $self->{_base};
206
207 15 for my $at( @VALID_URL_ATTRIBUTES ) {
208 240 ▶ if( exists $$NL{$at} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $NL->{$at}) 13 227 240
209 13 ▶ $$NL{$at} = URI->new_abs( $$NL{$at}, $base) if $base;
Branch: 1 branch
BranchTrueFalseTotal
if $base 5 8 13
210 }
211 }
212
213 15 ▶ if(exists $self->{_cb}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'_cb'}) { } 3 12 15
214 3 ▶ $self->{_cb}->($self, $NL ) if not $got_TAGS_IN_NEED or not @TEXT; #bug#5470
Branch: 1 branch
BranchTrueFalseTotal
unless $got_TAGS_IN_NEED and @TEXT 1 2 3
Condition66%2/3
$got_TAGS_IN_NEED and @TEXT
!ll&&!rl&&r
102
MC/DC50%1/2
$got_TAGS_IN_NEED and @TEXT
$got_TAGS_IN_NEED @TEXT
Truth table66%2/3
A and B
A= $got_TAGS_IN_NEEDB= @TEXT
ABresult
0X0
100
111
215 } else {
216 12 push @{$self->{_LINKS}}, $NL;
217 }
218 }
219 }## endof while (my $token = $p->get_token)
220
221 5 undef $self->{_tp};
222 5 return();
223 }
224
225 5 ▶sub links {
Subroutine links: called CC=1 SCAR=0.0
Pod documented
226 5 my $self = shift;
227 ## just like HTML::LinkExtor's
228 5 return $self->{_LINKS};
229 }
230
231
232 4 ▶sub _stripHTML {
Subroutine _stripHTML: called CC=2 SCAR=8.1
233 4 my $HtmlRef = shift;
234 4 my $tp = new HTML::TokeParser( $HtmlRef ); # my $tp = new HTML::TokeParser::Simple( $HtmlRef );
235 4 my $t = $tp->get_token(); # MUST BE A START TAG (@TAGS_IN_NEED)
236 # otherwise it ain't come from LinkExtractor
237 4 ▶ if($t->[0] eq 'S' ) { # if($t->is_start_tag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t->[0] eq 'S') { } 4 0 4
238 4 return $tp->get_trimmed_text( '/'.$t->[1] ); # return $tp->get_trimmed_text( '/'.$t->return_tag );
239 } else {
240 0 require Data::Dumper;
241 0 local $Data::Dumper::Indent=1;
242 0 die " IMPOSSIBLE!!!! ",
243 Data::Dumper::Dumper(
244 '$HtmlRef',$HtmlRef,
245 '$t', $t,
246 );
247 }
248 }
249
250 1 1;
251
252 package main;
253
254 1 ▶unless(caller()) {
Branch: 1 branch
BranchTrueFalseTotal
unless (caller()) 1 0 1
255 1 require Data::Dumper;
256 1 ▶ if(@ARGV) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV) { } 0 1 1
257 0 for my $file( @ARGV ) {
258 0 ▶ if( -e $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file) { } 0 0 0
259 0 my $LX = new HTML::LinkExtractor( );
260 0 $LX->parse( $file );
261 0 print Data::Dumper::Dumper($LX->links);
262 0 undef $LX;
263 } else {
264 0 warn "The file `$file' doesn't exist\n";
265 }
266 }
267
268 } else {
269
270 1 my $INPUT = q{
271 COUNT THEM BOYS AND GIRLS, LINKS OUTGHT TO HAVE 9 ELEMENTS.
272
273 1 <!DOCTYPE HTML SYSTEM "http://www.w3.org/DTD/HTML4-strict.dtd">
274 2 <meta HTTP-EQUIV="Refresh" CONTENT="5; URL=http://www.foo.com/foo.html">
275 3 <base href="http://perl.org">
276 4 <a href="http://www.perlmonks.org">Perlmonks.org</a>
277 <p>
278
279 5 <a href="#BUTTER" href="#SCOTCH">
280 hello there
281 6 <img src="#AND" src="#PEANUTS">
282 7 <a href="#butter"> now </a>
283 </a>
284
285 8 <q CITE="http://www.shakespeare.com/">To be or not to be.</q>
286 9 <blockquote CITE="http://www.stonehenge.com/merlyn/">
287 Just Another Perl Hacker,
288 </blockquote>
289 };
290
291 1 my $LX = new HTML::LinkExtractor();
292 1 $LX->parse(\$INPUT);
293
294 1 print scalar(@{$LX->links()})." we GOT\n";
295 1 print Data::Dumper::Dumper( $LX->links() );
296 }
297
298 }
299
300 __END__
↑ Summary