mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-18 18:46:17 +00:00
Making the format attribute customizable in helpers.
This commit is contained in:
parent
0deaa6eee0
commit
d97103d739
2 changed files with 69 additions and 10 deletions
|
@ -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 @@ class Helper extends Object {
|
|||
|
||||
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 @@ class Helper extends Object {
|
|||
* @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 @@ 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
|
||||
*
|
||||
* @package cake
|
||||
* @subpackage cake.tests.cases.libs
|
||||
* @subpackage cake.tests.cases.libs.view
|
||||
*/
|
||||
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(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)), '');
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Reference in a new issue