3 var XMLAttribute, XMLElement, XMLNode, XMLProcessingInstruction, create, every, isFunction, isObject,
4 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; },
5 hasProp = {}.hasOwnProperty;
7 create = require(
'lodash/create');
9 isObject = require(
'lodash/isObject');
11 isFunction = require(
'lodash/isFunction');
13 every = require(
'lodash/every');
15 XMLNode = require(
'./XMLNode');
17 XMLAttribute = require(
'./XMLAttribute');
19 XMLProcessingInstruction = require(
'./XMLProcessingInstruction');
21 module.exports = XMLElement = (
function(superClass) {
22 extend(XMLElement, superClass);
24 function XMLElement(parent, name, attributes) {
25 XMLElement.__super__.constructor.call(
this, parent);
27 throw new Error(
"Missing element name");
29 this.name = this.stringify.eleName(name);
31 this.instructions = [];
33 if (attributes != null) {
34 this.attribute(attributes);
38 XMLElement.prototype.clone =
function() {
39 var att, attName, clonedSelf, i, len, pi, ref, ref1;
40 clonedSelf = create(XMLElement.prototype,
this);
41 if (clonedSelf.isRoot) {
42 clonedSelf.documentObject = null;
44 clonedSelf.attributes = {};
45 ref = this.attributes;
46 for (attName in ref) {
47 if (!hasProp.call(ref, attName))
continue;
49 clonedSelf.attributes[attName] = att.clone();
51 clonedSelf.instructions = [];
52 ref1 = this.instructions;
53 for (i = 0, len = ref1.length; i < len; i++) {
55 clonedSelf.instructions.push(pi.clone());
57 clonedSelf.children = [];
58 this.children.forEach(
function(child) {
60 clonedChild = child.clone();
61 clonedChild.parent = clonedSelf;
62 return clonedSelf.children.push(clonedChild);
67 XMLElement.prototype.attribute =
function(name, value) {
68 var attName, attValue;
70 name = name.valueOf();
73 for (attName in name) {
74 if (!hasProp.call(name, attName))
continue;
75 attValue = name[attName];
76 this.attribute(attName, attValue);
79 if (isFunction(value)) {
80 value = value.apply();
82 if (!this.options.skipNullAttributes || (value != null)) {
83 this.attributes[name] =
new XMLAttribute(
this, name, value);
89 XMLElement.prototype.removeAttribute =
function(name) {
92 throw new Error(
"Missing attribute name");
94 name = name.valueOf();
95 if (Array.isArray(name)) {
96 for (i = 0, len = name.length; i < len; i++) {
98 delete this.attributes[attName];
101 delete this.attributes[name];
106 XMLElement.prototype.instruction =
function(target, value) {
107 var i, insTarget, insValue, instruction, len;
108 if (target != null) {
109 target = target.valueOf();
112 value = value.valueOf();
114 if (Array.isArray(target)) {
115 for (i = 0, len = target.length; i < len; i++) {
116 insTarget = target[i];
117 this.instruction(insTarget);
119 }
else if (isObject(target)) {
120 for (insTarget in target) {
121 if (!hasProp.call(target, insTarget))
continue;
122 insValue = target[insTarget];
123 this.instruction(insTarget, insValue);
126 if (isFunction(value)) {
127 value = value.apply();
129 instruction =
new XMLProcessingInstruction(
this, target, value);
130 this.instructions.push(instruction);
135 XMLElement.prototype.toString =
function(options, level) {
136 var att, child, i, indent, instruction, j, len, len1, name, newline, offset, pretty, r, ref, ref1, ref2, ref3, ref4, ref5, space;
137 pretty = (options != null ? options.pretty :
void 0) ||
false;
138 indent = (ref = options != null ? options.indent :
void 0) != null ? ref :
' ';
139 offset = (ref1 = options != null ? options.offset :
void 0) != null ? ref1 : 0;
140 newline = (ref2 = options != null ? options.newline :
void 0) != null ? ref2 :
'\n';
141 level || (level = 0);
142 space =
new Array(level + offset + 1).join(indent);
144 ref3 = this.instructions;
145 for (i = 0, len = ref3.length; i < len; i++) {
146 instruction = ref3[i];
147 r += instruction.toString(options, level);
152 r +=
'<' + this.name;
153 ref4 = this.attributes;
155 if (!hasProp.call(ref4, name))
continue;
157 r += att.toString(options);
159 if (this.children.length === 0 || every(this.children,
function(e) {
160 return e.value ===
'';
166 }
else if (pretty && this.children.length === 1 && (
this.children[0].value != null)) {
168 r += this.children[0].value;
169 r +=
'</' + this.name +
'>';
176 ref5 = this.children;
177 for (j = 0, len1 = ref5.length; j < len1; j++) {
179 r += child.toString(options, level + 1);
184 r +=
'</' + this.name +
'>';
192 XMLElement.prototype.att =
function(name, value) {
193 return this.attribute(name, value);
196 XMLElement.prototype.ins =
function(target, value) {
197 return this.instruction(target, value);
200 XMLElement.prototype.a =
function(name, value) {
201 return this.attribute(name, value);
204 XMLElement.prototype.i =
function(target, value) {
205 return this.instruction(target, value);