From 10d3dd5d2bd285a4b69cd49eaa49a6c76708ba7d Mon Sep 17 00:00:00 2001 From: Juan Basso Date: Sun, 23 Jan 2011 12:36:14 -0200 Subject: [PATCH] Moving the parseAttributes to Html helper. --- cake/libs/view/helper.php | 112 ----------------- cake/libs/view/helpers/html.php | 113 ++++++++++++++++++ cake/tests/cases/libs/view/helper.test.php | 55 --------- .../cases/libs/view/helpers/html.test.php | 71 +++++++++++ 4 files changed, 184 insertions(+), 167 deletions(-) diff --git a/cake/libs/view/helper.php b/cake/libs/view/helper.php index 16ea94798..88588f808 100644 --- a/cake/libs/view/helper.php +++ b/cake/libs/view/helper.php @@ -105,30 +105,6 @@ 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 * @@ -330,94 +306,6 @@ class Helper extends Object { 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. * diff --git a/cake/libs/view/helpers/html.php b/cake/libs/view/helpers/html.php index 7e4db5fcb..d4af24612 100644 --- a/cake/libs/view/helpers/html.php +++ b/cake/libs/view/helpers/html.php @@ -90,6 +90,30 @@ class HtmlHelper extends AppHelper { 'javascriptend' => '' ); +/** + * 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. * @@ -881,4 +905,93 @@ class HtmlHelper extends AppHelper { } 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; + } + } diff --git a/cake/tests/cases/libs/view/helper.test.php b/cake/tests/cases/libs/view/helper.test.php index 44b38f903..125e00ac1 100644 --- a/cake/tests/cases/libs/view/helper.test.php +++ b/cake/tests/cases/libs/view/helper.test.php @@ -179,35 +179,6 @@ class TestHelper extends Helper { } } -/** - * 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 * @@ -815,32 +786,6 @@ class HelperTest extends CakeTestCase { 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 * diff --git a/cake/tests/cases/libs/view/helpers/html.test.php b/cake/tests/cases/libs/view/helpers/html.test.php index 41d0aed9b..c3e73790e 100644 --- a/cake/tests/cases/libs/view/helpers/html.test.php +++ b/cake/tests/cases/libs/view/helpers/html.test.php @@ -47,6 +47,50 @@ class TheHtmlTestController extends Controller { public $uses = null; } +class TestHtmlHelper extends HtmlHelper { +/** + * expose a method as public + * + * @param string $options + * @param string $exclude + * @param string $insertBefore + * @param string $insertAfter + * @return void + */ + function parseAttributes($options, $exclude = null, $insertBefore = ' ', $insertAfter = null) { + return $this->_parseAttributes($options, $exclude, $insertBefore, $insertAfter); + } +} + +/** + * Html5TestHelper class + * + * @package cake.tests.cases.libs.view.helpers + */ +class Html5TestHelper extends TestHtmlHelper { + +/** + * 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"'; +} + /** * HtmlHelperTest class * @@ -1312,4 +1356,31 @@ class HtmlHelperTest extends CakeTestCase { ) ); } + +/** + * test parsing attributes. + * + * @return void + */ + function testParseAttributeCompact() { + $helper = new TestHtmlHelper($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)), ''); + } + }