]>
git.wh0rd.org - tt-rss.git/blob - lib/jshrink/Minifier.php
5 * Copyright (c) 2009-2012, Robert Hafner <tedivm@tedivm.com>.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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.
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.
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
50 * Usage - Minifier::minify($js);
51 * Usage - Minifier::minify($js, $options);
52 * Usage - Minifier::minify($js, array('flaggedComments' => false));
55 * @author Robert Hafner <tedivm@tedivm.com>
56 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
61 * The input javascript to be minified.
68 * The location of the character (in the input string) that is next to be
76 * The first of the characters currently being looked at.
84 * The next character being looked at (after a);
91 * This character is only active when certain look ahead actions take place.
98 * Contains the options for the current minification process.
105 * Contains the default options for minification. This array is merged with
106 * the one passed in by the user to create the request specific set of
107 * options (stored in the $options attribute).
111 static protected $defaultOptions = array('flaggedComments' => true);
114 * Contains a copy of the JShrink object used to run minification. This is
115 * only used internally, and is only stored for performance reasons. There
116 * is no internal data shared between minification requests.
118 static protected $jshrink;
121 * Minifier::minify takes a string containing javascript and removes
122 * unneeded characters in order to shrink the code without altering it's
125 static public function minify($js, $options = array())
129 $currentOptions = array_merge(self
::$defaultOptions, $options);
131 if(!isset(self
::$jshrink))
132 self
::$jshrink = new Minifier();
134 self
::$jshrink->breakdownScript($js, $currentOptions);
135 return ob_get_clean();
137 }catch(Exception
$e){
138 if(isset(self
::$jshrink))
139 self
::$jshrink->clean();
147 * Processes a javascript string and outputs only the required characters,
148 * stripping out all unneeded characters.
150 * @param string $js The raw javascript to be minified
151 * @param array $currentOptions Various runtime options in an associative array
153 protected function breakdownScript($js, $currentOptions)
155 // reset work attributes in case this isn't the first run.
158 $this->options
= $currentOptions;
160 $js = str_replace("\r\n", "\n", $js);
161 $this->input
= str_replace("\r", "\n", $js);
164 $this->a
= $this->getReal();
166 // the only time the length can be higher than 1 is if a conditional
167 // comment needs to be displayed and the only time that can happen for
168 // $a is on the very first run
169 while(strlen($this->a
) > 1)
172 $this->a
= $this->getReal();
175 $this->b
= $this->getReal();
177 while($this->a
!== false && !is_null($this->a
) && $this->a
!== '')
180 // now we give $b the same check for conditional comments we gave $a
181 // before we began looping
182 if(strlen($this->b
) > 1)
184 echo $this->a
. $this->b
;
185 $this->a
= $this->getReal();
186 $this->b
= $this->getReal();
194 // if the next line is something that can't stand alone
195 // preserve the newline
196 if(strpos('(-+{[@', $this->b
) !== false)
203 // if its a space we move down to the string test below
207 // otherwise we treat the newline like a space
210 if(self
::isAlphaNumeric($this->b
))
220 if(strpos('}])+-"\'', $this->a
) !== false)
226 if(self
::isAlphaNumeric($this->a
))
235 if(!self
::isAlphaNumeric($this->a
))
239 // check for some regex that breaks stuff
240 if($this->a
== '/' && ($this->b
== '\'' ||
$this->b
== '"'))
252 // do reg check of doom
253 $this->b
= $this->getReal();
255 if(($this->b
== '/' && strpos('(,=:[!&|?', $this->a
) !== false))
262 * Returns the next string for processing based off of the current index.
266 protected function getChar()
273 $tchar = substr($this->input
, $this->index
, 1);
274 if(isset($tchar) && $tchar !== false)
283 if($char !== "\n" && ord($char) < 32)
290 * This function gets the next "real" character. It is essentially a wrapper
291 * around the getChar function that skips comments. This has significant
292 * performance benefits as the skipping is done using native functions (ie,
293 * c code) rather than in script php.
295 * @return string Next 'real' character to be processed.
297 protected function getReal()
299 $startIndex = $this->index
;
300 $char = $this->getChar();
304 $this->c
= $this->getChar();
308 $thirdCommentString = substr($this->input
, $this->index
, 1);
311 $char = $this->getNext("\n");
313 if($thirdCommentString == '@')
315 $endPoint = ($this->index
) - $startIndex;
317 $char = "\n" . substr($this->input
, $startIndex, $endPoint);
319 $char = $this->getChar();
320 $char = $this->getChar();
323 }elseif($this->c
== '*'){
325 $this->getChar(); // current C
326 $thirdCommentString = $this->getChar();
328 if($thirdCommentString == '@')
330 // conditional comment
332 // we're gonna back up a bit and and send the comment back,
333 // where the first char will be echoed and the rest will be
334 // treated like a string
335 $this->index
= $this->index
-2;
338 }elseif($this->getNext('*/')){
339 // kill everything up to the next */
341 $this->getChar(); // get *
342 $this->getChar(); // get /
344 $char = $this->getChar(); // get next real character
346 // if YUI-style comments are enabled we reinsert it into the stream
347 if($this->options
['flaggedComments'] && $thirdCommentString == '!')
349 $endPoint = ($this->index
- 1) - $startIndex;
350 echo "\n" . substr($this->input
, $startIndex, $endPoint) . "\n";
358 throw new \
RuntimeException('Stray comment. ' . $this->index
);
360 // if we're here c is part of the comment and therefore tossed
369 * Pushes the index ahead to the next instance of the supplied string. If it
370 * is found the first character of the string is returned.
372 * @return string|false Returns the first character of the string or false.
374 protected function getNext($string)
376 $pos = strpos($this->input
, $string, $this->index
);
382 return substr($this->input
, $this->index
, 1);
386 * When a javascript string is detected this function crawls for the end of
387 * it and saves the whole string.
390 protected function saveString()
393 if($this->a
== "'" ||
$this->a
== '"') // is the character a quote
395 // save literal string
396 $stringType = $this->a
;
401 $this->a
= $this->getChar();
409 throw new \
RuntimeException('Unclosed string. ' . $this->index
);
414 $this->a
= $this->getChar();
421 * When a regular expression is detected this funcion crawls for the end of
422 * it and saves the whole regex.
424 protected function saveRegex()
426 echo $this->a
. $this->b
;
428 while(($this->a
= $this->getChar()) !== false)
436 $this->a
= $this->getChar();
440 throw new \
RuntimeException('Stray regex pattern. ' . $this->index
);
444 $this->b
= $this->getReal();
448 * Resets attributes that do not need to be stored between requests so that
449 * the next request is ready to go.
451 protected function clean()
455 $this->a
= $this->b
= '';
457 unset($this->options
);
461 * Checks to see if a character is alphanumeric.
465 static protected function isAlphaNumeric($char)
467 return preg_match('/^[\w\$]$/', $char) === 1 ||
$char == '/';