PHP Class 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.
Author: Matthias Mullie ([email protected])
Author: Tijs Verkoyen ([email protected])
Inheritance: extends Minify
Datei anzeigen Open project: matthiasmullie/minify Class Usage Examples

Protected Properties

Property Type Description
$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

Public Methods

Method Description
__construct ( )
execute ( string[optional] $path = null ) : string Minify the data.

Protected Methods

Method Description
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.

Method Details

__construct() public method

public __construct ( )

execute() public method

Perform JS optimizations.
public execute ( string[optional] $path = null ) : string
$path string[optional]
return string The minified data

extractRegex() protected method

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() protected method

This will prepare the given array by escaping all characters.
protected getKeywordsForRegex ( array $keywords, string $delimiter = '/' ) : string[]
$keywords array
$delimiter string
return string[]

getOperatorsForRegex() protected method

This will prepare the given array by escaping all characters.
protected getOperatorsForRegex ( array $operators, string $delimiter = '/' ) : string[]
$operators array
$delimiter string
return string[]

propertyNotation() protected method

Replaces all occurrences of array['key'] by array.key.
protected propertyNotation ( string $content ) : string
$content string
return string

shortenBools() protected method

Replaces true & false by !0 and !1.
protected shortenBools ( string $content ) : string
$content string
return string

stripComments() protected method

Strip comments from source code.
protected stripComments ( )

stripWhitespace() protected method

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
return string

Property Details

$keywordsAfter protected_oe property

..> 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
return string[]

$keywordsBefore protected_oe property

..> 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
return string[]

$keywordsReserved protected_oe property

Will be loaded from /data/js/keywords_reserved.txt.
See also: https://mathiasbynens.be/notes/reserved-keywords
protected string[] $keywordsReserved
return string[]

$operators protected_oe property

Will be loaded from /data/js/operators.txt
See also: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators
protected string[] $operators
return string[]

$operatorsAfter protected_oe property

..> 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 also: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators
protected string[] $operatorsAfter
return string[]

$operatorsBefore protected_oe property

..> 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 also: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators
protected string[] $operatorsBefore
return string[]