line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package HTML::Packer; |
2
|
|
|
|
|
|
|
|
3
|
11
|
|
|
11
|
|
290567
|
use 5.008009; |
|
11
|
|
|
|
|
144
|
|
4
|
11
|
|
|
11
|
|
98
|
use strict; |
|
11
|
|
|
|
|
38
|
|
|
11
|
|
|
|
|
218
|
|
5
|
11
|
|
|
11
|
|
133
|
use warnings; |
|
11
|
|
|
|
|
44
|
|
|
11
|
|
|
|
|
249
|
|
6
|
11
|
|
|
11
|
|
87
|
use Carp; |
|
11
|
|
|
|
|
71
|
|
|
11
|
|
|
|
|
480
|
|
7
|
11
|
|
|
11
|
|
2737
|
use Regexp::RegGrp; |
|
11
|
|
|
|
|
27658
|
|
|
11
|
|
|
|
|
289
|
|
8
|
11
|
|
|
11
|
|
2984
|
use Digest::SHA qw(sha256_base64 sha384_base64 sha512_base64); |
|
11
|
|
|
|
|
17502
|
|
|
11
|
|
|
|
|
4775
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------- |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = '2.09'; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our @BOOLEAN_ACCESSORS = ( |
15
|
|
|
|
|
|
|
'remove_comments', |
16
|
|
|
|
|
|
|
'remove_comments_aggressive', |
17
|
|
|
|
|
|
|
'remove_newlines', |
18
|
|
|
|
|
|
|
'no_compress_comment', |
19
|
|
|
|
|
|
|
'html5', |
20
|
|
|
|
|
|
|
); |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
our @JAVASCRIPT_OPTS = ( 'clean', 'obfuscate', 'shrink', 'best' ); |
23
|
|
|
|
|
|
|
our @CSS_OPTS = ( 'minify', 'pretty' ); |
24
|
|
|
|
|
|
|
our @CSP_OPTS = ( 'sha256', 'sha384', 'sha512' ); |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
our $REQUIRED_JAVASCRIPT_PACKER = '1.002001'; |
27
|
|
|
|
|
|
|
our $REQUIRED_CSS_PACKER = '1.000001'; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
our @SAVE_SPACE_ELEMENTS = ( |
30
|
|
|
|
|
|
|
'a', 'abbr', 'acronym', 'address', 'b', 'bdo', 'big', 'button', 'cite', |
31
|
|
|
|
|
|
|
'del', 'dfn', 'em', 'font', 'i', 'input', 'ins', 'kbd', 'label', 'q', |
32
|
|
|
|
|
|
|
's', 'samp', 'select', 'small', 'strike', 'strong', 'sub', 'sup', 'u', 'var' |
33
|
|
|
|
|
|
|
); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
our @VOID_ELEMENTS = ( |
36
|
|
|
|
|
|
|
'area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input', |
37
|
|
|
|
|
|
|
'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr' |
38
|
|
|
|
|
|
|
); |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
# Some regular expressions are from HTML::Clean |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
our $COMMENT = '((?>\s*))()((?>\s*))'; |
43
|
|
|
|
|
|
|
our $COMMENT_SAFE = '((?>\s*))()((?>\s*))'; |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
our $PACKER_COMMENT = ''; |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
our $DOCTYPE = '<\!DOCTYPE[^>]*>'; |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
our $DONT_CLEAN = '(<\s*(pre|code|textarea|script|style)[^>]*>)(.*?)(<\s*\/\2[^>]*>)'; |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
our $WHITESPACES = [ |
52
|
|
|
|
|
|
|
{ |
53
|
|
|
|
|
|
|
regexp => qr/^\s*/s, |
54
|
|
|
|
|
|
|
replacement => '' |
55
|
|
|
|
|
|
|
}, |
56
|
|
|
|
|
|
|
{ |
57
|
|
|
|
|
|
|
regexp => qr/\s*$/s, |
58
|
|
|
|
|
|
|
replacement => '' |
59
|
|
|
|
|
|
|
}, |
60
|
|
|
|
|
|
|
{ |
61
|
|
|
|
|
|
|
regexp => '^\s*', |
62
|
|
|
|
|
|
|
replacement => '', |
63
|
|
|
|
|
|
|
modifier => 'm' |
64
|
|
|
|
|
|
|
}, |
65
|
|
|
|
|
|
|
{ |
66
|
|
|
|
|
|
|
regexp => '[^\S\n]*$', |
67
|
|
|
|
|
|
|
replacement => '', |
68
|
|
|
|
|
|
|
modifier => 'm' |
69
|
|
|
|
|
|
|
}, |
70
|
|
|
|
|
|
|
{ |
71
|
|
|
|
|
|
|
regexp => qr/(?<=>)[^<>]*(?=<)/sm, |
72
|
|
|
|
|
|
|
replacement => sub { |
73
|
|
|
|
|
|
|
my $match = $_[0]->{match}; |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
$match =~ s/[^\S\n]{2,}/ /sg; |
76
|
|
|
|
|
|
|
$match =~ s/\s*\n+\s*/\n/sg; |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
return $match; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
}, |
81
|
|
|
|
|
|
|
{ |
82
|
|
|
|
|
|
|
regexp => '<\s*(\/)?\s*', |
83
|
|
|
|
|
|
|
replacement => sub { |
84
|
|
|
|
|
|
|
return sprintf( '<%s', $_[0]->{submatches}->[0] ); |
85
|
|
|
|
|
|
|
}, |
86
|
|
|
|
|
|
|
modifier => 's' |
87
|
|
|
|
|
|
|
}, |
88
|
|
|
|
|
|
|
{ |
89
|
|
|
|
|
|
|
regexp => '\s*(\/)?\s*>', |
90
|
|
|
|
|
|
|
replacement => sub { |
91
|
|
|
|
|
|
|
return sprintf( '%s>', $_[0]->{submatches}->[0] ); |
92
|
|
|
|
|
|
|
}, |
93
|
|
|
|
|
|
|
modifier => 's' |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
]; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
our $NEWLINES_TAGS = [ |
98
|
|
|
|
|
|
|
{ |
99
|
|
|
|
|
|
|
regexp => '(\s*)(<\s*\/?\s*(?:' . join( '|', @SAVE_SPACE_ELEMENTS ) . ')\b[^>]*>)(\s*)', |
100
|
|
|
|
|
|
|
replacement => sub { |
101
|
|
|
|
|
|
|
return sprintf( '%s%s%s', $_[0]->{submatches}->[0] ? ' ' : '', $_[0]->{submatches}->[1], $_[0]->{submatches}->[2] ? ' ' : '' ); |
102
|
|
|
|
|
|
|
}, |
103
|
|
|
|
|
|
|
modifier => 'is' |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
]; |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
our $NEWLINES = [ |
108
|
|
|
|
|
|
|
{ |
109
|
|
|
|
|
|
|
regexp => '(.)\n(.)', |
110
|
|
|
|
|
|
|
replacement => sub { |
111
|
|
|
|
|
|
|
my ( $pre, $post ) = @{$_[0]->{submatches}}; |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
my $ret; |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
if ( $pre eq '>' and $post eq '<' ) { |
116
|
|
|
|
|
|
|
$ret = $pre . $post; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
elsif ( $pre eq '-' and $post =~ /[\w]/ ) { |
119
|
|
|
|
|
|
|
$ret = $pre . $post; |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
else { |
122
|
|
|
|
|
|
|
$ret = $pre . ' ' . $post; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
return $ret; |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
]; |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
our @REGGRPS = ( 'newlines', 'newlines_tags', 'whitespaces', 'void_elements' ); |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
our $GLOBAL_REGGRP = 'global'; |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
########################################################################################## |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
{ |
137
|
11
|
|
|
11
|
|
124
|
no strict 'refs'; |
|
11
|
|
|
|
|
75
|
|
|
11
|
|
|
|
|
7634
|
|
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
foreach my $field ( @BOOLEAN_ACCESSORS ) { |
140
|
|
|
|
|
|
|
next if defined *{ __PACKAGE__ . '::' . $field }{CODE}; |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
*{ __PACKAGE__ . '::' . $field} = sub { |
143
|
394
|
|
|
394
|
|
693
|
my ( $self, $value ) = @_; |
144
|
|
|
|
|
|
|
|
145
|
394
|
100
|
|
|
|
731
|
$self->{'_' . $field} = $value ? 1 : undef if ( defined( $value ) ); |
|
|
100
|
|
|
|
|
|
146
|
|
|
|
|
|
|
|
147
|
394
|
|
|
|
|
999
|
return $self->{'_' . $field}; |
148
|
|
|
|
|
|
|
}; |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
foreach my $reggrp ( @REGGRPS, $GLOBAL_REGGRP ) { |
152
|
|
|
|
|
|
|
next if defined *{ __PACKAGE__ . '::reggrp_' . $reggrp }{CODE}; |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
*{ __PACKAGE__ . '::reggrp_' . $reggrp } = sub { |
155
|
228
|
|
|
228
|
|
401
|
my ( $self ) = shift; |
156
|
|
|
|
|
|
|
|
157
|
228
|
|
|
|
|
989
|
return $self->{ '_reggrp_' . $reggrp }; |
158
|
|
|
|
|
|
|
}; |
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
} |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
sub do_javascript { |
163
|
83
|
|
|
83
|
1
|
205
|
my ( $self, $value ) = @_; |
164
|
|
|
|
|
|
|
|
165
|
83
|
100
|
|
|
|
213
|
if ( defined( $value ) ) { |
166
|
34
|
100
|
|
|
|
145
|
if ( grep( $value eq $_, @JAVASCRIPT_OPTS ) ) { |
|
|
100
|
|
|
|
|
|
167
|
31
|
|
|
|
|
140
|
$self->{_do_javascript} = $value; |
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
elsif ( ! $value ) { |
170
|
5
|
|
|
|
|
62
|
$self->{_do_javascript} = undef; |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
|
174
|
80
|
|
|
|
|
160
|
return $self->{_do_javascript}; |
175
|
|
|
|
|
|
|
} |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
sub do_stylesheet { |
178
|
57
|
|
|
57
|
1
|
117
|
my ( $self, $value ) = @_; |
179
|
|
|
|
|
|
|
|
180
|
57
|
100
|
|
|
|
134
|
if ( defined( $value ) ) { |
181
|
9
|
100
|
|
|
|
43
|
if ( grep( $value eq $_, @CSS_OPTS ) ) { |
|
|
100
|
|
|
|
|
|
182
|
6
|
|
|
|
|
27
|
$self->{_do_stylesheet} = $value; |
183
|
|
|
|
|
|
|
} |
184
|
|
|
|
|
|
|
elsif ( ! $value ) { |
185
|
2
|
|
|
|
|
15
|
$self->{_do_stylesheet} = undef; |
186
|
|
|
|
|
|
|
} |
187
|
|
|
|
|
|
|
} |
188
|
|
|
|
|
|
|
|
189
|
57
|
|
|
|
|
109
|
return $self->{_do_stylesheet}; |
190
|
|
|
|
|
|
|
} |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
sub do_csp { |
193
|
48
|
|
|
48
|
1
|
99
|
my ( $self, $value ) = @_; |
194
|
|
|
|
|
|
|
|
195
|
48
|
100
|
|
|
|
112
|
if ( defined( $value ) ) { |
196
|
8
|
100
|
|
|
|
35
|
if ( grep( $value eq $_, @CSP_OPTS ) ) { |
|
|
100
|
|
|
|
|
|
197
|
6
|
|
|
|
|
27
|
$self->{_do_csp} = $value; |
198
|
|
|
|
|
|
|
} |
199
|
|
|
|
|
|
|
elsif ( ! $value ) { |
200
|
2
|
|
|
|
|
16
|
$self->{_do_csp} = undef; |
201
|
|
|
|
|
|
|
} |
202
|
|
|
|
|
|
|
} |
203
|
|
|
|
|
|
|
|
204
|
48
|
|
|
|
|
82
|
return $self->{_do_csp}; |
205
|
|
|
|
|
|
|
} |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
# these variables are used in the closures defined in the init function |
208
|
|
|
|
|
|
|
# below - we have to use globals as using $self within the closures leads |
209
|
|
|
|
|
|
|
# to a reference cycle and thus memory leak, and we can't scope them to |
210
|
|
|
|
|
|
|
# the init method as they may change. they are set by the minify sub |
211
|
|
|
|
|
|
|
our $remove_comments; |
212
|
|
|
|
|
|
|
our $remove_comments_aggressive; |
213
|
|
|
|
|
|
|
our $remove_newlines; |
214
|
|
|
|
|
|
|
our $html5; |
215
|
|
|
|
|
|
|
our $do_javascript; |
216
|
|
|
|
|
|
|
our $do_stylesheet; |
217
|
|
|
|
|
|
|
our $do_csp; |
218
|
|
|
|
|
|
|
our $js_packer; |
219
|
|
|
|
|
|
|
our $css_packer; |
220
|
|
|
|
|
|
|
our %csp; |
221
|
|
|
|
|
|
|
our $reggrp_ws; |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
sub init { |
224
|
38
|
|
|
38
|
0
|
50659
|
my $class = shift; |
225
|
38
|
|
|
|
|
94
|
my $self = {}; |
226
|
|
|
|
|
|
|
|
227
|
38
|
|
|
|
|
84
|
bless( $self, $class ); |
228
|
|
|
|
|
|
|
|
229
|
38
|
|
|
|
|
143
|
$self->{whitespaces}->{reggrp_data} = $WHITESPACES; |
230
|
38
|
|
|
|
|
95
|
$self->{newlines}->{reggrp_data} = $NEWLINES; |
231
|
38
|
|
|
|
|
84
|
$self->{newlines_tags}->{reggrp_data} = $NEWLINES_TAGS; |
232
|
|
|
|
|
|
|
$self->{global}->{reggrp_data} = [ |
233
|
|
|
|
|
|
|
{ |
234
|
|
|
|
|
|
|
regexp => $DOCTYPE, |
235
|
|
|
|
|
|
|
replacement => sub { |
236
|
2
|
|
|
2
|
|
221
|
return ''; |
237
|
|
|
|
|
|
|
}, |
238
|
|
|
|
|
|
|
store => sub { |
239
|
2
|
|
|
2
|
|
21
|
my $doctype = $_[0]->{match}; |
240
|
|
|
|
|
|
|
|
241
|
2
|
|
|
|
|
17
|
$doctype =~ s/\s+/ /gsm; |
242
|
|
|
|
|
|
|
|
243
|
2
|
|
|
|
|
17
|
return $doctype; |
244
|
|
|
|
|
|
|
} |
245
|
|
|
|
|
|
|
}, |
246
|
|
|
|
|
|
|
{ |
247
|
|
|
|
|
|
|
# this is using a variable that won't be initialized until after we have |
248
|
|
|
|
|
|
|
# called ->minify so we endup calling ->init again (see FIXME) |
249
|
|
|
|
|
|
|
regexp => $remove_comments_aggressive ? $COMMENT : $COMMENT_SAFE, |
250
|
|
|
|
|
|
|
replacement => sub { |
251
|
|
|
|
|
|
|
return $remove_comments ? ( |
252
|
|
|
|
|
|
|
$remove_newlines ? ' ' : ( |
253
|
|
|
|
|
|
|
( $_[0]->{submatches}->[0] =~ /\n/s or $_[0]->{submatches}->[2] =~ /\n/s ) ? "\n" : '' |
254
|
|
|
|
|
|
|
) |
255
|
37
|
50
|
33
|
37
|
|
4137
|
) : ''; |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
256
|
|
|
|
|
|
|
}, |
257
|
|
|
|
|
|
|
store => sub { |
258
|
|
|
|
|
|
|
my $ret = $remove_comments ? '' : ( |
259
|
|
|
|
|
|
|
( ( not $remove_newlines and $_[0]->{submatches}->[0] =~ /\n/s ) ? "\n" : '' ) . |
260
|
|
|
|
|
|
|
$_[0]->{submatches}->[1] . |
261
|
37
|
100
|
66
|
37
|
|
502
|
( ( not $remove_newlines and $_[0]->{submatches}->[2] =~ /\n/s ) ? "\n" : '' ) |
|
|
100
|
100
|
|
|
|
|
|
|
100
|
|
|
|
|
|
262
|
|
|
|
|
|
|
); |
263
|
|
|
|
|
|
|
|
264
|
37
|
|
|
|
|
79
|
return $ret; |
265
|
|
|
|
|
|
|
} |
266
|
|
|
|
|
|
|
}, |
267
|
|
|
|
|
|
|
{ |
268
|
|
|
|
|
|
|
regexp => $DONT_CLEAN, |
269
|
|
|
|
|
|
|
replacement => sub { |
270
|
46
|
|
|
46
|
|
7944
|
return ''; |
271
|
|
|
|
|
|
|
}, |
272
|
|
|
|
|
|
|
store => sub { |
273
|
46
|
|
|
46
|
|
603
|
my ( $opening, undef, $content, $closing ) = @{$_[0]->{submatches}}; |
|
46
|
|
|
|
|
161
|
|
274
|
|
|
|
|
|
|
|
275
|
46
|
50
|
|
|
|
114
|
if ( $content ) { |
276
|
46
|
100
|
|
|
|
145
|
my $opening_script_re = '<\s*script' . ( $html5 ? '[^>]*>' : '[^>]*(?:java|ecma)script[^>]*>' ); |
277
|
46
|
100
|
|
|
|
124
|
my $opening_style_re = '<\s*style' . ( $html5 ? '[^>]*>' : '[^>]*text\/css[^>]*>' ); |
278
|
46
|
|
|
|
|
81
|
my $js_type_re = q{type=['"]((application|text)/){0,1}(x-){0,1}(java|ecma)script['"]}; |
279
|
|
|
|
|
|
|
|
280
|
46
|
100
|
66
|
|
|
852
|
if ( |
|
|
100
|
66
|
|
|
|
|
281
|
|
|
|
|
|
|
$opening =~ /$opening_script_re/i |
282
|
|
|
|
|
|
|
&& ( $opening =~ /$js_type_re/i || $opening !~ /type/i ) |
283
|
|
|
|
|
|
|
) { |
284
|
27
|
100
|
|
|
|
84
|
$opening =~ s/ type="(text\/)?(java|ecma)script"//i if ( $html5 ); |
285
|
|
|
|
|
|
|
|
286
|
27
|
100
|
66
|
|
|
116
|
if ( $js_packer and $do_javascript ) { |
287
|
18
|
|
|
|
|
118
|
$js_packer->minify( \$content, { compress => $do_javascript } ); |
288
|
|
|
|
|
|
|
|
289
|
18
|
100
|
|
|
|
19449
|
unless ( $html5 ) { |
290
|
16
|
|
|
|
|
56
|
$content = '/**/'; |
291
|
|
|
|
|
|
|
} |
292
|
|
|
|
|
|
|
} |
293
|
|
|
|
|
|
|
|
294
|
27
|
100
|
|
|
|
87
|
if ( $do_csp ) { |
295
|
11
|
|
|
11
|
|
109
|
no strict 'refs'; |
|
11
|
|
|
|
|
45
|
|
|
11
|
|
|
|
|
922
|
|
296
|
10
|
|
|
|
|
23
|
push @{ $csp{'script-src'} }, &{ "${do_csp}_base64" } ( $content ); |
|
9
|
|
|
|
|
18
|
|
|
9
|
|
|
|
|
81
|
|
297
|
|
|
|
|
|
|
} |
298
|
|
|
|
|
|
|
} |
299
|
|
|
|
|
|
|
elsif ( $opening =~ /$opening_style_re/i ) { |
300
|
11
|
100
|
|
|
|
36
|
$opening =~ s/ type="text\/css"//i if ( $html5 ); |
301
|
|
|
|
|
|
|
|
302
|
11
|
100
|
66
|
|
|
47
|
if ( $css_packer and $do_stylesheet ) { |
303
|
2
|
|
|
|
|
12
|
$css_packer->minify( \$content, { compress => $do_stylesheet } ); |
304
|
2
|
50
|
|
|
|
1546
|
$content = "\n" . $content if ( $do_stylesheet eq 'pretty' ); |
305
|
|
|
|
|
|
|
} |
306
|
|
|
|
|
|
|
|
307
|
11
|
100
|
|
|
|
26
|
if ( $do_csp ) { |
308
|
11
|
|
|
11
|
|
101
|
no strict 'refs'; |
|
11
|
|
|
|
|
45
|
|
|
11
|
|
|
|
|
7333
|
|
309
|
9
|
|
|
|
|
12
|
push @{ $csp{'style-src'} }, &{ "${do_csp}_base64" } ( $content ); |
|
9
|
|
|
|
|
19
|
|
|
9
|
|
|
|
|
73
|
|
310
|
|
|
|
|
|
|
} |
311
|
|
|
|
|
|
|
} |
312
|
|
|
|
|
|
|
} |
313
|
|
|
|
|
|
|
else { |
314
|
0
|
|
|
|
|
0
|
$content = ''; |
315
|
|
|
|
|
|
|
} |
316
|
|
|
|
|
|
|
|
317
|
45
|
|
|
|
|
159
|
$reggrp_ws->exec( \$opening ); |
318
|
45
|
|
|
|
|
3001
|
$reggrp_ws->exec( \$closing ); |
319
|
|
|
|
|
|
|
|
320
|
45
|
|
|
|
|
2748
|
return $opening . $content . $closing; |
321
|
|
|
|
|
|
|
}, |
322
|
38
|
100
|
|
|
|
690
|
modifier => 'ism' |
323
|
|
|
|
|
|
|
} |
324
|
|
|
|
|
|
|
]; |
325
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
$self->{void_elements}->{reggrp_data} = [ |
327
|
|
|
|
|
|
|
{ |
328
|
|
|
|
|
|
|
regexp => '<\s*((?:' . join( '|', @VOID_ELEMENTS ) . ')\b[^>]*)\s*\/>', |
329
|
|
|
|
|
|
|
replacement => sub { |
330
|
2
|
|
|
3
|
|
205
|
return '<' . $_[0]->{submatches}->[0] . '>'; |
331
|
|
|
|
|
|
|
}, |
332
|
37
|
|
|
|
|
408
|
modifier => 'ism' |
333
|
|
|
|
|
|
|
} |
334
|
|
|
|
|
|
|
]; |
335
|
|
|
|
|
|
|
|
336
|
37
|
|
|
|
|
115
|
foreach ( @HTML::Packer::REGGRPS ) { |
337
|
148
|
|
|
|
|
29947
|
$self->{ '_reggrp_' . $_ } = Regexp::RegGrp->new( { reggrp => $self->{$_}->{reggrp_data} } ); |
338
|
|
|
|
|
|
|
} |
339
|
|
|
|
|
|
|
|
340
|
|
|
|
|
|
|
$self->{ '_reggrp_' . $GLOBAL_REGGRP } = Regexp::RegGrp->new( |
341
|
|
|
|
|
|
|
{ |
342
|
|
|
|
|
|
|
reggrp => $self->{$GLOBAL_REGGRP}->{reggrp_data}, |
343
|
37
|
|
|
|
|
4412
|
restore_pattern => qr// |
344
|
|
|
|
|
|
|
} |
345
|
|
|
|
|
|
|
); |
346
|
|
|
|
|
|
|
|
347
|
37
|
|
|
|
|
10103
|
return $self; |
348
|
|
|
|
|
|
|
} |
349
|
|
|
|
|
|
|
|
350
|
|
|
|
|
|
|
sub minify { |
351
|
41
|
|
|
42
|
0
|
2298
|
my ( $self, $input, $opts ); |
352
|
|
|
|
|
|
|
|
353
|
41
|
50
|
33
|
|
|
348
|
unless ( |
354
|
|
|
|
|
|
|
ref( $_[0] ) and |
355
|
|
|
|
|
|
|
$_[0]->isa( __PACKAGE__ ) |
356
|
|
|
|
|
|
|
) { |
357
|
0
|
|
|
|
|
0
|
$self = __PACKAGE__->init(); |
358
|
|
|
|
|
|
|
|
359
|
0
|
0
|
|
|
|
0
|
shift( @_ ) unless ( ref( $_[0] ) ); |
360
|
|
|
|
|
|
|
|
361
|
0
|
|
|
|
|
0
|
( $input, $opts ) = @_; |
362
|
|
|
|
|
|
|
} |
363
|
|
|
|
|
|
|
else { |
364
|
41
|
|
|
|
|
136
|
( $self, $input, $opts ) = @_; |
365
|
|
|
|
|
|
|
} |
366
|
|
|
|
|
|
|
|
367
|
41
|
50
|
|
|
|
132
|
if ( ref( $input ) ne 'SCALAR' ) { |
368
|
0
|
|
|
|
|
0
|
carp( 'First argument must be a scalarref!' ); |
369
|
0
|
|
|
|
|
0
|
return undef; |
370
|
|
|
|
|
|
|
} |
371
|
|
|
|
|
|
|
|
372
|
41
|
|
|
|
|
66
|
my $html; |
373
|
41
|
|
|
|
|
61
|
my $cont = 'void'; |
374
|
|
|
|
|
|
|
|
375
|
41
|
50
|
|
|
|
106
|
if ( defined( wantarray ) ) { |
376
|
0
|
0
|
|
|
|
0
|
my $tmp_input = ref( $input ) ? ${$input} : $input; |
|
0
|
|
|
|
|
0
|
|
377
|
|
|
|
|
|
|
|
378
|
0
|
|
|
|
|
0
|
$html = \$tmp_input; |
379
|
0
|
|
|
|
|
0
|
$cont = 'scalar'; |
380
|
|
|
|
|
|
|
} |
381
|
|
|
|
|
|
|
else { |
382
|
41
|
50
|
|
|
|
106
|
$html = ref( $input ) ? $input : \$input; |
383
|
|
|
|
|
|
|
} |
384
|
|
|
|
|
|
|
|
385
|
41
|
50
|
|
|
|
118
|
if ( ref( $opts ) eq 'HASH' ) { |
386
|
41
|
|
|
|
|
95
|
foreach my $field ( @BOOLEAN_ACCESSORS ) { |
387
|
205
|
100
|
|
|
|
482
|
$self->$field( $opts->{$field} ) if ( defined( $opts->{$field} ) ); |
388
|
|
|
|
|
|
|
} |
389
|
|
|
|
|
|
|
|
390
|
41
|
100
|
|
|
|
140
|
$self->do_javascript( $opts->{do_javascript} ) if ( defined( $opts->{do_javascript} ) ); |
391
|
41
|
100
|
|
|
|
681
|
$self->do_stylesheet( $opts->{do_stylesheet} ) if ( defined( $opts->{do_stylesheet} ) ); |
392
|
41
|
100
|
|
|
|
110
|
$self->do_csp( $opts->{do_csp} ) if ( defined( $opts->{do_csp} ) ); |
393
|
|
|
|
|
|
|
} |
394
|
|
|
|
|
|
|
|
395
|
41
|
100
|
100
|
|
|
106
|
if ( not $self->no_compress_comment() and ${$html} =~ /$PACKER_COMMENT/s ) { |
|
39
|
|
|
|
|
259
|
|
396
|
1
|
|
|
|
|
3
|
my $compress = $1; |
397
|
1
|
50
|
|
|
|
4
|
if ( $compress eq '_no_compress_' ) { |
398
|
1
|
50
|
|
|
|
15
|
return ( $cont eq 'scalar' ) ? ${$html} : undef; |
|
0
|
|
|
|
|
0
|
|
399
|
|
|
|
|
|
|
} |
400
|
|
|
|
|
|
|
} |
401
|
|
|
|
|
|
|
|
402
|
|
|
|
|
|
|
# (re)initialize variables used in the closures |
403
|
40
|
|
100
|
|
|
104
|
$remove_comments = $self->remove_comments || $self->remove_comments_aggressive; |
404
|
40
|
|
|
|
|
90
|
$remove_comments_aggressive = $self->remove_comments_aggressive; |
405
|
40
|
|
|
|
|
63
|
$remove_newlines = $self->remove_newlines; |
406
|
40
|
|
|
|
|
65
|
$html5 = $self->html5; |
407
|
40
|
|
|
|
|
76
|
$do_javascript = $self->do_javascript; |
408
|
40
|
|
|
|
|
100
|
$do_stylesheet = $self->do_stylesheet; |
409
|
40
|
|
|
|
|
87
|
$do_csp = $self->do_csp; |
410
|
40
|
|
|
|
|
97
|
$js_packer = $self->javascript_packer; |
411
|
40
|
|
|
|
|
122
|
$css_packer = $self->css_packer; |
412
|
40
|
|
|
|
|
122
|
$reggrp_ws = $self->reggrp_whitespaces; |
413
|
|
|
|
|
|
|
|
414
|
|
|
|
|
|
|
# blank out the CSP hash before populating it again |
415
|
40
|
|
|
|
|
94
|
%csp = (); |
416
|
|
|
|
|
|
|
|
417
|
|
|
|
|
|
|
# FIXME: hacky way to get around ->init being called before ->minify |
418
|
40
|
100
|
|
|
|
108
|
$self = ref( $self )->init if $remove_comments_aggressive; |
419
|
|
|
|
|
|
|
|
420
|
40
|
|
|
|
|
109
|
$self->reggrp_global()->exec( $html ); |
421
|
40
|
|
|
|
|
1510
|
$self->reggrp_whitespaces()->exec( $html ); |
422
|
40
|
100
|
|
|
|
4956
|
if ( $self->remove_newlines() ) { |
423
|
29
|
|
|
|
|
84
|
$self->reggrp_newlines_tags()->exec( $html ); |
424
|
29
|
|
|
|
|
515
|
$self->reggrp_newlines()->exec( $html ); |
425
|
|
|
|
|
|
|
} |
426
|
40
|
100
|
|
|
|
442
|
if ( $self->html5() ) { |
427
|
6
|
|
|
|
|
17
|
$self->reggrp_void_elements()->exec( $html ); |
428
|
|
|
|
|
|
|
} |
429
|
|
|
|
|
|
|
|
430
|
40
|
|
|
|
|
517
|
$self->reggrp_global()->restore_stored( $html ); |
431
|
|
|
|
|
|
|
|
432
|
40
|
50
|
|
|
|
2022
|
return ${$html} if ( $cont eq 'scalar' ); |
|
0
|
|
|
|
|
0
|
|
433
|
|
|
|
|
|
|
} |
434
|
|
|
|
|
|
|
|
435
|
|
|
|
|
|
|
sub javascript_packer { |
436
|
41
|
|
|
42
|
0
|
7034
|
my $self = shift; |
437
|
|
|
|
|
|
|
|
438
|
41
|
100
|
|
|
|
101
|
unless ( $self->{_checked_javascript_packer} ) { |
439
|
35
|
|
|
4
|
|
2282
|
eval "use JavaScript::Packer $REQUIRED_JAVASCRIPT_PACKER;"; |
|
4
|
|
|
|
|
923
|
|
|
4
|
|
|
|
|
10180
|
|
|
4
|
|
|
|
|
60
|
|
440
|
|
|
|
|
|
|
|
441
|
35
|
50
|
|
|
|
130
|
unless ( $@ ) { |
442
|
35
|
|
|
|
|
61
|
$self->{_javascript_packer} = eval { |
443
|
35
|
|
|
|
|
132
|
JavaScript::Packer->init(); |
444
|
|
|
|
|
|
|
}; |
445
|
|
|
|
|
|
|
} |
446
|
|
|
|
|
|
|
|
447
|
35
|
|
|
|
|
115830
|
$self->{_checked_javascript_packer} = 1; |
448
|
|
|
|
|
|
|
} |
449
|
|
|
|
|
|
|
|
450
|
41
|
|
|
|
|
2455
|
return $self->{_javascript_packer}; |
451
|
|
|
|
|
|
|
} |
452
|
|
|
|
|
|
|
|
453
|
|
|
|
|
|
|
sub css_packer { |
454
|
41
|
|
|
42
|
0
|
3792
|
my $self = shift; |
455
|
|
|
|
|
|
|
|
456
|
41
|
100
|
|
|
|
118
|
unless ( $self->{_checked_css_packer} ) { |
457
|
35
|
|
|
4
|
|
2337
|
eval "use CSS::Packer $REQUIRED_CSS_PACKER;"; |
|
4
|
|
|
|
|
1350
|
|
|
4
|
|
|
|
|
7712
|
|
|
4
|
|
|
|
|
58
|
|
458
|
|
|
|
|
|
|
|
459
|
35
|
50
|
|
|
|
139
|
unless ( $@ ) { |
460
|
35
|
|
|
|
|
59
|
$self->{_css_packer} = eval { |
461
|
35
|
|
|
|
|
145
|
CSS::Packer->init(); |
462
|
|
|
|
|
|
|
}; |
463
|
|
|
|
|
|
|
} |
464
|
|
|
|
|
|
|
|
465
|
35
|
|
|
|
|
61173
|
$self->{_checked_css_packer} = 1; |
466
|
|
|
|
|
|
|
} |
467
|
|
|
|
|
|
|
|
468
|
41
|
|
|
|
|
2132
|
return $self->{_css_packer}; |
469
|
|
|
|
|
|
|
} |
470
|
|
|
|
|
|
|
|
471
|
|
|
|
|
|
|
sub csp { |
472
|
7
|
|
|
8
|
1
|
66
|
my $self = shift; |
473
|
|
|
|
|
|
|
|
474
|
7
|
100
|
100
|
|
|
56
|
return 'script-src' => [ ], 'style-src' => [ ] unless $do_csp and %csp; |
475
|
|
|
|
|
|
|
|
476
|
|
|
|
|
|
|
return |
477
|
4
|
|
|
|
|
26
|
'script-src' => [ map "'$do_csp-$_='", @{ $csp{'script-src'} } ], |
478
|
4
|
|
|
|
|
9
|
'style-src' => [ map "'$do_csp-$_='", @{ $csp{'style-src'} } ], |
|
4
|
|
|
|
|
51
|
|
479
|
|
|
|
|
|
|
; |
480
|
|
|
|
|
|
|
} |
481
|
|
|
|
|
|
|
|
482
|
|
|
|
|
|
|
1; |
483
|
|
|
|
|
|
|
|
484
|
|
|
|
|
|
|
__END__ |