Merge pull request #17999 from eileenmcnaughton/sql_cache
[civicrm-core.git] / CRM / Utils / JS.php
index bcd80dd79967e5223b3518c738dc511554206d31..27e52d0d1ffb0c86acaaa11ecd4a949654563178 100644 (file)
@@ -54,13 +54,15 @@ class CRM_Utils_JS {
    * Note that you can only dedupe closures if they are directly adjacent and
    * have exactly the same parameters.
    *
+   * Also dedupes the "use strict" directive as it is only meaningful at the beginning of a closure.
+   *
    * @param array $scripts
    *   Javascript source.
    * @param array $localVars
    *   Ordered list of JS vars to identify the start of a closure.
    * @param array $inputVals
    *   Ordered list of input values passed into the closure.
-   * @return string
+   * @return string[]
    *   Javascript source.
    */
   public static function dedupeClosures($scripts, $localVars, $inputVals) {
@@ -70,7 +72,7 @@ class CRM_Utils_JS {
       return preg_quote($v, '/');
     }, $localVars));
     $opening .= '\)\s*\{';
-    $opening = '/^' . $opening . '/';
+    $opening = '/^' . $opening . '\s*(?:"use strict";\s|\'use strict\';\s)?/';
 
     // Example closing: })(angular, CRM.$, CRM._);
     $closing = '\}\s*\)\s*\(\s*';
@@ -107,7 +109,7 @@ class CRM_Utils_JS {
    * @return string
    */
   public static function stripComments($script) {
-    return preg_replace(":^\\s*//[^\n]+$:m", "", $script);
+    return preg_replace("#^\\s*//[^\n]*$(?:\r\n|\n)?#m", "", $script);
   }
 
   /**
@@ -124,6 +126,7 @@ class CRM_Utils_JS {
    *
    * @param string $js
    * @return mixed
+   * @throws Exception
    */
   public static function decode($js) {
     $js = trim($js);
@@ -187,7 +190,7 @@ class CRM_Utils_JS {
    *
    * @param $js
    * @return array
-   * @throws \Exception
+   * @throws Exception
    */
   public static function getRawProps($js) {
     $js = trim($js);