Index: includes/common.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/common.inc,v retrieving revision 1.1074 diff -u -p -r1.1074 common.inc --- includes/common.inc 3 Jan 2010 11:04:58 -0000 1.1074 +++ includes/common.inc 3 Jan 2010 21:32:40 -0000 @@ -4373,7 +4373,7 @@ function drupal_clear_js_cache() { */ function drupal_json_encode($var) { // json_encode() does not escape <, > and &, so we do it with str_replace(). - return str_replace(array('<', '>', '&'), array('\x3c', '\x3e', '\x26'), json_encode($var)); + return str_replace(array('<', '>', '&'), array('\u003c', '\u003e', '\u0026'), json_encode($var)); } /** @@ -4382,8 +4382,7 @@ function drupal_json_encode($var) { * @see drupal_json_encode() */ function drupal_json_decode($var) { - // json_decode() does not unescape <, > and &, so we do it with str_replace(). - return json_decode(str_replace(array('\x3c', '\x3e', '\x26'), array('<', '>', '&'), $var), TRUE); + return json_decode($var, TRUE); } /** Index: modules/simpletest/tests/common.test =================================================================== RCS file: /cvs/drupal/drupal/modules/simpletest/tests/common.test,v retrieving revision 1.97 diff -u -p -r1.97 common.test --- modules/simpletest/tests/common.test 15 Dec 2009 05:22:05 -0000 1.97 +++ modules/simpletest/tests/common.test 3 Jan 2010 21:25:38 -0000 @@ -1741,6 +1741,7 @@ class DrupalJSONTest extends DrupalUnitT } // Characters that must be escaped. $html_unsafe = array('<', '>', '&'); + $html_unsafe_escaped = array('\u003c', '\u003e', '\u0026'); // Verify there aren't character encoding problems with the source string. $this->assertIdentical(strlen($str), 128, t('A string with the full ASCII table has the correct length.')); @@ -1763,6 +1764,10 @@ class DrupalJSONTest extends DrupalUnitT foreach ($html_unsafe as $char) { $this->assertTrue(strpos($json, $char) === FALSE, t('A JSON encoded string does not contain @s.', array('@s' => $char))); } + // Verify that JSON encoding escapes the HTML unsafe characters + foreach ($html_unsafe_escaped as $char) { + $this->assertTrue(strpos($json, $char) > 0, t('A JSON encoded string contains @s.', array('@s' => $char))); + } $json_decoded = drupal_json_decode($json); $this->assertNotIdentical($source, $json, t('An array encoded in JSON is not identical to the source.')); $this->assertIdentical($source, $json_decoded, t('Encoding structured data to JSON and decoding back results in the original data.'));