diff --git a/src/JS.php b/src/JS.php index 82f6d84..61c951d 100644 --- a/src/JS.php +++ b/src/JS.php @@ -429,6 +429,12 @@ class JS extends Minify $content = preg_replace('/(for\([^;\{]*;[^;\{]*;[^;\{]*(\{([^\{\}]*(?-2))*[^\{\}]*\})?[^;\{]*\));(\}|$)/s', '\\1;;\\4', $content); $content = preg_replace('/(for\([^;\{]+\s+in\s+[^;\{]+\));(\}|$)/s', '\\1;;\\2', $content); + + /* + * Do the same for the if's that don't have a body but are followed by ;} + */ + $content = preg_replace('/(\bif\s*\([^{;]*\));\}/s', '\\1;;}', $content); + /* * Below will also keep `;` after a `do{}while();` along with `while();` * While these could be stripped after do-while, detecting this diff --git a/tests/js/JSTest.php b/tests/js/JSTest.php index 99d5ff3..459b0e6 100644 --- a/tests/js/JSTest.php +++ b/tests/js/JSTest.php @@ -1321,6 +1321,16 @@ var largeScreen=2048', 'if(l!==3){for(var V=w.length;l5){return e-5}return e}).length);}else var C=3' ); + // https://github.com/matthiasmullie/minify/issues/394 + $tests[] = array( + 'var a = function(){var b; if(b=3);}', + 'var a=function(){var b;if(b=3);}', + ); + $tests[] = array( + 'jQuery(document).ready(function(e){ if (jQuery(document.body).on("updated_wc_div", o), jQuery(document.body).on("updated_cart_totals", o)); });', + 'jQuery(document).ready(function(e){if(jQuery(document.body).on("updated_wc_div",o),jQuery(document.body).on("updated_cart_totals",o));})', + ); + // known minified files to help doublecheck changes in places not yet // anticipated in these tests $files = glob(__DIR__.'/sample/minified/*.js');