MediaWiki REL1_30
JavaScriptMinifier Class Reference

This class is meant to safely minify javascript code, while leaving syntactically correct programs intact. More...

Static Public Member Functions

static minify ( $s, $statementsOnOwnLine=false, $maxLineLength=1000)
 Returns minified JavaScript code.
 
static parseError ($fullJavascript, $position, $errorMsg)
 

Public Attributes

const CONDITION = 1
 
const EXPRESSION = 3
 
const EXPRESSION_FUNC = 6
 
const EXPRESSION_NO_NL = 4
 
const EXPRESSION_OP = 5
 
const EXPRESSION_TERNARY = 7
 
const EXPRESSION_TERNARY_FUNC = 9
 
const EXPRESSION_TERNARY_OP = 8
 
const PAREN_EXPRESSION = 10
 
const PAREN_EXPRESSION_FUNC = 12
 
const PAREN_EXPRESSION_OP = 11
 
const PROPERTY_ASSIGNMENT = 2
 
const PROPERTY_EXPRESSION = 13
 
const PROPERTY_EXPRESSION_FUNC = 15
 
const PROPERTY_EXPRESSION_OP = 14
 
const STACK_LIMIT = 1000
 
const STATEMENT = 0
 
const TYPE_ADD_OP = 4
 
const TYPE_BIN_OP = 3
 
const TYPE_BRACE_CLOSE = 10
 
const TYPE_BRACE_OPEN = 9
 
const TYPE_COLON = 6
 
const TYPE_COMMA = 7
 
const TYPE_DO = 15
 
const TYPE_FUNC = 16
 
const TYPE_HOOK = 5
 
const TYPE_IF = 14
 
const TYPE_INCR_OP = 2
 
const TYPE_LITERAL = 17
 
const TYPE_PAREN_CLOSE = 12
 
const TYPE_PAREN_OPEN = 11
 
const TYPE_RETURN = 13
 
const TYPE_SEMICOLON = 8
 
const TYPE_UN_OP = 1
 

Detailed Description

This class is meant to safely minify javascript code, while leaving syntactically correct programs intact.

Other libraries, such as JSMin require a certain coding style to work correctly. OTOH, libraries like jsminplus, that do parse the code correctly are rather slow, because they construct a complete parse tree before outputting the code minified. So this class is meant to allow arbitrary (but syntactically correct) input, while being fast enough to be used for on-the-fly minifying.

Definition at line 19 of file JavaScriptMinifier.php.

Member Function Documentation

◆ minify()

static JavaScriptMinifier::minify ( $s,
$statementsOnOwnLine = false,
$maxLineLength = 1000 )
static

Returns minified JavaScript code.

NOTE: $maxLineLength isn't a strict maximum. Longer lines will be produced when literals (e.g. quoted strings) longer than $maxLineLength are encountered or when required to guard against semicolon insertion.

Parameters
string$sJavaScript code to minify
bool$statementsOnOwnLineWhether to put each statement on its own line
int$maxLineLengthMaximum length of a single line, or -1 for no maximum.
Returns
String Minified code

Definition at line 81 of file JavaScriptMinifier.php.

References $last, $out, $s, $type, array(), parseError(), STATEMENT, and TYPE_LITERAL.

Referenced by ResourceLoader\applyFilter(), MinifyScript\minify(), JavaScriptMinifierTest\testExponentLineBreaking(), and JavaScriptMinifierTest\testJavaScriptMinifierOutput().

◆ parseError()

static JavaScriptMinifier::parseError ( $fullJavascript,
$position,
$errorMsg )
static

Definition at line 611 of file JavaScriptMinifier.php.

Referenced by minify().

Member Data Documentation

◆ CONDITION

const JavaScriptMinifier::CONDITION = 1

Definition at line 29 of file JavaScriptMinifier.php.

◆ EXPRESSION

const JavaScriptMinifier::EXPRESSION = 3

Definition at line 31 of file JavaScriptMinifier.php.

◆ EXPRESSION_FUNC

const JavaScriptMinifier::EXPRESSION_FUNC = 6

Definition at line 34 of file JavaScriptMinifier.php.

◆ EXPRESSION_NO_NL

const JavaScriptMinifier::EXPRESSION_NO_NL = 4

Definition at line 32 of file JavaScriptMinifier.php.

◆ EXPRESSION_OP

const JavaScriptMinifier::EXPRESSION_OP = 5

Definition at line 33 of file JavaScriptMinifier.php.

◆ EXPRESSION_TERNARY

const JavaScriptMinifier::EXPRESSION_TERNARY = 7

Definition at line 35 of file JavaScriptMinifier.php.

◆ EXPRESSION_TERNARY_FUNC

const JavaScriptMinifier::EXPRESSION_TERNARY_FUNC = 9

