Added double_encode paramater to h()

This commit is contained in:
Jeremy Harris 2010-10-11 18:23:05 -07:00
parent 5c025d0a18
commit c686362de8
2 changed files with 12 additions and 3 deletions

View file

@ -143,11 +143,12 @@ if (!function_exists('sortByKey')) {
* Convenience method for htmlspecialchars.
*
* @param string $text Text to wrap through htmlspecialchars
* @param boolean $double Encode existing html entities
* @param string $charset Character set to use when escaping. Defaults to config value in 'App.encoding' or 'UTF-8'
* @return string Wrapped text
* @link http://book.cakephp.org/view/1132/h
*/
function h($text, $charset = null) {
function h($text, $double = true, $charset = null) {
if (is_array($text)) {
return array_map('h', $text);
}
@ -160,9 +161,9 @@ if (!function_exists('sortByKey')) {
}
}
if ($charset) {
return htmlspecialchars($text, ENT_QUOTES, $charset);
return htmlspecialchars($text, ENT_QUOTES, $charset, $double);
} else {
return htmlspecialchars($text, ENT_QUOTES, $defaultCharset);
return htmlspecialchars($text, ENT_QUOTES, $defaultCharset, $double);
}
}

View file

@ -200,6 +200,14 @@ class BasicsTest extends CakeTestCase {
$result = h($in);
$expected = array('this & that', '<p>Which one</p>');
$this->assertEqual($expected, $result);
$string = '<foo> & &nbsp;';
$result = h($string);
$this->assertEqual('&lt;foo&gt; &amp; &amp;nbsp;', $result);
$string = '<foo> & &nbsp;';
$result = h($string, false);
$this->assertEqual('&lt;foo&gt; &amp; &nbsp;', $result);
}
/**