PageRenderTime 41ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/system/jch_optimize/cache/jsmin.php

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