Skip to content

Commit

Permalink
Improve regex detection
Browse files Browse the repository at this point in the history
Fixes issue #142
  • Loading branch information
matthiasmullie committed Nov 10, 2016
1 parent e93dffc commit 7dd8d25
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/JS.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,12 @@ protected function extractRegex()
return $placeholder;
};

$pattern = '\/.*?(?<!\\\\)(\\\\\\\\)*+\/[gimy]*(?![0-9a-zA-Z\/])';
// crazy regex explained: characters inside /.../ delimiters can be:
// * a /, as long as it's escaped with a \
// * but that \ can not in itself be escaped
// * so we need an uneven amount of \ if we have a /
// * any other character
$pattern = '\/([^\/]|(?<!\\\\)(\\\\\\\\)*\\\\\/)+\/[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
37 changes: 37 additions & 0 deletions tests/js/JSTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,22 @@ public function dataProvider()
'/abc\/def\//.test("abc")',
'/abc\/def\//.test("abc")',
);
$tests[] = array(
'/abc\/def\//.test("abc\/def\/")',
'/abc\/def\//.test("abc\/def\/")',
);
$tests[] = array(
// there's an escape mess here; below regex represent this JS line:
// /abc\/def\\\//.test("abc/def\\/")
'/abc\/def\\\\\//.test("abc/def\\\/")',
'/abc\/def\\\\\//.test("abc/def\\\/")',
);
$tests[] = array(
// escape mess, this represents:
// /abc\/def\\\\\//.test("abc/def\\\\/")
'/abc\/def\\\\\\\\\//.test("abc/def\\\\\\\\/")',
'/abc\/def\\\\\\\\\//.test("abc/def\\\\\\\\/")',
);
$tests[] = array(
'var a = /abc\/def\//.test("abc")',
'var a=/abc\/def\//.test("abc")',
Expand Down Expand Up @@ -832,6 +848,27 @@ function otherFuncName() {
'var a=1',
);

// https://github.com/matthiasmullie/minify/issues/142
$tests[] = array(
'return {
l: ((116 * y) - 16) / 100, // [0,100]
a: ((500 * (x - y)) + 128) / 255, // [-128,127]
b: ((200 * (y - z)) + 128) / 255 // [-128,127]
};',
'return{l:((116*y)-16)/100,a:((500*(x-y))+128)/255,b:((200*(y-z))+128)/255}'
);

// https://github.com/matthiasmullie/minify/issues/143
$tests[] = array(
"if(nutritionalPortionWeightUnit == 'lbs' && blockUnit == 'oz'){
itemFat = (qty * (fat/nutritionalPortionWeight))/16;
itemProtein = (qty * (protein/nutritionalPortionWeight))/16;
itemCarbs = (qty * (carbs/nutritionalPortionWeight))/16;
itemKcal = (qty * (kcal/nutritionalPortionWeight))/16;
}",
"if(nutritionalPortionWeightUnit=='lbs'&&blockUnit=='oz'){itemFat=(qty*(fat/nutritionalPortionWeight))/16;itemProtein=(qty*(protein/nutritionalPortionWeight))/16;itemCarbs=(qty*(carbs/nutritionalPortionWeight))/16;itemKcal=(qty*(kcal/nutritionalPortionWeight))/16}",
);

// update tests' expected results for cross-system compatibility
foreach ($tests as &$test) {
if (!empty($test[1])) {
Expand Down

0 comments on commit 7dd8d25

Please sign in to comment.