3 var XMLCData, XMLComment, XMLDeclaration, XMLDocType, XMLElement, XMLNode, XMLProcessingInstruction, XMLRaw, XMLText, isEmpty, isFunction, isObject, ref,
4 hasProp = {}.hasOwnProperty;
6 ref = require(
'./Utility'), isObject = ref.isObject, isFunction = ref.isFunction, isEmpty = ref.isEmpty;
14 XMLDeclaration = null;
22 XMLProcessingInstruction = null;
24 module.exports = XMLNode = (
function() {
25 function XMLNode(parent) {
28 this.options = this.parent.options;
29 this.stringify = this.parent.stringify;
33 XMLElement = require(
'./XMLElement');
34 XMLCData = require(
'./XMLCData');
35 XMLComment = require(
'./XMLComment');
36 XMLDeclaration = require(
'./XMLDeclaration');
37 XMLDocType = require(
'./XMLDocType');
38 XMLRaw = require(
'./XMLRaw');
39 XMLText = require(
'./XMLText');
40 XMLProcessingInstruction = require(
'./XMLProcessingInstruction');
44 XMLNode.prototype.element =
function(name, attributes, text) {
45 var childNode, item, j, k, key, lastChild, len, len1, ref1, val;
47 if (attributes == null) {
50 attributes = attributes.valueOf();
51 if (!isObject(attributes)) {
52 ref1 = [attributes, text], text = ref1[0], attributes = ref1[1];
55 name = name.valueOf();
57 if (Array.isArray(name)) {
58 for (j = 0, len = name.length; j < len; j++) {
60 lastChild = this.element(item);
62 }
else if (isFunction(name)) {
63 lastChild = this.element(name.apply());
64 }
else if (isObject(name)) {
66 if (!hasProp.call(name, key))
continue;
68 if (isFunction(val)) {
71 if ((isObject(val)) && (isEmpty(val))) {
74 if (!this.options.ignoreDecorators &&
this.stringify.convertAttKey && key.indexOf(
this.stringify.convertAttKey) === 0) {
75 lastChild = this.attribute(key.substr(
this.stringify.convertAttKey.length), val);
76 }
else if (!this.options.separateArrayItems && Array.isArray(val)) {
77 for (k = 0, len1 = val.length; k < len1; k++) {
80 childNode[key] = item;
81 lastChild = this.element(childNode);
83 }
else if (isObject(val)) {
84 lastChild = this.element(key);
85 lastChild.element(val);
87 lastChild = this.element(key, val);
91 if (!this.options.ignoreDecorators &&
this.stringify.convertTextKey && name.indexOf(
this.stringify.convertTextKey) === 0) {
92 lastChild = this.text(text);
93 }
else if (!this.options.ignoreDecorators &&
this.stringify.convertCDataKey && name.indexOf(
this.stringify.convertCDataKey) === 0) {
94 lastChild = this.cdata(text);
95 }
else if (!this.options.ignoreDecorators &&
this.stringify.convertCommentKey && name.indexOf(
this.stringify.convertCommentKey) === 0) {
96 lastChild = this.comment(text);
97 }
else if (!this.options.ignoreDecorators &&
this.stringify.convertRawKey && name.indexOf(
this.stringify.convertRawKey) === 0) {
98 lastChild = this.raw(text);
99 }
else if (!this.options.ignoreDecorators &&
this.stringify.convertPIKey && name.indexOf(
this.stringify.convertPIKey) === 0) {
100 lastChild = this.instruction(name.substr(
this.stringify.convertPIKey.length), text);
102 lastChild = this.node(name, attributes, text);
105 if (lastChild == null) {
106 throw new Error(
"Could not create any elements with: " + name);
111 XMLNode.prototype.insertBefore =
function(name, attributes, text) {
112 var child, i, removed;
114 throw new Error(
"Cannot insert elements at root level");
116 i = this.parent.children.indexOf(
this);
117 removed = this.parent.children.splice(i);
118 child = this.parent.element(name, attributes, text);
119 Array.prototype.push.apply(this.parent.children, removed);
123 XMLNode.prototype.insertAfter =
function(name, attributes, text) {
124 var child, i, removed;
126 throw new Error(
"Cannot insert elements at root level");
128 i = this.parent.children.indexOf(
this);
129 removed = this.parent.children.splice(i + 1);
130 child = this.parent.element(name, attributes, text);
131 Array.prototype.push.apply(this.parent.children, removed);
135 XMLNode.prototype.remove =
function() {
138 throw new Error(
"Cannot remove the root element");
140 i = this.parent.children.indexOf(
this);
141 [].splice.apply(this.parent.children, [i, i - i + 1].concat(ref1 = [])), ref1;
145 XMLNode.prototype.node =
function(name, attributes, text) {
148 name = name.valueOf();
150 attributes || (attributes = {});
151 attributes = attributes.valueOf();
152 if (!isObject(attributes)) {
153 ref1 = [attributes, text], text = ref1[0], attributes = ref1[1];
155 child =
new XMLElement(
this, name, attributes);
159 this.children.push(child);
163 XMLNode.prototype.text =
function(value) {
165 child =
new XMLText(
this, value);
166 this.children.push(child);
170 XMLNode.prototype.cdata =
function(value) {
172 child =
new XMLCData(
this, value);
173 this.children.push(child);
177 XMLNode.prototype.comment =
function(value) {
179 child =
new XMLComment(
this, value);
180 this.children.push(child);
184 XMLNode.prototype.commentBefore =
function(value) {
185 var child, i, removed;
186 i = this.parent.children.indexOf(
this);
187 removed = this.parent.children.splice(i);
188 child = this.parent.comment(value);
189 Array.prototype.push.apply(this.parent.children, removed);
193 XMLNode.prototype.commentAfter =
function(value) {
194 var child, i, removed;
195 i = this.parent.children.indexOf(
this);
196 removed = this.parent.children.splice(i + 1);
197 child = this.parent.comment(value);
198 Array.prototype.push.apply(this.parent.children, removed);
202 XMLNode.prototype.raw =
function(value) {
204 child =
new XMLRaw(
this, value);
205 this.children.push(child);
209 XMLNode.prototype.instruction =
function(target, value) {
210 var insTarget, insValue, instruction, j, len;
211 if (target != null) {
212 target = target.valueOf();
215 value = value.valueOf();
217 if (Array.isArray(target)) {
218 for (j = 0, len = target.length; j < len; j++) {
219 insTarget = target[j];
220 this.instruction(insTarget);
222 }
else if (isObject(target)) {
223 for (insTarget in target) {
224 if (!hasProp.call(target, insTarget))
continue;
225 insValue = target[insTarget];
226 this.instruction(insTarget, insValue);
229 if (isFunction(value)) {
230 value = value.apply();
232 instruction =
new XMLProcessingInstruction(
this, target, value);
233 this.children.push(instruction);
238 XMLNode.prototype.instructionBefore =
function(target, value) {
239 var child, i, removed;
240 i = this.parent.children.indexOf(
this);
241 removed = this.parent.children.splice(i);
242 child = this.parent.instruction(target, value);
243 Array.prototype.push.apply(this.parent.children, removed);
247 XMLNode.prototype.instructionAfter =
function(target, value) {
248 var child, i, removed;
249 i = this.parent.children.indexOf(
this);
250 removed = this.parent.children.splice(i + 1);
251 child = this.parent.instruction(target, value);
252 Array.prototype.push.apply(this.parent.children, removed);
256 XMLNode.prototype.declaration =
function(version, encoding, standalone) {
258 doc = this.document();
259 xmldec =
new XMLDeclaration(doc, version, encoding, standalone);
260 if (doc.children[0] instanceof XMLDeclaration) {
261 doc.children[0] = xmldec;
263 doc.children.unshift(xmldec);
265 return doc.root() || doc;
268 XMLNode.prototype.doctype =
function(pubID, sysID) {
269 var child, doc, doctype, i, j, k, len, len1, ref1, ref2;
270 doc = this.document();
271 doctype =
new XMLDocType(doc, pubID, sysID);
273 for (i = j = 0, len = ref1.length; j < len; i = ++j) {
275 if (child instanceof XMLDocType) {
276 doc.children[i] = doctype;
281 for (i = k = 0, len1 = ref2.length; k < len1; i = ++k) {
284 doc.children.splice(i, 0, doctype);
288 doc.children.push(doctype);
292 XMLNode.prototype.up =
function() {
294 throw new Error(
"The root node has no parent. Use doc() if you need to get the document object.");
299 XMLNode.prototype.root =
function() {
303 if (node.isDocument) {
304 return node.rootObject;
305 }
else if (node.isRoot) {
313 XMLNode.prototype.document =
function() {
317 if (node.isDocument) {
325 XMLNode.prototype.end =
function(options) {
326 return this.document().end(options);
329 XMLNode.prototype.prev =
function() {
331 i = this.parent.children.indexOf(
this);
333 throw new Error(
"Already at the first node");
335 return this.parent.children[i - 1];
338 XMLNode.prototype.next =
function() {
340 i = this.parent.children.indexOf(
this);
341 if (i === -1 || i === this.parent.children.length - 1) {
342 throw new Error(
"Already at the last node");
344 return this.parent.children[i + 1];
347 XMLNode.prototype.importDocument =
function(doc) {
349 clonedRoot = doc.root().clone();
350 clonedRoot.parent =
this;
351 clonedRoot.isRoot =
false;
352 this.children.push(clonedRoot);
356 XMLNode.prototype.ele =
function(name, attributes, text) {
357 return this.element(name, attributes, text);
360 XMLNode.prototype.nod =
function(name, attributes, text) {
361 return this.node(name, attributes, text);
364 XMLNode.prototype.txt =
function(value) {
365 return this.text(value);
368 XMLNode.prototype.dat =
function(value) {
369 return this.cdata(value);
372 XMLNode.prototype.com =
function(value) {
373 return this.comment(value);
376 XMLNode.prototype.ins =
function(target, value) {
377 return this.instruction(target, value);
380 XMLNode.prototype.doc =
function() {
381 return this.document();
384 XMLNode.prototype.dec =
function(version, encoding, standalone) {
385 return this.declaration(version, encoding, standalone);
388 XMLNode.prototype.dtd =
function(pubID, sysID) {
389 return this.doctype(pubID, sysID);
392 XMLNode.prototype.e =
function(name, attributes, text) {
393 return this.element(name, attributes, text);
396 XMLNode.prototype.n =
function(name, attributes, text) {
397 return this.node(name, attributes, text);
400 XMLNode.prototype.t =
function(value) {
401 return this.text(value);
404 XMLNode.prototype.d =
function(value) {
405 return this.cdata(value);
408 XMLNode.prototype.c =
function(value) {
409 return this.comment(value);
412 XMLNode.prototype.r =
function(value) {
413 return this.raw(value);
416 XMLNode.prototype.i =
function(target, value) {
417 return this.instruction(target, value);
420 XMLNode.prototype.u =
function() {
424 XMLNode.prototype.importXMLBuilder =
function(doc) {
425 return this.importDocument(doc);