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
|
#!perl -T
# =========================================================================== #
use Test::More;
my $js_input = <<EOT;
<script type="javascript">
alert('test');</script>
<a href="/" >link
1 < /a>
<!-- comment -->
< a href="/"> link 2
< / a >
EOT
my $js_expected = '<script type="javascript">/*<![CDATA[*/alert(\'test\');/*]]>*/</script> <a href="/">link 1 </a> <a href="/"> link 2 </a>';
my $js_expected_html5 = '<script>alert(\'test\');</script> <a href="/">link 1 </a> <a href="/"> link 2 </a>';
my $js_expected_html5_no_js = '<script>' . "\n\n\n\n" . ' alert(\'test\');</script> <a href="/">link 1 </a> <a href="/"> link 2 </a>';
my $js_expected_no_js = '<script type="javascript">' . "\n\n\n\n" . ' alert(\'test\');</script> <a href="/">link 1 </a> <a href="/"> link 2 </a>';
my $css_input = <<EOT;
<style type="text/css">
foo {
asdf:asdf;
ew:12;
}
</style>
<a href="/" >link
1 < /a>
<!-- comment -->
< a href="/"> link 2
< / a >
EOT
my $css_expected = '<style type="text/css">' . "\nfoo{\nasdf:asdf;\new:12;\n}\n" . '</style> <a href="/">link 1 </a> <a href="/"> link 2 </a>';
my $css_expected_no_css = '<style type="text/css">' . "\n\n foo {\n asdf:asdf;\n ew:12;\n }\n" . '</style> <a href="/">link 1 </a> <a href="/"> link 2 </a>';
my $css_expected_html5 = '<style>' . "\nfoo{\nasdf:asdf;\new:12;\n}\n" . '</style> <a href="/">link 1 </a> <a href="/"> link 2 </a>';
my $css_expected_html5_no_css = '<style>' . "\n\n foo {\n asdf:asdf;\n ew:12;\n }\n" . '</style> <a href="/">link 1 </a> <a href="/"> link 2 </a>';
my $html_input = <<EOT;
<script type="javascript">/*<![CDATA[*/
alert('test');/*]]>*/</script>
<br />
<img src="/bild.jpg" alt="hmpf" />
<a href="/" >link
1 < /a>
<!-- comment -->
< a href="/"> link 2
< / a >
EOT
my $html_expected = '<script>alert(\'test\');</script> <br> <img src="/bild.jpg" alt="hmpf"> <a href="/">link 1 </a> <a href="/"> link 2 </a>';
my $html_expected_no_js = '<script>/*<![CDATA[*/' . "\n\n\n\n " . 'alert(\'test\');/*]]>*/</script> <br> <img src="/bild.jpg" alt="hmpf"> <a href="/">link 1 </a> <a href="/"> link 2 </a>';
my $not = 11;
SKIP: {
eval { use HTML::Packer; };
skip( 'HTML::Packer not installed!', $not ) if ( $@ );
plan tests => $not;
minTest( 's1', undef, 'Test without opts.' );
minTest( 's2', { remove_newlines => 1 }, 'Test remove_newlines.' );
minTest( 's3', { remove_comments => 1 }, 'Test remove_comments.' );
minTest( 's4', { remove_comments => 1, remove_newlines => 1 }, 'Test remove_newlines and remove_comments.' );
minTest( 's5', { remove_comments => 1, remove_newlines => 1 }, 'Test _no_compress_ comment.' );
minTest( 's6', { remove_comments => 1, remove_newlines => 1, no_compress_comment => 1 }, 'Test _no_compress_ comment with no_compress_comment option.' );
my $packer = HTML::Packer->init();
my $js_comp_input = $js_input;
my $js_html5_input = $js_input;
$packer->minify( \$js_comp_input, { remove_comments => 1, remove_newlines => 1, do_javascript => 'clean' } );
$packer->minify( \$js_html5_input, { remove_comments => 1, remove_newlines => 1, do_javascript => 'clean', html5 => 1 } );
$packer->minify( \$html_input, { remove_comments => 1, remove_newlines => 1, do_javascript => 'clean', html5 => 1 } );
eval "use JavaScript::Packer $HTML::Packer::REQUIRED_JAVASCRIPT_PACKER;";
if ( $@ ) {
is( $js_comp_input, $js_expected_no_js, 'Test do_javascript. JavaScript::Packer >= ' . $HTML::Packer::REQUIRED_JAVASCRIPT_PACKER . ' not installed.' );
is( $js_html5_input, $js_expected_html5_no_js, 'Test do_javascript 2. JavaScript::Packer >= ' . $HTML::Packer::REQUIRED_JAVASCRIPT_PACKER . ' not installed.' );
is( $html_input, $html_expected_no_js, 'Test do_javascript 3. JavaScript::Packer >= ' . $HTML::Packer::REQUIRED_JAVASCRIPT_PACKER . ' not installed.' );
}
else {
is( $js_comp_input, $js_expected, 'Test do_javascript. JavaScript::Packer installed.' );
is( $js_html5_input, $js_expected_html5, 'Test do_javascript 2. JavaScript::Packer installed.' );
is( $html_input, $html_expected, 'Test do_javascript 3. JavaScript::Packer installed.' );
}
my $css_comp_input = $css_input;
my $css_html5_input = $css_input;
$packer->minify( \$css_comp_input, { remove_comments => 1, remove_newlines => 1, do_stylesheet => 'pretty', html5 => 0 } );
$packer->minify( \$css_html5_input, { remove_comments => 1, remove_newlines => 1, do_stylesheet => 'pretty', html5 => 1 } );
eval "use CSS::Packer $HTML::Packer::REQUIRED_CSS_PACKER;";
if ( $@ ) {
is( $css_comp_input, $css_expected_no_css, 'Test do_stylesheet. CSS::Packer >= ' . $HTML::Packer::REQUIRED_CSS_PACKER . ' not installed.' );
is( $css_html5_input, $css_expected_html5_no_css, 'Test do_stylesheet 2. CSS::Packer >= ' . $HTML::Packer::REQUIRED_CSS_PACKER . ' not installed.' );
}
else {
is( $css_comp_input, $css_expected, 'Test do_stylesheet. CSS::Packer installed.' );
is( $css_html5_input, $css_expected_html5, 'Test do_stylesheet 2. CSS::Packer installed.' );
}
}
sub filesMatch {
my $file1 = shift;
my $file2 = shift;
my $a;
my $b;
while (1) {
$a = getc($file1);
$b = getc($file2);
if (!defined($a) && !defined($b)) { # both files end at same place
return 1;
}
elsif (
!defined($b) || # file2 ends first
!defined($a) || # file1 ends first
$a ne $b
) { # a and b not the same
return 0;
}
}
}
sub minTest {
my $filename = shift;
my $opts = shift || {};
my $message = shift || '';
open(INFILE, 't/html/' . $filename . '.html') or die("couldn't open file");
open(GOTFILE, '>t/html/' . $filename . '-got.html') or die("couldn't open file");
my $html = join( '', <INFILE> );
my $packer = HTML::Packer->init();
$packer->minify( \$html, $opts );
print GOTFILE $html;
close(INFILE);
close(GOTFILE);
open(EXPECTEDFILE, 't/html/' . $filename . '-expected.html') or die("couldn't open file");
open(GOTFILE, 't/html/' . $filename . '-got.html') or die("couldn't open file");
ok(filesMatch(GOTFILE, EXPECTEDFILE), $message );
close(EXPECTEDFILE);
close(GOTFILE);
}
|