xmlbuilder
Advanced tools
Comparing version 0.4.3 to 1.0.0
@@ -1,2 +0,2 @@ | ||
// Generated by CoffeeScript 1.3.3 | ||
// Generated by CoffeeScript 1.6.1 | ||
(function() { | ||
@@ -8,9 +8,5 @@ var XMLBuilder; | ||
module.exports.create = function(name, xmldec, doctype, options) { | ||
if (name != null) { | ||
return new XMLBuilder(name, xmldec, doctype, options).root(); | ||
} else { | ||
return new XMLBuilder(); | ||
} | ||
return new XMLBuilder(name, xmldec, doctype, options).root(); | ||
}; | ||
}).call(this); |
@@ -1,84 +0,48 @@ | ||
// Generated by CoffeeScript 1.3.3 | ||
// Generated by CoffeeScript 1.6.1 | ||
(function() { | ||
var XMLBuilder, XMLFragment, | ||
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; | ||
var XMLBuilder, XMLFragment, XMLStringifier, _; | ||
_ = require('underscore'); | ||
XMLFragment = require('./XMLFragment'); | ||
XMLStringifier = require('./XMLStringifier'); | ||
XMLBuilder = (function() { | ||
function XMLBuilder(name, xmldec, doctype, options) { | ||
this.assertLegalChar = __bind(this.assertLegalChar, this); | ||
var att, child, _ref; | ||
var child, decatts, docatts, root; | ||
if (name == null) { | ||
throw new Error("Root element needs a name"); | ||
} | ||
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' | ||
}; | ||
options = _.extend({ | ||
'version': '1.0' | ||
}, xmldec, doctype, options); | ||
this.stringify = new XMLStringifier(options); | ||
name = this.stringify.eleName(name); | ||
if (!(options != null ? options.headless : void 0)) { | ||
decatts = {}; | ||
if (options.version != null) { | ||
decatts.version = this.stringify.xmlVersion(options.version); | ||
} | ||
} | ||
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); | ||
if (options.encoding != null) { | ||
decatts.encoding = this.stringify.xmlEncoding(options.encoding); | ||
} | ||
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 (options.standalone != null) { | ||
decatts.standalone = this.stringify.xmlStandalone(options.standalone); | ||
} | ||
if (xmldec.standalone != null) { | ||
att.standalone = xmldec.standalone ? "yes" : "no"; | ||
} | ||
child = new XMLFragment(this, '?xml', att); | ||
child = new XMLFragment(this, '?xml', decatts); | ||
this.children.push(child); | ||
} | ||
if (doctype != null) { | ||
att = {}; | ||
if (name != null) { | ||
att.name = name; | ||
docatts = {}; | ||
if (options.ext != null) { | ||
docatts.ext = this.stringify.xmlExternalSubset(options.ext); | ||
} | ||
if (doctype.ext != null) { | ||
doctype.ext = '' + doctype.ext || ''; | ||
att.ext = doctype.ext; | ||
if (!_.isEmpty(docatts)) { | ||
docatts.name = name; | ||
child = new XMLFragment(this, '!DOCTYPE', docatts); | ||
this.children.push(child); | ||
} | ||
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, {}); | ||
@@ -89,4 +53,3 @@ root.isRoot = true; | ||
this.rootObject = root; | ||
return root; | ||
}; | ||
} | ||
@@ -112,21 +75,2 @@ XMLBuilder.prototype.root = function() { | ||
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; | ||
@@ -133,0 +77,0 @@ |
@@ -1,6 +0,8 @@ | ||
// Generated by CoffeeScript 1.3.3 | ||
// Generated by CoffeeScript 1.6.1 | ||
(function() { | ||
var XMLFragment, | ||
var XMLFragment, _, | ||
__hasProp = {}.hasOwnProperty; | ||
_ = require('underscore'); | ||
XMLFragment = (function() { | ||
@@ -16,31 +18,33 @@ | ||
this.children = []; | ||
this.assertLegalChar = parent.assertLegalChar; | ||
this.instructions = []; | ||
this.stringify = parent.stringify; | ||
} | ||
XMLFragment.prototype.element = function(name, attributes, text) { | ||
var child, key, val, _ref, _ref1; | ||
if (!(name != null)) { | ||
var atts, child, key, val, _ref, _ref1; | ||
if (name == null) { | ||
throw new Error("Missing element name"); | ||
} | ||
name = '' + name || ''; | ||
this.assertLegalChar(name); | ||
name = this.stringify.eleName(name); | ||
if (attributes == null) { | ||
attributes = {}; | ||
} | ||
if (this.is(attributes, 'String') && this.is(text, 'Object')) { | ||
if (_.isString(attributes) && _.isObject(text)) { | ||
_ref = [text, attributes], attributes = _ref[0], text = _ref[1]; | ||
} else if (this.is(attributes, 'String')) { | ||
} else if (_.isString(attributes)) { | ||
_ref1 = [{}, attributes], attributes = _ref1[0], text = _ref1[1]; | ||
} | ||
atts = {}; | ||
for (key in attributes) { | ||
if (!__hasProp.call(attributes, key)) continue; | ||
val = attributes[key]; | ||
val = '' + val || ''; | ||
attributes[key] = this.escape(val); | ||
key = this.stringify.attName(key); | ||
val = this.stringify.attValue(val); | ||
if ((key != null) && (val != null)) { | ||
atts[key] = val; | ||
} | ||
} | ||
child = new XMLFragment(this, name, attributes); | ||
child = new XMLFragment(this, name, atts); | ||
if (text != null) { | ||
text = '' + text || ''; | ||
text = this.escape(text); | ||
this.assertLegalChar(text); | ||
text = this.stringify.eleText(text); | ||
child.raw(text); | ||
@@ -53,30 +57,31 @@ } | ||
XMLFragment.prototype.insertBefore = function(name, attributes, text) { | ||
var child, i, key, val, _ref, _ref1; | ||
var atts, child, i, key, val, _ref, _ref1; | ||
if (this.isRoot) { | ||
throw new Error("Cannot insert elements at root level"); | ||
} | ||
if (!(name != null)) { | ||
if (name == null) { | ||
throw new Error("Missing element name"); | ||
} | ||
name = '' + name || ''; | ||
this.assertLegalChar(name); | ||
name = this.stringify.eleName(name); | ||
if (attributes == null) { | ||
attributes = {}; | ||
} | ||
if (this.is(attributes, 'String') && this.is(text, 'Object')) { | ||
if (_.isString(attributes) && _.isObject(text)) { | ||
_ref = [text, attributes], attributes = _ref[0], text = _ref[1]; | ||
} else if (this.is(attributes, 'String')) { | ||
} else if (_.isString(attributes)) { | ||
_ref1 = [{}, attributes], attributes = _ref1[0], text = _ref1[1]; | ||
} | ||
atts = {}; | ||
for (key in attributes) { | ||
if (!__hasProp.call(attributes, key)) continue; | ||
val = attributes[key]; | ||
val = '' + val || ''; | ||
attributes[key] = this.escape(val); | ||
key = this.stringify.attName(key); | ||
val = this.stringify.attValue(val); | ||
if ((key != null) && (val != null)) { | ||
atts[key] = val; | ||
} | ||
} | ||
child = new XMLFragment(this.parent, name, attributes); | ||
child = new XMLFragment(this.parent, name, atts); | ||
if (text != null) { | ||
text = '' + text || ''; | ||
text = this.escape(text); | ||
this.assertLegalChar(text); | ||
text = this.stringify.eleText(text); | ||
child.raw(text); | ||
@@ -90,30 +95,31 @@ } | ||
XMLFragment.prototype.insertAfter = function(name, attributes, text) { | ||
var child, i, key, val, _ref, _ref1; | ||
var atts, child, i, key, val, _ref, _ref1; | ||
if (this.isRoot) { | ||
throw new Error("Cannot insert elements at root level"); | ||
} | ||
if (!(name != null)) { | ||
if (name == null) { | ||
throw new Error("Missing element name"); | ||
} | ||
name = '' + name || ''; | ||
this.assertLegalChar(name); | ||
name = this.stringify.eleName(name); | ||
if (attributes == null) { | ||
attributes = {}; | ||
} | ||
if (this.is(attributes, 'String') && this.is(text, 'Object')) { | ||
if (_.isString(attributes) && _.isObject(text)) { | ||
_ref = [text, attributes], attributes = _ref[0], text = _ref[1]; | ||
} else if (this.is(attributes, 'String')) { | ||
} else if (_.isString(attributes)) { | ||
_ref1 = [{}, attributes], attributes = _ref1[0], text = _ref1[1]; | ||
} | ||
atts = {}; | ||
for (key in attributes) { | ||
if (!__hasProp.call(attributes, key)) continue; | ||
val = attributes[key]; | ||
val = '' + val || ''; | ||
attributes[key] = this.escape(val); | ||
key = this.stringify.attName(key); | ||
val = this.stringify.attValue(val); | ||
if ((key != null) && (val != null)) { | ||
atts[key] = val; | ||
} | ||
} | ||
child = new XMLFragment(this.parent, name, attributes); | ||
child = new XMLFragment(this.parent, name, atts); | ||
if (text != null) { | ||
text = '' + text || ''; | ||
text = this.escape(text); | ||
this.assertLegalChar(text); | ||
text = this.stringify.eleText(text); | ||
child.raw(text); | ||
@@ -138,8 +144,6 @@ } | ||
var child; | ||
if (!(value != null)) { | ||
if (value == null) { | ||
throw new Error("Missing element text"); | ||
} | ||
value = '' + value || ''; | ||
value = this.escape(value); | ||
this.assertLegalChar(value); | ||
value = this.stringify.eleText(value); | ||
child = new XMLFragment(this, '', {}, value); | ||
@@ -152,11 +156,7 @@ this.children.push(child); | ||
var child; | ||
if (!(value != null)) { | ||
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 + ']]>'); | ||
value = this.stringify.cdata(value); | ||
child = new XMLFragment(this, '', {}, value); | ||
this.children.push(child); | ||
@@ -168,12 +168,7 @@ return this; | ||
var child; | ||
if (!(value != null)) { | ||
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 + ' -->'); | ||
value = this.stringify.comment(value); | ||
child = new XMLFragment(this, '', {}, value); | ||
this.children.push(child); | ||
@@ -185,6 +180,6 @@ return this; | ||
var child; | ||
if (!(value != null)) { | ||
if (value == null) { | ||
throw new Error("Missing raw text"); | ||
} | ||
value = '' + value || ''; | ||
value = this.stringify.raw(value); | ||
child = new XMLFragment(this, '', {}, value); | ||
@@ -271,14 +266,14 @@ this.children.push(child); | ||
var _ref; | ||
if (!(name != null)) { | ||
if (name == null) { | ||
throw new Error("Missing attribute name"); | ||
} | ||
if (!(value != null)) { | ||
if (value == null) { | ||
throw new Error("Missing attribute value"); | ||
} | ||
name = '' + name || ''; | ||
value = '' + value || ''; | ||
name = this.stringify.attName(name); | ||
value = this.stringify.attValue(value); | ||
if ((_ref = this.attributes) == null) { | ||
this.attributes = {}; | ||
} | ||
this.attributes[name] = this.escape(value); | ||
this.attributes[name] = value; | ||
return this; | ||
@@ -288,6 +283,6 @@ }; | ||
XMLFragment.prototype.removeAttribute = function(name) { | ||
if (!(name != null)) { | ||
if (name == null) { | ||
throw new Error("Missing attribute name"); | ||
} | ||
name = '' + name || ''; | ||
name = this.stringify.attName(name); | ||
delete this.attributes[name]; | ||
@@ -297,4 +292,23 @@ return this; | ||
XMLFragment.prototype.instruction = function(target, value) { | ||
var pi; | ||
if (target == null) { | ||
throw new Error("Missing instruction target"); | ||
} | ||
if (value == null) { | ||
value = ''; | ||
} | ||
target = this.stringify.insTarget(target); | ||
value = this.stringify.insValue(value); | ||
pi = target; | ||
if (value) { | ||
pi += ' '; | ||
} | ||
pi += value; | ||
this.instructions.push(pi); | ||
return this; | ||
}; | ||
XMLFragment.prototype.toString = function(options, level) { | ||
var attName, attValue, child, indent, newline, pretty, r, space, _i, _len, _ref, _ref1; | ||
var attName, attValue, child, indent, instruction, newline, pretty, r, space, _i, _j, _len, _len1, _ref, _ref1, _ref2; | ||
pretty = (options != null) && options.pretty || false; | ||
@@ -306,6 +320,17 @@ indent = (options != null) && options.indent || ' '; | ||
r = ''; | ||
_ref = this.instructions; | ||
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
instruction = _ref[_i]; | ||
if (pretty) { | ||
r += space; | ||
} | ||
r += '<?' + instruction + '?>'; | ||
if (pretty) { | ||
r += newline; | ||
} | ||
} | ||
if (pretty) { | ||
r += space; | ||
} | ||
if (!(this.value != null)) { | ||
if (this.value == null) { | ||
r += '<' + this.name; | ||
@@ -315,6 +340,6 @@ } else { | ||
} | ||
_ref = this.attributes; | ||
for (attName in _ref) { | ||
if (!__hasProp.call(_ref, attName)) continue; | ||
attValue = _ref[attName]; | ||
_ref1 = this.attributes; | ||
for (attName in _ref1) { | ||
if (!__hasProp.call(_ref1, attName)) continue; | ||
attValue = _ref1[attName]; | ||
if (this.name === '!DOCTYPE') { | ||
@@ -327,3 +352,3 @@ r += ' ' + attValue; | ||
if (this.children.length === 0) { | ||
if (!(this.value != null)) { | ||
if (this.value == null) { | ||
r += this.name === '?xml' ? '?>' : this.name === '!DOCTYPE' ? '>' : '/>'; | ||
@@ -344,5 +369,5 @@ } | ||
} | ||
_ref1 = this.children; | ||
for (_i = 0, _len = _ref1.length; _i < _len; _i++) { | ||
child = _ref1[_i]; | ||
_ref2 = this.children; | ||
for (_j = 0, _len1 = _ref2.length; _j < _len1; _j++) { | ||
child = _ref2[_j]; | ||
r += child.toString(options, level + 1); | ||
@@ -361,12 +386,2 @@ } | ||
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) { | ||
@@ -392,2 +407,6 @@ return this.element(name, attributes, text); | ||
XMLFragment.prototype.ins = function(target, value) { | ||
return this.instruction(target, value); | ||
}; | ||
XMLFragment.prototype.doc = function() { | ||
@@ -417,2 +436,6 @@ return this.document(); | ||
XMLFragment.prototype.i = function(target, value) { | ||
return this.instruction(target, value); | ||
}; | ||
XMLFragment.prototype.r = function(value) { | ||
@@ -419,0 +442,0 @@ return this.raw(value); |
{ | ||
"name": "xmlbuilder", | ||
"version": "0.4.3", | ||
"version": "1.0.0", | ||
"keywords": [ "xml", "xmlbuilder" ], | ||
@@ -25,11 +25,15 @@ "homepage": "http://github.com/oozcitak/xmlbuilder-js", | ||
}, | ||
"dependencies": { | ||
"underscore": ">=1.5.x" | ||
}, | ||
"devDependencies": { | ||
"coffee-script": "1.1.x" | ||
"coffee-script": ">=1.1.x", | ||
"vows": ">=0.7.x" | ||
}, | ||
"scripts": { | ||
"prepublish": "make release", | ||
"postpublish": "make clean", | ||
"test": "make test" | ||
"prepublish": "coffee -co lib/ src/*.coffee", | ||
"postpublish": "rm -rf lib", | ||
"test": "vows test/*" | ||
} | ||
} | ||
@@ -14,7 +14,2 @@ # xmlbuilder-js | ||
### Important: | ||
I had to break compatibility while adding multiple instances in 0.1.3. | ||
As a result, version from v0.1.3 are **not** compatible with previous versions. | ||
### Usage: | ||
@@ -21,0 +16,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
21100
580
0
1
2
69
+ Addedunderscore@>=1.5.x
+ Addedunderscore@1.13.7(transitive)