summaryrefslogtreecommitdiff
path: root/includes/common.inc
diff options
context:
space:
mode:
authorDries Buytaert <dries@buytaert.net>2008-03-17 17:01:05 +0000
committerDries Buytaert <dries@buytaert.net>2008-03-17 17:01:05 +0000
commit064417f34b761db196018ad2b519e37af9d16142 (patch)
treeef93a33c6f3d5762f67c0a520285b6c4a8be252d /includes/common.inc
parent45097b78b7ff86e2d5ecd53d9926204d52c87955 (diff)
downloadbrdo-064417f34b761db196018ad2b519e37af9d16142.tar.gz
brdo-064417f34b761db196018ad2b519e37af9d16142.tar.bz2
- Patch #222578 by pwolanin: alter drupal_to_js() to use json_encode().
Diffstat (limited to 'includes/common.inc')
-rw-r--r--includes/common.inc34
1 files changed, 2 insertions, 32 deletions
diff --git a/includes/common.inc b/includes/common.inc
index a9596764c..2d809132b 100644
--- a/includes/common.inc
+++ b/includes/common.inc
@@ -2255,38 +2255,8 @@ function drupal_clear_js_cache() {
* We use HTML-safe strings, i.e. with <, > and & escaped.
*/
function drupal_to_js($var) {
- switch (gettype($var)) {
- case 'boolean':
- return $var ? 'true' : 'false'; // Lowercase necessary!
- case 'integer':
- case 'double':
- return $var;
- case 'resource':
- case 'string':
- return '"'. str_replace(array("\r", "\n", "<", ">", "&"),
- 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) .' ]';
- }
- // Otherwise, fall through to convert the array as an object.
- case 'object':
- $output = array();
- foreach ($var as $k => $v) {
- $output[] = drupal_to_js(strval($k)) .': '. drupal_to_js($v);
- }
- return '{ '. implode(', ', $output) .' }';
- default:
- return 'null';
- }
+ // json_encode() does not escape <, > and &, so we do it with str_replace()
+ return str_replace(array("<", ">", "&"), array('\x3c', '\x3e', '\x26'), json_encode($var));
}
/**