line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#!/usr/bin/env perl |
2
|
|
|
|
|
|
|
package HTML::RewriteAttributes; |
3
|
11
|
|
|
11
|
|
22301
|
use strict; |
|
11
|
|
|
|
|
21
|
|
|
11
|
|
|
|
|
356
|
|
4
|
11
|
|
|
11
|
|
281
|
use warnings; |
|
11
|
|
|
|
|
20
|
|
|
11
|
|
|
|
|
432
|
|
5
|
11
|
|
|
11
|
|
58
|
use base 'HTML::Parser'; |
|
11
|
|
|
|
|
22
|
|
|
11
|
|
|
|
|
13061
|
|
6
|
11
|
|
|
11
|
|
87737
|
use Carp 'croak'; |
|
11
|
|
|
|
|
27
|
|
|
11
|
|
|
|
|
853
|
|
7
|
11
|
|
|
11
|
|
65
|
use HTML::Entities 'encode_entities'; |
|
11
|
|
|
|
|
19
|
|
|
11
|
|
|
|
|
8526
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our $VERSION = '0.05'; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub new { |
12
|
16
|
|
|
16
|
1
|
37
|
my $class = shift; |
13
|
16
|
|
|
|
|
195
|
return $class->SUPER::new( |
14
|
|
|
|
|
|
|
start_h => [ '_start_tag', "self,tagname,attr,attrseq,text" ], |
15
|
|
|
|
|
|
|
default_h => [ '_default', "self,tagname,attr,text" ], |
16
|
|
|
|
|
|
|
); |
17
|
|
|
|
|
|
|
} |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub rewrite { |
20
|
16
|
|
|
16
|
1
|
4378
|
my $self = shift; |
21
|
16
|
50
|
|
|
|
149
|
$self = $self->new if !ref($self); |
22
|
16
|
|
|
|
|
1544
|
$self->_rewrite(@_); |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub _rewrite { |
26
|
16
|
|
|
16
|
|
36
|
my $self = shift; |
27
|
16
|
|
|
|
|
34
|
my $html = shift; |
28
|
16
|
|
50
|
0
|
|
72
|
my $cb = shift || sub { $self->rewrite_resource(@_) }; |
|
0
|
|
|
|
|
0
|
|
29
|
|
|
|
|
|
|
|
30
|
16
|
|
|
|
|
123
|
$self->_begin_rewriting($cb); |
31
|
|
|
|
|
|
|
|
32
|
16
|
|
|
|
|
278
|
$self->parse($html); |
33
|
16
|
|
|
|
|
173
|
$self->eof; |
34
|
|
|
|
|
|
|
|
35
|
16
|
|
|
|
|
89
|
$self->_done_rewriting; |
36
|
|
|
|
|
|
|
|
37
|
16
|
|
|
|
|
209
|
return $self->{rewrite_html}; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub rewrite_resource { |
41
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
42
|
0
|
|
0
|
|
|
0
|
my $class = ref($self) || $self; |
43
|
|
|
|
|
|
|
|
44
|
0
|
|
|
|
|
0
|
my $error = "You must specify a callback to $class->rewrite"; |
45
|
0
|
0
|
|
|
|
0
|
$error .= " or define $class->rewrite_resource" if $class ne __PACKAGE__; |
46
|
0
|
|
|
|
|
0
|
croak "$error."; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub _begin_rewriting { |
50
|
16
|
|
|
16
|
|
38
|
my $self = shift; |
51
|
16
|
|
|
|
|
53
|
my $cb = shift; |
52
|
|
|
|
|
|
|
|
53
|
16
|
|
|
|
|
75
|
$self->{rewrite_html} = ''; |
54
|
16
|
|
|
|
|
67
|
$self->{rewrite_callback} = $cb; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
16
|
|
|
16
|
|
122
|
sub _done_rewriting { } |
58
|
|
|
|
|
|
|
|
59
|
15
|
|
|
15
|
|
39
|
sub _should_rewrite { 1 } |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub _start_tag { |
62
|
89
|
|
|
89
|
|
199
|
my ($self, $tag, $attrs, $attrseq, $text) = @_; |
63
|
|
|
|
|
|
|
|
64
|
89
|
|
|
|
|
183
|
$self->{rewrite_html} .= "<$tag"; |
65
|
|
|
|
|
|
|
|
66
|
89
|
|
|
|
|
188
|
for my $attr (@$attrseq) { |
67
|
94
|
100
|
|
|
|
615
|
next if $attr eq '/'; |
68
|
|
|
|
|
|
|
|
69
|
70
|
100
|
|
|
|
228
|
if ($self->_should_rewrite($tag, $attr)) { |
70
|
48
|
|
|
|
|
203
|
$attrs->{$attr} = $self->_invoke_callback($tag, $attr, $attrs->{$attr}); |
71
|
48
|
100
|
|
|
|
539
|
next if !defined($attrs->{$attr}); |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
64
|
|
|
|
|
258
|
$self->{rewrite_html} .= sprintf ' %s="%s"', |
75
|
|
|
|
|
|
|
$attr, |
76
|
|
|
|
|
|
|
encode_entities($attrs->{$attr}); |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
89
|
100
|
|
|
|
746
|
$self->{rewrite_html} .= ' /' if $attrs->{'/'}; |
80
|
89
|
|
|
|
|
747
|
$self->{rewrite_html} .= '>'; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub _default { |
84
|
235
|
|
|
235
|
|
443
|
my ($self, $tag, $attrs, $text) = @_; |
85
|
235
|
|
|
|
|
1863
|
$self->{rewrite_html} .= $text; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub _invoke_callback { |
89
|
41
|
|
|
41
|
|
59
|
my $self = shift; |
90
|
41
|
|
|
|
|
66
|
my ($tag, $attr, $value) = @_; |
91
|
|
|
|
|
|
|
|
92
|
41
|
|
|
|
|
132
|
return $self->{rewrite_callback}->($tag, $attr, $value); |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
1; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
__END__ |