diff options
Diffstat (limited to 'node_modules/xmlbuilder/lib/XMLElement.js')
-rw-r--r-- | node_modules/xmlbuilder/lib/XMLElement.js | 133 |
1 files changed, 117 insertions, 16 deletions
diff --git a/node_modules/xmlbuilder/lib/XMLElement.js b/node_modules/xmlbuilder/lib/XMLElement.js index 620714b72..27504a872 100644 --- a/node_modules/xmlbuilder/lib/XMLElement.js +++ b/node_modules/xmlbuilder/lib/XMLElement.js @@ -1,15 +1,23 @@ -// Generated by CoffeeScript 1.10.0 +// Generated by CoffeeScript 1.9.1 (function() { - var XMLAttribute, XMLElement, XMLNode, isFunction, isObject, ref, + var XMLAttribute, XMLElement, XMLNode, XMLProcessingInstruction, create, every, isFunction, isObject, extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, hasProp = {}.hasOwnProperty; - ref = require('./Utility'), isObject = ref.isObject, isFunction = ref.isFunction; + create = require('lodash/create'); + + isObject = require('lodash/isObject'); + + isFunction = require('lodash/isFunction'); + + every = require('lodash/every'); XMLNode = require('./XMLNode'); XMLAttribute = require('./XMLAttribute'); + XMLProcessingInstruction = require('./XMLProcessingInstruction'); + module.exports = XMLElement = (function(superClass) { extend(XMLElement, superClass); @@ -19,30 +27,33 @@ throw new Error("Missing element name"); } this.name = this.stringify.eleName(name); + this.children = []; + this.instructions = []; this.attributes = {}; if (attributes != null) { this.attribute(attributes); } - if (parent.isDocument) { - this.isRoot = true; - this.documentObject = parent; - parent.rootObject = this; - } } XMLElement.prototype.clone = function() { - var att, attName, clonedSelf, ref1; - clonedSelf = Object.create(this); + var att, attName, clonedSelf, i, len, pi, ref, ref1; + clonedSelf = create(XMLElement.prototype, this); if (clonedSelf.isRoot) { clonedSelf.documentObject = null; } clonedSelf.attributes = {}; - ref1 = this.attributes; - for (attName in ref1) { - if (!hasProp.call(ref1, attName)) continue; - att = ref1[attName]; + ref = this.attributes; + for (attName in ref) { + if (!hasProp.call(ref, attName)) continue; + att = ref[attName]; clonedSelf.attributes[attName] = att.clone(); } + clonedSelf.instructions = []; + ref1 = this.instructions; + for (i = 0, len = ref1.length; i < len; i++) { + pi = ref1[i]; + clonedSelf.instructions.push(pi.clone()); + } clonedSelf.children = []; this.children.forEach(function(child) { var clonedChild; @@ -92,18 +103,108 @@ return this; }; - XMLElement.prototype.toString = function(options) { - return this.options.writer.set(options).element(this); + XMLElement.prototype.instruction = function(target, value) { + var i, insTarget, insValue, instruction, len; + if (target != null) { + target = target.valueOf(); + } + if (value != null) { + value = value.valueOf(); + } + if (Array.isArray(target)) { + for (i = 0, len = target.length; i < len; i++) { + insTarget = target[i]; + this.instruction(insTarget); + } + } else if (isObject(target)) { + for (insTarget in target) { + if (!hasProp.call(target, insTarget)) continue; + insValue = target[insTarget]; + this.instruction(insTarget, insValue); + } + } else { + if (isFunction(value)) { + value = value.apply(); + } + instruction = new XMLProcessingInstruction(this, target, value); + this.instructions.push(instruction); + } + return this; + }; + + XMLElement.prototype.toString = function(options, level) { + var att, child, i, indent, instruction, j, len, len1, name, newline, offset, pretty, r, ref, ref1, ref2, ref3, ref4, ref5, space; + pretty = (options != null ? options.pretty : void 0) || false; + indent = (ref = options != null ? options.indent : void 0) != null ? ref : ' '; + offset = (ref1 = options != null ? options.offset : void 0) != null ? ref1 : 0; + newline = (ref2 = options != null ? options.newline : void 0) != null ? ref2 : '\n'; + level || (level = 0); + space = new Array(level + offset + 1).join(indent); + r = ''; + ref3 = this.instructions; + for (i = 0, len = ref3.length; i < len; i++) { + instruction = ref3[i]; + r += instruction.toString(options, level); + } + if (pretty) { + r += space; + } + r += '<' + this.name; + ref4 = this.attributes; + for (name in ref4) { + if (!hasProp.call(ref4, name)) continue; + att = ref4[name]; + r += att.toString(options); + } + if (this.children.length === 0 || every(this.children, function(e) { + return e.value === ''; + })) { + r += '/>'; + 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; + } + ref5 = this.children; + for (j = 0, len1 = ref5.length; j < len1; j++) { + child = ref5[j]; + r += child.toString(options, level + 1); + } + if (pretty) { + r += space; + } + r += '</' + this.name + '>'; + if (pretty) { + r += newline; + } + } + return r; }; XMLElement.prototype.att = function(name, value) { return this.attribute(name, value); }; + XMLElement.prototype.ins = function(target, value) { + return this.instruction(target, value); + }; + XMLElement.prototype.a = function(name, value) { return this.attribute(name, value); }; + XMLElement.prototype.i = function(target, value) { + return this.instruction(target, value); + }; + return XMLElement; })(XMLNode); |