Skip to content

Commit

Permalink
do not recompress already minified js FS#2574
Browse files Browse the repository at this point in the history
  • Loading branch information
splitbrain committed Aug 6, 2012
1 parent 63e967b commit 80a4729
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
26 changes: 26 additions & 0 deletions _test/tests/lib/exe/js_js_compress.test.php
Expand Up @@ -118,6 +118,32 @@ function test_multilinestring(){
$this->assertEquals("var foo='this is a multiline string';",js_compress($text));
}

function test_nocompress(){
$text = <<<EOF
var meh = 'test' ;
/* BEGIN NOCOMPRESS */
var foo = 'test' ;
var bar = 'test' ;
/* END NOCOMPRESS */
var moh = 'test' ;
EOF;
$out = <<<EOF
var meh='test';
var foo = 'test' ;
var bar = 'test' ;
var moh='test';
EOF;

$this->assertEquals($out, js_compress($text));
}

/**
* Test the files provided with the original JsStrip
Expand Down
17 changes: 16 additions & 1 deletion lib/exe/js.php
Expand Up @@ -102,8 +102,12 @@ function js_out(){

// load files
foreach($files as $file){
$ismin = (substr($file,-7) == '.min.js');

echo "\n\n/* XXXXXXXXXX begin of ".str_replace(DOKU_INC, '', $file) ." XXXXXXXXXX */\n\n";
if($ismin) echo "\n/* BEGIN NOCOMPRESS */\n";
js_load($file);
if($ismin) echo "\n/* END NOCOMPRESS */\n";
echo "\n\n/* XXXXXXXXXX end of " . str_replace(DOKU_INC, '', $file) . " XXXXXXXXXX */\n\n";
}

Expand Down Expand Up @@ -262,7 +266,18 @@ function js_compress($s){
if($ch == '/' && $s{$i+1} == '*' && $s{$i+2} != '@'){
$endC = strpos($s,'*/',$i+2);
if($endC === false) trigger_error('Found invalid /*..*/ comment', E_USER_ERROR);
$i = $endC + 2;

// check if this is a NOCOMPRESS comment
if(substr($s, $i, $endC+2-$i) == '/* BEGIN NOCOMPRESS */'){
$endNC = strpos($s, '/* END NOCOMPRESS */', $endC+2);
if($endNC === false) trigger_error('Found invalid NOCOMPRESS comment', E_USER_ERROR);

// verbatim copy contents, trimming but putting it on its own line
$result .= "\n".trim(substr($s, $i + 22, $endNC - ($i + 22)))."\n"; // BEGIN comment = 22 chars
$i = $endNC + 20; // END comment = 20 chars
}else{
$i = $endC + 2;
}
continue;
}

Expand Down

0 comments on commit 80a4729

Please sign in to comment.