Skip to content

Commit

Permalink
Make h() not puke when objects are passed in.
Browse files Browse the repository at this point in the history
This can happen in the default Exception rendering,
if an object is in the viewVars.
  • Loading branch information
markstory committed Oct 28, 2011
1 parent 8529d76 commit adf5223
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
9 changes: 9 additions & 0 deletions lib/Cake/Test/Case/BasicsTest.php
Expand Up @@ -19,6 +19,7 @@

require_once CAKE . 'basics.php';
App::uses('Folder', 'Utility');
App::uses('CakeResponse', 'Network');

/**
* BasicsTest class
Expand Down Expand Up @@ -234,6 +235,14 @@ public function testH() {
'n' => ' '
);
$this->assertEqual($expected, $result);

$obj = new stdClass();
$result = h($obj);
$this->assertEquals('(object)stdClass', $result);

$obj = new CakeResponse(array('body' => 'Body content'));
$result = h($obj);
$this->assertEquals('Body content', $result);
}

/**
Expand Down
10 changes: 9 additions & 1 deletion lib/Cake/basics.php
Expand Up @@ -148,7 +148,9 @@ function sortByKey(&$array, $sortby, $order = 'asc', $type = SORT_NUMERIC) {
/**
* Convenience method for htmlspecialchars.
*
* @param string $text Text to wrap through htmlspecialchars
* @param mixed $text Text to wrap through htmlspecialchars. Also works with arrays, and objects.
* Arrays will be mapped and have all their elements escaped. Objects will be string cast if they
* implement a `__toString` method. Otherwise the class name will be used.
* @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
Expand All @@ -161,6 +163,12 @@ function h($text, $double = true, $charset = null) {
$texts[$k] = h($t, $double, $charset);
}
return $texts;
} elseif (is_object($text)) {
if (method_exists($text, '__toString')) {
$text = (string) $text;
} else {
$text = '(object)' . get_class($text);
}
}

static $defaultCharset = false;
Expand Down

0 comments on commit adf5223

Please sign in to comment.