Don't strip double simicolons inside for

Fixes issue #89
This commit is contained in:
Matthias Mullie 2015-12-17 22:34:54 +01:00
parent cecee1ce5b
commit da03f6d13c
2 changed files with 16 additions and 4 deletions

View File

@ -284,10 +284,16 @@ class JS extends Minify
$content = preg_replace('/(^|[;\}\s])\K('.implode('|', $before).')\s+/', '\\2 ', $content);
$content = preg_replace('/\s+('.implode('|', $after).')(?=([;\{\s]|$))/', ' \\1', $content);
// get rid of double semicolons, except where they can be used like:
// "for(v=1,_=b;;)" or "for(v=1;;v++)"
$content = preg_replace('/;+(?!\))/', ';', $content);
$content = preg_replace('/\bfor\(([^;]*);([^;\(]*)\)/', 'for(\\1;;\\2)', $content);
/*
* Get rid of double semicolons, except where they can be used like:
* "for(v=1,_=b;;)", "for(v=1;;v++)" or "for(;;ja||(ja=true))".
* I'll safeguard these double semicolons inside for-loops by
* temporarily replacing them with an invalid condition: they won't have
* a double semicolon and will be easy to spot to restore afterwards.
*/
$content = preg_replace('/\bfor\(([^;]*);;([^;]*)\)/', 'for(\\1;-;\\2)', $content);
$content = preg_replace('/;+/', ';', $content);
$content = preg_replace('/\bfor\(([^;]*);-;([^;]*)\)/', 'for(\\1;;\\2)', $content);
/*
* Next, we'll be removing all semicolons where ASI kicks in.

View File

@ -653,6 +653,12 @@ BUG
"$(coming.wrap).bind('onReset',function(){try{\$(this).find('iframe').hide().attr('src','//about:blank').end().empty()}catch(e){}})",
);
// https://github.com/matthiasmullie/minify/issues/89
$tests[] = array(
"for(;;ja||(ja=true)){}",
"for(;;ja||(ja=!0)){}",
);
return $tests;
}
}