CRM_Utils_JS - also dedupe 'use strict' directive when deduping closures
[civicrm-core.git] / CRM / Utils / JS.php
CommitLineData
6a488035 1<?php
6a488035 2/*
bc77d7c0
TO
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
e70a7fc0 10 */
6a488035
TO
11
12/**
13 * Parse Javascript content and extract translatable strings.
14 *
15 * @package CRM
ca5cec67 16 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
17 */
18class CRM_Utils_JS {
6714d8d2 19
6a488035 20 /**
fe482240 21 * Parse a javascript file for translatable strings.
6a488035 22 *
77855840
TO
23 * @param string $jsCode
24 * Raw Javascript code.
a6c01b45 25 * @return array
16b10e64 26 * Array of translatable strings
6a488035
TO
27 */
28 public static function parseStrings($jsCode) {
be2fb01f 29 $strings = [];
6a488035
TO
30 // Match all calls to ts() in an array.
31 // Note: \s also matches newlines with the 's' modifier.
32 preg_match_all('~
33 [^\w]ts\s* # match "ts" with whitespace
34 \(\s* # match "(" argument list start
35 ((?:(?:\'(?:\\\\\'|[^\'])*\'|"(?:\\\\"|[^"])*")(?:\s*\+\s*)?)+)\s*
36 [,\)] # match ")" or "," to finish
37 ~sx', $jsCode, $matches);
38 foreach ($matches[1] as $text) {
39 $quote = $text[0];
40 // Remove newlines
41 $text = str_replace("\\\n", '', $text);
42 // Unescape escaped quotes
43 $text = str_replace('\\' . $quote, $quote, $text);
44 // Remove end quotes
45 $text = substr(ltrim($text, $quote), 0, -1);
46 $strings[$text] = $text;
47 }
48 return array_values($strings);
49 }
96025800 50
ad295ca9
TO
51 /**
52 * Identify duplicate, adjacent, identical closures and consolidate them.
53 *
54 * Note that you can only dedupe closures if they are directly adjacent and
55 * have exactly the same parameters.
56 *
df4b6c3f
CW
57 * Also dedupes the "use strict" directive as it is only meaningful at the beginning of a closure.
58 *
ad295ca9
TO
59 * @param array $scripts
60 * Javascript source.
61 * @param array $localVars
62 * Ordered list of JS vars to identify the start of a closure.
63 * @param array $inputVals
64 * Ordered list of input values passed into the closure.
65 * @return string
66 * Javascript source.
67 */
68 public static function dedupeClosures($scripts, $localVars, $inputVals) {
69 // Example opening: (function (angular, $, _) {
70 $opening = '\s*\(\s*function\s*\(\s*';
71 $opening .= implode(',\s*', array_map(function ($v) {
72 return preg_quote($v, '/');
73 }, $localVars));
74 $opening .= '\)\s*\{';
df4b6c3f 75 $opening = '/^' . $opening . '\s*(?:"use strict";\s|\'use strict\';\s)?/';
ad295ca9
TO
76
77 // Example closing: })(angular, CRM.$, CRM._);
78 $closing = '\}\s*\)\s*\(\s*';
79 $closing .= implode(',\s*', array_map(function ($v) {
80 return preg_quote($v, '/');
81 }, $inputVals));
82 $closing .= '\);\s*';
83 $closing = "/$closing\$/";
84
85 $scripts = array_values($scripts);
86 for ($i = count($scripts) - 1; $i > 0; $i--) {
87 if (preg_match($closing, $scripts[$i - 1]) && preg_match($opening, $scripts[$i])) {
88 $scripts[$i - 1] = preg_replace($closing, '', $scripts[$i - 1]);
89 $scripts[$i] = preg_replace($opening, '', $scripts[$i]);
90 }
91 }
92
93 return $scripts;
94 }
95
b047e061
TO
96 /**
97 * This is a primitive comment stripper. It doesn't catch all comments
98 * and falls short of minification, but it doesn't munge Angular injections
99 * and is fast enough to run synchronously (without caching).
100 *
101 * At time of writing, running this against the Angular modules, this impl
102 * of stripComments currently adds 10-20ms and cuts ~7%.
103 *
104 * Please be extremely cautious about extending this. If you want better
105 * minification, you should probably remove this implementation,
106 * import a proper JSMin implementation, and cache its output.
107 *
108 * @param string $script
109 * @return string
110 */
111 public static function stripComments($script) {
112 return preg_replace(":^\\s*//[^\n]+$:m", "", $script);
113 }
114
a49c5ad6 115 /**
9511ca30
CW
116 * Decodes a js variable (not necessarily strict json but valid js) into a php variable.
117 *
118 * This is similar to using json_decode($js, TRUE) but more forgiving about syntax.
a49c5ad6
CW
119 *
120 * ex. {a: 'Apple', 'b': "Banana", c: [1, 2, 3]}
9511ca30
CW
121 * Returns: [
122 * 'a' => 'Apple',
123 * 'b' => 'Banana',
124 * 'c' => [1, 2, 3],
125 * ]
a49c5ad6
CW
126 *
127 * @param string $js
128 * @return mixed
129 */
130 public static function decode($js) {
9511ca30 131 $js = trim($js);
d9c7a051
CW
132 $first = substr($js, 0, 1);
133 $last = substr($js, -1);
134 if ($last === $first && ($first === "'" || $first === '"')) {
9511ca30 135 // Use a temp placeholder for escaped backslashes
3807fa18
CW
136 $backslash = chr(0) . 'backslash' . chr(0);
137 return str_replace(['\\\\', "\\'", '\\"', '\\&', '\\/', $backslash], [$backslash, "'", '"', '&', '/', '\\'], substr($js, 1, -1));
a49c5ad6 138 }
d9c7a051 139 if (($first === '{' && $last === '}') || ($first === '[' && $last === ']')) {
9511ca30
CW
140 $obj = self::getRawProps($js);
141 foreach ($obj as $idx => $item) {
142 $obj[$idx] = self::decode($item);
143 }
144 return $obj;
145 }
146 return json_decode($js);
a49c5ad6
CW
147 }
148
89b24877 149 /**
10515677 150 * Encodes a variable to js notation (not strict json) suitable for e.g. an angular attribute.
89b24877 151 *
10515677
CW
152 * Like json_encode() but the output looks more like native javascript,
153 * with single quotes around strings and no unnecessary quotes around object keys.
89b24877 154 *
10515677
CW
155 * Ex input: [
156 * 'a' => 'Apple',
157 * 'b' => 'Banana',
158 * 'c' => [1, 2, 3],
159 * ]
160 * Ex output: {a: 'Apple', b: 'Banana', c: [1, 2, 3]}
161 *
162 * @param mixed $value
89b24877
CW
163 * @return string
164 */
165 public static function encode($value) {
166 if (is_array($value)) {
167 return self::writeObject($value, TRUE);
168 }
169 $result = json_encode($value, JSON_UNESCAPED_SLASHES);
10515677
CW
170 // Convert double-quotes around string to single quotes
171 if (is_string($value) && substr($result, 0, 1) === '"' && substr($result, -1) === '"') {
3807fa18
CW
172 $backslash = chr(0) . 'backslash' . chr(0);
173 return "'" . str_replace(['\\\\', '\\"', "'", $backslash], [$backslash, '"', "\\'", '\\\\'], substr($result, 1, -1)) . "'";
89b24877
CW
174 }
175 return $result;
176 }
177
3203414a 178 /**
9511ca30 179 * Gets the properties of a javascript object/array WITHOUT decoding them.
3203414a
CW
180 *
181 * Useful when the object might contain js functions, expressions, etc. which cannot be decoded.
182 * Returns an array with keys as property names and values as raw strings of js.
183 *
9511ca30
CW
184 * Ex Input: {foo: getFoo(arg), 'bar': function() {return "bar";}}
185 * Returns: [
3203414a 186 * 'foo' => 'getFoo(arg)',
9511ca30 187 * 'bar' => 'function() {return "bar";}',
3203414a
CW
188 * ]
189 *
190 * @param $js
191 * @return array
192 * @throws \Exception
193 */
9511ca30 194 public static function getRawProps($js) {
3203414a 195 $js = trim($js);
9511ca30
CW
196 if (!is_string($js) || $js === '' || !($js[0] === '{' || $js[0] === '[')) {
197 throw new Exception("Invalid js object string passed to CRM_Utils_JS::getRawProps");
3203414a
CW
198 }
199 $chars = str_split(substr($js, 1));
9511ca30
CW
200 $isEscaped = $quote = NULL;
201 $type = $js[0] === '{' ? 'object' : 'array';
202 $key = $type == 'array' ? 0 : NULL;
3203414a 203 $item = '';
9511ca30
CW
204 $end = strlen($js) - 2;
205 $quotes = ['"', "'", '/'];
206 $brackets = [
3203414a
CW
207 '}' => '{',
208 ')' => '(',
209 ']' => '[',
9511ca30 210 ':' => '?',
3203414a 211 ];
9511ca30 212 $enclosures = array_fill_keys($brackets, 0);
3203414a
CW
213 $result = [];
214 foreach ($chars as $index => $char) {
9511ca30
CW
215 if (!$isEscaped && in_array($char, $quotes, TRUE)) {
216 // Open quotes, taking care not to mistake the division symbol for opening a regex
217 if (!$quote && !($char == '/' && preg_match('{[\w)]\s*$}', $item))) {
218 $quote = $char;
219 }
220 // Close quotes
221 elseif ($char === $quote) {
222 $quote = NULL;
223 }
3203414a
CW
224 }
225 if (!$quote) {
3203414a 226 // Delineates property key
9511ca30 227 if ($char == ':' && !array_filter($enclosures) && !$key) {
3203414a
CW
228 $key = $item;
229 $item = '';
3203414a
CW
230 continue;
231 }
232 // Delineates property value
9511ca30
CW
233 if (($char == ',' || $index == $end) && !array_filter($enclosures) && isset($key) && trim($item) !== '') {
234 // Trim, unquote, and unescape characters in key
235 if ($type == 'object') {
236 $key = trim($key);
237 $key = in_array($key[0], $quotes) ? self::decode($key) : $key;
238 }
239 $result[$key] = trim($item);
240 $key = $type == 'array' ? $key + 1 : NULL;
3203414a 241 $item = '';
3203414a
CW
242 continue;
243 }
244 // Open brackets - we'll ignore delineators inside
245 if (isset($enclosures[$char])) {
246 $enclosures[$char]++;
247 }
248 // Close brackets
9511ca30
CW
249 if (isset($brackets[$char]) && $enclosures[$brackets[$char]]) {
250 $enclosures[$brackets[$char]]--;
3203414a
CW
251 }
252 }
253 $item .= $char;
9511ca30
CW
254 // We are escaping the next char if this is a backslash not preceded by an odd number of backslashes
255 $isEscaped = $char === '\\' && ((strlen($item) - strlen(rtrim($item, '\\'))) % 2);
3203414a
CW
256 }
257 return $result;
258 }
259
9511ca30
CW
260 /**
261 * Converts a php array to javascript object/array notation (not strict JSON).
262 *
263 * Does not encode keys unless they contain special characters.
264 * Does not encode values by default, so either specify $encodeValues = TRUE,
265 * or pass strings of valid js/json as values (per output from getRawProps).
266 * @see CRM_Utils_JS::getRawProps
267 *
268 * @param array $obj
269 * @param bool $encodeValues
270 * @return string
271 */
272 public static function writeObject($obj, $encodeValues = FALSE) {
273 $js = [];
274 $brackets = isset($obj[0]) && array_keys($obj) === range(0, count($obj) - 1) ? ['[', ']'] : ['{', '}'];
275 foreach ($obj as $key => $val) {
276 if ($encodeValues) {
89b24877 277 $val = self::encode($val);
9511ca30
CW
278 }
279 if ($brackets[0] == '{') {
280 // Enclose the key in quotes unless it is purely alphanumeric
281 if (preg_match('/\W/', $key)) {
282 // Prefer single quotes
283 $key = preg_match('/^[\w "]+$/', $key) ? "'" . $key . "'" : json_encode($key, JSON_UNESCAPED_SLASHES);
284 }
285 $js[] = "$key: $val";
286 }
287 else {
288 $js[] = $val;
289 }
290 }
291 return $brackets[0] . implode(', ', $js) . $brackets[1];
292 }
293
232624b1 294}