Transform/using/HTML/Parser.pm

filter: Statementstmt 100% 138 / 138 Branchbran 80.0% 32 / 40 Conditioncond 90.6% 29 / 32 MC/DCmcdc 75.0% 12 / 16 Subroutinesub 100% 31 / 31 Podpod 100% 5 / 5 totaltotal 94.2% 247 / 262 CC 32 SCAR 19.4
CC
32
Cov
95%
transform_function_wrapper
16.1
__ANON__
11.0
__ANON__
3.0
CRAP 7.0
« Transform.pm ↑ Summary Transform/using/CSS/RegExp.pm »
1 package URL::Transform::using::HTML::Parser;
2  
3 =head1 NAME
4  
5 URL::Transform::using::HTML::Parser - HTML::Parse parsing of the html/xml for url transformation
6  
7 =head1 SYNOPSIS
8  
9 my $urlt = URL::Transform::using::HTML::Parser->new(
10 'output_function' => sub { $output .= "@_" },
11 'transform_function' => sub { return (join '|', @_) },
12 );
13 $urlt->parse_file($Bin.'/data/URL-Transform-01.html');
14  
15 print "and this is the output: ", $output;
16  
17  
18 =head1 DESCRIPTION
19  
20 Using this module you can performs an url transformation on the HTML/XML documents.
21  
22 This module is used by L<URL::Transform>.
23  
24 The url matching algorithm is taken from L<HTML::Parser>/eg/hrefsub example
25 script.
26  
27 =cut
28  
29 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 2 our $VERSION = '0.01';
33  
34 2 ▶use HTML::Parser ();
Subroutine BEGIN: called CC=1 SCAR=0.0
35 2 ▶use Carp::Clan;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 2 ▶use English '$EVAL_ERROR';
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 2 ▶use URL::Transform ();
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40  
41 # Construct a hash of tag names that may have links.
42 2 my $_link_tags = URL::Transform::link_tags();
43 2 my $_js_attributes = URL::Transform::js_attributes();
44  
45  
46 2 ▶use base 'Class::Accessor::Fast';
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 =head1 PROPERTIES
49  
50 output_function
51 transform_function
52 parser_for
53  
54 _html_parser
55  
56 =cut
57  
58 2 __PACKAGE__->mk_accessors(qw{
59 output_function
60 transform_function
61 parser_for
62  
63 _html_parser
64 });
65  
66 =head1 METHODS
67  
68 =cut
69  
70  
71 =head2 new
72  
73 Object constructor.
74  
75 Requires:
76  
77 output_function
78 transform_function
79  
80 Optional:
81  
82 parser_for
83  
84 =cut
85  
86 5 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
87 5 my $class = shift;
88  
89 5 my $self = $class->SUPER::new({ @_ });
90  
91 5 my $output_function = $self->output_function;
92 5 my $transform_function = $self->transform_function;
93
94 5 ▶ croak 'pass print function'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $output_function eq "CODE" 0 5 5
95 if not (ref $output_function eq 'CODE');
96
97 5 ▶ croak 'pass transform url function'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $transform_function eq "CODE" 0 5 5
98 if not (ref $transform_function eq 'CODE');
99  
100 5 ▶ $transform_function = sub { $self->transform_function_wrapper(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
101
102 5 my $html_parser = HTML::Parser->new(api_version => 3);
103  
104 # The default is to print everything as it is.
105 $html_parser->handler(
106 15 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
107 15 $output_function->(@_);
108 5 }, "text"
109 );
110
111 # cleanup current tag on every end tag
112 # should work fine for our purpouse as we are not interrested in nested tags
113 5 my $current_tag = '';
114 $html_parser->handler(
115 47 ▶ end => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
116 47 $current_tag = '';
117 47 my $text = shift;
118
119 # rename </noscript> to </span> in case javascript is removed
120 47 ▶ if ($self->parser_for->('application/x-javascript') eq 'Remove') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->parser_for->("application/x-javascript") eq "Remove") 47 0 47
121 47 ▶ $text = '</span>' if $text =~ m{^</noscript}i;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ m[^</noscript]i 4 43 47
122 }
123
124 47 $output_function->($text);
125 5 }, "text"
126 );
127
128 # Links inside the text of the tag (just <style> for the moment)
129 $html_parser->handler(
130 95 ▶ text => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
131 95 my $text = shift;
132 95 ▶ if ($_link_tags->{$current_tag} and $_link_tags->{$current_tag}->{''}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_link_tags->{$current_tag} and $_link_tags->{$current_tag}{""}) 3 92 95
Condition100%3/3
$_link_tags->{$current_tag} and $_link_tags->{$current_tag}{""}
!ll&&!rl&&r
15773
MC/DC100%2/2
$_link_tags->{$current_tag} and $_link_tags->{$current_tag}{""}
$_link_tags->{$current_tag} $_link_tags->{$current_tag}{""}
Truth table100%3/3
A and B
A= $_link_tags->{$current_tag}B= $_link_tags->{$current_tag}{""}
ABresult
0X0
100
111
133 3 $text = $transform_function->(
134 'tag_name' => $current_tag,
135 'url' => $text,
136 );
137 }
138
139 95 $output_function->($text);
140 5 }, "text"
141 );
142
143 # All links are found in start tags. This handler will evaluate
144 # &edit for each link attribute found.
145 $html_parser->handler(
146 56 ▶ start => sub {
Subroutine __ANON__: called CC=11 SCAR=24.0
147 56 my($tagname, $pos, $text) = @_;
148
149 # note down current tag for 'text' handler if not tag with /> at the end
150 56 ▶ $current_tag = $tagname
Branch: 1 branch
BranchTrueFalseTotal
unless $text =~ m[/\s?>$] 47 9 56
151 if $text !~ m{/\s?>$};
152
153 # rename <noscript> to <span> in case javascript is removed
154 56 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($tagname eq "noscript" and $self->parser_for->("application/x-javascript") eq "Remove") 4 52 56
Condition66%2/3
$tagname eq "noscript" and $self->parser_for->("application/x-javascript") eq "Remove"
!ll&&!rl&&r
5204
MC/DC50%1/2
$tagname eq "noscript" and $self->parser_for->("application/x-javascript") eq "Remove"
$tagname eq "noscript" $self->parser_for->("application/x-javascript") eq "Remove"
Truth table66%2/3
A and B
A= $tagname eq "noscript"B= $self->parser_for->("application/x-javascript") eq "Remove"
ABresult
0X0
100
111
155 ($tagname eq 'noscript')
156 and ($self->parser_for->('application/x-javascript') eq 'Remove')
157 ) {
158 4 $text =~ s{^<noscript}{<span}i;
159 }
160
161 56 ▶ while (4 <= @$pos) {
Branch: 1 branch
BranchTrueFalseTotal
if (4 <= @$pos) 44 56 100
162 # use attribute sets from right to left
163 # to avoid invalidating the offsets
164 # when replacing the values
165 44 my($k_offset, $k_len, $v_offset, $v_len) =
166 splice(@$pos, -4);
167 44 my $attrname = lc(substr($text, $k_offset, $k_len));
168  
169 # skip if empty attribute
170 44 ▶ next unless $v_offset; # 0 v_offset means no value
Branch: 1 branch
BranchTrueFalseTotal
unless $v_offset 9 35 44
171
172 # skip if not links attr
173 # skip if tag is not associated with any link attributes
174 # and the attribute is not a general attribute (like style or onclick)
175 # skip if tag is associated with link attributes but not the current one
176 # and the attribute is not a general attribute (like style or onclick)
177 35 my $link_tags = $_link_tags->{$tagname};
178 next if (
179 ((not $link_tags) or (not $link_tags->{$attrname}))
180 35 ▶ and (not $_link_tags->{''}->{$attrname})
Branch: 1 branch
BranchTrueFalseTotal
unless $link_tags and $link_tags->{$attrname} or $_link_tags->{""}{$attrname} 12 23 35
Condition100%6/6
$link_tags and $link_tags->{$attrname} or $_link_tags->{""}{$attrname}
l!l&&r!l&&!r
16712
$link_tags and $link_tags->{$attrname}
!ll&&!rl&&r
61316
MC/DC100%3/3
$link_tags and $link_tags->{$attrname} or $_link_tags->{""}{$attrname}
$link_tags $link_tags->{$attrname} $_link_tags->{""}{$attrname}
Truth table100%5/5
A and B or C
A= $link_tagsB= $link_tags->{$attrname}C= $_link_tags->{""}{$attrname}
ABCresult
11X1
0X11
1011
0X00
1000
181 );
182
183 23 my $v = substr($text, $v_offset, $v_len);
184
185 # remove attribute quotes
186 23 $v =~ s/^([\'\"])(.*)\1$/$2/;
187
188 # get the characted that was used to quote
189 23 ▶ my $quote_char = $1 || '';
Condition100%2/2
$1 || ''
l!l
221
MC/DC100%1/1
$1 || ''
$1
Truth table100%2/2
A
A= $1
Aresult
11
00
190
191 # call for url transforming
192 23 my $new_v = $transform_function->(
193 'tag_name' => $tagname,
194 'attribute_name' => $attrname,
195 'url' => $v,
196 );
197
198 # skip if not change
199 23 ▶ next if $new_v eq $v;
Branch: 1 branch
BranchTrueFalseTotal
if $new_v eq $v 1 22 23
200
201 # replace the attribute value with a new one
202 22 substr($text, $v_offset, $v_len) = $quote_char.$new_v.$quote_char;
203 }
204 56 $output_function->($text);
205 },
206 5 "tagname, tokenpos, text"
207 );
208
209 5 $self->_html_parser($html_parser);
210  
211 5 return $self;
212 }
213  
214  
215 =head2 transform_function_wrapper
216  
217 Wrapper for transform function that can handle special cases of url-s
218 when hidden inside an attribute. Like meta refresh:
219  
220 <meta http-equiv="Refresh" content="0;http://someserver/" />
221  
222 =cut
223  
224 26 ▶sub transform_function_wrapper {
Subroutine transform_function_wrapper: called CC=16 SCAR=27.8
Pod documented
225 26 my $self = shift;
226 26 my %args = @_;
227
228 26 my $tag_name = $args{'tag_name'};
229 26 my $attribute_name = $args{'attribute_name'};
230
231 # special handling
232 26 my $url = $args{'url'};
233 26 ▶ if ($tag_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag_name) 26 0 26
234 26 my $parser;
235 # if <meta ...> tag
236 26 ▶ if ($tag_name eq 'meta') {
Branch: 3 branches
BranchTrueFalseTotal
if ($tag_name eq 'meta') { } 3 23 26
elsif (not $attribute_name and $tag_name eq 'style' or $attribute_name and $attribute_name eq 'style') { } 5 18 23
elsif (not $attribute_name and $tag_name eq 'script' or $attribute_name and exists $_js_attributes->{$attribute_name}) { } 5 13 18
237 3 $parser = $self->parser_for->('text/html/meta-content');
238 3 ▶ return $url if not defined $parser;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $parser 0 3 3
239 }
240 # elsif <style> tag or a tag with 'style' attribute
241 ▶ elsif (
Condition100%9/9
not $attribute_name and $tag_name eq "style" or $attribute_name and $attribute_name eq "style"
l!l&&r!l&&!r
2318
not $attribute_name and $tag_name eq "style"
!ll&&!rl&&r
2012
$attribute_name and $attribute_name eq "style"
!ll&&!rl&&r
1173
MC/DC75%3/4
not $attribute_name and $tag_name eq "style" or $attribute_name and $attribute_name eq "style"
not $attribute_name $tag_name eq "style" $attribute_name $attribute_name eq "style"
Truth table57%4/7
not(A) and B or C and D
A= not $attribute_nameB= $tag_name eq "style"C= $attribute_nameD= $attribute_name eq "style"
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
242 (
243 not $attribute_name
244 and ($tag_name eq 'style')
245 )
246 or (
247 $attribute_name
248 and ($attribute_name eq 'style')
249 )
250 ) {
251 5 $parser = $self->parser_for->('text/css');
252 5 ▶ return $url if not defined $parser;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $parser 0 5 5
253 }
254 # elsif <script> tag or some tag with javascript attribute (onload, onclick, on...)
255 ▶ elsif (
Condition77%7/9
not $attribute_name and $tag_name eq "script" or $attribute_name and exists $_js_attributes->{$attribute_name}
l!l&&r!l&&!r
1413
not $attribute_name and $tag_name eq "script"
!ll&&!rl&&r
1701
$attribute_name and exists $_js_attributes->{$attribute_name}
!ll&&!rl&&r
0134
MC/DC50%2/4
not $attribute_name and $tag_name eq "script" or $attribute_name and exists $_js_attributes->{$attribute_name}
not $attribute_name $tag_name eq "script" $attribute_name exists $_js_attributes->{$attribute_name}
Truth table42%3/7
not(A) and B or C and D
A= not $attribute_nameB= $tag_name eq "script"C= $attribute_nameD= exists $_js_attributes->{$attribute_name}
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
256 (
257 not $attribute_name
258 and ($tag_name eq 'script')
259 )
260 or (
261 $attribute_name
262 and exists $_js_attributes->{$attribute_name}
263 )
264 ) {
265 5 $parser = $self->parser_for->('application/x-javascript');
266 5 ▶ return $url if not defined $parser;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $parser 0 5 5
267 }
268
269 26 ▶ if (defined $parser) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $parser) 13 13 26
270 # FIXME should not reconstruct the parser every time
271 13 $parser = 'URL::Transform::using::'.$parser;
272 1 ▶ eval 'use '.$parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
273 13 ▶ die $EVAL_ERROR if $EVAL_ERROR;
Branch: 1 branch
BranchTrueFalseTotal
if $URL::Transform::using::HTML::Parser::EVAL_ERROR 0 13 13
274 13 my $output = '';
275 $parser = $parser->new(
276 'transform_function' => $self->transform_function,
277 13 ▶ 'output_function' => sub { $output .= "@_"; },
Subroutine __ANON__: called CC=1 SCAR=0.0
278 13 );
279 13 $parser->parse_string($url);
280
281 13 return $output;
282 }
283 }
284  
285 13 return $self->transform_function->(%args);
286 }
287  
288  
289 =head2 parse_string($string)
290  
291 Submit document as a string for parsing.
292  
293 =cut
294  
295 2 ▶sub parse_string {
Subroutine parse_string: called CC=1 SCAR=0.0
Pod documented
296 2 my $self = shift;
297
298 2 $self->_html_parser->parse(@_);
299 }
300  
301  
302 =head2 parse_chunk($chunk)
303  
304 Submit chunk of a document for parsing.
305  
306 =cut
307  
308 6 ▶sub parse_chunk {
Subroutine parse_chunk: called CC=1 SCAR=0.0
Pod documented
309 6 my $self = shift;
310
311 6 $self->_html_parser->parse(@_);
312 }
313  
314  
315 =head2 parse_file($file_name)
316  
317 Submit file for parsing.
318  
319 =cut
320  
321 5 ▶sub parse_file {
Subroutine parse_file: called CC=1 SCAR=0.0
Pod documented
322 5 my $self = shift;
323
324 5 $self->_html_parser->parse_file(@_);
325 }
326  
327  
328 2 1;
329  
330  
331 __END__
« Transform.pm ↑ Summary Transform/using/CSS/RegExp.pm »