Added backwards compatibility to h()'s second param

This commit is contained in:
Jeremy Harris 2010-10-11 19:26:41 -07:00
parent b58899cf04
commit 137c4f7295
2 changed files with 28 additions and 1 deletions

View file

@ -150,7 +150,11 @@ if (!function_exists('sortByKey')) {
*/ */
function h($text, $double = true, $charset = null) { function h($text, $double = true, $charset = null) {
if (is_array($text)) { if (is_array($text)) {
return array_map('h', $text); $texts = array();
foreach ($text as $t) {
$texts[] = h($t, $double, $charset);
}
return $texts;
} }
static $defaultCharset = false; static $defaultCharset = false;
@ -160,6 +164,9 @@ if (!function_exists('sortByKey')) {
$defaultCharset = 'UTF-8'; $defaultCharset = 'UTF-8';
} }
} }
if (is_string($double)) {
$charset = $double;
}
if ($charset) { if ($charset) {
return htmlspecialchars($text, ENT_QUOTES, $charset, $double); return htmlspecialchars($text, ENT_QUOTES, $charset, $double);
} else { } else {

View file

@ -208,6 +208,26 @@ class BasicsTest extends CakeTestCase {
$string = '<foo> & &nbsp;'; $string = '<foo> & &nbsp;';
$result = h($string, false); $result = h($string, false);
$this->assertEqual('&lt;foo&gt; &amp; &nbsp;', $result); $this->assertEqual('&lt;foo&gt; &amp; &nbsp;', $result);
$string = '<foo> & &nbsp;';
$result = h($string, 'UTF-8');
$this->assertEqual('&lt;foo&gt; &amp; &amp;nbsp;', $result);
$arr = array('<foo>', '&nbsp;');
$result = h($arr);
$expected = array(
'&lt;foo&gt;',
'&amp;nbsp;'
);
$this->assertEqual($expected, $result);
$arr = array('<foo>', '&nbsp;');
$result = h($arr, false);
$expected = array(
'&lt;foo&gt;',
'&nbsp;'
);
$this->assertEqual($expected, $result);
} }
/** /**