8000 [Var-Dumper] added feature to set default nodes collapsed · symfony/symfony@9a17127 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9a17127

Browse files
committed
[Var-Dumper] added feature to set default nodes collapsed
modify js htmldumper and add attr in root node
1 parent bb2727a commit 9a17127

File tree

3 files changed

+45
-9
lines changed

3 files changed

+45
-9
lines changed

src/Symfony/Bridge/Twig/Tests/Extension/DumpExtensionTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,17 @@ public function getDumpArgs()
8686
{
8787
return array(
8888
array(array(), array(), '', false),
89-
array(array(), array(), "<pre class=sf-dump id=sf-dump data-indent-pad=\" \">[]\n</pre><script>Sfdump(\"sf-dump\")</script>\n"),
89+
array(array(), array(), "<pre class=sf-dump id=sf-dump data-indent-pad=\" \" data-collapsed-by-default-nodes-higher-than=\"1\">[]\n</pre><script>Sfdump(\"sf-dump\")</script>\n"),
9090
array(
9191
array(),
9292
array(123, 456),
93-
"<pre class=sf-dump id=sf-dump data-indent-pad=\" \"><span class=sf-dump-num>123</span>\n</pre><script>Sfdump(\"sf-dump\")</script>\n"
94-
."<pre class=sf-dump id=sf-dump data-indent-pad=\" \"><span class=sf-dump-num>456</span>\n</pre><script>Sfdump(\"sf-dump\")</script>\n",
93+
"<pre class=sf-dump id=sf-dump data-indent-pad=\" \" data-collapsed-by-default-nodes-higher-than=\"1\"><span class=sf-dump-num>123</span>\n</pre><script>Sfdump(\"sf-dump\")</script>\n"
94+
."<pre class=sf-dump id=sf-dump data-indent-pad=\" \" data-collapsed-by-default-nodes-higher-than=\"1\"><span class=sf-dump-num>456</span>\n</pre><script>Sfdump(\"sf-dump\")</script>\n",
9595
),
9696
array(
9797
array('foo' => 'bar'),
9898
array(),
99-
"<pre class=sf-dump id=sf-dump data-indent-pad=\" \"><span class=sf-dump-note>array:1</span> [<samp>\n"
99+
"<pre class=sf-dump id=sf-dump data-indent-pad=\" \" data-collapsed-by-default-nodes-higher-than=\"1\"><span class=sf-dump-note>array:1</span> [<samp>\n"
100100
." \"<span class=sf-dump-key>foo</span>\" => \"<span class=sf-dump-str title=\"3 characters\">bar</span>\"\n"
101101
."</samp>]\n"
102102
."</pre><script>Sfdump(\"sf-dump\")</script>\n",

src/Symfony/Component/HttpKernel/Tests/DataCollector/DumpDataCollectorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function testDump()
4040

4141
$xDump = array(
4242
array(
43-
'data' => "<pre class=sf-dump id=sf-dump data-indent-pad=\" \"><span class=sf-dump-num>123</span>\n</pre><script>Sfdump(\"sf-dump\")</script>\n",
43+
'data' => "<pre class=sf-dump id=sf-dump data-indent-pad=\" \" data-collapsed-by-default-nodes-higher-than=\"1\"><span class=sf-dump-num>123</span>\n</pre><script>Sfdump(\"sf-dump\")</script>\n",
4444
'name' => 'DumpDataCollectorTest.php',
4545
'file' => __FILE__,
4646
'line' => $line,
@@ -86,7 +86,7 @@ public function testCollectHtml()
8686
$line = __LINE__ - 1;
8787
$file = __FILE__;
8888
$xOutput = <<<EOTXT
89-
<pre class=sf-dump id=sf-dump data-indent-pad=" "><a href="test://{$file}:{$line}" title="{$file}"><span class=sf-dump-meta>DumpDataCollectorTest.php</span></a> on line <span class=sf-dump-meta>{$line}</span>:
89+
<pre class=sf-dump id=sf-dump data-indent-pad=" " data-collapsed-by-default-nodes-higher-than="1"><a href="test://{$file}:{$line}" title="{$file}"><span class=sf-dump-meta>DumpDataCollectorTest.php</span></a> on line <span class=sf-dump-meta>{$line}</span>:
9090
<span class=sf-dump-num>123</span>
9191
</pre>
9292

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

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class HtmlDumper extends CliDumper
2424
public static $defaultOutput = 'php://output';
2525

2626
protected $dumpHeader;
27-
protected $dumpPrefix = '<pre class=sf-dump id=%s data-indent-pad="%s">';
27+
protected $dumpPrefix = '<pre class=sf-dump id=%s data-indent-pad="%s" data-collapsed-by-default-nodes-higher-than="%s">';
2828
protected $dumpSuffix = '</pre><script>Sfdump("%s")</script>';
2929
protected $dumpId = 'sf-dump';
3030
protected $colors = true;
@@ -45,6 +45,10 @@ class HtmlDumper extends CliDumper
4545
'index' => 'color:#1299DA',
4646
);
4747

48+
protected $jsProperties = array(
49+
'collapsedByDefaultNodesHigherThan' => 1
50+
);
51+
4852
/**
4953
* {@inheritdoc}
5054
*/
@@ -75,6 +79,16 @@ public function setStyles(array $styles)
7579
$this->styles = $styles + $this->styles;
7680
}
7781

82+
/**
83+
* Configures js properties.
84+
*
85+
* @param array $jsProperties A map of jsProperties names to customize the behavior.
86+
*/
87+
public function setJsProperties(array $jsProperties)
88+
{
89+
$this->jsProperties = $jsProperties;
90+
}
91+
7892
/**
7993
* Sets an HTML header that will be dumped once in the output stream.
8094
*
@@ -119,6 +133,7 @@ protected function getDumpHeader()
119133

120134
$line = <<<'EOHTML'
121135
<script>
136+
122137
Sfdump = window.Sfdump || (function (doc) {
123138
124139
var refStyle = doc.createElement('style'),
@@ -175,6 +190,7 @@ function toggle(a, recursive) {
175190
176191
return function (root) {
177192
root = doc.getElementById(root);
193+
var collapsedByDefaultNodesHigherThan = root.getAttribute('data-collapsed-by-default-nodes-higher-than');
178194
179195
function a(e, f) {
180196
addEventListener(root, e, function (e) {
@@ -252,6 +268,19 @@ function isCtrlKey(e) {
252268
len = t.length;
253269
i = t = 0;
254270
271+
function getLevelNodeFromRoot(node, level) {
272+
level = level || 0;
273+
level++;
274+
275+
node = node.parentNode;
276+
277+
if ('sf-dump' == node.className) {
278+
return level;
279+
}
280+
281+
return getLevelNodeFromRoot(node, level);
282+
}
283+
255284
while (i < len) {
256285
elt = root[i];
257286
if ("SAMP" == elt.tagName) {
@@ -267,9 +296,11 @@ function isCtrlKey(e) {
267296
a.title = (a.title ? a.title+'\n[' : '[')+keyHint+'+click] Expand all children';
268297
a.innerHTML += '<span>▼</span>';
269298
a.className += ' sf-dump-toggle';
270-
if ('sf-dump' != elt.parentNode.className) {
299+
300+
if (getLevelNodeFromRoot(elt) > collapsedByDefaultNodesHigherThan) {
271301
toggle(a);
272302
}
303+
273304
} else if ("sf-dump-ref" == elt.className && (a = elt.getAttribute('href'))) {
274305
a = a.substr(1);
275306
elt.className += ' '+a;
@@ -431,7 +462,12 @@ protected function style($style, $value, $attr = array())
431462
protected function dumpLine($depth, $endOfValue = false)
432463
{
433464
if (-1 === $this->lastDepth) {
434-
$this->line = sprintf($this->dumpPrefix, $this->dumpId, $this->indentPad).$this->line;
465+
$this->line = sprintf(
466+
$this->dumpPrefix,
467+
$this->dumpId,
468+
$this->indentPad,
469+
$this->jsProperties['collapsedByDefaultNodesHigherThan']
470+
) . $this->line;
435471
}
436472
if (!$this->headerIsDumped) {
437473
$this->line = $this->getDumpHeader().$this->line;

0 commit comments

Comments
 (0)
0