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

Escape array values to prevent StyleNotFound exception #14

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Printers/CliPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,12 @@ public function optionsHtml(MessageLogged $messageLogged): string
return collect($options)->merge(
$messageLogged->context() // @phpstan-ignore-line
)->reject(fn (mixed $value, string|int $key) => is_int($key) && is_null($value))
->map(fn (mixed $value) => is_string($value) ? e($value) : var_export($value, true))
->map(fn (array|string|null $value) => is_array($value) ?
collect($value)->map(fn (string|null $value) => is_string($value) ? e($value) : var_export($value, true))->implode(', ') :
$value
)
->map(fn (mixed $value) => is_string($value) ? e($value): $value)
->map(fn (mixed $value) => is_string($value) ? $value : var_export($value, true))
->map(fn (string $value, string|int $key) => is_string($key) ? "$key: $value" : $value)
->map(fn (string $value) => "<span class=\"font-bold\">$value</span>")
->implode(' • ');
Expand Down
30 changes: 30 additions & 0 deletions tests/Unit/CliPrinterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,33 @@
EOF
);
});

test('escaping an array of html options', function () {
$output = output([
'message' => 'Context that contains html',
'level_name' => 'info',
'datetime' => '2021-01-01 00:00:00',
'context' => [
'html' => ['<div class=3D"gmail-adL" style=3D"box-sizing:border-box"></div>'],
'__pail' => [
'origin' => [
'type' => 'http',
'method' => 'GET',
'path' => '/logs',
'auth_id' => null,
'auth_email' => null,
],
],
],
]);

$html = e('<div class=3D"gmail-adL" style=3D"box-sizing:border-box"></div>');

expect($output)->toBe(<<<EOF
┌ 03:04:05 INFO ─────────────────────────────────┐
│ Context that contains html │
└ GET: /logs • Auth ID: guest • html: $html ┘

EOF
);
});