Permalink
Browse files

Fix stack traces not being highlighted correctly.

Fixes #3439
  • Loading branch information...
1 parent 6cf6903 commit 1e49be3472b27fd7b67094eaab78cae84db3d893 @markstory markstory committed Dec 9, 2012
Showing with 28 additions and 10 deletions.
  1. +7 −1 lib/Cake/Test/Case/Utility/DebuggerTest.php
  2. +21 −9 lib/Cake/Utility/Debugger.php
@@ -84,7 +84,13 @@ public function testExcerpt() {
$this->assertTrue(is_array($result));
$this->assertEquals(4, count($result));
- $pattern = '/<code><span style\="color\: \#\d+">.*?&lt;\?php/';
+ $pattern = '/<code>.*?<span style\="color\: \#\d+">.*?&lt;\?php/';
+ $this->assertRegExp($pattern, $result[0]);
+
+ $result = Debugger::excerpt(__FILE__, 10, 2);
+ $this->assertEquals(5, count($result));
+
+ $pattern = '/<span style\="color\: \#\d{6}">\*<\/span>/';
$this->assertRegExp($pattern, $result[0]);
$return = Debugger::excerpt('[internal]', 2, 2);
@@ -396,12 +396,14 @@ public static function excerpt($file, $line, $context = 2) {
return array();
}
$data = file_get_contents($file);
- if (!empty($data) && strpos($data, "\n") !== false) {
+ if (empty($data)) {
+ return $lines;
+ }
+ if (strpos($data, "\n") !== false) {
$data = explode("\n", $data);
}
-
- if (empty($data) || !isset($data[$line])) {
- return;
+ if (!isset($data[$line])) {
+ return $lines;
}
for ($i = $line - ($context + 1); $i < $line + $context; $i++) {
if (!isset($data[$i])) {
@@ -425,13 +427,23 @@ public static function excerpt($file, $line, $context = 2) {
* @return string
*/
protected static function _highlight($str) {
- static $supportHighlight = null;
- if (!$supportHighlight && function_exists('hphp_log')) {
- $supportHighlight = false;
+ if (function_exists('hphp_log')) {
return htmlentities($str);
}
- $supportHighlight = true;
- return highlight_string($str, true);
+ $added = false;
+ if (strpos($str, '<?php') === false) {
+ $added = true;
+ $str = "<?php \n" . $str;
+ }
+ $highlight = highlight_string($str, true);
+ if ($added) {
+ $highlight = str_replace(
+ '&lt;?php&nbsp;<br />',
+ '',
+ $highlight
+ );
+ }
+ return $highlight;
}
/**

0 comments on commit 1e49be3

Please sign in to comment.