98624dd291f0761a3aca392f859a59b6e6df2f8f
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
37 * Parse a javascript file for translatable strings.
39 * @param string $jsCode
40 * Raw Javascript code.
42 * Array of translatable strings
44 public static function parseStrings($jsCode) {
46 // Match all calls to ts() in an array.
47 // Note: \s also matches newlines with the 's' modifier.
49 [^\w]ts\s* # match "ts" with whitespace
50 \(\s* # match "(" argument list start
51 ((?:(?:\'(?:\\\\\'|[^\'])*\'|"(?:\\\\"|[^"])*")(?:\s*\+\s*)?)+)\s*
52 [,\)] # match ")" or "," to finish
53 ~sx', $jsCode, $matches);
54 foreach ($matches[1] as $text) {
57 $text = str_replace("\\\n", '', $text);
58 // Unescape escaped quotes
59 $text = str_replace('\\' . $quote, $quote, $text);
61 $text = substr(ltrim($text, $quote), 0, -1);
62 $strings[$text] = $text;
64 return array_values($strings);
68 * Identify duplicate, adjacent, identical closures and consolidate them.
70 * Note that you can only dedupe closures if they are directly adjacent and
71 * have exactly the same parameters.
73 * @param array $scripts
75 * @param array $localVars
76 * Ordered list of JS vars to identify the start of a closure.
77 * @param array $inputVals
78 * Ordered list of input values passed into the closure.
82 public static function dedupeClosures($scripts, $localVars, $inputVals) {
83 // Example opening: (function (angular, $, _) {
84 $opening = '\s*\(\s*function\s*\(\s*';
85 $opening .= implode(',\s*', array_map(function ($v) {
86 return preg_quote($v, '/');
88 $opening .= '\)\s*\{';
89 $opening = '/^' . $opening . '/';
91 // Example closing: })(angular, CRM.$, CRM._);
92 $closing = '\}\s*\)\s*\(\s*';
93 $closing .= implode(',\s*', array_map(function ($v) {
94 return preg_quote($v, '/');
97 $closing = "/$closing\$/";
99 $scripts = array_values($scripts);
100 for ($i = count($scripts) - 1; $i > 0; $i--) {
101 if (preg_match($closing, $scripts[$i - 1]) && preg_match($opening, $scripts[$i])) {
102 $scripts[$i - 1] = preg_replace($closing, '', $scripts[$i - 1]);
103 $scripts[$i] = preg_replace($opening, '', $scripts[$i]);
111 * This is a primitive comment stripper. It doesn't catch all comments
112 * and falls short of minification, but it doesn't munge Angular injections
113 * and is fast enough to run synchronously (without caching).
115 * At time of writing, running this against the Angular modules, this impl
116 * of stripComments currently adds 10-20ms and cuts ~7%.
118 * Please be extremely cautious about extending this. If you want better
119 * minification, you should probably remove this implementation,
120 * import a proper JSMin implementation, and cache its output.
122 * @param string $script
125 public static function stripComments($script) {
126 return preg_replace(":^\\s*//[^\n]+$:m", "", $script);
130 * Decodes a js object (not necessarily strict json but valid js) into an array or primitive type
132 * ex. {a: 'Apple', 'b': "Banana", c: [1, 2, 3]}
137 public static function decode($js) {
138 if (!class_exists('Services_JSON')) {
139 require_once 'packages/OpenFlashChart/php-ofc-library/JSON.php';
141 $codec = new Services_JSON();
142 $result = $codec->decode($js);
143 // Converts stdClass to array
144 return json_decode(json_encode($result), TRUE);