Skip to content

Commit 7d4542b

Browse files
Alternative fix for #393
This keeps whitespace around comments while they're extracted from the content, and leaves it up to stripWhitespace later on to strip when appropriate, ensureing that only the contents of the comment are preserved or stripped (in stripComments), not the whitespace around it.
1 parent 0c77ac9 commit 7d4542b

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

src/JS.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -199,28 +199,24 @@ protected function stripComments()
199199
$minifier = $this;
200200
$callback = function ($match) use ($minifier) {
201201
if (
202-
substr($match[1], 0, 1) === '!' ||
203-
strpos($match[1], '@license') !== false ||
204-
strpos($match[1], '@preserve') !== false
202+
substr($match[2], 0, 1) === '!' ||
203+
strpos($match[2], '@license') !== false ||
204+
strpos($match[2], '@preserve') !== false
205205
) {
206206
// preserve multi-line comments that start with /*!
207207
// or contain @license or @preserve annotations
208208
$count = count($minifier->extracted);
209209
$placeholder = '/*'.$count.'*/';
210210
$minifier->extracted[$placeholder] = $match[0];
211211

212-
return $placeholder . ($match[3] ? $match[2] . $match[3] : '');
213-
}
214-
// should not remove the \n before a var|let etc. at this stage, because it could be a case like this: var a=1\n/*comment*/\nvar b=2;
215-
if($match[3]) {
216-
return $match[2] . $match[3];
212+
return $match[1] . $placeholder . $match[3];
217213
}
218214

219-
return '';
215+
return $match[1] . $match[3];
220216
};
221217

222218
// multi-line comments
223-
$this->registerPattern('/\n?\/\*(.*?)\*\/(\n?)(var|let|const|enum|function|class|)/s', $callback);
219+
$this->registerPattern('/(\n?)\/\*(.*?)\*\/(\n?)/s', $callback);
224220

225221
// single-line comments
226222
$this->registerPattern('/\/\/.*$/m', '');

tests/js/JSTest.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,6 +1344,21 @@ function someOtherFunction() {
13441344
'var crypt=function(){}
13451345
var Sbox=2',
13461346
);
1347+
$tests[] = array(
1348+
'a = function() {}
1349+
/* some comment to be removed */
1350+
b = 2',
1351+
'a=function(){}
1352+
b=2',
1353+
);
1354+
$tests[] = array(
1355+
'a = function() {}
1356+
/* @preserve some comment to be preserved */
1357+
b = 2',
1358+
'a=function(){}
1359+
/* @preserve some comment to be preserved */
1360+
b=2',
1361+
);
13471362

13481363
// known minified files to help doublecheck changes in places not yet
13491364
// anticipated in these tests
@@ -1359,13 +1374,12 @@ function someOtherFunction() {
13591374
}
13601375
}
13611376

1362-
//some other files that are minified correctly, ensure they stay like this
1377+
// some other files that are minified correctly, ensure they stay like this
13631378
// https://github.com/matthiasmullie/minify/issues/393
13641379
$source = trim(file_get_contents(__DIR__.'/sample/source/Decrypt.js'));
13651380
$minified = trim(file_get_contents(__DIR__.'/sample/minified2/Decrypt.min.js'));
13661381
$tests[] = array($source, $minified);
13671382

1368-
13691383
return $tests;
13701384
}
13711385
}

0 commit comments

Comments
 (0)