Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Message max length #383

Merged
merged 17 commits into from Sep 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Tracy/BlueScreen/BlueScreen.php
Expand Up @@ -112,7 +112,7 @@ private function renderTemplate(\Throwable $exception, string $template, $toScre
$messageHtml = preg_replace(
'#\'\S(?:[^\']|\\\\\')*\S\'|"\S(?:[^"]|\\\\")*\S"#',
'<i>$0</i>',
htmlspecialchars((string) $exception->getMessage(), ENT_SUBSTITUTE, 'UTF-8')
htmlspecialchars(Dumper::encodeString((string) $exception->getMessage(), 32768), ENT_SUBSTITUTE, 'UTF-8')
);
$info = array_filter($this->info);
$source = Helpers::getSource();
Expand Down
4 changes: 2 additions & 2 deletions src/Tracy/BlueScreen/assets/content.phtml
Expand Up @@ -29,10 +29,10 @@ $code = $exception->getCode() ? ' #' . $exception->getCode() : '';
<a id="tracy-bs-toggle" href="#" class="tracy-toggle"></a>
<div class="tracy-bs-main">
<div id="tracy-bs-error" class="panel">
<?php if ($exception->getMessage()): ?><p><?= Helpers::escapeHtml($title . $code) ?></p><?php endif ?>
<?php if ($exception->getMessage()): ?><p><?= Helpers::escapeHtml(Dumper::encodeString($title . $code, 32768)) ?></p><?php endif ?>


<h1><span><?= $messageHtml ?: Helpers::escapeHtml($title . $code) ?></span>
<h1><span><?= $messageHtml ?: Helpers::escapeHtml(Dumper::encodeString($title . $code, 32768)) ?></span>
<?php foreach ($actions as $item): ?>
<a href="<?= Helpers::escapeHtml($item['link']) ?>"<?= empty($item['external']) ? '' : ' target="_blank" rel="noreferrer noopener"'?>><?= Helpers::escapeHtml($item['label']) ?>&#x25ba;</a>
<?php endforeach ?></h1>
Expand Down
64 changes: 35 additions & 29 deletions src/Tracy/Dumper/Dumper.php
Expand Up @@ -504,46 +504,52 @@ public static function formatSnapshotAttribute(array &$snapshot): string
*/
public static function encodeString(string $s, int $maxLength = null): string
{
static $table;
if ($table === null) {
foreach (array_merge(range("\x00", "\x1F"), range("\x7F", "\xFF")) as $ch) {
$table[$ch] = '\x' . str_pad(dechex(ord($ch)), 2, '0', STR_PAD_LEFT);
}
$table['\\'] = '\\\\';
$table["\r"] = '\r';
$table["\n"] = '\n';
$table["\t"] = '\t';
}

if ($maxLength && strlen($s) > $maxLength) { // shortens to $maxLength in UTF-8 or longer
if (function_exists('mb_substr')) {
$s = mb_substr($tmp = $s, 0, $maxLength, 'UTF-8');
$shortened = $s !== $tmp;
} else {
$i = $len = 0;
$maxI = $maxLength * 4; // max UTF-8 length
do {
if (($s[$i] < "\x80" || $s[$i] >= "\xC0") && (++$len > $maxLength) || $i >= $maxI) {
$s = substr($s, 0, $i);
$shortened = true;
break;
}
} while (isset($s[++$i]));
}
if ($maxLength) {
$s = self::truncateString($tmp = $s, $maxLength);
$shortened = $s !== $tmp;
}

if (preg_match('#[^\x09\x0A\x0D\x20-\x7E\xA0-\x{10FFFF}]#u', $s) || preg_last_error()) { // is binary?
if ($maxLength && strlen($s) > $maxLength) {
$s = substr($s, 0, $maxLength);
$shortened = true;
static $table;
if ($table === null) {
foreach (array_merge(range("\x00", "\x1F"), range("\x7F", "\xFF")) as $ch) {
$table[$ch] = '\x' . str_pad(dechex(ord($ch)), 2, '0', STR_PAD_LEFT);
}
$table['\\'] = '\\\\';
$table["\r"] = '\r';
$table["\n"] = '\n';
$table["\t"] = '\t';
}

$s = strtr($s, $table);
}

return $s . (empty($shortened) ? '' : ' ... ');
}


/**
* @internal
*/
public static function truncateString(string $s, int $maxLength): string
{
if (!preg_match('##u', $s)) {
$s = substr($s, 0, $maxLength); // not UTF-8
} elseif (function_exists('mb_substr')) {
$s = mb_substr($s, 0, $maxLength, 'UTF-8');
} else {
$i = $len = 0;
do {
if (($s[$i] < "\x80" || $s[$i] >= "\xC0") && (++$len > $maxLength)) {
$s = substr($s, 0, $i);
break;
}
} while (isset($s[++$i]));
}
return $s;
}


/**
* @param int|string $k
* @return int|string
Expand Down
29 changes: 29 additions & 0 deletions tests/Tracy/Dumper.truncateString(().phpt
@@ -0,0 +1,29 @@
<?php

/**
* Test: Tracy\Dumper::truncateString()
*/

declare(strict_types=1);

use Tester\Assert;
use Tracy\Dumper;


require __DIR__ . '/../bootstrap.php';


Assert::same('', Dumper::truncateString('', 1));
Assert::same('h', Dumper::truncateString('hello', 1));
Assert::same('hello', Dumper::truncateString('hello', 5));
Assert::same('hello', Dumper::truncateString('hello', 6));

Assert::same('Iñtërnâtiônàlizætiøn', Dumper::truncateString('Iñtërnâtiônàlizætiøn', 20));
Assert::same('Iñtër', Dumper::truncateString('Iñtërnâtiônàlizætiøn', 5));

Assert::same("\x00", Dumper::truncateString("\x00\x01", 1));
Assert::same("\x00\x01", Dumper::truncateString("\x00\x01", 5));

Assert::same('bad', Dumper::truncateString("bad\xff", 3));
Assert::same("bad\xff", Dumper::truncateString("bad\xff", 4));
Assert::same("bad\xff", Dumper::truncateString("bad\xff", 5));