Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/tools/jsdoc-toolkit/app/frame/Dumper.js
blob: d8b007b1d9e1a6751609eb11ffad50b89d3ce76d (plain)
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
/**
 * @class
<pre>
This is a lightly modified version of Kevin Jones' JavaScript
library Data.Dump. To download the original visit:
    <a href="http://openjsan.org/doc/k/ke/kevinj/Data/Dump/">http://openjsan.org/doc/k/ke/kevinj/Data/Dump/</a>

AUTHORS

The Data.Dump JavaScript module is written by Kevin Jones 
(kevinj@cpan.org), based on Data::Dump by Gisle Aas (gisle@aas.no),
based on Data::Dumper by Gurusamy Sarathy (gsar@umich.edu).

COPYRIGHT

Copyright 2007 Kevin Jones. Copyright 1998-2000,2003-2004 Gisle Aas.
Copyright 1996-1998 Gurusamy Sarathy.

This program is free software; you can redistribute it and/or modify
it under the terms of the Perl Artistic License

See http://www.perl.com/perl/misc/Artistic.html
</pre>
 * @static
 */
Dumper = {
	/** @param [...] The objects to dump. */
	dump: function () {
	    if (arguments.length > 1)
	        return this._dump(arguments);
	    else if (arguments.length == 1)
	        return this._dump(arguments[0]);
	    else
	        return "()";
	},
	
	_dump: function (obj) {
		if (typeof obj == 'undefined') return 'undefined';
		var out;
		if (obj.serialize) { return obj.serialize(); }
		var type = this._typeof(obj);
		if (obj.circularReference) obj.circularReference++;
		switch (type) {
			case 'circular':
				out = "{ //circularReference\n}";
				break;
			case 'object':
				var pairs = new Array;
				
				for (var prop in obj) {
					if (prop != "circularReference" && obj.hasOwnProperty(prop)) { //hide inherited properties
						pairs.push(prop + ': ' + this._dump(obj[prop]));
					}
				}
	
				out = '{' + this._format_list(pairs) + '}';
				break;
	
			case 'string':
				for (var prop in Dumper.ESC) {
					if (Dumper.ESC.hasOwnProperty(prop)) {
						obj = obj.replace(prop, Dumper.ESC[prop]);
					}
				}
	
			// Escape UTF-8 Strings
				if (obj.match(/^[\x00-\x7f]*$/)) {
					out = '"' + obj.replace(/\"/g, "\\\"").replace(/([\n\r]+)/g, "\\$1") + '"';
				}
				else {
					out = "unescape('"+escape(obj)+"')";
				}
				break;
	
			case 'array':
				var elems = new Array;
	
				for (var i=0; i<obj.length; i++) {
					elems.push( this._dump(obj[i]) );
				}
	
				out = '[' + this._format_list(elems) + ']';
				break;
	
			case 'date':
			// firefox returns GMT strings from toUTCString()...
			var utc_string = obj.toUTCString().replace(/GMT/,'UTC');
				out = 'new Date("' + utc_string + '")';
				break;
	
			case 'element':
				// DOM element
				out = this._dump_dom(obj);
				break;
		
				default:
					out = obj;
		}
	
		out = String(out).replace(/\n/g, '\n    ');
		out = out.replace(/\n    (.*)$/,"\n$1");
	
		return out;
	},
	
	_format_list: function (list) {
		if (!list.length) return '';
		var nl = list.toString().length > 60 ? '\n' : ' ';
		return nl + list.join(',' + nl) + nl;
    },
    
    _typeof: function (obj) {
    	if (obj && obj.circularReference && obj.circularReference > 1) return 'circular';
		if (Array.prototype.isPrototypeOf(obj)) return 'array';
		if (Date.prototype.isPrototypeOf(obj)) return 'date';
		if (typeof obj.nodeType != 'undefined') return 'element';
		return typeof(obj);
	},
	
	_dump_dom: function (obj) {
		return '"' + Dumper.nodeTypes[obj.nodeType] + '"';
	}
};

Dumper.ESC = {
    "\t": "\\t",
    "\n": "\\n",
    "\f": "\\f"
};

Dumper.nodeTypes = {
    1: "ELEMENT_NODE",
    2: "ATTRIBUTE_NODE",
    3: "TEXT_NODE",
    4: "CDATA_SECTION_NODE",
    5: "ENTITY_REFERENCE_NODE",
    6: "ENTITY_NODE",
    7: "PROCESSING_INSTRUCTION_NODE",
    8: "COMMENT_NODE",
    9: "DOCUMENT_NODE",
    10: "DOCUMENT_TYPE_NODE",
    11: "DOCUMENT_FRAGMENT_NODE",
    12: "NOTATION_NODE"
};