PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/libs/JSMin.php

https://github.com/learncss/Expose-Framework-Library
PHP | 354 lines | 227 code | 17 blank | 110 comment | 42 complexity | b950fcba367f0368d0c9e8c07f65ef9e MD5 | raw file
  1. <?php
  2. /**
  3. * Js Compression Engine
  4. *
  5. * @package Expose
  6. * @version 3.0.1
  7. * @author ThemeXpert http://www.themexpert.com
  8. * @copyright Copyright (C) 2010 - 2011 ThemeXpert
  9. * @license http://www.gnu.org/licenses/gpl-3.0.html GNU/GPLv3
  10. **/
  11. /**
  12. * jsmin.php - extended PHP implementation of Douglas Crockford's JSMin.
  13. *
  14. * <code>
  15. * $minifiedJs = JSMin::minify($js);
  16. * </code>
  17. *
  18. * This is a direct port of jsmin.c to PHP with a few PHP performance tweaks and
  19. * modifications to preserve some comments (see below). Also, rather than using
  20. * stdin/stdout, JSMin::minify() accepts a string as input and returns another
  21. * string as output.
  22. *
  23. * Comments containing IE conditional compilation are preserved, as are multi-line
  24. * comments that begin with "/*!" (for documentation purposes). In the latter case
  25. * newlines are inserted around the comment to enhance readability.
  26. *
  27. * PHP 5 or higher is required.
  28. *
  29. * Permission is hereby granted to use this version of the library under the
  30. * same terms as jsmin.c, which has the following license:
  31. *
  32. * --
  33. * Copyright (c) 2002 Douglas Crockford (www.crockford.com)
  34. *
  35. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  36. * this software and associated documentation files (the "Software"), to deal in
  37. * the Software without restriction, including without limitation the rights to
  38. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  39. * of the Software, and to permit persons to whom the Software is furnished to do
  40. * so, subject to the following conditions:
  41. *
  42. * The above copyright notice and this permission notice shall be included in all
  43. * copies or substantial portions of the Software.
  44. *
  45. * The Software shall be used for Good, not Evil.
  46. *
  47. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  48. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  49. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  50. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  51. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  52. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  53. * SOFTWARE.
  54. * --
  55. *
  56. * @package JSMin
  57. * @author Ryan Grove <ryan@wonko.com> (PHP port)
  58. * @author Steve Clay <steve@mrclay.org> (modifications + cleanup)
  59. * @author Andrea Giammarchi <http://www.3site.eu> (spaceBeforeRegExp)
  60. * @copyright 2002 Douglas Crockford <douglas@crockford.com> (jsmin.c)
  61. * @copyright 2008 Ryan Grove <ryan@wonko.com> (PHP port)
  62. * @license http://opensource.org/licenses/mit-license.php MIT License
  63. * @link http://code.google.com/p/jsmin-php/
  64. */
  65. class ExposeJSMin {
  66. const ORD_LF = 10;
  67. const ORD_SPACE = 32;
  68. const ACTION_KEEP_A = 1;
  69. const ACTION_DELETE_A = 2;
  70. const ACTION_DELETE_A_B = 3;
  71. protected $a = "\n";
  72. protected $b = '';
  73. protected $input = '';
  74. protected $inputIndex = 0;
  75. protected $inputLength = 0;
  76. protected $lookAhead = null;
  77. protected $output = '';
  78. /**
  79. * Minify Javascript.
  80. *
  81. * @param string $js Javascript to be minified
  82. * @return string
  83. */
  84. public static function minify($js)
  85. {
  86. $jsmin = new ExposeJSMin($js);
  87. return $jsmin->min();
  88. }
  89. /**
  90. * @param string $input
  91. */
  92. public function __construct($input)
  93. {
  94. $this->input = $input;
  95. // look out for syntax like "++ +" and "- ++"
  96. $p = '\\+';
  97. $m = '\\-';
  98. if (preg_match("/([$p$m])(?:\\1 [$p$m]| (?:$p$p|$m$m))/", $input)) {
  99. // likely pre-minified and would be broken by JSMin
  100. $this->output = $input;
  101. }
  102. }
  103. /**
  104. * Perform minification, return result
  105. */
  106. public function min()
  107. {
  108. if ($this->output !== '') { // min already run
  109. return $this->output;
  110. }
  111. $mbIntEnc = null;
  112. if (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2)) {
  113. $mbIntEnc = mb_internal_encoding();
  114. mb_internal_encoding('8bit');
  115. }
  116. $this->input = str_replace("\r\n", "\n", $this->input);
  117. $this->inputLength = strlen($this->input);
  118. $this->action(self::ACTION_DELETE_A_B);
  119. while ($this->a !== null) {
  120. // determine next command
  121. $command = self::ACTION_KEEP_A; // default
  122. if ($this->a === ' ') {
  123. if (! $this->isAlphaNum($this->b)) {
  124. $command = self::ACTION_DELETE_A;
  125. }
  126. } elseif ($this->a === "\n") {
  127. if ($this->b === ' ') {
  128. $command = self::ACTION_DELETE_A_B;
  129. // in case of mbstring.func_overload & 2, must check for null b,
  130. // otherwise mb_strpos will give WARNING
  131. } elseif ($this->b === null
  132. || (false === strpos('{[(+-', $this->b)
  133. && ! $this->isAlphaNum($this->b))) {
  134. $command = self::ACTION_DELETE_A;
  135. }
  136. } elseif (! $this->isAlphaNum($this->a)) {
  137. if ($this->b === ' '
  138. || ($this->b === "\n"
  139. && (false === strpos('}])+-"\'', $this->a)))) {
  140. $command = self::ACTION_DELETE_A_B;
  141. }
  142. }
  143. $this->action($command);
  144. }
  145. $this->output = trim($this->output);
  146. if ($mbIntEnc !== null) {
  147. mb_internal_encoding($mbIntEnc);
  148. }
  149. return $this->output;
  150. }
  151. /**
  152. * ACTION_KEEP_A = Output A. Copy B to A. Get the next B.
  153. * ACTION_DELETE_A = Copy B to A. Get the next B.
  154. * ACTION_DELETE_A_B = Get the next B.
  155. */
  156. protected function action($command)
  157. {
  158. switch ($command) {
  159. case self::ACTION_KEEP_A:
  160. $this->output .= $this->a;
  161. // fallthrough
  162. case self::ACTION_DELETE_A:
  163. $this->a = $this->b;
  164. if ($this->a === "'" || $this->a === '"') { // string literal
  165. $str = $this->a; // in case needed for exception
  166. while (true) {
  167. $this->output .= $this->a;
  168. $this->a = $this->get();
  169. if ($this->a === $this->b) { // end quote
  170. break;
  171. }
  172. if (ord($this->a) <= self::ORD_LF) {
  173. throw new JSMin_UnterminatedStringException(
  174. "JSMin: Unterminated String at byte "
  175. . $this->inputIndex . ": {$str}");
  176. }
  177. $str .= $this->a;
  178. if ($this->a === '\\') {
  179. $this->output .= $this->a;
  180. $this->a = $this->get();
  181. $str .= $this->a;
  182. }
  183. }
  184. }
  185. // fallthrough
  186. case self::ACTION_DELETE_A_B:
  187. $this->b = $this->next();
  188. if ($this->b === '/' && $this->isRegexpLiteral()) { // RegExp literal
  189. $this->output .= $this->a . $this->b;
  190. $pattern = '/'; // in case needed for exception
  191. while (true) {
  192. $this->a = $this->get();
  193. $pattern .= $this->a;
  194. if ($this->a === '/') { // end pattern
  195. break; // while (true)
  196. } elseif ($this->a === '\\') {
  197. $this->output .= $this->a;
  198. $this->a = $this->get();
  199. $pattern .= $this->a;
  200. } elseif (ord($this->a) <= self::ORD_LF) {
  201. throw new JSMin_UnterminatedRegExpException(
  202. "JSMin: Unterminated RegExp at byte "
  203. . $this->inputIndex .": {$pattern}");
  204. }
  205. $this->output .= $this->a;
  206. }
  207. $this->b = $this->next();
  208. }
  209. // end case ACTION_DELETE_A_B
  210. }
  211. }
  212. protected function isRegexpLiteral()
  213. {
  214. if (false !== strpos("\n{;(,=:[!&|?", $this->a)) { // we aren't dividing
  215. return true;
  216. }
  217. if (' ' === $this->a) {
  218. $length = strlen($this->output);
  219. if ($length < 2) { // weird edge case
  220. return true;
  221. }
  222. // you can't divide a keyword
  223. if (preg_match('/(?:case|else|in|return|typeof)$/', $this->output, $m)) {
  224. if ($this->output === $m[0]) { // odd but could happen
  225. return true;
  226. }
  227. // make sure it's a keyword, not end of an identifier
  228. $charBeforeKeyword = substr($this->output, $length - strlen($m[0]) - 1, 1);
  229. if (! $this->isAlphaNum($charBeforeKeyword)) {
  230. return true;
  231. }
  232. }
  233. }
  234. return false;
  235. }
  236. /**
  237. * Get next char. Convert ctrl char to space.
  238. */
  239. protected function get()
  240. {
  241. $c = $this->lookAhead;
  242. $this->lookAhead = null;
  243. if ($c === null) {
  244. if ($this->inputIndex < $this->inputLength) {
  245. $c = $this->input[$this->inputIndex];
  246. $this->inputIndex += 1;
  247. } else {
  248. return null;
  249. }
  250. }
  251. if ($c === "\r" || $c === "\n") {
  252. return "\n";
  253. }
  254. if (ord($c) < self::ORD_SPACE) { // control char
  255. return ' ';
  256. }
  257. return $c;
  258. }
  259. /**
  260. * Get next char. If is ctrl character, translate to a space or newline.
  261. */
  262. protected function peek()
  263. {
  264. $this->lookAhead = $this->get();
  265. return $this->lookAhead;
  266. }
  267. /**
  268. * Is $c a letter, digit, underscore, dollar sign, escape, or non-ASCII?
  269. */
  270. protected function isAlphaNum($c)
  271. {
  272. return (preg_match('/^[0-9a-zA-Z_\\$\\\\]$/', $c) || ord($c) > 126);
  273. }
  274. protected function singleLineComment()
  275. {
  276. $comment = '';
  277. while (true) {
  278. $get = $this->get();
  279. $comment .= $get;
  280. if (ord($get) <= self::ORD_LF) { // EOL reached
  281. // if IE conditional comment
  282. if (preg_match('/^\\/@(?:cc_on|if|elif|else|end)\\b/', $comment)) {
  283. return "/{$comment}";
  284. }
  285. return $get;
  286. }
  287. }
  288. }
  289. protected function multipleLineComment()
  290. {
  291. $this->get();
  292. $comment = '';
  293. while (true) {
  294. $get = $this->get();
  295. if ($get === '*') {
  296. if ($this->peek() === '/') { // end of comment reached
  297. $this->get();
  298. // if comment preserved by YUI Compressor
  299. if (0 === strpos($comment, '!')) {
  300. return "\n/*!" . substr($comment, 1) . "*/\n";
  301. }
  302. // if IE conditional comment
  303. if (preg_match('/^@(?:cc_on|if|elif|else|end)\\b/', $comment)) {
  304. return "/*{$comment}*/";
  305. }
  306. return ' ';
  307. }
  308. } elseif ($get === null) {
  309. throw new JSMin_UnterminatedCommentException(
  310. "JSMin: Unterminated comment at byte "
  311. . $this->inputIndex . ": /*{$comment}");
  312. }
  313. $comment .= $get;
  314. }
  315. }
  316. /**
  317. * Get the next character, skipping over comments.
  318. * Some comments may be preserved.
  319. */
  320. protected function next()
  321. {
  322. $get = $this->get();
  323. if ($get !== '/') {
  324. return $get;
  325. }
  326. switch ($this->peek()) {
  327. case '/': return $this->singleLineComment();
  328. case '*': return $this->multipleLineComment();
  329. default: return $get;
  330. }
  331. }
  332. }
  333. class JSMin_UnterminatedStringException extends Exception {}
  334. class JSMin_UnterminatedCommentException extends Exception {}
  335. class JSMin_UnterminatedRegExpException extends Exception {}