Skip to content

Commit

Permalink
Making the format attribute customizable in helpers.
Browse files Browse the repository at this point in the history
  • Loading branch information
jrbasso committed Dec 10, 2010
1 parent 0deaa6e commit d97103d
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 10 deletions.
40 changes: 31 additions & 9 deletions cake/libs/view/helper.php
Expand Up @@ -107,6 +107,30 @@ class Helper extends Object {
*/
protected $_View;

/**
* Minimized attributes
*
* @var array
*/
protected $_minimizedAttributes = array(
'compact', 'checked', 'declare', 'readonly', 'disabled', 'selected',
'defer', 'ismap', 'nohref', 'noshade', 'nowrap', 'multiple', 'noresize'
);

/**
* Format to attribute
*
* @var string
*/
protected $_attributeFormat = '%s="%s"';

/**
* Format to attribute
*
* @var string
*/
protected $_minimizedAttributeFormat = '%s="%s"';

/**
* Default Constructor
*
Expand Down Expand Up @@ -361,7 +385,7 @@ public function _parseAttributes($options, $exclude = null, $insertBefore = ' ',

foreach ($filtered as $key => $value) {
if ($value !== false && $value !== null) {
$attributes[] = $this->__formatAttribute($key, $value, $escape);
$attributes[] = $this->_formatAttribute($key, $value, $escape);
}
}
$out = implode(' ', $attributes);
Expand All @@ -378,23 +402,21 @@ public function _parseAttributes($options, $exclude = null, $insertBefore = ' ',
* @param string $key The name of the attribute to create
* @param string $value The value of the attribute to create.
* @return string The composed attribute.
* @access private
*/
function __formatAttribute($key, $value, $escape = true) {
protected function _formatAttribute($key, $value, $escape = true) {
$attribute = '';
$attributeFormat = '%s="%s"';
$minimizedAttributes = array('compact', 'checked', 'declare', 'readonly', 'disabled',
'selected', 'defer', 'ismap', 'nohref', 'noshade', 'nowrap', 'multiple', 'noresize');
if (is_array($value)) {
$value = '';
}

if (in_array($key, $minimizedAttributes)) {
if (is_numeric($key)) {
$attribute = sprintf($this->_minimizedAttributeFormat, $value, $value);
} elseif (in_array($key, $this->_minimizedAttributes)) {
if ($value === 1 || $value === true || $value === 'true' || $value === '1' || $value == $key) {
$attribute = sprintf($attributeFormat, $key, $key);
$attribute = sprintf($this->_minimizedAttributeFormat, $key, $key);
}
} else {
$attribute = sprintf($attributeFormat, $key, ($escape ? h($value) : $value));
$attribute = sprintf($this->_attributeFormat, $key, ($escape ? h($value) : $value));
}
return $attribute;
}
Expand Down
39 changes: 38 additions & 1 deletion cake/tests/cases/libs/view/helper.test.php
Expand Up @@ -184,11 +184,41 @@ function parseAttributes($options, $exclude = null, $insertBefore = ' ', $insert
}
}

/**
* Html5TestHelper class
*
* @package cake
* @subpackage cake.tests.cases.libs.view
*/
class Html5TestHelper extends TestHelper {

/**
* Minimized
*
* @var array
*/
protected $_minimizedAttributes = array('require', 'checked');

/**
* Allow compact use in HTML
*
* @var string
*/
protected $_minimizedAttributeFormat = '%s';

/**
* Test to attribute format
*
* @var string
*/
protected $_attributeFormat = 'data-%s="%s"';
}

/**
* HelperTest class
*
* @package cake
* @subpackage cake.tests.cases.libs
* @subpackage cake.tests.cases.libs.view
*/
class HelperTest extends CakeTestCase {

Expand Down Expand Up @@ -809,6 +839,13 @@ function testParseAttributeCompact() {
$this->assertEqual($helper->parseAttributes($attrs), $expected, '%s Failed on ' . $value);
}
}
$this->assertEqual($helper->parseAttributes(array('compact')), ' compact="compact"');

$helper = new Html5TestHelper($this->View);
$expected = ' require';
$this->assertEqual($helper->parseAttributes(array('require')), $expected);
$this->assertEqual($helper->parseAttributes(array('require' => true)), $expected);
$this->assertEqual($helper->parseAttributes(array('require' => false)), '');
}

/**
Expand Down

0 comments on commit d97103d

Please sign in to comment.