MediaWiki  1.33.0
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)
 Returns minified JavaScript code. More...
 
static parseError ( $fullJavascript, $position, $errorMsg)
 

Public Attributes

const ACTION_GOTO = 201
 
const ACTION_POP = 203
 
const ACTION_PUSH = 202
 
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 = 104
 
const TYPE_BIN_OP = 103
 
const TYPE_BRACE_CLOSE = 110
 
const TYPE_BRACE_OPEN = 109
 
const TYPE_COLON = 106
 
const TYPE_COMMA = 107
 
const TYPE_DO = 115
 
const TYPE_FUNC = 116
 
const TYPE_HOOK = 105
 
const TYPE_IF = 114
 
const TYPE_INCR_OP = 102
 
const TYPE_LITERAL = 117
 
const TYPE_PAREN_CLOSE = 112
 
const TYPE_PAREN_OPEN = 111
 
const TYPE_RETURN = 113
 
const TYPE_SEMICOLON = 108
 
const TYPE_UN_OP = 101
 

Static Private Attributes

static $maxLineLength = 1000
 Maximum line length. More...
 

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.

This class was written with ECMA-262 Edition 3 in mind ("ECMAScript 3"). Parsing features new to ECMAScript 5 or later might not be supported. However, Edition 5.1 better reflects how actual JS engines worked and work and is simpler and more readable prose. As such, the below code will refer to sections of the 5.1 specification.

See https://www.ecma-international.org/ecma-262/5.1/.

Definition at line 28 of file JavaScriptMinifier.php.

Member Function Documentation

◆ minify()

◆ parseError()

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

Definition at line 846 of file JavaScriptMinifier.php.

Referenced by minify().

Member Data Documentation

◆ $maxLineLength

JavaScriptMinifier::$maxLineLength = 1000
staticprivate

Maximum line length.

This is not a strict maximum, but a guideline. Longer lines will be produced when literals (e.g. quoted strings) longer than this are encountered, or when required to guard against semicolon insertion.

This is a private member (instead of constant) to allow tests to set it to 1, to verify ASI and line-breaking behaviour.

Definition at line 89 of file JavaScriptMinifier.php.

◆ ACTION_GOTO

const JavaScriptMinifier::ACTION_GOTO = 201

Definition at line 72 of file JavaScriptMinifier.php.

Referenced by minify().

◆ ACTION_POP

const JavaScriptMinifier::ACTION_POP = 203

Definition at line 74 of file JavaScriptMinifier.php.

◆ ACTION_PUSH

const JavaScriptMinifier::ACTION_PUSH = 202

Definition at line 73 of file JavaScriptMinifier.php.

Referenced by minify().

◆ CONDITION

const JavaScriptMinifier::CONDITION = 1

Definition at line 37 of file JavaScriptMinifier.php.

Referenced by minify().

◆ EXPRESSION

const JavaScriptMinifier::EXPRESSION = 3

Definition at line 39 of file JavaScriptMinifier.php.

Referenced by minify().

◆ EXPRESSION_FUNC

const JavaScriptMinifier::EXPRESSION_FUNC = 6

Definition at line 42 of file JavaScriptMinifier.php.

Referenced by minify().

◆ EXPRESSION_NO_NL

const JavaScriptMinifier::EXPRESSION_NO_NL = 4

Definition at line 40 of file JavaScriptMinifier.php.

Referenced by minify().

◆ EXPRESSION_OP

const JavaScriptMinifier::EXPRESSION_OP = 5

Definition at line 41 of file JavaScriptMinifier.php.

Referenced by minify().

◆ EXPRESSION_TERNARY

const JavaScriptMinifier::EXPRESSION_TERNARY = 7

Definition at line 43 of file JavaScriptMinifier.php.

Referenced by minify().

◆ EXPRESSION_TERNARY_FUNC

const JavaScriptMinifier::EXPRESSION_TERNARY_FUNC = 9

Definition at line 45 of file JavaScriptMinifier.php.

Referenced by minify().

◆ EXPRESSION_TERNARY_OP

const JavaScriptMinifier::EXPRESSION_TERNARY_OP = 8

Definition at line 44 of file JavaScriptMinifier.php.

Referenced by minify().

◆ PAREN_EXPRESSION

const JavaScriptMinifier::PAREN_EXPRESSION = 10

Definition at line 46 of file JavaScriptMinifier.php.

Referenced by minify().

◆ PAREN_EXPRESSION_FUNC

const JavaScriptMinifier::PAREN_EXPRESSION_FUNC = 12

Definition at line 48 of file JavaScriptMinifier.php.

Referenced by minify().

