84 protected static $defaultOptions = array(
'flaggedComments' =>
true);
92 protected $locks = array();
103 public static function minify($js, $options = array())
109 $js = $jshrink->lock($js);
110 $jshrink->minifyDirectToOutput($js, $options);
113 $js = ltrim(ob_get_clean());
114 $js = $jshrink->unlock($js);
119 }
catch (\Exception $e) {
121 if (isset($jshrink)) {
143 $this->initialize($js, $options);
156 $this->options = array_merge(static::$defaultOptions, $options);
157 $js = str_replace(
"\r\n",
"\n", $js);
158 $js = str_replace(
'/**/',
'', $js);
159 $this->input = str_replace(
"\r",
"\n", $js);
164 $this->input .= PHP_EOL;
169 $this->b = $this->getReal();
178 while ($this->a !==
false && !is_null($this->a) && $this->a !==
'') {
184 if (strpos(
'(-+{[@', $this->b) !==
false) {
198 if(static::isAlphaNumeric($this->b))
207 if (strpos(
'}])+-"\'', $this->a) !==
false) {
212 if (static::isAlphaNumeric($this->a)) {
220 if(!static::isAlphaNumeric($this->a))
225 if ($this->a ===
'/' && ($this->b ===
'\'' || $this->b ===
'"')) {
237 $this->b = $this->getReal();
239 if(($this->b ==
'/' && strpos(
'(,=:[!&|?', $this->a) !==
false))
253 $this->a = $this->b =
'';
255 unset($this->options);
266 if (isset($this->c)) {
272 $char = substr($this->input, $this->index, 1);
275 if (isset($char) && $char ===
false) {
285 if($char !==
"\n" && ord($char) < 32)
304 $startIndex = $this->index;
305 $char = $this->getChar();
312 $this->c = $this->getChar();
314 if ($this->c ===
'/') {
315 return $this->processOneLineComments($startIndex);
317 } elseif ($this->c ===
'*') {
318 return $this->processMultiLineComments($startIndex);
333 $thirdCommentString = substr($this->input, $this->index, 1);
336 $this->getNext(
"\n");
338 if ($thirdCommentString ==
'@') {
339 $endPoint = $this->index - $startIndex;
341 $char =
"\n" . substr($this->input, $startIndex, $endPoint);
345 $char = $this->getChar();
362 $thirdCommentString = $this->getChar();
365 if ($this->getNext(
'*/')) {
369 $char = $this->getChar();
372 if (($this->options[
'flaggedComments'] && $thirdCommentString ===
'!')
373 || ($thirdCommentString ===
'@') ) {
377 if ($startIndex > 0) {
382 if ($this->input[($startIndex - 1)] ===
"\n") {
387 $endPoint = ($this->index - 1) - $startIndex;
388 echo substr($this->input, $startIndex, $endPoint);
398 throw new \RuntimeException(
'Unclosed multiline comment at position: ' . ($this->index - 2));
418 $pos = strpos($this->input, $string, $this->index);
429 return substr($this->input, $this->index, 1);
440 $startpos = $this->index;
447 if ($this->a !==
"'" && $this->a !==
'"') {
452 $stringType = $this->a;
461 $this->a = $this->getChar();
476 throw new \RuntimeException(
'Unclosed string at position: ' . $startpos );
485 $this->b = $this->getChar();
488 if ($this->b ===
"\n") {
493 echo $this->a . $this->b;
513 echo $this->a . $this->b;
515 while (($this->a = $this->getChar()) !==
false) {
519 if ($this->a ===
'\\') {
521 $this->a = $this->getChar();
524 if($this->a ===
"\n")
525 throw new \RuntimeException(
'Unclosed regex pattern at position: ' . $this->index);
529 $this->b = $this->getReal();
540 return preg_match(
'/^[\w\$]$/', $char) === 1 || $char ==
'/';
552 $lock =
'"LOCK---' . crc32(time()) .
'"';
555 preg_match(
'/([+-])(\s+)([+-])/S', $js, $matches);
556 if (empty($matches)) {
560 $this->locks[$lock] = $matches[2];
562 $js = preg_replace(
'/([+-])\s+([+-])/S',
"$1{$lock}$2", $js);
576 if (empty($this->locks)) {
580 foreach ($this->locks as $lock => $replacement) {
581 $js = str_replace($lock, $replacement, $js);
unlock($js)
Replace "locks" with the original characters.
saveString()
When a javascript string is detected this function crawls for the end of it and saves the whole strin...
getReal()
This function gets the next "real" character.
static isAlphaNumeric($char)
Checks to see if a character is alphanumeric.
processMultiLineComments($startIndex)
Skips multiline comments where appropriate, and includes them where needed.
processOneLineComments($startIndex)
Removed one line comments, with the exception of some very specific types of conditional comments...
getNext($string)
Pushes the index ahead to the next instance of the supplied string.
saveRegex()
When a regular expression is detected this function crawls for the end of it and saves the whole rege...
static minify($js, $options=array())
Takes a string containing javascript and removes unneeded characters in order to shrink the code with...
clean()
Resets attributes that do not need to be stored between requests so that the next request is ready to...
loop()
The primary action occurs here.
lock($js)
Replace patterns in the given string and store the replacement.
getChar()
Returns the next string for processing based off of the current index.
minifyDirectToOutput($js, $options)
Processes a javascript string and outputs only the required characters, stripping out all unneeded ch...
initialize($js, $options)
Initializes internal variables, normalizes new lines,.