Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e7e59fb

Browse files
feature #48748 [VarDumper] Display invisible characters (alamirault)
This PR was squashed before being merged into the 6.3 branch. Discussion ---------- [VarDumper] Display invisible characters | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | no | New feature? | yes | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | Fix #47519 <!-- prefix each issue number with "Fix #", no need to create an issue if none exists, explain below instead --> | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> This PR show invisible characters in dumper. `foo` (there is invisible char)=> `foo\u{feff}` Commits ------- 6936845 [VarDumper] Display invisible characters
2 parents 90d7761 + 6936845 commit e7e59fb

File tree

6 files changed

+32
-5
lines changed

6 files changed

+32
-5
lines changed

src/Symfony/Component/VarDumper/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ CHANGELOG
77
* Add caster for `WeakMap`
88
* Add support of named arguments to `dd()` and `dump()` to display the argument name
99
* Add support for `Relay\Relay`
10+
* Add display of invisible characters
1011

1112
6.2
1213
---

src/Symfony/Component/VarDumper/Dumper/CliDumper.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class CliDumper extends AbstractDumper
5151
"\r" => '\r',
5252
"\033" => '\e',
5353
];
54+
protected static $unicodeCharsRx = "/[\u{00A0}\u{00AD}\u{034F}\u{061C}\u{115F}\u{1160}\u{17B4}\u{17B5}\u{180E}\u{2000}-\u{200F}\u{202F}\u{205F}\u{2060}-\u{2064}\u{206A}-\u{206F}\u{3000}\u{2800}\u{3164}\u{FEFF}\u{FFA0}\u{1D159}\u{1D173}-\u{1D17A}]/u";
5455

5556
protected $collapseNextHash = false;
5657
protected $expandNextHash = false;
@@ -450,6 +451,14 @@ protected function style(string $style, string $value, array $attr = []): string
450451
return $s.$endCchr;
451452
}, $value, -1, $cchrCount);
452453

454+
if (!($attr['binary'] ?? false)) {
455+
$value = preg_replace_callback(static::$unicodeCharsRx, function ($c) use (&$cchrCount, $startCchr, $endCchr) {
456+
++$cchrCount;
457+
458+
return $startCchr.'\u{'.strtoupper(dechex(mb_ord($c[0]))).'}'.$endCchr;
459+
}, $value);
460+
}
461+
453462
if ($this->colors) {
454463
if ($cchrCount && "\033" === $value[0]) {
455464
$value = substr($value, \strlen($startCchr));

src/Symfony/Component/VarDumper/Dumper/HtmlDumper.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,6 @@ protected function style(string $style, string $value, array $attr = []): string
862862
} elseif ('private' === $style) {
863863
$style .= sprintf(' title="Private property defined in class:&#10;`%s`"', esc($this->utf8Encode($attr['class'])));
864864
}
865-
$map = static::$controlCharsMap;
866865

867866
if (isset($attr['ellipsis'])) {
868867
$class = 'sf-dump-ellipsis';
@@ -881,6 +880,7 @@ protected function style(string $style, string $value, array $attr = []): string
881880
}
882881
}
883882

883+
$map = static::$controlCharsMap;
884884
$v = "<span class=sf-dump-{$style}>".preg_replace_callback(static::$controlCharsRx, function ($c) use ($map) {
885885
$s = $b = '<span class="sf-dump-default';
886886
$c = $c[$i = 0];
@@ -903,6 +903,12 @@ protected function style(string $style, string $value, array $attr = []): string
903903
return $s.'</span>';
904904
}, $v).'</span>';
905905

906+
if (!($attr['binary'] ?? false)) {
907+
$v = preg_replace_callback(static::$unicodeCharsRx, function ($c) {
908+
return '<span class="sf-dump-default>\u{'.strtoupper(dechex(mb_ord($c[0]))).'}</span>';
909+
}, $v);
910+
}
911+
906912
if (isset($attr['file']) && $href = $this->getSourceLink($attr['file'], $attr['line'] ?? 0)) {
907913
$attr['href'] = $href;
908914
}

src/Symfony/Component/VarDumper/Tests/Dumper/CliDumperTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function testGet()
5151

5252
$this->assertStringMatchesFormat(
5353
<<<EOTXT
54-
array:24 [
54+
array:25 [
5555
"number" => 1
5656
0 => &1 null
5757
"const" => 1.1
@@ -66,6 +66,7 @@ public function testGet()
6666
é\\x01test\\t\\n
6767
ing
6868
"""
69+
"bo\\u{FEFF}m" => "te\\u{FEFF}st"
6970
"[]" => []
7071
"res" => stream resource {@{$res}
7172
%A wrapper_type: "plainfile"

src/Symfony/Component/VarDumper/Tests/Dumper/HtmlDumperTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function testGet()
5454

5555
$this->assertStringMatchesFormat(
5656
<<<EOTXT
57-
<foo></foo><bar><span class=sf-dump-note>array:24</span> [<samp data-depth=1 class=sf-dump-expanded>
57+
<foo></foo><bar><span class=sf-dump-note>array:25</span> [<samp data-depth=1 class=sf-dump-expanded>
5858
"<span class=sf-dump-key>number</span>" => <span class=sf-dump-num>1</span>
5959
<span class=sf-dump-key>0</span> => <a class=sf-dump-ref href=#{$dumpId}-ref01 title="2 occurrences">&amp;1</a> <span class=sf-dump-const>null</span>
6060
"<span class=sf-dump-key>const</span>" => <span class=sf-dump-num>1.1</span>
@@ -69,6 +69,7 @@ public function testGet()
6969
<span class=sf-dump-str title="11 binary or non-UTF-8 characters">&#233;<span class="sf-dump-default">\\x01</span>test<span class="sf-dump-default">\\t</span><span class="sf-dump-default sf-dump-ns">\\n</span></span>
7070
<span class=sf-dump-str title="11 binary or non-UTF-8 characters">ing</span>
7171
"""
72+
"<span class=sf-dump-key>bo<span class="sf-dump-default>\\u{FEFF}</span>m</span>" => "<span class=sf-dump-str title="5 characters">te<span class="sf-dump-default>\\u{FEFF}</span>st</span>"
7273
"<span class=sf-dump-key>[]</span>" => []
7374
"<span class=sf-dump-key>res</span>" => <span class=sf-dump-note>stream resource</span> <a class=sf-dump-ref>@{$res}</a><samp data-depth=2 class=sf-dump-compact>
7475
%A <span class=sf-dump-meta>wrapper_type</span>: "<span class=sf-dump-str title="9 characters">plainfile</span>"

src/Symfony/Component/VarDumper/Tests/Fixtures/dumb-var.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
<?php
22

3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
312
namespace Symfony\Component\VarDumper\Tests\Fixture;
413

514
if (!class_exists(\Symfony\Component\VarDumper\Tests\Fixture\DumbFoo::class)) {
@@ -17,8 +26,8 @@ class DumbFoo
1726

1827
$var = [
1928
'number' => 1, null,
20-
'const' => 1.1, true, false, NAN, INF, -INF, PHP_INT_MAX,
21-
'str' => "déjà\n", "\xE9\x01test\t\ning",
29+
'const' => 1.1, true, false, \NAN, \INF, -\INF, \PHP_INT_MAX,
30+
'str' => "déjà\n", "\xE9\x01test\t\ning", "bo\u{feff}m" => "te\u{feff}st",
2231
'[]' => [],
2332
'res' => $g,
2433
'obj' => $foo,

0 commit comments

Comments
 (0)