class-fl-jsmin.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. // @codingStandardsIgnoreFile
  3. /**
  4. * FLJSMin.php - Renamed original class to avoid conflicts.
  5. *
  6. * jsmin.php - PHP implementation of Douglas Crockford's JSMin.
  7. *
  8. * This is pretty much a direct port of jsmin.c to PHP with just a few
  9. * PHP-specific performance tweaks. Also, whereas jsmin.c reads from stdin and
  10. * outputs to stdout, this library accepts a string as input and returns another
  11. * string as output.
  12. *
  13. * PHP 5 or higher is required.
  14. *
  15. * Permission is hereby granted to use this version of the library under the
  16. * same terms as jsmin.c, which has the following license:
  17. *
  18. * --
  19. * Copyright (c) 2002 Douglas Crockford (www.crockford.com)
  20. *
  21. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  22. * this software and associated documentation files (the "Software"), to deal in
  23. * the Software without restriction, including without limitation the rights to
  24. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  25. * of the Software, and to permit persons to whom the Software is furnished to do
  26. * so, subject to the following conditions:
  27. *
  28. * The above copyright notice and this permission notice shall be included in all
  29. * copies or substantial portions of the Software.
  30. *
  31. * The Software shall be used for Good, not Evil.
  32. *
  33. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  34. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  35. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  36. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  37. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  38. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  39. * SOFTWARE.
  40. * --
  41. *
  42. * @package JSMin
  43. * @author Ryan Grove <ryan@wonko.com>
  44. * @copyright 2002 Douglas Crockford <douglas@crockford.com> (jsmin.c)
  45. * @copyright 2008 Ryan Grove <ryan@wonko.com> (PHP port)
  46. * @license http://opensource.org/licenses/mit-license.php MIT License
  47. * @version 1.1.1 (2008-03-02)
  48. * @link http://code.google.com/p/jsmin-php/
  49. */
  50. class FLJSMin {
  51. const ORD_LF = 10;
  52. const ORD_SPACE = 32;
  53. protected $a = '';
  54. protected $b = '';
  55. protected $input = '';
  56. protected $inputIndex = 0;
  57. protected $inputLength = 0;
  58. protected $lookAhead = null;
  59. protected $output = '';
  60. // -- Public Static Methods --------------------------------------------------
  61. public static function minify( $js ) {
  62. $jsmin = new FLJSMin( $js );
  63. return $jsmin->min();
  64. }
  65. // -- Public Instance Methods ------------------------------------------------
  66. public function __construct( $input ) {
  67. $this->input = str_replace( "\r\n", "\n", $input );
  68. $this->inputLength = strlen( $this->input );
  69. }
  70. // -- Protected Instance Methods ---------------------------------------------
  71. protected function action( $d ) {
  72. switch ( $d ) {
  73. case 1:
  74. $this->output .= $this->a;
  75. case 2:
  76. $this->a = $this->b;
  77. if ( $this->a === "'" || $this->a === '"' ) {
  78. for ( ;; ) {
  79. $this->output .= $this->a;
  80. $this->a = $this->get();
  81. if ( $this->a === $this->b ) {
  82. break;
  83. }
  84. if ( ord( $this->a ) <= self::ORD_LF ) {
  85. throw new FLJSMinException( 'Unterminated string literal.' );
  86. }
  87. if ( $this->a === '\\' ) {
  88. $this->output .= $this->a;
  89. $this->a = $this->get();
  90. }
  91. }
  92. }
  93. case 3:
  94. $this->b = $this->next();
  95. if ( $this->b === '/' && (
  96. $this->a === '(' || $this->a === ',' || $this->a === '=' ||
  97. $this->a === ':' || $this->a === '[' || $this->a === '!' ||
  98. $this->a === '&' || $this->a === '|' || $this->a === '?') ) {
  99. $this->output .= $this->a . $this->b;
  100. for ( ;; ) {
  101. $this->a = $this->get();
  102. if ( $this->a === '/' ) {
  103. break;
  104. } elseif ( $this->a === '\\' ) {
  105. $this->output .= $this->a;
  106. $this->a = $this->get();
  107. } elseif ( ord( $this->a ) <= self::ORD_LF ) {
  108. throw new FLJSMinException( 'Unterminated regular expression literal.' );
  109. }
  110. $this->output .= $this->a;
  111. }
  112. $this->b = $this->next();
  113. }
  114. }
  115. }
  116. protected function get() {
  117. $c = $this->lookAhead;
  118. $this->lookAhead = null;
  119. if ( $c === null ) {
  120. if ( $this->inputIndex < $this->inputLength ) {
  121. $c = substr( $this->input, $this->inputIndex, 1 );
  122. $this->inputIndex += 1;
  123. } else {
  124. $c = null;
  125. }
  126. }
  127. if ( $c === "\r" ) {
  128. return "\n";
  129. }
  130. if ( $c === null || $c === "\n" || ord( $c ) >= self::ORD_SPACE ) {
  131. return $c;
  132. }
  133. return ' ';
  134. }
  135. protected function isAlphaNum( $c ) {
  136. return ord( $c ) > 126 || $c === '\\' || preg_match( '/^[\w\$]$/', $c ) === 1;
  137. }
  138. protected function min() {
  139. $this->a = "\n";
  140. $this->action( 3 );
  141. while ( $this->a !== null ) {
  142. switch ( $this->a ) {
  143. case ' ':
  144. if ( $this->isAlphaNum( $this->b ) ) {
  145. $this->action( 1 );
  146. } else {
  147. $this->action( 2 );
  148. }
  149. break;
  150. case "\n":
  151. switch ( $this->b ) {
  152. case '{':
  153. case '[':
  154. case '(':
  155. case '+':
  156. case '-':
  157. $this->action( 1 );
  158. break;
  159. case ' ':
  160. $this->action( 3 );
  161. break;
  162. default:
  163. if ( $this->isAlphaNum( $this->b ) ) {
  164. $this->action( 1 );
  165. } else {
  166. $this->action( 2 );
  167. }
  168. }
  169. break;
  170. default:
  171. switch ( $this->b ) {
  172. case ' ':
  173. if ( $this->isAlphaNum( $this->a ) ) {
  174. $this->action( 1 );
  175. break;
  176. }
  177. $this->action( 3 );
  178. break;
  179. case "\n":
  180. switch ( $this->a ) {
  181. case '}':
  182. case ']':
  183. case ')':
  184. case '+':
  185. case '-':
  186. case '"':
  187. case "'":
  188. $this->action( 1 );
  189. break;
  190. default:
  191. if ( $this->isAlphaNum( $this->a ) ) {
  192. $this->action( 1 );
  193. } else {
  194. $this->action( 3 );
  195. }
  196. }
  197. break;
  198. default:
  199. $this->action( 1 );
  200. break;
  201. }
  202. }
  203. }
  204. return $this->output;
  205. }
  206. protected function next() {
  207. $c = $this->get();
  208. if ( $c === '/' ) {
  209. switch ( $this->peek() ) {
  210. case '/':
  211. for ( ;; ) {
  212. $c = $this->get();
  213. if ( ord( $c ) <= self::ORD_LF ) {
  214. return $c;
  215. }
  216. }
  217. case '*':
  218. $this->get();
  219. for ( ;; ) {
  220. switch ( $this->get() ) {
  221. case '*':
  222. if ( $this->peek() === '/' ) {
  223. $this->get();
  224. return ' ';
  225. }
  226. break;
  227. case null:
  228. throw new FLJSMinException( 'Unterminated comment.' );
  229. }
  230. }
  231. default:
  232. return $c;
  233. }
  234. }
  235. return $c;
  236. }
  237. protected function peek() {
  238. $this->lookAhead = $this->get();
  239. return $this->lookAhead;
  240. }
  241. }
  242. // -- Exceptions ---------------------------------------------------------------
  243. class FLJSMinException extends Exception {}