@@ -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" %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 ;
@@ -43,16 +43,13 @@ class HtmlDumper extends CliDumper
43
43
'meta ' => 'color:#B729D9 ' ,
44
44
'key ' => 'color:#56DB3A ' ,
45
45
'index ' => 'color:#1299DA ' ,
46
- 'str .max-string-length b ' => 'color:#A0A0A0 ' ,
47
46
);
48
47
49
- protected $ displayOptions = array (
50
- 'initDepth ' => 1 ,
51
- 'maxDepth ' => 1 ,
48
+ private $ displayOptions = array (
49
+ 'maxDepth ' => 2 ,
52
50
'maxStringLength ' => 160 ,
53
51
);
54
-
55
- protected $ displayOptionsIsUpdated = false ;
52
+ private $ extraDisplayOptions = array ();
56
53
57
54
/**
58
55
* {@inheritdoc}
@@ -87,15 +84,12 @@ public function setStyles(array $styles)
87
84
/**
88
85
* Configures display options.
89
86
*
90
- * @param array $displayOptions A map of displayOptions names to customize the behavior.
87
+ * @param array $displayOptions A map of display options to customize the behavior.
91
88
*/
92
89
public function setDisplayOptions (array $ displayOptions )
93
90
{
94
- if ($ displayOptions )
95
- {
96
- $ this ->displayOptionsIsUpdated = true ;
97
- $ this ->displayOptions = $ displayOptions + $ this ->displayOptions ;
98
- }
91
+ $ this ->headerIsDumped = false ;
92
+ $ this ->displayOptions = $ displayOptions + $ this ->displayOptions ;
99
93
}
100
94
101
95
/**
@@ -123,9 +117,9 @@ public function setDumpBoundaries($prefix, $suffix)
123
117
/**
124
118
* {@inheritdoc}
125
119
*/
126
- public function dump (Data $ data , $ output = null , array $ displayOptions = [] )
120
+ public function dump (Data $ data , $ output = null , array $ extraDisplayOptions = array () )
127
121
{
128
- $ this ->setDisplayOptions ( $ displayOptions ) ;
122
+ $ this ->extraDisplayOptions = $ extraDisplayOptions ;
129
123
parent ::dump ($ data , $ output );
130
124
$ this ->dumpId = 'sf-dump- ' .mt_rand ();
131
125
}
@@ -141,7 +135,7 @@ protected function getDumpHeader()
141
135
return $ this ->dumpHeader ;
142
136
}
143
137
144
- $ line = <<<'EOHTML'
138
+ $ line = str_replace ( ' {$options} ' , json_encode ( $ this -> displayOptions , JSON_FORCE_OBJECT ), <<<'EOHTML'
145
139
<script>
146
140
Sfdump = window.Sfdump || (function (doc) {
147
141
@@ -165,8 +159,8 @@ protected function getDumpHeader()
165
159
};
166
160
}
167
161
168
- function toggle(a, depth ) {
169
- var s = a.nextSibling || {}, oldClass = s.className, arrow, newClass, i ;
162
+ function toggle(a, recursive ) {
163
+ var s = a.nextSibling || {}, oldClass = s.className, arrow, newClass;
170
164
171
165
if ('sf-dump-compact' == oldClass) {
172
166
arrow = '▼';
@@ -181,21 +175,13 @@ function toggle(a, depth) {
181
175
a.lastChild.innerHTML = arrow;
182
176
s.className = newClass;
183
177
184
- if (depth) {
185
-
178
+ if (recursive) {
186
179
try {
187
180
a = s.querySelectorAll('.'+oldClass);
188
-
189
- for (i = 0; i < a.length; ++i) {
190
- if (depth != 'ALL' && isNaN(depth) == false) {
191
- if (getLevelNodeForParent(s, a[i]) >= depth) {
192
- continue;
193
- }
194
- }
195
-
196
- if (a[i].className !== newClass) {
197
- a[i].className = newClass;
198
- a[i].previousSibling.lastChild.innerHTML = arrow;
181
+ for (s = 0; s < a.length; ++s) {
182
+ if (a[s].className !== newClass) {
183
+ a[s].className = newClass;
184
+ a[s].previousSibling.lastChild.innerHTML = arrow;
199
185
}
200
186
}
201
187
} catch (e) {
@@ -205,26 +191,9 @@ function toggle(a, depth) {
205
191
return true;
206
192
};
207
193
208
- function getLevelNodeForParent(parentNode, currentNode, level) {
209
- level = level || 0;
210
- level++;
211
-
212
- if (parentNode.isSameNode(currentNode)) {
213
- return level-1;
214
- }
215
-
216
- currentNode = currentNode.parentNode;
217
-
218
- return getLevelNodeForParent(parentNode, currentNode, level);
219
- }
220
-
221
- return function (root, options) {
194
+ return function (root, extraOptions) {
222
195
root = doc.getElementById(root);
223
- EOHTML;
224
-
225
- $ line .= 'options = options || ' .json_encode ($ this ->displayOptions ).'; ' ;
226
196
227
- $ line .= <<<'EOHTML'
228
197
function a(e, f) {
229
198
addEventListener(root, e, function (e) {
230
199
if ('A' == e.target.tagName) {
@@ -253,8 +222,7 @@ function isCtrlKey(e) {
253
222
a('click', function (a, e) {
254
223
if (/\bsf-dump-toggle\b/.test(a.className)) {
255
224
e.preventDefault();
256
- var maxDepth = isCtrlKey(e) ? 'ALL' : options.maxDepth ;
257
- if (!toggle(a, maxDepth)) {
225
+ if (!toggle(a, isCtrlKey(e))) {
258
226
var r = doc.getElementById(a.getAttribute('href').substr(1)),
259
227
s = r.previousSibling,
260
228
f = r.parentNode,
@@ -268,7 +236,7 @@ function isCtrlKey(e) {
268
236
r.innerHTML = r.innerHTML.replace(new RegExp('^'+f[0].replace(rxEsc, '\\$1'), 'mg'), t[0]);
269
237
}
270
238
if ('sf-dump-compact' == r.className) {
271
- toggle(s, maxDepth );
239
+ toggle(s, isCtrlKey(e) );
272
240
}
273
241
}
274
242
@@ -281,28 +249,34 @@ function isCtrlKey(e) {
281
249
} else {
282
250
doc.selection.empty();
283
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);
284
256
}
285
257
});
286
258
287
259
var indentRx = new RegExp('^('+(root.getAttribute('data-indent-pad') || ' ').replace(rxEsc, '\\$1')+')+', 'm'),
260
+ options = {$options},
288
261
elt = root.getElementsByTagName('A'),
289
262
len = elt.length,
290
- i = 0,
291
- t = [],
292
- temp;
263
+ i = 0, s, h, r,
264
+ t = [];
293
265
294
266
while (i < len) t.push(elt[i++]);
295
267
268
+ for (i in extraOptions) {
269
+ options[i] = $extraOptions[i];
270
+ }
271
+
296
272
elt = root.getElementsByTagName('SAMP');
297
273
len = elt.length;
298
274
i = 0;
299
275
300
276
while (i < len) t.push(elt[i++]);
301
-
302
277
len = t.length;
303
- i = 0;
304
278
305
- while (i < len) {
279
+ for (i = 0; i < len; ++i ) {
306
280
elt = t[i];
307
281
if ("SAMP" == elt.tagName) {
308
282
elt.className = "sf-dump-expanded";
@@ -317,9 +291,14 @@ function isCtrlKey(e) {
317
291
a.title = (a.title ? a.title+'\n[' : '[')+keyHint+'+click] Expand all children';
318
292
a.innerHTML += '<span>▼</span>';
319
293
a.className += ' sf-dump-toggle';
320
- if (getLevelNodeForParent(root, a) > options.initDepth) {
321
- toggle(a);
294
+ r = 1;
295
+ if ('sf-dump' != elt.parentNode.className) {
296
+ r += elt.parentNode.getAttribute('data-depth')/1;
297
+ if (r > options.maxDepth) {
298
+ toggle(a);
299
+ }
322
300
}
301
+ elt.setAttribute('data-depth', r);
323
302
} else if ("sf-dump-ref" == elt.className && (a = elt.getAttribute('href'))) {
324
303
a = a.substr(1);
325
304
elt.className += ' '+a;
@@ -345,43 +324,33 @@ function isCtrlKey(e) {
345
324
}
346
325
}
347
326
}
348
- ++i;
349
327
}
350
328
351
- if (options.maxStringLength) {
352
- configureMaxStringCollapse() ;
329
+ if (0 >= options.maxStringLength) {
330
+ return ;
353
331
}
332
+ try {
333
+ elt = root.querySelectorAll('.sf-dump-str');
334
+ len = elt.length;
335
+ i = 0;
336
+ t = [];
354
337
355
- function configureMaxStringCollapse()
356
- {
357
- var t = root.querySelectorAll('.sf-dump-str'), i = 0;
338
+ while (i < len) t.push(elt[i++]);
339
+ len = t.length;
358
340
359
- for (i = 0; i < t.length ; ++i) {
341
+ for (i = 0; i < len ; ++i) {
360
342
elt = t[i];
361
- if (elt.innerText.length > options.maxStringLength) {
362
- elt.innerHTML = '<span class="max-string-length collapsed">' +
363
- '<span class="collapsed">' + elt.innerText.substring(0, options.maxStringLength)+'...' +' <b>[+]</b></span>'+
364
- '<span class="expanded">' + elt.innerHTML +' <b>[-]</b></span>' +
365
- '</span>';
366
- }
367
- }
368
-
369
- t = root.querySelectorAll('.max-string-length span b');
370
-
371
- for (i = 0; i < t.length; ++i) {
372
- t[i].addEventListener("click", function(e) {
373
- toggleMaxStringLength(e.target.parentNode.parentNode);
374
- });
375
- }
376
-
377
- function toggleMaxStringLength(elt) {
378
-
379
- if (elt.className == 'max-string-length expanded') {
380
- elt.className = 'max-string-length collapsed';
381
- }else{
382
- elt.className = 'max-string-length expanded';
343
+ s = elt.innerText || elt.textContent;
344
+ r = s.length - options.maxStringLength;
345
+ if (0 < r) {
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="'+r+' remaining characters"> ▶</a></span>';
383
351
}
384
352
}
353
+ } catch (e) {
385
354
}
386
355
};
387
356
@@ -410,16 +379,14 @@ function toggleMaxStringLength(elt) {
410
379
border: 0;
411
380
outline: none;
412
381
}
413
- pre.sf-dump .max-string-length.expanded .collapsed {
414
- display:none;
415
- }
416
- pre.sf-dump .max-string-length.collapsed .expanded {
417
- display:none
382
+ .sf-dump-str-collapse .sf-dump-str-collapse {
383
+ display: none;
418
384
}
419
- pre .sf-dump .max-string-length b {
420
- cursor: pointer ;
385
+ .sf-dump-str-expand .sf-dump-str-expand {
386
+ display: none ;
421
387
}
422
- EOHTML;
388
+ EOHTML
389
+ );
423
390
424
391
foreach ($ this ->styles as $ class => $ style ) {
425
392
$ line .= 'pre.sf-dump ' .('default ' !== $ class ? ' .sf-dump- ' .$ class : '' ).'{ ' .$ style .'} ' ;
@@ -534,9 +501,8 @@ protected function dumpLine($depth, $endOfValue = false)
534
501
535
502
if (-1 === $ depth ) {
536
503
$ this ->line .= sprintf (
537
- $ this ->dumpSuffix ,
538
- $ this ->dumpId ,
539
- $ this ->displayOptionsIsUpdated ? ', ' .json_encode ($ this ->displayOptions ) : ''
504
+ str_replace ('"%s" ' , '%s ' , $ this ->dumpSuffix ), // Replace is for BC
505
+ implode (', ' , array ('" ' .$ this ->dumpId .'" ' , json_encode ($ this ->extraDisplayOptions , JSON_FORCE_OBJECT )))
540
506
);
541
507
}
542
508
$ this ->lastDepth = $ depth ;
0 commit comments