7b4e5523943e1e823abb0427f60e3bd087176ba7
3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
29 * Parse Javascript content and extract translatable strings.
32 * @copyright CiviCRM LLC (c) 2004-2019
36 * Parse a javascript file for translatable strings.
38 * @param string $jsCode
39 * Raw Javascript code.
41 * Array of translatable strings
43 public static function parseStrings($jsCode) {
45 // Match all calls to ts() in an array.
46 // Note: \s also matches newlines with the 's' modifier.
48 [^\w]ts\s* # match "ts" with whitespace
49 \(\s* # match "(" argument list start
50 ((?:(?:\'(?:\\\\\'|[^\'])*\'|"(?:\\\\"|[^"])*")(?:\s*\+\s*)?)+)\s*
51 [,\)] # match ")" or "," to finish
52 ~sx', $jsCode, $matches);
53 foreach ($matches[1] as $text) {
56 $text = str_replace("\\\n", '', $text);
57 // Unescape escaped quotes
58 $text = str_replace('\\' . $quote, $quote, $text);
60 $text = substr(ltrim($text, $quote), 0, -1);
61 $strings[$text] = $text;
63 return array_values($strings);
67 * Identify duplicate, adjacent, identical closures and consolidate them.
69 * Note that you can only dedupe closures if they are directly adjacent and
70 * have exactly the same parameters.
72 * @param array $scripts
74 * @param array $localVars
75 * Ordered list of JS vars to identify the start of a closure.
76 * @param array $inputVals
77 * Ordered list of input values passed into the closure.
81 public static function dedupeClosures($scripts, $localVars, $inputVals) {
82 // Example opening: (function (angular, $, _) {
83 $opening = '\s*\(\s*function\s*\(\s*';
84 $opening .= implode(',\s*', array_map(function ($v) {
85 return preg_quote($v, '/');
87 $opening .= '\)\s*\{';
88 $opening = '/^' . $opening . '/';
90 // Example closing: })(angular, CRM.$, CRM._);
91 $closing = '\}\s*\)\s*\(\s*';
92 $closing .= implode(',\s*', array_map(function ($v) {
93 return preg_quote($v, '/');
96 $closing = "/$closing\$/";
98 $scripts = array_values($scripts);
99 for ($i = count($scripts) - 1; $i > 0; $i--) {
100 if (preg_match($closing, $scripts[$i - 1]) && preg_match($opening, $scripts[$i])) {
101 $scripts[$i - 1] = preg_replace($closing, '', $scripts[$i - 1]);
102 $scripts[$i] = preg_replace($opening, '', $scripts[$i]);
110 * This is a primitive comment stripper. It doesn't catch all comments
111 * and falls short of minification, but it doesn't munge Angular injections
112 * and is fast enough to run synchronously (without caching).
114 * At time of writing, running this against the Angular modules, this impl
115 * of stripComments currently adds 10-20ms and cuts ~7%.
117 * Please be extremely cautious about extending this. If you want better
118 * minification, you should probably remove this implementation,
119 * import a proper JSMin implementation, and cache its output.
121 * @param string $script
124 public static function stripComments($script) {
125 return preg_replace(":^\\s*//[^\n]+$:m", "", $script);