-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
@@ -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%>'; | ||
protected $dumpSuffix = '</pre><script>Sfjs.dump.instrument()</script>'; | ||
protected $dumpId = 'sf-dump'; | ||
protected $colors = true; | ||
protected $headerIsDumped = false; | ||
protected $lastDepth = -1; | ||
|
@@ -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. | ||
*/ | ||
|
@@ -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' | ||
|
@@ -129,7 +140,7 @@ protected function getDumpHeader() | |
}; | ||
</script> | ||
<style> | ||
#sf-dump { | ||
#%id% { | ||
display: block; | ||
background-color: #300a24; | ||
white-space: pre; | ||
|
@@ -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}}"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just for the reader wondering why, I used |
||
} | ||
|
||
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); | ||
} | ||
|
||
/** | ||
|
@@ -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>"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Having different ids is great, why is it also required for names? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @nicolas-grekas the name of the anchor is used to reference it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh I didn't see that, it's a cool feature! There was a problem hiding this comment. Choose a reason for hiding this commentThe 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>"; | ||
} | ||
} | ||
|
||
|
@@ -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; | ||
|
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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-]