Skip to content

Commit

Permalink
Updating parseOptions to allow safeKeys that do not need to be escaped
Browse files Browse the repository at this point in the history
Useful for handling function callbacks in options arrays.
  • Loading branch information
markstory committed Mar 15, 2009
1 parent ffdbec1 commit 7286696
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
6 changes: 6 additions & 0 deletions cake/libs/view/helpers/jquery_engine.php
Expand Up @@ -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 .'});';
}
}
?>
9 changes: 5 additions & 4 deletions cake/libs/view/helpers/js.php
Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions cake/tests/cases/libs/view/helpers/jquery_engine.test.php
Expand Up @@ -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);
Expand Down
24 changes: 24 additions & 0 deletions cake/tests/cases/libs/view/helpers/js.test.php
Expand Up @@ -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);
}
}

/**
Expand Down Expand Up @@ -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.