Converts a PHP variable into its Javascript equivalent.

We use HTML-safe strings, i.e. with <, > and & escaped.

1 string reference to 'advagg_drupal_to_js'
advagg_process_js dans ./advagg.module
Returns a themed presentation of all JavaScript code for the current page.

Fichier

./advagg.module, line 2613

Code

function advagg_drupal_to_js($var) {
    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) {
        return json_encode($var, JSON_HEX_QUOT | JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS);
    }
    // if json_encode exists, use it.
    if (function_exists('json_encode')) {
        return str_replace(array(
            "<",
            ">",
            "&",
        ), array(
            '\\u003c',
            '\\u003e',
            '\\u0026',
        ), json_encode($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
            // sequences (\") to prevent browsers interpreting these as
            // special characters.
            $replace_pairs = array(
                // ", \ and U+0000 - U+001F must be escaped according to RFC 4627.
'\\' => '\\u005C',
                '"' => '\\u0022',
                "\x00" => '\\u0000',
                "\x01" => '\\u0001',
                "\x02" => '\\u0002',
                "\x03" => '\\u0003',
                "\x04" => '\\u0004',
                "\x05" => '\\u0005',
                "\x06" => '\\u0006',
                "\x07" => '\\u0007',
                "\x08" => '\\u0008',
                "\t" => '\\u0009',
                "\n" => '\\u000A',
                "\v" => '\\u000B',
                "\f" => '\\u000C',
                "\r" => '\\u000D',
                "\x0e" => '\\u000E',
                "\x0f" => '\\u000F',
                "\x10" => '\\u0010',
                "\x11" => '\\u0011',
                "\x12" => '\\u0012',
                "\x13" => '\\u0013',
                "\x14" => '\\u0014',
                "\x15" => '\\u0015',
                "\x16" => '\\u0016',
                "\x17" => '\\u0017',
                "\x18" => '\\u0018',
                "\x19" => '\\u0019',
                "\x1a" => '\\u001A',
                "\x1b" => '\\u001B',
                "\x1c" => '\\u001C',
                "\x1d" => '\\u001D',
                "\x1e" => '\\u001E',
                "\x1f" => '\\u001F',
                // Prevent browsers from interpreting these as as special.
"'" => '\\u0027',
                '<' => '\\u003C',
                '>' => '\\u003E',
                '&' => '\\u0026',
                // Prevent browsers from interpreting the solidus as special and
                // 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 parsers.
"
" => '\\u2028',
                // U+2028, Line Separator.
"
" => '\\u2029',
            );
            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)) {
                $output = array();
                foreach ($var as $v) {
                    $output[] = advagg_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[] = advagg_drupal_to_js(strval($k)) . ': ' . advagg_drupal_to_js($v);
            }
            return '{ ' . implode(', ', $output) . ' }';
        default:
            return 'null';
    }
}