Skip to content

Commit

Permalink
Added double_encode paramater to h()
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyharris committed Oct 12, 2010
1 parent 5c025d0 commit c686362
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
7 changes: 4 additions & 3 deletions cake/basics.php
Expand Up @@ -143,11 +143,12 @@ function sortByKey(&$array, $sortby, $order = 'asc', $type = SORT_NUMERIC) {
* 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);
}
Expand All @@ -160,9 +161,9 @@ function h($text, $charset = null) {
}
}
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);
}
}

Expand Down
8 changes: 8 additions & 0 deletions cake/tests/cases/basics.test.php
Expand Up @@ -200,6 +200,14 @@ public function testH() {
$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);
}

/**
Expand Down

0 comments on commit c686362

Please sign in to comment.