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

Skip to content

[VarDumper] Improved dump in html #12109

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

Merged
merged 1 commit into from
Oct 3, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public function testDump($context, $args, $expectedOutput, $debug = true)
if ($debug) {
$this->assertStringStartsWith('<script>', $dump);
$dump = preg_replace('/^.*?<pre/', '<pre', $dump);
$dump = preg_replace('/sf-dump-\\d{2,}/', 'sf-dump', $dump);
}
$this->assertEquals($expectedOutput, $dump);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,21 @@ public function testDump()
$collector->dump($data); $line = __LINE__;
$this->assertSame(1, $collector->getDumpsCount());

$dump = $collector->getDumps('html');
$this->assertTrue(isset($dump[0]['data']));
$dump[0]['data'] = preg_replace('/^.*?<pre/', '<pre', $dump[0]['data']);
preg_match('/sf-dump-(\\d{2,})/', $dump[0]['data'], $matches);
$dumpId = $matches[1];

$xDump = array(
array(
'data' => "<pre id=sf-dump><span class=sf-dump-0><span class=sf-dump-num>123</span>\n</span></pre><script>Sfjs.dump.instrument()</script>\n",
'name' => 'DumpDataCollectorTest.php',
'file' => __FILE__,
'line' => $line,
'fileExcerpt' => false,
'data' => "<pre id=sf-dump-{$dumpId}><span class=sf-dump-0><span class=sf-dump-num>123</span>\n</span></pre><script>Sfjs.dump.instrument()</script>\n",
'name' => 'DumpDataCollectorTest.php',
'file' => __FILE__,
'line' => $line,
'fileExcerpt' => false,
),
);
$dump = $collector->getDumps('html');
$this->assertTrue(isset($dump[0]['data']));
$dump[0]['data'] = preg_replace('/^.*?<pre/', '<pre', $dump[0]['data']);
$this->assertSame($xDump, $dump);

$this->assertStringStartsWith(
Expand Down
41 changes: 27 additions & 14 deletions src/Symfony/Component/VarDumper/Dumper/HtmlDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\VarDumper\Dumper;

use Symfony\Component\VarDumper\Cloner\Cursor;
use Symfony\Component\VarDumper\Cloner\Data;

/**
* HtmlDumper dumps variables as HTML.
Expand All @@ -23,8 +24,9 @@ class HtmlDumper extends CliDumper
public static $defaultOutputStream = 'php://output';

protected $dumpHeader;
protected $dumpPrefix = '<pre id=sf-dump>';
protected $dumpPrefix = '<pre id=%id%>';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add quotes around the attribute (otherwise you need a more complex escaping)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generated id is safe, it contains only [a-z0-9-]

protected $dumpSuffix = '</pre><script>Sfjs.dump.instrument()</script>';
protected $dumpId = 'sf-dump';
protected $colors = true;
protected $headerIsDumped = false;
protected $lastDepth = -1;
Expand Down Expand Up @@ -82,6 +84,15 @@ public function setDumpBoundaries($prefix, $suffix)
$this->dumpSuffix = $suffix;
}

/**
* {@inheritdoc}
*/
public function dump(Data $data, $lineDumper = null)
{
$this->dumpId = 'sf-dump-'.mt_rand();
parent::dump($data, $lineDumper);
}

/**
* Dumps the HTML header.
*/
Expand All @@ -90,7 +101,7 @@ protected function getDumpHeader()
$this->headerIsDumped = true;

if (null !== $this->dumpHeader) {
return $this->dumpHeader;
return str_replace('%id%', $this->dumpId, $this->dumpHeader);
}

$line = <<<'EOHTML'
Expand Down Expand Up @@ -129,7 +140,7 @@ protected function getDumpHeader()
};
</script>
<style>
#sf-dump {
#%id% {
display: block;
background-color: #300a24;
white-space: pre;
Expand All @@ -138,31 +149,33 @@ protected function getDumpHeader()
font: 12px monospace, sans-serif;
padding: 5px;
}
#sf-dump span {
#%id% span {
display: inline;
}
#sf-dump .sf-dump-compact {
#%id% .sf-dump-compact {
display: none;
}
#sf-dump abbr {
#%id% abbr {
text-decoration: none;
border: none;
cursor: help;
}
#sf-dump a {
#%id% a {
text-decoration: none;
cursor: pointer;
}
#sf-dump a:hover {
#%id% a:hover {
text-decoration: underline;
}
EOHTML;

