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>
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).
16 Copyright 2007 Kevin Jones. Copyright 1998-2000,2003-2004 Gisle Aas.
17 Copyright 1996-1998 Gurusamy Sarathy.
19 This program is free software; you can redistribute it and/or modify
20 it under the terms of the Perl Artistic License
22 See http://www.perl.com/perl/misc/Artistic.html
27 /** @param [...] The objects to dump. */
29 if (arguments
.length
> 1)
30 return this._dump(arguments
);
31 else if (arguments
.length
== 1)
32 return this._dump(arguments
[0]);
37 _dump
: function (obj
) {
38 if (typeof obj
== 'undefined') return 'undefined';
40 if (obj
.serialize
) { return obj
.serialize(); }
41 var type
= this._typeof(obj
);
42 if (obj
.circularReference
) obj
.circularReference
++;
45 out
= "{ //circularReference\n}";
48 var pairs
= new Array
;
50 for (var prop
in obj
) {
51 if (prop
!= "circularReference" && obj
.hasOwnProperty(prop
)) { //hide inherited properties
52 pairs
.push(prop
+ ': ' + this._dump(obj
[prop
]));
56 out
= '{' + this._format_list(pairs
) + '}';
60 for (var prop
in Dumper
.ESC
) {
61 if (Dumper
.ESC
.hasOwnProperty(prop
)) {
62 obj
= obj
.replace(prop
, Dumper
.ESC
[prop
]);
66 // Escape UTF-8 Strings
67 if (obj
.match(/^[\x00-\x7f]*$/)) {
68 out
= '"' + obj
.replace(/\"/g, "\\\"").replace(/([\n\r]+)/g, "\\$1") + '"';
71 out
= "unescape('"+escape(obj
)+"')";
76 var elems
= new Array
;
78 for (var i
=0; i
<obj
.length
; i
++) {
79 elems
.push( this._dump(obj
[i
]) );
82 out
= '[' + this._format_list(elems
) + ']';
86 // firefox returns GMT strings from toUTCString()...
87 var utc_string
= obj
.toUTCString().replace(/GMT/,'UTC');
88 out
= 'new Date("' + utc_string
+ '")';
93 out
= this._dump_dom(obj
);
100 out
= String(out
).replace(/\n/g, '\n ');
101 out
= out
.replace(/\n (.*)$/,"\n$1");
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
;
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';
120 _dump_dom
: function (obj
) {
121 return '"' + Dumper
.nodeTypes
[obj
.nodeType
] + '"';
135 4: "CDATA_SECTION_NODE",
136 5: "ENTITY_REFERENCE_NODE",
138 7: "PROCESSING_INSTRUCTION_NODE",
141 10: "DOCUMENT_TYPE_NODE",
142 11: "DOCUMENT_FRAGMENT_NODE",