Skip to content

Commit 76791fe

Browse files
committed
feature symfony#18948 [VarDumper] Add maxDepth & maxStringLength display options (MGDSoft, nicolas-grekas)
This PR was merged into the 3.2-dev branch. Discussion ---------- [VarDumper] Add maxDepth & maxStringLength display options | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#16741 | License | MIT | Doc PR | - Takes over symfony#18148 to add display options to html dumps. Status: needs work Commits ------- 998ff33 [VarDumper] Tweak display options implementation 58eb665 [VarDumper] Add maxDepth & maxStringLength display options
2 parents 06f5c86 + 998ff33 commit 76791fe

File tree

1 file changed

+86
-19
lines changed

1 file changed

+86
-19
lines changed

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

Lines changed: 86 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class HtmlDumper extends CliDumper
2525

2626
protected $dumpHeader;
2727
protected $dumpPrefix = '<pre class=sf-dump id=%s data-indent-pad="%s">';
28-
protected $dumpSuffix = '</pre><script>Sfdump("%s")</script>';
28+
protected $dumpSuffix = '</pre><script>Sfdump(%s)</script>';
2929
protected $dumpId = 'sf-dump';
3030
protected $colors = true;
3131
protected $headerIsDumped = false;
@@ -45,6 +45,12 @@ class HtmlDumper extends CliDumper
4545
'index' => 'color:#1299DA',
4646
);
4747

48+
private $displayOptions = array(
49+
'maxDepth' => 1,
50+
'maxStringLength' => 160,
51+
);
52+
private $extraDisplayOptions = array();
53+
4854
/**
4955
* {@inheritdoc}
5056
*/
@@ -75,6 +81,17 @@ public function setStyles(array $styles)
7581
$this->styles = $styles + $this->styles;
7682
}
7783

84+
/**
85+
* Configures display options.
86+
*
87+
* @param array $displayOptions A map of display options to customize the behavior.
88+
*/
89+
public function setDisplayOptions(array $displayOptions)
90+
{
91+
$this->headerIsDumped = false;
92+
$this->displayOptions = $displayOptions + $this->displayOptions;
93+
}
94+
7895
/**
7996
* Sets an HTML header that will be dumped once in the output stream.
8097
*
@@ -100,8 +117,9 @@ public function setDumpBoundaries($prefix, $suffix)
100117
/**
101118
* {@inheritdoc}
102119
*/
103-
public function dump(Data $data, $output = null)
120+
public function dump(Data $data, $output = null, array $extraDisplayOptions = array())
104121
{
122+
$this->extraDisplayOptions = $extraDisplayOptions;
105123
parent::dump($data, $output);
106124
$this->dumpId = 'sf-dump-'.mt_rand();
107125
}
@@ -117,7 +135,7 @@ protected function getDumpHeader()
117135
return $this->dumpHeader;
118136
}
119137

