diff --git a/lib/.gitattributes b/lib/.gitattributes new file mode 100644 index 0000000..7e568ca --- /dev/null +++ b/lib/.gitattributes @@ -0,0 +1,9 @@ +* text=auto + +/tests export-ignore +/.gitattributes export-ignore +/.gitignore export-ignore +/.travis.yml export-ignore +/makefile export-ignore +/phpunit.xml.dist export-ignore +/README.md export-ignore diff --git a/lib/.gitignore b/lib/.gitignore new file mode 100644 index 0000000..1ae3107 --- /dev/null +++ b/lib/.gitignore @@ -0,0 +1,10 @@ +/build +/docs +/vendor +/.idea +/.project +/.cache +/.buildpath +/.settings +.*.swp +.DS_Store diff --git a/lib/.travis.yml b/lib/.travis.yml new file mode 100644 index 0000000..4d6054a --- /dev/null +++ b/lib/.travis.yml @@ -0,0 +1,18 @@ +language: php + +php: + - 7 + - 5.6 + - 5.5 + - 5.4 + - hhvm + +before_script: + - pip install --user codecov + - composer self-update && composer install --dev + +script: + - ./vendor/bin/phpunit + +after_success: + - codecov diff --git a/lib/LICENSE b/lib/LICENSE new file mode 100644 index 0000000..0c0d08a --- /dev/null +++ b/lib/LICENSE @@ -0,0 +1,18 @@ +Copyright (c) 2012 Matthias Mullie + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/lib/README.md b/lib/README.md new file mode 100644 index 0000000..2ba2b38 --- /dev/null +++ b/lib/README.md @@ -0,0 +1,140 @@ +# Minify + +[![Build status](https://api.travis-ci.org/matthiasmullie/minify.svg?branch=master)](https://travis-ci.org/matthiasmullie/minify) +[![Code coverage](http://img.shields.io/codecov/c/github/matthiasmullie/minify.svg)](https://codecov.io/github/matthiasmullie/minify) +[![Code quality](http://img.shields.io/scrutinizer/g/matthiasmullie/minify.svg)](https://scrutinizer-ci.com/g/matthiasmullie/minify) +[![Latest version](http://img.shields.io/packagist/v/matthiasmullie/minify.svg)](https://packagist.org/packages/matthiasmullie/minify) +[![Downloads total](http://img.shields.io/packagist/dt/matthiasmullie/minify.svg)](https://packagist.org/packages/matthiasmullie/minify) +[![License](http://img.shields.io/packagist/l/matthiasmullie/minify.svg)](https://github.com/matthiasmullie/minify/blob/master/LICENSE) + + +## Usage + +### CSS + +```php +use MatthiasMullie\Minify; + +$sourcePath = '/path/to/source/css/file.css'; +$minifier = new Minify\CSS($sourcePath); + +// we can even add another file, they'll then be +// joined in 1 output file +$sourcePath2 = '/path/to/second/source/css/file.css'; +$minifier->add($sourcePath2); + +// or we can just add plain CSS +$css = 'body { color: #000000; }'; +$minifier->add($css); + +// save minified file to disk +$minifiedPath = '/path/to/minified/css/file.css'; +$minifier->minify($minifiedPath); + +// or just output the content +echo $minifier->minify(); +``` + +### JS + +```php +// just look at the CSS example; it's exactly the same, but with the JS class & JS files :) +``` + + +## Methods + +Available methods, for both CSS & JS minifier, are: + +### __construct(/* overload paths */) + +The object constructor accepts 0, 1 or multiple paths of files, or even complete CSS/JS content, that should be minified. +All CSS/JS passed along, will be combined into 1 minified file. + +```php +use MatthiasMullie\Minify; +$minifier = new Minify\JS($path1, $path2); +``` + +### add($path, /* overload paths */) + +This is roughly equivalent to the constructor. + +```php +$minifier->add($path3); +$minifier->add($js); +``` + +### minify($path) + +This will minify the files' content, save the result to $path and return the resulting content. +If the $path parameter is omitted, the result will not be written anywhere. + +*CAUTION: If you have CSS with relative paths (to imports, images, ...), you should always specify a target path! Then those relative paths will be adjusted in accordance with the new path.* + +```php +$minifier->minify('/target/path.js'); +``` + +### gzip($path, $level) + +Minifies and optionally saves to a file, just like `minify()`, but it also `gzencode()`s the minified content. + +```php +$minifier->gzip('/target/path.js'); +``` + +### setMaxImportSize($size) *(CSS only)* + +The CSS minifier will automatically embed referenced files (like images, fonts, ...) into the minified CSS, so they don't have to be fetched over multiple connections. + +However, for really large files, it's likely better to load them separately (as it would increase the CSS load time if they were included.) + +This method allows the max size of files to import into the minified CSS to be set (in kB). The default size is 5. + +```php +$minifier->setMaxImportSize(10); +``` + +### setImportExtensions($extensions) *(CSS only)* + +The CSS minifier will automatically embed referenced files (like images, fonts, ...) into minified CSS, so they don't have to be fetched over multiple connections. + +This methods allows the type of files to be specified, along with their data:mime type. + +The default embedded file types are gif, png, jpg, jpeg, svg & woff. + +```php +$extensions = array( + 'gif' => 'data:image/gif', + 'png' => 'data:image/png', +); + +$minifier->setImportExtensions($extensions); +``` + + +## Installation + +Simply add a dependency on matthiasmullie/minify to your composer.json file if you use [Composer](https://getcomposer.org/) to manage the dependencies of your project: + +```sh +composer require matthiasmullie/minify +``` + +Although it's recommended to use Composer, you can actually [include these files](https://github.com/matthiasmullie/minify/issues/83) anyway you want. + + +## Try it + +Simply try it out online at . + + +## License + +Minify is [MIT](http://opensource.org/licenses/MIT) licensed. + + +## Challenges + +If you're interested in learning some of the harder technical challenges I've encountered building this, you probably want to take a look at [what I wrote about it](http://www.mullie.eu/dont-build-your-own-minifier/) on my blog. diff --git a/lib/bin/minifycss b/lib/bin/minifycss new file mode 100644 index 0000000..6a681a8 --- /dev/null +++ b/lib/bin/minifycss @@ -0,0 +1,45 @@ +#!/usr/bin/env php +minify(); +} catch (Exception $e) { + fwrite(STDERR, $e->getMessage(), PHP_EOL); + exit(1); +} diff --git a/lib/bin/minifyjs b/lib/bin/minifyjs new file mode 100644 index 0000000..4cbe63f --- /dev/null +++ b/lib/bin/minifyjs @@ -0,0 +1,45 @@ +#!/usr/bin/env php +minify(); +} catch (Exception $e) { + fwrite(STDERR, $e->getMessage(), PHP_EOL); + exit(1); +} diff --git a/lib/composer.json b/lib/composer.json new file mode 100644 index 0000000..ae1bdf0 --- /dev/null +++ b/lib/composer.json @@ -0,0 +1,34 @@ +{ + "name": "matthiasmullie/minify", + "type": "library", + "description": "CSS & JS minifier", + "keywords": ["minify", "minifier", "css", "js", "javascript"], + "homepage": "http://www.minifier.org", + "license": "MIT", + "authors": [ + { + "name": "Matthias Mullie", + "homepage": "http://www.mullie.eu", + "email": "minify@mullie.eu", + "role": "Developer" + } + ], + "require": { + "php": ">=5.3.0", + "ext-pcre": "*", + "matthiasmullie/path-converter": "~1.0" + }, + "require-dev": { + "matthiasmullie/scrapbook": "~1.0", + "phpunit/phpunit": "~4.8" + }, + "autoload": { + "psr-4": { + "MatthiasMullie\\Minify\\": "src/" + } + }, + "bin": [ + "bin/minifycss", + "bin/minifyjs" + ] +} diff --git a/lib/data/js/keywords_after.txt b/lib/data/js/keywords_after.txt new file mode 100644 index 0000000..5c8cba7 --- /dev/null +++ b/lib/data/js/keywords_after.txt @@ -0,0 +1,7 @@ +in +public +extends +private +protected +implements +instanceof \ No newline at end of file diff --git a/lib/data/js/keywords_before.txt b/lib/data/js/keywords_before.txt new file mode 100644 index 0000000..40b4ec0 --- /dev/null +++ b/lib/data/js/keywords_before.txt @@ -0,0 +1,27 @@ +do +in +let +new +var +case +else +enum +void +with +class +const +yield +delete +export +import +public +static +typeof +extends +package +private +continue +function +protected +implements +instanceof \ No newline at end of file diff --git a/lib/data/js/keywords_reserved.txt b/lib/data/js/keywords_reserved.txt new file mode 100644 index 0000000..fa6cd04 --- /dev/null +++ b/lib/data/js/keywords_reserved.txt @@ -0,0 +1,47 @@ +do +if +in +for +let +new +try +var +case +else +enum +eval +null +this +true +void +with +break +catch +class +const +false +super +throw +while +yield +delete +export +import +public +return +static +switch +typeof +default +extends +finally +package +private +continue +debugger +function +arguments +interface +protected +implements +instanceof diff --git a/lib/data/js/operators_after.txt b/lib/data/js/operators_after.txt new file mode 100644 index 0000000..2c11084 --- /dev/null +++ b/lib/data/js/operators_after.txt @@ -0,0 +1,45 @@ ++ +- +* +/ +% += ++= +-= +*= +/= +%= +<<= +>>= +>>>= +&= +^= +|= +& +| +^ +~ +<< +>> +>>> +== +=== +!= +!== +> +< +>= +<= +&& +|| +. +[ +] +? +: +, +; +( +) +{ +} \ No newline at end of file diff --git a/lib/data/js/operators_before.txt b/lib/data/js/operators_before.txt new file mode 100644 index 0000000..ff50d87 --- /dev/null +++ b/lib/data/js/operators_before.txt @@ -0,0 +1,43 @@ ++ +- +* +/ +% += ++= +-= +*= +/= +%= +<<= +>>= +>>>= +&= +^= +|= +& +| +^ +~ +<< +>> +>>> +== +=== +!= +!== +> +< +>= +<= +&& +|| +! +. +[ +? +: +, +; +( +{ diff --git a/lib/makefile b/lib/makefile new file mode 100644 index 0000000..1c40be7 --- /dev/null +++ b/lib/makefile @@ -0,0 +1,5 @@ +docs: + wget http://apigen.org/apigen.phar + chmod +x apigen.phar + php apigen.phar generate --source=src --destination=docs --template-theme=bootstrap + rm apigen.phar diff --git a/lib/phpunit.xml.dist b/lib/phpunit.xml.dist new file mode 100644 index 0000000..f33452b --- /dev/null +++ b/lib/phpunit.xml.dist @@ -0,0 +1,12 @@ + + + + + tests/css + tests/js + + + + + + diff --git a/lib/src/CSS.php b/lib/src/CSS.php new file mode 100644 index 0000000..51fb659 --- /dev/null +++ b/lib/src/CSS.php @@ -0,0 +1,573 @@ + + * @author Tijs Verkoyen + * @copyright Copyright (c) 2012, Matthias Mullie. All rights reserved. + * @license MIT License + */ +class CSS extends Minify +{ + /** + * @var int + */ + protected $maxImportSize = 5; + + /** + * @var string[] + */ + protected $importExtensions = array( + 'gif' => 'data:image/gif', + 'png' => 'data:image/png', + 'jpe' => 'data:image/jpeg', + 'jpg' => 'data:image/jpeg', + 'jpeg' => 'data:image/jpeg', + 'svg' => 'data:image/svg+xml', + 'woff' => 'data:application/x-font-woff', + 'tif' => 'image/tiff', + 'tiff' => 'image/tiff', + 'xbm' => 'image/x-xbitmap', + ); + + /** + * Set the maximum size if files to be imported. + * + * Files larger than this size (in kB) will not be imported into the CSS. + * Importing files into the CSS as data-uri will save you some connections, + * but we should only import relatively small decorative images so that our + * CSS file doesn't get too bulky. + * + * @param int $size Size in kB + */ + public function setMaxImportSize($size) + { + $this->maxImportSize = $size; + } + + /** + * Set the type of extensions to be imported into the CSS (to save network + * connections). + * Keys of the array should be the file extensions & respective values + * should be the data type. + * + * @param string[] $extensions Array of file extensions + */ + public function setImportExtensions(array $extensions) + { + $this->importExtensions = $extensions; + } + + /** + * Move any import statements to the top. + * + * @param $content string Nearly finished CSS content + * + * @return string + */ + protected function moveImportsToTop($content) + { + if (preg_match_all('/@import[^;]+;/', $content, $matches)) { + + // remove from content + foreach ($matches[0] as $import) { + $content = str_replace($import, '', $content); + } + + // add to top + $content = implode('', $matches[0]).$content; + }; + + return $content; + } + + /** + * Combine CSS from import statements. + * + * @import's will be loaded and their content merged into the original file, + * to save HTTP requests. + * + * @param string $source The file to combine imports for. + * @param string $content The CSS content to combine imports for. + * + * @return string + */ + protected function combineImports($source, $content) + { + $importRegexes = array( + // @import url(xxx) + '/ + # import statement + @import + + # whitespace + \s+ + + # open url() + url\( + + # (optional) open path enclosure + (?P["\']?) + + # fetch path + (?P + + # do not fetch data uris or external sources + (?!( + ["\']? + (data|https?): + )) + + .+? + ) + + # (optional) close path enclosure + (?P=quotes) + + # close url() + \) + + # (optional) trailing whitespace + \s* + + # (optional) media statement(s) + (?P[^;]*) + + # (optional) trailing whitespace + \s* + + # (optional) closing semi-colon + ;? + + /ix', + + // @import 'xxx' + '/ + + # import statement + @import + + # whitespace + \s+ + + # open path enclosure + (?P["\']) + + # fetch path + (?P + + # do not fetch data uris or external sources + (?!( + ["\']? + (data|https?): + )) + + .+? + ) + + # close path enclosure + (?P=quotes) + + # (optional) trailing whitespace + \s* + + # (optional) media statement(s) + (?P[^;]*) + + # (optional) trailing whitespace + \s* + + # (optional) closing semi-colon + ;? + + /ix', + ); + + // find all relative imports in css + $matches = array(); + foreach ($importRegexes as $importRegex) { + if (preg_match_all($importRegex, $content, $regexMatches, PREG_SET_ORDER)) { + $matches = array_merge($matches, $regexMatches); + } + } + + $search = array(); + $replace = array(); + + // loop the matches + foreach ($matches as $match) { + // get the path for the file that will be imported + $importPath = dirname($source).'/'.$match['path']; + + // only replace the import with the content if we can grab the + // content of the file + if (strlen($importPath) < PHP_MAXPATHLEN && file_exists($importPath) && is_file($importPath)) { + // grab referenced file & minify it (which may include importing + // yet other @import statements recursively) + $minifier = new static($importPath); + $importContent = $minifier->execute($source); + + // check if this is only valid for certain media + if ($match['media']) { + $importContent = '@media '.$match['media'].'{'.$importContent.'}'; + } + + // add to replacement array + $search[] = $match[0]; + $replace[] = $importContent; + } + } + + // replace the import statements + $content = str_replace($search, $replace, $content); + + return $content; + } + + /** + * Import files into the CSS, base64-ized. + * + * @url(image.jpg) images will be loaded and their content merged into the + * original file, to save HTTP requests. + * + * @param string $source The file to import files for. + * @param string $content The CSS content to import files for. + * + * @return string + */ + protected function importFiles($source, $content) + { + $extensions = array_keys($this->importExtensions); + $regex = '/url\((["\']?)((?!["\']?data:).*?\.('.implode('|', $extensions).'))\\1\)/i'; + if ($extensions && preg_match_all($regex, $content, $matches, PREG_SET_ORDER)) { + $search = array(); + $replace = array(); + + // loop the matches + foreach ($matches as $match) { + // get the path for the file that will be imported + $path = $match[2]; + $path = dirname($source).'/'.$path; + $extension = $match[3]; + + // only replace the import with the content if we're able to get + // the content of the file, and it's relatively small + $import = strlen($path) < PHP_MAXPATHLEN; + $import = $import && file_exists($path); + $import = $import && is_file($path); + $import = $import && filesize($path) <= $this->maxImportSize * 1024; + if (!$import) { + continue; + } + + // grab content && base64-ize + $importContent = $this->load($path); + $importContent = base64_encode($importContent); + + // build replacement + $search[] = $match[0]; + $replace[] = 'url('.$this->importExtensions[$extension].';base64,'.$importContent.')'; + } + + // replace the import statements + $content = str_replace($search, $replace, $content); + } + + return $content; + } + + /** + * Minify the data. + * Perform CSS optimizations. + * + * @param string[optional] $path Path to write the data to. + * + * @return string The minified data. + */ + public function execute($path = null) + { + $content = ''; + + // loop files + foreach ($this->data as $source => $css) { + /* + * Let's first take out strings & comments, since we can't just remove + * whitespace anywhere. If whitespace occurs inside a string, we should + * leave it alone. E.g.: + * p { content: "a test" } + */ + $this->extractStrings(); + $this->stripComments(); + $css = $this->replace($css); + + $css = $this->stripWhitespace($css); + $css = $this->shortenHex($css); + $css = $this->shortenZeroes($css); + $css = $this->stripEmptyTags($css); + + // restore the string we've extracted earlier + $css = $this->restoreExtractedData($css); + + $source = $source ?: ''; + $css = $this->combineImports($source, $css); + $css = $this->importFiles($source, $css); + + /* + * If we'll save to a new path, we'll have to fix the relative paths + * to be relative no longer to the source file, but to the new path. + * If we don't write to a file, fall back to same path so no + * conversion happens (because we still want it to go through most + * of the move code...) + */ + //$converter = new Converter($source, $path ?: $source); + //$css = $this->move($converter, $css); + + // combine css + $content .= $css; + } + + $content = $this->moveImportsToTop($content); + + return $content; + } + + /** + * Moving a css file should update all relative urls. + * Relative references (e.g. ../images/image.gif) in a certain css file, + * will have to be updated when a file is being saved at another location + * (e.g. ../../images/image.gif, if the new CSS file is 1 folder deeper). + * + * @param Converter $converter Relative path converter + * @param string $content The CSS content to update relative urls for. + * + * @return string + */ + protected function move(Converter $converter, $content) + { + /* + * Relative path references will usually be enclosed by url(). @import + * is an exception, where url() is not necessary around the path (but is + * allowed). + * This *could* be 1 regular expression, where both regular expressions + * in this array are on different sides of a |. But we're using named + * patterns in both regexes, the same name on both regexes. This is only + * possible with a (?J) modifier, but that only works after a fairly + * recent PCRE version. That's why I'm doing 2 separate regular + * expressions & combining the matches after executing of both. + */ + $relativeRegexes = array( + // url(xxx) + '/ + # open url() + url\( + + \s* + + # open path enclosure + (?P["\'])? + + # fetch path + (?P + + # do not fetch data uris or external sources + (?!( + \s? + ["\']? + (data|https?): + )) + + .+? + ) + + # close path enclosure + (?(quotes)(?P=quotes)) + + \s* + + # close url() + \) + + /ix', + + // @import "xxx" + '/ + # import statement + @import + + # whitespace + \s+ + + # we don\'t have to check for @import url(), because the + # condition above will already catch these + + # open path enclosure + (?P["\']) + + # fetch path + (?P + + # do not fetch data uris or external sources + (?!( + ["\']? + (data|https?): + )) + + .+? + ) + + # close path enclosure + (?P=quotes) + + /ix', + ); + + // find all relative urls in css + $matches = array(); + foreach ($relativeRegexes as $relativeRegex) { + if (preg_match_all($relativeRegex, $content, $regexMatches, PREG_SET_ORDER)) { + $matches = array_merge($matches, $regexMatches); + } + } + + $search = array(); + $replace = array(); + + // loop all urls + foreach ($matches as $match) { + // determine if it's a url() or an @import match + $type = (strpos($match[0], '@import') === 0 ? 'import' : 'url'); + + // fix relative url + $url = $converter->convert($match['path']); + + // build replacement + $search[] = $match[0]; + if ($type == 'url') { + $replace[] = 'url('.$url.')'; + } elseif ($type == 'import') { + $replace[] = '@import "'.$url.'"'; + } + } + + // replace urls + $content = str_replace($search, $replace, $content); + + return $content; + } + + /** + * Shorthand hex color codes. + * #FF0000 -> #F00. + * + * @param string $content The CSS content to shorten the hex color codes for. + * + * @return string + */ + protected function shortenHex($content) + { + $content = preg_replace('/(? 0) + // NOTE: it should be safe to remove all units for a 0 value, but in + // practice, Webkit (especially Safari) seems to stumble over at least + // 0%, potentially other units as well. Only stripping 'px' for now. + // @see https://github.com/matthiasmullie/minify/issues/60 + $content = preg_replace('/'.$before.'(-?0*(\.0+)?)(?<=0)px'.$after.'/', '\\1', $content); + + // strip 0-digits (.0 -> 0) + $content = preg_replace('/'.$before.'\.0+'.$units.'?'.$after.'/', '0\\1', $content); + // strip trailing 0: 50.10 -> 50.1, 50.10px -> 50.1px + $content = preg_replace('/'.$before.'(-?[0-9]+\.[0-9]+)0+'.$units.'?'.$after.'/', '\\1\\2', $content); + // strip trailing 0: 50.00 -> 50, 50.00px -> 50px + $content = preg_replace('/'.$before.'(-?[0-9]+)\.0+'.$units.'?'.$after.'/', '\\1\\2', $content); + // strip leading 0: 0.1 -> .1, 01.1 -> 1.1 + $content = preg_replace('/'.$before.'(-?)0+([0-9]*\.[0-9]+)'.$units.'?'.$after.'/', '\\1\\2\\3', $content); + + // strip negative zeroes (-0 -> 0) & truncate zeroes (00 -> 0) + $content = preg_replace('/'.$before.'-?0+'.$units.'?'.$after.'/', '0\\1', $content); + + return $content; + } + + /** + * Strip comments from source code. + * + * @param string $content + * + * @return string + */ + protected function stripEmptyTags($content) + { + return preg_replace('/(^|\})[^\{\}]+\{\s*\}/', '\\1', $content); + } + + /** + * Strip comments from source code. + */ + protected function stripComments() + { + $this->registerPattern('/\/\*.*?\*\//s', ''); + } + + /** + * Strip whitespace. + * + * @param string $content The CSS content to strip the whitespace for. + * + * @return string + */ + protected function stripWhitespace($content) + { + // remove leading & trailing whitespace + $content = preg_replace('/^\s*/m', '', $content); + $content = preg_replace('/\s*$/m', '', $content); + + // replace newlines with a single space + $content = preg_replace('/\s+/', ' ', $content); + + // remove whitespace around meta characters + // inspired by stackoverflow.com/questions/15195750/minify-compress-css-with-regex + $content = preg_replace('/\s*([\*$~^|]?+=|[{};,>~]|!important\b)\s*/', '$1', $content); + $content = preg_replace('/([\[(:])\s+/', '$1', $content); + $content = preg_replace('/\s+([\]\)])/', '$1', $content); + $content = preg_replace('/\s+(:)(?![^\}]*\{)/', '$1', $content); + + // whitespace around + and - can only be stripped in selectors, like + // :nth-child(3+2n), not in things like calc(3px + 2px) or shorthands + // like 3px -2px + $content = preg_replace('/\s*([+-])\s*(?=[^}]*{)/', '$1', $content); + + // remove semicolon/whitespace followed by closing bracket + $content = str_replace(';}', '}', $content); + + return trim($content); + } +} diff --git a/lib/src/Exception.php b/lib/src/Exception.php new file mode 100644 index 0000000..5f2af0d --- /dev/null +++ b/lib/src/Exception.php @@ -0,0 +1,10 @@ + + */ +class Exception extends \Exception +{ +} diff --git a/lib/src/JS.php b/lib/src/JS.php new file mode 100644 index 0000000..f0bd4fb --- /dev/null +++ b/lib/src/JS.php @@ -0,0 +1,490 @@ + + * @author Tijs Verkoyen + * @copyright Copyright (c) 2012, Matthias Mullie. All rights reserved. + * @license MIT License + */ +class JS extends Minify +{ + /** + * Var-matching regex based on http://stackoverflow.com/a/9337047/802993. + * + * Note that regular expressions using that bit must have the PCRE_UTF8 + * pattern modifier (/u) set. + * + * @var string + */ + const REGEX_VARIABLE = '\b[$A-Z\_a-z\xaa\xb5\xba\xc0-\xd6\xd8-\xf6\xf8-\x{02c1}\x{02c6}-\x{02d1}\x{02e0}-\x{02e4}\x{02ec}\x{02ee}\x{0370}-\x{0374}\x{0376}\x{0377}\x{037a}-\x{037d}\x{0386}\x{0388}-\x{038a}\x{038c}\x{038e}-\x{03a1}\x{03a3}-\x{03f5}\x{03f7}-\x{0481}\x{048a}-\x{0527}\x{0531}-\x{0556}\x{0559}\x{0561}-\x{0587}\x{05d0}-\x{05ea}\x{05f0}-\x{05f2}\x{0620}-\x{064a}\x{066e}\x{066f}\x{0671}-\x{06d3}\x{06d5}\x{06e5}\x{06e6}\x{06ee}\x{06ef}\x{06fa}-\x{06fc}\x{06ff}\x{0710}\x{0712}-\x{072f}\x{074d}-\x{07a5}\x{07b1}\x{07ca}-\x{07ea}\x{07f4}\x{07f5}\x{07fa}\x{0800}-\x{0815}\x{081a}\x{0824}\x{0828}\x{0840}-\x{0858}\x{08a0}\x{08a2}-\x{08ac}\x{0904}-\x{0939}\x{093d}\x{0950}\x{0958}-\x{0961}\x{0971}-\x{0977}\x{0979}-\x{097f}\x{0985}-\x{098c}\x{098f}\x{0990}\x{0993}-\x{09a8}\x{09aa}-\x{09b0}\x{09b2}\x{09b6}-\x{09b9}\x{09bd}\x{09ce}\x{09dc}\x{09dd}\x{09df}-\x{09e1}\x{09f0}\x{09f1}\x{0a05}-\x{0a0a}\x{0a0f}\x{0a10}\x{0a13}-\x{0a28}\x{0a2a}-\x{0a30}\x{0a32}\x{0a33}\x{0a35}\x{0a36}\x{0a38}\x{0a39}\x{0a59}-\x{0a5c}\x{0a5e}\x{0a72}-\x{0a74}\x{0a85}-\x{0a8d}\x{0a8f}-\x{0a91}\x{0a93}-\x{0aa8}\x{0aaa}-\x{0ab0}\x{0ab2}\x{0ab3}\x{0ab5}-\x{0ab9}\x{0abd}\x{0ad0}\x{0ae0}\x{0ae1}\x{0b05}-\x{0b0c}\x{0b0f}\x{0b10}\x{0b13}-\x{0b28}\x{0b2a}-\x{0b30}\x{0b32}\x{0b33}\x{0b35}-\x{0b39}\x{0b3d}\x{0b5c}\x{0b5d}\x{0b5f}-\x{0b61}\x{0b71}\x{0b83}\x{0b85}-\x{0b8a}\x{0b8e}-\x{0b90}\x{0b92}-\x{0b95}\x{0b99}\x{0b9a}\x{0b9c}\x{0b9e}\x{0b9f}\x{0ba3}\x{0ba4}\x{0ba8}-\x{0baa}\x{0bae}-\x{0bb9}\x{0bd0}\x{0c05}-\x{0c0c}\x{0c0e}-\x{0c10}\x{0c12}-\x{0c28}\x{0c2a}-\x{0c33}\x{0c35}-\x{0c39}\x{0c3d}\x{0c58}\x{0c59}\x{0c60}\x{0c61}\x{0c85}-\x{0c8c}\x{0c8e}-\x{0c90}\x{0c92}-\x{0ca8}\x{0caa}-\x{0cb3}\x{0cb5}-\x{0cb9}\x{0cbd}\x{0cde}\x{0ce0}\x{0ce1}\x{0cf1}\x{0cf2}\x{0d05}-\x{0d0c}\x{0d0e}-\x{0d10}\x{0d12}-\x{0d3a}\x{0d3d}\x{0d4e}\x{0d60}\x{0d61}\x{0d7a}-\x{0d7f}\x{0d85}-\x{0d96}\x{0d9a}-\x{0db1}\x{0db3}-\x{0dbb}\x{0dbd}\x{0dc0}-\x{0dc6}\x{0e01}-\x{0e30}\x{0e32}\x{0e33}\x{0e40}-\x{0e46}\x{0e81}\x{0e82}\x{0e84}\x{0e87}\x{0e88}\x{0e8a}\x{0e8d}\x{0e94}-\x{0e97}\x{0e99}-\x{0e9f}\x{0ea1}-\x{0ea3}\x{0ea5}\x{0ea7}\x{0eaa}\x{0eab}\x{0ead}-\x{0eb0}\x{0eb2}\x{0eb3}\x{0ebd}\x{0ec0}-\x{0ec4}\x{0ec6}\x{0edc}-\x{0edf}\x{0f00}\x{0f40}-\x{0f47}\x{0f49}-\x{0f6c}\x{0f88}-\x{0f8c}\x{1000}-\x{102a}\x{103f}\x{1050}-\x{1055}\x{105a}-\x{105d}\x{1061}\x{1065}\x{1066}\x{106e}-\x{1070}\x{1075}-\x{1081}\x{108e}\x{10a0}-\x{10c5}\x{10c7}\x{10cd}\x{10d0}-\x{10fa}\x{10fc}-\x{1248}\x{124a}-\x{124d}\x{1250}-\x{1256}\x{1258}\x{125a}-\x{125d}\x{1260}-\x{1288}\x{128a}-\x{128d}\x{1290}-\x{12b0}\x{12b2}-\x{12b5}\x{12b8}-\x{12be}\x{12c0}\x{12c2}-\x{12c5}\x{12c8}-\x{12d6}\x{12d8}-\x{1310}\x{1312}-\x{1315}\x{1318}-\x{135a}\x{1380}-\x{138f}\x{13a0}-\x{13f4}\x{1401}-\x{166c}\x{166f}-\x{167f}\x{1681}-\x{169a}\x{16a0}-\x{16ea}\x{16ee}-\x{16f0}\x{1700}-\x{170c}\x{170e}-\x{1711}\x{1720}-\x{1731}\x{1740}-\x{1751}\x{1760}-\x{176c}\x{176e}-\x{1770}\x{1780}-\x{17b3}\x{17d7}\x{17dc}\x{1820}-\x{1877}\x{1880}-\x{18a8}\x{18aa}\x{18b0}-\x{18f5}\x{1900}-\x{191c}\x{1950}-\x{196d}\x{1970}-\x{1974}\x{1980}-\x{19ab}\x{19c1}-\x{19c7}\x{1a00}-\x{1a16}\x{1a20}-\x{1a54}\x{1aa7}\x{1b05}-\x{1b33}\x{1b45}-\x{1b4b}\x{1b83}-\x{1ba0}\x{1bae}\x{1baf}\x{1bba}-\x{1be5}\x{1c00}-\x{1c23}\x{1c4d}-\x{1c4f}\x{1c5a}-\x{1c7d}\x{1ce9}-\x{1cec}\x{1cee}-\x{1cf1}\x{1cf5}\x{1cf6}\x{1d00}-\x{1dbf}\x{1e00}-\x{1f15}\x{1f18}-\x{1f1d}\x{1f20}-\x{1f45}\x{1f48}-\x{1f4d}\x{1f50}-\x{1f57}\x{1f59}\x{1f5b}\x{1f5d}\x{1f5f}-\x{1f7d}\x{1f80}-\x{1fb4}\x{1fb6}-\x{1fbc}\x{1fbe}\x{1fc2}-\x{1fc4}\x{1fc6}-\x{1fcc}\x{1fd0}-\x{1fd3}\x{1fd6}-\x{1fdb}\x{1fe0}-\x{1fec}\x{1ff2}-\x{1ff4}\x{1ff6}-\x{1ffc}\x{2071}\x{207f}\x{2090}-\x{209c}\x{2102}\x{2107}\x{210a}-\x{2113}\x{2115}\x{2119}-\x{211d}\x{2124}\x{2126}\x{2128}\x{212a}-\x{212d}\x{212f}-\x{2139}\x{213c}-\x{213f}\x{2145}-\x{2149}\x{214e}\x{2160}-\x{2188}\x{2c00}-\x{2c2e}\x{2c30}-\x{2c5e}\x{2c60}-\x{2ce4}\x{2ceb}-\x{2cee}\x{2cf2}\x{2cf3}\x{2d00}-\x{2d25}\x{2d27}\x{2d2d}\x{2d30}-\x{2d67}\x{2d6f}\x{2d80}-\x{2d96}\x{2da0}-\x{2da6}\x{2da8}-\x{2dae}\x{2db0}-\x{2db6}\x{2db8}-\x{2dbe}\x{2dc0}-\x{2dc6}\x{2dc8}-\x{2dce}\x{2dd0}-\x{2dd6}\x{2dd8}-\x{2dde}\x{2e2f}\x{3005}-\x{3007}\x{3021}-\x{3029}\x{3031}-\x{3035}\x{3038}-\x{303c}\x{3041}-\x{3096}\x{309d}-\x{309f}\x{30a1}-\x{30fa}\x{30fc}-\x{30ff}\x{3105}-\x{312d}\x{3131}-\x{318e}\x{31a0}-\x{31ba}\x{31f0}-\x{31ff}\x{3400}-\x{4db5}\x{4e00}-\x{9fcc}\x{a000}-\x{a48c}\x{a4d0}-\x{a4fd}\x{a500}-\x{a60c}\x{a610}-\x{a61f}\x{a62a}\x{a62b}\x{a640}-\x{a66e}\x{a67f}-\x{a697}\x{a6a0}-\x{a6ef}\x{a717}-\x{a71f}\x{a722}-\x{a788}\x{a78b}-\x{a78e}\x{a790}-\x{a793}\x{a7a0}-\x{a7aa}\x{a7f8}-\x{a801}\x{a803}-\x{a805}\x{a807}-\x{a80a}\x{a80c}-\x{a822}\x{a840}-\x{a873}\x{a882}-\x{a8b3}\x{a8f2}-\x{a8f7}\x{a8fb}\x{a90a}-\x{a925}\x{a930}-\x{a946}\x{a960}-\x{a97c}\x{a984}-\x{a9b2}\x{a9cf}\x{aa00}-\x{aa28}\x{aa40}-\x{aa42}\x{aa44}-\x{aa4b}\x{aa60}-\x{aa76}\x{aa7a}\x{aa80}-\x{aaaf}\x{aab1}\x{aab5}\x{aab6}\x{aab9}-\x{aabd}\x{aac0}\x{aac2}\x{aadb}-\x{aadd}\x{aae0}-\x{aaea}\x{aaf2}-\x{aaf4}\x{ab01}-\x{ab06}\x{ab09}-\x{ab0e}\x{ab11}-\x{ab16}\x{ab20}-\x{ab26}\x{ab28}-\x{ab2e}\x{abc0}-\x{abe2}\x{ac00}-\x{d7a3}\x{d7b0}-\x{d7c6}\x{d7cb}-\x{d7fb}\x{f900}-\x{fa6d}\x{fa70}-\x{fad9}\x{fb00}-\x{fb06}\x{fb13}-\x{fb17}\x{fb1d}\x{fb1f}-\x{fb28}\x{fb2a}-\x{fb36}\x{fb38}-\x{fb3c}\x{fb3e}\x{fb40}\x{fb41}\x{fb43}\x{fb44}\x{fb46}-\x{fbb1}\x{fbd3}-\x{fd3d}\x{fd50}-\x{fd8f}\x{fd92}-\x{fdc7}\x{fdf0}-\x{fdfb}\x{fe70}-\x{fe74}\x{fe76}-\x{fefc}\x{ff21}-\x{ff3a}\x{ff41}-\x{ff5a}\x{ff66}-\x{ffbe}\x{ffc2}-\x{ffc7}\x{ffca}-\x{ffcf}\x{ffd2}-\x{ffd7}\x{ffda}-\x{ffdc}][$A-Z\_a-z\xaa\xb5\xba\xc0-\xd6\xd8-\xf6\xf8-\x{02c1}\x{02c6}-\x{02d1}\x{02e0}-\x{02e4}\x{02ec}\x{02ee}\x{0370}-\x{0374}\x{0376}\x{0377}\x{037a}-\x{037d}\x{0386}\x{0388}-\x{038a}\x{038c}\x{038e}-\x{03a1}\x{03a3}-\x{03f5}\x{03f7}-\x{0481}\x{048a}-\x{0527}\x{0531}-\x{0556}\x{0559}\x{0561}-\x{0587}\x{05d0}-\x{05ea}\x{05f0}-\x{05f2}\x{0620}-\x{064a}\x{066e}\x{066f}\x{0671}-\x{06d3}\x{06d5}\x{06e5}\x{06e6}\x{06ee}\x{06ef}\x{06fa}-\x{06fc}\x{06ff}\x{0710}\x{0712}-\x{072f}\x{074d}-\x{07a5}\x{07b1}\x{07ca}-\x{07ea}\x{07f4}\x{07f5}\x{07fa}\x{0800}-\x{0815}\x{081a}\x{0824}\x{0828}\x{0840}-\x{0858}\x{08a0}\x{08a2}-\x{08ac}\x{0904}-\x{0939}\x{093d}\x{0950}\x{0958}-\x{0961}\x{0971}-\x{0977}\x{0979}-\x{097f}\x{0985}-\x{098c}\x{098f}\x{0990}\x{0993}-\x{09a8}\x{09aa}-\x{09b0}\x{09b2}\x{09b6}-\x{09b9}\x{09bd}\x{09ce}\x{09dc}\x{09dd}\x{09df}-\x{09e1}\x{09f0}\x{09f1}\x{0a05}-\x{0a0a}\x{0a0f}\x{0a10}\x{0a13}-\x{0a28}\x{0a2a}-\x{0a30}\x{0a32}\x{0a33}\x{0a35}\x{0a36}\x{0a38}\x{0a39}\x{0a59}-\x{0a5c}\x{0a5e}\x{0a72}-\x{0a74}\x{0a85}-\x{0a8d}\x{0a8f}-\x{0a91}\x{0a93}-\x{0aa8}\x{0aaa}-\x{0ab0}\x{0ab2}\x{0ab3}\x{0ab5}-\x{0ab9}\x{0abd}\x{0ad0}\x{0ae0}\x{0ae1}\x{0b05}-\x{0b0c}\x{0b0f}\x{0b10}\x{0b13}-\x{0b28}\x{0b2a}-\x{0b30}\x{0b32}\x{0b33}\x{0b35}-\x{0b39}\x{0b3d}\x{0b5c}\x{0b5d}\x{0b5f}-\x{0b61}\x{0b71}\x{0b83}\x{0b85}-\x{0b8a}\x{0b8e}-\x{0b90}\x{0b92}-\x{0b95}\x{0b99}\x{0b9a}\x{0b9c}\x{0b9e}\x{0b9f}\x{0ba3}\x{0ba4}\x{0ba8}-\x{0baa}\x{0bae}-\x{0bb9}\x{0bd0}\x{0c05}-\x{0c0c}\x{0c0e}-\x{0c10}\x{0c12}-\x{0c28}\x{0c2a}-\x{0c33}\x{0c35}-\x{0c39}\x{0c3d}\x{0c58}\x{0c59}\x{0c60}\x{0c61}\x{0c85}-\x{0c8c}\x{0c8e}-\x{0c90}\x{0c92}-\x{0ca8}\x{0caa}-\x{0cb3}\x{0cb5}-\x{0cb9}\x{0cbd}\x{0cde}\x{0ce0}\x{0ce1}\x{0cf1}\x{0cf2}\x{0d05}-\x{0d0c}\x{0d0e}-\x{0d10}\x{0d12}-\x{0d3a}\x{0d3d}\x{0d4e}\x{0d60}\x{0d61}\x{0d7a}-\x{0d7f}\x{0d85}-\x{0d96}\x{0d9a}-\x{0db1}\x{0db3}-\x{0dbb}\x{0dbd}\x{0dc0}-\x{0dc6}\x{0e01}-\x{0e30}\x{0e32}\x{0e33}\x{0e40}-\x{0e46}\x{0e81}\x{0e82}\x{0e84}\x{0e87}\x{0e88}\x{0e8a}\x{0e8d}\x{0e94}-\x{0e97}\x{0e99}-\x{0e9f}\x{0ea1}-\x{0ea3}\x{0ea5}\x{0ea7}\x{0eaa}\x{0eab}\x{0ead}-\x{0eb0}\x{0eb2}\x{0eb3}\x{0ebd}\x{0ec0}-\x{0ec4}\x{0ec6}\x{0edc}-\x{0edf}\x{0f00}\x{0f40}-\x{0f47}\x{0f49}-\x{0f6c}\x{0f88}-\x{0f8c}\x{1000}-\x{102a}\x{103f}\x{1050}-\x{1055}\x{105a}-\x{105d}\x{1061}\x{1065}\x{1066}\x{106e}-\x{1070}\x{1075}-\x{1081}\x{108e}\x{10a0}-\x{10c5}\x{10c7}\x{10cd}\x{10d0}-\x{10fa}\x{10fc}-\x{1248}\x{124a}-\x{124d}\x{1250}-\x{1256}\x{1258}\x{125a}-\x{125d}\x{1260}-\x{1288}\x{128a}-\x{128d}\x{1290}-\x{12b0}\x{12b2}-\x{12b5}\x{12b8}-\x{12be}\x{12c0}\x{12c2}-\x{12c5}\x{12c8}-\x{12d6}\x{12d8}-\x{1310}\x{1312}-\x{1315}\x{1318}-\x{135a}\x{1380}-\x{138f}\x{13a0}-\x{13f4}\x{1401}-\x{166c}\x{166f}-\x{167f}\x{1681}-\x{169a}\x{16a0}-\x{16ea}\x{16ee}-\x{16f0}\x{1700}-\x{170c}\x{170e}-\x{1711}\x{1720}-\x{1731}\x{1740}-\x{1751}\x{1760}-\x{176c}\x{176e}-\x{1770}\x{1780}-\x{17b3}\x{17d7}\x{17dc}\x{1820}-\x{1877}\x{1880}-\x{18a8}\x{18aa}\x{18b0}-\x{18f5}\x{1900}-\x{191c}\x{1950}-\x{196d}\x{1970}-\x{1974}\x{1980}-\x{19ab}\x{19c1}-\x{19c7}\x{1a00}-\x{1a16}\x{1a20}-\x{1a54}\x{1aa7}\x{1b05}-\x{1b33}\x{1b45}-\x{1b4b}\x{1b83}-\x{1ba0}\x{1bae}\x{1baf}\x{1bba}-\x{1be5}\x{1c00}-\x{1c23}\x{1c4d}-\x{1c4f}\x{1c5a}-\x{1c7d}\x{1ce9}-\x{1cec}\x{1cee}-\x{1cf1}\x{1cf5}\x{1cf6}\x{1d00}-\x{1dbf}\x{1e00}-\x{1f15}\x{1f18}-\x{1f1d}\x{1f20}-\x{1f45}\x{1f48}-\x{1f4d}\x{1f50}-\x{1f57}\x{1f59}\x{1f5b}\x{1f5d}\x{1f5f}-\x{1f7d}\x{1f80}-\x{1fb4}\x{1fb6}-\x{1fbc}\x{1fbe}\x{1fc2}-\x{1fc4}\x{1fc6}-\x{1fcc}\x{1fd0}-\x{1fd3}\x{1fd6}-\x{1fdb}\x{1fe0}-\x{1fec}\x{1ff2}-\x{1ff4}\x{1ff6}-\x{1ffc}\x{2071}\x{207f}\x{2090}-\x{209c}\x{2102}\x{2107}\x{210a}-\x{2113}\x{2115}\x{2119}-\x{211d}\x{2124}\x{2126}\x{2128}\x{212a}-\x{212d}\x{212f}-\x{2139}\x{213c}-\x{213f}\x{2145}-\x{2149}\x{214e}\x{2160}-\x{2188}\x{2c00}-\x{2c2e}\x{2c30}-\x{2c5e}\x{2c60}-\x{2ce4}\x{2ceb}-\x{2cee}\x{2cf2}\x{2cf3}\x{2d00}-\x{2d25}\x{2d27}\x{2d2d}\x{2d30}-\x{2d67}\x{2d6f}\x{2d80}-\x{2d96}\x{2da0}-\x{2da6}\x{2da8}-\x{2dae}\x{2db0}-\x{2db6}\x{2db8}-\x{2dbe}\x{2dc0}-\x{2dc6}\x{2dc8}-\x{2dce}\x{2dd0}-\x{2dd6}\x{2dd8}-\x{2dde}\x{2e2f}\x{3005}-\x{3007}\x{3021}-\x{3029}\x{3031}-\x{3035}\x{3038}-\x{303c}\x{3041}-\x{3096}\x{309d}-\x{309f}\x{30a1}-\x{30fa}\x{30fc}-\x{30ff}\x{3105}-\x{312d}\x{3131}-\x{318e}\x{31a0}-\x{31ba}\x{31f0}-\x{31ff}\x{3400}-\x{4db5}\x{4e00}-\x{9fcc}\x{a000}-\x{a48c}\x{a4d0}-\x{a4fd}\x{a500}-\x{a60c}\x{a610}-\x{a61f}\x{a62a}\x{a62b}\x{a640}-\x{a66e}\x{a67f}-\x{a697}\x{a6a0}-\x{a6ef}\x{a717}-\x{a71f}\x{a722}-\x{a788}\x{a78b}-\x{a78e}\x{a790}-\x{a793}\x{a7a0}-\x{a7aa}\x{a7f8}-\x{a801}\x{a803}-\x{a805}\x{a807}-\x{a80a}\x{a80c}-\x{a822}\x{a840}-\x{a873}\x{a882}-\x{a8b3}\x{a8f2}-\x{a8f7}\x{a8fb}\x{a90a}-\x{a925}\x{a930}-\x{a946}\x{a960}-\x{a97c}\x{a984}-\x{a9b2}\x{a9cf}\x{aa00}-\x{aa28}\x{aa40}-\x{aa42}\x{aa44}-\x{aa4b}\x{aa60}-\x{aa76}\x{aa7a}\x{aa80}-\x{aaaf}\x{aab1}\x{aab5}\x{aab6}\x{aab9}-\x{aabd}\x{aac0}\x{aac2}\x{aadb}-\x{aadd}\x{aae0}-\x{aaea}\x{aaf2}-\x{aaf4}\x{ab01}-\x{ab06}\x{ab09}-\x{ab0e}\x{ab11}-\x{ab16}\x{ab20}-\x{ab26}\x{ab28}-\x{ab2e}\x{abc0}-\x{abe2}\x{ac00}-\x{d7a3}\x{d7b0}-\x{d7c6}\x{d7cb}-\x{d7fb}\x{f900}-\x{fa6d}\x{fa70}-\x{fad9}\x{fb00}-\x{fb06}\x{fb13}-\x{fb17}\x{fb1d}\x{fb1f}-\x{fb28}\x{fb2a}-\x{fb36}\x{fb38}-\x{fb3c}\x{fb3e}\x{fb40}\x{fb41}\x{fb43}\x{fb44}\x{fb46}-\x{fbb1}\x{fbd3}-\x{fd3d}\x{fd50}-\x{fd8f}\x{fd92}-\x{fdc7}\x{fdf0}-\x{fdfb}\x{fe70}-\x{fe74}\x{fe76}-\x{fefc}\x{ff21}-\x{ff3a}\x{ff41}-\x{ff5a}\x{ff66}-\x{ffbe}\x{ffc2}-\x{ffc7}\x{ffca}-\x{ffcf}\x{ffd2}-\x{ffd7}\x{ffda}-\x{ffdc}0-9\x{0300}-\x{036f}\x{0483}-\x{0487}\x{0591}-\x{05bd}\x{05bf}\x{05c1}\x{05c2}\x{05c4}\x{05c5}\x{05c7}\x{0610}-\x{061a}\x{064b}-\x{0669}\x{0670}\x{06d6}-\x{06dc}\x{06df}-\x{06e4}\x{06e7}\x{06e8}\x{06ea}-\x{06ed}\x{06f0}-\x{06f9}\x{0711}\x{0730}-\x{074a}\x{07a6}-\x{07b0}\x{07c0}-\x{07c9}\x{07eb}-\x{07f3}\x{0816}-\x{0819}\x{081b}-\x{0823}\x{0825}-\x{0827}\x{0829}-\x{082d}\x{0859}-\x{085b}\x{08e4}-\x{08fe}\x{0900}-\x{0903}\x{093a}-\x{093c}\x{093e}-\x{094f}\x{0951}-\x{0957}\x{0962}\x{0963}\x{0966}-\x{096f}\x{0981}-\x{0983}\x{09bc}\x{09be}-\x{09c4}\x{09c7}\x{09c8}\x{09cb}-\x{09cd}\x{09d7}\x{09e2}\x{09e3}\x{09e6}-\x{09ef}\x{0a01}-\x{0a03}\x{0a3c}\x{0a3e}-\x{0a42}\x{0a47}\x{0a48}\x{0a4b}-\x{0a4d}\x{0a51}\x{0a66}-\x{0a71}\x{0a75}\x{0a81}-\x{0a83}\x{0abc}\x{0abe}-\x{0ac5}\x{0ac7}-\x{0ac9}\x{0acb}-\x{0acd}\x{0ae2}\x{0ae3}\x{0ae6}-\x{0aef}\x{0b01}-\x{0b03}\x{0b3c}\x{0b3e}-\x{0b44}\x{0b47}\x{0b48}\x{0b4b}-\x{0b4d}\x{0b56}\x{0b57}\x{0b62}\x{0b63}\x{0b66}-\x{0b6f}\x{0b82}\x{0bbe}-\x{0bc2}\x{0bc6}-\x{0bc8}\x{0bca}-\x{0bcd}\x{0bd7}\x{0be6}-\x{0bef}\x{0c01}-\x{0c03}\x{0c3e}-\x{0c44}\x{0c46}-\x{0c48}\x{0c4a}-\x{0c4d}\x{0c55}\x{0c56}\x{0c62}\x{0c63}\x{0c66}-\x{0c6f}\x{0c82}\x{0c83}\x{0cbc}\x{0cbe}-\x{0cc4}\x{0cc6}-\x{0cc8}\x{0cca}-\x{0ccd}\x{0cd5}\x{0cd6}\x{0ce2}\x{0ce3}\x{0ce6}-\x{0cef}\x{0d02}\x{0d03}\x{0d3e}-\x{0d44}\x{0d46}-\x{0d48}\x{0d4a}-\x{0d4d}\x{0d57}\x{0d62}\x{0d63}\x{0d66}-\x{0d6f}\x{0d82}\x{0d83}\x{0dca}\x{0dcf}-\x{0dd4}\x{0dd6}\x{0dd8}-\x{0ddf}\x{0df2}\x{0df3}\x{0e31}\x{0e34}-\x{0e3a}\x{0e47}-\x{0e4e}\x{0e50}-\x{0e59}\x{0eb1}\x{0eb4}-\x{0eb9}\x{0ebb}\x{0ebc}\x{0ec8}-\x{0ecd}\x{0ed0}-\x{0ed9}\x{0f18}\x{0f19}\x{0f20}-\x{0f29}\x{0f35}\x{0f37}\x{0f39}\x{0f3e}\x{0f3f}\x{0f71}-\x{0f84}\x{0f86}\x{0f87}\x{0f8d}-\x{0f97}\x{0f99}-\x{0fbc}\x{0fc6}\x{102b}-\x{103e}\x{1040}-\x{1049}\x{1056}-\x{1059}\x{105e}-\x{1060}\x{1062}-\x{1064}\x{1067}-\x{106d}\x{1071}-\x{1074}\x{1082}-\x{108d}\x{108f}-\x{109d}\x{135d}-\x{135f}\x{1712}-\x{1714}\x{1732}-\x{1734}\x{1752}\x{1753}\x{1772}\x{1773}\x{17b4}-\x{17d3}\x{17dd}\x{17e0}-\x{17e9}\x{180b}-\x{180d}\x{1810}-\x{1819}\x{18a9}\x{1920}-\x{192b}\x{1930}-\x{193b}\x{1946}-\x{194f}\x{19b0}-\x{19c0}\x{19c8}\x{19c9}\x{19d0}-\x{19d9}\x{1a17}-\x{1a1b}\x{1a55}-\x{1a5e}\x{1a60}-\x{1a7c}\x{1a7f}-\x{1a89}\x{1a90}-\x{1a99}\x{1b00}-\x{1b04}\x{1b34}-\x{1b44}\x{1b50}-\x{1b59}\x{1b6b}-\x{1b73}\x{1b80}-\x{1b82}\x{1ba1}-\x{1bad}\x{1bb0}-\x{1bb9}\x{1be6}-\x{1bf3}\x{1c24}-\x{1c37}\x{1c40}-\x{1c49}\x{1c50}-\x{1c59}\x{1cd0}-\x{1cd2}\x{1cd4}-\x{1ce8}\x{1ced}\x{1cf2}-\x{1cf4}\x{1dc0}-\x{1de6}\x{1dfc}-\x{1dff}\x{200c}\x{200d}\x{203f}\x{2040}\x{2054}\x{20d0}-\x{20dc}\x{20e1}\x{20e5}-\x{20f0}\x{2cef}-\x{2cf1}\x{2d7f}\x{2de0}-\x{2dff}\x{302a}-\x{302f}\x{3099}\x{309a}\x{a620}-\x{a629}\x{a66f}\x{a674}-\x{a67d}\x{a69f}\x{a6f0}\x{a6f1}\x{a802}\x{a806}\x{a80b}\x{a823}-\x{a827}\x{a880}\x{a881}\x{a8b4}-\x{a8c4}\x{a8d0}-\x{a8d9}\x{a8e0}-\x{a8f1}\x{a900}-\x{a909}\x{a926}-\x{a92d}\x{a947}-\x{a953}\x{a980}-\x{a983}\x{a9b3}-\x{a9c0}\x{a9d0}-\x{a9d9}\x{aa29}-\x{aa36}\x{aa43}\x{aa4c}\x{aa4d}\x{aa50}-\x{aa59}\x{aa7b}\x{aab0}\x{aab2}-\x{aab4}\x{aab7}\x{aab8}\x{aabe}\x{aabf}\x{aac1}\x{aaeb}-\x{aaef}\x{aaf5}\x{aaf6}\x{abe3}-\x{abea}\x{abec}\x{abed}\x{abf0}-\x{abf9}\x{fb1e}\x{fe00}-\x{fe0f}\x{fe20}-\x{fe26}\x{fe33}\x{fe34}\x{fe4d}-\x{fe4f}\x{ff10}-\x{ff19}\x{ff3f}]*\b'; + + /** + * Full list of JavaScript reserved words. + * Will be loaded from /data/js/keywords_reserved.txt. + * + * @see https://mathiasbynens.be/notes/reserved-keywords + * + * @var string[] + */ + protected $keywordsReserved = array(); + + /** + * List of JavaScript reserved words that accept a + * after them. Some end of lines are not the end of a statement, like with + * these keywords. + * + * E.g.: we shouldn't insert a ; after this else + * else + * console.log('this is quite fine') + * + * Will be loaded from /data/js/keywords_before.txt + * + * @var string[] + */ + protected $keywordsBefore = array(); + + /** + * List of JavaScript reserved words that accept a + * before them. Some end of lines are not the end of a statement, like when + * continued by one of these keywords on the newline. + * + * E.g.: we shouldn't insert a ; before this instanceof + * variable + * instanceof String + * + * Will be loaded from /data/js/keywords_after.txt + * + * @var string[] + */ + protected $keywordsAfter = array(); + + /** + * List of JavaScript operators that accept a after + * them. Some end of lines are not the end of a statement, like with these + * operators. + * + * Note: Most operators are fine, we've only removed !, ++ and --. + * There can't be a newline separating ! and whatever it is negating. + * ++ & -- have to be joined with the value they're in-/decrementing. + * + * Will be loaded from /data/js/operators_before.txt + * + * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators + * + * @var string[] + */ + protected $operatorsBefore = array(); + + /** + * List of JavaScript operators that accept a before + * them. Some end of lines are not the end of a statement, like when + * continued by one of these operators on the newline. + * + * Note: Most operators are fine, we've only removed ), ], ++ and --. + * ++ & -- have to be joined with the value they're in-/decrementing. + * ) & ] are "special" in that they have lots or usecases. () for example + * is used for function calls, for grouping, in if () and for (), ... + * + * Will be loaded from /data/js/operators_after.txt + * + * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators + * + * @var string[] + */ + protected $operatorsAfter = array(); + + /** + * {@inheritdoc} + */ + public function __construct() + { + call_user_func_array(array('parent', '__construct'), func_get_args()); + + $dataDir = __DIR__.'/../data/js/'; + $options = FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES; + $this->keywordsReserved = file($dataDir.'keywords_reserved.txt', $options); + $this->keywordsBefore = file($dataDir.'keywords_before.txt', $options); + $this->keywordsAfter = file($dataDir.'keywords_after.txt', $options); + $this->operatorsBefore = file($dataDir.'operators_before.txt', $options); + $this->operatorsAfter = file($dataDir.'operators_after.txt', $options); + } + + /** + * Minify the data. + * Perform JS optimizations. + * + * @param string[optional] $path Path to write the data to. + * + * @return string The minified data. + */ + public function execute($path = null) + { + $content = ''; + + // loop files + foreach ($this->data as $source => $js) { + /* + * Combine js: separating the scripts by a ; + * I'm also adding a newline: it will be eaten when whitespace is + * stripped, but we need to make sure we're not just appending + * a new script right after a previous script that ended with a + * singe-line comment on the last line (in which case it would also + * be seen as part of that comment) + */ + $content .= $js."\n;"; + } + + /* + * Let's first take out strings, comments and regular expressions. + * All of these can contain JS code-like characters, and we should make + * sure any further magic ignores anything inside of these. + * + * Consider this example, where we should not strip any whitespace: + * var str = "a test"; + * + * Comments will be removed altogether, strings and regular expressions + * will be replaced by placeholder text, which we'll restore later. + */ + $this->extractStrings('\'"`'); + $this->stripComments(); + $this->extractRegex(); + $content = $this->replace($content); + + $content = $this->stripWhitespace($content); + $content = $this->propertyNotation($content); + $content = $this->shortenBools($content); + + /* + * Earlier, we extracted strings & regular expressions and replaced them + * with placeholder text. This will restore them. + */ + $content = $this->restoreExtractedData($content); + + return $content; + } + + /** + * Strip comments from source code. + */ + protected function stripComments() + { + // single-line comments + $this->registerPattern('/\/\/.*$/m', ''); + + // multi-line comments + $this->registerPattern('/\/\*.*?\*\//s', ''); + } + + /** + * JS can have /-delimited regular expressions, like: /ab+c/.match(string). + * + * The content inside the regex can contain characters that may be confused + * for JS code: e.g. it could contain whitespace it needs to match & we + * don't want to strip whitespace in there. + * + * The regex can be pretty simple: we don't have to care about comments, + * (which also use slashes) because stripComments() will have stripped those + * already. + * + * This method will replace all string content with simple REGEX# + * placeholder text, so we've rid all regular expressions from characters + * that may be misinterpreted. Original regex content will be saved in + * $this->extracted and after doing all other minifying, we can restore the + * original content via restoreRegex() + */ + protected function extractRegex() + { + // PHP only supports $this inside anonymous functions since 5.4 + $minifier = $this; + $callback = function ($match) use ($minifier) { + $count = count($minifier->extracted); + $placeholder = '/'.$count.'/'; + $minifier->extracted[$placeholder] = $match[1]; + + return $placeholder; + }; + + // it's a regex if we can find an opening and (not escaped) closing /, + // include \n because it may be there for a reason + // (https://github.com/matthiasmullie/minify/issues/56) + $pattern = '(\/.*?(?registerPattern('/^\s*\K'.$pattern.'/', $callback); + // * following another operator, it's not division, but regex + $operators = $this->getOperatorsForRegex($this->operatorsBefore, '/'); + $operators += $this->getKeywordsForRegex($this->keywordsReserved, '/'); + $this->registerPattern('/(?:'.implode('|', $operators).')\s*\K'.$pattern.'/', $callback); + } + + /** + * Strip whitespace. + * + * We won't strip *all* whitespace, but as much as possible. The thing that + * we'll preserve are newlines we're unsure about. + * JavaScript doesn't require statements to be terminated with a semicolon. + * It will automatically fix missing semicolons with ASI (automatic semi- + * colon insertion) at the end of line causing errors (without semicolon.) + * + * Because it's sometimes hard to tell if a newline is part of a statement + * that should be terminated or not, we'll just leave some of them alone. + * + * @param string $content The content to strip the whitespace for. + * + * @return string + */ + protected function stripWhitespace($content) + { + // uniform line endings, make them all line feed + $content = str_replace(array("\r\n", "\r"), "\n", $content); + + // collapse all non-line feed whitespace into a single space + $content = preg_replace('/[^\S\n]+/', ' ', $content); + + // strip leading & trailing whitespace + $content = str_replace(array(" \n", "\n "), "\n", $content); + + // collapse consecutive line feeds into just 1 + $content = preg_replace('/\n+/', "\n", $content); + + $before = $this->getOperatorsForRegex($this->operatorsBefore, '/'); + $after = $this->getOperatorsForRegex($this->operatorsAfter, '/'); + $operators = $before + $after; + + // strip whitespace that ends in (or next line begin with) an operator + // that allows statements to be broken up over multiple lines + unset($before['+'], $before['-'], $after['+'], $after['-']); + $content = preg_replace('/('.implode('|', $before).')\s+/', '\\1', $content); + $content = preg_replace('/\s+('.implode('|', $after).')/', '\\1', $content); + + // make sure + and - can't be mistaken for, or joined into ++ and -- + $content = preg_replace('/(?getKeywordsForRegex($this->keywordsBefore, '/'); + $after = $this->getKeywordsForRegex($this->keywordsAfter, '/'); + $content = preg_replace('/(^|[;\}\s])\K('.implode('|', $before).')\s+/', '\\2 ', $content); + $content = preg_replace('/\s+('.implode('|', $after).')(?=([;\{\s]|$))/', ' \\1', $content); + + /* + * Get rid of double semicolons, except where they can be used like: + * "for(v=1,_=b;;)", "for(v=1;;v++)" or "for(;;ja||(ja=true))". + * I'll safeguard these double semicolons inside for-loops by + * temporarily replacing them with an invalid condition: they won't have + * a double semicolon and will be easy to spot to restore afterwards. + */ + $content = preg_replace('/\bfor\(([^;]*);;([^;]*)\)/', 'for(\\1;-;\\2)', $content); + $content = preg_replace('/;+/', ';', $content); + $content = preg_replace('/\bfor\(([^;]*);-;([^;]*)\)/', 'for(\\1;;\\2)', $content); + + /* + * Next, we'll be removing all semicolons where ASI kicks in. + * for-loops however, can have an empty body (ending in only a + * semicolon), like: `for(i=1;i<3;i++);` + * Here, nothing happens during the loop; it's just used to keep + * increasing `i`. With that ; omitted, the next line would be expected + * to be the for-loop's body... + * I'm going to double that semicolon (if any) so after the next line, + * which strips semicolons here & there, we're still left with this one. + */ + $content = preg_replace('/(for\([^;]*;[^;]*;[^;\{]*\));(\}|$)/s', '\\1;;\\2', $content); + + /* + * We also can't strip empty else-statements. Even though they're + * useless and probably shouldn't be in the code in the first place, we + * shouldn't be stripping the `;` that follows it as it breaks the code. + * We can just remove those useless else-statements completely. + * + * @see https://github.com/matthiasmullie/minify/issues/91 + */ + $content = preg_replace('/else;/s', '', $content); + + /* + * We also don't really want to terminate statements followed by closing + * curly braces (which we've ignored completely up until now) or end-of- + * script: ASI will kick in here & we're all about minifying. + * Semicolons at beginning of the file don't make any sense either. + */ + $content = preg_replace('/;(\}|$)/s', '\\1', $content); + $content = ltrim($content, ';'); + + // get rid of remaining whitespace af beginning/end + return trim($content); + } + + /** + * We'll strip whitespace around certain operators with regular expressions. + * This will prepare the given array by escaping all characters. + * + * @param string[] $operators + * @param string $delimiter + * + * @return string[] + */ + protected function getOperatorsForRegex(array $operators, $delimiter = '/') + { + // escape operators for use in regex + $delimiter = array_fill(0, count($operators), $delimiter); + $escaped = array_map('preg_quote', $operators, $delimiter); + + $operators = array_combine($operators, $escaped); + + // ignore + & - for now, they'll get special treatment + unset($operators['+'], $operators['-']); + + // dot can not just immediately follow a number; it can be confused for + // decimal point, or calling a method on it, e.g. 42 .toString() + $operators['.'] = '(?%&|'); + $operators['='] = '(?keywordsReserved; + $callback = function ($match) use ($minifier, $keywords) { + $property = trim($minifier->extracted[$match[1]], '\'"'); + + /* + * Check if the property is a reserved keyword. In this context (as + * property of an object literal/array) it shouldn't matter, but IE8 + * freaks out with "Expected identifier". + */ + if (in_array($property, $keywords)) { + return $match[0]; + } + + /* + * See if the property is in a variable-like format (e.g. + * array['key-here'] can't be replaced by array.key-here since '-' + * is not a valid character there. + */ + if (!preg_match('/^'.$minifier::REGEX_VARIABLE.'$/u', $property)) { + return $match[0]; + } + + return '.'.$property; + }; + + /* + * Figure out if previous character is a variable name (of the array + * we want to use property notation on) - this is to make sure + * standalone ['value'] arrays aren't confused for keys-of-an-array. + * We can (and only have to) check the last character, because PHP's + * regex implementation doesn't allow un-fixed-length lookbehind + * assertions. + */ + preg_match('/(\[[^\]]+\])[^\]]*$/', static::REGEX_VARIABLE, $previousChar); + $previousChar = $previousChar[1]; + + /* + * Make sure word preceding the ['value'] is not a keyword, e.g. + * return['x']. Because -again- PHP's regex implementation doesn't allow + * un-fixed-length lookbehind assertions, I'm just going to do a lot of + * separate lookbehind assertions, one for each keyword. + */ + $keywords = $this->getKeywordsForRegex($keywords); + $keywords = '(? + * @copyright Copyright (c) 2012, Matthias Mullie. All rights reserved. + * @license MIT License + */ +abstract class Minify +{ + /** + * The data to be minified. + * + * @var string[] + */ + protected $data = array(); + + /** + * Array of patterns to match. + * + * @var string[] + */ + protected $patterns = array(); + + /** + * This array will hold content of strings and regular expressions that have + * been extracted from the JS source code, so we can reliably match "code", + * without having to worry about potential "code-like" characters inside. + * + * @var string[] + */ + public $extracted = array(); + + /** + * Init the minify class - optionally, code may be passed along already. + */ + public function __construct(/* $data = null, ... */) + { + // it's possible to add the source through the constructor as well ;) + if (func_num_args()) { + call_user_func_array(array($this, 'add'), func_get_args()); + } + } + + /** + * Add a file or straight-up code to be minified. + * + * @param string $data + */ + public function add($data /* $data = null, ... */) + { + // bogus "usage" of parameter $data: scrutinizer warns this variable is + // not used (we're using func_get_args instead to support overloading), + // but it still needs to be defined because it makes no sense to have + // this function without argument :) + $args = array($data) + func_get_args(); + + // this method can be overloaded + foreach ($args as $data) { + // redefine var + $data = (string) $data; + + // load data + $value = $this->load($data); + $key = ($data != $value) ? $data : count($this->data); + + // store data + $this->data[$key] = $value; + } + } + + /** + * Load data. + * + * @param string $data Either a path to a file or the content itself. + * + * @return string + */ + protected function load($data) + { + // check if the data is a file + if (strlen($data) < PHP_MAXPATHLEN && file_exists($data) && is_file($data)) { + $data = file_get_contents($data); + + // strip BOM, if any + if (substr($data, 0, 3) == "\xef\xbb\xbf") { + $data = substr($data, 3); + } + } + + return $data; + } + + /** + * Save to file. + * + * @param string $content The minified data. + * @param string $path The path to save the minified data to. + * + * @throws Exception + */ + protected function save($content, $path) + { + // create file & open for writing + if (($handler = @fopen($path, 'w')) === false) { + throw new Exception('The file "'.$path.'" could not be opened. Check if PHP has enough permissions.'); + } + + // write to file + if (@fwrite($handler, $content) === false) { + throw new Exception('The file "'.$path.'" could not be written to. Check if PHP has enough permissions.'); + } + + // close the file + @fclose($handler); + } + + /** + * Minify the data & (optionally) saves it to a file. + * + * @param string[optional] $path Path to write the data to. + * + * @return string The minified data. + */ + public function minify($path = null) + { + $content = $this->execute($path); + + // save to path + if ($path !== null) { + $this->save($content, $path); + } + + return $content; + } + + /** + * Minify & gzip the data & (optionally) saves it to a file. + * + * @param string[optional] $path Path to write the data to. + * @param int[optional] $level Compression level, from 0 to 9. + * + * @return string The minified & gzipped data. + */ + public function gzip($path = null, $level = 9) + { + $content = $this->execute($path); + $content = gzencode($content, $level, FORCE_GZIP); + + // save to path + if ($path !== null) { + $this->save($content, $path); + } + + 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 public function execute($path = null); + + /** + * Register a pattern to execute against the source content. + * + * @param string $pattern PCRE pattern. + * @param string|callable $replacement Replacement value for matched pattern. + * + * @throws Exception + */ + protected function registerPattern($pattern, $replacement = '') + { + // study the pattern, we'll execute it more than once + $pattern .= 'S'; + + $this->patterns[] = array($pattern, $replacement); + } + + /** + * We can't "just" run some regular expressions against JavaScript: it's a + * complex language. E.g. having an occurrence of // xyz would be a comment, + * unless it's used within a string. Of you could have something that looks + * like a 'string', but inside a comment. + * The only way to accurately replace these pieces is to traverse the JS one + * character at a time and try to find whatever starts first. + * + * @param string $content The content to replace patterns in. + * + * @return string The (manipulated) content. + */ + protected function replace($content) + { + $processed = ''; + $positions = array_fill(0, count($this->patterns), -1); + $matches = array(); + + while ($content) { + // find first match for all patterns + foreach ($this->patterns as $i => $pattern) { + list($pattern, $replacement) = $pattern; + + // no need to re-run matches that are still in the part of the + // content that hasn't been processed + if ($positions[$i] >= 0) { + continue; + } + + $match = null; + if (preg_match($pattern, $content, $match)) { + $matches[$i] = $match; + + // we'll store the match position as well; that way, we + // don't have to redo all preg_matches after changing only + // the first (we'll still know where those others are) + $positions[$i] = strpos($content, $match[0]); + } else { + // if the pattern couldn't be matched, there's no point in + // executing it again in later runs on this same content; + // ignore this one until we reach end of content + unset($matches[$i]); + $positions[$i] = strlen($content); + } + } + + // no more matches to find: everything's been processed, break out + if (!$matches) { + $processed .= $content; + break; + } + + // see which of the patterns actually found the first thing (we'll + // only want to execute that one, since we're unsure if what the + // other found was not inside what the first found) + $discardLength = min($positions); + $firstPattern = array_search($discardLength, $positions); + $match = $matches[$firstPattern][0]; + + // execute the pattern that matches earliest in the content string + list($pattern, $replacement) = $this->patterns[$firstPattern]; + $replacement = $this->replacePattern($pattern, $replacement, $content); + + // figure out which part of the string was unmatched; that's the + // part we'll execute the patterns on again next + $content = substr($content, $discardLength); + $unmatched = (string) substr($content, strpos($content, $match) + strlen($match)); + + // move the replaced part to $processed and prepare $content to + // again match batch of patterns against + $processed .= substr($replacement, 0, strlen($replacement) - strlen($unmatched)); + $content = $unmatched; + + // first match has been replaced & that content is to be left alone, + // the next matches will start after this replacement, so we should + // fix their offsets + foreach ($positions as $i => $position) { + $positions[$i] -= $discardLength + strlen($match); + } + } + + return $processed; + } + + /** + * This is where a pattern is matched against $content and the matches + * are replaced by their respective value. + * This function will be called plenty of times, where $content will always + * move up 1 character. + * + * @param string $pattern Pattern to match. + * @param string|callable $replacement Replacement value. + * @param string $content Content to match pattern against. + * + * @return string + */ + protected function replacePattern($pattern, $replacement, $content) + { + if (is_callable($replacement)) { + return preg_replace_callback($pattern, $replacement, $content, 1, $count); + } else { + return preg_replace($pattern, $replacement, $content, 1, $count); + } + } + + /** + * Strings are a pattern we need to match, in order to ignore potential + * code-like content inside them, but we just want all of the string + * content to remain untouched. + * + * This method will replace all string content with simple STRING# + * placeholder text, so we've rid all strings from characters that may be + * misinterpreted. Original string content will be saved in $this->extracted + * and after doing all other minifying, we can restore the original content + * via restoreStrings() + * + * @param string[optional] $chars + */ + protected function extractStrings($chars = '\'"') + { + // PHP only supports $this inside anonymous functions since 5.4 + $minifier = $this; + $callback = function ($match) use ($minifier) { + if (!$match[1]) { + /* + * Empty strings need no placeholder; they can't be confused for + * anything else anyway. + * But we still needed to match them, for the extraction routine + * to skip over this particular string. + */ + return $match[0]; + } + + $count = count($minifier->extracted); + $placeholder = $match[1].$count.$match[1]; + $minifier->extracted[$placeholder] = $match[1].$match[2].$match[1]; + + return $placeholder; + }; + + /* + * The \\ messiness explained: + * * Don't count ' or " as end-of-string if it's escaped (has backslash + * in front of it) + * * Unless... that backslash itself is escaped (another leading slash), + * in which case it's no longer escaping the ' or " + * * So there can be either no backslash, or an even number + * * multiply all of that times 4, to account for the escaping that has + * to be done to pass the backslash into the PHP string without it being + * considered as escape-char (times 2) and to get it in the regex, + * escaped (times 2) + */ + $this->registerPattern('/(['.$chars.'])(.*?(?extracted. + * + * @param string $content + * + * @return string + */ + protected function restoreExtractedData($content) + { + if (!$this->extracted) { + // nothing was extracted, nothing to restore + return $content; + } + + $content = strtr($content, $this->extracted); + + $this->extracted = array(); + + return $content; + } +} diff --git a/lib/tests/bootstrap.php b/lib/tests/bootstrap.php new file mode 100644 index 0000000..991ea43 --- /dev/null +++ b/lib/tests/bootstrap.php @@ -0,0 +1,3 @@ +minifier = $this->getMockBuilder('\MatthiasMullie\Minify\CSS') + ->setMethods(array('save')) + ->getMock(); + } + + /** + * Cleans up the environment after running a test. + */ + protected function tearDown() + { + $this->minifier = null; + parent::tearDown(); + } + + /** + * Test CSS minifier rules, provided by dataProvider. + * + * @test + * @dataProvider dataProvider + */ + public function minify($input, $expected) + { + $input = (array) $input; + foreach ($input as $css) { + $this->minifier->add($css); + } + $result = $this->minifier->minify(); + + $this->assertEquals($expected, $result); + } + + /** + * Test conversion of relative paths, provided by dataProviderPaths. + * + * @test + * @dataProvider dataProviderPaths + */ + public function convertRelativePath($source, $target, $expected) + { + $source = (array) $source; + foreach ($source as $path => $css) { + $this->minifier->add($css); + + // $source also accepts an array where the key is a bogus path + if (is_string($path)) { + $object = new ReflectionObject($this->minifier); + $property = $object->getProperty('data'); + $property->setAccessible(true); + $data = $property->getValue($this->minifier); + + // keep content, but make it appear from the given path + $data[$path] = array_pop($data); + $property->setValue($this->minifier, $data); + $property->setAccessible(false); + } + } + + $result = $this->minifier->minify($target); + + $this->assertEquals($expected, $result); + } + + /** + * Test minifier import configuration methods. + * + * @test + */ + public function setConfig() + { + $this->minifier->setMaxImportSize(10); + $this->minifier->setImportExtensions(array('gif' => 'data:image/gif')); + + $object = new ReflectionObject($this->minifier); + + $property = $object->getProperty('maxImportSize'); + $property->setAccessible(true); + $this->assertEquals($property->getValue($this->minifier), 10); + + $property = $object->getProperty('importExtensions'); + $property->setAccessible(true); + $this->assertEquals($property->getValue($this->minifier), array('gif' => 'data:image/gif')); + } + + /** + * @return array [input, expected result] + */ + public function dataProvider() + { + $tests = array(); + + // try importing, with both @import syntax types & media queries + $tests[] = array( + __DIR__.'/sample/combine_imports/index.css', + 'body{color:red}', + ); + $tests[] = array( + __DIR__.'/sample/combine_imports/index2.css', + 'body{color:red}', + ); + $tests[] = array( + __DIR__.'/sample/combine_imports/index3.css', + 'body{color:red}body{color:red}', + ); + $tests[] = array( + __DIR__.'/sample/combine_imports/index4.css', + '@media only screen{body{color:red}}@media only screen{body{color:red}}', + ); + $tests[] = array( + __DIR__.'/sample/combine_imports/index5.css', + 'body{color:red}body{color:red}', + ); + $tests[] = array( + __DIR__.'/sample/combine_imports/index6a.css', + 'body{color:red}', + ); + + // shorthand hex color codes + $tests[] = array( + 'color:#FF00FF;', + 'color:#F0F;', + ); + + // import files + $tests[] = array( + __DIR__.'/sample/import_files/index.css', + 'body{background:url(data:image/png;base64,'.base64_encode(file_get_contents(__DIR__.'/sample/import_files/file.png')).')}', + ); + + // strip comments + $tests[] = array( + '/* This is a CSS comment */', + '', + ); + + // strip whitespace + $tests[] = array( + 'body { color: red; }', + 'body{color:red}', + ); + + // whitespace inside strings shouldn't be replaced + $tests[] = array( + 'content:"preserve whitespace"', + 'content:"preserve whitespace"', + ); + + $tests[] = array( + 'html + body { + color: red; + }', + 'html body{color:red}', + ); + + $tests[] = array( +<<<'JS' +p * i , html +/* remove spaces */ + +/* " comments have no escapes \*/ +body/* keep */ /* space */p, +p [ remove ~= " spaces " ] :nth-child( 3 + 2n ) > b span i , div::after + +{ + /* comment */ + content : " escapes \" allowed \\" ; + content: " /* string */ " !important ; + width: calc( 100% - 3em + 5px ) ; + margin-top : 0; + margin-bottom : 0; + margin-left : 10px; + margin-right : 10px; +} +JS + , + 'p * i,html body p,p [remove~=" spaces "] :nth-child(3+2n)>b span i,div::after{content:" escapes \\" allowed \\\\";content:" /* string */ "!important;width:calc(100% - 3em + 5px);margin-top:0;margin-bottom:0;margin-left:10px;margin-right:10px}', + ); + + /* + * https://github.com/forkcms/forkcms/issues/387 + * + * CSS backslash. + * * Backslash escaped by backslash in CSS + * * Double CSS backslashed escaped twice for in PHP string + */ + $tests[] = array( + '.iconic.map-pin:before { content: "\\\\"; }', + '.iconic.map-pin:before{content:"\\\\"}', + ); + + // strip BOM + $tests[] = array( + __DIR__.'/sample/bom/bom.css', + 'body{color:red}', + ); + + // https://github.com/matthiasmullie/minify/issues/22 + $tests[] = array( + 'p { background-position: -0px -64px; }', + 'p{background-position:0 -64px}', + ); + + // https://github.com/matthiasmullie/minify/issues/23 + $tests[] = array( + 'ul.pagination { +display: block; +min-height: 1.5rem; +margin-left: -0.3125rem; +}', + 'ul.pagination{display:block;min-height:1.5rem;margin-left:-.3125rem}', + ); + + // edge cases for stripping zeroes + $tests[] = array( + 'p { margin: -0.0rem; }', + 'p{margin:0rem}', + ); + $tests[] = array( + 'p { margin: -0.01rem; }', + 'p{margin:-.01rem}', + ); + $tests[] = array( + 'p { margin: .0; }', + 'p{margin:0}', + ); + $tests[] = array( + 'p { margin: .0%; }', + 'p{margin:0%}', + ); + $tests[] = array( + 'p { margin: 1.0; }', + 'p{margin:1}', + ); + $tests[] = array( + 'p { margin: 1.0px; }', + 'p{margin:1px}', + ); + $tests[] = array( + 'p { margin: 1.1; }', + 'p{margin:1.1}', + ); + $tests[] = array( + 'p { margin: 1.1em; }', + 'p{margin:1.1em}', + ); + $tests[] = array( + 'p { margin: 00px; }', + 'p{margin:0}', + ); + $tests[] = array( + 'p { margin: 0.1px; }', + 'p{margin:.1px}', + ); + $tests[] = array( + 'p { margin: 01.1px; }', + 'p{margin:1.1px}', + ); + $tests[] = array( + 'p { margin: 0.060px; }', + 'p{margin:.06px}', + ); + $tests[] = array( + 'p.class00 { background-color: #000000; color: #000; }', + 'p.class00{background-color:#000;color:#000}', + ); + + // https://github.com/matthiasmullie/minify/issues/24 + $tests[] = array( + '.col-1-1 { width: 100.00%; }', + '.col-1-1{width:100%}', + ); + + // https://github.com/matthiasmullie/minify/issues/25 + $tests[] = array( + 'p { background-color: #000000; color: #000; }', + 'p{background-color:#000;color:#000}', + ); + + // https://github.com/matthiasmullie/minify/issues/26 + $tests[] = array( + '.hr > :first-child { width: 0.0001%; }', + '.hr>:first-child{width:.0001%}', + ); + + // https://github.com/matthiasmullie/minify/issues/28 + $tests[] = array( + '@font-face { src: url(//netdna.bootstrapcdn.com/font-awesome/4.2.0/fonts/fontawesome-webfont.eot?v=4.2.0); }', + '@font-face{src:url(//netdna.bootstrapcdn.com/font-awesome/4.2.0/fonts/fontawesome-webfont.eot?v=4.2.0)}', + ); + + // https://github.com/matthiasmullie/minify/issues/31 + $tests[] = array( + 'dfn,em,img{color:red}', + 'dfn,em,img{color:red}', + ); + + // https://github.com/matthiasmullie/minify/issues/49 + $tests[] = array( + __DIR__.'/sample/import_files/issue49.css', + '.social-btn a[href*="facebook"]{background-image:url(data:image/png;base64,'.base64_encode(file_get_contents(__DIR__.'/sample/import_files/facebook.png')).')}'. + '.social-btn a[href*="vimeo"]{background-image:url(data:image/png;base64,'.base64_encode(file_get_contents(__DIR__.'/sample/import_files/vimeo.png')).')}'. + '.social-btn a[href*="instagram"]{background-image:url(data:image/png;base64,'.base64_encode(file_get_contents(__DIR__.'/sample/import_files/instagram.png')).')}', + ); + + // https://github.com/matthiasmullie/minify/issues/68 + $tests[] = array( + __DIR__.'/sample/external_imports/issue68.css', + '@import url(http://localhost/file.css);body{background:green}', + ); + + // https://github.com/matthiasmullie/minify/issues/67 + $tests[] = array( + 'body { } +p { color: #fff; }', + 'p{color:#fff}', + ); + $tests[] = array( + 'body {} +p { color: #fff; } +h1 { } +strong { color: red; }', + 'p{color:#fff}strong{color:red}', + ); + + // https://github.com/matthiasmullie/minify/issues/74 + $tests[] = array( + "@media only screen and (-webkit-min-device-pixel-ratio: 1.5), +only screen and (min--moz-device-pixel-ratio: 1.5), +only screen and (min-device-pixel-ratio: 1.5) { + + #fancybox-loading,.fancybox-close,.fancybox-prev span,.fancybox-next span { + background-image: url('/path/to/image.png'); + background-size: 44px 152px; + } + + #fancybox-loading div { + background-image: url('/path/to/image.gif'); + background-size: 24px 24px; + } +}", + '@media only screen and (-webkit-min-device-pixel-ratio:1.5),only screen and (min--moz-device-pixel-ratio:1.5),only screen and (min-device-pixel-ratio:1.5){#fancybox-loading,.fancybox-close,.fancybox-prev span,.fancybox-next span{background-image:url(/path/to/image.png);background-size:44px 152px}#fancybox-loading div{background-image:url(/path/to/image.gif);background-size:24px 24px}}', + ); + + // https://github.com/matthiasmullie/minify/issues/92 + $tests[] = array( + '@media (min-width:320px) { + /* smartphones, iPhone, portrait 480x320 phones */ + p { + background-color: red; + } +} +@media (min-width:1025px) { + /* big landscape tablets, laptops, and desktops */ + /* LEFT EMPTY OF ANY SELECTORS */ +} +@media (min-width:1281px) { + /* hi-res laptops and desktops */ + p { + background-color: blue; + } +}', + '@media (min-width:320px){p{background-color:red}}@media (min-width:1281px){p{background-color:blue}}', + ); + + return $tests; + } + + /** + * @return array [input, expected result] + */ + public function dataProviderPaths() + { + $tests = array(); + + $source = __DIR__.'/sample/convert_relative_path/source'; + $target = __DIR__.'/sample/convert_relative_path/target'; + + // external link + $tests[] = array( + $source.'/external.css', + $target.'/external.css', + file_get_contents($source.'/external.css'), + ); + + // absolute path + $tests[] = array( + $source.'/absolute.css', + $target.'/absolute.css', + file_get_contents($source.'/absolute.css'), + ); + + // relative paths + $tests[] = array( + $source.'/relative.css', + $target.'/relative.css', + '@import url(stylesheet.css);', + ); + $tests[] = array( + $source.'/../source/relative.css', + $target.'/target/relative.css', + '@import url(../stylesheet.css);', + ); + + // https://github.com/matthiasmullie/minify/issues/29 + $tests[] = array( + $source.'/issue29.css', + $target.'/issue29.css', + "@import url('http://myurl.de');", + ); + + // https://github.com/matthiasmullie/minify/issues/38 + $tests[] = array( + $source.'/relative.css', + null, // no output file + file_get_contents($source.'/relative.css'), + ); + + // https://github.com/matthiasmullie/minify/issues/39 + $tests[] = array( + $source.'/issue39.css', + null, // no output file + // relative paths should remain untouched + "@font-face{font-family:'blackcat';src:url(../webfont/blackcat.eot);src:url(../webfont/blackcat.eot?#iefix) format('embedded-opentype'),url(../webfont/blackcat.svg#blackcat) format('svg'),url(../webfont/blackcat.woff) format('woff'),url(../webfont/blackcat.ttf) format('truetype');font-weight:normal;font-style:normal}", + ); + $tests[] = array( + $source.'/issue39.css', + $target.'/issue39.css', + // relative paths should remain untouched + "@font-face{font-family:'blackcat';src:url(../webfont/blackcat.eot);src:url(../webfont/blackcat.eot?#iefix) format('embedded-opentype'),url(../webfont/blackcat.svg#blackcat) format('svg'),url(../webfont/blackcat.woff) format('woff'),url(../webfont/blackcat.ttf) format('truetype');font-weight:normal;font-style:normal}", + ); + $tests[] = array( + $source.'/issue39.css', + $target.'/target/issue39.css', + // relative paths should have changed + "@font-face{font-family:'blackcat';src:url(../../webfont/blackcat.eot);src:url(../../webfont/blackcat.eot?#iefix) format('embedded-opentype'),url(../../webfont/blackcat.svg#blackcat) format('svg'),url(../../webfont/blackcat.woff) format('woff'),url(../../webfont/blackcat.ttf) format('truetype');font-weight:normal;font-style:normal}", + ); + + // https://github.com/forkcms/forkcms/issues/1121 + $tests[] = array( + $source.'/nested/nested.css', + $target.'/nested.css', + '@import url(stylesheet.css);', + ); + + // https://github.com/forkcms/forkcms/issues/1186 + $tests[] = array( + array( + // key is a bogus path + '/Users/mathias/Documents/— Projecten/PROJECT_NAAM/Web/src/Backend/Core/Layout/Css/screen.css' => '@import url("imports/typography.css");', + ), + '/Users/mathias/Documents/— Projecten/PROJECT_NAAM/Web/src/Backend/Cache/MinifiedCss/some-hash.css', + '@import url(../../Core/Layout/Css/imports/typography.css);', + ); + + $sourceRelative = 'tests/css/sample/convert_relative_path/source'; + $targetRelative = 'tests/css/sample/convert_relative_path/target'; + + // from and/or to are relative links + $tests[] = array( + $sourceRelative.'/relative.css', + $target.'/relative.css', + '@import url(stylesheet.css);', + ); + // note: relative target only works if the file already exists: it has + // to be able to realpath() + $tests[] = array( + $source.'/relative.css', + $targetRelative.'/relative.css', + '@import url(stylesheet.css);', + ); + $tests[] = array( + $sourceRelative.'/relative.css', + $targetRelative.'/relative.css', + '@import url(stylesheet.css);', + ); + + $source = __DIR__.'/sample/symlink'; + $target = __DIR__.'/sample/symlink/target'; + $sourceRelative = 'tests/css/sample/symlink'; + $targetRelative = 'tests/css/sample/symlink/target'; + + // import symlinked files: relative, absolute & mix + $tests[] = array( + $source.'/import_symlinked_file.css', + $target.'/import_symlinked_file.css', + '', + ); + $tests[] = array( + $sourceRelative.'/import_symlinked_file.css', + $targetRelative.'/import_symlinked_file.css', + '', + ); + $tests[] = array( + $source.'/import_symlinked_file.css', + $targetRelative.'/import_symlinked_file.css', + '', + ); + $tests[] = array( + $sourceRelative.'/import_symlinked_file.css', + $target.'/import_symlinked_file.css', + '', + ); + + // move symlinked files: relative, absolute & mix + $tests[] = array( + $source.'/move_symlinked_file.css', + $target.'/move_symlinked_file.css', + 'body{background-url:url(../assets/symlink.bmp)}', + ); + $tests[] = array( + $sourceRelative.'/move_symlinked_file.css', + $targetRelative.'/move_symlinked_file.css', + 'body{background-url:url(../assets/symlink.bmp)}', + ); + $tests[] = array( + $source.'/move_symlinked_file.css', + $targetRelative.'/move_symlinked_file.css', + 'body{background-url:url(../assets/symlink.bmp)}', + ); + $tests[] = array( + $source.'/move_symlinked_file.css', + $targetRelative.'/move_symlinked_file.css', + 'body{background-url:url(../assets/symlink.bmp)}', + ); + + // import symlinked folders: relative, absolute & mix + $tests[] = array( + $source.'/import_symlinked_folder.css', + $target.'/import_symlinked_folder.css', + '', + ); + $tests[] = array( + $sourceRelative.'/import_symlinked_folder.css', + $targetRelative.'/import_symlinked_folder.css', + '', + ); + $tests[] = array( + $source.'/import_symlinked_folder.css', + $targetRelative.'/import_symlinked_folder.css', + '', + ); + $tests[] = array( + $sourceRelative.'/import_symlinked_folder.css', + $target.'/import_symlinked_folder.css', + '', + ); + + // move symlinked folders: relative, absolute & mix + $tests[] = array( + $source.'/move_symlinked_folder.css', + $target.'/move_symlinked_folder.css', + 'body{background-url:url(../assets_symlink/asset.bmp)}', + ); + $tests[] = array( + $sourceRelative.'/move_symlinked_folder.css', + $targetRelative.'/move_symlinked_folder.css', + 'body{background-url:url(../assets_symlink/asset.bmp)}', + ); + $tests[] = array( + $source.'/move_symlinked_folder.css', + $targetRelative.'/move_symlinked_folder.css', + 'body{background-url:url(../assets_symlink/asset.bmp)}', + ); + $tests[] = array( + $sourceRelative.'/move_symlinked_folder.css', + $target.'/move_symlinked_folder.css', + 'body{background-url:url(../assets_symlink/asset.bmp)}', + ); + + return $tests; + } +} diff --git a/lib/tests/css/sample/bom/bom.css b/lib/tests/css/sample/bom/bom.css new file mode 100644 index 0000000..8317012 --- /dev/null +++ b/lib/tests/css/sample/bom/bom.css @@ -0,0 +1,3 @@ +body { + color: red; +} diff --git a/lib/tests/css/sample/combine_imports/import.css b/lib/tests/css/sample/combine_imports/import.css new file mode 100644 index 0000000..b05faf8 --- /dev/null +++ b/lib/tests/css/sample/combine_imports/import.css @@ -0,0 +1,3 @@ +body { + color: red; +} diff --git a/lib/tests/css/sample/combine_imports/index.css b/lib/tests/css/sample/combine_imports/index.css new file mode 100644 index 0000000..0efd0b2 --- /dev/null +++ b/lib/tests/css/sample/combine_imports/index.css @@ -0,0 +1 @@ +@import url(import.css); diff --git a/lib/tests/css/sample/combine_imports/index2.css b/lib/tests/css/sample/combine_imports/index2.css new file mode 100644 index 0000000..ba2ed83 --- /dev/null +++ b/lib/tests/css/sample/combine_imports/index2.css @@ -0,0 +1 @@ +@import 'import.css'; diff --git a/lib/tests/css/sample/combine_imports/index3.css b/lib/tests/css/sample/combine_imports/index3.css new file mode 100644 index 0000000..b56ac2e --- /dev/null +++ b/lib/tests/css/sample/combine_imports/index3.css @@ -0,0 +1,2 @@ +@import url(import.css); +@import 'import.css'; diff --git a/lib/tests/css/sample/combine_imports/index4.css b/lib/tests/css/sample/combine_imports/index4.css new file mode 100644 index 0000000..e385dfd --- /dev/null +++ b/lib/tests/css/sample/combine_imports/index4.css @@ -0,0 +1,2 @@ +@import url(import.css) only screen; +@import 'import.css' only screen; diff --git a/lib/tests/css/sample/combine_imports/index5.css b/lib/tests/css/sample/combine_imports/index5.css new file mode 100644 index 0000000..b56ac2e --- /dev/null +++ b/lib/tests/css/sample/combine_imports/index5.css @@ -0,0 +1,2 @@ +@import url(import.css); +@import 'import.css'; diff --git a/lib/tests/css/sample/combine_imports/index6a.css b/lib/tests/css/sample/combine_imports/index6a.css new file mode 100644 index 0000000..fb69c36 --- /dev/null +++ b/lib/tests/css/sample/combine_imports/index6a.css @@ -0,0 +1 @@ +@import url(index6b.css); diff --git a/lib/tests/css/sample/combine_imports/index6b.css b/lib/tests/css/sample/combine_imports/index6b.css new file mode 100644 index 0000000..0efd0b2 --- /dev/null +++ b/lib/tests/css/sample/combine_imports/index6b.css @@ -0,0 +1 @@ +@import url(import.css); diff --git a/lib/tests/css/sample/convert_relative_path/source/absolute.css b/lib/tests/css/sample/convert_relative_path/source/absolute.css new file mode 100644 index 0000000..6338d24 --- /dev/null +++ b/lib/tests/css/sample/convert_relative_path/source/absolute.css @@ -0,0 +1 @@ +@import url(/absolute/path/to/stylesheet.css); \ No newline at end of file diff --git a/lib/tests/css/sample/convert_relative_path/source/external.css b/lib/tests/css/sample/convert_relative_path/source/external.css new file mode 100644 index 0000000..4066b6d --- /dev/null +++ b/lib/tests/css/sample/convert_relative_path/source/external.css @@ -0,0 +1 @@ +@import url(http://fonts.googleapis.com/css?family=Lato:100,300,400,700,900|Droid+Sans:400,700); \ No newline at end of file diff --git a/lib/tests/css/sample/convert_relative_path/source/issue29.css b/lib/tests/css/sample/convert_relative_path/source/issue29.css new file mode 100644 index 0000000..f18343a --- /dev/null +++ b/lib/tests/css/sample/convert_relative_path/source/issue29.css @@ -0,0 +1 @@ +@import url( 'http://myurl.de'); diff --git a/lib/tests/css/sample/convert_relative_path/source/issue39.css b/lib/tests/css/sample/convert_relative_path/source/issue39.css new file mode 100644 index 0000000..b342a6e --- /dev/null +++ b/lib/tests/css/sample/convert_relative_path/source/issue39.css @@ -0,0 +1,10 @@ +@font-face { + font-family: 'blackcat'; + src:url('../webfont/blackcat.eot'); + src:url('../webfont/blackcat.eot?#iefix') format('embedded-opentype'), + url('../webfont/blackcat.svg#blackcat') format('svg'), + url('../webfont/blackcat.woff') format('woff'), + url('../webfont/blackcat.ttf') format('truetype'); + font-weight: normal; + font-style: normal; +} diff --git a/lib/tests/css/sample/convert_relative_path/source/nested/nested.css b/lib/tests/css/sample/convert_relative_path/source/nested/nested.css new file mode 100644 index 0000000..4636951 --- /dev/null +++ b/lib/tests/css/sample/convert_relative_path/source/nested/nested.css @@ -0,0 +1 @@ +@import url(../relative.css); diff --git a/lib/tests/css/sample/convert_relative_path/source/relative.css b/lib/tests/css/sample/convert_relative_path/source/relative.css new file mode 100644 index 0000000..76b01d2 --- /dev/null +++ b/lib/tests/css/sample/convert_relative_path/source/relative.css @@ -0,0 +1 @@ +@import url(../target/stylesheet.css); \ No newline at end of file diff --git a/lib/tests/css/sample/external_imports/file.css b/lib/tests/css/sample/external_imports/file.css new file mode 100644 index 0000000..302ed05 --- /dev/null +++ b/lib/tests/css/sample/external_imports/file.css @@ -0,0 +1,3 @@ +body { + background: green; +} \ No newline at end of file diff --git a/lib/tests/css/sample/external_imports/issue68.css b/lib/tests/css/sample/external_imports/issue68.css new file mode 100644 index 0000000..b41a1c8 --- /dev/null +++ b/lib/tests/css/sample/external_imports/issue68.css @@ -0,0 +1,2 @@ +@import "file.css"; +@import url(http://localhost/file.css); \ No newline at end of file diff --git a/lib/tests/css/sample/import_files/facebook.png b/lib/tests/css/sample/import_files/facebook.png new file mode 100644 index 0000000..ff27ba3 Binary files /dev/null and b/lib/tests/css/sample/import_files/facebook.png differ diff --git a/lib/tests/css/sample/import_files/file.png b/lib/tests/css/sample/import_files/file.png new file mode 100644 index 0000000..ff27ba3 Binary files /dev/null and b/lib/tests/css/sample/import_files/file.png differ diff --git a/lib/tests/css/sample/import_files/index.css b/lib/tests/css/sample/import_files/index.css new file mode 100644 index 0000000..d57bae0 --- /dev/null +++ b/lib/tests/css/sample/import_files/index.css @@ -0,0 +1 @@ +body { background: url(file.png); } diff --git a/lib/tests/css/sample/import_files/instagram.png b/lib/tests/css/sample/import_files/instagram.png new file mode 100644 index 0000000..ff27ba3 Binary files /dev/null and b/lib/tests/css/sample/import_files/instagram.png differ diff --git a/lib/tests/css/sample/import_files/issue49.css b/lib/tests/css/sample/import_files/issue49.css new file mode 100644 index 0000000..06cd08e --- /dev/null +++ b/lib/tests/css/sample/import_files/issue49.css @@ -0,0 +1,3 @@ +.social-btn a[href*="facebook"] { background-image: url(facebook.png) } /* encode skipped */ +.social-btn a[href*="vimeo"] { background-image: url(vimeo.png) } +.social-btn a[href*="instagram"] { background-image: url(instagram.png) } \ No newline at end of file diff --git a/lib/tests/css/sample/import_files/vimeo.png b/lib/tests/css/sample/import_files/vimeo.png new file mode 100644 index 0000000..ff27ba3 Binary files /dev/null and b/lib/tests/css/sample/import_files/vimeo.png differ diff --git a/lib/tests/css/sample/symlink/assets/symlink.bmp b/lib/tests/css/sample/symlink/assets/symlink.bmp new file mode 100644 index 0000000..cb1adfb --- /dev/null +++ b/lib/tests/css/sample/symlink/assets/symlink.bmp @@ -0,0 +1 @@ +../other_path/assets/asset.bmp \ No newline at end of file diff --git a/lib/tests/css/sample/symlink/assets_symlink b/lib/tests/css/sample/symlink/assets_symlink new file mode 100644 index 0000000..19a3e62 --- /dev/null +++ b/lib/tests/css/sample/symlink/assets_symlink @@ -0,0 +1 @@ +other_path/assets \ No newline at end of file diff --git a/lib/tests/css/sample/symlink/import_symlinked_file.css b/lib/tests/css/sample/symlink/import_symlinked_file.css new file mode 100644 index 0000000..22a8217 --- /dev/null +++ b/lib/tests/css/sample/symlink/import_symlinked_file.css @@ -0,0 +1 @@ +@import url(imports/symlink.css); \ No newline at end of file diff --git a/lib/tests/css/sample/symlink/import_symlinked_folder.css b/lib/tests/css/sample/symlink/import_symlinked_folder.css new file mode 100644 index 0000000..3e81fbd --- /dev/null +++ b/lib/tests/css/sample/symlink/import_symlinked_folder.css @@ -0,0 +1 @@ +@import url(imports_symlink/import.css); \ No newline at end of file diff --git a/lib/tests/css/sample/symlink/imports/symlink.css b/lib/tests/css/sample/symlink/imports/symlink.css new file mode 100644 index 0000000..297dc22 --- /dev/null +++ b/lib/tests/css/sample/symlink/imports/symlink.css @@ -0,0 +1 @@ +../other_path/imports/import.css \ No newline at end of file diff --git a/lib/tests/css/sample/symlink/imports_symlink b/lib/tests/css/sample/symlink/imports_symlink new file mode 100644 index 0000000..2aecae9 --- /dev/null +++ b/lib/tests/css/sample/symlink/imports_symlink @@ -0,0 +1 @@ +other_path/imports \ No newline at end of file diff --git a/lib/tests/css/sample/symlink/move_symlinked_file.css b/lib/tests/css/sample/symlink/move_symlinked_file.css new file mode 100644 index 0000000..aababbd --- /dev/null +++ b/lib/tests/css/sample/symlink/move_symlinked_file.css @@ -0,0 +1 @@ +body{background-url:url(assets/symlink.bmp)} \ No newline at end of file diff --git a/lib/tests/css/sample/symlink/move_symlinked_folder.css b/lib/tests/css/sample/symlink/move_symlinked_folder.css new file mode 100644 index 0000000..9b77b0c --- /dev/null +++ b/lib/tests/css/sample/symlink/move_symlinked_folder.css @@ -0,0 +1 @@ +body{background-url:url(assets_symlink/asset.bmp)} \ No newline at end of file diff --git a/lib/tests/css/sample/symlink/other_path/assets/asset.bmp b/lib/tests/css/sample/symlink/other_path/assets/asset.bmp new file mode 100644 index 0000000..e69de29 diff --git a/lib/tests/css/sample/symlink/other_path/imports/import.css b/lib/tests/css/sample/symlink/other_path/imports/import.css new file mode 100644 index 0000000..e69de29 diff --git a/lib/tests/js/AbstractTest.php b/lib/tests/js/AbstractTest.php new file mode 100644 index 0000000..e00e6ce --- /dev/null +++ b/lib/tests/js/AbstractTest.php @@ -0,0 +1,149 @@ +minify(); + + $this->assertEquals($content1, $result); + + // multiple sources in constructor + $minifier = new Minify\JS($content1, $content2); + $result = $minifier->minify(); + + $this->assertEquals($content1.';'.$content2, $result); + + // file in constructor + $minifier = new Minify\JS($path1); + $result = $minifier->minify(); + + $this->assertEquals($content1, $result); + + // multiple files in constructor + $minifier = new Minify\JS($path1, $path2); + $result = $minifier->minify(); + + $this->assertEquals($content1.';'.$content2, $result); + } + + /** + * @test + */ + public function add() + { + $path1 = __DIR__.'/sample/source/script1.js'; + $path2 = __DIR__.'/sample/source/script2.js'; + $content1 = file_get_contents($path1); + $content2 = file_get_contents($path2); + + // 1 source in add + $minifier = new Minify\JS(); + $minifier->add($content1); + $result = $minifier->minify(); + + $this->assertEquals($content1, $result); + + // multiple sources in add + $minifier = new Minify\JS(); + $minifier->add($content1); + $minifier->add($content2); + $result = $minifier->minify(); + + $this->assertEquals($content1.';'.$content2, $result); + + // file in add + $minifier = new Minify\JS(); + $minifier->add($path1); + $result = $minifier->minify(); + + $this->assertEquals($content1, $result); + + // multiple files in add + $minifier = new Minify\JS(); + $minifier->add($path1); + $minifier->add($path2); + $result = $minifier->minify(); + + $this->assertEquals($content1.';'.$content2, $result); + } + + /** + * @test + */ + public function load() + { + // content greater than PHP_MAXPATHLEN + // https://github.com/matthiasmullie/minify/issues/90 + $content = rtrim(str_repeat('var a="b";', 500), ';'); + + $minifier = new Minify\JS($content); + + $this->assertEquals($minifier->minify(), $content); + } + + /** + * @test + */ + public function save() + { + $path = __DIR__.'/sample/source/script1.js'; + $content = file_get_contents($path); + $savePath = __DIR__.'/sample/target/script1.js'; + + $minifier = new Minify\JS($path); + $minifier->minify($savePath); + + $this->assertEquals(file_get_contents($savePath), $content); + } + + /** + * @test + */ + public function gzip() + { + $path = __DIR__.'/sample/source/script1.js'; + $content = file_get_contents($path); + $savePath = __DIR__.'/sample/target/script1.js.gz'; + + $minifier = new Minify\JS($path); + $minifier->gzip($savePath, 9); + + $this->assertEquals(file_get_contents($savePath), gzencode($content, 9, FORCE_GZIP)); + } + + /** + * @test + */ + public function cache() + { + $path = __DIR__.'/sample/source/script1.js'; + $content = file_get_contents($path); + + $cache = new MemoryStore(); + $pool = new Pool($cache); + $item = $pool->getItem('cache-script1'); + + $minifier = new Minify\JS($path); + $item = $minifier->cache($item); + + $this->assertEquals($item->get(), $content); + } +} diff --git a/lib/tests/js/JSTest.php b/lib/tests/js/JSTest.php new file mode 100644 index 0000000..0498ad7 --- /dev/null +++ b/lib/tests/js/JSTest.php @@ -0,0 +1,670 @@ +minifier = $this->getMockBuilder('\MatthiasMullie\Minify\JS') + ->setMethods(array('save')) + ->getMock(); + } + + /** + * Cleans up the environment after running a test. + */ + protected function tearDown() + { + $this->minifier = null; + parent::tearDown(); + } + + /** + * Test JS minifier rules, provided by dataProvider. + * + * @test + * @dataProvider dataProvider + */ + public function minify($input, $expected) + { + $input = (array) $input; + foreach ($input as $js) { + $this->minifier->add($js); + } + $result = $this->minifier->minify(); + + $this->assertEquals($expected, $result); + } + + /** + * @return array [input, expected result] + */ + public function dataProvider() + { + $tests = array(); + + // escaped quotes should not terminate string + $tests[] = array( + 'alert("Escaped quote which is same as string quotes: \"; should not match")', + 'alert("Escaped quote which is same as string quotes: \"; should not match")', + ); + + // backtick string (allow string interpolation) + $tests[] = array( + 'var str=`Hi, ${name}`', + 'var str=`Hi, ${name}`', + ); + + // regex delimiters need to be treated as strings + // (two forward slashes could look like a comment) + $tests[] = array( + '/abc\/def\//.test("abc")', + '/abc\/def\//.test("abc")', + ); + $tests[] = array( + 'var a = /abc\/def\//.test("abc")', + 'var a=/abc\/def\//.test("abc")', + ); + + // don't confuse multiple slashes for regexes + $tests[] = array( + 'a = b / c; d = e / f', + 'a=b/c;d=e/f', + ); + + // mixture of quotes starting in comment/regex, to make sure strings are + // matched correctly, not inside comment/regex. + $tests[] = array( + '/abc"def/.test("abc")', + '/abc"def/.test("abc")', + ); + $tests[] = array( + '/* Bogus " */var test="test";', + 'var test="test"', + ); + + // replace comments + $tests[] = array( + '/* This is a JS comment */', + '', + ); + + // make sure no ; is added in places it shouldn't + $tests[] = array( + 'if(true){}else{}', + 'if(!0){}else{}', + ); + $tests[] = array( + 'do{i++}while(i<1)', + 'do{i++}while(i<1)', + ); + + $tests[] = array( + 'if(true)statement;else statement', + 'if(!0)statement;else statement', + ); + + $tests[] = array( + 'for ( i = 0; ; i++ ) statement', + 'for(i=0;;i++)statement', + ); + $tests[] = array( + 'for (i = 0; (i < 10); i++) statement', + 'for(i=0;(i<10);i++)statement', + ); + $tests[] = array( + 'alert("test");;alert("test2")', + 'alert("test");alert("test2")', + ); + $tests[] = array( + '-1 + +2', + '-1+2', + ); + $tests[] = array( + '-1+ + 2', + '-1+2', + ); + $tests[] = array( + 'alert("this is a test");', + 'alert("this is a test")', + ); + + // test where newline should be preserved (for ASI) or semicolon added + $tests[] = array( + 'function(){console.log("this is a test");}', + 'function(){console.log("this is a test")}', + ); + $tests[] = array( + 'alert("this is a test") +alert("this is another test")', + 'alert("this is a test") +alert("this is another test")', + ); + $tests[] = array( + 'a=b+c + d=e+f', + 'a=b+c +d=e+f', + ); + $tests[] = array( + 'a++ + + ++b', + 'a++ +++b', + ); + $tests[] = array( + '!a + !b', + '!a +!b', + ); + $tests[] = array( + // don't confuse with 'if' + 'digestif + (true) + statement', + 'digestif(!0) +statement', + ); + $tests[] = array( + 'if + ( + ( + true + ) + && + ( + true + ) + ) + statement', + 'if((!0)&&(!0)) +statement', + ); + $tests[] = array( + 'if + ( + true + ) + { + } + else + { + }', + 'if(!0){} +else{}', + ); + $tests[] = array( + 'do + { + i++ + } + while + ( + i<1 + )', + 'do{i++} +while(i<1)', + ); + $tests[] = array( + 'if ( true ) + statement + else + statement', + 'if(!0) +statement +else statement', + ); + + // test if whitespace around keywords is properly collapsed + $tests[] = array( + 'var + variable + = + "value";', + 'var variable="value"', + ); + $tests[] = array( + 'var variable = { + test: + { + } + }', + 'var variable={test:{}}', + ); + $tests[] = array( + 'if ( true ) { + } else { + }', + 'if(!0){}else{}', + ); + $tests[] = array( + '53 instanceof String', + '53 instanceof String', + ); + + // remove whitespace around operators + $tests[] = array( + 'a = 1 + 2', + 'a=1+2', + ); + $tests[] = array( + 'object . property', + 'object.property', + ); + $tests[] = array( + 'object + .property', + 'object.property', + ); + $tests[] = array( + 'alert ( "this is a test" );', + 'alert("this is a test")', + ); + + // mix of ++ and +: three consecutive +es will be interpreted as ++ + + $tests[] = array( + 'a++ +b', + 'a++ +b', + ); + $tests[] = array( + 'a+ ++b', + 'a+ ++b', // +++ would actually be allowed as well + ); + + // SyntaxError: identifier starts immediately after numeric literal + $tests[] = array( + '42 .toString()', + '42 .toString()', + ); + + // add comment in between whitespace that needs to be stripped + $tests[] = array( + 'object + // haha, some comment, just to make things harder! + .property', + 'object.property', + ); + + // add comment in between whitespace that needs to be stripped + $tests[] = array( + 'var test=true,test2=false', + 'var test=!0,test2=!1', + ); + $tests[] = array( + 'var testtrue="testing if true as part of varname is ignored as it should"', + 'var testtrue="testing if true as part of varname is ignored as it should"', + ); + + // random bits of code that tripped errors during development + $tests[] = array( + ' + // check if it isn\'t a text-element + if(currentElement.attr(\'type\') != \'text\') + { + // remove the current one + currentElement.remove(); + } + + // already a text element + else newElement = currentElement; +', + 'if(currentElement.attr(\'type\')!=\'text\'){currentElement.remove()} +else newElement=currentElement', + ); + $tests[] = array( + 'var jsBackend = + { + debug: false, + current: {} + }', + 'var jsBackend={debug:!1,current:{}}', + ); + $tests[] = array( + 'var utils = + { + debug: false + } + utils.array = + { + }', + 'var utils={debug:!1} +utils.array={}', + ); + $tests[] = array( + 'rescape = /\'|\\\\/g, + + // blablabla here was some more code but the point was that somewhere + // down below, there would be a closing quote which would cause the + // regex (confused for escaped closing tag) not to be recognized, + // taking the opening single quote & looking for a string. + // So here\'s <-- the closing quote + runescape = \'blabla\'', + 'rescape=/\'|\\\\/g,runescape=\'blabla\'', + ); + $tests[] = array( + 'var rsingleTag = (/^<(\w+)\s*\/?>(?:<\/\1>|)$/)', + 'var rsingleTag=(/^<(\w+)\s*\/?>(?:<\/\1>|)$/)', + ); + $tests[] = array( + 'if (this.sliding) return this.$element.one(\'slid.bs.carousel\', function () { that.to(pos) }) // yes, "slid" +if (activeIndex == pos) return this.pause().cycle()', + 'if(this.sliding)return this.$element.one(\'slid.bs.carousel\',function(){that.to(pos)}) +if(activeIndex==pos)return this.pause().cycle()', + ); + $tests[] = array( + 'if (e.which == 38 && index > 0) index-- // up +if (e.which == 40 && index < $items.length - 1) index++ // down', + 'if(e.which==38&&index>0)index-- +if(e.which==40&&index<$items.length-1)index++', + ); + + // replace associative array key references by property notation + $tests[] = array( + 'array["key"][\'key2\']', + 'array.key.key2', + ); + $tests[] = array( + 'array[ "key" ][ \'key2\' ]', + 'array.key.key2', + ); + $tests[] = array( + 'array["a","b","c"]', + 'array["a","b","c"]', + ); + // + $tests[] = array( + "['loader']", + "['loader']", + ); + $tests[] = array( + 'array["dont-replace"][\'key2\']', + 'array["dont-replace"].key2', + ); + + // shorten bools + $tests[] = array( + 'while(true){break}', + 'for(;;){break}', + ); + // make sure we don't get "missing while after do-loop body" + $tests[] = array( + 'do{break}while(true)', + 'do{break}while(!0)', + ); + $tests[] = array( + "do break\nwhile(true)", + "do break\nwhile(!0)", + ); + $tests[] = array( + "do{break}while(true){alert('test')}", + "do{break}while(!0){alert('test')}", + ); + $tests[] = array( + "do break\nwhile(true){alert('test')}", + "do break\nwhile(!0){alert('test')}", + ); + // nested do-while & while + $tests[] = array( + "do{while(true){break}break}while(true){alert('test')}", + "do{for(;;){break}break}while(!0){alert('test')}", + ); + $tests[] = array( + "do{while(true){break}break}while(true){alert('test')}while(true){break}", + "do{for(;;){break}break}while(!0){alert('test')}for(;;){break}", + ); + $tests[] = array( + "do{while(true){break}break}while(true){alert('test')}while(true){break}do{while(true){break}break}while(true){alert('test')}while(true){break}", + "do{for(;;){break}break}while(!0){alert('test')}for(;;){break}do{for(;;){break}break}while(!0){alert('test')}for(;;){break}", + ); + + // https://github.com/matthiasmullie/minify/issues/10 + $tests[] = array( + '// first mutation patch +// second mutation patch +// third mutation patch +// fourth mutation patch', + '', + ); + $tests[] = array( + '///////////////////////// +// first mutation patch +// second mutation patch +// third mutation patch +// fourth mutation patch +/////////////////////////', + '', + ); + + // https://github.com/matthiasmullie/minify/issues/14 + $tests[] = array( + 'function foo (a, b) +{ + return a / b; +} +function foo (a, b) +{ + return a / b; +}', + 'function foo(a,b){return a/b} +function foo(a,b){return a/b}', + ); + + // https://github.com/matthiasmullie/minify/issues/15 + $tests[] = array( + 'if ( !data.success ) + deferred.reject(); else + deferred.resolve(data);', + 'if(!data.success) +deferred.reject();else deferred.resolve(data)', + ); + $tests[] = array( + "if ( typeof jQuery === 'undefined' ) + throw new Error('.editManager.js: jQuery is required and must be loaded first');", + "if(typeof jQuery==='undefined') +throw new Error('.editManager.js: jQuery is required and must be loaded first')", + ); + + // https://github.com/matthiasmullie/minify/issues/27 + $tests[] = array( + '$.expr[":"]', + '$.expr[":"]', + ); + + // https://github.com/matthiasmullie/minify/issues/31 + $tests[] = array( + "$(_this).attr('src',this.src).trigger('adapt',['loader'])", + "$(_this).attr('src',this.src).trigger('adapt',['loader'])", + ); + + // https://github.com/matthiasmullie/minify/issues/33 + $tests[] = array( + '$.fn.alert = Plugin +$.fn.alert.Constructor = Alert', + '$.fn.alert=Plugin +$.fn.alert.Constructor=Alert', + ); + + // https://github.com/matthiasmullie/minify/issues/34 + $tests[] = array( + 'a.replace("\\\\","");hi="This is a string"', + 'a.replace("\\\\","");hi="This is a string"', + ); + + // https://github.com/matthiasmullie/minify/issues/35 + $tests[] = array( + array( + '// script that ends with comment', + 'var test=1', + ), + 'var test=1', + ); + + // https://github.com/matthiasmullie/minify/issues/37 + $tests[] = array( + 'function () { ;;;;;;;; }', + 'function(){}', + ); + + // https://github.com/matthiasmullie/minify/issues/40 + $tests[] = array( + 'for(v=1,_=b;;){}', + 'for(v=1,_=b;;){}', + ); + + // https://github.com/matthiasmullie/minify/issues/41 + $tests[] = array( + "conf.zoomHoverIcons['default']", + "conf.zoomHoverIcons['default']", + ); + + // https://github.com/matthiasmullie/minify/issues/42 + $tests[] = array( + 'for(i=1;i<2;i++);', + 'for(i=1;i<2;i++);', + ); + $tests[] = array( + 'if(1){for(i=1;i<2;i++);}', + 'if(1){for(i=1;i<2;i++);}', + ); + + // https://github.com/matthiasmullie/minify/issues/43 + $tests[] = array( + '{"key":"3","key2":"value","key3":"3"}', + '{"key":"3","key2":"value","key3":"3"}', + ); + + // https://github.com/matthiasmullie/minify/issues/44 + $tests[] = array( + 'return ["x"]', + 'return["x"]', + ); + + // https://github.com/matthiasmullie/minify/issues/50 + $tests[] = array( + 'do{var dim=this._getDaysInMonth(year,month-1);if(day<=dim){break}month++;day-=dim}while(true)}', + 'do{var dim=this._getDaysInMonth(year,month-1);if(day<=dim){break}month++;day-=dim}while(!0)}', + ); + + // https://github.com/matthiasmullie/minify/issues/53 + $tests[] = array( + 'a.validator.addMethod("accept", function (b, c, d) { + var e, f, g = "string" == typeof d ? + d.replace(/\s/g, "").replace(/,/g, "|") : + "image/*", h = this.optional(c); + if (h)return h; + if ("file" === a(c).attr("type") && (g = g.replace(/\*/g, ".*"), c.files && c.files.length)) + for (e = 0; e < c.files.length; e++) + if (f = c.files[e], !f.type.match(new RegExp(".?(" + g + ")$", "i"))) + return !1; + return !0 +}', + 'a.validator.addMethod("accept",function(b,c,d){var e,f,g="string"==typeof d?d.replace(/\s/g,"").replace(/,/g,"|"):"image/*",h=this.optional(c);if(h)return h;if("file"===a(c).attr("type")&&(g=g.replace(/\*/g,".*"),c.files&&c.files.length)) +for(e=0;e