Skip to content

Commit 83d116b

Browse files
minor symfony#27317 [VarDumper] make control characters non-selectable in HTML (kiler129)
This PR was merged into the 4.2-dev branch. Discussion ---------- [VarDumper] make control characters non-selectable in HTML | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | maybe | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#23921 | License | MIT | Doc PR | - ### Overview This fixes long term annoyance with control characters in dumps. It makes dumps unusable to use with e.g. SQLs, since copying them will leave `\n` (and others). ### Changes This PR does three things: 1. Adds special class of characters abbreviated as `ctrl` (cosmetic, already exists in fact but it's not exposed in HTML) 2. Makes control characters non-selectable 3. Changes color of control character from default orange to grey (used by Symfony.com dark theme) which improves UX by showing *"hey, this is something special"* ### Preview **Inline dump** <img width="243" alt="screenshot 2018-05-19 17 26 18" src="https://user-images.githubusercontent.com/1227834/40273711-7d963450-5b8b-11e8-843a-dda1e2719b59.png"> **Web Profiler** <img width="418" alt="screenshot 2018-05-19 17 38 36" src="https://user-images.githubusercontent.com/1227834/40273714-86b9cfec-5b8b-11e8-898b-b7cb5f150a72.png"> <img width="533" alt="screenshot 2018-05-19 17 20 42" src="https://user-images.githubusercontent.com/1227834/40273720-a2370852-5b8b-11e8-88c7-aed9281cc056.png"> **Dump Server** <img width="194" alt="screenshot 2018-05-19 17 30 12" src="https://user-images.githubusercontent.com/1227834/40273722-aa444686-5b8b-11e8-9ae9-ea2eebb3368d.png"> Commits ------- b0d4c99 [VarDumper] make control characters non-selectable in HTML
2 parents 80aa8df + b0d4c99 commit 83d116b

File tree

5 files changed

+25
-5
lines changed

5 files changed

+25
-5
lines changed

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class HtmlDumper extends CliDumper
4444
'key' => 'color:#56DB3A',
4545
'index' => 'color:#1299DA',
4646
'ellipsis' => 'color:#FF8400',
47+
'ns' => 'user-select:none;',
4748
);
4849

4950
private $displayOptions = array(
@@ -837,9 +838,21 @@ protected function style($style, $value, $attr = array())
837838
}
838839

839840
$v = "<span class=sf-dump-{$style}>".preg_replace_callback(static::$controlCharsRx, function ($c) use ($map) {
840-
$s = '<span class=sf-dump-default>';
841+
$s = $b = '<span class="sf-dump-default';
841842
$c = $c[$i = 0];
843+
if ($ns = "\r" === $c[$i] || "\n" === $c[$i])) {
844+
$s .= ' sf-dump-ns';
845+
}
846+
$s .= '">';
842847
do {
848+
if (("\r" === $c[$i] || "\n" === $c[$i]) !== $ns) {
849+
$s .= '</span>'.$b;
850+
if ($ns = !$ns) {
851+
$s .= ' sf-dump-ns';
852+
}
853+
$s .= '">';
854+
}
855+
843856
$s .= isset($map[$c[$i]]) ? $map[$c[$i]] : sprintf('\x%02X', ord($c[$i]));
844857
} while (isset($c[++$i]));
845858

src/Symfony/Component/VarDumper/Resources/css/htmlDescriptor.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,4 @@ pre.sf-dump {
127127
.dumped-tag > .sf-dump .sf-dump-ref { color: #6E6E6E; }
128128
.dumped-tag > .sf-dump .sf-dump-ellipsis { color: #CC7832; max-width: 100em; }
129129
.dumped-tag > .sf-dump .sf-dump-ellipsis-path { max-width: 5em; }
130+
.dumped-tag > .sf-dump .sf-dump-ns { user-select: none; }

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ public function testGet()
6161
5 => -INF
6262
6 => {$intMax}
6363
"str" => "déjà\\n"
64-
7 => b"é\\x00"
64+
7 => b"""
65+
é\\x00test\\t\\n
66+
ing
67+
"""
6568
"[]" => []
6669
"res" => stream resource {@{$res}
6770
%A wrapper_type: "plainfile"

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,11 @@ public function testGet()
6363
<span class=sf-dump-key>4</span> => <span class=sf-dump-num>INF</span>
6464
<span class=sf-dump-key>5</span> => <span class=sf-dump-num>-INF</span>
6565
<span class=sf-dump-key>6</span> => <span class=sf-dump-num>{$intMax}</span>
66-
"<span class=sf-dump-key>str</span>" => "<span class=sf-dump-str title="5 characters">d&%s;j&%s;<span class=sf-dump-default>\\n</span></span>"
67-
<span class=sf-dump-key>7</span> => b"<span class=sf-dump-str title="2 binary or non-UTF-8 characters">&%s;<span class=sf-dump-default>\\x00</span></span>"
66+
"<span class=sf-dump-key>str</span>" => "<span class=sf-dump-str title="5 characters">d&%s;j&%s;<span class="sf-dump-default sf-dump-ns">\\n</span></span>"
67+
<span class=sf-dump-key>7</span> => b"""
68+
<span class=sf-dump-str title="11 binary or non-UTF-8 characters">&eacute;<span class="sf-dump-default">\\x00</span>test<span class="sf-dump-default">\\t</span><span class="sf-dump-default sf-dump-ns">\\n</span></span>
69+
<span class=sf-dump-str title="11 binary or non-UTF-8 characters">ing</span>
70+
"""
6871
"<span class=sf-dump-key>[]</span>" => []
6972
"<span class=sf-dump-key>res</span>" => <span class=sf-dump-note>stream resource</span> <a class=sf-dump-ref>@{$res}</a><samp>
7073
%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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class DumbFoo
1717
$var = array(
1818
'number' => 1, null,
1919
'const' => 1.1, true, false, NAN, INF, -INF, PHP_INT_MAX,
20-
'str' => "déjà\n", "\xE9\x00",
20+
'str' => "déjà\n", "\xE9\x00test\t\ning",
2121
'[]' => array(),
2222
'res' => $g,
2323
'obj' => $foo,

0 commit comments

Comments
 (0)