/usr/share/perl5/HTML/Packer.pm is in libhtml-packer-perl 2.030000-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 | package HTML::Packer;
use 5.008009;
use strict;
use warnings;
use Carp;
use Regexp::RegGrp;
# -----------------------------------------------------------------------------
our $VERSION = '2.03';
our @BOOLEAN_ACCESSORS = (
'remove_comments',
'remove_newlines',
'no_compress_comment',
'html5',
);
our @JAVASCRIPT_OPTS = ( 'clean', 'obfuscate', 'shrink', 'best' );
our @CSS_OPTS = ( 'minify', 'pretty' );
our $REQUIRED_JAVASCRIPT_PACKER = '1.002001';
our $REQUIRED_CSS_PACKER = '1.000001';
our @SAVE_SPACE_ELEMENTS = (
'a', 'abbr', 'acronym', 'address', 'b', 'bdo', 'big', 'button', 'cite',
'del', 'dfn', 'em', 'font', 'i', 'input', 'ins', 'kbd', 'label', 'q',
's', 'samp', 'select', 'small', 'strike', 'strong', 'sub', 'sup', 'u', 'var'
);
our @VOID_ELEMENTS = (
'area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input',
'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr'
);
# Some regular expressions are from HTML::Clean
our $COMMENT = '((?>\s*))(<!--(?:(?![#\[]| google_ad_section_).*?)?-->)((?>\s*))';
our $PACKER_COMMENT = '<!--\s*HTML::Packer\s*(\w+)\s*-->';
our $DOCTYPE = '<\!DOCTYPE[^>]*>';
our $DONT_CLEAN = '(<\s*(pre|code|textarea|script|style)[^>]*>)(.*?)(<\s*\/\2[^>]*>)';
our $WHITESPACES = [
{
regexp => qr/^\s*/s,
replacement => ''
},
{
regexp => qr/\s*$/s,
replacement => ''
},
{
regexp => '^\s*',
replacement => '',
modifier => 'm'
},
{
regexp => '[^\S\n]*$',
replacement => '',
modifier => 'm'
},
{
regexp => qr/(?<=>)[^<>]*(?=<)/sm,
replacement => sub {
my $match = $_[0]->{match};
$match =~ s/[^\S\n]{2,}/ /sg;
$match =~ s/\s*\n+\s*/\n/sg;
return $match;
}
},
{
regexp => '<\s*(\/)?\s*',
replacement => sub {
return sprintf( '<%s', $_[0]->{submatches}->[0] );
},
modifier => 's'
},
{
regexp => '\s*(\/)?\s*>',
replacement => sub {
return sprintf( '%s>', $_[0]->{submatches}->[0] );
},
modifier => 's'
}
];
our $NEWLINES_TAGS = [
{
regexp => '(\s*)(<\s*\/?\s*(?:' . join( '|', @SAVE_SPACE_ELEMENTS ) . ')\b[^>]*>)(\s*)',
replacement => sub {
return sprintf( '%s%s%s', $_[0]->{submatches}->[0] ? ' ' : '', $_[0]->{submatches}->[1], $_[0]->{submatches}->[2] ? ' ' : '' );
},
modifier => 'is'
}
];
our $NEWLINES = [
{
regexp => '(.)\n(.)',
replacement => sub {
my ( $pre, $post ) = @{$_[0]->{submatches}};
my $ret;
if ( $pre eq '>' or $post eq '<' ) {
$ret = $pre . ' ' . $post;
}
elsif ( $pre =~ /[\w-]/ and $post =~ /[\w-]/ ) {
$ret = $pre . ' ' . $post;
}
else {
$ret = $pre . $post;
}
return $ret;
}
}
];
our @REGGRPS = ( 'newlines', 'newlines_tags', 'whitespaces', 'void_elements' );
our $GLOBAL_REGGRP = 'global';
##########################################################################################
{
no strict 'refs';
foreach my $field ( @BOOLEAN_ACCESSORS ) {
next if defined *{ __PACKAGE__ . '::' . $field }{CODE};
*{ __PACKAGE__ . '::' . $field} = sub {
my ( $self, $value ) = @_;
$self->{'_' . $field} = $value ? 1 : undef if ( defined( $value ) );
return $self->{'_' . $field};
};
}
foreach my $reggrp ( @REGGRPS, $GLOBAL_REGGRP ) {
next if defined *{ __PACKAGE__ . '::reggrp_' . $reggrp }{CODE};
*{ __PACKAGE__ . '::reggrp_' . $reggrp } = sub {
my ( $self ) = shift;
return $self->{ '_reggrp_' . $reggrp };
};
}
}
sub do_javascript {
my ( $self, $value ) = @_;
if ( defined( $value ) ) {
if ( grep( $value eq $_, @JAVASCRIPT_OPTS ) ) {
$self->{_do_javascript} = $value;
}
elsif ( ! $value ) {
$self->{_do_javascript} = undef;
}
}
return $self->{_do_javascript};
}
sub do_stylesheet {
my ( $self, $value ) = @_;
if ( defined( $value ) ) {
if ( grep( $value eq $_, @CSS_OPTS ) ) {
$self->{_do_stylesheet} = $value;
}
elsif ( ! $value ) {
$self->{_do_stylesheet} = undef;
}
}
return $self->{_do_stylesheet};
}
# these variables are used in the closures defined in the init function
# below - we have to use globals as using $self within the closures leads
# to a reference cycle and thus memory leak, and we can't scope them to
# the init method as they may change. they are set by the minify sub
our $remove_comments;
our $remove_newlines;
our $html5;
our $do_javascript;
our $do_stylesheet;
our $js_packer;
our $css_packer;
our $reggrp_ws;
sub init {
my $class = shift;
my $self = {};
bless( $self, $class );
$self->{whitespaces}->{reggrp_data} = $WHITESPACES;
$self->{newlines}->{reggrp_data} = $NEWLINES;
$self->{newlines_tags}->{reggrp_data} = $NEWLINES_TAGS;
$self->{global}->{reggrp_data} = [
{
regexp => $DOCTYPE,
replacement => sub {
return '<!--~' . $_[0]->{store_index} . '~-->';
},
store => sub {
my $doctype = $_[0]->{match};
$doctype =~ s/\s+/ /gsm;
return $doctype;
}
},
{
regexp => $COMMENT,
replacement => sub {
return $remove_comments ? (
$remove_newlines ? ' ' : (
( $_[0]->{submatches}->[0] =~ /\n/s or $_[0]->{submatches}->[2] =~ /\n/s ) ? "\n" : ''
)
) : '<!--~' . $_[0]->{store_index} . '~-->';
},
store => sub {
my $ret = $remove_comments ? '' : (
( ( not $remove_newlines and $_[0]->{submatches}->[0] =~ /\n/s ) ? "\n" : '' ) .
$_[0]->{submatches}->[1] .
( ( not $remove_newlines and $_[0]->{submatches}->[2] =~ /\n/s ) ? "\n" : '' )
);
return $ret;
}
},
{
regexp => $DONT_CLEAN,
replacement => sub {
return '<!--~' . $_[0]->{store_index} . '~-->';
},
store => sub {
my ( $opening, undef, $content, $closing ) = @{$_[0]->{submatches}};
if ( $content ) {
my $opening_script_re = '<\s*script' . ( $html5 ? '[^>]*>' : '[^>]*(?:java|ecma)script[^>]*>' );
my $opening_style_re = '<\s*style' . ( $html5 ? '[^>]*>' : '[^>]*text\/css[^>]*>' );
if ( $opening =~ /$opening_script_re/i ) {
$opening =~ s/ type="(text\/)?(java|ecma)script"//i if ( $html5 );
if ( $js_packer and $do_javascript ) {
$js_packer->minify( \$content, { compress => $do_javascript } );
unless ( $html5 ) {
$content = '/*<![CDATA[*/' . $content . '/*]]>*/';
}
}
}
elsif ( $opening =~ /$opening_style_re/i ) {
$opening =~ s/ type="text\/css"//i if ( $html5 );
if ( $css_packer and $do_stylesheet ) {
$css_packer->minify( \$content, { compress => $do_stylesheet } );
$content = "\n" . $content if ( $do_stylesheet eq 'pretty' );
}
}
}
else {
$content = '';
}
$reggrp_ws->exec( \$opening );
$reggrp_ws->exec( \$closing );
return $opening . $content . $closing;
},
modifier => 'ism'
}
];
$self->{void_elements}->{reggrp_data} = [
{
regexp => '<\s*((?:' . join( '|', @VOID_ELEMENTS ) . ')\b[^>]*)\s*\/>',
replacement => sub {
return '<' . $_[0]->{submatches}->[0] . '>';
},
modifier => 'ism'
}
];
foreach ( @HTML::Packer::REGGRPS ) {
$self->{ '_reggrp_' . $_ } = Regexp::RegGrp->new( { reggrp => $self->{$_}->{reggrp_data} } );
}
$self->{ '_reggrp_' . $GLOBAL_REGGRP } = Regexp::RegGrp->new(
{
reggrp => $self->{$GLOBAL_REGGRP}->{reggrp_data},
restore_pattern => qr/<!--~(\d+)~-->/
}
);
return $self;
}
sub minify {
my ( $self, $input, $opts );
unless (
ref( $_[0] ) and
ref( $_[0] ) eq __PACKAGE__
) {
$self = __PACKAGE__->init();
shift( @_ ) unless ( ref( $_[0] ) );
( $input, $opts ) = @_;
}
else {
( $self, $input, $opts ) = @_;
}
if ( ref( $input ) ne 'SCALAR' ) {
carp( 'First argument must be a scalarref!' );
return undef;
}
my $html;
my $cont = 'void';
if ( defined( wantarray ) ) {
my $tmp_input = ref( $input ) ? ${$input} : $input;
$html = \$tmp_input;
$cont = 'scalar';
}
else {
$html = ref( $input ) ? $input : \$input;
}
if ( ref( $opts ) eq 'HASH' ) {
foreach my $field ( @BOOLEAN_ACCESSORS ) {
$self->$field( $opts->{$field} ) if ( defined( $opts->{$field} ) );
}
$self->do_javascript( $opts->{do_javascript} ) if ( defined( $opts->{do_javascript} ) );
$self->do_stylesheet( $opts->{do_stylesheet} ) if ( defined( $opts->{do_stylesheet} ) );
}
if ( not $self->no_compress_comment() and ${$html} =~ /$PACKER_COMMENT/s ) {
my $compress = $1;
if ( $compress eq '_no_compress_' ) {
return ( $cont eq 'scalar' ) ? ${$html} : undef;
}
}
# (re)initialize variables used in the closures
$remove_comments = $self->remove_comments;
$remove_newlines = $self->remove_newlines;
$html5 = $self->html5;
$do_javascript = $self->do_javascript;
$do_stylesheet = $self->do_stylesheet;
$js_packer = $self->javascript_packer;
$css_packer = $self->css_packer;
$reggrp_ws = $self->reggrp_whitespaces;
$self->reggrp_global()->exec( $html );
$self->reggrp_whitespaces()->exec( $html );
if ( $self->remove_newlines() ) {
$self->reggrp_newlines_tags()->exec( $html );
$self->reggrp_newlines()->exec( $html );
}
if ( $self->html5() ) {
$self->reggrp_void_elements()->exec( $html );
}
$self->reggrp_global()->restore_stored( $html );
return ${$html} if ( $cont eq 'scalar' );
}
sub javascript_packer {
my $self = shift;
unless ( $self->{_checked_javascript_packer} ) {
eval "use JavaScript::Packer $REQUIRED_JAVASCRIPT_PACKER;";
unless ( $@ ) {
$self->{_javascript_packer} = eval {
JavaScript::Packer->init();
};
}
$self->{_checked_javascript_packer} = 1;
}
return $self->{_javascript_packer};
}
sub css_packer {
my $self = shift;
unless ( $self->{_checked_css_packer} ) {
eval "use CSS::Packer $REQUIRED_CSS_PACKER;";
unless ( $@ ) {
$self->{_css_packer} = eval {
CSS::Packer->init();
};
}
$self->{_checked_css_packer} = 1;
}
return $self->{_css_packer};
}
1;
__END__
=head1 NAME
HTML::Packer - Another HTML code cleaner
=for html
<a href='https://travis-ci.org/leejo/html-packer-perl?branch=master'><img src='https://travis-ci.org/leejo/html-packer-perl.svg?branch=master' alt='Build Status' /></a>
<a href='https://coveralls.io/r/leejo/html-packer-perl'><img src='https://coveralls.io/repos/leejo/html-packer-perl/badge.png?branch=master' alt='Coverage Status' /></a>
=head1 VERSION
Version 2.03
=head1 DESCRIPTION
A HTML Compressor.
=head1 SYNOPSIS
use HTML::Packer;
my $packer = HTML::Packer->init();
$packer->minify( $scalarref, $opts );
To return a scalar without changing the input simply use (e.g. example 2):
my $ret = $packer->minify( $scalarref, $opts );
For backward compatibility it is still possible to call 'minify' as a function:
HTML::Packer::minify( $scalarref, $opts );
First argument must be a scalarref of HTML-Code.
Second argument must be a hashref of options. Possible options are
=over 4
=item remove_comments
HTML-Comments will be removed if 'remove_comments' has a true value.
=item remove_newlines
ALL newlines will be removed if 'remove_newlines' has a true value.
=item do_javascript
Defines compression level for javascript. Possible values are 'clean', 'obfuscate', 'shrink' and 'best'.
Default is no compression for javascript.
This option only takes effect if L<JavaScript::Packer> is installed.
=item do_stylesheet
Defines compression level for CSS. Possible values are 'minify' and 'pretty'.
Default is no compression for CSS.
This option only takes effect if L<CSS::Packer> is installed.
=item no_compress_comment
If not set to a true value it is allowed to set a HTML comment that prevents the input being packed.
<!-- HTML::Packer _no_compress_ -->
Is not set by default.
=item html5
If set to a true value closing slashes will be removed from void elements.
=back
=head1 AUTHOR
Merten Falk, C<< <nevesenin at cpan.org> >>. Now maintained by Lee
Johnson (LEEJO).
=head1 BUGS
Please report any bugs or feature requests through
the web interface at L<https://github.com/leejo/html-packer-perl/issues>. I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.
=head1 SUPPORT
You can find documentation for this module with the perldoc command.
perldoc HTML::Packer
=head1 COPYRIGHT & LICENSE
Copyright 2009 - 2011 Merten Falk, all rights reserved.
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
=head1 SEE ALSO
L<HTML::Clean>
=cut
|