PHP Класс MatthiasMullie\Minify\JS

This source file can be used to minify Javascript files. The class is documented in the file itself. If you find any bugs help me out and report them. Reporting can be done by sending an email to [email protected]. If you report a bug, make sure you give me enough information (include your code). License Copyright (c) 2012, Matthias Mullie. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. This software is provided by the author "as is" and any express or implied warranties, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose are disclaimed. In no event shall the author be liable for any direct, indirect, incidental, special, exemplary, or consequential damages (including, but not limited to, procurement of substitute goods or services; loss of use, data, or profits; or business interruption) however caused and on any theory of liability, whether in contract, strict liability, or tort (including negligence or otherwise) arising in any way out of the use of this software, even if advised of the possibility of such damage.
Автор: Matthias Mullie ([email protected])
Автор: Tijs Verkoyen ([email protected])
Наследование: extends Minify
Показать файл Открыть проект Примеры использования класса

Защищенные свойства (Protected)

Свойство Тип Описание
$keywordsAfter string[] ..> 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
$keywordsBefore string[] ..> 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
$keywordsReserved string[] Will be loaded from /data/js/keywords_reserved.txt.
$operators string[] Will be loaded from /data/js/operators.txt
$operatorsAfter string[] ..> 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
$operatorsBefore string[] ..> 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

Открытые методы

Метод Описание
__construct ( )
execute ( string[optional] $path = null ) : string Minify the data.

Защищенные методы

Метод Описание
extractRegex ( ) JS can have /-delimited regular expressions, like: /ab+c/.match(string).
getKeywordsForRegex ( array $keywords, string $delimiter = '/' ) : string[] We'll strip whitespace around certain keywords with regular expressions.
getOperatorsForRegex ( array $operators, string $delimiter = '/' ) : string[] We'll strip whitespace around certain operators with regular expressions.
propertyNotation ( string $content ) : string Replaces all occurrences of array['key'] by array.key.
shortenBools ( string $content ) : string Replaces true & false by !0 and !1.
stripComments ( ) Strip comments from source code.
stripWhitespace ( string $content ) : string Strip whitespace.

Описание методов

__construct() публичный Метод

public __construct ( )

execute() публичный Метод

Perform JS optimizations.
public execute ( string[optional] $path = null ) : string
$path string[optional]
Результат string The minified data

extractRegex() защищенный Метод

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 extractRegex ( )

getKeywordsForRegex() защищенный Метод

This will prepare the given array by escaping all characters.
protected getKeywordsForRegex ( array $keywords, string $delimiter = '/' ) : string[]
$keywords array
$delimiter string
Результат string[]

getOperatorsForRegex() защищенный Метод

This will prepare the given array by escaping all characters.
protected getOperatorsForRegex ( array $operators, string $delimiter = '/' ) : string[]
$operators array
$delimiter string
Результат string[]

propertyNotation() защищенный Метод

Replaces all occurrences of array['key'] by array.key.
protected propertyNotation ( string $content ) : string
$content string
Результат string

shortenBools() защищенный Метод

Replaces true & false by !0 and !1.
protected shortenBools ( string $content ) : string
$content string
Результат string

stripComments() защищенный Метод

Strip comments from source code.
protected stripComments ( )

stripWhitespace() защищенный Метод

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.
protected stripWhitespace ( string $content ) : string
$content string The content to strip the whitespace for
Результат string

Описание свойств

$keywordsAfter защищенное свойство

..> 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
protected string[] $keywordsAfter
Результат string[]

$keywordsBefore защищенное свойство

..> 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
protected string[] $keywordsBefore
Результат string[]

$keywordsReserved защищенное свойство

Will be loaded from /data/js/keywords_reserved.txt.
См. также: https://mathiasbynens.be/notes/reserved-keywords
protected string[] $keywordsReserved
Результат string[]

$operators защищенное свойство

Will be loaded from /data/js/operators.txt
См. также: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators
protected string[] $operators
Результат string[]

$operatorsAfter защищенное свойство

..> 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
См. также: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators
protected string[] $operatorsAfter
Результат string[]

$operatorsBefore защищенное свойство

..> 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
См. также: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators
protected string[] $operatorsBefore
Результат string[]