Minifier.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. <?php
  2. /**
  3. * JShrink
  4. *
  5. * Copyright (c) 2009-2012, Robert Hafner <tedivm@tedivm.com>.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * * Neither the name of Robert Hafner nor the names of his
  21. * contributors may be used to endorse or promote products derived
  22. * from this software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  27. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  28. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  29. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  30. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  31. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  34. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35. * POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. * @package JShrink
  38. * @author Robert Hafner <tedivm@tedivm.com>
  39. * @copyright 2009-2012 Robert Hafner <tedivm@tedivm.com>
  40. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  41. * @link https://github.com/tedivm/JShrink
  42. * @version Release: 0.5.1
  43. */
  44. namespace JShrink;
  45. /**
  46. * Minifier
  47. *
  48. * Usage - Minifier::minify($js);
  49. * Usage - Minifier::minify($js, $options);
  50. * Usage - Minifier::minify($js, array('flaggedComments' => false));
  51. *
  52. * @package JShrink
  53. * @author Robert Hafner <tedivm@tedivm.com>
  54. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  55. */
  56. class Minifier
  57. {
  58. /**
  59. * The input javascript to be minified.
  60. *
  61. * @var string
  62. */
  63. protected $input;
  64. /**
  65. * The location of the character (in the input string) that is next to be
  66. * processed.
  67. *
  68. * @var int
  69. */
  70. protected $index = 0;
  71. /**
  72. * The first of the characters currently being looked at.
  73. *
  74. * @var string
  75. */
  76. protected $a = '';
  77. /**
  78. * The next character being looked at (after a);
  79. *
  80. * @var string
  81. */
  82. protected $b = '';
  83. /**
  84. * This character is only active when certain look ahead actions take place.
  85. *
  86. * @var string
  87. */
  88. protected $c;
  89. /**
  90. * Contains the options for the current minification process.
  91. *
  92. * @var array
  93. */
  94. protected $options;
  95. /**
  96. * Contains the default options for minification. This array is merged with
  97. * the one passed in by the user to create the request specific set of
  98. * options (stored in the $options attribute).
  99. *
  100. * @var array
  101. */
  102. static protected $defaultOptions = array('flaggedComments' => true);
  103. /**
  104. * Contains a copy of the JShrink object used to run minification. This is
  105. * only used internally, and is only stored for performance reasons. There
  106. * is no internal data shared between minification requests.
  107. */
  108. static protected $jshrink;
  109. /**
  110. * Minifier::minify takes a string containing javascript and removes
  111. * unneeded characters in order to shrink the code without altering it's
  112. * functionality.
  113. */
  114. static public function minify($js, $options = array())
  115. {
  116. try{
  117. ob_start();
  118. $currentOptions = array_merge(self::$defaultOptions, $options);
  119. if(!isset(self::$jshrink))
  120. self::$jshrink = new Minifier();
  121. self::$jshrink->breakdownScript($js, $currentOptions);
  122. return ob_get_clean();
  123. }catch(Exception $e){
  124. if(isset(self::$jshrink))
  125. self::$jshrink->clean();
  126. ob_end_clean();
  127. throw $e;
  128. }
  129. }
  130. /**
  131. * Processes a javascript string and outputs only the required characters,
  132. * stripping out all unneeded characters.
  133. *
  134. * @param string $js The raw javascript to be minified
  135. * @param array $currentOptions Various runtime options in an associative array
  136. */
  137. protected function breakdownScript($js, $currentOptions)
  138. {
  139. // reset work attributes in case this isn't the first run.
  140. $this->clean();
  141. $this->options = $currentOptions;
  142. $js = str_replace("\r\n", "\n", $js);
  143. $this->input = str_replace("\r", "\n", $js);
  144. $this->a = $this->getReal();
  145. // the only time the length can be higher than 1 is if a conditional
  146. // comment needs to be displayed and the only time that can happen for
  147. // $a is on the very first run
  148. while(strlen($this->a) > 1)
  149. {
  150. echo $this->a;
  151. $this->a = $this->getReal();
  152. }
  153. $this->b = $this->getReal();
  154. while($this->a !== false && !is_null($this->a) && $this->a !== '')
  155. {
  156. // now we give $b the same check for conditional comments we gave $a
  157. // before we began looping
  158. if(strlen($this->b) > 1)
  159. {
  160. echo $this->a . $this->b;
  161. $this->a = $this->getReal();
  162. $this->b = $this->getReal();
  163. continue;
  164. }
  165. switch($this->a)
  166. {
  167. // new lines
  168. case "\n":
  169. // if the next line is something that can't stand alone
  170. // preserve the newline
  171. if(strpos('(-+{[@', $this->b) !== false)
  172. {
  173. echo $this->a;
  174. $this->saveString();
  175. break;
  176. }
  177. // if its a space we move down to the string test below
  178. if($this->b === ' ')
  179. break;
  180. // otherwise we treat the newline like a space
  181. case ' ':
  182. if(self::isAlphaNumeric($this->b))
  183. echo $this->a;
  184. $this->saveString();
  185. break;
  186. default:
  187. switch($this->b)
  188. {
  189. case "\n":
  190. if(strpos('}])+-"\'', $this->a) !== false)
  191. {
  192. echo $this->a;
  193. $this->saveString();
  194. break;
  195. }else{
  196. if(self::isAlphaNumeric($this->a))
  197. {
  198. echo $this->a;
  199. $this->saveString();
  200. }
  201. }
  202. break;
  203. case ' ':
  204. if(!self::isAlphaNumeric($this->a))
  205. break;
  206. default:
  207. // check for some regex that breaks stuff
  208. if($this->a == '/' && ($this->b == '\'' || $this->b == '"'))
  209. {
  210. $this->saveRegex();
  211. continue;
  212. }
  213. echo $this->a;
  214. $this->saveString();
  215. break;
  216. }
  217. }
  218. // do reg check of doom
  219. $this->b = $this->getReal();
  220. if(($this->b == '/' && strpos('(,=:[!&|?', $this->a) !== false))
  221. $this->saveRegex();
  222. }
  223. $this->clean();
  224. }
  225. /**
  226. * Returns the next string for processing based off of the current index.
  227. *
  228. * @return string
  229. */
  230. protected function getChar()
  231. {
  232. if(isset($this->c))
  233. {
  234. $char = $this->c;
  235. unset($this->c);
  236. }else{
  237. $tchar = substr($this->input, $this->index, 1);
  238. if(isset($tchar) && $tchar !== false)
  239. {
  240. $char = $tchar;
  241. $this->index++;
  242. }else{
  243. return false;
  244. }
  245. }
  246. if($char !== "\n" && ord($char) < 32)
  247. return ' ';
  248. return $char;
  249. }
  250. /**
  251. * This function gets the next "real" character. It is essentially a wrapper
  252. * around the getChar function that skips comments. This has significant
  253. * performance benefits as the skipping is done using native functions (ie,
  254. * c code) rather than in script php.
  255. *
  256. * @return string Next 'real' character to be processed.
  257. */
  258. protected function getReal()
  259. {
  260. $startIndex = $this->index;
  261. $char = $this->getChar();
  262. if($char == '/')
  263. {
  264. $this->c = $this->getChar();
  265. if($this->c == '/')
  266. {
  267. $thirdCommentString = substr($this->input, $this->index, 1);
  268. // kill rest of line
  269. $char = $this->getNext("\n");
  270. if($thirdCommentString == '@')
  271. {
  272. $endPoint = ($this->index) - $startIndex;
  273. unset($this->c);
  274. $char = "\n" . substr($this->input, $startIndex, $endPoint);
  275. }else{
  276. $char = $this->getChar();
  277. $char = $this->getChar();
  278. }
  279. }elseif($this->c == '*'){
  280. $this->getChar(); // current C
  281. $thirdCommentString = $this->getChar();
  282. if($thirdCommentString == '@')
  283. {
  284. // conditional comment
  285. // we're gonna back up a bit and and send the comment back,
  286. // where the first char will be echoed and the rest will be
  287. // treated like a string
  288. $this->index = $this->index-2;
  289. return '/';
  290. }elseif($this->getNext('*/')){
  291. // kill everything up to the next */
  292. $this->getChar(); // get *
  293. $this->getChar(); // get /
  294. $char = $this->getChar(); // get next real character
  295. // if YUI-style comments are enabled we reinsert it into the stream
  296. if($this->options['flaggedComments'] && $thirdCommentString == '!')
  297. {
  298. $endPoint = ($this->index - 1) - $startIndex;
  299. echo "\n" . substr($this->input, $startIndex, $endPoint) . "\n";
  300. }
  301. }else{
  302. $char = false;
  303. }
  304. if($char === false)
  305. throw new \RuntimeException('Stray comment. ' . $this->index);
  306. // if we're here c is part of the comment and therefore tossed
  307. if(isset($this->c))
  308. unset($this->c);
  309. }
  310. }
  311. return $char;
  312. }
  313. /**
  314. * Pushes the index ahead to the next instance of the supplied string. If it
  315. * is found the first character of the string is returned.
  316. *
  317. * @return string|false Returns the first character of the string or false.
  318. */
  319. protected function getNext($string)
  320. {
  321. $pos = strpos($this->input, $string, $this->index);
  322. if($pos === false)
  323. return false;
  324. $this->index = $pos;
  325. return substr($this->input, $this->index, 1);
  326. }
  327. /**
  328. * When a javascript string is detected this function crawls for the end of
  329. * it and saves the whole string.
  330. *
  331. */
  332. protected function saveString()
  333. {
  334. $this->a = $this->b;
  335. if($this->a == "'" || $this->a == '"') // is the character a quote
  336. {
  337. // save literal string
  338. $stringType = $this->a;
  339. while(1)
  340. {
  341. echo $this->a;
  342. $this->a = $this->getChar();
  343. switch($this->a)
  344. {
  345. case $stringType:
  346. break 2;
  347. case "\n":
  348. throw new \RuntimeException('Unclosed string. ' . $this->index);
  349. break;
  350. case '\\':
  351. echo $this->a;
  352. $this->a = $this->getChar();
  353. }
  354. }
  355. }
  356. }
  357. /**
  358. * When a regular expression is detected this funcion crawls for the end of
  359. * it and saves the whole regex.
  360. */
  361. protected function saveRegex()
  362. {
  363. echo $this->a . $this->b;
  364. while(($this->a = $this->getChar()) !== false)
  365. {
  366. if($this->a == '/')
  367. break;
  368. if($this->a == '\\')
  369. {
  370. echo $this->a;
  371. $this->a = $this->getChar();
  372. }
  373. if($this->a == "\n")
  374. throw new \RuntimeException('Stray regex pattern. ' . $this->index);
  375. echo $this->a;
  376. }
  377. $this->b = $this->getReal();
  378. }
  379. /**
  380. * Resets attributes that do not need to be stored between requests so that
  381. * the next request is ready to go.
  382. */
  383. protected function clean()
  384. {
  385. unset($this->input);
  386. $this->index = 0;
  387. $this->a = $this->b = '';
  388. unset($this->c);
  389. unset($this->options);
  390. }
  391. /**
  392. * Checks to see if a character is alphanumeric.
  393. *
  394. * @return bool
  395. */
  396. static protected function isAlphaNumeric($char)
  397. {
  398. return preg_match('/^[\w\$]$/', $char) === 1 || $char == '/';
  399. }
  400. }