Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Improvments to getJSObject #1566

Merged
merged 2 commits into from

2 participants

@okonomiyaki3000

Resources are ignored
Object keys and strings are encoded with json_encode
If the input array contains (php) objects, their values are used for recursion rather than the objects themselves (the function takes an array, not an object)
Less string concatenation, more array implosion

okonomiyaki3000 added some commits
@okonomiyaki3000 okonomiyaki3000 Improvments to getJSObject
	Resources are ignored
	Object keys and strings are encoded with json_encode
	If the input array contains (php) objects, their values are used for recursion rather than the objects themselves (the function takes an array, not an object)
	Less string concatenation, more array implosion
0d9e920
@okonomiyaki3000 okonomiyaki3000 remove trailing whitespace 2771572
@LouisLandry

Nice. Thanks!

@LouisLandry LouisLandry merged commit f1dfc46 into joomla:staging
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 5, 2012
  1. @okonomiyaki3000

    Improvments to getJSObject

    okonomiyaki3000 authored
    	Resources are ignored
    	Object keys and strings are encoded with json_encode
    	If the input array contains (php) objects, their values are used for recursion rather than the objects themselves (the function takes an array, not an object)
    	Less string concatenation, more array implosion
  2. @okonomiyaki3000
This page is out of date. Refresh to see the latest.
Showing with 25 additions and 20 deletions.
  1. +25 −20 libraries/joomla/html/html.php
View
45 libraries/joomla/html/html.php
@@ -930,41 +930,46 @@ public static function addIncludePath($path = '')
*/
public static function getJSObject(array $array = array())
{
- $object = '{';
-
- // Iterate over array to build objects
- foreach ((array) $array as $k => $v)
+ $elements = array();
+ foreach ($array as $k => $v)
{
- if (is_null($v))
+ // Don't encode either of these types
+ if (is_null($v) || is_resource($v))
{
continue;
}
+ // Safely encode as a Javascript string
+ $key = json_encode((string) $k);
+
if (is_bool($v))
{
- $object .= ' ' . $k . ': ';
- $object .= ($v) ? 'true' : 'false';
- $object .= ',';
+ $elements[] = $key . ': ' . ($v ? 'true' : 'false');
}
- elseif (!is_array($v) && !is_object($v))
+ elseif (is_numeric($v))
{
- $object .= ' ' . $k . ': ';
- $object .= (is_numeric($v) || strpos($v, '\\') === 0) ? (is_numeric($v)) ? $v : substr($v, 1) : "'" . $v . "'";
- $object .= ',';
+ $elements[] = $key . ': ' . ($v + 0);
+ }
+ elseif (is_string($v))
+ {
+ if (strpos($v, '\\') === 0)
+ {
+ // Items such as functions and JSON objects are prefixed with \, strip the prefix and don't encode them
+ $elements[] = $key . ': ' . substr($v, 1);
+ }
+ else
+ {
+ // The safest way to insert a string
+ $elements[] = $key . ': ' . json_encode((string) $v);
+ }
}
else
{
- $object .= ' ' . $k . ': ' . self::getJSObject($v) . ',';
+ $elements[] = $key . ': ' . self::getJSObject(is_object($v) ? get_object_vars($v) : $v);
}
}
- if (substr($object, -1) == ',')
- {
- $object = substr($object, 0, -1);
- }
-
- $object .= '}';
+ return '{' . implode(',', $elements) . '}';
- return $object;
}
}
Something went wrong with that request. Please try again.