foreach ($this->styles as $class => $style) {
$line .= "#sf-dump .sf-dump-$class {{$style}}";
$line .= "#%id% .sf-dump-$class {{$style}}";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for the reader wondering why, I used #id css selectors to have the highest possible precedence.

}

return $this->dumpHeader = preg_replace('/\s+/', ' ', $line).'</style>'.$this->dumpHeader;
$this->dumpHeader = preg_replace('/\s+/', ' ', $line).'</style>'.$this->dumpHeader;

return str_replace('%id%', $this->dumpId, $this->dumpHeader);
}

/**
Expand Down Expand Up @@ -201,9 +214,9 @@ protected function style($style, $val)
if ('ref' === $style) {
$ref = substr($val, 1);
if ('#' === $val[0]) {
return "<a class=sf-dump-ref name=\"sf-dump-ref$ref\">$val</a>";
return "<a class=sf-dump-ref name=\"{$this->dumpId}-ref$ref\">$val</a>";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having different ids is great, why is it also required for names?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicolas-grekas the name of the anchor is used to reference it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I didn't see that, it's a cool feature!
Though, I had a similar idea, but maybe a little bit better: instead of opening the target of the link, we could just swap the children of the object and toggle the structure right where it is clicked on. That would make navigation even smoother.
If you're ok, I propose you revert this feature and keep this PR purelly on the id subject, then we can work on an other one with this ref displaying feature.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reverted

} else {
return "<a class=sf-dump-ref href=\"#sf-dump-ref$ref\">$val</a>";
return "<a class=sf-dump-ref href=\"#{$this->dumpId}-ref$ref\">$val</a>";
}
}

Expand Down Expand Up @@ -236,14 +249,14 @@ protected function dumpLine($depth)
}

if (-1 === $this->lastDepth) {
$this->line = $this->dumpPrefix.$this->line;
$this->line = str_replace('%id%', $this->dumpId, $this->dumpPrefix).$this->line;
}
if (!$this->headerIsDumped) {
$this->line = $this->getDumpHeader().$this->line;
}

if (-1 === $depth) {
$this->line .= $this->dumpSuffix;
$this->line .= str_replace('%id%', $this->dumpId, $this->dumpSuffix);
parent::dumpLine(0);
}
$this->lastDepth = $depth;
Expand Down
22 changes: 12 additions & 10 deletions src/Symfony/Component/VarDumper/Tests/HtmlDumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function testGet()
unset($a['uri']);

return $a;
}
},
));
$data = $cloner->cloneVar($var);

Expand All @@ -44,12 +44,14 @@ public function testGet()
$out = preg_replace('/[ \t]+$/m', '', $out);
$var['file'] = htmlspecialchars($var['file'], ENT_QUOTES, 'UTF-8');
$intMax = PHP_INT_MAX;
preg_match('/sf-dump-(\\d{2,})/', $out, $matches);
$dumpId = $matches[1];

$this->assertSame(
<<<EOTXT
<foo></foo><bar><span class=sf-dump-0><span class=sf-dump-note>array:25</span> [<span name=sf-dump-child>
<span class=sf-dump-1>"<span class=sf-dump-meta>number</span>" => <span class=sf-dump-num>1</span>
<span class=sf-dump-meta>0</span> => <span class=sf-dump-const>null</span> <a class=sf-dump-ref name="sf-dump-ref1">#1</a>
<span class=sf-dump-meta>0</span> => <span class=sf-dump-const>null</span> <a class=sf-dump-ref name="sf-dump-{$dumpId}-ref1">#1</a>
"<span class=sf-dump-meta>const</span>" => <span class=sf-dump-num>1.1</span>
<span class=sf-dump-meta>1</span> => <span class=sf-dump-const>true</span>
<span class=sf-dump-meta>2</span> => <span class=sf-dump-const>false</span>
Expand All @@ -72,7 +74,7 @@ public function testGet()
<span class=sf-dump-meta>options</span>: []
</span></span>}
<span class=sf-dump-meta>8</span> => resource:<span class=sf-dump-note>Unknown</span> {}
"<span class=sf-dump-meta>obj</span>" => <span class=sf-dump-note><abbr title="Symfony\Component\VarDumper\Tests\Fixture\DumbFoo" class=sf-dump-note>DumbFoo</abbr></span> {<span name=sf-dump-child> <a class=sf-dump-ref name="sf-dump-ref2">#2</a>
"<span class=sf-dump-meta>obj</span>" => <span class=sf-dump-note><abbr title="Symfony\Component\VarDumper\Tests\Fixture\DumbFoo" class=sf-dump-note>DumbFoo</abbr></span> {<span name=sf-dump-child> <a class=sf-dump-ref name="sf-dump-{$dumpId}-ref2">#2</a>
<span class=sf-dump-2><span class=sf-dump-public>foo</span>: "<span class=sf-dump-str>foo</span>"
"<span class=sf-dump-public>bar</span>": "<span class=sf-dump-str>bar</span>"
</span></span>}
Expand All @@ -90,15 +92,15 @@ public function testGet()
</span></span>}
"<span class=sf-dump-meta>line</span>" => <span class=sf-dump-num>{$var['line']}</span>
"<span class=sf-dump-meta>nobj</span>" => <span class=sf-dump-note>array:1</span> [<span name=sf-dump-child>
<span class=sf-dump-2><span class=sf-dump-meta>0</span> => {} <a class=sf-dump-ref name="sf-dump-ref3">#3</a>
<span class=sf-dump-2><span class=sf-dump-meta>0</span> => {} <a class=sf-dump-ref name="sf-dump-{$dumpId}-ref3">#3</a>
</span></span>]
"<span class=sf-dump-meta>recurs</span>" => <span class=sf-dump-note>array:1</span> [<span name=sf-dump-child> <a class=sf-dump-ref name="sf-dump-ref4">#4</a>
<span class=sf-dump-2><span class=sf-dump-meta>0</span> => <a class=sf-dump-ref href="#sf-dump-ref4">&4</a> <span class=sf-dump-note>array:1</span> [<a class=sf-dump-ref href="#sf-dump-ref4">@4</a>]
"<span class=sf-dump-meta>recurs</span>" => <span class=sf-dump-note>array:1</span> [<span name=sf-dump-child> <a class=sf-dump-ref name="sf-dump-{$dumpId}-ref4">#4</a>
<span class=sf-dump-2><span class=sf-dump-meta>0</span> => <a class=sf-dump-ref href="#sf-dump-{$dumpId}-ref4">&4</a> <span class=sf-dump-note>array:1</span> [<a class=sf-dump-ref href="#sf-dump-{$dumpId}-ref4">@4</a>]
</span></span>]
<span class=sf-dump-meta>9</span> => <a class=sf-dump-ref href="#sf-dump-ref1">&1</a> <span class=sf-dump-const>null</span>
"<span class=sf-dump-meta>sobj</span>" => <span class=sf-dump-note><abbr title="Symfony\Component\VarDumper\Tests\Fixture\DumbFoo" class=sf-dump-note>DumbFoo</abbr></span> {<a class=sf-dump-ref href="#sf-dump-ref2">@2</a>}
"<span class=sf-dump-meta>snobj</span>" => <a class=sf-dump-ref href="#sf-dump-ref3">&3</a> {<a class=sf-dump-ref href="#sf-dump-ref3">@3</a>}
"<span class=sf-dump-meta>snobj2</span>" => {<a class=sf-dump-ref href="#sf-dump-ref3">@3</a>}
<span class=sf-dump-meta>9</span> => <a class=sf-dump-ref href="#sf-dump-{$dumpId}-ref1">&1</a> <span class=sf-dump-const>null</span>
"<span class=sf-dump-meta>sobj</span>" => <span class=sf-dump-note><abbr title="Symfony\Component\VarDumper\Tests\Fixture\DumbFoo" class=sf-dump-note>DumbFoo</abbr></span> {<a class=sf-dump-ref href="#sf-dump-{$dumpId}-ref2">@2</a>}
"<span class=sf-dump-meta>snobj</span>" => <a class=sf-dump-ref href="#sf-dump-{$dumpId}-ref3">&3</a> {<a class=sf-dump-ref href="#sf-dump-{$dumpId}-ref3">@3</a>}
"<span class=sf-dump-meta>snobj2</span>" => {<a class=sf-dump-ref href="#sf-dump-{$dumpId}-ref3">@3</a>}
"<span class=sf-dump-meta>file</span>" => "<span class=sf-dump-str>{$var['file']}</span>"
b"<span class=sf-dump-meta>bin-key-é</span>" => ""
</span></span>]
Expand Down