Permalink
Browse files

Updating parseOptions to allow safeKeys that do not need to be escaped

Useful for handling function callbacks in options arrays.
  • Loading branch information...
markstory committed Mar 15, 2009
1 parent ffdbec1 commit 72866966b0ffa4f072aa564cc59e07238c1ebbce
@@ -123,6 +123,12 @@ function effect($name, $options = array()) {
function request($url, $options = array()) {
$url = $this->url($url);
$options = $this->_mapOptions('request', $options);
+ if (isset($options['data']) && is_array($options['data'])) {
+ //handle data array to query string.
+ }
+ $options['url'] = $url;
+ $options = $this->_parseOptions($options);
+ return '$.ajax({' . $options .'});';
}
}
?>
@@ -583,16 +583,17 @@ function request($url, $options = array()) {
* does not include { }
*
* @param array $options Options to be converted
+ * @param array $safeKeys Keys that should not be escaped.
* @return string
* @access protected
**/
- function _parseOptions($options) {
+ function _parseOptions($options, $safeKeys = array()) {
$out = array();
foreach ($options as $key => $value) {
- if (!is_int($val)) {
- $val = '"' . $val . '"';
+ if (!is_int($value) && !in_array($key, $safeKeys)) {
+ $value = '"' . $this->escape($value) . '"';
}
- $out[] = $key . ':' . $val;
+ $out[] = $key . ':' . $value;
}
return join(', ', $out);
}
@@ -71,11 +71,11 @@ function testEvent() {
$result = $this->Jquery->get('#myLink')->event('click', 'doClick', array('wrap' => false));
$expected = "$('#myLink').bind('click', doClick);";
$this->assertEqual($result, $expected);
-
+
$result = $this->Jquery->get('#myLink')->event('click', '$(this).show();', array('stop' => false));
$expected = "$('#myLink').bind('click', function (event) {\$(this).show();});";
$this->assertEqual($result, $expected);
-
+
$result = $this->Jquery->get('#myLink')->event('click', '$(this).hide();');
$expected = "\$('#myLink').bind('click', function (event) {\$(this).hide();\nreturn false;});";
$this->assertEqual($result, $expected);
@@ -46,6 +46,14 @@ class OptionEngineHelper extends JsBaseEngineHelper {
function testMap($options = array()) {
return $this->_mapOptions('request', $options);
}
+/**
+ * test method for option parsing
+ *
+ * @return void
+ **/
+ function testParseOptions($options, $safe = array()) {
+ return $this->_parseOptions($options, $safe);
+ }
}
/**
@@ -319,6 +327,22 @@ function testOptionMapping() {
$result = $JsEngine->testMap(array('complete' => 'myFunc', 'type' => 'json', 'update' => '#element'));
$this->assertEqual($result, array('success' => 'myFunc', 'dataType' => 'json', 'update' => '#element'));
}
+/**
+ * test that option parsing escapes strings and saves what is supposed to be saved.
+ *
+ * @return void
+ **/
+ function testOptionParsing() {
+ $JsEngine = new OptionEngineHelper();
+
+ $result = $JsEngine->testParseOptions(array('url' => '/posts/view/1', 'key' => 1));
+ $expected = 'url:"/posts/view/1", key:1';
+ $this->assertEqual($result, $expected);
+
+ $result = $JsEngine->testParseOptions(array('url' => '/posts/view/1', 'success' => 'doSuccess'), array('success'));
+ $expected = 'url:"/posts/view/1", success:doSuccess';
+ $this->assertEqual($result, $expected);
+ }
}
?>

0 comments on commit 7286696

Please sign in to comment.