2016-11-16 01:59:39 +01:00
|
|
|
// Generated by CoffeeScript 1.9.1
|
2016-11-03 01:33:53 +01:00
|
|
|
(function() {
|
2016-11-16 01:59:39 +01:00
|
|
|
var XMLCData, XMLComment, XMLDeclaration, XMLDocType, XMLElement, XMLNode, XMLRaw, XMLText, isEmpty, isFunction, isObject,
|
2016-11-03 01:33:53 +01:00
|
|
|
hasProp = {}.hasOwnProperty;
|
|
|
|
|
2016-11-16 01:59:39 +01:00
|
|
|
isObject = require('lodash/isObject');
|
|
|
|
|
|
|
|
isFunction = require('lodash/isFunction');
|
|
|
|
|
|
|
|
isEmpty = require('lodash/isEmpty');
|
2016-11-03 01:33:53 +01:00
|
|
|
|
|
|
|
XMLElement = null;
|
|
|
|
|
|
|
|
XMLCData = null;
|
|
|
|
|
|
|
|
XMLComment = null;
|
|
|
|
|
|
|
|
XMLDeclaration = null;
|
|
|
|
|
|
|
|
XMLDocType = null;
|
|
|
|
|
|
|
|
XMLRaw = null;
|
|
|
|
|
|
|
|
XMLText = null;
|
|
|
|
|
|
|
|
module.exports = XMLNode = (function() {
|
|
|
|
function XMLNode(parent) {
|
|
|
|
this.parent = parent;
|
2016-11-16 01:59:39 +01:00
|
|
|
this.options = this.parent.options;
|
|
|
|
this.stringify = this.parent.stringify;
|
|
|
|
if (XMLElement === null) {
|
2016-11-03 01:33:53 +01:00
|
|
|
XMLElement = require('./XMLElement');
|
|
|
|
XMLCData = require('./XMLCData');
|
|
|
|
XMLComment = require('./XMLComment');
|
|
|
|
XMLDeclaration = require('./XMLDeclaration');
|
|
|
|
XMLDocType = require('./XMLDocType');
|
|
|
|
XMLRaw = require('./XMLRaw');
|
|
|
|
XMLText = require('./XMLText');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
XMLNode.prototype.element = function(name, attributes, text) {
|
2016-11-16 01:59:39 +01:00
|
|
|
var childNode, item, j, k, key, lastChild, len, len1, ref, val;
|
2016-11-03 01:33:53 +01:00
|
|
|
lastChild = null;
|
|
|
|
if (attributes == null) {
|
|
|
|
attributes = {};
|
|
|
|
}
|
|
|
|
attributes = attributes.valueOf();
|
|
|
|
if (!isObject(attributes)) {
|
2016-11-16 01:59:39 +01:00
|
|
|
ref = [attributes, text], text = ref[0], attributes = ref[1];
|
2016-11-03 01:33:53 +01:00
|
|
|
}
|
|
|
|
if (name != null) {
|
|
|
|
name = name.valueOf();
|
|
|
|
}
|
|
|
|
if (Array.isArray(name)) {
|
|
|
|
for (j = 0, len = name.length; j < len; j++) {
|
|
|
|
item = name[j];
|
|
|
|
lastChild = this.element(item);
|
|
|
|
}
|
|
|
|
} else if (isFunction(name)) {
|
|
|
|
lastChild = this.element(name.apply());
|
|
|
|
} else if (isObject(name)) {
|
|
|
|
for (key in name) {
|
|
|
|
if (!hasProp.call(name, key)) continue;
|
|
|
|
val = name[key];
|
|
|
|
if (isFunction(val)) {
|
|
|
|
val = val.apply();
|
|
|
|
}
|
|
|
|
if ((isObject(val)) && (isEmpty(val))) {
|
|
|
|
val = null;
|
|
|
|
}
|
|
|
|
if (!this.options.ignoreDecorators && this.stringify.convertAttKey && key.indexOf(this.stringify.convertAttKey) === 0) {
|
|
|
|
lastChild = this.attribute(key.substr(this.stringify.convertAttKey.length), val);
|
2016-11-16 01:59:39 +01:00
|
|
|
} else if (!this.options.ignoreDecorators && this.stringify.convertPIKey && key.indexOf(this.stringify.convertPIKey) === 0) {
|
|
|
|
lastChild = this.instruction(key.substr(this.stringify.convertPIKey.length), val);
|
2016-11-03 01:33:53 +01:00
|
|
|
} else if (!this.options.separateArrayItems && Array.isArray(val)) {
|
|
|
|
for (k = 0, len1 = val.length; k < len1; k++) {
|
|
|
|
item = val[k];
|
|
|
|
childNode = {};
|
|
|
|
childNode[key] = item;
|
|
|
|
lastChild = this.element(childNode);
|
|
|
|
}
|
|
|
|
} else if (isObject(val)) {
|
|
|
|
lastChild = this.element(key);
|
|
|
|
lastChild.element(val);
|
|
|
|
} else {
|
|
|
|
lastChild = this.element(key, val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!this.options.ignoreDecorators && this.stringify.convertTextKey && name.indexOf(this.stringify.convertTextKey) === 0) {
|
|
|
|
lastChild = this.text(text);
|
|
|
|
} else if (!this.options.ignoreDecorators && this.stringify.convertCDataKey && name.indexOf(this.stringify.convertCDataKey) === 0) {
|
|
|
|
lastChild = this.cdata(text);
|
|
|
|
} else if (!this.options.ignoreDecorators && this.stringify.convertCommentKey && name.indexOf(this.stringify.convertCommentKey) === 0) {
|
|
|
|
lastChild = this.comment(text);
|
|
|
|
} else if (!this.options.ignoreDecorators && this.stringify.convertRawKey && name.indexOf(this.stringify.convertRawKey) === 0) {
|
|
|
|
lastChild = this.raw(text);
|
|
|
|
} else {
|
|
|
|
lastChild = this.node(name, attributes, text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (lastChild == null) {
|
|
|
|
throw new Error("Could not create any elements with: " + name);
|
|
|
|
}
|
|
|
|
return lastChild;
|
|
|
|
};
|
|
|
|
|
|
|
|
XMLNode.prototype.insertBefore = function(name, attributes, text) {
|
|
|
|
var child, i, removed;
|
|
|
|
if (this.isRoot) {
|
|
|
|
throw new Error("Cannot insert elements at root level");
|
|
|
|
}
|
|
|
|
i = this.parent.children.indexOf(this);
|
|
|
|
removed = this.parent.children.splice(i);
|
|
|
|
child = this.parent.element(name, attributes, text);
|
|
|
|
Array.prototype.push.apply(this.parent.children, removed);
|
|
|
|
return child;
|
|
|
|
};
|
|
|
|
|
|
|
|
XMLNode.prototype.insertAfter = function(name, attributes, text) {
|
|
|
|
var child, i, removed;
|
|
|
|
if (this.isRoot) {
|
|
|
|
throw new Error("Cannot insert elements at root level");
|
|
|
|
}
|
|
|
|
i = this.parent.children.indexOf(this);
|
|
|
|
removed = this.parent.children.splice(i + 1);
|
|
|
|
child = this.parent.element(name, attributes, text);
|
|
|
|
Array.prototype.push.apply(this.parent.children, removed);
|
|
|
|
return child;
|
|
|
|
};
|
|
|
|
|
|
|
|
XMLNode.prototype.remove = function() {
|
2016-11-16 01:59:39 +01:00
|
|
|
var i, ref;
|
2016-11-03 01:33:53 +01:00
|
|
|
if (this.isRoot) {
|
|
|
|
throw new Error("Cannot remove the root element");
|
|
|
|
}
|
|
|
|
i = this.parent.children.indexOf(this);
|
2016-11-16 01:59:39 +01:00
|
|
|
[].splice.apply(this.parent.children, [i, i - i + 1].concat(ref = [])), ref;
|
2016-11-03 01:33:53 +01:00
|
|
|
return this.parent;
|
|
|
|
};
|
|
|
|
|
|
|
|
XMLNode.prototype.node = function(name, attributes, text) {
|
2016-11-16 01:59:39 +01:00
|
|
|
var child, ref;
|
2016-11-03 01:33:53 +01:00
|
|
|
if (name != null) {
|
|
|
|
name = name.valueOf();
|
|
|
|
}
|
2016-11-16 01:59:39 +01:00
|
|
|
if (attributes == null) {
|
|
|
|
attributes = {};
|
|
|
|
}
|
2016-11-03 01:33:53 +01:00
|
|
|
attributes = attributes.valueOf();
|
|
|
|
if (!isObject(attributes)) {
|
2016-11-16 01:59:39 +01:00
|
|
|
ref = [attributes, text], text = ref[0], attributes = ref[1];
|
2016-11-03 01:33:53 +01:00
|
|
|
}
|
|
|
|
child = new XMLElement(this, name, attributes);
|
|
|
|
if (text != null) {
|
|
|
|
child.text(text);
|
|
|
|
}
|
|
|
|
this.children.push(child);
|
|
|
|
return child;
|
|
|
|
};
|
|
|
|
|
|
|
|
XMLNode.prototype.text = function(value) {
|
|
|
|
var child;
|
|
|
|
child = new XMLText(this, value);
|
|
|
|
this.children.push(child);
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
XMLNode.prototype.cdata = function(value) {
|
|
|
|
var child;
|
|
|
|
child = new XMLCData(this, value);
|
|
|
|
this.children.push(child);
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
XMLNode.prototype.comment = function(value) {
|
|
|
|
var child;
|
|
|
|
child = new XMLComment(this, value);
|
|
|
|
this.children.push(child);
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
XMLNode.prototype.raw = function(value) {
|
|
|
|
var child;
|
|
|
|
child = new XMLRaw(this, value);
|
|
|
|
this.children.push(child);
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
XMLNode.prototype.declaration = function(version, encoding, standalone) {
|
|
|
|
var doc, xmldec;
|
|
|
|
doc = this.document();
|
|
|
|
xmldec = new XMLDeclaration(doc, version, encoding, standalone);
|
2016-11-16 01:59:39 +01:00
|
|
|
doc.xmldec = xmldec;
|
|
|
|
return doc.root();
|
2016-11-03 01:33:53 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
XMLNode.prototype.doctype = function(pubID, sysID) {
|
2016-11-16 01:59:39 +01:00
|
|
|
var doc, doctype;
|
2016-11-03 01:33:53 +01:00
|
|
|
doc = this.document();
|
|
|
|
doctype = new XMLDocType(doc, pubID, sysID);
|
2016-11-16 01:59:39 +01:00
|
|
|
doc.doctype = doctype;
|
2016-11-03 01:33:53 +01:00
|
|
|
return doctype;
|
|
|
|
};
|
|
|
|
|
|
|
|
XMLNode.prototype.up = function() {
|
|
|
|
if (this.isRoot) {
|
|
|
|
throw new Error("The root node has no parent. Use doc() if you need to get the document object.");
|
|
|
|
}
|
|
|
|
return this.parent;
|
|
|
|
};
|
|
|
|
|
|
|
|
XMLNode.prototype.root = function() {
|
2016-11-16 01:59:39 +01:00
|
|
|
var child;
|
|
|
|
if (this.isRoot) {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
child = this.parent;
|
|
|
|
while (!child.isRoot) {
|
|
|
|
child = child.parent;
|
2016-11-03 01:33:53 +01:00
|
|
|
}
|
2016-11-16 01:59:39 +01:00
|
|
|
return child;
|
2016-11-03 01:33:53 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
XMLNode.prototype.document = function() {
|
2016-11-16 01:59:39 +01:00
|
|
|
return this.root().documentObject;
|
2016-11-03 01:33:53 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
XMLNode.prototype.end = function(options) {
|
2016-11-16 01:59:39 +01:00
|
|
|
return this.document().toString(options);
|
2016-11-03 01:33:53 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
XMLNode.prototype.prev = function() {
|
|
|
|
var i;
|
2016-11-16 01:59:39 +01:00
|
|
|
if (this.isRoot) {
|
|
|
|
throw new Error("Root node has no siblings");
|
|
|
|
}
|
2016-11-03 01:33:53 +01:00
|
|
|
i = this.parent.children.indexOf(this);
|
|
|
|
if (i < 1) {
|
|
|
|
throw new Error("Already at the first node");
|
|
|
|
}
|
|
|
|
return this.parent.children[i - 1];
|
|
|
|
};
|
|
|
|
|
|
|
|
XMLNode.prototype.next = function() {
|
|
|
|
var i;
|
2016-11-16 01:59:39 +01:00
|
|
|
if (this.isRoot) {
|
|
|
|
throw new Error("Root node has no siblings");
|
|
|
|
}
|
2016-11-03 01:33:53 +01:00
|
|
|
i = this.parent.children.indexOf(this);
|
|
|
|
if (i === -1 || i === this.parent.children.length - 1) {
|
|
|
|
throw new Error("Already at the last node");
|
|
|
|
}
|
|
|
|
return this.parent.children[i + 1];
|
|
|
|
};
|
|
|
|
|
2016-11-16 01:59:39 +01:00
|
|
|
XMLNode.prototype.importXMLBuilder = function(xmlbuilder) {
|
2016-11-03 01:33:53 +01:00
|
|
|
var clonedRoot;
|
2016-11-16 01:59:39 +01:00
|
|
|
clonedRoot = xmlbuilder.root().clone();
|
2016-11-03 01:33:53 +01:00
|
|
|
clonedRoot.parent = this;
|
|
|
|
clonedRoot.isRoot = false;
|
|
|
|
this.children.push(clonedRoot);
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
XMLNode.prototype.ele = function(name, attributes, text) {
|
|
|
|
return this.element(name, attributes, text);
|
|
|
|
};
|
|
|
|
|
|
|
|
XMLNode.prototype.nod = function(name, attributes, text) {
|
|
|
|
return this.node(name, attributes, text);
|
|
|
|
};
|
|
|
|
|
|
|
|
XMLNode.prototype.txt = function(value) {
|
|
|
|
return this.text(value);
|
|
|
|
};
|
|
|
|
|
|
|
|
XMLNode.prototype.dat = function(value) {
|
|
|
|
return this.cdata(value);
|
|
|
|
};
|
|
|
|
|
|
|
|
XMLNode.prototype.com = function(value) {
|
|
|
|
return this.comment(value);
|
|
|
|
};
|
|
|
|
|
|
|
|
XMLNode.prototype.doc = function() {
|
|
|
|
return this.document();
|
|
|
|
};
|
|
|
|
|
|
|
|
XMLNode.prototype.dec = function(version, encoding, standalone) {
|
|
|
|
return this.declaration(version, encoding, standalone);
|
|
|
|
};
|
|
|
|
|
|
|
|
XMLNode.prototype.dtd = function(pubID, sysID) {
|
|
|
|
return this.doctype(pubID, sysID);
|
|
|
|
};
|
|
|
|
|
|
|
|
XMLNode.prototype.e = function(name, attributes, text) {
|
|
|
|
return this.element(name, attributes, text);
|
|
|
|
};
|
|
|
|
|
|
|
|
XMLNode.prototype.n = function(name, attributes, text) {
|
|
|
|
return this.node(name, attributes, text);
|
|
|
|
};
|
|
|
|
|
|
|
|
XMLNode.prototype.t = function(value) {
|
|
|
|
return this.text(value);
|
|
|
|
};
|
|
|
|
|
|
|
|
XMLNode.prototype.d = function(value) {
|
|
|
|
return this.cdata(value);
|
|
|
|
};
|
|
|
|
|
|
|
|
XMLNode.prototype.c = function(value) {
|
|
|
|
return this.comment(value);
|
|
|
|
};
|
|
|
|
|
|
|
|
XMLNode.prototype.r = function(value) {
|
|
|
|
return this.raw(value);
|
|
|
|
};
|
|
|
|
|
|
|
|
XMLNode.prototype.u = function() {
|
|
|
|
return this.up();
|
|
|
|
};
|
|
|
|
|
|
|
|
return XMLNode;
|
|
|
|
|
|
|
|
})();
|
|
|
|
|
|
|
|
}).call(this);
|