XMLElement.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Generated by CoffeeScript 1.12.7
  2. (function() {
  3. var XMLAttribute, XMLElement, XMLNode, isFunction, isObject, ref,
  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;
  6. ref = require('./Utility'), isObject = ref.isObject, isFunction = ref.isFunction;
  7. XMLNode = require('./XMLNode');
  8. XMLAttribute = require('./XMLAttribute');
  9. module.exports = XMLElement = (function(superClass) {
  10. extend(XMLElement, superClass);
  11. function XMLElement(parent, name, attributes) {
  12. XMLElement.__super__.constructor.call(this, parent);
  13. if (name == null) {
  14. throw new Error("Missing element name");
  15. }
  16. this.name = this.stringify.eleName(name);
  17. this.attributes = {};
  18. if (attributes != null) {
  19. this.attribute(attributes);
  20. }
  21. if (parent.isDocument) {
  22. this.isRoot = true;
  23. this.documentObject = parent;
  24. parent.rootObject = this;
  25. }
  26. }
  27. XMLElement.prototype.clone = function() {
  28. var att, attName, clonedSelf, ref1;
  29. clonedSelf = Object.create(this);
  30. if (clonedSelf.isRoot) {
  31. clonedSelf.documentObject = null;
  32. }
  33. clonedSelf.attributes = {};
  34. ref1 = this.attributes;
  35. for (attName in ref1) {
  36. if (!hasProp.call(ref1, attName)) continue;
  37. att = ref1[attName];
  38. clonedSelf.attributes[attName] = att.clone();
  39. }
  40. clonedSelf.children = [];
  41. this.children.forEach(function(child) {
  42. var clonedChild;
  43. clonedChild = child.clone();
  44. clonedChild.parent = clonedSelf;
  45. return clonedSelf.children.push(clonedChild);
  46. });
  47. return clonedSelf;
  48. };
  49. XMLElement.prototype.attribute = function(name, value) {
  50. var attName, attValue;
  51. if (name != null) {
  52. name = name.valueOf();
  53. }
  54. if (isObject(name)) {
  55. for (attName in name) {
  56. if (!hasProp.call(name, attName)) continue;
  57. attValue = name[attName];
  58. this.attribute(attName, attValue);
  59. }
  60. } else {
  61. if (isFunction(value)) {
  62. value = value.apply();
  63. }
  64. if (!this.options.skipNullAttributes || (value != null)) {
  65. this.attributes[name] = new XMLAttribute(this, name, value);
  66. }
  67. }
  68. return this;
  69. };
  70. XMLElement.prototype.removeAttribute = function(name) {
  71. var attName, i, len;
  72. if (name == null) {
  73. throw new Error("Missing attribute name");
  74. }
  75. name = name.valueOf();
  76. if (Array.isArray(name)) {
  77. for (i = 0, len = name.length; i < len; i++) {
  78. attName = name[i];
  79. delete this.attributes[attName];
  80. }
  81. } else {
  82. delete this.attributes[name];
  83. }
  84. return this;
  85. };
  86. XMLElement.prototype.toString = function(options) {
  87. return this.options.writer.set(options).element(this);
  88. };
  89. XMLElement.prototype.att = function(name, value) {
  90. return this.attribute(name, value);
  91. };
  92. XMLElement.prototype.a = function(name, value) {
  93. return this.attribute(name, value);
  94. };
  95. return XMLElement;
  96. })(XMLNode);
  97. }).call(this);