* 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
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*';
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._);)'";
// 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"];
$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"];