00001
00002 (function() {
00003 var XMLAttribute, XMLElement, XMLNode, isFunction, isObject, ref,
00004 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; },
00005 hasProp = {}.hasOwnProperty;
00006
00007 ref = require('./Utility'), isObject = ref.isObject, isFunction = ref.isFunction;
00008
00009 XMLNode = require('./XMLNode');
00010
00011 XMLAttribute = require('./XMLAttribute');
00012
00013 module.exports = XMLElement = (function(superClass) {
00014 extend(XMLElement, superClass);
00015
00016 function XMLElement(parent, name, attributes) {
00017 XMLElement.__super__.constructor.call(this, parent);
00018 if (name == null) {
00019 throw new Error("Missing element name");
00020 }
00021 this.name = this.stringify.eleName(name);
00022 this.attributes = {};
00023 if (attributes != null) {
00024 this.attribute(attributes);
00025 }
00026 if (parent.isDocument) {
00027 this.isRoot = true;
00028 this.documentObject = parent;
00029 parent.rootObject = this;
00030 }
00031 }
00032
00033 XMLElement.prototype.clone = function() {
00034 var att, attName, clonedSelf, ref1;
00035 clonedSelf = Object.create(this);
00036 if (clonedSelf.isRoot) {
00037 clonedSelf.documentObject = null;
00038 }
00039 clonedSelf.attributes = {};
00040 ref1 = this.attributes;
00041 for (attName in ref1) {
00042 if (!hasProp.call(ref1, attName)) continue;
00043 att = ref1[attName];
00044 clonedSelf.attributes[attName] = att.clone();
00045 }
00046 clonedSelf.children = [];
00047 this.children.forEach(function(child) {
00048 var clonedChild;
00049 clonedChild = child.clone();
00050 clonedChild.parent = clonedSelf;
00051 return clonedSelf.children.push(clonedChild);
00052 });
00053 return clonedSelf;
00054 };
00055
00056 XMLElement.prototype.attribute = function(name, value) {
00057 var attName, attValue;
00058 if (name != null) {
00059 name = name.valueOf();
00060 }
00061 if (isObject(name)) {
00062 for (attName in name) {
00063 if (!hasProp.call(name, attName)) continue;
00064 attValue = name[attName];
00065 this.attribute(attName, attValue);
00066 }
00067 } else {
00068 if (isFunction(value)) {
00069 value = value.apply();
00070 }
00071 if (!this.options.skipNullAttributes || (value != null)) {
00072 this.attributes[name] = new XMLAttribute(this, name, value);
00073 }
00074 }
00075 return this;
00076 };
00077
00078 XMLElement.prototype.removeAttribute = function(name) {
00079 var attName, i, len;
00080 if (name == null) {
00081 throw new Error("Missing attribute name");
00082 }
00083 name = name.valueOf();
00084 if (Array.isArray(name)) {
00085 for (i = 0, len = name.length; i < len; i++) {
00086 attName = name[i];
00087 delete this.attributes[attName];
00088 }
00089 } else {
00090 delete this.attributes[name];
00091 }
00092 return this;
00093 };
00094
00095 XMLElement.prototype.toString = function(options) {
00096 return this.options.writer.set(options).element(this);
00097 };
00098
00099 XMLElement.prototype.att = function(name, value) {
00100 return this.attribute(name, value);
00101 };
00102
00103 XMLElement.prototype.a = function(name, value) {
00104 return this.attribute(name, value);
00105 };
00106
00107 return XMLElement;
00108
00109 })(XMLNode);
00110
00111 }).call(this);