Adding possibility to specify if option values should be escaped, and adding tests for it

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5019 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
mariano.iglesias 2007-05-05 00:34:56 +00:00
parent d4bd5e5dbc
commit 22adcb07a1
2 changed files with 40 additions and 4 deletions

View file

@ -751,6 +751,13 @@ class FormHelper extends AppHelper {
*/
function select($fieldName, $options = array(), $selected = null, $attributes = array(), $showEmpty = '') {
$showParents = false;
$escapeOptions = true;
if (isset($attributes['escape'])) {
$escapeOptions = $attributes['escape'];
unset($attributes['escape']);
}
$this->setFormTag($fieldName);
$this->__secure();
$attributes = $this->domId((array)$attributes);
@ -790,7 +797,7 @@ class FormHelper extends AppHelper {
$options[''] = $showEmpty;
$options = array_reverse($options, true);
}
$select = am($select, $this->__selectOptions(array_reverse($options, true), $selected, array(), $showParents));
$select = am($select, $this->__selectOptions(array_reverse($options, true), $selected, array(), $showParents, array('escape' => $escapeOptions)));
$select[] = sprintf($this->Html->tags['selectend']);
return $this->output(implode("\n", $select));
}
@ -1033,7 +1040,9 @@ class FormHelper extends AppHelper {
*
* @return array
*/
function __selectOptions($elements = array(), $selected = null, $parents = array(), $showParents = null) {
function __selectOptions($elements = array(), $selected = null, $parents = array(), $showParents = null, $attributes = array()) {
$attributes = am(array('escape' => true), $attributes);
$select = array();
foreach($elements as $name => $title) {
$htmlOptions = array();
@ -1042,7 +1051,7 @@ class FormHelper extends AppHelper {
$select[] = $this->Html->tags['optiongroupend'];
$parents[] = $name;
}
$select = am($select, $this->__selectOptions($title, $selected, $parents, $showParents));
$select = am($select, $this->__selectOptions($title, $selected, $parents, $showParents, $attributes));
if (!empty($name)) {
$select[] = sprintf($this->Html->tags['optiongroup'], $name, '');
}
@ -1061,7 +1070,8 @@ class FormHelper extends AppHelper {
}
if($showParents || (!in_array($title, $parents))) {
$select[] = sprintf($this->Html->tags['selectoption'], $name, $this->Html->_parseAttributes($htmlOptions), h($title));
$title = ife($attributes['escape'], h($title), $title);
$select[] = sprintf($this->Html->tags['selectoption'], $name, $this->Html->_parseAttributes($htmlOptions), $title);
}
}
}

View file

@ -537,6 +537,32 @@ class FormHelperTest extends CakeTestCase {
$this->assertNoPattern('/option value="other"\s+selected="selected"/', $result);
$this->assertNoPattern('/<select[^<>]+[^name|id]=[^<>]*>/', $result);
$this->assertNoPattern('/<option[^<>]+[^value|selected]=[^<>]*>/', $result);
$this->Form->data = array();
$result = $this->Form->select('Model/field', array('value' => 'good', 'other' => 'bad'));
$this->assertPattern('/option value=""/', $result);
$this->assertPattern('/option value="value"/', $result);
$this->assertPattern('/option value="other"/', $result);
$this->assertPattern('/<\/option>\s+<option/', $result);
$this->assertPattern('/<\/option>\s+<\/select>/', $result);
$this->assertNoPattern('/option value="field"\s+selected="selected"/', $result);
$this->assertNoPattern('/option value="other"\s+selected="selected"/', $result);
$result = $this->Form->select('Model/field', array('first' => 'first "html" <chars>', 'second' => 'value'), null, array(), false);
$this->assertPattern('/' .
'<select[^>]*>\s+' .
'<option\s+value="first"[^>]*>first &quot;html&quot; &lt;chars&gt;<\/option>\s+'.
'<option\s+value="second"[^>]*>value<\/option>\s+'.
'<\/select>'.
'/i', $result);
$result = $this->Form->select('Model/field', array('first' => 'first "html" <chars>', 'second' => 'value'), null, array('escape' => false), false);
$this->assertPattern('/' .
'<select[^>]*>\s+' .
'<option\s+value="first"[^>]*>first "html" <chars><\/option>\s+'.
'<option\s+value="second"[^>]*>value<\/option>\s+'.
'<\/select>'.
'/i', $result);
}
function testDaySelect() {