Procházet zdrojové kódy

#248 matthiasmullie/minify v.1.3.28 & matthiasmullie/path-converter v.1.0.5

Alex Trofimov před 11 roky
rodič
revize
479090dc43

+ 0 - 1
plugins/matthiasmullie/data/js/keywords_before.txt

@@ -15,7 +15,6 @@ delete
 export
 import
 public
-return
 static
 typeof
 extends

+ 1 - 1
plugins/matthiasmullie/minify/CSS.php

@@ -258,7 +258,7 @@ class CSS extends Minify
      * @param  string[optional] $path Path to write the data to.
      * @return string           The minified data.
      */
-    protected function execute($path = null)
+    public function execute($path = null)
     {
         $content = '';
 

+ 32 - 5
plugins/matthiasmullie/minify/JS.php

@@ -201,16 +201,18 @@ class JS extends Minify
         $callback = function ($match) use ($minifier) {
             $count = count($minifier->extracted);
             $placeholder = '/'.$count.'/';
-            $minifier->extracted[$placeholder] = '/'.$match[1].'/';
+            $minifier->extracted[$placeholder] = $match[1];
 
             return $placeholder;
         };
 
         // it's a regex if we can find an opening (not preceded by variable,
         // value or similar) & (non-escaped) closing /,
-        $before = $this->getOperatorsForRegex($this->operatorsBefore, '/');
-        $this->registerPattern('/^\s*+\K\/(.*?(?<!\\\\)(\\\\\\\\)*+)\//', $callback);
-        $this->registerPattern('/(?:'.implode('|', $before).')\s*+\K\/(.*?(?<!\\\\)(\\\\\\\\)*+)\//', $callback);
+        // include \n because it may be there for a reason
+        // (https://github.com/matthiasmullie/minify/issues/56)
+        $this->registerPattern('/^\s*+\K(\/.*?(?<!\\\\)(\\\\\\\\)*+\/\n?)/', $callback);
+        $before = $this->getOperatorsForRegex($this->operatorsBefore, '/') + $this->getKeywordsForRegex($this->keywordsReserved, '/');
+        $this->registerPattern('/(?:'.implode('|', $before).')\s*+\K(\/.*?(?<!\\\\)(\\\\\\\\)*+\/\n?)/', $callback);
     }
 
     /**
@@ -416,7 +418,32 @@ class JS extends Minify
         $content = preg_replace('/\bfalse\b/', '!1', $content);
 
         // for(;;) is exactly the same as while(true)
-        $content = preg_replace('/\bwhile\(!0\)/', 'for(;;)', $content);
+        $content = preg_replace('/\bwhile\(!0\){/', 'for(;;){', $content);
+
+        // now make sure we didn't turn any do ... while(true) into do ... for(;;)
+        preg_match_all('/\bdo\b/', $content, $dos, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
+
+        // go backward to make sure positional offsets aren't altered when $content changes
+        $dos = array_reverse($dos);
+        foreach ($dos as $do) {
+            $offsetDo = $do[0][1];
+
+            // find all `while` (now `for`) following `do`: one of those must be
+            // associated with the `do` and be turned back into `while`
+            preg_match_all('/\bfor\(;;\)/', $content, $whiles, PREG_OFFSET_CAPTURE | PREG_SET_ORDER, $offsetDo);
+            foreach ($whiles as $while) {
+                $offsetWhile = $while[0][1];
+
+                $open = substr_count($content, '{', $offsetDo, $offsetWhile - $offsetDo);
+                $close = substr_count($content, '}', $offsetDo, $offsetWhile - $offsetDo);
+                if ($open === $close) {
+                    // only restore `while` if amount of `{` and `}` are the same;
+                    // otherwise, that `for` isn't associated with this `do`
+                    $content = substr_replace($content, 'while(!0)', $offsetWhile, strlen('for(;;)'));
+                    break;
+                }
+            }
+        }
 
         return $content;
     }

+ 18 - 2
plugins/matthiasmullie/minify/Minify.php

@@ -1,6 +1,8 @@
 <?php
 namespace MatthiasMullie\Minify;
 
+use Psr\Cache\CacheItemInterface;
+
 /**
  * Abstract minifier class.
  *
@@ -156,13 +158,27 @@ abstract class Minify
         return $content;
     }
 
+    /**
+     * Minify the data & write it to a CacheItemInterface object.
+     *
+     * @param  CacheItemInterface $item Cache item to write the data to.
+     * @return CacheItemInterface       Cache item with the minifier data.
+     */
+    public function cache(CacheItemInterface $item)
+    {
+        $content = $this->execute();
+        $item->set($content);
+
+        return $item;
+    }
+
     /**
      * Minify the data.
      *
      * @param  string[optional] $path Path to write the data to.
      * @return string           The minified data.
      */
-    abstract protected function execute($path = null);
+    abstract public function execute($path = null);
 
     /**
      * Register a pattern to execute against the source content.
@@ -329,7 +345,7 @@ abstract class Minify
          * considered as escape-char (times 2) and to get it in the regex,
          * escaped (times 2)
          */
-        $this->registerPattern('/(['.$chars.'])(.*?((?<!\\\\)|\\\\\\\\+))\\1/s', $callback);
+        $this->registerPattern('/(['.$chars.'])(.*?(?<!\\\\)(\\\\\\\\)*+)\\1/s', $callback);
     }
 
     /**

+ 5 - 5
plugins/matthiasmullie/path-converter/Converter.php

@@ -138,11 +138,11 @@ class Converter
 
         // strip shared ancestor paths
         $shared = $this->shared($path, $to);
-        $path = substr($path, mb_strlen($shared));
-        $to = substr($to, mb_strlen($shared));
+        $path = mb_substr($path, mb_strlen($shared));
+        $to = mb_substr($to, mb_strlen($shared));
 
         // add .. for every directory that needs to be traversed to new path
-        $to = str_repeat('../', substr_count($to, '/'));
+        $to = str_repeat('../', mb_substr_count($to, '/'));
 
         return $to.ltrim($path, '/');
     }
@@ -166,12 +166,12 @@ class Converter
         // no known file/dir, start making assumptions
 
         // ends in / = dir
-        if (substr($path, -1) === '/') {
+        if (mb_substr($path, -1) === '/') {
             return rtrim($path, '/');
         }
 
         // has a dot in the name, likely a file
-        if (preg_match('/(^\/).*\..*$/', $path) !== 0) {
+        if (preg_match('/.*\..*$/', basename($path)) !== 0) {
             return dirname($path);
         }