return $scripts;
}
+ /**
+ * This is a primitive comment stripper. It doesn't catch all comments
+ * and falls short of minification, but it doesn't munge Angular injections
+ * and is fast enough to run synchronously (without caching).
+ *
+ * At time of writing, running this against the Angular modules, this impl
+ * of stripComments currently adds 10-20ms and cuts ~7%.
+ *
+ * Please be extremely cautious about extending this. If you want better
+ * minification, you should probably remove this implementation,
+ * import a proper JSMin implementation, and cache its output.
+ *
+ * @param string $script
+ * @return string
+ */
+ public static function stripComments($script) {
+ return preg_replace(":^\\s*//[^\n]+$:m", "", $script);
+ }
+
}
array('angular', '$', '_'),
array('angular', 'CRM.$', 'CRM._')
);
- return implode("\n", $scripts);
+ // This impl of stripComments currently adds 10-20ms and cuts ~7%
+ return \CRM_Utils_JS::stripComments(implode("\n", $scripts));
}
/**
);
$this->assertEquals($expectedOutput, implode("", $actualOutput));
}
+
+ public function stripCommentsExamples() {
+ $cases = array();
+ $cases[] = array(
+ "a();\n//# sourceMappingURL=../foo/bar/baz.js\nb();",
+ "a();\n\nb();",
+ );
+ $cases[] = array(
+ "// foo\na();",
+ "\na();",
+ );
+ $cases[] = array(
+ "b();\n // foo",
+ "b();\n",
+ );
+ $cases[] = array(
+ "/// foo\na();\n\t \t//bar\nb();\n// whiz",
+ "\na();\n\nb();\n",
+ );
+ $cases[] = array(
+ "alert('//# sourceMappingURL=../foo/bar/baz.js');\n//zoop\na();",
+ "alert('//# sourceMappingURL=../foo/bar/baz.js');\n\na();",
+ );
+ return $cases;
+ }
+
+ /**
+ * @param string $input
+ * @param string $expectedOutput
+ * @dataProvider stripCommentsExamples
+ */
+ public function testStripComments($input, $expectedOutput) {
+ $this->assertEquals($expectedOutput, CRM_Utils_JS::stripComments($input));
+ }
+
}