Making the format attribute customizable in helpers.

This commit is contained in:
Juan Basso 2010-12-10 02:42:38 -02:00
parent 0deaa6eee0
commit d97103d739
2 changed files with 69 additions and 10 deletions

View file

@ -107,6 +107,30 @@ class Helper extends Object {
*/ */
protected $_View; 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 * Default Constructor
* *
@ -361,7 +385,7 @@ class Helper extends Object {
foreach ($filtered as $key => $value) { foreach ($filtered as $key => $value) {
if ($value !== false && $value !== null) { if ($value !== false && $value !== null) {
$attributes[] = $this->__formatAttribute($key, $value, $escape); $attributes[] = $this->_formatAttribute($key, $value, $escape);
} }
} }
$out = implode(' ', $attributes); $out = implode(' ', $attributes);
@ -378,23 +402,21 @@ class Helper extends Object {
* @param string $key The name of the attribute to create * @param string $key The name of the attribute to create
* @param string $value The value of the attribute to create. * @param string $value The value of the attribute to create.
* @return string The composed attribute. * @return string The composed attribute.
* @access private
*/ */
function __formatAttribute($key, $value, $escape = true) { protected function _formatAttribute($key, $value, $escape = true) {
$attribute = ''; $attribute = '';
$attributeFormat = '%s="%s"';
$minimizedAttributes = array('compact', 'checked', 'declare', 'readonly', 'disabled',
'selected', 'defer', 'ismap', 'nohref', 'noshade', 'nowrap', 'multiple', 'noresize');
if (is_array($value)) { if (is_array($value)) {
$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) { if ($value === 1 || $value === true || $value === 'true' || $value === '1' || $value == $key) {
$attribute = sprintf($attributeFormat, $key, $key); $attribute = sprintf($this->_minimizedAttributeFormat, $key, $key);
} }
} else { } else {
$attribute = sprintf($attributeFormat, $key, ($escape ? h($value) : $value)); $attribute = sprintf($this->_attributeFormat, $key, ($escape ? h($value) : $value));
} }
return $attribute; return $attribute;
} }

View file

@ -184,11 +184,41 @@ class TestHelper extends Helper {
} }
} }
/**
* 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 * HelperTest class
* *
* @package cake * @package cake
* @subpackage cake.tests.cases.libs * @subpackage cake.tests.cases.libs.view
*/ */
class HelperTest extends CakeTestCase { class HelperTest extends CakeTestCase {
@ -809,6 +839,13 @@ class HelperTest extends CakeTestCase {
$this->assertEqual($helper->parseAttributes($attrs), $expected, '%s Failed on ' . $value); $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)), '');
} }
/** /**