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

Improve resulting HTML of syntax highlighting in EchoHandler #112

Merged
merged 3 commits into from
Dec 30, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ jobs:
os: >-
['ubuntu-latest', 'windows-latest']
php: >-
['8.0', '8.1', '8.2']
['8.0', '8.1', '8.2', '8.3']
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## 1.7.1 under development

- no changes in this release.
- Enh #112: Improve resulting HTML of syntax highlighting in `EchoHandler` (@vjik)

## 1.7.0 October 08, 2023

Expand Down
18 changes: 13 additions & 5 deletions src/Handler/EchoHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,19 @@ public function handle(mixed $variable, int $depth, bool $highlight = false): vo
$varDumper = VarDumper::create($variable);
$output = $varDumper->asString($depth);

if ($highlight) {
$result = highlight_string("<?php\n" . $output, true);
$output = preg_replace('/&lt;\\?php<br \\/>/', '', $result, 1);
}
echo $highlight
? $this->highlight($output)
: $output;
}

private function highlight(string $string): string
{
$result = highlight_string("<?php\n" . $string, true);

$pattern = PHP_VERSION_ID >= 80300
? '~<span style="color: #0000BB">&lt;\\?php\n</span>~'
: '~<span style="color: #0000BB">&lt;\\?php<br \\/></span>~';

echo $output;
return preg_replace($pattern, '', $result, 1);
}
}
16 changes: 11 additions & 5 deletions tests/Handler/EchoHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,16 @@ public function testHighlight(): void

$handler->handle('test', 1, true);

$this->expectOutputString(
<<<HTML
<code><span style="color: #000000">\n<span style="color: #0000BB"></span><span style="color: #DD0000">'test'</span>\n</span>\n</code>
HTML
);
if (PHP_VERSION_ID >= 80300) {
$expected = <<<HTML
<pre><code style="color: #000000"><span style="color: #DD0000">'test'</span></code></pre>
HTML;
} else {
$expected = <<<HTML
<code><span style="color: #000000">\n<span style="color: #DD0000">'test'</span>\n</span>\n</code>
HTML;
}

$this->expectOutputString($expected);
}
}
8 changes: 2 additions & 6 deletions tests/VarDumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
use Yiisoft\VarDumper\VarDumper as Dumper;

use function fopen;
use function highlight_string;
use function preg_replace;
use function spl_object_id;
use function str_replace;
Expand Down Expand Up @@ -1445,11 +1444,8 @@ public function testDFunctionWithMultipleVariables(): void

public function testDumpWithHighlight(): void
{
$var = 'content';
$result = highlight_string("<?php\n'{$var}'", true);
$output = preg_replace('/&lt;\\?php<br \\/>/', '', $result, 1);
VarDumper::dump($var);
$this->expectOutputString($output);
VarDumper::dump('content');
$this->expectOutputRegex('~<span style="color: #DD0000">\'content\'</span>~');
}

public function testDumpWithOutHighlight(): void
Expand Down
Loading