120-
$line = <<<'EOHTML'
138+
$line = str_replace('{$options}', json_encode($this->displayOptions, JSON_FORCE_OBJECT), <<<'EOHTML'
121139
<script>
122140
Sfdump = window.Sfdump || (function (doc) {
123141
@@ -173,7 +191,7 @@ function toggle(a, recursive) {
173191
return true;
174192
};
175193
176-
return function (root) {
194+
return function (root, x) {
177195
root = doc.getElementById(root);
178196
179197
function a(e, f) {
@@ -231,31 +249,37 @@ function isCtrlKey(e) {
231249
} else {
232250
doc.selection.empty();
233251
}
252+
} else if (/\bsf-dump-str-toggle\b/.test(a.className)) {
253+
e.preventDefault();
254+
e = a.parentNode.parentNode;
255+
e.className = e.className.replace(/sf-dump-str-(expand|collapse)/, a.parentNode.className);
234256
}
235257
});
236258
237259
var indentRx = new RegExp('^('+(root.getAttribute('data-indent-pad') || ' ').replace(rxEsc, '\\$1')+')+', 'm'),
260+
options = {$options},
238261
elt = root.getElementsByTagName('A'),
239262
len = elt.length,
240-
i = 0,
263+
i = 0, s, h,
241264
t = [];
242265
243266
while (i < len) t.push(elt[i++]);
244267
268+
for (i in x) {
269+
options[i] = x[i];
270+
}
271+
245272
elt = root.getElementsByTagName('SAMP');
246273
len = elt.length;
247274
i = 0;
248275
249276
while (i < len) t.push(elt[i++]);
250-
251-
root = t;
252277
len = t.length;
253-
i = t = 0;
254278
255-
while (i < len) {
256-
elt = root[i];
257-
if ("SAMP" == elt.tagName) {
258-
elt.className = "sf-dump-expanded";
279+
for (i = 0; i < len; ++i) {
280+
elt = t[i];
281+
if ('SAMP' == elt.tagName) {
282+
elt.className = 'sf-dump-expanded';
259283
a = elt.previousSibling || {};
260284
if ('A' != a.tagName) {
261285
a = doc.createElement('A');
@@ -267,19 +291,24 @@ function isCtrlKey(e) {
267291
a.title = (a.title ? a.title+'\n[' : '[')+keyHint+'+click] Expand all children';
268292
a.innerHTML += '<span>▼</span>';
269293
a.className += ' sf-dump-toggle';
294+
x = 1;
270295
if ('sf-dump' != elt.parentNode.className) {
271-
toggle(a);
296+
x += elt.parentNode.getAttribute('data-depth')/1;
297+
if (x > options.maxDepth) {
298+
toggle(a);
299+
}
272300
}
273-
} else if ("sf-dump-ref" == elt.className && (a = elt.getAttribute('href'))) {
301+
elt.setAttribute('data-depth', x);
302+
} else if ('sf-dump-ref' == elt.className && (a = elt.getAttribute('href'))) {
274303
a = a.substr(1);
275304
elt.className += ' '+a;
276305
277306
if (/[\[{]$/.test(elt.previousSibling.nodeValue)) {
278307
a = a != elt.nextSibling.id && doc.getElementById(a);
279308
try {
280-
t = a.nextSibling;
309+
s = a.nextSibling;
281310
elt.appendChild(a);
282-
t.parentNode.insertBefore(a, t);
311+
s.parentNode.insertBefore(a, s);
283312
if (/^[@#]/.test(elt.innerHTML)) {
284313
elt.innerHTML += ' <span>▶</span>';
285314
} else {
@@ -295,7 +324,33 @@ function isCtrlKey(e) {
295324
}
296325
}
297326
}
298-
++i;
327+
}
328+
329+
if (0 >= options.maxStringLength) {
330+
return;
331+
}
332+
try {
333+
elt = root.querySelectorAll('.sf-dump-str');
334+
len = elt.length;
335+
i = 0;
336+
t = [];
337+
338+
while (i < len) t.push(elt[i++]);
339+
len = t.length;
340+
341+
for (i = 0; i < len; ++i) {
342+
elt = t[i];
343+
s = elt.innerText || elt.textContent;
344+
x = s.length - options.maxStringLength;
345+
if (0 < x) {
346+
h = elt.innerHTML;
347+
elt[elt.innerText ? 'innerText' : 'textContent'] = s.substring(0, options.maxStringLength);
348+
elt.className += ' sf-dump-str-collapse';
349+
elt.innerHTML = '<span class=sf-dump-str-collapse>'+h+'<a class="sf-dump-ref sf-dump-str-toggle" title="Collapse"> ◀</a></span>'+
350+
'<span class=sf-dump-str-expand>'+elt.innerHTML+'<a class="sf-dump-ref sf-dump-str-toggle" title="'+x+' remaining characters"> ▶</a></span>';
351+
}
352+
}
353+
} catch (e) {
299354
}
300355
};
301356
@@ -324,7 +379,14 @@ function isCtrlKey(e) {
324379
border: 0;
325380
outline: none;
326381
}
327-
EOHTML;
382+
.sf-dump-str-collapse .sf-dump-str-collapse {
383+
display: none;
384+
}
385+
.sf-dump-str-expand .sf-dump-str-expand {
386+
display: none;
387+
}
388+
EOHTML
389+
);
328390

329391
foreach ($this->styles as $class => $style) {
330392
$line .= 'pre.sf-dump'.('default' !== $class ? ' .sf-dump-'.$class : '').'{'.$style.'}';
@@ -438,7 +500,12 @@ protected function dumpLine($depth, $endOfValue = false)
438500
}
439501

440502
if (-1 === $depth) {
441-
$this->line .= sprintf($this->dumpSuffix, $this->dumpId);
503+
$args = array('"'.$this->dumpId.'"');
504+
if ($this->extraDisplayOptions) {
505+
$args[] = json_encode($this->extraDisplayOptions, JSON_FORCE_OBJECT);
506+
}
507+
// Replace is for BC
508+
$this->line .= sprintf(str_replace('"%s"', '%s', $this->dumpSuffix), implode(', ', $args));
442509
}
443510
$this->lastDepth = $depth;
444511

0 commit comments

Comments
 (0)