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).

Details

at line 43
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 81
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 124
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