rails_jq_grid 0.0.3.pre2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/public/javascripts/rails-jqgrid/Changes.txt +0 -0
- data/public/javascripts/rails-jqgrid/i18n/grid.locale-bg.js +132 -1
- data/public/javascripts/rails-jqgrid/i18n/grid.locale-bg1251.js +132 -1
- data/public/javascripts/rails-jqgrid/i18n/grid.locale-cat.js +128 -1
- data/public/javascripts/rails-jqgrid/i18n/grid.locale-cn.js +132 -1
- data/public/javascripts/rails-jqgrid/i18n/grid.locale-cs.js +128 -1
- data/public/javascripts/rails-jqgrid/i18n/grid.locale-da.js +128 -0
- data/public/javascripts/rails-jqgrid/i18n/grid.locale-de.js +133 -1
- data/public/javascripts/rails-jqgrid/i18n/grid.locale-el.js +126 -1
- data/public/javascripts/rails-jqgrid/i18n/grid.locale-en.js +128 -1
- data/public/javascripts/rails-jqgrid/i18n/grid.locale-es.js +128 -1
- data/public/javascripts/rails-jqgrid/i18n/grid.locale-fa.js +145 -1
- data/public/javascripts/rails-jqgrid/i18n/grid.locale-fi.js +130 -1
- data/public/javascripts/rails-jqgrid/i18n/grid.locale-fr.js +126 -1
- data/public/javascripts/rails-jqgrid/i18n/grid.locale-gl.js +126 -0
- data/public/javascripts/rails-jqgrid/i18n/grid.locale-he.js +127 -1
- data/public/javascripts/rails-jqgrid/i18n/grid.locale-hu.js +129 -1
- data/public/javascripts/rails-jqgrid/i18n/grid.locale-is.js +126 -1
- data/public/javascripts/rails-jqgrid/i18n/grid.locale-it.js +1 -1
- data/public/javascripts/rails-jqgrid/i18n/grid.locale-ja.js +155 -1
- data/public/javascripts/rails-jqgrid/i18n/grid.locale-nl.js +149 -1
- data/public/javascripts/rails-jqgrid/i18n/grid.locale-no.js +1 -1
- data/public/javascripts/rails-jqgrid/i18n/grid.locale-pl.js +127 -1
- data/public/javascripts/rails-jqgrid/i18n/grid.locale-pt-br.js +127 -1
- data/public/javascripts/rails-jqgrid/i18n/grid.locale-pt.js +125 -1
- data/public/javascripts/rails-jqgrid/i18n/grid.locale-ro.js +139 -1
- data/public/javascripts/rails-jqgrid/i18n/grid.locale-ru.js +127 -1
- data/public/javascripts/rails-jqgrid/i18n/grid.locale-sk.js +127 -1
- data/public/javascripts/rails-jqgrid/i18n/grid.locale-sr.js +128 -0
- data/public/javascripts/rails-jqgrid/i18n/grid.locale-sv.js +127 -1
- data/public/javascripts/rails-jqgrid/i18n/grid.locale-tr.js +126 -1
- data/public/javascripts/rails-jqgrid/i18n/grid.locale-ua.js +127 -1
- data/public/javascripts/rails-jqgrid/install.txt +0 -0
- data/public/javascripts/rails-jqgrid/jquery.jqGrid.min.js +445 -432
- data/public/javascripts/rails-jqgrid/src/JsonXml.js +329 -329
- data/public/javascripts/rails-jqgrid/src/css/ellipsis-xbl.xml +0 -0
- data/public/javascripts/rails-jqgrid/src/css/ui.jqgrid.css +139 -129
- data/public/javascripts/rails-jqgrid/src/css/ui.multiselect.css +0 -0
- data/public/javascripts/rails-jqgrid/src/grid.base.js +3078 -3003
- data/public/javascripts/rails-jqgrid/src/grid.celledit.js +486 -486
- data/public/javascripts/rails-jqgrid/src/grid.common.js +646 -636
- data/public/javascripts/rails-jqgrid/src/grid.custom.js +820 -817
- data/public/javascripts/rails-jqgrid/src/grid.formedit.js +1954 -1872
- data/public/javascripts/rails-jqgrid/src/grid.grouping.js +250 -0
- data/public/javascripts/rails-jqgrid/src/grid.import.js +200 -200
- data/public/javascripts/rails-jqgrid/src/grid.inlinedit.js +255 -250
- data/public/javascripts/rails-jqgrid/src/grid.jqueryui.js +504 -503
- data/public/javascripts/rails-jqgrid/src/grid.loader.js +1 -0
- data/public/javascripts/rails-jqgrid/src/grid.postext.js +63 -63
- data/public/javascripts/rails-jqgrid/src/grid.setcolumns.js +125 -125
- data/public/javascripts/rails-jqgrid/src/grid.subgrid.js +260 -260
- data/public/javascripts/rails-jqgrid/src/grid.tbltogrid.js +106 -106
- data/public/javascripts/rails-jqgrid/src/grid.treegrid.js +489 -482
- data/public/javascripts/rails-jqgrid/src/i18n/grid.locale-bg.js +132 -132
- data/public/javascripts/rails-jqgrid/src/i18n/grid.locale-bg1251.js +132 -132
- data/public/javascripts/rails-jqgrid/src/i18n/grid.locale-cat.js +128 -128
- data/public/javascripts/rails-jqgrid/src/i18n/grid.locale-cn.js +132 -132
- data/public/javascripts/rails-jqgrid/src/i18n/grid.locale-cs.js +128 -128
- data/public/javascripts/rails-jqgrid/src/i18n/grid.locale-da.js +128 -0
- data/public/javascripts/rails-jqgrid/src/i18n/grid.locale-de.js +132 -127
- data/public/javascripts/rails-jqgrid/src/i18n/grid.locale-dk.js +0 -0
- data/public/javascripts/rails-jqgrid/src/i18n/grid.locale-el.js +126 -126
- data/public/javascripts/rails-jqgrid/src/i18n/grid.locale-en.js +128 -128
- data/public/javascripts/rails-jqgrid/src/i18n/grid.locale-es.js +128 -128
- data/public/javascripts/rails-jqgrid/src/i18n/grid.locale-fa.js +145 -125
- data/public/javascripts/rails-jqgrid/src/i18n/grid.locale-fi.js +130 -130
- data/public/javascripts/rails-jqgrid/src/i18n/grid.locale-fr.js +2 -2
- data/public/javascripts/rails-jqgrid/src/i18n/grid.locale-gl.js +126 -0
- data/public/javascripts/rails-jqgrid/src/i18n/grid.locale-he.js +127 -127
- data/public/javascripts/rails-jqgrid/src/i18n/grid.locale-hu.js +129 -129
- data/public/javascripts/rails-jqgrid/src/i18n/grid.locale-is.js +126 -126
- data/public/javascripts/rails-jqgrid/src/i18n/grid.locale-it.js +0 -0
- data/public/javascripts/rails-jqgrid/src/i18n/grid.locale-ja.js +155 -155
- data/public/javascripts/rails-jqgrid/src/i18n/grid.locale-nl.js +148 -148
- data/public/javascripts/rails-jqgrid/src/i18n/grid.locale-no.js +0 -0
- data/public/javascripts/rails-jqgrid/src/i18n/grid.locale-pl.js +126 -126
- data/public/javascripts/rails-jqgrid/src/i18n/grid.locale-pt-br.js +127 -127
- data/public/javascripts/rails-jqgrid/src/i18n/grid.locale-pt.js +125 -125
- data/public/javascripts/rails-jqgrid/src/i18n/grid.locale-ro.js +139 -139
- data/public/javascripts/rails-jqgrid/src/i18n/grid.locale-ru.js +127 -127
- data/public/javascripts/rails-jqgrid/src/i18n/grid.locale-sk.js +127 -127
- data/public/javascripts/rails-jqgrid/src/i18n/grid.locale-sr.js +128 -0
- data/public/javascripts/rails-jqgrid/src/i18n/grid.locale-sv.js +127 -127
- data/public/javascripts/rails-jqgrid/src/i18n/grid.locale-tr.js +126 -126
- data/public/javascripts/rails-jqgrid/src/i18n/grid.locale-ua.js +127 -127
- data/public/javascripts/rails-jqgrid/src/jqDnR.js +68 -67
- data/public/javascripts/rails-jqgrid/src/jqModal.js +68 -68
- data/public/javascripts/rails-jqgrid/src/jquery.fmatter.js +563 -541
- data/public/javascripts/rails-jqgrid/src/jquery.searchFilter.js +1 -1
- data/public/javascripts/rails-jqgrid/src/ui.multiselect.js +0 -0
- data/public/stylesheets/rails-jqgrid/ellipsis-xbl.xml +0 -0
- data/public/stylesheets/rails-jqgrid/ui.jqgrid.css +139 -2
- data/rails_jq_grid.gemspec +10 -3
- metadata +15 -11
@@ -1,330 +1,330 @@
|
|
1
|
-
/*
|
2
|
-
The below work is licensed under Creative Commons GNU LGPL License.
|
3
|
-
|
4
|
-
Original work:
|
5
|
-
|
6
|
-
License: http://creativecommons.org/licenses/LGPL/2.1/
|
7
|
-
Author: Stefan Goessner/2006
|
8
|
-
Web: http://goessner.net/
|
9
|
-
|
10
|
-
Modifications made:
|
11
|
-
|
12
|
-
Version: 0.9-p5
|
13
|
-
Description: Restructured code, JSLint validated (no strict whitespaces),
|
14
|
-
added handling of empty arrays, empty strings, and int/floats values.
|
15
|
-
Author: Michael Schøler/2008-01-29
|
16
|
-
Web: http://michael.hinnerup.net/blog/2008/01/26/converting-json-to-xml-and-xml-to-json/
|
17
|
-
|
18
|
-
Description: json2xml added support to convert functions as CDATA
|
19
|
-
so it will be easy to write characters that cause some problems when convert
|
20
|
-
Author: Tony Tomov
|
21
|
-
*/
|
22
|
-
|
23
|
-
/*global alert */
|
24
|
-
var xmlJsonClass = {
|
25
|
-
// Param "xml": Element or document DOM node.
|
26
|
-
// Param "tab": Tab or indent string for pretty output formatting omit or use empty string "" to supress.
|
27
|
-
// Returns: JSON string
|
28
|
-
xml2json: function(xml, tab) {
|
29
|
-
if (xml.nodeType === 9) {
|
30
|
-
// document node
|
31
|
-
xml = xml.documentElement;
|
32
|
-
}
|
33
|
-
var nws = this.removeWhite(xml);
|
34
|
-
var obj = this.toObj(nws);
|
35
|
-
var json = this.toJson(obj, xml.nodeName, "\t");
|
36
|
-
return "{\n" + tab + (tab ? json.replace(/\t/g, tab) : json.replace(/\t|\n/g, "")) + "\n}";
|
37
|
-
},
|
38
|
-
|
39
|
-
// Param "o": JavaScript object
|
40
|
-
// Param "tab": tab or indent string for pretty output formatting omit or use empty string "" to supress.
|
41
|
-
// Returns: XML string
|
42
|
-
json2xml: function(o, tab) {
|
43
|
-
var toXml = function(v, name, ind) {
|
44
|
-
var xml = "";
|
45
|
-
var i, n;
|
46
|
-
if (v instanceof Array) {
|
47
|
-
if (v.length === 0) {
|
48
|
-
xml += ind + "<"+name+">__EMPTY_ARRAY_</"+name+">\n";
|
49
|
-
}
|
50
|
-
else {
|
51
|
-
for (i = 0, n = v.length; i < n; i += 1) {
|
52
|
-
var sXml = ind + toXml(v[i], name, ind+"\t") + "\n";
|
53
|
-
xml += sXml;
|
54
|
-
}
|
55
|
-
}
|
56
|
-
}
|
57
|
-
else if (typeof(v) === "object") {
|
58
|
-
var hasChild = false;
|
59
|
-
xml += ind + "<" + name;
|
60
|
-
var m;
|
61
|
-
for (m in v) if (v.hasOwnProperty(m)) {
|
62
|
-
if (m.charAt(0) === "@") {
|
63
|
-
xml += " " + m.substr(1) + "=\"" + v[m].toString() + "\"";
|
64
|
-
}
|
65
|
-
else {
|
66
|
-
hasChild = true;
|
67
|
-
}
|
68
|
-
}
|
69
|
-
xml += hasChild ? ">" : "/>";
|
70
|
-
if (hasChild) {
|
71
|
-
for (m in v) if (v.hasOwnProperty(m)) {
|
72
|
-
if (m === "#text") {
|
73
|
-
xml += v[m];
|
74
|
-
}
|
75
|
-
else if (m === "#cdata") {
|
76
|
-
xml += "<![CDATA[" + v[m] + "]]>";
|
77
|
-
}
|
78
|
-
else if (m.charAt(0) !== "@") {
|
79
|
-
xml += toXml(v[m], m, ind+"\t");
|
80
|
-
}
|
81
|
-
}
|
82
|
-
xml += (xml.charAt(xml.length - 1) === "\n" ? ind : "") + "</" + name + ">";
|
83
|
-
}
|
84
|
-
}
|
85
|
-
else if (typeof(v) === "function") {
|
86
|
-
xml += ind + "<" + name + ">" + "<![CDATA[" + v + "]]>" + "</" + name + ">";
|
87
|
-
}
|
88
|
-
else {
|
89
|
-
if (v.toString() === "\"\"" || v.toString().length === 0) {
|
90
|
-
xml += ind + "<" + name + ">__EMPTY_STRING_</" + name + ">";
|
91
|
-
}
|
92
|
-
else {
|
93
|
-
xml += ind + "<" + name + ">" + v.toString() + "</" + name + ">";
|
94
|
-
}
|
95
|
-
}
|
96
|
-
return xml;
|
97
|
-
};
|
98
|
-
var xml = "";
|
99
|
-
var m;
|
100
|
-
for (m in o) if (o.hasOwnProperty(m)) {
|
101
|
-
xml += toXml(o[m], m, "");
|
102
|
-
}
|
103
|
-
return tab ? xml.replace(/\t/g, tab) : xml.replace(/\t|\n/g, "");
|
104
|
-
},
|
105
|
-
// Internal methods
|
106
|
-
toObj: function(xml) {
|
107
|
-
var o = {};
|
108
|
-
var FuncTest = /function/i;
|
109
|
-
if (xml.nodeType === 1) {
|
110
|
-
// element node ..
|
111
|
-
if (xml.attributes.length) {
|
112
|
-
// element with attributes ..
|
113
|
-
var i;
|
114
|
-
for (i = 0; i < xml.attributes.length; i += 1) {
|
115
|
-
o["@" + xml.attributes[i].nodeName] = (xml.attributes[i].nodeValue || "").toString();
|
116
|
-
}
|
117
|
-
}
|
118
|
-
if (xml.firstChild) {
|
119
|
-
// element has child nodes ..
|
120
|
-
var textChild = 0, cdataChild = 0, hasElementChild = false;
|
121
|
-
var n;
|
122
|
-
for (n = xml.firstChild; n; n = n.nextSibling) {
|
123
|
-
if (n.nodeType === 1) {
|
124
|
-
hasElementChild = true;
|
125
|
-
}
|
126
|
-
else if (n.nodeType === 3 && n.nodeValue.match(/[^ \f\n\r\t\v]/)) {
|
127
|
-
// non-whitespace text
|
128
|
-
textChild += 1;
|
129
|
-
}
|
130
|
-
else if (n.nodeType === 4) {
|
131
|
-
// cdata section node
|
132
|
-
cdataChild += 1;
|
133
|
-
}
|
134
|
-
}
|
135
|
-
if (hasElementChild) {
|
136
|
-
if (textChild < 2 && cdataChild < 2) {
|
137
|
-
// structured element with evtl. a single text or/and cdata node ..
|
138
|
-
this.removeWhite(xml);
|
139
|
-
for (n = xml.firstChild; n; n = n.nextSibling) {
|
140
|
-
if (n.nodeType === 3) {
|
141
|
-
// text node
|
142
|
-
o["#text"] = this.escape(n.nodeValue);
|
143
|
-
}
|
144
|
-
else if (n.nodeType === 4) {
|
145
|
-
// cdata node
|
146
|
-
if (FuncTest.test(n.nodeValue)) {
|
147
|
-
o[n.nodeName] = [o[n.nodeName], n.nodeValue];
|
148
|
-
} else {
|
149
|
-
o["#cdata"] = this.escape(n.nodeValue);
|
150
|
-
}
|
151
|
-
}
|
152
|
-
else if (o[n.nodeName]) {
|
153
|
-
// multiple occurence of element ..
|
154
|
-
if (o[n.nodeName] instanceof Array) {
|
155
|
-
o[n.nodeName][o[n.nodeName].length] = this.toObj(n);
|
156
|
-
}
|
157
|
-
else {
|
158
|
-
o[n.nodeName] = [o[n.nodeName], this.toObj(n)];
|
159
|
-
}
|
160
|
-
}
|
161
|
-
else {
|
162
|
-
// first occurence of element ..
|
163
|
-
o[n.nodeName] = this.toObj(n);
|
164
|
-
}
|
165
|
-
}
|
166
|
-
}
|
167
|
-
else {
|
168
|
-
// mixed content
|
169
|
-
if (!xml.attributes.length) {
|
170
|
-
o = this.escape(this.innerXml(xml));
|
171
|
-
}
|
172
|
-
else {
|
173
|
-
o["#text"] = this.escape(this.innerXml(xml));
|
174
|
-
}
|
175
|
-
}
|
176
|
-
}
|
177
|
-
else if (textChild) {
|
178
|
-
// pure text
|
179
|
-
if (!xml.attributes.length) {
|
180
|
-
o = this.escape(this.innerXml(xml));
|
181
|
-
if (o === "__EMPTY_ARRAY_") {
|
182
|
-
o = "[]";
|
183
|
-
} else if (o === "__EMPTY_STRING_") {
|
184
|
-
o = "";
|
185
|
-
}
|
186
|
-
}
|
187
|
-
else {
|
188
|
-
o["#text"] = this.escape(this.innerXml(xml));
|
189
|
-
}
|
190
|
-
}
|
191
|
-
else if (cdataChild) {
|
192
|
-
// cdata
|
193
|
-
if (cdataChild > 1) {
|
194
|
-
o = this.escape(this.innerXml(xml));
|
195
|
-
}
|
196
|
-
else {
|
197
|
-
for (n = xml.firstChild; n; n = n.nextSibling) {
|
198
|
-
if(FuncTest.test(xml.firstChild.nodeValue)) {
|
199
|
-
o = xml.firstChild.nodeValue;
|
200
|
-
break;
|
201
|
-
} else {
|
202
|
-
o["#cdata"] = this.escape(n.nodeValue);
|
203
|
-
}
|
204
|
-
}
|
205
|
-
}
|
206
|
-
}
|
207
|
-
}
|
208
|
-
if (!xml.attributes.length && !xml.firstChild) {
|
209
|
-
o = null;
|
210
|
-
}
|
211
|
-
}
|
212
|
-
else if (xml.nodeType === 9) {
|
213
|
-
// document.node
|
214
|
-
o = this.toObj(xml.documentElement);
|
215
|
-
}
|
216
|
-
else {
|
217
|
-
alert("unhandled node type: " + xml.nodeType);
|
218
|
-
}
|
219
|
-
return o;
|
220
|
-
},
|
221
|
-
toJson: function(o, name, ind) {
|
222
|
-
var json = name ? ("\"" + name + "\"") : "";
|
223
|
-
if (o === "[]") {
|
224
|
-
json += (name ? ":[]" : "[]");
|
225
|
-
}
|
226
|
-
else if (o instanceof Array) {
|
227
|
-
var n, i, ar=[];
|
228
|
-
for (i = 0, n = o.length; i < n; i += 1) {
|
229
|
-
ar[i] = this.toJson(o[i], "", ind + "\t");
|
230
|
-
}
|
231
|
-
json += (name ? ":[" : "[") + (ar.length > 1 ? ("\n" + ind + "\t" + ar.join(",\n" + ind + "\t") + "\n" + ind) : ar.join("")) + "]";
|
232
|
-
}
|
233
|
-
else if (o === null) {
|
234
|
-
json += (name && ":") + "null";
|
235
|
-
}
|
236
|
-
else if (typeof(o) === "object") {
|
237
|
-
var arr = [];
|
238
|
-
var m;
|
239
|
-
for (m in o) if (o.hasOwnProperty(m)) {
|
240
|
-
arr[arr.length] = this.toJson(o[m], m, ind + "\t");
|
241
|
-
}
|
242
|
-
json += (name ? ":{" : "{") + (arr.length > 1 ? ("\n" + ind + "\t" + arr.join(",\n" + ind + "\t") + "\n" + ind) : arr.join("")) + "}";
|
243
|
-
}
|
244
|
-
else if (typeof(o) === "string") {
|
245
|
-
var objRegExp = /(^-?\d+\.?\d*$)/;
|
246
|
-
var FuncTest = /function/i;
|
247
|
-
var os = o.toString();
|
248
|
-
if (objRegExp.test(os) || FuncTest.test(os) || os==="false" || os==="true") {
|
249
|
-
// int or float
|
250
|
-
json += (name && ":") + os;
|
251
|
-
}
|
252
|
-
else {
|
253
|
-
json += (name && ":") + "\"" + o + "\"";
|
254
|
-
}
|
255
|
-
}
|
256
|
-
else {
|
257
|
-
json += (name && ":") + o.toString();
|
258
|
-
}
|
259
|
-
return json;
|
260
|
-
},
|
261
|
-
innerXml: function(node) {
|
262
|
-
var s = "";
|
263
|
-
if ("innerHTML" in node) {
|
264
|
-
s = node.innerHTML;
|
265
|
-
}
|
266
|
-
else {
|
267
|
-
var asXml = function(n) {
|
268
|
-
var s = "", i;
|
269
|
-
if (n.nodeType === 1) {
|
270
|
-
s += "<" + n.nodeName;
|
271
|
-
for (i = 0; i < n.attributes.length; i += 1) {
|
272
|
-
s += " " + n.attributes[i].nodeName + "=\"" + (n.attributes[i].nodeValue || "").toString() + "\"";
|
273
|
-
}
|
274
|
-
if (n.firstChild) {
|
275
|
-
s += ">";
|
276
|
-
for (var c = n.firstChild; c; c = c.nextSibling) {
|
277
|
-
s += asXml(c);
|
278
|
-
}
|
279
|
-
s += "</" + n.nodeName + ">";
|
280
|
-
}
|
281
|
-
else {
|
282
|
-
s += "/>";
|
283
|
-
}
|
284
|
-
}
|
285
|
-
else if (n.nodeType === 3) {
|
286
|
-
s += n.nodeValue;
|
287
|
-
}
|
288
|
-
else if (n.nodeType === 4) {
|
289
|
-
s += "<![CDATA[" + n.nodeValue + "]]>";
|
290
|
-
}
|
291
|
-
return s;
|
292
|
-
};
|
293
|
-
for (var c = node.firstChild; c; c = c.nextSibling) {
|
294
|
-
s += asXml(c);
|
295
|
-
}
|
296
|
-
}
|
297
|
-
return s;
|
298
|
-
},
|
299
|
-
escape: function(txt) {
|
300
|
-
return txt.replace(/[\\]/g, "\\\\").replace(/[\"]/g, '\\"').replace(/[\n]/g, '\\n').replace(/[\r]/g, '\\r');
|
301
|
-
},
|
302
|
-
removeWhite: function(e) {
|
303
|
-
e.normalize();
|
304
|
-
var n;
|
305
|
-
for (n = e.firstChild; n; ) {
|
306
|
-
if (n.nodeType === 3) {
|
307
|
-
// text node
|
308
|
-
if (!n.nodeValue.match(/[^ \f\n\r\t\v]/)) {
|
309
|
-
// pure whitespace text node
|
310
|
-
var nxt = n.nextSibling;
|
311
|
-
e.removeChild(n);
|
312
|
-
n = nxt;
|
313
|
-
}
|
314
|
-
else {
|
315
|
-
n = n.nextSibling;
|
316
|
-
}
|
317
|
-
}
|
318
|
-
else if (n.nodeType === 1) {
|
319
|
-
// element node
|
320
|
-
this.removeWhite(n);
|
321
|
-
n = n.nextSibling;
|
322
|
-
}
|
323
|
-
else {
|
324
|
-
// any other node
|
325
|
-
n = n.nextSibling;
|
326
|
-
}
|
327
|
-
}
|
328
|
-
return e;
|
329
|
-
}
|
1
|
+
/*
|
2
|
+
The below work is licensed under Creative Commons GNU LGPL License.
|
3
|
+
|
4
|
+
Original work:
|
5
|
+
|
6
|
+
License: http://creativecommons.org/licenses/LGPL/2.1/
|
7
|
+
Author: Stefan Goessner/2006
|
8
|
+
Web: http://goessner.net/
|
9
|
+
|
10
|
+
Modifications made:
|
11
|
+
|
12
|
+
Version: 0.9-p5
|
13
|
+
Description: Restructured code, JSLint validated (no strict whitespaces),
|
14
|
+
added handling of empty arrays, empty strings, and int/floats values.
|
15
|
+
Author: Michael Schøler/2008-01-29
|
16
|
+
Web: http://michael.hinnerup.net/blog/2008/01/26/converting-json-to-xml-and-xml-to-json/
|
17
|
+
|
18
|
+
Description: json2xml added support to convert functions as CDATA
|
19
|
+
so it will be easy to write characters that cause some problems when convert
|
20
|
+
Author: Tony Tomov
|
21
|
+
*/
|
22
|
+
|
23
|
+
/*global alert */
|
24
|
+
var xmlJsonClass = {
|
25
|
+
// Param "xml": Element or document DOM node.
|
26
|
+
// Param "tab": Tab or indent string for pretty output formatting omit or use empty string "" to supress.
|
27
|
+
// Returns: JSON string
|
28
|
+
xml2json: function(xml, tab) {
|
29
|
+
if (xml.nodeType === 9) {
|
30
|
+
// document node
|
31
|
+
xml = xml.documentElement;
|
32
|
+
}
|
33
|
+
var nws = this.removeWhite(xml);
|
34
|
+
var obj = this.toObj(nws);
|
35
|
+
var json = this.toJson(obj, xml.nodeName, "\t");
|
36
|
+
return "{\n" + tab + (tab ? json.replace(/\t/g, tab) : json.replace(/\t|\n/g, "")) + "\n}";
|
37
|
+
},
|
38
|
+
|
39
|
+
// Param "o": JavaScript object
|
40
|
+
// Param "tab": tab or indent string for pretty output formatting omit or use empty string "" to supress.
|
41
|
+
// Returns: XML string
|
42
|
+
json2xml: function(o, tab) {
|
43
|
+
var toXml = function(v, name, ind) {
|
44
|
+
var xml = "";
|
45
|
+
var i, n;
|
46
|
+
if (v instanceof Array) {
|
47
|
+
if (v.length === 0) {
|
48
|
+
xml += ind + "<"+name+">__EMPTY_ARRAY_</"+name+">\n";
|
49
|
+
}
|
50
|
+
else {
|
51
|
+
for (i = 0, n = v.length; i < n; i += 1) {
|
52
|
+
var sXml = ind + toXml(v[i], name, ind+"\t") + "\n";
|
53
|
+
xml += sXml;
|
54
|
+
}
|
55
|
+
}
|
56
|
+
}
|
57
|
+
else if (typeof(v) === "object") {
|
58
|
+
var hasChild = false;
|
59
|
+
xml += ind + "<" + name;
|
60
|
+
var m;
|
61
|
+
for (m in v) if (v.hasOwnProperty(m)) {
|
62
|
+
if (m.charAt(0) === "@") {
|
63
|
+
xml += " " + m.substr(1) + "=\"" + v[m].toString() + "\"";
|
64
|
+
}
|
65
|
+
else {
|
66
|
+
hasChild = true;
|
67
|
+
}
|
68
|
+
}
|
69
|
+
xml += hasChild ? ">" : "/>";
|
70
|
+
if (hasChild) {
|
71
|
+
for (m in v) if (v.hasOwnProperty(m)) {
|
72
|
+
if (m === "#text") {
|
73
|
+
xml += v[m];
|
74
|
+
}
|
75
|
+
else if (m === "#cdata") {
|
76
|
+
xml += "<![CDATA[" + v[m] + "]]>";
|
77
|
+
}
|
78
|
+
else if (m.charAt(0) !== "@") {
|
79
|
+
xml += toXml(v[m], m, ind+"\t");
|
80
|
+
}
|
81
|
+
}
|
82
|
+
xml += (xml.charAt(xml.length - 1) === "\n" ? ind : "") + "</" + name + ">";
|
83
|
+
}
|
84
|
+
}
|
85
|
+
else if (typeof(v) === "function") {
|
86
|
+
xml += ind + "<" + name + ">" + "<![CDATA[" + v + "]]>" + "</" + name + ">";
|
87
|
+
}
|
88
|
+
else {
|
89
|
+
if (v.toString() === "\"\"" || v.toString().length === 0) {
|
90
|
+
xml += ind + "<" + name + ">__EMPTY_STRING_</" + name + ">";
|
91
|
+
}
|
92
|
+
else {
|
93
|
+
xml += ind + "<" + name + ">" + v.toString() + "</" + name + ">";
|
94
|
+
}
|
95
|
+
}
|
96
|
+
return xml;
|
97
|
+
};
|
98
|
+
var xml = "";
|
99
|
+
var m;
|
100
|
+
for (m in o) if (o.hasOwnProperty(m)) {
|
101
|
+
xml += toXml(o[m], m, "");
|
102
|
+
}
|
103
|
+
return tab ? xml.replace(/\t/g, tab) : xml.replace(/\t|\n/g, "");
|
104
|
+
},
|
105
|
+
// Internal methods
|
106
|
+
toObj: function(xml) {
|
107
|
+
var o = {};
|
108
|
+
var FuncTest = /function/i;
|
109
|
+
if (xml.nodeType === 1) {
|
110
|
+
// element node ..
|
111
|
+
if (xml.attributes.length) {
|
112
|
+
// element with attributes ..
|
113
|
+
var i;
|
114
|
+
for (i = 0; i < xml.attributes.length; i += 1) {
|
115
|
+
o["@" + xml.attributes[i].nodeName] = (xml.attributes[i].nodeValue || "").toString();
|
116
|
+
}
|
117
|
+
}
|
118
|
+
if (xml.firstChild) {
|
119
|
+
// element has child nodes ..
|
120
|
+
var textChild = 0, cdataChild = 0, hasElementChild = false;
|
121
|
+
var n;
|
122
|
+
for (n = xml.firstChild; n; n = n.nextSibling) {
|
123
|
+
if (n.nodeType === 1) {
|
124
|
+
hasElementChild = true;
|
125
|
+
}
|
126
|
+
else if (n.nodeType === 3 && n.nodeValue.match(/[^ \f\n\r\t\v]/)) {
|
127
|
+
// non-whitespace text
|
128
|
+
textChild += 1;
|
129
|
+
}
|
130
|
+
else if (n.nodeType === 4) {
|
131
|
+
// cdata section node
|
132
|
+
cdataChild += 1;
|
133
|
+
}
|
134
|
+
}
|
135
|
+
if (hasElementChild) {
|
136
|
+
if (textChild < 2 && cdataChild < 2) {
|
137
|
+
// structured element with evtl. a single text or/and cdata node ..
|
138
|
+
this.removeWhite(xml);
|
139
|
+
for (n = xml.firstChild; n; n = n.nextSibling) {
|
140
|
+
if (n.nodeType === 3) {
|
141
|
+
// text node
|
142
|
+
o["#text"] = this.escape(n.nodeValue);
|
143
|
+
}
|
144
|
+
else if (n.nodeType === 4) {
|
145
|
+
// cdata node
|
146
|
+
if (FuncTest.test(n.nodeValue)) {
|
147
|
+
o[n.nodeName] = [o[n.nodeName], n.nodeValue];
|
148
|
+
} else {
|
149
|
+
o["#cdata"] = this.escape(n.nodeValue);
|
150
|
+
}
|
151
|
+
}
|
152
|
+
else if (o[n.nodeName]) {
|
153
|
+
// multiple occurence of element ..
|
154
|
+
if (o[n.nodeName] instanceof Array) {
|
155
|
+
o[n.nodeName][o[n.nodeName].length] = this.toObj(n);
|
156
|
+
}
|
157
|
+
else {
|
158
|
+
o[n.nodeName] = [o[n.nodeName], this.toObj(n)];
|
159
|
+
}
|
160
|
+
}
|
161
|
+
else {
|
162
|
+
// first occurence of element ..
|
163
|
+
o[n.nodeName] = this.toObj(n);
|
164
|
+
}
|
165
|
+
}
|
166
|
+
}
|
167
|
+
else {
|
168
|
+
// mixed content
|
169
|
+
if (!xml.attributes.length) {
|
170
|
+
o = this.escape(this.innerXml(xml));
|
171
|
+
}
|
172
|
+
else {
|
173
|
+
o["#text"] = this.escape(this.innerXml(xml));
|
174
|
+
}
|
175
|
+
}
|
176
|
+
}
|
177
|
+
else if (textChild) {
|
178
|
+
// pure text
|
179
|
+
if (!xml.attributes.length) {
|
180
|
+
o = this.escape(this.innerXml(xml));
|
181
|
+
if (o === "__EMPTY_ARRAY_") {
|
182
|
+
o = "[]";
|
183
|
+
} else if (o === "__EMPTY_STRING_") {
|
184
|
+
o = "";
|
185
|
+
}
|
186
|
+
}
|
187
|
+
else {
|
188
|
+
o["#text"] = this.escape(this.innerXml(xml));
|
189
|
+
}
|
190
|
+
}
|
191
|
+
else if (cdataChild) {
|
192
|
+
// cdata
|
193
|
+
if (cdataChild > 1) {
|
194
|
+
o = this.escape(this.innerXml(xml));
|
195
|
+
}
|
196
|
+
else {
|
197
|
+
for (n = xml.firstChild; n; n = n.nextSibling) {
|
198
|
+
if(FuncTest.test(xml.firstChild.nodeValue)) {
|
199
|
+
o = xml.firstChild.nodeValue;
|
200
|
+
break;
|
201
|
+
} else {
|
202
|
+
o["#cdata"] = this.escape(n.nodeValue);
|
203
|
+
}
|
204
|
+
}
|
205
|
+
}
|
206
|
+
}
|
207
|
+
}
|
208
|
+
if (!xml.attributes.length && !xml.firstChild) {
|
209
|
+
o = null;
|
210
|
+
}
|
211
|
+
}
|
212
|
+
else if (xml.nodeType === 9) {
|
213
|
+
// document.node
|
214
|
+
o = this.toObj(xml.documentElement);
|
215
|
+
}
|
216
|
+
else {
|
217
|
+
alert("unhandled node type: " + xml.nodeType);
|
218
|
+
}
|
219
|
+
return o;
|
220
|
+
},
|
221
|
+
toJson: function(o, name, ind) {
|
222
|
+
var json = name ? ("\"" + name + "\"") : "";
|
223
|
+
if (o === "[]") {
|
224
|
+
json += (name ? ":[]" : "[]");
|
225
|
+
}
|
226
|
+
else if (o instanceof Array) {
|
227
|
+
var n, i, ar=[];
|
228
|
+
for (i = 0, n = o.length; i < n; i += 1) {
|
229
|
+
ar[i] = this.toJson(o[i], "", ind + "\t");
|
230
|
+
}
|
231
|
+
json += (name ? ":[" : "[") + (ar.length > 1 ? ("\n" + ind + "\t" + ar.join(",\n" + ind + "\t") + "\n" + ind) : ar.join("")) + "]";
|
232
|
+
}
|
233
|
+
else if (o === null) {
|
234
|
+
json += (name && ":") + "null";
|
235
|
+
}
|
236
|
+
else if (typeof(o) === "object") {
|
237
|
+
var arr = [];
|
238
|
+
var m;
|
239
|
+
for (m in o) if (o.hasOwnProperty(m)) {
|
240
|
+
arr[arr.length] = this.toJson(o[m], m, ind + "\t");
|
241
|
+
}
|
242
|
+
json += (name ? ":{" : "{") + (arr.length > 1 ? ("\n" + ind + "\t" + arr.join(",\n" + ind + "\t") + "\n" + ind) : arr.join("")) + "}";
|
243
|
+
}
|
244
|
+
else if (typeof(o) === "string") {
|
245
|
+
var objRegExp = /(^-?\d+\.?\d*$)/;
|
246
|
+
var FuncTest = /function/i;
|
247
|
+
var os = o.toString();
|
248
|
+
if (objRegExp.test(os) || FuncTest.test(os) || os==="false" || os==="true") {
|
249
|
+
// int or float
|
250
|
+
json += (name && ":") + os;
|
251
|
+
}
|
252
|
+
else {
|
253
|
+
json += (name && ":") + "\"" + o + "\"";
|
254
|
+
}
|
255
|
+
}
|
256
|
+
else {
|
257
|
+
json += (name && ":") + o.toString();
|
258
|
+
}
|
259
|
+
return json;
|
260
|
+
},
|
261
|
+
innerXml: function(node) {
|
262
|
+
var s = "";
|
263
|
+
if ("innerHTML" in node) {
|
264
|
+
s = node.innerHTML;
|
265
|
+
}
|
266
|
+
else {
|
267
|
+
var asXml = function(n) {
|
268
|
+
var s = "", i;
|
269
|
+
if (n.nodeType === 1) {
|
270
|
+
s += "<" + n.nodeName;
|
271
|
+
for (i = 0; i < n.attributes.length; i += 1) {
|
272
|
+
s += " " + n.attributes[i].nodeName + "=\"" + (n.attributes[i].nodeValue || "").toString() + "\"";
|
273
|
+
}
|
274
|
+
if (n.firstChild) {
|
275
|
+
s += ">";
|
276
|
+
for (var c = n.firstChild; c; c = c.nextSibling) {
|
277
|
+
s += asXml(c);
|
278
|
+
}
|
279
|
+
s += "</" + n.nodeName + ">";
|
280
|
+
}
|
281
|
+
else {
|
282
|
+
s += "/>";
|
283
|
+
}
|
284
|
+
}
|
285
|
+
else if (n.nodeType === 3) {
|
286
|
+
s += n.nodeValue;
|
287
|
+
}
|
288
|
+
else if (n.nodeType === 4) {
|
289
|
+
s += "<![CDATA[" + n.nodeValue + "]]>";
|
290
|
+
}
|
291
|
+
return s;
|
292
|
+
};
|
293
|
+
for (var c = node.firstChild; c; c = c.nextSibling) {
|
294
|
+
s += asXml(c);
|
295
|
+
}
|
296
|
+
}
|
297
|
+
return s;
|
298
|
+
},
|
299
|
+
escape: function(txt) {
|
300
|
+
return txt.replace(/[\\]/g, "\\\\").replace(/[\"]/g, '\\"').replace(/[\n]/g, '\\n').replace(/[\r]/g, '\\r');
|
301
|
+
},
|
302
|
+
removeWhite: function(e) {
|
303
|
+
e.normalize();
|
304
|
+
var n;
|
305
|
+
for (n = e.firstChild; n; ) {
|
306
|
+
if (n.nodeType === 3) {
|
307
|
+
// text node
|
308
|
+
if (!n.nodeValue.match(/[^ \f\n\r\t\v]/)) {
|
309
|
+
// pure whitespace text node
|
310
|
+
var nxt = n.nextSibling;
|
311
|
+
e.removeChild(n);
|
312
|
+
n = nxt;
|
313
|
+
}
|
314
|
+
else {
|
315
|
+
n = n.nextSibling;
|
316
|
+
}
|
317
|
+
}
|
318
|
+
else if (n.nodeType === 1) {
|
319
|
+
// element node
|
320
|
+
this.removeWhite(n);
|
321
|
+
n = n.nextSibling;
|
322
|
+
}
|
323
|
+
else {
|
324
|
+
// any other node
|
325
|
+
n = n.nextSibling;
|
326
|
+
}
|
327
|
+
}
|
328
|
+
return e;
|
329
|
+
}
|
330
330
|
};
|