1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
/**
* Copyright (c)2005-2009 Matt Kruse (javascripttoolbox.com)
*
* Dual licensed under the MIT and GPL licenses.
* This basically means you can use this code however you want for
* free, but don't claim to have written it yourself!
* Donations always accepted: http://www.JavascriptToolbox.com/donate/
*
* Please do not link to the .js files on javascripttoolbox.com from
* your site. Copy the files locally to your server instead.
*
*/
var Dumper = (function(){
// "Private"
var maxIterations = 1000;
var maxDepth = -1; // Max depth that Dumper will traverse in object
var iterations = 0;
var indent = 1;
var indentText = " ";
var newline = "\n";
var object = null; // Keeps track of the root object passed in
var properties = null; // Holds properties of top-level object to traverse - others are ignored
function args(a,index) {
var myargs = new Array();
for (var i=index; i<a.length; i++) {
myargs[myargs.length] = a[i];
}
return myargs;
};
function pad(len) {
var ret = "";
for (var i=0; i<len; i++) {
ret += indentText;
}
return ret;
};
function string(o) {
var level = 1;
var indentLevel = indent;
var ret = "";
if (arguments.length>1 && typeof(arguments[1])=="number") {
level = arguments[1];
indentLevel = arguments[2];
if (o == object) {
return "[original object]";
}
}
else {
iterations = 0;
object = o;
// If a list of properties are passed in
if (arguments.length>1) {
var list = arguments;
var listIndex = 1;
if (typeof(arguments[1])=="object") {
list = arguments[1];
listIndex = 0;
}
for (var i=listIndex; i<list.length; i++) {
if (properties == null) { properties = new Object(); }
properties[list[i]]=1;
}
}
}
if (iterations++>maxIterations) { return "[Max Iterations Reached]"; } // Just in case, so the script doesn't hang
if (maxDepth != -1 && level > (maxDepth+1)) {
return "...";
}
// undefined
if (typeof(o)=="undefined") {
return "[undefined]";
}
// NULL
if (o==null) {
return "[null]";
}
// DOM Object
if (o==window) {
return "[window]";
}
if (o==window.document) {
return "[document]";
}
// FUNCTION
if (typeof(o)=="function") {
return "[function]";
}
// BOOLEAN
if (typeof(o)=="boolean") {
return (o)?"true":"false";
}
// STRING
if (typeof(o)=="string") {
return "'" + o + "'";
}
// NUMBER
if (typeof(o)=="number") {
return o;
}
if (typeof(o)=="object") {
if (typeof(o.length)=="number" ) {
// ARRAY
if (maxDepth != -1 && level > maxDepth) {
return "[ ... ]";
}
ret = "[";
for (var i=0; i<o.length;i++) {
if (i>0) {
ret += "," + newline + pad(indentLevel);
}
else {
ret += newline + pad(indentLevel);
}
ret += string(o[i],level+1,indentLevel-0+indent);
}
if (i > 0) {
ret += newline + pad(indentLevel-indent);
}
ret += "]";
return ret;
}
else {
// OBJECT
if (maxDepth != -1 && level > maxDepth) {
return "{ ... }";
}
ret = "{";
var count = 0;
for (i in o) {
if (o==object && properties!=null && properties[i]!=1) {
// do nothing with this node
}
else {
if (typeof(o[i]) != "unknown") {
var processAttribute = true;
// Check if this is a DOM object, and if so, if we have to limit properties to look at
if (o.ownerDocument|| o.tagName || (o.nodeType && o.nodeName)) {
processAttribute = false;
if (i=="tagName" || i=="nodeName" || i=="nodeType" || i=="id" || i=="className") {
processAttribute = true;
}
}
if (processAttribute) {
if (count++>0) {
ret += "," + newline + pad(indentLevel);
}
else {
ret += newline + pad(indentLevel);
}
ret += "'" + i + "' => " + string(o[i],level+1,indentLevel-0+i.length+6+indent);
}
}
}
}
if (count > 0) {
ret += newline + pad(indentLevel-indent);
}
ret += "}";
return ret;
}
}
};
string.popup = function(o) {
var w = window.open("about:blank");
w.document.open();
w.document.writeln("<HTML><BODY><PRE>");
w.document.writeln(string(o,args(arguments,1)));
w.document.writeln("</PRE></BODY></HTML>");
w.document.close();
};
string.alert = function(o) {
alert(string(o,args(arguments,1)));
};
string.write = function(o) {
var argumentsIndex = 1;
var d = document;
if (arguments.length>1 && arguments[1]==window.document) {
d = arguments[1];
argumentsIndex = 2;
}
var temp = indentText;
indentText = " ";
d.write(string(o,args(arguments,argumentsIndex)));
indentText = temp;
};
string.setMaxIterations = function(i) {
maxIterations = i;
};
string.setMaxDepth = function(i) {
maxDepth = i;
};
string.$VERSION = 1.0;
return string;
})();
|