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

Useful for handling function callbacks in options arrays.
This commit is contained in:
mark_story 2009-03-15 12:26:12 -04:00
parent ffdbec12fe
commit 72866966b0
4 changed files with 37 additions and 6 deletions

View file

@ -123,6 +123,12 @@ class JqueryEngineHelper extends JsBaseEngineHelper {
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 .'});';
}
}
?>

View file

@ -583,16 +583,17 @@ class JsBaseEngineHelper extends AppHelper {
* 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);
}

View file

@ -71,11 +71,11 @@ class JqueryEngineHelperTestCase extends CakeTestCase {
$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);

View file

@ -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 @@ class JsBaseEngineTestCase extends CakeTestCase {
$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);
}
}
?>