PageRenderTime 63ms CodeModel.GetById 24ms RepoModel.GetById 11ms app.codeStats 0ms

/classes/casset/jsmin.php

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