line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#!/usr/bin/env perl |
2
|
|
|
|
|
|
|
package HTML::RewriteAttributes::Resources; |
3
|
6
|
|
|
6
|
|
4363
|
use strict; |
|
6
|
|
|
|
|
12
|
|
|
6
|
|
|
|
|
198
|
|
4
|
6
|
|
|
6
|
|
31
|
use warnings; |
|
6
|
|
|
|
|
13
|
|
|
6
|
|
|
|
|
164
|
|
5
|
6
|
|
|
6
|
|
32
|
use base 'HTML::RewriteAttributes'; |
|
6
|
|
|
|
|
9
|
|
|
6
|
|
|
|
|
3360
|
|
6
|
6
|
|
|
6
|
|
730203
|
use URI; |
|
6
|
|
|
|
|
52012
|
|
|
6
|
|
|
|
|
5274
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
my %rewritable_attrs = ( |
11
|
|
|
|
|
|
|
bgsound => { src => 1 }, |
12
|
|
|
|
|
|
|
body => { background => 1 }, |
13
|
|
|
|
|
|
|
img => { src => 1 }, |
14
|
|
|
|
|
|
|
input => { src => 1 }, |
15
|
|
|
|
|
|
|
table => { background => 1 }, |
16
|
|
|
|
|
|
|
td => { background => 1 }, |
17
|
|
|
|
|
|
|
th => { background => 1 }, |
18
|
|
|
|
|
|
|
tr => { background => 1 }, |
19
|
|
|
|
|
|
|
); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub _rewrite { |
22
|
6
|
|
|
6
|
|
17
|
my $self = shift; |
23
|
6
|
|
|
|
|
16
|
my $html = shift; |
24
|
6
|
|
|
|
|
13
|
my $cb = shift; |
25
|
6
|
|
|
|
|
22
|
my %args = @_; |
26
|
|
|
|
|
|
|
|
27
|
6
|
|
|
|
|
54
|
$self->{rewrite_inline_css_cb} = $args{inline_css}; |
28
|
6
|
|
|
|
|
18
|
$self->{rewrite_inline_imports} = $args{inline_imports}; |
29
|
6
|
|
|
|
|
18
|
$self->{rewrite_inline_imports_seen} = {}; |
30
|
|
|
|
|
|
|
|
31
|
6
|
|
|
|
|
51
|
$self->SUPER::_rewrite($html, $cb); |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub _should_rewrite { |
35
|
19
|
|
|
19
|
|
45
|
my ($self, $tag, $attr) = @_; |
36
|
|
|
|
|
|
|
|
37
|
19
|
|
100
|
|
|
175
|
return ( $rewritable_attrs{$tag} || {} )->{$attr}; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub _invoke_callback { |
41
|
7
|
|
|
7
|
|
13
|
my $self = shift; |
42
|
7
|
|
|
|
|
16
|
my ($tag, $attr, $value) = @_; |
43
|
|
|
|
|
|
|
|
44
|
7
|
|
|
|
|
25
|
return $self->{rewrite_callback}->($value, tag => $tag, attr => $attr, rewriter => $self); |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub _start_tag { |
48
|
39
|
|
|
39
|
|
66
|
my $self = shift; |
49
|
39
|
|
|
|
|
76
|
my ($tag, $attr, $attrseq, $text) = @_; |
50
|
|
|
|
|
|
|
|
51
|
39
|
100
|
|
|
|
106
|
if ($self->{rewrite_inline_css_cb}) { |
52
|
25
|
50
|
66
|
|
|
171
|
if ($tag eq 'link' and defined $attr->{type} and $attr->{type} eq 'text/css' and defined $attr->{href}) { |
|
|
|
66
|
|
|
|
|
|
|
|
33
|
|
|
|
|
53
|
7
|
|
|
|
|
33
|
my $content = $self->_import($attr->{href}); |
54
|
7
|
50
|
|
|
|
38
|
if (defined $content) { |
55
|
7
|
|
|
|
|
24
|
$content = $self->_handle_imports($content, $attr->{href}); |
56
|
7
|
|
|
|
|
19
|
$self->{rewrite_html} .= "\n\n"; |
59
|
7
|
|
|
|
|
73
|
return; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
} |
62
|
18
|
50
|
66
|
|
|
77
|
if ($tag eq 'style' and defined $attr->{type} and $attr->{type} eq 'text/css') { |
|
|
|
66
|
|
|
|
|
63
|
2
|
|
|
|
|
5
|
$self->{rewrite_look_for_style} = 1; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
32
|
|
|
|
|
134
|
$self->SUPER::_start_tag(@_); |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub _default { |
71
|
97
|
|
|
97
|
|
176
|
my ($self, $tag, $attrs, $text) = @_; |
72
|
97
|
100
|
|
|
|
243
|
if (delete $self->{rewrite_look_for_style}) { |
73
|
2
|
|
|
|
|
5
|
$text = $self->_handle_imports($text, '.'); |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
97
|
|
|
|
|
332
|
$self->SUPER::_default($tag, $attrs, $text); |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub _handle_imports { |
80
|
19
|
|
|
19
|
|
25
|
my $self = shift; |
81
|
19
|
|
|
|
|
25
|
my $content = shift; |
82
|
19
|
|
|
|
|
24
|
my $base = shift; |
83
|
|
|
|
|
|
|
|
84
|
19
|
100
|
|
|
|
63
|
return $content if !$self->{rewrite_inline_imports}; |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
# here we both try to preserve comments *and* ignore any @import |
87
|
|
|
|
|
|
|
# statements that are in comments |
88
|
10
|
|
|
|
|
73
|
$content =~ s{ |
89
|
|
|
|
|
|
|
( /\* .*? \*/ ) |
90
|
|
|
|
|
|
|
| |
91
|
|
|
|
|
|
|
(//[^\n]*) |
92
|
|
|
|
|
|
|
| |
93
|
|
|
|
|
|
|
\@import \s* " ([^"]+) " \s* ; |
94
|
|
|
|
|
|
|
}{ |
95
|
25
|
100
|
|
|
|
154
|
defined($1) ? $1 |
|
|
100
|
|
|
|
|
|
96
|
|
|
|
|
|
|
: defined($2) ? $2 |
97
|
|
|
|
|
|
|
: $self->_import($self->_absolutify($3, $base)) |
98
|
|
|
|
|
|
|
}xsmeg; |
99
|
|
|
|
|
|
|
|
100
|
10
|
|
|
|
|
43
|
return $content; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
sub _absolutify { |
104
|
7
|
|
|
7
|
|
9
|
my $self = shift; |
105
|
7
|
|
|
|
|
12
|
my $path = shift; |
106
|
7
|
|
|
|
|
9
|
my $base = shift; |
107
|
|
|
|
|
|
|
|
108
|
7
|
|
|
|
|
31
|
my $uri = URI->new($path); |
109
|
7
|
50
|
|
|
|
12966
|
unless (defined $uri->scheme) { |
110
|
7
|
|
|
|
|
302
|
$uri = $uri->abs($base); |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
7
|
|
|
|
|
1213
|
return $uri->as_string; |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
sub _import { |
117
|
14
|
|
|
14
|
|
61
|
my $self = shift; |
118
|
14
|
|
|
|
|
26
|
my $path = shift; |
119
|
|
|
|
|
|
|
|
120
|
14
|
100
|
|
|
|
76
|
return '' if $self->{rewrite_inline_imports_seen}{$path}++; |
121
|
|
|
|
|
|
|
|
122
|
10
|
|
|
|
|
72
|
my $content = "\n/* $path */\n" |
123
|
|
|
|
|
|
|
. $self->{rewrite_inline_css_cb}->($path); |
124
|
10
|
|
|
|
|
97
|
return $self->_handle_imports($content, $path); |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
1; |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
__END__ |