CRM_Utils_JS
class CRM_Utils_JS
Parse Javascript content and extract translatable strings.
Methods
Parse a javascript file for translatable strings.
Identify duplicate, adjacent, identical closures and consolidate them.
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).
Decodes a js variable (not necessarily strict json but valid js) into a php variable.
Encodes a variable to js notation (not strict json) suitable for e.g. an angular attribute.
Gets the properties of a javascript object/array WITHOUT decoding them.
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.
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.
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.
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], ]
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]}
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";}', ]
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).