From 62ab8b24fc37a25eab9205c46321fa41729e5faf Mon Sep 17 00:00:00 2001 From: Eric Barnes Date: Sat, 28 Jul 2012 14:57:04 -0400 Subject: Adding optional attributes as array or object for html helper --- application/controllers/welcome.php | 5 ++- system/helpers/html_helper.php | 59 +++++++++++++++++--------- tests/codeigniter/helpers/html_helper_test.php | 17 +++++++- 3 files changed, 60 insertions(+), 21 deletions(-) diff --git a/application/controllers/welcome.php b/application/controllers/welcome.php index 1ed82d2a7..9aaab14ad 100644 --- a/application/controllers/welcome.php +++ b/application/controllers/welcome.php @@ -44,7 +44,10 @@ class Welcome extends CI_Controller { */ public function index() { - $this->load->view('welcome_message'); + $this->load->helper('html'); + echo img('test.png', FALSE, array('class' => 'foo')); + + // $this->load->view('welcome_message'); } } diff --git a/system/helpers/html_helper.php b/system/helpers/html_helper.php index 6fabf9c05..96234bb02 100644 --- a/system/helpers/html_helper.php +++ b/system/helpers/html_helper.php @@ -51,7 +51,7 @@ if ( ! function_exists('heading')) */ function heading($data = '', $h = '1', $attributes = '') { - return ''.$data.''; + return ''.$data.''; } } @@ -119,23 +119,8 @@ if ( ! function_exists('_list')) // Set the indentation based on the depth $out = str_repeat(' ', $depth); - // Were any attributes submitted? If so generate a string - if (is_array($attributes)) - { - $atts = ''; - foreach ($attributes as $key => $val) - { - $atts .= ' '.$key.'="'.$val.'"'; - } - $attributes = $atts; - } - elseif (is_string($attributes) && strlen($attributes) > 0) - { - $attributes = ' '.$attributes; - } - // Write the opening list tag - $out .= '<'.$type.$attributes.">\n"; + $out .= '<'.$type._html_attributes_to_string($attributes).">\n"; // Cycle through the list elements. If an array is // encountered we will recursively call _list() @@ -191,9 +176,10 @@ if ( ! function_exists('img')) * * @param mixed * @param bool + * @param mixed * @return string */ - function img($src = '', $index_page = FALSE) + function img($src = '', $index_page = FALSE, $attributes = '') { if ( ! is_array($src) ) { @@ -229,7 +215,7 @@ if ( ! function_exists('img')) } } - return $img.'/>'; + return $img._html_attributes_to_string($attributes).'/>'; } } @@ -407,5 +393,40 @@ if ( ! function_exists('nbs')) } } +if ( ! function_exists('_html_attributes_to_string')) +{ + /** + * Attributes To String + * + * Helper function used to convert array or object of attributes to a string + * + * @param mixed + * @param bool + * @return string + */ + function _html_attributes_to_string($attributes) + { + if (is_object($attributes) && count($attributes) > 0) + { + $attributes = (array) $attributes; + } + + if (is_array($attributes) && count($attributes) > 0) + { + $atts = ''; + foreach ($attributes as $key => $val) + { + $atts .= ' '.$key.'="'.$val.'"'; + } + return $atts; + } + elseif (is_string($attributes) && strlen($attributes) > 0) + { + return ' '.$attributes; + } + + return $attributes; + } +} /* End of file html_helper.php */ /* Location: ./system/helpers/html_helper.php */ \ No newline at end of file diff --git a/tests/codeigniter/helpers/html_helper_test.php b/tests/codeigniter/helpers/html_helper_test.php index 9a7bb48bf..4dd717ff7 100644 --- a/tests/codeigniter/helpers/html_helper_test.php +++ b/tests/codeigniter/helpers/html_helper_test.php @@ -22,6 +22,22 @@ class Html_helper_test extends CI_TestCase { $this->assertEquals('

foobar

', heading('foobar', 2, 'class="bar"')); } + public function test_heading_array_attributes() + { + // Test array of attributes + $this->assertEquals('

foobar

', heading('foobar', 2, array('class' => 'bar', 'id' => 'foo'))); + } + + public function test_heading_object_attributes() + { + // Test array of attributes + $this->assertEquals('

foobar

', heading('foobar', 2, array('class' => 'bar', 'id' => 'foo'))); + $test = new stdClass; + $test->class = "bar"; + $test->id = "foo"; + $this->assertEquals('

foobar

', heading('foobar', 2, $test)); + } + // ------------------------------------------------------------------------ public function test_Ul() @@ -72,5 +88,4 @@ EOH; $this->assertEquals($expect, meta(array('name' => 'foo'))); } - } \ No newline at end of file -- cgit v1.2.3-24-g4f1b From acedd2b1a37b22cb04b01038f21876ddfe38b83a Mon Sep 17 00:00:00 2001 From: Eric Barnes Date: Sun, 29 Jul 2012 00:15:40 -0400 Subject: Adding a common stringify_attributes function for dealing with attributes through out various helpers. Signed-off-by: Eric Barnes --- system/core/Common.php | 49 +++++++++++++++++++++++++++++++++ system/helpers/date_helper.php | 17 +----------- system/helpers/html_helper.php | 41 ++-------------------------- system/helpers/url_helper.php | 50 +++------------------------------- tests/codeigniter/core/Common_test.php | 31 +++++++++++++++++++++ 5 files changed, 88 insertions(+), 100 deletions(-) diff --git a/system/core/Common.php b/system/core/Common.php index 06b162264..d4d01f813 100644 --- a/system/core/Common.php +++ b/system/core/Common.php @@ -598,5 +598,54 @@ if ( ! function_exists('html_escape')) } } +// ------------------------------------------------------------------------ + +if ( ! function_exists('_stringify_attributes')) +{ + /** + * Attributes To String + * + * Helper function used to convert an array or object of + * attributes to a string + * + * @param mixed + * @return string + */ + function _stringify_attributes($attributes, $js = FALSE) + { + if (is_object($attributes) && count($attributes) > 0) + { + $attributes = (array) $attributes; + } + + if (is_array($attributes)) + { + $atts = ''; + if (count($attributes) === 0) + { + return $atts; + } + foreach ($attributes as $key => $val) + { + if ($js) + { + $atts .= $key.'='.$val.','; + } + else + { + $atts .= ' '.$key.'="'.$val.'"'; + } + } + return rtrim($atts, ','); + } + elseif (is_string($attributes) && strlen($attributes) > 0) + { + return ' '.$attributes; + } + + return $attributes; + } +} + /* End of file Common.php */ /* Location: ./system/core/Common.php */ \ No newline at end of file diff --git a/system/helpers/date_helper.php b/system/helpers/date_helper.php index a45b3d7ac..a792f09a2 100644 --- a/system/helpers/date_helper.php +++ b/system/helpers/date_helper.php @@ -575,22 +575,7 @@ if ( ! function_exists('timezone_menu')) $menu .= ' class="'.$class.'"'; } - // Generate a string from the attributes submitted, if any - if (is_array($attributes)) - { - $atts = ''; - foreach ($attributes as $key => $val) - { - $atts .= ' '.$key.'="'.$val.'"'; - } - $attributes = $atts; - } - elseif (is_string($attributes) && strlen($attributes) > 0) - { - $attributes = ' '.$attributes; - } - - $menu .= $attributes.">\n"; + $menu .= _stringify_attributes($attributes).">\n"; foreach (timezones() as $key => $val) { diff --git a/system/helpers/html_helper.php b/system/helpers/html_helper.php index 96234bb02..9843e804e 100644 --- a/system/helpers/html_helper.php +++ b/system/helpers/html_helper.php @@ -51,7 +51,7 @@ if ( ! function_exists('heading')) */ function heading($data = '', $h = '1', $attributes = '') { - return ''.$data.''; + return ''.$data.''; } } @@ -120,7 +120,7 @@ if ( ! function_exists('_list')) $out = str_repeat(' ', $depth); // Write the opening list tag - $out .= '<'.$type._html_attributes_to_string($attributes).">\n"; + $out .= '<'.$type._stringify_attributes($attributes).">\n"; // Cycle through the list elements. If an array is // encountered we will recursively call _list() @@ -215,7 +215,7 @@ if ( ! function_exists('img')) } } - return $img._html_attributes_to_string($attributes).'/>'; + return $img._stringify_attributes($attributes).'/>'; } } @@ -393,40 +393,5 @@ if ( ! function_exists('nbs')) } } -if ( ! function_exists('_html_attributes_to_string')) -{ - /** - * Attributes To String - * - * Helper function used to convert array or object of attributes to a string - * - * @param mixed - * @param bool - * @return string - */ - function _html_attributes_to_string($attributes) - { - if (is_object($attributes) && count($attributes) > 0) - { - $attributes = (array) $attributes; - } - - if (is_array($attributes) && count($attributes) > 0) - { - $atts = ''; - foreach ($attributes as $key => $val) - { - $atts .= ' '.$key.'="'.$val.'"'; - } - return $atts; - } - elseif (is_string($attributes) && strlen($attributes) > 0) - { - return ' '.$attributes; - } - - return $attributes; - } -} /* End of file html_helper.php */ /* Location: ./system/helpers/html_helper.php */ \ No newline at end of file diff --git a/system/helpers/url_helper.php b/system/helpers/url_helper.php index 39e6343a6..57208c948 100644 --- a/system/helpers/url_helper.php +++ b/system/helpers/url_helper.php @@ -165,7 +165,7 @@ if ( ! function_exists('anchor')) if ($attributes !== '') { - $attributes = _parse_attributes($attributes); + $attributes = _stringify_attributes($attributes); } return ''.$title.''; @@ -221,10 +221,10 @@ if ( ! function_exists('anchor_popup')) unset($attributes[$key]); } - $attributes = empty($attributes) ? '' : _parse_attributes($attributes); + $attributes = _stringify_attributes($attributes); return ''.$title.''; } } @@ -250,7 +250,7 @@ if ( ! function_exists('mailto')) $title = $email; } - return ''.$title.''; + return ''.$title.''; } } @@ -560,47 +560,5 @@ if ( ! function_exists('redirect')) } } -// ------------------------------------------------------------------------ - -if ( ! function_exists('_parse_attributes')) -{ - /** - * Parse out the attributes - * - * Some of the functions use this - * - * @param array - * @param bool - * @return string - */ - function _parse_attributes($attributes, $javascript = FALSE) - { - if (is_string($attributes)) - { - return ($attributes !== '') ? ' '.$attributes : ''; - } - - $att = ''; - foreach ($attributes as $key => $val) - { - if ($javascript === TRUE) - { - $att .= $key.'='.$val.','; - } - else - { - $att .= ' '.$key.'="'.$val.'"'; - } - } - - if ($javascript === TRUE && $att !== '') - { - return substr($att, 0, -1); - } - - return $att; - } -} - /* End of file url_helper.php */ /* Location: ./system/helpers/url_helper.php */ \ No newline at end of file diff --git a/tests/codeigniter/core/Common_test.php b/tests/codeigniter/core/Common_test.php index f9bf6c27f..27d48efc2 100644 --- a/tests/codeigniter/core/Common_test.php +++ b/tests/codeigniter/core/Common_test.php @@ -10,4 +10,35 @@ class Common_test extends CI_TestCase { $this->assertEquals(FALSE, is_php('9999.9.9')); } + // ------------------------------------------------------------------------ + + public function test_stringify_attributes() + { + $this->assertEquals(' class="foo" id="bar"', _stringify_attributes(array('class' => 'foo', 'id' => 'bar'))); + + $atts = new Stdclass; + $atts->class = 'foo'; + $atts->id = 'bar'; + $this->assertEquals(' class="foo" id="bar"', _stringify_attributes($atts)); + + $atts = new Stdclass; + $this->assertEquals('', _stringify_attributes($atts)); + + $this->assertEquals(' class="foo" id="bar"', _stringify_attributes('class="foo" id="bar"')); + + $this->assertEquals('', _stringify_attributes(array())); + } + + // ------------------------------------------------------------------------ + + public function test_stringify_js_attributes() + { + $this->assertEquals('width=800,height=600', _stringify_attributes(array('width' => '800', 'height' => '600'), TRUE)); + + $atts = new Stdclass; + $atts->width = 800; + $atts->height = 600; + $this->assertEquals('width=800,height=600', _stringify_attributes($atts, TRUE)); + } + } \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 19204f96b8abc1322feb0f660240ed7abb69026b Mon Sep 17 00:00:00 2001 From: Eric Barnes Date: Sun, 29 Jul 2012 00:18:07 -0400 Subject: Refactored _stringify_attributes function Signed-off-by: Eric Barnes --- system/core/Common.php | 46 ++++++++++++++++++++++------------------------ 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/system/core/Common.php b/system/core/Common.php index d4d01f813..7c46c590a 100644 --- a/system/core/Common.php +++ b/system/core/Common.php @@ -603,47 +603,45 @@ if ( ! function_exists('html_escape')) if ( ! function_exists('_stringify_attributes')) { /** - * Attributes To String + * Stringify attributes for use in html tags. * - * Helper function used to convert an array or object of + * Helper function used to convert a string, array, or object of * attributes to a string * - * @param mixed + * @param mixed string, array, object + * @param bool * @return string */ function _stringify_attributes($attributes, $js = FALSE) { - if (is_object($attributes) && count($attributes) > 0) + if (is_string($attributes)) + { + return strlen($attributes) > 0 ? ' '.$attributes : $attributes; + } + + if (is_object($attributes)) { $attributes = (array) $attributes; } - if (is_array($attributes)) + if (count($attributes) === 0) + { + return; + } + + $atts = ''; + foreach ($attributes as $key => $val) { - $atts = ''; - if (count($attributes) === 0) + if ($js) { - return $atts; + $atts .= $key.'='.$val.','; } - foreach ($attributes as $key => $val) + else { - if ($js) - { - $atts .= $key.'='.$val.','; - } - else - { - $atts .= ' '.$key.'="'.$val.'"'; - } + $atts .= ' '.$key.'="'.$val.'"'; } - return rtrim($atts, ','); - } - elseif (is_string($attributes) && strlen($attributes) > 0) - { - return ' '.$attributes; } - - return $attributes; + return rtrim($atts, ','); } } -- cgit v1.2.3-24-g4f1b From a1abadaab34d8d08c54557c7e60d12eb624b72fe Mon Sep 17 00:00:00 2001 From: Chad Furman Date: Sun, 29 Jul 2012 01:03:50 -0400 Subject: refactored (crunched down) _stringify_attributes --- system/core/Common.php | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/system/core/Common.php b/system/core/Common.php index 06b162264..5cd3961d1 100644 --- a/system/core/Common.php +++ b/system/core/Common.php @@ -598,5 +598,44 @@ if ( ! function_exists('html_escape')) } } +// ------------------------------------------------------------------------ + +if ( ! function_exists('_stringify_attributes')) +{ + /** + * Stringify attributes for use in html tags. + * + * Helper function used to convert a string, array, or object of + * attributes to a string + * + * @param mixed string, array, object + * @param bool + * @return string + */ + function _stringify_attributes($attributes, $js = FALSE) + { + $atts = null; + + if (empty($attributes)) + { + return $atts; + } + + if (is_string($attributes)) + { + return ' '.$attributes; + } + + $attributes = (array) $attributes; + + foreach ($attributes as $key => $val) + { + $atts .= ($js) ? $key.'='.$val.',' : ' '.$key.'="'.$val.'"'; + } + + return rtrim($atts, ','); + } +} + /* End of file Common.php */ -/* Location: ./system/core/Common.php */ \ No newline at end of file +/* Location: ./system/core/Common.php */ -- cgit v1.2.3-24-g4f1b From 49d2f304d4bb9c04c41c34f4675813b9d8ac3204 Mon Sep 17 00:00:00 2001 From: Eric Barnes Date: Sun, 29 Jul 2012 01:21:53 -0400 Subject: Fixed messed up welcome controller. Not sure how to revert this. Signed-off-by: Eric Barnes --- application/controllers/welcome.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/application/controllers/welcome.php b/application/controllers/welcome.php index 9aaab14ad..1ed82d2a7 100644 --- a/application/controllers/welcome.php +++ b/application/controllers/welcome.php @@ -44,10 +44,7 @@ class Welcome extends CI_Controller { */ public function index() { - $this->load->helper('html'); - echo img('test.png', FALSE, array('class' => 'foo')); - - // $this->load->view('welcome_message'); + $this->load->view('welcome_message'); } } -- cgit v1.2.3-24-g4f1b