? autocomplete.html ? content_type.php ? d_13.patch.txt ? d_17.patch.txt ? js.po ? patches ? temp ? test.php ? misc/autofocus.js ? misc/date.js ? misc/dependencies.js ? modules/tree2 ? modules/user/access_control.js ? sites/all/modules Index: includes/common.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/common.inc,v retrieving revision 1.619 diff -u -d -F^\s*function -r1.619 common.inc --- includes/common.inc 8 Mar 2007 19:33:55 -0000 1.619 +++ includes/common.inc 10 Mar 2007 12:31:38 -0000 @@ -1748,13 +1748,59 @@ function drupal_get_js($scope = 'header' * Converts a PHP variable into its Javascript equivalent. * * We use HTML-safe strings, i.e. with <, > and & escaped. + * + * @parameter $var + * The variable that should be converted to a JavaScript compatible variable. + * If you pass in the plain variable, the type of the variable is determined + * automatically. If you want to specify the type of the variable or passing + * a value that should be treated differently, you can use the advanced + * notation: + * + * array( + * '#type' => 'function', + * '#value' => 'function(x) { alert(x); }' + * ); + * + * array( + * '#type' => 'regex', + * '#value' => '/^(hello\\s+world)!?$/i', + * ); + * + * Allowed values for the type field are boolean, integer, double, function, + * regex, string, array and object. If you specify another type, it will be + * null. + * + * Note that arrays in JavaScript differ from those in PHP: JavaScript arrays + * can only have sequential numeric indices. If you don't specify manually + * that the PHP array should be a JavaScript array, it is converted to an + * object (which allows string indices) when the keys of the array are not + * sequential whole numbers starting with 0. + * @return + * The converted variable. */ function drupal_to_js($var) { - switch (gettype($var)) { + // Determine the type of the variable + if (isset($var['#type']) && isset($var['#value']) && count($var) == 2) { + $type = $var['#type']; + $var = $var['#value']; + } + else { + $type = gettype($var); + // Arrays in JSON can't be associative. If the array is not empty or if it + // doesn't have sequential whole number keys starting with 0, it's associative + // and we need to convert this into an object instead of an array. + if ($type == 'array' && (!empty($var) && array_keys($var) !== range(0, sizeof($var) - 1))) { + $type = 'object'; + } + } + + switch ($type) { case 'boolean': return $var ? 'true' : 'false'; // Lowercase necessary! case 'integer': case 'double': + case 'function': + case 'regex': return $var; case 'resource': case 'string': @@ -1762,17 +1808,11 @@ function drupal_to_js($var) { array('\r', '\n', '\x3c', '\x3e', '\x26'), addslashes($var)) .'"'; case 'array': - // Arrays in JSON can't be associative. If the array is empty or if it - // has sequential whole number keys starting with 0, it's not associative - // so we can go ahead and convert it as an array. - if (empty ($var) || array_keys($var) === range(0, sizeof($var) - 1)) { - $output = array(); - foreach ($var as $v) { - $output[] = drupal_to_js($v); - } - return '[ '. implode(', ', $output) .' ]'; + $output = array(); + foreach ($var as $v) { + $output[] = drupal_to_js($v); } - // Otherwise, fall through to convert the array as an object. + return '[ '. implode(', ', $output) .' ]'; case 'object': $output = array(); foreach ($var as $k => $v) {