Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[VarDumper] Add maxDepth & maxStringLength display options #18948

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 2 commits into from
Jun 28, 2016
Merged
Changes from 1 commit
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
Prev Previous commit
[VarDumper] Tweak display options implementation
  • Loading branch information
nicolas-grekas committed Jun 25, 2016
commit 998ff33e8931a0ed10ffc54a5135f9ba91093a29
180 changes: 74 additions & 106 deletions 180 src/Symfony/Component/VarDumper/Dumper/HtmlDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class HtmlDumper extends CliDumper

protected $dumpHeader;
protected $dumpPrefix = '<pre class=sf-dump id=%s data-indent-pad="%s">';
protected $dumpSuffix = '</pre><script>Sfdump("%s"%s)</script>';
protected $dumpSuffix = '</pre><script>Sfdump(%s)</script>';
protected $dumpId = 'sf-dump';
protected $colors = true;
protected $headerIsDumped = false;
Expand All @@ -43,16 +43,13 @@ class HtmlDumper extends CliDumper
'meta' => 'color:#B729D9',
'key' => 'color:#56DB3A',
'index' => 'color:#1299DA',
'str .max-string-length b' => 'color:#A0A0A0',
);

protected $displayOptions = array(
'initDepth' => 1,
private $displayOptions = array(
'maxDepth' => 1,
'maxStringLength' => 160,
);

protected $displayOptionsIsUpdated = false;
private $extraDisplayOptions = array();

/**
* {@inheritdoc}
Expand Down Expand Up @@ -87,15 +84,12 @@ public function setStyles(array $styles)
/**
* Configures display options.
*
* @param array $displayOptions A map of displayOptions names to customize the behavior.
* @param array $displayOptions A map of display options to customize the behavior.
Copy link
Member

Choose a reason for hiding this comment

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

no dot at the end

Copy link
Member

Choose a reason for hiding this comment

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

ahah, you've been doing that for a long time, working on a PR to fix that :)

*/
public function setDisplayOptions(array $displayOptions)
{
if ($displayOptions)
{
$this->displayOptionsIsUpdated = true;
$this->displayOptions = $displayOptions + $this->displayOptions;
}
$this->headerIsDumped = false;
$this->displayOptions = $displayOptions + $this->displayOptions;
}

/**
Expand Down Expand Up @@ -123,9 +117,9 @@ public function setDumpBoundaries($prefix, $suffix)
/**
* {@inheritdoc}
*/
public function dump(Data $data, $output = null, array $displayOptions = [])
public function dump(Data $data, $output = null, array $extraDisplayOptions = array())
{
$this->setDisplayOptions($displayOptions);
$this->extraDisplayOptions = $extraDisplayOptions;
parent::dump($data, $output);
$this->dumpId = 'sf-dump-'.mt_rand();
}
Expand All @@ -141,7 +135,7 @@ protected function getDumpHeader()
return $this->dumpHeader;
}