Definition at line 37 of file JavaScriptMinifier.php.

◆ EXPRESSION_TERNARY_OP

const JavaScriptMinifier::EXPRESSION_TERNARY_OP = 8

Definition at line 36 of file JavaScriptMinifier.php.

◆ PAREN_EXPRESSION

const JavaScriptMinifier::PAREN_EXPRESSION = 10

Definition at line 38 of file JavaScriptMinifier.php.

◆ PAREN_EXPRESSION_FUNC

const JavaScriptMinifier::PAREN_EXPRESSION_FUNC = 12

Definition at line 40 of file JavaScriptMinifier.php.

◆ PAREN_EXPRESSION_OP

const JavaScriptMinifier::PAREN_EXPRESSION_OP = 11

Definition at line 39 of file JavaScriptMinifier.php.

◆ PROPERTY_ASSIGNMENT

const JavaScriptMinifier::PROPERTY_ASSIGNMENT = 2

Definition at line 30 of file JavaScriptMinifier.php.

◆ PROPERTY_EXPRESSION

const JavaScriptMinifier::PROPERTY_EXPRESSION = 13

Definition at line 41 of file JavaScriptMinifier.php.

◆ PROPERTY_EXPRESSION_FUNC

const JavaScriptMinifier::PROPERTY_EXPRESSION_FUNC = 15

Definition at line 43 of file JavaScriptMinifier.php.

◆ PROPERTY_EXPRESSION_OP

const JavaScriptMinifier::PROPERTY_EXPRESSION_OP = 14

Definition at line 42 of file JavaScriptMinifier.php.

◆ STACK_LIMIT

const JavaScriptMinifier::STACK_LIMIT = 1000

Definition at line 65 of file JavaScriptMinifier.php.

◆ STATEMENT

const JavaScriptMinifier::STATEMENT = 0

Definition at line 28 of file JavaScriptMinifier.php.

Referenced by minify().

◆ TYPE_ADD_OP

const JavaScriptMinifier::TYPE_ADD_OP = 4

Definition at line 49 of file JavaScriptMinifier.php.

◆ TYPE_BIN_OP

const JavaScriptMinifier::TYPE_BIN_OP = 3

Definition at line 48 of file JavaScriptMinifier.php.

◆ TYPE_BRACE_CLOSE

const JavaScriptMinifier::TYPE_BRACE_CLOSE = 10

Definition at line 55 of file JavaScriptMinifier.php.

◆ TYPE_BRACE_OPEN

const JavaScriptMinifier::TYPE_BRACE_OPEN = 9

Definition at line 54 of file JavaScriptMinifier.php.

◆ TYPE_COLON

const JavaScriptMinifier::TYPE_COLON = 6

Definition at line 51 of file JavaScriptMinifier.php.

◆ TYPE_COMMA

const JavaScriptMinifier::TYPE_COMMA = 7

Definition at line 52 of file JavaScriptMinifier.php.

◆ TYPE_DO

const JavaScriptMinifier::TYPE_DO = 15

Definition at line 60 of file JavaScriptMinifier.php.

◆ TYPE_FUNC

const JavaScriptMinifier::TYPE_FUNC = 16

Definition at line 61 of file JavaScriptMinifier.php.

◆ TYPE_HOOK

const JavaScriptMinifier::TYPE_HOOK = 5

Definition at line 50 of file JavaScriptMinifier.php.

◆ TYPE_IF

const JavaScriptMinifier::TYPE_IF = 14

Definition at line 59 of file JavaScriptMinifier.php.

◆ TYPE_INCR_OP

const JavaScriptMinifier::TYPE_INCR_OP = 2

Definition at line 47 of file JavaScriptMinifier.php.

◆ TYPE_LITERAL

const JavaScriptMinifier::TYPE_LITERAL = 17

Definition at line 62 of file JavaScriptMinifier.php.

Referenced by minify().

◆ TYPE_PAREN_CLOSE

const JavaScriptMinifier::TYPE_PAREN_CLOSE = 12

Definition at line 57 of file JavaScriptMinifier.php.

◆ TYPE_PAREN_OPEN

const JavaScriptMinifier::TYPE_PAREN_OPEN = 11

Definition at line 56 of file JavaScriptMinifier.php.

◆ TYPE_RETURN

const JavaScriptMinifier::TYPE_RETURN = 13

Definition at line 58 of file JavaScriptMinifier.php.

◆ TYPE_SEMICOLON

const JavaScriptMinifier::TYPE_SEMICOLON = 8

Definition at line 53 of file JavaScriptMinifier.php.

◆ TYPE_UN_OP

const JavaScriptMinifier::TYPE_UN_OP = 1

Definition at line 46 of file JavaScriptMinifier.php.


The documentation for this class was generated from the following file: