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

[VarDumper] Display invisible characters #48748

Conversation

alamirault
Copy link
Contributor

Q A
Branch? 6.3
Bug fix? no
New feature? yes
Deprecations? no
Tickets Fix #47519
License MIT
Doc PR symfony/symfony-docs#...

This PR show invisible characters in dumper.

foo (there is invisible char)=> foo\u{feff}

@nicolas-grekas
Copy link
Member

Let's go with a new property. Here is the implementation I propose:

--- a/src/Symfony/Component/VarDumper/Dumper/CliDumper.php
+++ b/src/Symfony/Component/VarDumper/Dumper/CliDumper.php
@@ -51,6 +51,7 @@ class CliDumper extends AbstractDumper
         "\r" => '\r',
         "\033" => '\e',
     ];
+    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";
 
     protected $collapseNextHash = false;
     protected $expandNextHash = false;
@@ -450,6 +451,13 @@ class CliDumper extends AbstractDumper
             return $s.$endCchr;
         }, $value, -1, $cchrCount);
 
+        if (!($attr['binary'] ?? false)) {
+            $value = preg_replace_callback(static::$unicodeCharsRx, function ($c) use (&$cchrCount, $startCchr, $endCchr) {
+                ++$cchrCount;
+
+                return $startCchr.'\u{'.strtoupper(dechex(mb_ord($c[0]))).'}'.$endCchr;
+            }, $value);
+        }
         if ($this->colors) {
             if ($cchrCount && "\033" === $value[0]) {
                 $value = substr($value, \strlen($startCchr));

@alamirault alamirault force-pushed the feature/47519-display-invisible-characters branch from 76b3768 to 395783f Compare January 9, 2023 19:22
@alamirault
Copy link
Contributor Author

I applied patch but one HtmlDumperTest is broken and I don't understand why.

Expected #%d, real value given (ex: #1388)

@nicolas-grekas nicolas-grekas force-pushed the feature/47519-display-invisible-characters branch from 395783f to 6936845 Compare January 26, 2023 14:58
@nicolas-grekas
Copy link
Member

Thank you @alamirault.

@nicolas-grekas nicolas-grekas merged commit e7e59fb into symfony:6.3 Jan 26, 2023
@alamirault alamirault deleted the feature/47519-display-invisible-characters branch January 27, 2023 19:15
@fabpot fabpot mentioned this pull request May 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[VarDumper] Display BOM characters
3 participants