$line = <<<'EOHTML'
$line = str_replace('{$options}', json_encode($this->displayOptions, JSON_FORCE_OBJECT), <<<'EOHTML'
<script>
Sfdump = window.Sfdump || (function (doc) {

Expand All @@ -165,8 +159,8 @@ protected function getDumpHeader()
};
}

function toggle(a, depth) {
var s = a.nextSibling || {}, oldClass = s.className, arrow, newClass, i;
function toggle(a, recursive) {
var s = a.nextSibling || {}, oldClass = s.className, arrow, newClass;

if ('sf-dump-compact' == oldClass) {
arrow = '▼';
Expand All @@ -181,21 +175,13 @@ function toggle(a, depth) {
a.lastChild.innerHTML = arrow;
s.className = newClass;

if (depth) {

if (recursive) {
try {
a = s.querySelectorAll('.'+oldClass);

for (i = 0; i < a.length; ++i) {
if (depth != 'ALL' && isNaN(depth) == false) {
if (getLevelNodeForParent(s, a[i]) >= depth) {
continue;
}
}

if (a[i].className !== newClass) {
a[i].className = newClass;
a[i].previousSibling.lastChild.innerHTML = arrow;
for (s = 0; s < a.length; ++s) {
if (a[s].className !== newClass) {
a[s].className = newClass;
a[s].previousSibling.lastChild.innerHTML = arrow;
}
}
} catch (e) {
Expand All @@ -205,26 +191,9 @@ function toggle(a, depth) {
return true;
};

function getLevelNodeForParent(parentNode, currentNode, level) {
level = level || 0;
level++;

if (parentNode.isSameNode(currentNode)) {
return level-1;
}

currentNode = currentNode.parentNode;

return getLevelNodeForParent(parentNode, currentNode, level);
}

return function (root, options) {
return function (root, x) {
root = doc.getElementById(root);
EOHTML;

$line .= 'options = options || '.json_encode($this->displayOptions).';';

$line .= <<<'EOHTML'
function a(e, f) {
addEventListener(root, e, function (e) {
if ('A' == e.target.tagName) {
Expand Down Expand Up @@ -253,8 +222,7 @@ function isCtrlKey(e) {
a('click', function (a, e) {
if (/\bsf-dump-toggle\b/.test(a.className)) {
e.preventDefault();
var maxDepth = isCtrlKey(e) ? 'ALL' : options.maxDepth ;
if (!toggle(a, maxDepth)) {
if (!toggle(a, isCtrlKey(e))) {
var r = doc.getElementById(a.getAttribute('href').substr(1)),
s = r.previousSibling,
f = r.parentNode,
Expand All @@ -268,7 +236,7 @@ function isCtrlKey(e) {
r.innerHTML = r.innerHTML.replace(new RegExp('^'+f[0].replace(rxEsc, '\\$1'), 'mg'), t[0]);
}
if ('sf-dump-compact' == r.className) {
toggle(s, maxDepth);
toggle(s, isCtrlKey(e));
}
}

Expand All @@ -281,31 +249,37 @@ function isCtrlKey(e) {
} else {
doc.selection.empty();
}
} else if (/\bsf-dump-str-toggle\b/.test(a.className)) {
e.preventDefault();
e = a.parentNode.parentNode;
e.className = e.className.replace(/sf-dump-str-(expand|collapse)/, a.parentNode.className);
}
});

var indentRx = new RegExp('^('+(root.getAttribute('data-indent-pad') || ' ').replace(rxEsc, '\\$1')+')+', 'm'),
options = {$options},
elt = root.getElementsByTagName('A'),
len = elt.length,
i = 0,
t = [],
temp;
i = 0, s, h,
t = [];

while (i < len) t.push(elt[i++]);

for (i in x) {
options[i] = x[i];
}

elt = root.getElementsByTagName('SAMP');
len = elt.length;
i = 0;

while (i < len) t.push(elt[i++]);

len = t.length;
i = 0;

while (i < len) {
for (i = 0; i < len; ++i) {
elt = t[i];
if ("SAMP" == elt.tagName) {
elt.className = "sf-dump-expanded";
if ('SAMP' == elt.tagName) {
elt.className = 'sf-dump-expanded';
a = elt.previousSibling || {};
if ('A' != a.tagName) {
a = doc.createElement('A');
Expand All @@ -317,19 +291,24 @@ function isCtrlKey(e) {
a.title = (a.title ? a.title+'\n[' : '[')+keyHint+'+click] Expand all children';
a.innerHTML += '<span>▼</span>';
a.className += ' sf-dump-toggle';
if (getLevelNodeForParent(root, a) > options.initDepth) {
toggle(a);
x = 1;
if ('sf-dump' != elt.parentNode.className) {
x += elt.parentNode.getAttribute('data-depth')/1;
if (x > options.maxDepth) {
toggle(a);
}
}
} else if ("sf-dump-ref" == elt.className && (a = elt.getAttribute('href'))) {
elt.setAttribute('data-depth', x);
} else if ('sf-dump-ref' == elt.className && (a = elt.getAttribute('href'))) {
a = a.substr(1);
elt.className += ' '+a;

if (/[\[{]$/.test(elt.previousSibling.nodeValue)) {
a = a != elt.nextSibling.id && doc.getElementById(a);
try {
t = a.nextSibling;
s = a.nextSibling;
elt.appendChild(a);
t.parentNode.insertBefore(a, t);
s.parentNode.insertBefore(a, s);
if (/^[@#]/.test(elt.innerHTML)) {
elt.innerHTML += ' <span>▶</span>';
} else {
Expand All @@ -345,43 +324,33 @@ function isCtrlKey(e) {
}
}
}
++i;
}

if (options.maxStringLength) {
configureMaxStringCollapse();
if (0 >= options.maxStringLength) {
return;
}
try {
elt = root.querySelectorAll('.sf-dump-str');
len = elt.length;
i = 0;
t = [];

function configureMaxStringCollapse()
{
var t = root.querySelectorAll('.sf-dump-str'), i = 0;
while (i < len) t.push(elt[i++]);
len = t.length;

for (i = 0; i < t.length; ++i) {
for (i = 0; i < len; ++i) {
elt = t[i];
if (elt.innerText.length > options.maxStringLength) {
elt.innerHTML = '<span class="max-string-length collapsed">' +
'<span class="collapsed">' + elt.innerText.substring(0, options.maxStringLength)+'...' +' <b>[+]</b></span>'+
'<span class="expanded">' + elt.innerHTML +' <b>[-]</b></span>' +
'</span>';
}
}

t = root.querySelectorAll('.max-string-length span b');

for (i = 0; i < t.length; ++i) {
t[i].addEventListener("click", function(e) {
toggleMaxStringLength(e.target.parentNode.parentNode);
});
}

function toggleMaxStringLength(elt) {

if (elt.className == 'max-string-length expanded') {
elt.className = 'max-string-length collapsed';
}else{
elt.className = 'max-string-length expanded';
s = elt.innerText || elt.textContent;
x = s.length - options.maxStringLength;
if (0 < x) {
h = elt.innerHTML;
elt[elt.innerText ? 'innerText' : 'textContent'] = s.substring(0, options.maxStringLength);
elt.className += ' sf-dump-str-collapse';
elt.innerHTML = '<span class=sf-dump-str-collapse>'+h+'<a class="sf-dump-ref sf-dump-str-toggle" title="Collapse"> ◀</a></span>'+
'<span class=sf-dump-str-expand>'+elt.innerHTML+'<a class="sf-dump-ref sf-dump-str-toggle" title="'+x+' remaining characters"> ▶</a></span>';
}
}
} catch (e) {
}
};

Expand Down Expand Up @@ -410,16 +379,14 @@ function toggleMaxStringLength(elt) {
border: 0;
outline: none;
}
pre.sf-dump .max-string-length.expanded .collapsed {
display:none;
}
pre.sf-dump .max-string-length.collapsed .expanded {
display:none
.sf-dump-str-collapse .sf-dump-str-collapse {
display: none;
}
pre.sf-dump .max-string-length b {
cursor: pointer;
.sf-dump-str-expand .sf-dump-str-expand {
display: none;
}
EOHTML;
EOHTML
);

foreach ($this->styles as $class => $style) {
$line .= 'pre.sf-dump'.('default' !== $class ? ' .sf-dump-'.$class : '').'{'.$style.'}';
Expand Down Expand Up @@ -533,11 +500,12 @@ protected function dumpLine($depth, $endOfValue = false)
}

if (-1 === $depth) {
$this->line .= sprintf(
$this->dumpSuffix,
$this->dumpId,
$this->displayOptionsIsUpdated ? ','.json_encode($this->displayOptions) : ''
);
$args = array('"'.$this->dumpId.'"');
if ($this->extraDisplayOptions) {
$args[] = json_encode($this->extraDisplayOptions, JSON_FORCE_OBJECT);
}
// Replace is for BC
$this->line .= sprintf(str_replace('"%s"', '%s', $this->dumpSuffix), implode(', ', $args));
}
$this->lastDepth = $depth;

Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.