Merge pull request #13258 from eileenmcnaughton/isam
[civicrm-core.git] / CRM / Utils / JS.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3+--------------------------------------------------------------------+
fee14197 4| CiviCRM version 5 |
6a488035 5+--------------------------------------------------------------------+
8c9251b3 6| Copyright CiviCRM LLC (c) 2004-2018 |
6a488035
TO
7+--------------------------------------------------------------------+
8| This file is a part of CiviCRM. |
9| |
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. |
13| |
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. |
18| |
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+--------------------------------------------------------------------+
e70a7fc0 26 */
6a488035
TO
27
28/**
29 * Parse Javascript content and extract translatable strings.
30 *
31 * @package CRM
8c9251b3 32 * @copyright CiviCRM LLC (c) 2004-2018
6a488035
TO
33 */
34class CRM_Utils_JS {
35 /**
fe482240 36 * Parse a javascript file for translatable strings.
6a488035 37 *
77855840
TO
38 * @param string $jsCode
39 * Raw Javascript code.
a6c01b45 40 * @return array
16b10e64 41 * Array of translatable strings
6a488035
TO
42 */
43 public static function parseStrings($jsCode) {
44 $strings = array();
45 // Match all calls to ts() in an array.
46 // Note: \s also matches newlines with the 's' modifier.
47 preg_match_all('~
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) {
54 $quote = $text[0];
55 // Remove newlines
56 $text = str_replace("\\\n", '', $text);
57 // Unescape escaped quotes
58 $text = str_replace('\\' . $quote, $quote, $text);
59 // Remove end quotes
60 $text = substr(ltrim($text, $quote), 0, -1);
61 $strings[$text] = $text;
62 }
63 return array_values($strings);
64 }
96025800 65
ad295ca9
TO
66 /**
67 * Identify duplicate, adjacent, identical closures and consolidate them.
68 *
69 * Note that you can only dedupe closures if they are directly adjacent and
70 * have exactly the same parameters.
71 *
72 * @param array $scripts
73 * Javascript source.
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.
78 * @return string
79 * Javascript source.
80 */
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, '/');
86 }, $localVars));
87 $opening .= '\)\s*\{';
88 $opening = '/^' . $opening . '/';
89
90 // Example closing: })(angular, CRM.$, CRM._);
91 $closing = '\}\s*\)\s*\(\s*';
92 $closing .= implode(',\s*', array_map(function ($v) {
93 return preg_quote($v, '/');
94 }, $inputVals));
95 $closing .= '\);\s*';
96 $closing = "/$closing\$/";
97
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]);
103 }
104 }
105
106 return $scripts;
107 }
108
b047e061
TO
109 /**
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).
113 *
114 * At time of writing, running this against the Angular modules, this impl
115 * of stripComments currently adds 10-20ms and cuts ~7%.
116 *
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.
120 *
121 * @param string $script
122 * @return string
123 */
124 public static function stripComments($script) {
125 return preg_replace(":^\\s*//[^\n]+$:m", "", $script);
126 }
127
232624b1 128}