From 7d0cee175a1cf1a198ef966062b3eafd995b7f56 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Mon, 19 Sep 2016 10:40:17 +0200 Subject: [PATCH] [VarDumper] Fix small ClassStub rendering issues --- .../views/Profiler/profiler.css.twig | 11 ++- .../Component/VarDumper/Dumper/HtmlDumper.php | 80 ++++++++++--------- .../Tests/Caster/ExceptionCasterTest.php | 6 +- .../VarDumper/Tests/Caster/StubCasterTest.php | 14 ++-- .../VarDumper/Tests/HtmlDumperTest.php | 7 +- 5 files changed, 63 insertions(+), 55 deletions(-) diff --git a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/profiler.css.twig b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/profiler.css.twig index d2c66721ce640..1275a62f0c0ea 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/profiler.css.twig +++ b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/profiler.css.twig @@ -881,7 +881,11 @@ table.logs .metadata { margin: 0.5em 0; } -#collector-content pre.sf-dump { color: #CC7832; } +#collector-content pre.sf-dump, +#collector-content .sf-dump-default { + color: #CC7832; + background: none; +} #collector-content .sf-dump-str { color: #629755; } #collector-content .sf-dump-private, #collector-content .sf-dump-protected, @@ -893,7 +897,6 @@ table.logs .metadata { #collector-content .sf-dump { margin: 0; padding: 0; - background: none; line-height: 1.4; } @@ -904,10 +907,6 @@ table.logs .metadata { line-height: inherit; } -#collector-content .dump-inline pre.sf-dump .sf-dump-ellipsis { - width: 4em; -} - #collector-content .sf-dump .trace { font-size: 12px; } diff --git a/src/Symfony/Component/VarDumper/Dumper/HtmlDumper.php b/src/Symfony/Component/VarDumper/Dumper/HtmlDumper.php index ad6fb2a21f411..1b9c6b602c2f4 100644 --- a/src/Symfony/Component/VarDumper/Dumper/HtmlDumper.php +++ b/src/Symfony/Component/VarDumper/Dumper/HtmlDumper.php @@ -59,6 +59,7 @@ public function __construct($output = null, $charset = null, $flags = 0) { AbstractDumper::__construct($output, $charset, $flags); $this->dumpId = 'sf-dump-'.mt_rand(); + $this->displayOptions['fileLinkFormat'] = ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format'); } /** @@ -189,7 +190,7 @@ function toggle(a, recursive) { options = {$options}, elt = root.getElementsByTagName('A'), len = elt.length, - i = 0, s, h, fmt, + i = 0, s, h, t = []; while (i < len) t.push(elt[i++]); @@ -197,10 +198,6 @@ function toggle(a, recursive) { for (i in x) { options[i] = x[i]; } - fmt = options.fileLinkFormat; - if (fmt && 'string' == typeof fmt) { - fmt = [fmt]; - } function a(e, f) { addEventListener(root, e, function (e) { @@ -221,8 +218,10 @@ function isCtrlKey(e) { refStyle.innerHTML = ''; } }); - a('mouseover', function (a) { - if (a = idRx.exec(a.className)) { + a('mouseover', function (a, e, c) { + if (c) { + e.target.style.cursor = "pointer"; + } else if (a = idRx.exec(a.className)) { try { refStyle.innerHTML = 'pre.sf-dump .'+a[0]+'{background-color: #B729D9; color: #FFF !important; border-radius: 2px}'; } catch (e) { @@ -321,16 +320,6 @@ function isCtrlKey(e) { } } } - } else if (fmt && (a = elt.getAttribute('data-file'))) { - if (fmt[1]) { - for (x in fmt[1]) { - if (0 === a.indexOf(x)) { - a = fmt[1][x] + a.substr(x.length); - break; - } - } - } - elt.href = fmt[0].replace('%l', elt.getAttribute('data-line')).replace('%f', a); } } @@ -391,7 +380,7 @@ function isCtrlKey(e) { display: inline-block; overflow: visible; text-overflow: ellipsis; - width: 50px; + width: 5em; white-space: nowrap; overflow: hidden; vertical-align: top; @@ -411,7 +400,7 @@ function isCtrlKey(e) { ); foreach ($this->styles as $class => $style) { - $line .= 'pre.sf-dump'.('default' !== $class ? ' .sf-dump-'.$class : '').'{'.$style.'}'; + $line .= 'pre.sf-dump'.('default' === $class ? ', pre.sf-dump' : '').' .sf-dump-'.$class.'{'.$style.'}'; } return $this->dumpHeader = preg_replace('/\s+/', ' ', $line).''.$this->dumpHeader; @@ -485,37 +474,27 @@ protected function style($style, $value, $attr = array()) $style .= sprintf(' title="Private property defined in class: `%s`"', esc($this->utf8Encode($attr['class']))); } $map = static::$controlCharsMap; - $style = ""; if (isset($attr['ellipsis'])) { $label = esc(substr($value, -$attr['ellipsis'])); - - $v = sprintf('%s%2$s%s%1$s', $style, substr($v, 0, -strlen($label)), $label); + $style = str_replace(' title="', " title=\"$v\n", $style); + $v = sprintf('%s%s', substr($v, 0, -strlen($label)), $label); } - $v = preg_replace_callback(static::$controlCharsRx, function ($c) use ($map, $style) { - $s = ''; + $v = "".preg_replace_callback(static::$controlCharsRx, function ($c) use ($map) { + $s = ''; $c = $c[$i = 0]; do { $s .= isset($map[$c[$i]]) ? $map[$c[$i]] : sprintf('\x%02X', ord($c[$i])); } while (isset($c[++$i])); - return $s.$style; - }, $v, -1, $cchrCount); + return $s.''; + }, $v).''; - if ('<' === $v[0]) { - $v = substr($v, 7); - } else { - $v = $style.$v; - } - if ('>' === substr($v, -1)) { - $v = substr($v, 0, -strlen($style)); - } else { - $v .= ''; + if (isset($attr['file']) && $href = $this->getSourceLink($attr['file'], isset($attr['line']) ? $attr['line'] : 0)) { + $attr['href'] = $href; } - if (isset($attr['file'])) { - $v = sprintf('%s', esc($this->utf8Encode($attr['file'])), isset($attr['line']) ? $attr['line'] : 1, $v); - } elseif (isset($attr['href'])) { + if (isset($attr['href'])) { $v = sprintf('%s', esc($this->utf8Encode($attr['href'])), $v); } if (isset($attr['lang'])) { @@ -554,6 +533,31 @@ protected function dumpLine($depth, $endOfValue = false) } AbstractDumper::dumpLine($depth); } + + private function getSourceLink($file, $line) + { + $fileLinkFormat = $this->extraDisplayOptions + $this->displayOptions; + + if (!$fileLinkFormat = $fileLinkFormat['fileLinkFormat']) { + return false; + } + if (!is_array($fileLinkFormat)) { + $i = max(strpos($fileLinkFormat, '%f'), strpos($fileLinkFormat, '%l')); + $i = strpos($fileLinkFormat, '#"', $i) ?: strlen($fileLinkFormat); + $fileLinkFormat = array(substr($fileLinkFormat, 0, $i), substr($fileLinkFormat, $i + 1)); + $fileLinkFormat[1] = @json_decode('{'.$fileLinkFormat[1].'}', true) ?: array(); + $this->extraDisplayOptions['fileLinkFormat'] = $fileLinkFormat; + } + + foreach ($fileLinkFormat[1] as $k => $v) { + if (0 === strpos($file, $k)) { + $file = substr_replace($file, $v, 0, strlen($k)); + break; + } + } + + return strtr($fileLinkFormat[0], array('%f' => $file, '%l' => $line)); + } } function esc($str) diff --git a/src/Symfony/Component/VarDumper/Tests/Caster/ExceptionCasterTest.php b/src/Symfony/Component/VarDumper/Tests/Caster/ExceptionCasterTest.php index 24d045ccb0eb9..ca5099dd76121 100644 --- a/src/Symfony/Component/VarDumper/Tests/Caster/ExceptionCasterTest.php +++ b/src/Symfony/Component/VarDumper/Tests/Caster/ExceptionCasterTest.php @@ -150,10 +150,12 @@ public function testHtmlDump() Exception { #message: "foo" #code: 0 - #file: "%sTests%eCaster%eExceptionCasterTest.php" + #file: "%sTests%eCaster%eExceptionCasterTest.php" #line: 25 -trace: { - %sVarDumper%eTests%eCaster%eExceptionCasterTest.php: 25 + %sVarDumper%eTests%eCaster%eExceptionCasterTest.php: 25 …12 } } diff --git a/src/Symfony/Component/VarDumper/Tests/Caster/StubCasterTest.php b/src/Symfony/Component/VarDumper/Tests/Caster/StubCasterTest.php index 6837a4dac6b00..0fe96d39e38f0 100644 --- a/src/Symfony/Component/VarDumper/Tests/Caster/StubCasterTest.php +++ b/src/Symfony/Component/VarDumper/Tests/Caster/StubCasterTest.php @@ -94,11 +94,12 @@ public function testLinkStub() $dumper = new HtmlDumper(); $dumper->setDumpHeader(''); $dumper->setDumpBoundaries('', ''); + $dumper->setDisplayOptions(array('fileLinkFormat' => '%f:%l')); $dump = $dumper->dump($cloner->cloneVar($var), true); $expectedDump = <<<'EODUMP' array:1 [ - 0 => "Symfony\Component\VarDumper\Tests\Caster\StubCasterTest" + 0 => "Symfony\Component\VarDumper\Tests\Caster\StubCasterTest" ] EODUMP; @@ -114,11 +115,11 @@ public function testClassStub() $dumper = new HtmlDumper(); $dumper->setDumpHeader(''); $dumper->setDumpBoundaries('', ''); - $dump = $dumper->dump($cloner->cloneVar($var), true); + $dump = $dumper->dump($cloner->cloneVar($var), true, array('fileLinkFormat' => '%f:%l')); $expectedDump = <<<'EODUMP' array:1 [ - 0 => "hello" + 0 => "hello" ] EODUMP; @@ -138,7 +139,8 @@ public function testClassStubWithNotExistingClass() $expectedDump = <<<'EODUMP' array:1 [ - 0 => "Symfony\Component\VarDumper\Tests\Caster\NotExisting" + 0 => "Symfony\Component\VarDumper\Tests\Caster\NotExisting" ] EODUMP; @@ -154,11 +156,11 @@ public function testClassStubWithNotExistingMethod() $dumper = new HtmlDumper(); $dumper->setDumpHeader(''); $dumper->setDumpBoundaries('', ''); - $dump = $dumper->dump($cloner->cloneVar($var), true); + $dump = $dumper->dump($cloner->cloneVar($var), true, array('fileLinkFormat' => '%f:%l')); $expectedDump = <<<'EODUMP' array:1 [ - 0 => "hello" + 0 => "hello" ] EODUMP; diff --git a/src/Symfony/Component/VarDumper/Tests/HtmlDumperTest.php b/src/Symfony/Component/VarDumper/Tests/HtmlDumperTest.php index dd9092909a547..d4985d949b780 100644 --- a/src/Symfony/Component/VarDumper/Tests/HtmlDumperTest.php +++ b/src/Symfony/Component/VarDumper/Tests/HtmlDumperTest.php @@ -59,8 +59,8 @@ public function testGet() 4 => INF 5 => -INF 6 => {$intMax} - "str" => "d&%s;j&%s;\\n" - 7 => b"&%s;\\x00" + "str" => "d&%s;j&%s;\\n" + 7 => b"&%s;\\x00" "[]" => [] "res" => stream resource @{$res} %A wrapper_type: "plainfile" @@ -84,7 +84,8 @@ public function testGet() default: null } } - file: "%sTests%eFixtures%edumb-var.php" + file: "%sTests%eFixtures%edumb-var.php" line: "{$var['line']} to {$var['line']}" } "line" => {$var['line']}