00001
00002 (function() {
00003 var XMLCData, XMLComment, XMLDeclaration, XMLDocType, XMLElement, XMLNode, XMLRaw, XMLText, isEmpty, isFunction, isObject,
00004 hasProp = {}.hasOwnProperty;
00005
00006 isObject = require('lodash/isObject');
00007
00008 isFunction = require('lodash/isFunction');
00009
00010 isEmpty = require('lodash/isEmpty');
00011
00012 XMLElement = null;
00013
00014 XMLCData = null;
00015
00016 XMLComment = null;
00017
00018 XMLDeclaration = null;
00019
00020 XMLDocType = null;
00021
00022 XMLRaw = null;
00023
00024 XMLText = null;
00025
00026 module.exports = XMLNode = (function() {
00027 function XMLNode(parent) {
00028 this.parent = parent;
00029 this.options = this.parent.options;
00030 this.stringify = this.parent.stringify;
00031 if (XMLElement === null) {
00032 XMLElement = require('./XMLElement');
00033 XMLCData = require('./XMLCData');
00034 XMLComment = require('./XMLComment');
00035 XMLDeclaration = require('./XMLDeclaration');
00036 XMLDocType = require('./XMLDocType');
00037 XMLRaw = require('./XMLRaw');
00038 XMLText = require('./XMLText');
00039 }
00040 }
00041
00042 XMLNode.prototype.element = function(name, attributes, text) {
00043 var childNode, item, j, k, key, lastChild, len, len1, ref, val;
00044 lastChild = null;
00045 if (attributes == null) {
00046 attributes = {};
00047 }
00048 attributes = attributes.valueOf();
00049 if (!isObject(attributes)) {
00050 ref = [attributes, text], text = ref[0], attributes = ref[1];
00051 }
00052 if (name != null) {
00053 name = name.valueOf();
00054 }
00055 if (Array.isArray(name)) {
00056 for (j = 0, len = name.length; j < len; j++) {
00057 item = name[j];
00058 lastChild = this.element(item);
00059 }
00060 } else if (isFunction(name)) {
00061 lastChild = this.element(name.apply());
00062 } else if (isObject(name)) {
00063 for (key in name) {
00064 if (!hasProp.call(name, key)) continue;
00065 val = name[key];
00066 if (isFunction(val)) {
00067 val = val.apply();
00068 }
00069 if ((isObject(val)) && (isEmpty(val))) {
00070 val = null;
00071 }
00072 if (!this.options.ignoreDecorators && this.stringify.convertAttKey && key.indexOf(this.stringify.convertAttKey) === 0) {
00073 lastChild = this.attribute(key.substr(this.stringify.convertAttKey.length), val);
00074 } else if (!this.options.ignoreDecorators && this.stringify.convertPIKey && key.indexOf(this.stringify.convertPIKey) === 0) {
00075 lastChild = this.instruction(key.substr(this.stringify.convertPIKey.length), val);
00076 } else if (!this.options.separateArrayItems && Array.isArray(val)) {
00077 for (k = 0, len1 = val.length; k < len1; k++) {
00078 item = val[k];
00079 childNode = {};
00080 childNode[key] = item;
00081 lastChild = this.element(childNode);
00082 }
00083 } else if (isObject(val)) {
00084 lastChild = this.element(key);
00085 lastChild.element(val);
00086 } else {
00087 lastChild = this.element(key, val);
00088 }
00089 }
00090 } else {
00091 if (!this.options.ignoreDecorators && this.stringify.convertTextKey && name.indexOf(this.stringify.convertTextKey) === 0) {
00092 lastChild = this.text(text);
00093 } else if (!this.options.ignoreDecorators && this.stringify.convertCDataKey && name.indexOf(this.stringify.convertCDataKey) === 0) {
00094 lastChild = this.cdata(text);
00095 } else if (!this.options.ignoreDecorators && this.stringify.convertCommentKey && name.indexOf(this.stringify.convertCommentKey) === 0) {
00096 lastChild = this.comment(text);
00097 } else if (!this.options.ignoreDecorators && this.stringify.convertRawKey && name.indexOf(this.stringify.convertRawKey) === 0) {
00098 lastChild = this.raw(text);
00099 } else {
00100 lastChild = this.node(name, attributes, text);
00101 }
00102 }
00103 if (lastChild == null) {
00104 throw new Error("Could not create any elements with: " + name);
00105 }
00106 return lastChild;
00107 };
00108
00109 XMLNode.prototype.insertBefore = function(name, attributes, text) {
00110 var child, i, removed;
00111 if (this.isRoot) {
00112 throw new Error("Cannot insert elements at root level");
00113 }
00114 i = this.parent.children.indexOf(this);
00115 removed = this.parent.children.splice(i);
00116 child = this.parent.element(name, attributes, text);
00117 Array.prototype.push.apply(this.parent.children, removed);
00118 return child;
00119 };
00120
00121 XMLNode.prototype.insertAfter = function(name, attributes, text) {
00122 var child, i, removed;
00123 if (this.isRoot) {
00124 throw new Error("Cannot insert elements at root level");
00125 }
00126 i = this.parent.children.indexOf(this);
00127 removed = this.parent.children.splice(i + 1);
00128 child = this.parent.element(name, attributes, text);
00129 Array.prototype.push.apply(this.parent.children, removed);
00130 return child;
00131 };
00132
00133 XMLNode.prototype.remove = function() {
00134 var i, ref;
00135 if (this.isRoot) {
00136 throw new Error("Cannot remove the root element");
00137 }
00138 i = this.parent.children.indexOf(this);
00139 [].splice.apply(this.parent.children, [i, i - i + 1].concat(ref = [])), ref;
00140 return this.parent;
00141 };
00142
00143 XMLNode.prototype.node = function(name, attributes, text) {
00144 var child, ref;
00145 if (name != null) {
00146 name = name.valueOf();
00147 }
00148 if (attributes == null) {
00149 attributes = {};
00150 }
00151 attributes = attributes.valueOf();
00152 if (!isObject(attributes)) {
00153 ref = [attributes, text], text = ref[0], attributes = ref[1];
00154 }
00155 child = new XMLElement(this, name, attributes);
00156 if (text != null) {
00157 child.text(text);
00158 }
00159 this.children.push(child);
00160 return child;
00161 };
00162
00163 XMLNode.prototype.text = function(value) {
00164 var child;
00165 child = new XMLText(this, value);
00166 this.children.push(child);
00167 return this;
00168 };
00169
00170 XMLNode.prototype.cdata = function(value) {
00171 var child;
00172 child = new XMLCData(this, value);
00173 this.children.push(child);
00174 return this;
00175 };
00176
00177 XMLNode.prototype.comment = function(value) {
00178 var child;
00179 child = new XMLComment(this, value);
00180 this.children.push(child);
00181 return this;
00182 };
00183
00184 XMLNode.prototype.raw = function(value) {
00185 var child;
00186 child = new XMLRaw(this, value);
00187 this.children.push(child);
00188 return this;
00189 };
00190
00191 XMLNode.prototype.declaration = function(version, encoding, standalone) {
00192 var doc, xmldec;
00193 doc = this.document();
00194 xmldec = new XMLDeclaration(doc, version, encoding, standalone);
00195 doc.xmldec = xmldec;
00196 return doc.root();
00197 };
00198
00199 XMLNode.prototype.doctype = function(pubID, sysID) {
00200 var doc, doctype;
00201 doc = this.document();
00202 doctype = new XMLDocType(doc, pubID, sysID);
00203 doc.doctype = doctype;
00204 return doctype;
00205 };
00206
00207 XMLNode.prototype.up = function() {
00208 if (this.isRoot) {
00209 throw new Error("The root node has no parent. Use doc() if you need to get the document object.");
00210 }
00211 return this.parent;
00212 };
00213
00214 XMLNode.prototype.root = function() {
00215 var child;
00216 if (this.isRoot) {
00217 return this;
00218 }
00219 child = this.parent;
00220 while (!child.isRoot) {
00221 child = child.parent;
00222 }
00223 return child;
00224 };
00225
00226 XMLNode.prototype.document = function() {
00227 return this.root().documentObject;
00228 };
00229
00230 XMLNode.prototype.end = function(options) {
00231 return this.document().toString(options);
00232 };
00233
00234 XMLNode.prototype.prev = function() {
00235 var i;
00236 if (this.isRoot) {
00237 throw new Error("Root node has no siblings");
00238 }
00239 i = this.parent.children.indexOf(this);
00240 if (i < 1) {
00241 throw new Error("Already at the first node");
00242 }
00243 return this.parent.children[i - 1];
00244 };
00245
00246 XMLNode.prototype.next = function() {
00247 var i;
00248 if (this.isRoot) {
00249 throw new Error("Root node has no siblings");
00250 }
00251 i = this.parent.children.indexOf(this);
00252 if (i === -1 || i === this.parent.children.length - 1) {
00253 throw new Error("Already at the last node");
00254 }
00255 return this.parent.children[i + 1];
00256 };
00257
00258 XMLNode.prototype.importXMLBuilder = function(xmlbuilder) {
00259 var clonedRoot;
00260 clonedRoot = xmlbuilder.root().clone();
00261 clonedRoot.parent = this;
00262 clonedRoot.isRoot = false;
00263 this.children.push(clonedRoot);
00264 return this;
00265 };
00266
00267 XMLNode.prototype.ele = function(name, attributes, text) {
00268 return this.element(name, attributes, text);
00269 };
00270
00271 XMLNode.prototype.nod = function(name, attributes, text) {
00272 return this.node(name, attributes, text);
00273 };
00274
00275 XMLNode.prototype.txt = function(value) {
00276 return this.text(value);
00277 };
00278
00279 XMLNode.prototype.dat = function(value) {
00280 return this.cdata(value);
00281 };
00282
00283 XMLNode.prototype.com = function(value) {
00284 return this.comment(value);
00285 };
00286
00287 XMLNode.prototype.doc = function() {
00288 return this.document();
00289 };
00290
00291 XMLNode.prototype.dec = function(version, encoding, standalone) {
00292 return this.declaration(version, encoding, standalone);
00293 };
00294
00295 XMLNode.prototype.dtd = function(pubID, sysID) {
00296 return this.doctype(pubID, sysID);
00297 };
00298
00299 XMLNode.prototype.e = function(name, attributes, text) {
00300 return this.element(name, attributes, text);
00301 };
00302
00303 XMLNode.prototype.n = function(name, attributes, text) {
00304 return this.node(name, attributes, text);
00305 };
00306
00307 XMLNode.prototype.t = function(value) {
00308 return this.text(value);
00309 };
00310
00311 XMLNode.prototype.d = function(value) {
00312 return this.cdata(value);
00313 };
00314
00315 XMLNode.prototype.c = function(value) {
00316 return this.comment(value);
00317 };
00318
00319 XMLNode.prototype.r = function(value) {
00320 return this.raw(value);
00321 };
00322
00323 XMLNode.prototype.u = function() {
00324 return this.up();
00325 };
00326
00327 return XMLNode;
00328
00329 })();
00330
00331 }).call(this);