diff --git a/CRM/Utils/JS.php b/CRM/Utils/JS.php index bcd80dd79967..192a25ad09a3 100644 --- a/CRM/Utils/JS.php +++ b/CRM/Utils/JS.php @@ -54,6 +54,8 @@ public static function parseStrings($jsCode) { * 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 @@ -70,7 +72,7 @@ public static function dedupeClosures($scripts, $localVars, $inputVals) { 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*'; diff --git a/tests/phpunit/CRM/Utils/JSTest.php b/tests/phpunit/CRM/Utils/JSTest.php index aa08e911dd2f..8cf3a7ab6af3 100644 --- a/tests/phpunit/CRM/Utils/JSTest.php +++ b/tests/phpunit/CRM/Utils/JSTest.php @@ -107,8 +107,8 @@ public function testParseStrings($jsCode, $expectedStrings) { public function dedupeClosureExamples() { // Each example string here is named for its body, eg the body of $a calls "a()". - $a = "(function (angular, $, _) {\na();\n})(angular, CRM.$, CRM._);"; - $b = "(function(angular,$,_){\nb();\n})(angular,CRM.$,CRM._);"; + $a = "(function (angular, $, _) {\n 'use strict';\n a();\n})(angular, CRM.$, CRM._);"; + $b = "(function(angular,$,_){\n \"use strict\";\n b();\n})(angular,CRM.$,CRM._);"; $c = "(function( angular, $,_) {\nc();\n})(angular,CRM.$, CRM._);"; $d = "(function (angular, $, _, whiz) {\nd();\n})(angular, CRM.$, CRM._, CRM.whizbang);"; $m = "alert('i is the trickster (function( angular, $,_) {\nm();\n})(angular,CRM.$, CRM._);)'"; @@ -116,9 +116,10 @@ public function dedupeClosureExamples() { // Each example string here is a deduped combination of others, // eg "$ab" is the deduping of $a+$b. - $ab = "(function (angular, $, _) {\na();\n\nb();\n})(angular,CRM.$,CRM._);"; - $abc = "(function (angular, $, _) {\na();\n\nb();\n\nc();\n})(angular,CRM.$, CRM._);"; - $cb = "(function( angular, $,_) {\nc();\n\nb();\n})(angular,CRM.$,CRM._);"; + $ab = "(function (angular, $, _) {\n 'use strict';\n a();\n b();\n})(angular,CRM.$,CRM._);"; + $ba = "(function(angular,$,_){\n \"use strict\";\n b();\n a();\n})(angular, CRM.$, CRM._);"; + $abc = "(function (angular, $, _) {\n 'use strict';\n a();\n b();\nc();\n})(angular,CRM.$, CRM._);"; + $cb = "(function( angular, $,_) {\nc();\n b();\n})(angular,CRM.$,CRM._);"; $cases = []; $cases[] = [[$a], "$a"]; @@ -127,6 +128,7 @@ public function dedupeClosureExamples() { $cases[] = [[$d], "$d"]; $cases[] = [[$m], "$m"]; $cases[] = [[$a, $b], "$ab"]; + $cases[] = [[$b, $a], "$ba"]; $cases[] = [[$a, $m, $b], "$a$m$b"]; $cases[] = [[$a, $d], "$a$d"]; $cases[] = [[$a, $d, $b], "$a$d$b"];