Skip to content

Commit

Permalink
Require that a regex has some content
Browse files Browse the repository at this point in the history
Otherwise, the `//` that start a comment could be misread
as regexes.

Fixes #196
  • Loading branch information
matthiasmullie committed Sep 13, 2017
1 parent 0913101 commit 3f9cebc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/JS.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ protected function extractRegex()
(isset($match['after']) ? $match['after'] : '');
};

$pattern = '(?P<regex>\/.*?(?<!\\\\)(\\\\\\\\)*\/[gimy]*)(?![0-9a-zA-Z\/])';
$pattern = '(?P<regex>\/(.|\\\\)+?((?<!\\\\)\\\\\\\\)*\/[gimy]*)(?![0-9a-zA-Z\/])';

// a regular expression can only be followed by a few operators or some
// of the RegExp methods (a `\` followed by a variable or value is
Expand Down
11 changes: 11 additions & 0 deletions tests/js/JSTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1115,6 +1115,17 @@ function someOtherFunction() {
'if(!0||/^(https?:)?\/\//.test(\'xxx\'))alert(1)',
);

// https://github.com/matthiasmullie/minify/issues/196
$tests[] = array(
'if ( true ) {
console.log(true);
// ...comment number 2 (something with dots?)
} else {
console.log(false);
}',
'if(!0){console.log(!0)}else{console.log(!1)}',
);

// known minified files to help doublecheck changes in places not yet
// anticipated in these tests
$files = glob(__DIR__.'/sample/minified/*.js');
Expand Down

0 comments on commit 3f9cebc

Please sign in to comment.