Skip to content

Commit

Permalink
Added backwards compatibility to h()'s second param
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyharris committed Oct 12, 2010
1 parent b58899c commit 137c4f7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
9 changes: 8 additions & 1 deletion cake/basics.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,11 @@ function sortByKey(&$array, $sortby, $order = 'asc', $type = SORT_NUMERIC) {
*/
function h($text, $double = true, $charset = null) {
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;
Expand All @@ -160,6 +164,9 @@ function h($text, $double = true, $charset = null) {
$defaultCharset = 'UTF-8';
}
}
if (is_string($double)) {
$charset = $double;
}
if ($charset) {
return htmlspecialchars($text, ENT_QUOTES, $charset, $double);
} else {
Expand Down
20 changes: 20 additions & 0 deletions cake/tests/cases/basics.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,26 @@ public function testH() {
$string = '<foo> & &nbsp;';
$result = h($string, false);
$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);
}

/**
Expand Down

0 comments on commit 137c4f7

Please sign in to comment.