Skip to content

Commit

Permalink
Moving the parseAttributes to Html helper.
Browse files Browse the repository at this point in the history
  • Loading branch information
jrbasso committed Jan 23, 2011
1 parent 5bc0f0c commit 10d3dd5
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 167 deletions.
112 changes: 0 additions & 112 deletions cake/libs/view/helper.php
Expand Up @@ -105,30 +105,6 @@ class Helper extends Object {
*/
protected $_View;

This comment has been minimized.

Copy link
@hiromi2424

hiromi2424 Oct 12, 2011

Why this deletion does not effect to "Helper" now?
It seems commit history of this was lost and remains the codes of parseAttributes() in the (renamed)file.

This comment has been minimized.

Copy link
@jrbasso

jrbasso Oct 12, 2011

Author Member

It was lost in some merge with 1.3. We just got it in the release candidate step and now these methods are marked as deprecated in Helper and will be removed in 2.1+

/**
* 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 @@ -330,94 +306,6 @@ public function clean($output) {
return $this->__cleaned;
}

/**
* Returns a space-delimited string with items of the $options array. If a
* key of $options array happens to be one of:
*
* - 'compact'
* - 'checked'
* - 'declare'
* - 'readonly'
* - 'disabled'
* - 'selected'
* - 'defer'
* - 'ismap'
* - 'nohref'
* - 'noshade'
* - 'nowrap'
* - 'multiple'
* - 'noresize'
*
* And its value is one of:
*
* - '1' (string)
* - 1 (integer)
* - true (boolean)
* - 'true' (string)
*
* Then the value will be reset to be identical with key's name.
* If the value is not one of these 3, the parameter is not output.
*
* 'escape' is a special option in that it controls the conversion of
* attributes to their html-entity encoded equivalents. Set to false to disable html-encoding.
*
* If value for any option key is set to `null` or `false`, that option will be excluded from output.
*
* @param array $options Array of options.
* @param array $exclude Array of options to be excluded, the options here will not be part of the return.
* @param string $insertBefore String to be inserted before options.
* @param string $insertAfter String to be inserted after options.
* @return string Composed attributes.
*/
public function _parseAttributes($options, $exclude = null, $insertBefore = ' ', $insertAfter = null) {
if (is_array($options)) {
$options = array_merge(array('escape' => true), $options);

if (!is_array($exclude)) {
$exclude = array();
}
$filtered = array_diff_key($options, array_merge(array_flip($exclude), array('escape' => true)));
$escape = $options['escape'];
$attributes = array();

foreach ($filtered as $key => $value) {
if ($value !== false && $value !== null) {
$attributes[] = $this->_formatAttribute($key, $value, $escape);
}
}
$out = implode(' ', $attributes);
} else {
$out = $options;
}
return $out ? $insertBefore . $out . $insertAfter : '';
}

/**
* Formats an individual attribute, and returns the string value of the composed attribute.
* Works with minimized attributes that have the same value as their name such as 'disabled' and 'checked'
*
* @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.
*/
protected function _formatAttribute($key, $value, $escape = true) {
$attribute = '';
if (is_array($value)) {
$value = '';
}

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($this->_minimizedAttributeFormat, $key, $key);
}
} else {
$attribute = sprintf($this->_attributeFormat, $key, ($escape ? h($value) : $value));
}
return $attribute;
}

/**
* Sets this helper's model and field properties to the dot-separated value-pair in $entity.
*
Expand Down
113 changes: 113 additions & 0 deletions cake/libs/view/helpers/html.php
Expand Up @@ -90,6 +90,30 @@ class HtmlHelper extends AppHelper {
'javascriptend' => '</script>'
);

/**
* 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"';

/**
* Breadcrumbs.
*
Expand Down Expand Up @@ -881,4 +905,93 @@ function __nestedListItem($items, $options, $itemOptions, $tag) {
}
return $out;
}

/**
* Returns a space-delimited string with items of the $options array. If a
* key of $options array happens to be one of:
*
* - 'compact'
* - 'checked'
* - 'declare'
* - 'readonly'
* - 'disabled'
* - 'selected'
* - 'defer'
* - 'ismap'
* - 'nohref'
* - 'noshade'
* - 'nowrap'
* - 'multiple'
* - 'noresize'
*
* And its value is one of:
*
* - '1' (string)
* - 1 (integer)
* - true (boolean)
* - 'true' (string)
*
* Then the value will be reset to be identical with key's name.
* If the value is not one of these 3, the parameter is not output.
*
* 'escape' is a special option in that it controls the conversion of
* attributes to their html-entity encoded equivalents. Set to false to disable html-encoding.
*
* If value for any option key is set to `null` or `false`, that option will be excluded from output.
*
* @param array $options Array of options.
* @param array $exclude Array of options to be excluded, the options here will not be part of the return.
* @param string $insertBefore String to be inserted before options.
* @param string $insertAfter String to be inserted after options.
* @return string Composed attributes.
*/
public function _parseAttributes($options, $exclude = null, $insertBefore = ' ', $insertAfter = null) {
if (is_array($options)) {
$options = array_merge(array('escape' => true), $options);

if (!is_array($exclude)) {
$exclude = array();
}
$filtered = array_diff_key($options, array_merge(array_flip($exclude), array('escape' => true)));
$escape = $options['escape'];
$attributes = array();

foreach ($filtered as $key => $value) {
if ($value !== false && $value !== null) {
$attributes[] = $this->_formatAttribute($key, $value, $escape);
}
}
$out = implode(' ', $attributes);
} else {
$out = $options;
}
return $out ? $insertBefore . $out . $insertAfter : '';
}

/**
* Formats an individual attribute, and returns the string value of the composed attribute.
* Works with minimized attributes that have the same value as their name such as 'disabled' and 'checked'
*
* @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.
*/
protected function _formatAttribute($key, $value, $escape = true) {
$attribute = '';
if (is_array($value)) {
$value = '';
}

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($this->_minimizedAttributeFormat, $key, $key);
}
} else {
$attribute = sprintf($this->_attributeFormat, $key, ($escape ? h($value) : $value));
}
return $attribute;
}

}
55 changes: 0 additions & 55 deletions cake/tests/cases/libs/view/helper.test.php
Expand Up @@ -179,35 +179,6 @@ function parseAttributes($options, $exclude = null, $insertBefore = ' ', $insert
}
}

/**
* Html5TestHelper class
*
* @package 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
*
Expand Down Expand Up @@ -815,32 +786,6 @@ function testWebrootPaths() {
Configure::write('App.www_root', $webRoot);
}

/**
* test parsing attributes.
*
* @return void
*/
function testParseAttributeCompact() {
$helper = new TestHelper($this->View);
$compact = array('compact', 'checked', 'declare', 'readonly', 'disabled',
'selected', 'defer', 'ismap', 'nohref', 'noshade', 'nowrap', 'multiple', 'noresize');

foreach ($compact as $attribute) {
foreach (array('true', true, 1, '1', $attribute) as $value) {
$attrs = array($attribute => $value);
$expected = ' ' . $attribute . '="' . $attribute . '"';
$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)), '');
}

/**
* test lazy loading helpers is seamless
*
Expand Down

0 comments on commit 10d3dd5

Please sign in to comment.