jQuery.fn.dump = function(showTypes, showAttributes) {
jQuery.dump($(this), showTypes, showAttributes);
return this;
};
jQuery.dump = function(object, showTypes, showAttributes) {
var dump = '';
var st = typeof showTypes == 'undefined' ? true : showTypes;
var sa = typeof showAttributes == 'undefined' ? true : showAttributes;
var winName = 'dumpWin';
var w = 760;
var h = 500;
var leftPos = screen.width ? (screen.width - w) / 2 : 0;
var topPos = screen.height ? (screen.height - h) / 2 : 0;
var settings = 'height=' + h + ',width=' + w + ',top=' + topPos + ',left=' + leftPos + ',scrollbars=yes,menubar=yes,status=yes,resizable=yes';
var title = 'Dump';
var script = 'function tRow(s) {t = s.parentNode.lastChild;tTarget(t, tSource(s)) ;}function tTable(s) {var switchToState = tSource(s) ;var table = s.parentNode.parentNode;for (var i = 1; i < table.childNodes.length; i++) {t = table.childNodes[i] ;if (t.style) {tTarget(t, switchToState);}}}function tSource(s) {if (s.style.fontStyle == "italic" || s.style.fontStyle == null) {s.style.fontStyle = "normal";s.title = "click to collapse";return "open";} else {s.style.fontStyle = "italic";s.title = "click to expand";return "closed" ;}}function tTarget (t, switchToState) {if (switchToState == "open") {t.style.display = "";} else {t.style.display = "none";}}';
var _recurse = function (o, type) {
var i;
var j = 0;
var r = '';
type = _dumpType(o);
switch (type) {
case 'regexp':
var t = type;
r += '
| ' + t + ' |
';
r += '';
j++;
break;
case 'date':
var t = type;
r += '| ' + t + ' | ';
r += '';
j++;
break;
case 'function':
var t = type;
var a = o.toString().match(/^.*function.*?\((.*?)\)/im);
var args = (a == null || typeof a[1] == 'undefined' || a[1] == '') ? 'none' : a[1];
r += '| ' + t + ' | ';
r += '| Arguments: | ' + args + ' | | Function: | ' + o + ' | ';
j++;
break;
case 'domelement':
var t = type;
var attr = '';
if (sa) {
for (i in o) {if (!/innerHTML|outerHTML/i.test(i)) {attr += i + ': ' + o[i] + ' ';}}
}
r += '| ' + t + ' | ';
r += '| Node Name: | ' + o.nodeName.toLowerCase() + ' | ';
r += '| Node Type: | ' + o.nodeType + ' | ';
r += '| Node Value: | ' + o.nodeValue + ' | ';
if (sa) {
r += '| Attributes: | ' + attr + ' | ';
r += '| innerHTML: | ' + o.innerHTML + ' | ';
if (typeof o.outerHTML != 'undefined') {
r += '| outerHTML: | ' + o.outerHTML + ' | ';
}
}
j++;
break;
}
if (/object|array/.test(type)) {
for (i in o) {
var t = _dumpType(o[i]);
if (j < 1) {
r += '| ' + type + ' | ';
j++;
}
if (typeof o[i] == 'object' && o[i] != null) {
r += '| ' + i + (st ? ' [' + t + ']' : '') + ' | ' + _recurse(o[i], t) + ' | ';
} else if (typeof o[i] == 'function') {
r += '| ' + i + (st ? ' [' + t + ']' : '') + ' | ' + _recurse(o[i], t) + ' | ';
} else {
r += '| ' + i + (st ? ' [' + t + ']' : '') + ' | ' + o[i] + ' | ';
}
}
}
if (j == 0) {
r += '| ' + type + ' [empty] | ';
}
r += ' ';
return r;
};
var _dumpStyles = function(type, use) {
var r = '';
var table = 'font-size:xx-small;font-family:verdana,arial,helvetica,sans-serif;cell-spacing:2px;';
var th = 'font-size:xx-small;font-family:verdana,arial,helvetica,sans-serif;text-align:left;color: white;padding: 5px;vertical-align :top;cursor:hand;cursor:pointer;';
var td = 'font-size:xx-small;font-family:verdana,arial,helvetica,sans-serif;vertical-align:top;padding:3px;';
var thScript = 'onClick="tTable(this);" title="click to collapse"';
var tdScript = 'onClick="tRow(this);" title="click to collapse"';
switch (type) {
case 'string':
case 'number':
case 'boolean':
case 'undefined':
case 'object':
switch (use) {
case 'table':
r = ' style="' + table + 'background-color:#0000cc;"';
break;
case 'th':
r = ' style="' + th + 'background-color:#4444cc;"' + thScript;
break;
case 'td-key':
r = ' style="' + td + 'background-color:#ccddff;cursor:hand;cursor:pointer;"' + tdScript;
break;
case 'td-value':
r = ' style="' + td + 'background-color:#fff;"';
break;
}
break;
case 'array':
switch (use) {
case 'table':
r = ' style="' + table + 'background-color:#006600;"';
break;
case 'th':
r = ' style="' + th + 'background-color:#009900;"' + thScript;
break;
case 'td-key':
r = ' style="' + td + 'background-color:#ccffcc;cursor:hand;cursor:pointer;"' + tdScript;
break;
case 'td-value':
r = ' style="' + td + 'background-color:#fff;"';
break;
}
break;
case 'function':
switch (use) {
case 'table':
r = ' style="' + table + 'background-color:#aa4400;"';
break;
case 'th':
r = ' style="' + th + 'background-color:#cc6600;"' + thScript;
break;
case 'td-key':
r = ' style="' + td + 'background-color:#fff;cursor:hand;cursor:pointer;"' + tdScript;
break;
case 'td-value':
r = ' style="' + td + 'background-color:#fff;"';
break;
}
break;
case 'arguments':
switch (use) {
case 'table':
r = ' style="' + table + 'background-color:#dddddd;cell-spacing:3;"';
break;
case 'td-key':
r = ' style="' + th + 'background-color:#eeeeee;color:#000000;cursor:hand;cursor:pointer;"' + tdScript;
break;
}
break;
case 'regexp':
switch (use) {
case 'table':
r = ' style="' + table + 'background-color:#CC0000;cell-spacing:3;"';
break;
case 'th':
r = ' style="' + th + 'background-color:#FF0000;"' + thScript;
break;
case 'td-key':
r = ' style="' + th + 'background-color:#FF5757;color:#000000;cursor:hand;cursor:pointer;"' + tdScript;
break;
case 'td-value':
r = ' style="' + td + 'background-color:#fff;"';
break;
}
break;
case 'date':
switch (use) {
case 'table':
r = ' style="' + table + 'background-color:#663399;cell-spacing:3;"';
break;
case 'th':
r = ' style="' + th + 'background-color:#9966CC;"' + thScript;
break;
case 'td-key':
r = ' style="' + th + 'background-color:#B266FF;color:#000000;cursor:hand;cursor:pointer;"' + tdScript;
break;
case 'td-value':
r = ' style="' + td + 'background-color:#fff;"';
break;
}
break;
case 'domelement':
case 'document':
case 'window':
switch (use) {
case 'table':
r = ' style="' + table + 'background-color:#FFCC33;cell-spacing:3;"';
break;
case 'th':
r = ' style="' + th + 'background-color:#FFD966;"' + thScript;
break;
case 'td-key':
r = ' style="' + th + 'background-color:#FFF2CC;color:#000000;cursor:hand;cursor:pointer;"' + tdScript;
break;
case 'td-value':
r = ' style="' + td + 'background-color:#fff;"';
break;
}
break;
}
return r;
};
var _dumpType = function (obj) {
var t = typeof(obj);
if (t == 'function') {
var f = obj.toString();
if ( ( /^\/.*\/[gi]??[gi]??$/ ).test(f)) {
return 'regexp';
} else if ((/^\[object.*\]$/i ).test(f)) {
t = 'object'
}
}
if (t != 'object') {
return t;
}
switch (obj) {
case null:
return 'null';
case window:
return 'window';
case document:
return 'document';
case window.event:
return 'event';
}
if (window.event && (event.type == obj.type)) {
return 'event';
}
var c = obj.constructor;
if (c != null) {
switch(c) {
case Array:
t = 'array';
break;
case Date:
return 'date';
case RegExp:
return 'regexp';
case Object:
t = 'object';
break;
case ReferenceError:
return 'error';
default:
var sc = c.toString();
var m = sc.match(/\s*function (.*)\(/);
if (m != null) {
return 'object';
}
}
}
var nt = obj.nodeType;
if (nt != null) {
switch(nt) {
case 1:
return 'domelement';
case 3:
return 'string';
}
}
if (obj.toString != null) {
var ex = obj.toString();
var am = ex.match(/^\[object (.*)\]$/i);
if (am != null) {
var am = am[1];
switch(am.toLowerCase()) {
case 'event':
return 'event';
case 'nodelist':
case 'htmlcollection':
case 'elementarray':
return 'array';
case 'htmldocument':
return 'htmldocument';
}
}
}
return t;
};
dump += (/string|number|undefined|boolean/.test(typeof(object)) || object == null) ? object : _recurse(object, typeof object);
winName = window.open('', '', settings);
if (jQuery.browser.msie || jQuery.browser.browser == 'opera' || jQuery.browser.browser == 'safari') {
winName.document.write(' ' + title + ' ');
winName.document.write('' + dump + '');
} else {
winName.document.body.innerHTML = dump;
winName.document.title = title;
var ffs = winName.document.createElement('script');
ffs.setAttribute('type', 'text/javascript');
ffs.appendChild(document.createTextNode(script));
winName.document.getElementsByTagName('head')[0].appendChild(ffs);
}
winName.focus();
};
|
|
|