Skip to content
Permalink
Browse files

Making the format attribute customizable in helpers.

  • Loading branch information...
jrbasso committed Dec 10, 2010
1 parent 0deaa6e commit d97103d7391502a65b8050b6163aee823aaadda1
Showing with 69 additions and 10 deletions.
  1. +31 −9 cake/libs/view/helper.php
  2. +38 −1 cake/tests/cases/libs/view/helper.test.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;
}
@@ -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)), '');
}
/**

0 comments on commit d97103d

Please sign in to comment.
You can’t perform that action at this time.