PageRenderTime 51ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/application/libraries/Minify/drivers/Minify_js.php

https://github.com/sevir/Creamy
PHP | 399 lines | 244 code | 52 blank | 103 comment | 35 complexity | e46b4a2ebf67033981c6a73732ecff77 MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter Minify
  4. *
  5. * @package ci-minify
  6. * @author Eric Barnes, F.S.Encinas
  7. * @copyright Copyright (c) Eric Barnes
  8. * @since Version 1.0
  9. * @link http://ericlbarnes.com
  10. */
  11. // ------------------------------------------------------------------------
  12. /**
  13. * Minify JS Driver
  14. *
  15. * @subpackage Drivers
  16. */
  17. class Minify_js extends CI_Driver {
  18. /**
  19. * CI Object
  20. * @var object
  21. */
  22. protected $_ci = '';
  23. // ------------------------------------------------------------------------
  24. /**
  25. * Constructor
  26. *
  27. * @return \Minify_js
  28. */
  29. public function __construct()
  30. {
  31. $this->_ci =& get_instance();
  32. log_message('debug', 'Minify JS Initialized');
  33. }
  34. // ------------------------------------------------------------------------
  35. /**
  36. * Min
  37. *
  38. * Minify a js file
  39. *
  40. * @param string $file
  41. * @param bool $compact
  42. * @return string
  43. */
  44. public function min($file = '', $compact = TRUE)
  45. {
  46. if ($file == '' OR ! file_exists($file))
  47. {
  48. log_message('error', 'Minify_js->min missing file');
  49. return FALSE;
  50. }
  51. $contents = file_get_contents($file);
  52. if ($compact != FALSE)
  53. {
  54. return trim(JSMin::minify($contents))."\n";
  55. }
  56. else
  57. {
  58. return "\n".trim($contents)."\n\n";
  59. }
  60. }
  61. }
  62. /**
  63. * jsmin.php - PHP implementation of Douglas Crockford's JSMin.
  64. *
  65. * This is pretty much a direct port of jsmin.c to PHP with just a few
  66. * PHP-specific performance tweaks. Also, whereas jsmin.c reads from stdin and
  67. * outputs to stdout, this library accepts a string as input and returns another
  68. * string as output.
  69. *
  70. * PHP 5 or higher is required.
  71. *
  72. * Permission is hereby granted to use this version of the library under the
  73. * same terms as jsmin.c, which has the following license:
  74. *
  75. * --
  76. * Copyright (c) 2002 Douglas Crockford (www.crockford.com)
  77. *
  78. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  79. * this software and associated documentation files (the "Software"), to deal in
  80. * the Software without restriction, including without limitation the rights to
  81. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  82. * of the Software, and to permit persons to whom the Software is furnished to do
  83. * so, subject to the following conditions:
  84. *
  85. * The above copyright notice and this permission notice shall be included in all
  86. * copies or substantial portions of the Software.
  87. *
  88. * The Software shall be used for Good, not Evil.
  89. *
  90. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  91. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  92. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  93. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  94. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  95. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  96. * SOFTWARE.
  97. * --
  98. *
  99. * @package JSMin
  100. * @author Ryan Grove <ryan@wonko.com>
  101. * @copyright 2002 Douglas Crockford <douglas@crockford.com> (jsmin.c)
  102. * @copyright 2008 Ryan Grove <ryan@wonko.com> (PHP port)
  103. * @license http://opensource.org/licenses/mit-license.php MIT License
  104. * @version 1.1.1 (2008-03-02)
  105. * @link https://github.com/rgrove/jsmin-php/
  106. */
  107. class JSMin {
  108. const ORD_LF = 10;
  109. const ORD_SPACE = 32;
  110. protected $a = '';
  111. protected $b = '';
  112. protected $input = '';
  113. protected $inputIndex = 0;
  114. protected $inputLength = 0;
  115. protected $lookAhead = null;
  116. protected $output = '';
  117. // -- Public Static Methods --------------------------------------------------
  118. public static function minify($js) {
  119. $jsmin = new JSMin($js);
  120. return $jsmin->min();
  121. }
  122. // -- Public Instance Methods ------------------------------------------------
  123. public function __construct($input) {
  124. $this->input = str_replace("\r\n", "\n", $input);
  125. $this->inputLength = strlen($this->input);
  126. }
  127. // -- Protected Instance Methods ---------------------------------------------
  128. /* action -- do something! What you do is determined by the argument:
  129. 1 Output A. Copy B to A. Get the next B.
  130. 2 Copy B to A. Get the next B. (Delete A).
  131. 3 Get the next B. (Delete B).
  132. action treats a string as a single character. Wow!
  133. action recognizes a regular expression if it is preceded by ( or , or =.
  134. */
  135. protected function action($d) {
  136. switch($d) {
  137. case 1:
  138. $this->output .= $this->a;
  139. case 2:
  140. $this->a = $this->b;
  141. if ($this->a === "'" || $this->a === '"') {
  142. for (;;) {
  143. $this->output .= $this->a;
  144. $this->a = $this->get();
  145. if ($this->a === $this->b) {
  146. break;
  147. }
  148. if (ord($this->a) <= self::ORD_LF) {
  149. throw new JSMinException('Unterminated string literal.');
  150. }
  151. if ($this->a === '\\') {
  152. $this->output .= $this->a;
  153. $this->a = $this->get();
  154. }
  155. }
  156. }
  157. case 3:
  158. $this->b = $this->next();
  159. if ($this->b === '/' && (
  160. $this->a === '(' || $this->a === ',' || $this->a === '=' ||
  161. $this->a === ':' || $this->a === '[' || $this->a === '!' ||
  162. $this->a === '&' || $this->a === '|' || $this->a === '?' ||
  163. $this->a === '{' || $this->a === '}' || $this->a === ';' ||
  164. $this->a === "\n" )) {
  165. $this->output .= $this->a . $this->b;
  166. for (;;) {
  167. $this->a = $this->get();
  168. if ($this->a === '[') {
  169. /*
  170. inside a regex [...] set, which MAY contain a '/' itself. Example: mootools Form.Validator near line 460:
  171. return Form.Validator.getValidator('IsEmpty').test(element) || (/^(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]\.?){0,63}[a-z0-9!#$%&'*+/=?^_`{|}~-]@(?:(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)*[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\])$/i).test(element.get('value'));
  172. */
  173. for (;;) {
  174. $this->output .= $this->a;
  175. $this->a = $this->get();
  176. if ($this->a === ']') {
  177. break;
  178. } elseif ($this->a === '\\') {
  179. $this->output .= $this->a;
  180. $this->a = $this->get();
  181. } elseif (ord($this->a) <= self::ORD_LF) {
  182. throw new JSMinException('Unterminated regular expression set in regex literal.');
  183. }
  184. }
  185. } elseif ($this->a === '/') {
  186. break;
  187. } elseif ($this->a === '\\') {
  188. $this->output .= $this->a;
  189. $this->a = $this->get();
  190. } elseif (ord($this->a) <= self::ORD_LF) {
  191. throw new JSMinException('Unterminated regular expression literal.');
  192. }
  193. $this->output .= $this->a;
  194. }
  195. $this->b = $this->next();
  196. }
  197. }
  198. }
  199. protected function get() {
  200. $c = $this->lookAhead;
  201. $this->lookAhead = null;
  202. if ($c === null) {
  203. if ($this->inputIndex < $this->inputLength) {
  204. $c = substr($this->input, $this->inputIndex, 1);
  205. $this->inputIndex += 1;
  206. } else {
  207. $c = null;
  208. }
  209. }
  210. if ($c === "\r") {
  211. return "\n";
  212. }
  213. if ($c === null || $c === "\n" || ord($c) >= self::ORD_SPACE) {
  214. return $c;
  215. }
  216. return ' ';
  217. }
  218. /* isAlphanum -- return true if the character is a letter, digit, underscore,
  219. dollar sign, or non-ASCII character.
  220. */
  221. protected function isAlphaNum($c) {
  222. return ord($c) > 126 || $c === '\\' || preg_match('/^[\w\$]$/', $c) === 1;
  223. }
  224. protected function min() {
  225. $this->a = "\n";
  226. $this->action(3);
  227. while ($this->a !== null) {
  228. switch ($this->a) {
  229. case ' ':
  230. if ($this->isAlphaNum($this->b)) {
  231. $this->action(1);
  232. } else {
  233. $this->action(2);
  234. }
  235. break;
  236. case "\n":
  237. switch ($this->b) {
  238. case '{':
  239. case '[':
  240. case '(':
  241. case '+':
  242. case '-':
  243. $this->action(1);
  244. break;
  245. case ' ':
  246. $this->action(3);
  247. break;
  248. default:
  249. if ($this->isAlphaNum($this->b)) {
  250. $this->action(1);
  251. }
  252. else {
  253. $this->action(2);
  254. }
  255. }
  256. break;
  257. default:
  258. switch ($this->b) {
  259. case ' ':
  260. if ($this->isAlphaNum($this->a)) {
  261. $this->action(1);
  262. break;
  263. }
  264. $this->action(3);
  265. break;
  266. case "\n":
  267. switch ($this->a) {
  268. case '}':
  269. case ']':
  270. case ')':
  271. case '+':
  272. case '-':
  273. case '"':
  274. case "'":
  275. $this->action(1);
  276. break;
  277. default:
  278. if ($this->isAlphaNum($this->a)) {
  279. $this->action(1);
  280. }
  281. else {
  282. $this->action(3);
  283. }
  284. }
  285. break;
  286. default:
  287. $this->action(1);
  288. break;
  289. }
  290. }
  291. }
  292. return $this->output;
  293. }
  294. /* next -- get the next character, excluding comments. peek() is used to see
  295. if a '/' is followed by a '/' or '*'.
  296. */
  297. protected function next() {
  298. $c = $this->get();
  299. if ($c === '/') {
  300. switch($this->peek()) {
  301. case '/':
  302. for (;;) {
  303. $c = $this->get();
  304. if (ord($c) <= self::ORD_LF) {
  305. return $c;
  306. }
  307. }
  308. case '*':
  309. $this->get();
  310. for (;;) {
  311. switch($this->get()) {
  312. case '*':
  313. if ($this->peek() === '/') {
  314. $this->get();
  315. return ' ';
  316. }
  317. break;
  318. case null:
  319. throw new JSMinException('Unterminated comment.');
  320. }
  321. }
  322. default:
  323. return $c;
  324. }
  325. }
  326. return $c;
  327. }
  328. protected function peek() {
  329. $this->lookAhead = $this->get();
  330. return $this->lookAhead;
  331. }
  332. }
  333. // -- Exceptions ---------------------------------------------------------------
  334. class JSMinException extends Exception {}
  335. /* End of file Minify_js.php */
  336. /* Location: ./application/libraries/Minify/drivers/Minify_js.php */