diff --git a/cake/libs/view/helper.php b/cake/libs/view/helper.php index 59470ea9f38..6397cf72238 100644 --- a/cake/libs/view/helper.php +++ b/cake/libs/view/helper.php @@ -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 * @@ -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); @@ -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; } diff --git a/cake/tests/cases/libs/view/helper.test.php b/cake/tests/cases/libs/view/helper.test.php index b30c2a76715..ba401fe6eba 100644 --- a/cake/tests/cases/libs/view/helper.test.php +++ b/cake/tests/cases/libs/view/helper.test.php @@ -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 { @@ -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)), ''); } /**