Refactored _stringify_attributes in system/core/Common.php
diff --git a/application/controllers/welcome.php b/application/controllers/welcome.php
index 1ed82d2..9aaab14 100644
--- a/application/controllers/welcome.php
+++ b/application/controllers/welcome.php
@@ -44,7 +44,10 @@
 	 */
 	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/date_helper.php b/system/helpers/date_helper.php
index a45b3d7..a792f09 100644
--- a/system/helpers/date_helper.php
+++ b/system/helpers/date_helper.php
@@ -575,22 +575,7 @@
 			$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 6fabf9c..9843e80 100644
--- a/system/helpers/html_helper.php
+++ b/system/helpers/html_helper.php
@@ -51,7 +51,7 @@
 	 */
 	function heading($data = '', $h = '1', $attributes = '')
 	{
-		return '<h'.$h.($attributes !== '' ? ' ' : '').$attributes.'>'.$data.'</h'.$h.'>';
+		return '<h'.$h._stringify_attributes($attributes).'>'.$data.'</h'.$h.'>';
 	}
 }
 
@@ -119,23 +119,8 @@
 		// 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._stringify_attributes($attributes).">\n";
 
 		// Cycle through the list elements.  If an array is
 		// encountered we will recursively call _list()
@@ -191,9 +176,10 @@
 	 *
 	 * @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 @@
 			}
 		}
 
-		return $img.'/>';
+		return $img._stringify_attributes($attributes).'/>';
 	}
 }
 
diff --git a/system/helpers/url_helper.php b/system/helpers/url_helper.php
index 39e6343..57208c9 100644
--- a/system/helpers/url_helper.php
+++ b/system/helpers/url_helper.php
@@ -165,7 +165,7 @@
 
 		if ($attributes !== '')
 		{
-			$attributes = _parse_attributes($attributes);
+			$attributes = _stringify_attributes($attributes);
 		}
 
 		return '<a href="'.$site_url.'"'.$attributes.'>'.$title.'</a>';
@@ -221,10 +221,10 @@
 			unset($attributes[$key]);
 		}
 
-		$attributes = empty($attributes) ? '' : _parse_attributes($attributes);
+		$attributes = _stringify_attributes($attributes);
 
 		return '<a href="'.$site_url
-			.'" onclick="window.open(\''.$site_url."', '".$window_name."', '"._parse_attributes($atts, TRUE)."'); return false;\""
+			.'" onclick="window.open(\''.$site_url."', '".$window_name."', '"._stringify_attributes($atts, TRUE)."'); return false;\""
 			.$attributes.'>'.$title.'</a>';
 	}
 }
@@ -250,7 +250,7 @@
 			$title = $email;
 		}
 
-		return '<a href="mailto:'.$email.'"'._parse_attributes($attributes).'>'.$title.'</a>';
+		return '<a href="mailto:'.$email.'"'._stringify_attributes($attributes).'>'.$title.'</a>';
 	}
 }
 
@@ -560,47 +560,5 @@
 	}
 }
 
-// ------------------------------------------------------------------------
-
-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 f9bf6c2..27d48ef 100644
--- a/tests/codeigniter/core/Common_test.php
+++ b/tests/codeigniter/core/Common_test.php
@@ -10,4 +10,35 @@
 		$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
diff --git a/tests/codeigniter/helpers/html_helper_test.php b/tests/codeigniter/helpers/html_helper_test.php
index 9a7bb48..4dd717f 100644
--- a/tests/codeigniter/helpers/html_helper_test.php
+++ b/tests/codeigniter/helpers/html_helper_test.php
@@ -22,6 +22,22 @@
 		$this->assertEquals('<h2 class="bar">foobar</h2>', heading('foobar', 2, 'class="bar"'));
 	}
 
+	public function test_heading_array_attributes()
+	{
+		// Test array of attributes
+		$this->assertEquals('<h2 class="bar" id="foo">foobar</h2>', heading('foobar', 2, array('class' => 'bar', 'id' => 'foo')));
+	}
+
+	public function test_heading_object_attributes()
+	{
+		// Test array of attributes
+		$this->assertEquals('<h2 class="bar" id="foo">foobar</h2>', heading('foobar', 2, array('class' => 'bar', 'id' => 'foo')));
+		$test = new stdClass;
+		$test->class = "bar";
+		$test->id = "foo";
+		$this->assertEquals('<h2 class="bar" id="foo">foobar</h2>', heading('foobar', 2, $test));
+	}
+
 	// ------------------------------------------------------------------------
 
 	public function test_Ul()
@@ -72,5 +88,4 @@
 		$this->assertEquals($expect, meta(array('name' => 'foo')));
 
 	}
-
 }
\ No newline at end of file