Initial commit
This commit is contained in:
commit
ec49fedc45
1288 files changed
+226749
No files matched your search
+136
@@ -0,0 +1,136 @@
|
||||
// Generated by CoffeeScript 1.3.3
|
||||
(function() {
|
||||
var XMLBuilder, XMLFragment,
|
||||
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
|
||||
|
||||
XMLFragment = require('./XMLFragment');
|
||||
|
||||
XMLBuilder = (function() {
|
||||
|
||||
function XMLBuilder(name, xmldec, doctype, options) {
|
||||
this.assertLegalChar = __bind(this.assertLegalChar, this);
|
||||
|
||||
var att, child, _ref;
|
||||
this.children = [];
|
||||
this.rootObject = null;
|
||||
if (this.is(name, 'Object')) {
|
||||
_ref = [name, xmldec], xmldec = _ref[0], doctype = _ref[1];
|
||||
name = null;
|
||||
}
|
||||
if (name != null) {
|
||||
name = '' + name || '';
|
||||
if (xmldec == null) {
|
||||
xmldec = {
|
||||
'version': '1.0'
|
||||
};
|
||||
}
|
||||
}
|
||||
this.allowSurrogateChars = options != null ? options.allowSurrogateChars : void 0;
|
||||
if ((xmldec != null) && !(xmldec.version != null)) {
|
||||
throw new Error("Version number is required");
|
||||
}
|
||||
if (xmldec != null) {
|
||||
xmldec.version = '' + xmldec.version || '';
|
||||
if (!xmldec.version.match(/1\.[0-9]+/)) {
|
||||
throw new Error("Invalid version number: " + xmldec.version);
|
||||
}
|
||||
att = {
|
||||
version: xmldec.version
|
||||
};
|
||||
if (xmldec.encoding != null) {
|
||||
xmldec.encoding = '' + xmldec.encoding || '';
|
||||
if (!xmldec.encoding.match(/[A-Za-z](?:[A-Za-z0-9._-]|-)*/)) {
|
||||
throw new Error("Invalid encoding: " + xmldec.encoding);
|
||||
}
|
||||
att.encoding = xmldec.encoding;
|
||||
}
|
||||
if (xmldec.standalone != null) {
|
||||
att.standalone = xmldec.standalone ? "yes" : "no";
|
||||
}
|
||||
child = new XMLFragment(this, '?xml', att);
|
||||
this.children.push(child);
|
||||
}
|
||||
if (doctype != null) {
|
||||
att = {};
|
||||
if (name != null) {
|
||||
att.name = name;
|
||||
}
|
||||
if (doctype.ext != null) {
|
||||
doctype.ext = '' + doctype.ext || '';
|
||||
att.ext = doctype.ext;
|
||||
}
|
||||
child = new XMLFragment(this, '!DOCTYPE', att);
|
||||
this.children.push(child);
|
||||
}
|
||||
if (name != null) {
|
||||
this.begin(name);
|
||||
}
|
||||
}
|
||||
|
||||
XMLBuilder.prototype.begin = function(name, xmldec, doctype) {
|
||||
var doc, root;
|
||||
if (!(name != null)) {
|
||||
throw new Error("Root element needs a name");
|
||||
}
|
||||
if (this.rootObject) {
|
||||
this.children = [];
|
||||
this.rootObject = null;
|
||||
}
|
||||
if (xmldec != null) {
|
||||
doc = new XMLBuilder(name, xmldec, doctype);
|
||||
return doc.root();
|
||||
}
|
||||
name = '' + name || '';
|
||||
root = new XMLFragment(this, name, {});
|
||||
root.isRoot = true;
|
||||
root.documentObject = this;
|
||||
this.children.push(root);
|
||||
this.rootObject = root;
|
||||
return root;
|
||||
};
|
||||
|
||||
XMLBuilder.prototype.root = function() {
|
||||
return this.rootObject;
|
||||
};
|
||||
|
||||
XMLBuilder.prototype.end = function(options) {
|
||||
return toString(options);
|
||||
};
|
||||
|
||||
XMLBuilder.prototype.toString = function(options) {
|
||||
var child, r, _i, _len, _ref;
|
||||
r = '';
|
||||
_ref = this.children;
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
child = _ref[_i];
|
||||
r += child.toString(options);
|
||||
}
|
||||
return r;
|
||||
};
|
||||
|
||||
XMLBuilder.prototype.is = function(obj, type) {
|
||||
var clas;
|
||||
clas = Object.prototype.toString.call(obj).slice(8, -1);
|
||||
return (obj != null) && clas === type;
|
||||
};
|
||||
|
||||
XMLBuilder.prototype.assertLegalChar = function(str) {
|
||||
var chars, chr;
|
||||
if (this.allowSurrogateChars) {
|
||||
chars = /[\u0000-\u0008\u000B-\u000C\u000E-\u001F\uFFFE-\uFFFF]/;
|
||||
} else {
|
||||
chars = /[\u0000-\u0008\u000B-\u000C\u000E-\u001F\uD800-\uDFFF\uFFFE-\uFFFF]/;
|
||||
}
|
||||
chr = str.match(chars);
|
||||
if (chr) {
|
||||
throw new Error("Invalid character (" + chr + ") in string: " + str + " at index " + chr.index);
|
||||
}
|
||||
};
|
||||
|
||||
return XMLBuilder;
|
||||
|
||||
})();
|
||||
|
||||
module.exports = XMLBuilder;
|
||||
|
||||
}).call(this);
|
||||
+415
@@ -0,0 +1,415 @@
|
||||
// Generated by CoffeeScript 1.3.3
|
||||
(function() {
|
||||
var XMLFragment,
|
||||
__hasProp = {}.hasOwnProperty;
|
||||
|
||||
XMLFragment = (function() {
|
||||
|
||||
function XMLFragment(parent, name, attributes, text) {
|
||||
this.isRoot = false;
|
||||
this.documentObject = null;
|
||||
this.parent = parent;
|
||||
this.name = name;
|
||||
this.attributes = attributes;
|
||||
this.value = text;
|
||||
this.children = [];
|
||||
this.assertLegalChar = parent.assertLegalChar;
|
||||
}
|
||||
|
||||
XMLFragment.prototype.element = function(name, attributes, text) {
|
||||
var child, key, val, _ref, _ref1;
|
||||
if (!(name != null)) {
|
||||
throw new Error("Missing element name");
|
||||
}
|
||||
name = '' + name || '';
|
||||
this.assertLegalChar(name);
|
||||
if (attributes == null) {
|
||||
attributes = {};
|
||||
}
|
||||
if (this.is(attributes, 'String') && this.is(text, 'Object')) {
|
||||
_ref = [text, attributes], attributes = _ref[0], text = _ref[1];
|
||||
} else if (this.is(attributes, 'String')) {
|
||||
_ref1 = [{}, attributes], attributes = _ref1[0], text = _ref1[1];
|
||||
}
|
||||
for (key in attributes) {
|
||||
if (!__hasProp.call(attributes, key)) continue;
|
||||
val = attributes[key];
|
||||
val = '' + val || '';
|
||||
attributes[key] = this.escape(val);
|
||||
}
|
||||
child = new XMLFragment(this, name, attributes);
|
||||
if (text != null) {
|
||||
text = '' + text || '';
|
||||
text = this.escape(text);
|
||||
this.assertLegalChar(text);
|
||||
child.raw(text);
|
||||
}
|
||||
this.children.push(child);
|
||||
return child;
|
||||
};
|
||||
|
||||
XMLFragment.prototype.insertBefore = function(name, attributes, text) {
|
||||
var child, i, key, val, _ref, _ref1;
|
||||
if (this.isRoot) {
|
||||
throw new Error("Cannot insert elements at root level");
|
||||
}
|
||||
if (!(name != null)) {
|
||||
throw new Error("Missing element name");
|
||||
}
|
||||
name = '' + name || '';
|
||||
this.assertLegalChar(name);
|
||||
if (attributes == null) {
|
||||
attributes = {};
|
||||
}
|
||||
if (this.is(attributes, 'String') && this.is(text, 'Object')) {
|
||||
_ref = [text, attributes], attributes = _ref[0], text = _ref[1];
|
||||
} else if (this.is(attributes, 'String')) {
|
||||
_ref1 = [{}, attributes], attributes = _ref1[0], text = _ref1[1];
|
||||
}
|
||||
for (key in attributes) {
|
||||
if (!__hasProp.call(attributes, key)) continue;
|
||||
val = attributes[key];
|
||||
val = '' + val || '';
|
||||
attributes[key] = this.escape(val);
|
||||
}
|
||||
child = new XMLFragment(this.parent, name, attributes);
|
||||
if (text != null) {
|
||||
text = '' + text || '';
|
||||
text = this.escape(text);
|
||||
this.assertLegalChar(text);
|
||||
child.raw(text);
|
||||
}
|
||||
i = this.parent.children.indexOf(this);
|
||||
this.parent.children.splice(i, 0, child);
|
||||
return child;
|
||||
};
|
||||
|
||||
XMLFragment.prototype.insertAfter = function(name, attributes, text) {
|
||||
var child, i, key, val, _ref, _ref1;
|
||||
if (this.isRoot) {
|
||||
throw new Error("Cannot insert elements at root level");
|
||||
}
|
||||
if (!(name != null)) {
|
||||
throw new Error("Missing element name");
|
||||
}
|
||||
name = '' + name || '';
|
||||
this.assertLegalChar(name);
|
||||
if (attributes == null) {
|
||||
attributes = {};
|
||||
}
|
||||
if (this.is(attributes, 'String') && this.is(text, 'Object')) {
|
||||
_ref = [text, attributes], attributes = _ref[0], text = _ref[1];
|
||||
} else if (this.is(attributes, 'String')) {
|
||||
_ref1 = [{}, attributes], attributes = _ref1[0], text = _ref1[1];
|
||||
}
|
||||
for (key in attributes) {
|
||||
if (!__hasProp.call(attributes, key)) continue;
|
||||
val = attributes[key];
|
||||
val = '' + val || '';
|
||||
attributes[key] = this.escape(val);
|
||||
}
|
||||
child = new XMLFragment(this.parent, name, attributes);
|
||||
if (text != null) {
|
||||
text = '' + text || '';
|
||||
text = this.escape(text);
|
||||
this.assertLegalChar(text);
|
||||
child.raw(text);
|
||||
}
|
||||
i = this.parent.children.indexOf(this);
|
||||
this.parent.children.splice(i + 1, 0, child);
|
||||
return child;
|
||||
};
|
||||
|
||||
XMLFragment.prototype.remove = function() {
|
||||
var i, _ref;
|
||||
if (this.isRoot) {
|
||||
throw new Error("Cannot remove the root element");
|
||||
}
|
||||
i = this.parent.children.indexOf(this);
|
||||
[].splice.apply(this.parent.children, [i, i - i + 1].concat(_ref = [])), _ref;
|
||||
return this.parent;
|
||||
};
|
||||
|
||||
XMLFragment.prototype.text = function(value) {
|
||||
var child;
|
||||
if (!(value != null)) {
|
||||
throw new Error("Missing element text");
|
||||
}
|
||||
value = '' + value || '';
|
||||
value = this.escape(value);
|
||||
this.assertLegalChar(value);
|
||||
child = new XMLFragment(this, '', {}, value);
|
||||
this.children.push(child);
|
||||
return this;
|
||||
};
|
||||
|
||||
XMLFragment.prototype.cdata = function(value) {
|
||||
var child;
|
||||
if (!(value != null)) {
|
||||
throw new Error("Missing CDATA text");
|
||||
}
|
||||
value = '' + value || '';
|
||||
this.assertLegalChar(value);
|
||||
if (value.match(/]]>/)) {
|
||||
throw new Error("Invalid CDATA text: " + value);
|
||||
}
|
||||
child = new XMLFragment(this, '', {}, '<![CDATA[' + value + ']]>');
|
||||
this.children.push(child);
|
||||
return this;
|
||||
};
|
||||
|
||||
XMLFragment.prototype.comment = function(value) {
|
||||
var child;
|
||||
if (!(value != null)) {
|
||||
throw new Error("Missing comment text");
|
||||
}
|
||||
value = '' + value || '';
|
||||
value = this.escape(value);
|
||||
this.assertLegalChar(value);
|
||||
if (value.match(/--/)) {
|
||||
throw new Error("Comment text cannot contain double-hypen: " + value);
|
||||
}
|
||||
child = new XMLFragment(this, '', {}, '<!-- ' + value + ' -->');
|
||||
this.children.push(child);
|
||||
return this;
|
||||
};
|
||||
|
||||
XMLFragment.prototype.raw = function(value) {
|
||||
var child;
|
||||
if (!(value != null)) {
|
||||
throw new Error("Missing raw text");
|
||||
}
|
||||
value = '' + value || '';
|
||||
child = new XMLFragment(this, '', {}, value);
|
||||
this.children.push(child);
|
||||
return this;
|
||||
};
|
||||
|
||||
XMLFragment.prototype.up = function() {
|
||||
if (this.isRoot) {
|
||||
throw new Error("This node has no parent. Use doc() if you need to get the document object.");
|
||||
}
|
||||
return this.parent;
|
||||
};
|
||||
|
||||
XMLFragment.prototype.root = function() {
|
||||
var child;
|
||||
if (this.isRoot) {
|
||||
return this;
|
||||
}
|
||||
child = this.parent;
|
||||
while (!child.isRoot) {
|
||||
child = child.parent;
|
||||
}
|
||||
return child;
|
||||
};
|
||||
|
||||
XMLFragment.prototype.document = function() {
|
||||
return this.root().documentObject;
|
||||
};
|
||||
|
||||
XMLFragment.prototype.end = function(options) {
|
||||
return this.document().toString(options);
|
||||
};
|
||||
|
||||
XMLFragment.prototype.prev = function() {
|
||||
var i;
|
||||
if (this.isRoot) {
|
||||
throw new Error("Root node has no siblings");
|
||||
}
|
||||
i = this.parent.children.indexOf(this);
|
||||
if (i < 1) {
|
||||
throw new Error("Already at the first node");
|
||||
}
|
||||
return this.parent.children[i - 1];
|
||||
};
|
||||
|
||||
XMLFragment.prototype.next = function() {
|
||||
var i;
|
||||
if (this.isRoot) {
|
||||
throw new Error("Root node has no siblings");
|
||||
}
|
||||
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];
|
||||
};
|
||||
|
||||
XMLFragment.prototype.clone = function(deep) {
|
||||
var clonedSelf;
|
||||
clonedSelf = new XMLFragment(this.parent, this.name, this.attributes, this.value);
|
||||
if (deep) {
|
||||
this.children.forEach(function(child) {
|
||||
var clonedChild;
|
||||
clonedChild = child.clone(deep);
|
||||
clonedChild.parent = clonedSelf;
|
||||
return clonedSelf.children.push(clonedChild);
|
||||
});
|
||||
}
|
||||
return clonedSelf;
|
||||
};
|
||||
|
||||
XMLFragment.prototype.importXMLBuilder = function(xmlbuilder) {
|
||||
var clonedRoot;
|
||||
clonedRoot = xmlbuilder.root().clone(true);
|
||||
clonedRoot.parent = this;
|
||||
this.children.push(clonedRoot);
|
||||
clonedRoot.isRoot = false;
|
||||
return this;
|
||||
};
|
||||
|
||||
XMLFragment.prototype.attribute = function(name, value) {
|
||||
var _ref;
|
||||
if (!(name != null)) {
|
||||
throw new Error("Missing attribute name");
|
||||
}
|
||||
if (!(value != null)) {
|
||||
throw new Error("Missing attribute value");
|
||||
}
|
||||
name = '' + name || '';
|
||||
value = '' + value || '';
|
||||
if ((_ref = this.attributes) == null) {
|
||||
this.attributes = {};
|
||||
}
|
||||
this.attributes[name] = this.escape(value);
|
||||
return this;
|
||||
};
|
||||
|
||||
XMLFragment.prototype.removeAttribute = function(name) {
|
||||
if (!(name != null)) {
|
||||
throw new Error("Missing attribute name");
|
||||
}
|
||||
name = '' + name || '';
|
||||
delete this.attributes[name];
|
||||
return this;
|
||||
};
|
||||
|
||||
XMLFragment.prototype.toString = function(options, level) {
|
||||
var attName, attValue, child, indent, newline, pretty, r, space, _i, _len, _ref, _ref1;
|
||||
pretty = (options != null) && options.pretty || false;
|
||||
indent = (options != null) && options.indent || ' ';
|
||||
newline = (options != null) && options.newline || '\n';
|
||||
level || (level = 0);
|
||||
space = new Array(level + 1).join(indent);
|
||||
r = '';
|
||||
if (pretty) {
|
||||
r += space;
|
||||
}
|
||||
if (!(this.value != null)) {
|
||||
r += '<' + this.name;
|
||||
} else {
|
||||
r += '' + this.value;
|
||||
}
|
||||
_ref = this.attributes;
|
||||
for (attName in _ref) {
|
||||
if (!__hasProp.call(_ref, attName)) continue;
|
||||
attValue = _ref[attName];
|
||||
if (this.name === '!DOCTYPE') {
|
||||
r += ' ' + attValue;
|
||||
} else {
|
||||
r += ' ' + attName + '="' + attValue + '"';
|
||||
}
|
||||
}
|
||||
if (this.children.length === 0) {
|
||||
if (!(this.value != null)) {
|
||||
r += this.name === '?xml' ? '?>' : this.name === '!DOCTYPE' ? '>' : '/>';
|
||||
}
|
||||
if (pretty) {
|
||||
r += newline;
|
||||
}
|
||||
} else if (pretty && this.children.length === 1 && (this.children[0].value != null)) {
|
||||
r += '>';
|
||||
r += this.children[0].value;
|
||||
r += '</' + this.name + '>';
|
||||
r += newline;
|
||||
} else {
|
||||
r += '>';
|
||||
if (pretty) {
|
||||
r += newline;
|
||||
}
|
||||
_ref1 = this.children;
|
||||
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
|
||||
child = _ref1[_i];
|
||||
r += child.toString(options, level + 1);
|
||||
}
|
||||
if (pretty) {
|
||||
r += space;
|
||||
}
|
||||
r += '</' + this.name + '>';
|
||||
if (pretty) {
|
||||
r += newline;
|
||||
}
|
||||
}
|
||||
return r;
|
||||
};
|
||||
|
||||
XMLFragment.prototype.escape = function(str) {
|
||||
return str.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/'/g, ''').replace(/"/g, '"');
|
||||
};
|
||||
|
||||
XMLFragment.prototype.is = function(obj, type) {
|
||||
var clas;
|
||||
clas = Object.prototype.toString.call(obj).slice(8, -1);
|
||||
return (obj != null) && clas === type;
|
||||
};
|
||||
|
||||
XMLFragment.prototype.ele = function(name, attributes, text) {
|
||||
return this.element(name, attributes, text);
|
||||
};
|
||||
|
||||
XMLFragment.prototype.txt = function(value) {
|
||||
return this.text(value);
|
||||
};
|
||||
|
||||
XMLFragment.prototype.dat = function(value) {
|
||||
return this.cdata(value);
|
||||
};
|
||||
|
||||
XMLFragment.prototype.att = function(name, value) {
|
||||
return this.attribute(name, value);
|
||||
};
|
||||
|
||||
XMLFragment.prototype.com = function(value) {
|
||||
return this.comment(value);
|
||||
};
|
||||
|
||||
XMLFragment.prototype.doc = function() {
|
||||
return this.document();
|
||||
};
|
||||
|
||||
XMLFragment.prototype.e = function(name, attributes, text) {
|
||||
return this.element(name, attributes, text);
|
||||
};
|
||||
|
||||
XMLFragment.prototype.t = function(value) {
|
||||
return this.text(value);
|
||||
};
|
||||
|
||||
XMLFragment.prototype.d = function(value) {
|
||||
return this.cdata(value);
|
||||
};
|
||||
|
||||
XMLFragment.prototype.a = function(name, value) {
|
||||
return this.attribute(name, value);
|
||||
};
|
||||
|
||||
XMLFragment.prototype.c = function(value) {
|
||||
return this.comment(value);
|
||||
};
|
||||
|
||||
XMLFragment.prototype.r = function(value) {
|
||||
return this.raw(value);
|
||||
};
|
||||
|
||||
XMLFragment.prototype.u = function() {
|
||||
return this.up();
|
||||
};
|
||||
|
||||
return XMLFragment;
|
||||
|
||||
})();
|
||||
|
||||
module.exports = XMLFragment;
|
||||
|
||||
}).call(this);
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
// Generated by CoffeeScript 1.3.3
|
||||
(function() {
|
||||
var XMLBuilder;
|
||||
|
||||
XMLBuilder = require('./XMLBuilder');
|
||||
|
||||
module.exports.create = function(name, xmldec, doctype, options) {
|
||||
if (name != null) {
|
||||
return new XMLBuilder(name, xmldec, doctype, options).root();
|
||||
} else {
|
||||
return new XMLBuilder();
|
||||
}
|
||||
};
|
||||
|
||||
}).call(this);
|
||||
Reference in new issue
Block a user