Skip to content

Commit

Permalink
resolve #6 - don't minify non-javascript script content
Browse files Browse the repository at this point in the history
anything in <script> tags that has a type that doesn't look like
javascript will no longer be minified - add tests to confirm this
is the case
  • Loading branch information
leejo committed Mar 4, 2017
1 parent c0093f7 commit d75e632
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 4 deletions.
3 changes: 3 additions & 0 deletions Changes
@@ -1,5 +1,8 @@
Revision history for HTML-Packer

2.04 2017-03-04
- Fix: don't minify none javascript script content (GH #6)

2.03 2016-05-22
- Fix: require recent versions of deps to fix memory leaks

Expand Down
1 change: 1 addition & 0 deletions MANIFEST
Expand Up @@ -22,6 +22,7 @@ t/01-interface.t
t/02-io.t
t/03-leaks.t
t/04-leaks_full.t
t/gh-6_exempt_some_script_tags.t
t/html/s1-expected.html
t/html/s1.html
t/html/s2-expected.html
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -10,7 +10,7 @@ HTML::Packer - Another HTML code cleaner

# VERSION

Version 2.03
Version 2.04

# DESCRIPTION

Expand Down
10 changes: 7 additions & 3 deletions lib/HTML/Packer.pm
Expand Up @@ -8,7 +8,7 @@ use Regexp::RegGrp;

# -----------------------------------------------------------------------------

our $VERSION = '2.03';
our $VERSION = '2.04';

our @BOOLEAN_ACCESSORS = (
'remove_comments',
Expand Down Expand Up @@ -251,8 +251,12 @@ sub init {
if ( $content ) {
my $opening_script_re = '<\s*script' . ( $html5 ? '[^>]*>' : '[^>]*(?:java|ecma)script[^>]*>' );
my $opening_style_re = '<\s*style' . ( $html5 ? '[^>]*>' : '[^>]*text\/css[^>]*>' );
my $js_type_re = q{type=['"]((application|text)/){0,1}(x-){0,1}(java|ecma)script['"]};

if ( $opening =~ /$opening_script_re/i ) {
if (
$opening =~ /$opening_script_re/i
&& ( $opening =~ /$js_type_re/i || $opening !~ /type/i )
) {
$opening =~ s/ type="(text\/)?(java|ecma)script"//i if ( $html5 );

if ( $js_packer and $do_javascript ) {
Expand Down Expand Up @@ -435,7 +439,7 @@ HTML::Packer - Another HTML code cleaner
=head1 VERSION
Version 2.03
Version 2.04
=head1 DESCRIPTION
Expand Down
80 changes: 80 additions & 0 deletions t/gh-6_exempt_some_script_tags.t
@@ -0,0 +1,80 @@
#!perl

use strict;
use warnings;

use Test::More;

eval 'use HTML::Packer;';
plan skip_all => 'HTML::Packer not installed!' if $@;

plan tests => 20;

SKIP: {

foreach my $content_type (
'application/javascript',
'application/ecmascript',
'text/javascript',
'text/ecmascript',
'text/x-javascript',
'application/x-javascript',
'javascript',
'',
'text/template',
'text/html',
) {
foreach my $attr_def (
" type=\"$content_type\"",
" foo=\"bar\" type=\"$content_type\"",
) {
$attr_def =~ s/ type=""//;

my $js_input = <<EOT;
<script$attr_def>
alert('test');</script>
<a href="/" >link
1 < /a>
<!-- comment -->
< a href="/"> link 2
< / a >
EOT

my $js_expected = "<script$attr_def>" . '/*<![CDATA[*/alert(\'test\');/*]]>*/</script> <a href="/">link 1 </a> <a href="/"> link 2 </a>';
my $js_expected_no_js = "<script$attr_def>" . "\n\n\n\n" . ' alert(\'test\');</script> <a href="/">link 1 </a> <a href="/"> link 2 </a>';

eval "use JavaScript::Packer $HTML::Packer::REQUIRED_JAVASCRIPT_PACKER;";

my $js_comp_input = $js_input;
my $packer = HTML::Packer->init();
$packer->minify(
\$js_comp_input,
{
remove_comments => 1,
remove_newlines => 1,
do_javascript => 'clean'
}
);

if ( $@ ) {
is( $js_comp_input, $js_expected_no_js, 'Test do_javascript. JavaScript::Packer >= ' . $HTML::Packer::REQUIRED_JAVASCRIPT_PACKER . ' not installed.' );
} else {
( !$content_type or $content_type !~ /script/ )
? is( $js_comp_input,$js_expected_no_js,"DO NOT minifiy for $content_type" )
: is( $js_comp_input,$js_expected, "minify for $content_type" );
}
}
}
}

0 comments on commit d75e632

Please sign in to comment.