@@ -25,7 +25,7 @@ class HtmlDumper extends CliDumper
25
25
26
26
protected $ dumpHeader ;
27
27
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> ' ;
29
29
protected $ dumpId = 'sf-dump ' ;
30
30
protected $ colors = true ;
31
31
protected $ headerIsDumped = false ;
@@ -45,6 +45,12 @@ class HtmlDumper extends CliDumper
45
45
'index ' => 'color:#1299DA ' ,
46
46
);
47
47
48
+ private $ displayOptions = array (
49
+ 'maxDepth ' => 1 ,
50
+ 'maxStringLength ' => 160 ,
51
+ );
52
+ private $ extraDisplayOptions = array ();
53
+
48
54
/**
49
55
* {@inheritdoc}
50
56
*/
@@ -75,6 +81,17 @@ public function setStyles(array $styles)
75
81
$ this ->styles = $ styles + $ this ->styles ;
76
82
}
77
83
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
+
78
95
/**
79
96
* Sets an HTML header that will be dumped once in the output stream.
80
97
*
@@ -100,8 +117,9 @@ public function setDumpBoundaries($prefix, $suffix)
100
117
/**
101
118
* {@inheritdoc}
102
119
*/
103
- public function dump (Data $ data , $ output = null )
120
+ public function dump (Data $ data , $ output = null , array $ extraDisplayOptions = array () )
104
121
{
122
+ $ this ->extraDisplayOptions = $ extraDisplayOptions ;
105
123
parent ::dump ($ data , $ output );
106
124
$ this ->dumpId = 'sf-dump- ' .mt_rand ();
107
125
}
@@ -117,7 +135,7 @@ protected function getDumpHeader()
117
135
return $ this ->dumpHeader ;
118
136
}
119
137
120
- $ line = <<<'EOHTML'
138
+ $ line = str_replace ( ' {$options} ' , json_encode ( $ this -> displayOptions , JSON_FORCE_OBJECT ), <<<'EOHTML'
121
139
<script>
122
140
Sfdump = window.Sfdump || (function (doc) {
123
141
@@ -173,7 +191,7 @@ function toggle(a, recursive) {
173
191
return true;
174
192
};
175
193
176
- return function (root) {
194
+ return function (root, x ) {
177
195
root = doc.getElementById(root);
178
196
179
197
function a(e, f) {
@@ -231,31 +249,37 @@ function isCtrlKey(e) {
231
249
} else {
232
250
doc.selection.empty();
233
251
}
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);
234
256
}
235
257
});
236
258
237
259
var indentRx = new RegExp('^('+(root.getAttribute('data-indent-pad') || ' ').replace(rxEsc, '\\$1')+')+', 'm'),
260
+ options = {$options},
238
261
elt = root.getElementsByTagName('A'),
239
262
len = elt.length,
240
- i = 0,
263
+ i = 0, s, h,
241
264
t = [];
242
265
243
266
while (i < len) t.push(elt[i++]);
244
267
268
+ for (i in x) {
269
+ options[i] = x[i];
270
+ }
271
+
245
272
elt = root.getElementsByTagName('SAMP');
246
273
len = elt.length;
247
274
i = 0;
248
275
249
276
while (i < len) t.push(elt[i++]);
250
-
251
- root = t;
252
277
len = t.length;
253
- i = t = 0;
254
278
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' ;
259
283
a = elt.previousSibling || {};
260
284
if ('A' != a.tagName) {
261
285
a = doc.createElement('A');
@@ -267,19 +291,24 @@ function isCtrlKey(e) {
267
291
a.title = (a.title ? a.title+'\n[' : '[')+keyHint+'+click] Expand all children';
268
292
a.innerHTML += '<span>▼</span>';
269
293
a.className += ' sf-dump-toggle';
294
+ x = 1;
270
295
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
+ }
272
300
}
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'))) {
274
303
a = a.substr(1);
275
304
elt.className += ' '+a;
276
305
277
306
if (/[\[{]$/.test(elt.previousSibling.nodeValue)) {
278
307
a = a != elt.nextSibling.id && doc.getElementById(a);
279
308
try {
280
- t = a.nextSibling;
309
+ s = a.nextSibling;
281
310
elt.appendChild(a);
282
- t .parentNode.insertBefore(a, t );
311
+ s .parentNode.insertBefore(a, s );
283
312
if (/^[@#]/.test(elt.innerHTML)) {
284
313
elt.innerHTML += ' <span>▶</span>';
285
314
} else {
@@ -295,7 +324,33 @@ function isCtrlKey(e) {
295
324
}
296
325
}
297
326
}
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) {
299
354
}
300
355
};
301
356
@@ -324,7 +379,14 @@ function isCtrlKey(e) {
324
379
border: 0;
325
380
outline: none;
326
381
}
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
+ );
328
390
329
391
foreach ($ this ->styles as $ class => $ style ) {
330
392
$ line .= 'pre.sf-dump ' .('default ' !== $ class ? ' .sf-dump- ' .$ class : '' ).'{ ' .$ style .'} ' ;
@@ -438,7 +500,12 @@ protected function dumpLine($depth, $endOfValue = false)
438
500
}
439
501
440
502
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 ));
442
509
}
443
510
$ this ->lastDepth = $ depth ;
444
511
0 commit comments