return json_decode($js);
}
+ /**
+ * Encodes a variable to js notation (not strict json).
+ *
+ * Like json_encode but the output is more suitable for an html attribute.
+ *
+ * @param $value
+ * @return string
+ */
+ public static function encode($value) {
+ if (is_array($value)) {
+ return self::writeObject($value, TRUE);
+ }
+ $result = json_encode($value, JSON_UNESCAPED_SLASHES);
+ // Prefer single quotes
+ if (is_string($value) && strpos($result, "'") === FALSE) {
+ return "'" . substr($result, 1, -1) . "'";
+ }
+ return $result;
+ }
+
/**
* Gets the properties of a javascript object/array WITHOUT decoding them.
*
$brackets = isset($obj[0]) && array_keys($obj) === range(0, count($obj) - 1) ? ['[', ']'] : ['{', '}'];
foreach ($obj as $key => $val) {
if ($encodeValues) {
- $val = json_encode($val, JSON_UNESCAPED_SLASHES);
+ $val = self::encode($val);
}
if ($brackets[0] == '{') {
// Enclose the key in quotes unless it is purely alphanumeric
$this->assertEquals($expectedOutput, CRM_Utils_JS::decode($input));
}
+ public static function encodeExamples() {
+ return [
+ [
+ ['a' => 'Apple', 'b' => 'Banana', 'c' => [1, 2, 3]],
+ "{a: 'Apple', b: 'Banana', c: [1, 2, 3]}",
+ ],
+ [
+ ['a' => ['foo', 'bar'], 'b' => ["'a'" => ['foo/bar', 'bar(foo)'], 'b' => ['a' => ["fo'oo", 'bar'], 'b' => []]]],
+ "{a: ['foo', 'bar'], b: {\"'a'\": ['foo/bar', 'bar(foo)'], b: {a: [\"fo'oo\", 'bar'], b: {}}}}",
+ ],
+ [TRUE, 'true'],
+ [' ', "' '"],
+ [FALSE, 'false'],
+ [NULL, 'null'],
+ ['true', "'true'"],
+ ['0.5', "'0.5'"],
+ [0.5, '0.5'],
+ [[], "{}"],
+ ];
+ }
+
+ /**
+ * @param string $input
+ * @param string $expectedOutput
+ * @dataProvider encodeExamples
+ */
+ public function testEncode($input, $expectedOutput) {
+ $this->assertEquals($expectedOutput, CRM_Utils_JS::encode($input));
+ }
+
/**
* @return array
*/