◆ PAREN_EXPRESSION_OP

const JavaScriptMinifier::PAREN_EXPRESSION_OP = 11

Definition at line 47 of file JavaScriptMinifier.php.

Referenced by minify().

◆ PROPERTY_ASSIGNMENT

const JavaScriptMinifier::PROPERTY_ASSIGNMENT = 2

Definition at line 38 of file JavaScriptMinifier.php.

Referenced by minify().

◆ PROPERTY_EXPRESSION

const JavaScriptMinifier::PROPERTY_EXPRESSION = 13

Definition at line 49 of file JavaScriptMinifier.php.

Referenced by minify().

◆ PROPERTY_EXPRESSION_FUNC

const JavaScriptMinifier::PROPERTY_EXPRESSION_FUNC = 15

Definition at line 51 of file JavaScriptMinifier.php.

Referenced by minify().

◆ PROPERTY_EXPRESSION_OP

const JavaScriptMinifier::PROPERTY_EXPRESSION_OP = 14

Definition at line 50 of file JavaScriptMinifier.php.

Referenced by minify().

◆ STACK_LIMIT

const JavaScriptMinifier::STACK_LIMIT = 1000

Definition at line 77 of file JavaScriptMinifier.php.

Referenced by minify().

◆ STATEMENT

const JavaScriptMinifier::STATEMENT = 0

Definition at line 36 of file JavaScriptMinifier.php.

Referenced by minify().

◆ TYPE_ADD_OP

const JavaScriptMinifier::TYPE_ADD_OP = 104

Definition at line 57 of file JavaScriptMinifier.php.

Referenced by minify().

◆ TYPE_BIN_OP

const JavaScriptMinifier::TYPE_BIN_OP = 103

Definition at line 56 of file JavaScriptMinifier.php.

Referenced by minify().

◆ TYPE_BRACE_CLOSE

const JavaScriptMinifier::TYPE_BRACE_CLOSE = 110

Definition at line 63 of file JavaScriptMinifier.php.

Referenced by minify().

◆ TYPE_BRACE_OPEN

const JavaScriptMinifier::TYPE_BRACE_OPEN = 109

Definition at line 62 of file JavaScriptMinifier.php.

Referenced by minify().

◆ TYPE_COLON

const JavaScriptMinifier::TYPE_COLON = 106

Definition at line 59 of file JavaScriptMinifier.php.

Referenced by minify().

◆ TYPE_COMMA

const JavaScriptMinifier::TYPE_COMMA = 107

Definition at line 60 of file JavaScriptMinifier.php.

Referenced by minify().

◆ TYPE_DO

const JavaScriptMinifier::TYPE_DO = 115

Definition at line 68 of file JavaScriptMinifier.php.

Referenced by minify().

◆ TYPE_FUNC

const JavaScriptMinifier::TYPE_FUNC = 116

Definition at line 69 of file JavaScriptMinifier.php.

Referenced by minify().

◆ TYPE_HOOK

const JavaScriptMinifier::TYPE_HOOK = 105

Definition at line 58 of file JavaScriptMinifier.php.

Referenced by minify().

◆ TYPE_IF

const JavaScriptMinifier::TYPE_IF = 114

Definition at line 67 of file JavaScriptMinifier.php.

Referenced by minify().

◆ TYPE_INCR_OP

const JavaScriptMinifier::TYPE_INCR_OP = 102

Definition at line 55 of file JavaScriptMinifier.php.

Referenced by minify().

◆ TYPE_LITERAL

const JavaScriptMinifier::TYPE_LITERAL = 117

Definition at line 70 of file JavaScriptMinifier.php.

Referenced by minify().

◆ TYPE_PAREN_CLOSE

const JavaScriptMinifier::TYPE_PAREN_CLOSE = 112

Definition at line 65 of file JavaScriptMinifier.php.

Referenced by minify().

◆ TYPE_PAREN_OPEN

const JavaScriptMinifier::TYPE_PAREN_OPEN = 111

Definition at line 64 of file JavaScriptMinifier.php.

Referenced by minify().

◆ TYPE_RETURN

const JavaScriptMinifier::TYPE_RETURN = 113

Definition at line 66 of file JavaScriptMinifier.php.

Referenced by minify().

◆ TYPE_SEMICOLON

const JavaScriptMinifier::TYPE_SEMICOLON = 108

Definition at line 61 of file JavaScriptMinifier.php.

Referenced by minify().

◆ TYPE_UN_OP

const JavaScriptMinifier::TYPE_UN_OP = 101

Definition at line 54 of file JavaScriptMinifier.php.

Referenced by minify().


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