Skip to content

Commit c7cf6cf

Browse files
[VarDumper] fix control chars styling
1 parent 8838143 commit c7cf6cf

File tree

4 files changed

+6
-10
lines changed

4 files changed

+6
-10
lines changed

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

+3-7
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class CliDumper extends AbstractDumper
4141
'meta' => '38;5;27',
4242
);
4343

44-
protected static $controlCharsRx = "/\\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F\x7F/";
44+
protected static $controlCharsRx = '/[\x00-\x1F\x7F]/';
4545

4646
/**
4747
* Enables/disables colored output.
@@ -321,17 +321,13 @@ protected function style($style, $value, $attr = array())
321321
$this->colors = $this->supportsColors($this->outputStream);
322322
}
323323

324-
if (!$this->colors || '' === $value) {
325-
return $value;
326-
}
327-
328324
$style = $this->styles[$style];
329-
$cchr = "\033[m\033[{$style};{$this->styles['cchr']}m%s\033[m\033[{$style}m";
325+
$cchr = $this->colors ? "\033[m\033[{$style};{$this->styles['cchr']}m%s\033[m\033[{$style}m" : '%s';
330326
$value = preg_replace_callback(self::$controlCharsRx, function ($r) use ($cchr) {
331327
return sprintf($cchr, "\x7F" === $r[0] ? '?' : chr(64 + ord($r[0])));
332328
}, $value);
333329

334-
return sprintf("\033[%sm%s\033[m", $style, $value);
330+
return $this->colors ? sprintf("\033[%sm%s\033[m", $style, $value) : $value;
335331
}
336332

337333
/**

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function testGet()
5757
5 => -INF
5858
6 => {$intMax}
5959
"str" => "déjà"
60-
7 => b"é"
60+
7 => b"é@"
6161
"[]" => []
6262
"res" => :stream {@{$res1}
6363
wrapper_type: "plainfile"

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class DumbFoo
1919
$var = array(
2020
'number' => 1, null,
2121
'const' => 1.1, true, false, NAN, INF, -INF, PHP_INT_MAX,
22-
'str' => "déjà", "\xE9",
22+
'str' => "déjà", "\xE9\x00",
2323
'[]' => array(),
2424
'res' => $g,
2525
$h,

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function testGet()
6262
<span class=sf-dump-meta>5</span> => <span class=sf-dump-num>-INF</span>
6363
<span class=sf-dump-meta>6</span> => <span class=sf-dump-num>{$intMax}</span>
6464
"<span class=sf-dump-meta>str</span>" => "<span class=sf-dump-str title="4 characters">d&#233;j&#224;</span>"
65-
<span class=sf-dump-meta>7</span> => b"<span class=sf-dump-str>&#233;</span>"
65+
<span class=sf-dump-meta>7</span> => b"<span class=sf-dump-str title="2 binary or non-UTF-8 characters">&#233;<span class=sf-dump-cchr title=\\x00>@</span></span>"
6666
"<span class=sf-dump-meta>[]</span>" => []
6767
"<span class=sf-dump-meta>res</span>" => <abbr title="`stream` resource" class=sf-dump-note>:stream</abbr> {<a class=sf-dump-solo-ref>@{$res1}</a><samp>
6868
<span class=sf-dump-meta>wrapper_type</span>: "<span class=sf-dump-str title="9 characters">plainfile</span>"

0 commit comments

Comments
 (0)