| 1 | /** |
| 2 | * @class |
| 3 | <pre> |
| 4 | This is a lightly modified version of Kevin Jones' JavaScript |
| 5 | library Data.Dump. To download the original visit: |
| 6 | <a href="http://openjsan.org/doc/k/ke/kevinj/Data/Dump/">http://openjsan.org/doc/k/ke/kevinj/Data/Dump/</a> |
| 7 | |
| 8 | AUTHORS |
| 9 | |
| 10 | The Data.Dump JavaScript module is written by Kevin Jones |
| 11 | (kevinj@cpan.org), based on Data::Dump by Gisle Aas (gisle@aas.no), |
| 12 | based on Data::Dumper by Gurusamy Sarathy (gsar@umich.edu). |
| 13 | |
| 14 | COPYRIGHT |
| 15 | |
| 16 | Copyright 2007 Kevin Jones. Copyright 1998-2000,2003-2004 Gisle Aas. |
| 17 | Copyright 1996-1998 Gurusamy Sarathy. |
| 18 | |
| 19 | This program is free software; you can redistribute it and/or modify |
| 20 | it under the terms of the Perl Artistic License |
| 21 | |
| 22 | See http://www.perl.com/perl/misc/Artistic.html |
| 23 | </pre> |
| 24 | * @static |
| 25 | */ |
| 26 | Dumper = { |
| 27 | /** @param [...] The objects to dump. */ |
| 28 | dump: function () { |
| 29 | if (arguments.length > 1) |
| 30 | return this._dump(arguments); |
| 31 | else if (arguments.length == 1) |
| 32 | return this._dump(arguments[0]); |
| 33 | else |
| 34 | return "()"; |
| 35 | }, |
| 36 | |
| 37 | _dump: function (obj) { |
| 38 | if (typeof obj == 'undefined') return 'undefined'; |
| 39 | var out; |
| 40 | if (obj.serialize) { return obj.serialize(); } |
| 41 | var type = this._typeof(obj); |
| 42 | if (obj.circularReference) obj.circularReference++; |
| 43 | switch (type) { |
| 44 | case 'circular': |
| 45 | out = "{ //circularReference\n}"; |
| 46 | break; |
| 47 | case 'object': |
| 48 | var pairs = new Array; |
| 49 | |
| 50 | for (var prop in obj) { |
| 51 | if (prop != "circularReference" && obj.hasOwnProperty(prop)) { //hide inherited properties |
| 52 | pairs.push(prop + ': ' + this._dump(obj[prop])); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | out = '{' + this._format_list(pairs) + '}'; |
| 57 | break; |
| 58 | |
| 59 | case 'string': |
| 60 | for (var prop in Dumper.ESC) { |
| 61 | if (Dumper.ESC.hasOwnProperty(prop)) { |
| 62 | obj = obj.replace(prop, Dumper.ESC[prop]); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // Escape UTF-8 Strings |
| 67 | if (obj.match(/^[\x00-\x7f]*$/)) { |
| 68 | out = '"' + obj.replace(/\"/g, "\\\"").replace(/([\n\r]+)/g, "\\$1") + '"'; |
| 69 | } |
| 70 | else { |
| 71 | out = "unescape('"+escape(obj)+"')"; |
| 72 | } |
| 73 | break; |
| 74 | |
| 75 | case 'array': |
| 76 | var elems = new Array; |
| 77 | |
| 78 | for (var i=0; i<obj.length; i++) { |
| 79 | elems.push( this._dump(obj[i]) ); |
| 80 | } |
| 81 | |
| 82 | out = '[' + this._format_list(elems) + ']'; |
| 83 | break; |
| 84 | |
| 85 | case 'date': |
| 86 | // firefox returns GMT strings from toUTCString()... |
| 87 | var utc_string = obj.toUTCString().replace(/GMT/,'UTC'); |
| 88 | out = 'new Date("' + utc_string + '")'; |
| 89 | break; |
| 90 | |
| 91 | case 'element': |
| 92 | // DOM element |
| 93 | out = this._dump_dom(obj); |
| 94 | break; |
| 95 | |
| 96 | default: |
| 97 | out = obj; |
| 98 | } |
| 99 | |
| 100 | out = String(out).replace(/\n/g, '\n '); |
| 101 | out = out.replace(/\n (.*)$/,"\n$1"); |
| 102 | |
| 103 | return out; |
| 104 | }, |
| 105 | |
| 106 | _format_list: function (list) { |
| 107 | if (!list.length) return ''; |
| 108 | var nl = list.toString().length > 60 ? '\n' : ' '; |
| 109 | return nl + list.join(',' + nl) + nl; |
| 110 | }, |
| 111 | |
| 112 | _typeof: function (obj) { |
| 113 | if (obj && obj.circularReference && obj.circularReference > 1) return 'circular'; |
| 114 | if (Array.prototype.isPrototypeOf(obj)) return 'array'; |
| 115 | if (Date.prototype.isPrototypeOf(obj)) return 'date'; |
| 116 | if (typeof obj.nodeType != 'undefined') return 'element'; |
| 117 | return typeof(obj); |
| 118 | }, |
| 119 | |
| 120 | _dump_dom: function (obj) { |
| 121 | return '"' + Dumper.nodeTypes[obj.nodeType] + '"'; |
| 122 | } |
| 123 | }; |
| 124 | |
| 125 | Dumper.ESC = { |
| 126 | "\t": "\\t", |
| 127 | "\n": "\\n", |
| 128 | "\f": "\\f" |
| 129 | }; |
| 130 | |
| 131 | Dumper.nodeTypes = { |
| 132 | 1: "ELEMENT_NODE", |
| 133 | 2: "ATTRIBUTE_NODE", |
| 134 | 3: "TEXT_NODE", |
| 135 | 4: "CDATA_SECTION_NODE", |
| 136 | 5: "ENTITY_REFERENCE_NODE", |
| 137 | 6: "ENTITY_NODE", |
| 138 | 7: "PROCESSING_INSTRUCTION_NODE", |
| 139 | 8: "COMMENT_NODE", |
| 140 | 9: "DOCUMENT_NODE", |
| 141 | 10: "DOCUMENT_TYPE_NODE", |
| 142 | 11: "DOCUMENT_FRAGMENT_NODE", |
| 143 | 12: "NOTATION_NODE" |
| 144 | }; |