-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxmlQuery.js
More file actions
295 lines (256 loc) · 9.14 KB
/
xmlQuery.js
File metadata and controls
295 lines (256 loc) · 9.14 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/* ================================================
* xmlQuery.js v1.0.0beta
* https://github.com/laubstein/xmlQuery
* ================================================
* Copyright 2013 Thiago Laubstein
*
* Released under the MIT license
* http://www.opensource.org/licenses/MIT
*
* Based on jQuery - (http://jquery.com)
*/
/*global define, window, document, DOMParser, ActiveXObject, XMLSerializer */
(function (window) {
var
xmlQuery = function (element, context) {
return new xmlQuery.fn.init(element, context);
};
xmlQuery.fn = xmlQuery.prototype = {
constructor: xmlQuery,
sort: [].sort,
splice: [].splice,
push: [].push,
length: 0,
size: function () {
return this.length;
},
// Based on 'extend' method from jQuery (http://jquery.com).
// Take an array of elements and push it onto the stack
// (returning the new matched element set)
pushStack: function (elems) {
// Build a new jQuery matched element set
var ret = xmlQuery.merge(this.constructor(), elems);
// Add the old object onto the stack (as a reference)
ret.prevObject = this;
ret.context = this.context;
// Return the newly-formed element set
return ret;
},
init: function (element, context) {
var e, i, len, parent, child, el = [];
if (element !== null && element !== undefined) {
if (context) {
parent = xmlQuery(context);
element = parent.find(element);
for (i = 0, len = element.length; i < len; i++) {
el.push(element[i]);
}
}
if (element.length !== undefined && typeof element === 'string') {
return this.append(element);
}
if (element.type === 'xmlQuery') {
return element;
}
if (element.length !== undefined && !element.type) {
if (element.length > 0) {
el = el.concat(element);
}
} else {
el.push((element.documentElement || element));
}
} else {
return this;
}
return this.append(el);
}
};
xmlQuery.fn.init.prototype = xmlQuery.fn;
// Based on 'extend' method from jQuery (http://jquery.com).
xmlQuery.extend = xmlQuery.fn.extend = function (plainObject) {
var src, copyIsArray, copy, name, options, clone,
target = this;
// Only deal with non-null/undefined values
if ((options = plainObject) !== null) {
// Extend the base object
for (name in options) {
if (options.hasOwnProperty(name)) {
src = target[name];
copy = options[name];
// Prevent never-ending loop
if (target === copy) {
continue;
}
// Recurse if we're merging plain objects or arrays
if (copy && typeof copy === 'object') {
clone = src;
// Never move original objects, clone them
target[name] = xmlQuery.extend(copy);
// Don't bring in undefined values
} else if (copy !== undefined) {
target[name] = copy;
}
}
}
}
// Return the modified object
return target;
};
xmlQuery.extend({
error: function (msg) {
throw new Error(msg);
},
serializeXML: function (xmlObject) {
var xml;
try {
if (!window.XMLSerializer) {
window.XMLSerializer = function () {};
window.XMLSerializer.prototype.serializeToString = function (xmlObject) {
return xmlObject.xml || '';
};
}
xml = (new XMLSerializer()).serializeToString(xmlObject);
} catch (e) {
xml = undefined;
}
if (!xml) {
xmlQuery.error('Invalid XML Object');
}
return xml;
},
parseXML: function (data) {
var xml, tmp;
if (!data || typeof data !== 'string') {
return null;
}
try {
if (window.DOMParser) { // Standard
tmp = new DOMParser();
xml = tmp.parseFromString(data, 'text/xml');
} else { // IE
xml = new ActiveXObject('Microsoft.XMLDOM');
xml.async = 'false';
xml.loadXML(data);
}
} catch (e) {
xml = undefined;
}
if (!xml || !xml.documentElement || xml.getElementsByTagName('parsererror').length) {
xmlQuery.error('Invalid XML: ' + data);
}
return xml;
},
// Based on 'extend' method from jQuery (http://jquery.com).
merge: function (first, second) {
var l = second.length,
i = first.length,
j;
if (typeof l === 'number') {
for (j = 0; j < l; j++) {
first[i++] = second[j];
}
} else {
while (second[j] !== undefined) {
first[i++] = second[j++];
}
}
first.length = i;
return first;
}
});
xmlQuery.fn.extend({
type: 'xmlQuery',
attr: function (name, value) {
var i;
if (value === undefined) {
return this[0].getAttribute(name) || undefined;
}
for (i = 0; i < this.length; i++) {
if (value !== null && value !== undefined) {
this[i].setAttribute(name, value);
}
}
return this;
},
find: function (element) {
var i, j, found,
ret = [];
for (i = 0; i < this.length; i++) {
found = this[i].getElementsByTagName(element);
for (j = 0; j < found.length; j++) {
ret.push(found[j]);
}
}
return this.pushStack(ret);
},
append: function (value) {
var i, e, len, cloned = [];
// string
if (value.length && typeof value === 'string') {
value = xmlQuery.parseXML('<root>' + value + '</root>').documentElement.childNodes;
for (i = 0, len = value.length; i < len; i++) {
cloned.push((value[i].documentElement || value[i]).cloneNode(true));
}
value = cloned;
}
if (typeof value.length === 'number' && this.length === 1) {
// array
for (i = 0, len = value.length; i < len; i++) {
this.append(value[i]);
}
return this;
}
// object
if (this.length === 0) {
if (value.length === undefined && typeof value === 'object') {
value = [value];
}
for (i = 0, len = value.length; i < len; i++) {
this.push(value[i]);
}
} else {
for (i = 0, len = this.length; i < len; i++) {
if (this[i].importNode) {
e = this[i].importNode(value);
} else {
this[i].appendChild((value.documentElement || value).cloneNode(true));
}
}
}
return this;
},
serialize: function () {
var ret;
if (this.length > 1) {
ret = xmlQuery('<root></root>').append(this)[0];
} else {
ret = this[0];
}
return xmlQuery.serializeXML(ret).replace(/ ?xmlns="[^\"]*" ?/g, ' ');
},
toString: function () {
return this.serialize();
},
first: function () {
return this.length > 0 ? xmlQuery(this[0]) : this;
},
last: function () {
return this.length > 0 ? xmlQuery(this[this.length - 1]) : this;
},
eq: function (idx) {
return xmlQuery(this[idx]);
},
size: function () {
return this.length;
},
clone: function (deep) {
var i, len, cloned = this.constructor();
deep = deep === undefined ? true : deep;
for (i = 0, len = this.length; i < len; i++) {
cloned.append(this[i].cloneNode(deep));
}
return cloned;
}
});
window.xmlQuery = window.$X = xmlQuery;
}(window));