forked from web-scrobbler/web-scrobbler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.dump.js
165 lines (149 loc) · 4.22 KB
/
jquery.dump.js
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
/**
* jquery.dump.js
* @author Torkild Dyvik Olsen
* @version 1.0
*
* A simple debug function to gather information about an object.
* Returns a nested tree with information.
*
*/
(function($) {
$.fn.dump = function() {
return $.dump(this);
}
$.dump = function(object) {
var recursion = function(obj, level) {
if(!level) level = 0;
var dump = '', p = '';
for(i = 0; i < level; i++) p += "\t";
t = type(obj);
switch(t) {
case "string":
return '"' + obj + '"';
break;
case "number":
return obj.toString();
break;
case "boolean":
return obj ? 'true' : 'false';
case "date":
return "Date: " + obj.toLocaleString();
case "array":
dump += 'Array ( \n';
$.each(obj, function(k,v) {
dump += p +'\t' + k + ' => ' + recursion(v, level + 1) + '\n';
});
dump += p + ')';
break;
case "object":
dump += 'Object { \n';
$.each(obj, function(k,v) {
dump += p + '\t' + k + ': ' + recursion(v, level + 1) + '\n';
});
dump += p + '}';
break;
case "jquery":
dump += 'jQuery Object { \n';
$.each(obj, function(k,v) {
dump += p + '\t' + k + ' = ' + recursion(v, level + 1) + '\n';
});
dump += p + '}';
break;
case "regexp":
return "RegExp: " + obj.toString();
case "error":
return obj.toString();
case "document":
case "domelement":
dump += 'DOMElement [ \n'
+ p + '\tnodeName: ' + obj.nodeName + '\n'
+ p + '\tnodeValue: ' + obj.nodeValue + '\n'
+ p + '\tinnerHTML: [ \n';
$.each(obj.childNodes, function(k,v) {
if(k < 1) var r = 0;
if(type(v) == "string") {
if(v.textContent.match(/[^\s]/)) {
dump += p + '\t\t' + (k - (r||0)) + ' = String: ' + trim(v.textContent) + '\n';
} else {
r--;
}
} else {
dump += p + '\t\t' + (k - (r||0)) + ' = ' + recursion(v, level + 2) + '\n';
}
});
dump += p + '\t]\n'
+ p + ']';
break;
case "function":
var match = obj.toString().match(/^(.*)\(([^\)]*)\)/im);
match[1] = trim(match[1].replace(new RegExp("[\\s]+", "g"), " "));
match[2] = trim(match[2].replace(new RegExp("[\\s]+", "g"), " "));
return match[1] + "(" + match[2] + ")";
case "window":
default:
dump += 'N/A: ' + t;
break;
}
return dump;
}
var type = function(obj) {
var type = typeof(obj);
if(type != "object") {
return type;
}
switch(obj) {
case null:
return 'null';
case window:
return 'window';
case document:
return 'document';
case window.event:
return 'event';
default:
break;
}
if(obj.jquery) {
return 'jquery';
}
switch(obj.constructor) {
case Array:
return 'array';
case Boolean:
return 'boolean';
case Date:
return 'date';
case Object:
return 'object';
case RegExp:
return 'regexp';
case ReferenceError:
case Error:
return 'error';
case null:
default:
break;
}
switch(obj.nodeType) {
case 1:
return 'domelement';
case 3:
return 'string';
case null:
default:
break;
}
return 'Unknown';
}
return recursion(object);
}
function trim(str) {
return ltrim(rtrim(str));
}
function ltrim(str) {
return str.replace(new RegExp("^[\\s]+", "g"), "");
}
function rtrim(str) {
return str.replace(new RegExp("[\\s]+$", "g"), "");
}
})(jQuery);