diff --git a/includes/common.inc b/includes/common.inc index 5c645c7..534bf3b 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -4805,21 +4805,20 @@ function drupal_clear_js_cache() { * @ingroup php_wrappers */ function drupal_json_encode($var) { - // We do not want to use drupal_static() since the PHP version will never - // change during a request. + // PHP version cannot change within a request. static $php530; if (!isset($php530)) { $php530 = version_compare(PHP_VERSION, '5.3.0', '>='); } - // json_encode on PHP prior to PHP 5.3.0 doesn't support options. if ($php530) { - // json_encode() escapes <, >, ', &, and " using its options parameter. + // Encode <, >, ', &, and " using the json_encode() options parameter. return json_encode($var, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT); } - // In PHP versions less than 5.3.0, use our helper. + // json_encode() escapes <, >, ', &, and " using its options parameter, but + // does not support this parameter prior to PHP 5.3.0. Use a helper instead. return drupal_json_encode_helper($var); } @@ -4832,9 +4831,11 @@ function drupal_json_encode_helper($var) { switch (gettype($var)) { case 'boolean': return $var ? 'true' : 'false'; // Lowercase necessary! + case 'integer': case 'double': return $var; + case 'resource': case 'string': // Always use Unicode escape sequences (\u0022) over JSON escape @@ -4885,30 +4886,33 @@ function drupal_json_encode_helper($var) { // non-compliant JSON parsers from interpreting // as a comment. '/' => '\u002F', // While these are allowed unescaped according to ECMA-262, section - // 15.12.2, they cause problems in some JSON parser. + // 15.12.2, they cause problems in some JSON parsers. "\xe2\x80\xa8" => '\u2028', // U+2028, Line Separator. "\xe2\x80\xa9" => '\u2029', // U+2029, Paragraph Separator. ); - return '"'. strtr($var, $replace_pairs) .'"'; + return '"' . strtr($var, $replace_pairs) . '"'; + 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)) { + if (empty($var) || array_keys($var) === range(0, sizeof($var) - 1)) { $output = array(); foreach ($var as $v) { $output[] = drupal_json_encode_helper($v); } - return '[ '. implode(', ', $output) .' ]'; + return '[ ' . implode(', ', $output) . ' ]'; } // Otherwise, fall through to convert the array as an object. + case 'object': $output = array(); foreach ($var as $k => $v) { $output[] = drupal_json_encode_helper(strval($k)) . ':' . drupal_json_encode_helper($v); } - return '{'. implode(', ', $output) .'}'; + return '{' . implode(', ', $output) . '}'; + default: return 'null'; } diff --git a/modules/taxonomy/taxonomy.test b/modules/taxonomy/taxonomy.test index 4e9eb2c..597bfc3 100644 --- a/modules/taxonomy/taxonomy.test +++ b/modules/taxonomy/taxonomy.test @@ -627,8 +627,8 @@ class TaxonomyTermTestCase extends TaxonomyWebTestCase { } $this->assertNoText($term1->name, t('The deleted term %name does not appear on the node page.', array('%name' => $term1->name))); - // Test autocomplete on term 2 - it contains a comma, so expect the key to - // be quoted. + // Test autocomplete on term 2, which contains a comma. + // The term will be quoted, and the " will be encoded in unicode (\u0022). $input = substr($term2->name, 0, 3); $this->drupalGet('taxonomy/autocomplete/taxonomy_' . $this->vocabulary->machine_name . '/' . $input); $this->assertRaw('{"\u0022' . $term2->name . '\u0022":"' . $term2->name . '"}', t('Autocomplete returns term %term_name after typing the first 3 letters.', array('%term_name' => $term2->name)));