JSMin.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. <?php
  2. /**
  3. * JSMin.php - modified PHP implementation of Douglas Crockford's JSMin.
  4. *
  5. * <code>
  6. * $minifiedJs = JSMin::minify($js);
  7. * </code>
  8. *
  9. * This is a modified port of jsmin.c. Improvements:
  10. *
  11. * Does not choke on some regexp literals containing quote characters. E.g. /'/
  12. *
  13. * Spaces are preserved after some add/sub operators, so they are not mistakenly
  14. * converted to post-inc/dec. E.g. a + ++b -> a+ ++b
  15. *
  16. * Preserves multi-line comments that begin with /*!
  17. *
  18. * PHP 5 or higher is required.
  19. *
  20. * Permission is hereby granted to use this version of the library under the
  21. * same terms as jsmin.c, which has the following license:
  22. *
  23. * --
  24. * Copyright (c) 2002 Douglas Crockford (www.crockford.com)
  25. *
  26. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  27. * this software and associated documentation files (the "Software"), to deal in
  28. * the Software without restriction, including without limitation the rights to
  29. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  30. * of the Software, and to permit persons to whom the Software is furnished to do
  31. * so, subject to the following conditions:
  32. *
  33. * The above copyright notice and this permission notice shall be included in all
  34. * copies or substantial portions of the Software.
  35. *
  36. * The Software shall be used for Good, not Evil.
  37. *
  38. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  39. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  40. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  41. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  42. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  43. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  44. * SOFTWARE.
  45. * --
  46. *
  47. * @package JSMin
  48. * @author Ryan Grove <ryan@wonko.com> (PHP port)
  49. * @author Steve Clay <steve@mrclay.org> (modifications + cleanup)
  50. * @author Andrea Giammarchi <http://www.3site.eu> (spaceBeforeRegExp)
  51. * @copyright 2002 Douglas Crockford <douglas@crockford.com> (jsmin.c)
  52. * @copyright 2008 Ryan Grove <ryan@wonko.com> (PHP port)
  53. * @license http://opensource.org/licenses/mit-license.php MIT License
  54. * @link http://code.google.com/p/jsmin-php/
  55. */
  56. class JSMin {
  57. const ORD_LF = 10;
  58. const ORD_SPACE = 32;
  59. const ACTION_KEEP_A = 1;
  60. const ACTION_DELETE_A = 2;
  61. const ACTION_DELETE_A_B = 3;
  62. protected $a = "\n";
  63. protected $b = '';
  64. protected $input = '';
  65. protected $inputIndex = 0;
  66. protected $inputLength = 0;
  67. protected $lookAhead = null;
  68. protected $output = '';
  69. protected $lastByteOut = '';
  70. protected $keptComment = '';
  71. /**
  72. * Minify Javascript.
  73. *
  74. * @param string $js Javascript to be minified
  75. *
  76. * @return string
  77. */
  78. public static function minify($js)
  79. {
  80. $jsmin = new JSMin($js);
  81. return $jsmin->min();
  82. }
  83. /**
  84. * @param string $input
  85. */
  86. public function __construct($input)
  87. {
  88. $this->input = $input;
  89. }
  90. /**
  91. * Perform minification, return result
  92. *
  93. * @return string
  94. */
  95. public function min()
  96. {
  97. if ($this->output !== '') { // min already run
  98. return $this->output;
  99. }
  100. $mbIntEnc = null;
  101. if (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2)) {
  102. $mbIntEnc = mb_internal_encoding();
  103. mb_internal_encoding('8bit');
  104. }
  105. $this->input = str_replace("\r\n", "\n", $this->input);
  106. $this->inputLength = strlen($this->input);
  107. $this->action(self::ACTION_DELETE_A_B);
  108. while ($this->a !== null) {
  109. // determine next command
  110. $command = self::ACTION_KEEP_A; // default
  111. if ($this->a === ' ') {
  112. if (($this->lastByteOut === '+' || $this->lastByteOut === '-')
  113. && ($this->b === $this->lastByteOut)) {
  114. // Don't delete this space. If we do, the addition/subtraction
  115. // could be parsed as a post-increment
  116. } elseif (! $this->isAlphaNum($this->b)) {
  117. $command = self::ACTION_DELETE_A;
  118. }
  119. } elseif ($this->a === "\n") {
  120. if ($this->b === ' ') {
  121. $command = self::ACTION_DELETE_A_B;
  122. // in case of mbstring.func_overload & 2, must check for null b,
  123. // otherwise mb_strpos will give WARNING
  124. } elseif ($this->b === null
  125. || (false === strpos('{[(+-!~', $this->b)
  126. && ! $this->isAlphaNum($this->b))) {
  127. $command = self::ACTION_DELETE_A;
  128. }
  129. } elseif (! $this->isAlphaNum($this->a)) {
  130. if ($this->b === ' '
  131. || ($this->b === "\n"
  132. && (false === strpos('}])+-"\'', $this->a)))) {
  133. $command = self::ACTION_DELETE_A_B;
  134. }
  135. }
  136. $this->action($command);
  137. }
  138. $this->output = trim($this->output);
  139. if ($mbIntEnc !== null) {
  140. mb_internal_encoding($mbIntEnc);
  141. }
  142. return $this->output;
  143. }
  144. /**
  145. * ACTION_KEEP_A = Output A. Copy B to A. Get the next B.
  146. * ACTION_DELETE_A = Copy B to A. Get the next B.
  147. * ACTION_DELETE_A_B = Get the next B.
  148. *
  149. * @param int $command
  150. * @throws JSMin_UnterminatedRegExpException|JSMin_UnterminatedStringException
  151. */
  152. protected function action($command)
  153. {
  154. // make sure we don't compress "a + ++b" to "a+++b", etc.
  155. if ($command === self::ACTION_DELETE_A_B
  156. && $this->b === ' '
  157. && ($this->a === '+' || $this->a === '-')) {
  158. // Note: we're at an addition/substraction operator; the inputIndex
  159. // will certainly be a valid index
  160. if ($this->input[$this->inputIndex] === $this->a) {
  161. // This is "+ +" or "- -". Don't delete the space.
  162. $command = self::ACTION_KEEP_A;
  163. }
  164. }
  165. switch ($command) {
  166. case self::ACTION_KEEP_A: // 1
  167. $this->output .= $this->a;
  168. if ($this->keptComment) {
  169. $this->output = rtrim($this->output, "\n");
  170. $this->output .= $this->keptComment;
  171. $this->keptComment = '';
  172. }
  173. $this->lastByteOut = $this->a;
  174. // fallthrough intentional
  175. case self::ACTION_DELETE_A: // 2
  176. $this->a = $this->b;
  177. if ($this->a === "'" || $this->a === '"') { // string literal
  178. $str = $this->a; // in case needed for exception
  179. for(;;) {
  180. $this->output .= $this->a;
  181. $this->lastByteOut = $this->a;
  182. $this->a = $this->get();
  183. if ($this->a === $this->b) { // end quote
  184. break;
  185. }
  186. if ($this->isEOF($this->a)) {
  187. $byte = $this->inputIndex - 1;
  188. throw new JSMin_UnterminatedStringException(
  189. "JSMin: Unterminated String at byte {$byte}: {$str}");
  190. }
  191. $str .= $this->a;
  192. if ($this->a === '\\') {
  193. $this->output .= $this->a;
  194. $this->lastByteOut = $this->a;
  195. $this->a = $this->get();
  196. $str .= $this->a;
  197. }
  198. }
  199. }
  200. // fallthrough intentional
  201. case self::ACTION_DELETE_A_B: // 3
  202. $this->b = $this->next();
  203. if ($this->b === '/' && $this->isRegexpLiteral()) {
  204. $this->output .= $this->a . $this->b;
  205. $pattern = '/'; // keep entire pattern in case we need to report it in the exception
  206. for(;;) {
  207. $this->a = $this->get();
  208. $pattern .= $this->a;
  209. if ($this->a === '[') {
  210. for(;;) {
  211. $this->output .= $this->a;
  212. $this->a = $this->get();
  213. $pattern .= $this->a;
  214. if ($this->a === ']') {
  215. break;
  216. }
  217. if ($this->a === '\\') {
  218. $this->output .= $this->a;
  219. $this->a = $this->get();
  220. $pattern .= $this->a;
  221. }
  222. if ($this->isEOF($this->a)) {
  223. throw new JSMin_UnterminatedRegExpException(
  224. "JSMin: Unterminated set in RegExp at byte "
  225. . $this->inputIndex .": {$pattern}");
  226. }
  227. }
  228. }
  229. if ($this->a === '/') { // end pattern
  230. break; // while (true)
  231. } elseif ($this->a === '\\') {
  232. $this->output .= $this->a;
  233. $this->a = $this->get();
  234. $pattern .= $this->a;
  235. } elseif ($this->isEOF($this->a)) {
  236. $byte = $this->inputIndex - 1;
  237. throw new JSMin_UnterminatedRegExpException(
  238. "JSMin: Unterminated RegExp at byte {$byte}: {$pattern}");
  239. }
  240. $this->output .= $this->a;
  241. $this->lastByteOut = $this->a;
  242. }
  243. $this->b = $this->next();
  244. }
  245. // end case ACTION_DELETE_A_B
  246. }
  247. }
  248. /**
  249. * @return bool
  250. */
  251. protected function isRegexpLiteral()
  252. {
  253. if (false !== strpos("(,=:[!&|?+-~*{;", $this->a)) {
  254. // we obviously aren't dividing
  255. return true;
  256. }
  257. // we have to check for a preceding keyword, and we don't need to pattern
  258. // match over the whole output.
  259. $recentOutput = substr($this->output, -10);
  260. // check if return/typeof directly precede a pattern without a space
  261. foreach (array('return', 'typeof') as $keyword) {
  262. if ($this->a !== substr($keyword, -1)) {
  263. // certainly wasn't keyword
  264. continue;
  265. }
  266. if (preg_match("~(^|[\\s\\S])" . substr($keyword, 0, -1) . "$~", $recentOutput, $m)) {
  267. if ($m[1] === '' || !$this->isAlphaNum($m[1])) {
  268. return true;
  269. }
  270. }
  271. }
  272. // check all keywords
  273. if ($this->a === ' ' || $this->a === "\n") {
  274. if (preg_match('~(^|[\\s\\S])(?:case|else|in|return|typeof)$~', $recentOutput, $m)) {
  275. if ($m[1] === '' || !$this->isAlphaNum($m[1])) {
  276. return true;
  277. }
  278. }
  279. }
  280. return false;
  281. }
  282. /**
  283. * Return the next character from stdin. Watch out for lookahead. If the character is a control character,
  284. * translate it to a space or linefeed.
  285. *
  286. * @return string
  287. */
  288. protected function get()
  289. {
  290. $c = $this->lookAhead;
  291. $this->lookAhead = null;
  292. if ($c === null) {
  293. // getc(stdin)
  294. if ($this->inputIndex < $this->inputLength) {
  295. $c = $this->input[$this->inputIndex];
  296. $this->inputIndex += 1;
  297. } else {
  298. $c = null;
  299. }
  300. }
  301. if (ord($c) >= self::ORD_SPACE || $c === "\n" || $c === null) {
  302. return $c;
  303. }
  304. if ($c === "\r") {
  305. return "\n";
  306. }
  307. return ' ';
  308. }
  309. /**
  310. * Does $a indicate end of input?
  311. *
  312. * @param string $a
  313. * @return bool
  314. */
  315. protected function isEOF($a)
  316. {
  317. return ord($a) <= self::ORD_LF;
  318. }
  319. /**
  320. * Get next char (without getting it). If is ctrl character, translate to a space or newline.
  321. *
  322. * @return string
  323. */
  324. protected function peek()
  325. {
  326. $this->lookAhead = $this->get();
  327. return $this->lookAhead;
  328. }
  329. /**
  330. * Return true if the character is a letter, digit, underscore, dollar sign, or non-ASCII character.
  331. *
  332. * @param string $c
  333. *
  334. * @return bool
  335. */
  336. protected function isAlphaNum($c)
  337. {
  338. return (preg_match('/^[a-z0-9A-Z_\\$\\\\]$/', $c) || ord($c) > 126);
  339. }
  340. /**
  341. * Consume a single line comment from input (possibly retaining it)
  342. */
  343. protected function consumeSingleLineComment()
  344. {
  345. $comment = '';
  346. while (true) {
  347. $get = $this->get();
  348. $comment .= $get;
  349. if (ord($get) <= self::ORD_LF) { // end of line reached
  350. // if IE conditional comment
  351. if (preg_match('/^\\/@(?:cc_on|if|elif|else|end)\\b/', $comment)) {
  352. $this->keptComment .= "/{$comment}";
  353. }
  354. return;
  355. }
  356. }
  357. }
  358. /**
  359. * Consume a multiple line comment from input (possibly retaining it)
  360. *
  361. * @throws JSMin_UnterminatedCommentException
  362. */
  363. protected function consumeMultipleLineComment()
  364. {
  365. $this->get();
  366. $comment = '';
  367. for(;;) {
  368. $get = $this->get();
  369. if ($get === '*') {
  370. if ($this->peek() === '/') { // end of comment reached
  371. $this->get();
  372. if (0 === strpos($comment, '!')) {
  373. // preserved by YUI Compressor
  374. if (!$this->keptComment) {
  375. // don't prepend a newline if two comments right after one another
  376. $this->keptComment = "\n";
  377. }
  378. $this->keptComment .= "/*!" . substr($comment, 1) . "*/\n";
  379. } else if (preg_match('/^@(?:cc_on|if|elif|else|end)\\b/', $comment)) {
  380. // IE conditional
  381. $this->keptComment .= "/*{$comment}*/";
  382. }
  383. return;
  384. }
  385. } elseif ($get === null) {
  386. throw new JSMin_UnterminatedCommentException(
  387. "JSMin: Unterminated comment at byte {$this->inputIndex}: /*{$comment}");
  388. }
  389. $comment .= $get;
  390. }
  391. }
  392. /**
  393. * Get the next character, skipping over comments. Some comments may be preserved.
  394. *
  395. * @return string
  396. */
  397. protected function next()
  398. {
  399. $get = $this->get();
  400. if ($get === '/') {
  401. switch ($this->peek()) {
  402. case '/':
  403. $this->consumeSingleLineComment();
  404. $get = "\n";
  405. break;
  406. case '*':
  407. $this->consumeMultipleLineComment();
  408. $get = ' ';
  409. break;
  410. }
  411. }
  412. return $get;
  413. }
  414. }
  415. class JSMin_UnterminatedStringException extends Exception {}
  416. class JSMin_UnterminatedCommentException extends Exception {}
  417. class JSMin_UnterminatedRegExpException extends Exception {}