class CRM_Utils_JS

Parse Javascript content and extract translatable strings.

Methods

static array
parseStrings(string $jsCode)

Parse a javascript file for translatable strings.

static string
dedupeClosures(array $scripts, array $localVars, array $inputVals)

Identify duplicate, adjacent, identical closures and consolidate them.

static string
stripComments(string $script)

This is a primitive comment stripper. It doesn't catch all comments and falls short of minification, but it doesn't munge Angular injections and is fast enough to run synchronously (without caching).

static mixed
decode(string $js)

Decodes a js variable (not necessarily strict json but valid js) into a php variable.

static string
encode(mixed $value)

Encodes a variable to js notation (not strict json) suitable for e.g. an angular attribute.

static array
getRawProps($js)

Gets the properties of a javascript object/array WITHOUT decoding them.

static string
writeObject(array $obj, bool $encodeValues = FALSE)

Converts a php array to javascript object/array notation (not strict JSON).

Details

at line 44
static array parseStrings(string $jsCode)

Parse a javascript file for translatable strings.

Parameters

string $jsCode Raw Javascript code.

Return Value

array Array of translatable strings

at line 82
static string dedupeClosures(array $scripts, array $localVars, array $inputVals)

Identify duplicate, adjacent, identical closures and consolidate them.

Note that you can only dedupe closures if they are directly adjacent and have exactly the same parameters.

Parameters

array $scripts Javascript source.
array $localVars Ordered list of JS vars to identify the start of a closure.
array $inputVals Ordered list of input values passed into the closure.

Return Value

string Javascript source.

at line 125
static string stripComments(string $script)

This is a primitive comment stripper. It doesn't catch all comments and falls short of minification, but it doesn't munge Angular injections and is fast enough to run synchronously (without caching).

At time of writing, running this against the Angular modules, this impl of stripComments currently adds 10-20ms and cuts ~7%.

Please be extremely cautious about extending this. If you want better minification, you should probably remove this implementation, import a proper JSMin implementation, and cache its output.

Parameters

string $script

Return Value

string

at line 144
static mixed decode(string $js)

Decodes a js variable (not necessarily strict json but valid js) into a php variable.

This is similar to using json_decode($js, TRUE) but more forgiving about syntax.

ex. {a: 'Apple', 'b': "Banana", c: [1, 2, 3]} Returns: [ 'a' => 'Apple', 'b' => 'Banana', 'c' => [1, 2, 3], ]

Parameters

string $js

Return Value

mixed

at line 179
static string encode(mixed $value)

Encodes a variable to js notation (not strict json) suitable for e.g. an angular attribute.

Like json_encode() but the output looks more like native javascript, with single quotes around strings and no unnecessary quotes around object keys.

Ex input: [ 'a' => 'Apple', 'b' => 'Banana', 'c' => [1, 2, 3], ] Ex output: {a: 'Apple', b: 'Banana', c: [1, 2, 3]}

Parameters

mixed $value

Return Value

string

at line 208
static array getRawProps($js)

Gets the properties of a javascript object/array WITHOUT decoding them.

Useful when the object might contain js functions, expressions, etc. which cannot be decoded. Returns an array with keys as property names and values as raw strings of js.

Ex Input: {foo: getFoo(arg), 'bar': function() {return "bar";}} Returns: [ 'foo' => 'getFoo(arg)', 'bar' => 'function() {return "bar";}', ]

Parameters

$js

Return Value

array

Exceptions

Exception

at line 286
static string writeObject(array $obj, bool $encodeValues = FALSE)

Converts a php array to javascript object/array notation (not strict JSON).

Does not encode keys unless they contain special characters. Does not encode values by default, so either specify $encodeValues = TRUE, or pass strings of valid js/json as values (per output from getRawProps).

Parameters

array $obj
bool $encodeValues

Return Value

string

See also

CRM_Utils_JS::getRawProps