Skip to content

Commit

Permalink
Fix CRM_Utils_JS::dedupeClosures to ignore comments
Browse files Browse the repository at this point in the history
This runs stripComments BEFORE dedupeClosures so that comments don't interfere with the deduping.
Also fixes stripComments to remove the line break as well as the comment line.
  • Loading branch information
colemanw committed Jun 30, 2020
1 parent 06c114d commit 0290b3e
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion CRM/Utils/JS.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public static function dedupeClosures($scripts, $localVars, $inputVals) {
* @return string
*/
public static function stripComments($script) {
return preg_replace(":^\\s*//[^\n]+$:m", "", $script);
return preg_replace("#^\\s*//[^\n]*$(?:\r\n|\n)?#m", "", $script);
}

/**
Expand Down
5 changes: 2 additions & 3 deletions Civi/Angular/Page/Modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,14 @@ public static function buildAngularModules($event) {
public function digestJs($files) {
$scripts = [];
foreach ($files as $file) {
$scripts[] = file_get_contents($file);
$scripts[] = \CRM_Utils_JS::stripComments(file_get_contents($file));
}
$scripts = \CRM_Utils_JS::dedupeClosures(
$scripts,
['angular', '$', '_'],
['angular', 'CRM.$', 'CRM._']
);
// This impl of stripComments currently adds 10-20ms and cuts ~7%
return \CRM_Utils_JS::stripComments(implode("\n", $scripts));
return implode("\n", $scripts);
}

/**
Expand Down
1 change: 0 additions & 1 deletion ang/crmUi.js
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,6 @@
// $scope.myOrder.setDir('field2', '');
// HTML: <tr ng-repeat="... | order:myOrder.get()">...</tr>
.service('CrmUiOrderCtrl', function(){
//
function CrmUiOrderCtrl(defaults){
this.values = defaults;
}
Expand Down
12 changes: 8 additions & 4 deletions tests/phpunit/CRM/Utils/JSTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,23 +155,27 @@ public function stripCommentsExamples() {
$cases = [];
$cases[] = [
"a();\n//# sourceMappingURL=../foo/bar/baz.js\nb();",
"a();\n\nb();",
"a();\nb();",
];
$cases[] = [
"// foo\na();",
"\na();",
"a();",
];
$cases[] = [
"// foo\n //\na();",
"a();",
];
$cases[] = [
"b();\n // foo",
"b();\n",
];
$cases[] = [
"/// foo\na();\n\t \t//bar\nb();\n// whiz",
"\na();\n\nb();\n",
"a();\nb();\n",
];
$cases[] = [
"alert('//# sourceMappingURL=../foo/bar/baz.js');\n//zoop\na();",
"alert('//# sourceMappingURL=../foo/bar/baz.js');\n\na();",
"alert('//# sourceMappingURL=../foo/bar/baz.js');\na();",
];
return $cases;
}
Expand Down

0 comments on commit 0290b3e

Please sign in to comment.