function test_escape_javascript() { $strings = array("\r" => '\\n', "\n" => '\\n', '\\' => '\\\\', '</' => '<\\/', '"' => '\\"', "'" => "\\'"); foreach ($strings as $string => $expect) { $this->assertEqual(JsHelper::escape_javascript($string), $expect); } }
/** * Returns the javascript needed for a remote function. * Takes the same arguments as 'link_to_remote()'. * * Example: * <select id="options" onchange="<?= * remote_function(array('update' => 'options', * 'url' => '@update_options')) ?>"> * <option value="0">Hello</option> * <option value="1">World</option> * </select> * * @param type <description> * * @return type <description> */ function remote_function($options) { $javascript_options = PrototypeHelper::options_for_ajax($options); $update = ''; if (isset($options['update']) && is_array($options['update'])) { $update = array(); if (isset($options['update']['success'])) { $update[] = "success:'" . $options['update']['success'] . "'"; } if (isset($options['update']['failure'])) { $update[] = "failure:'" . $options['update']['failure'] . "'"; } $update = '{' . join(',', $update) . '}'; } else { if (isset($options['update'])) { $update .= "'" . $options['update'] . "'"; } } $function = sprintf("new Ajax.%s(%s'%s', %s)", $update ? 'Updater' : 'Request', $update ? "{$update}, " : '', $options['url'], $javascript_options); if (isset($options['before'])) { $function = $options['before'] . '; ' . $function; } if (isset($options['after'])) { $function = $function . '; ' . $options['after']; } if (isset($options['condition'])) { $function = 'if (' . $options['condition'] . ') { ' . $function . '; }'; } if (isset($options['confirm'])) { $function = "if (confirm('" . JsHelper::escape_javascript($options['confirm']) . "')) { {$function}; }"; if (isset($options['cancel'])) { $function .= ' else { ' . $options['cancel'] . ' }'; } } return $function; }