XMLElement.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. // Generated by CoffeeScript 1.12.7
  2. (function() {
  3. var NodeType, XMLAttribute, XMLElement, XMLNamedNodeMap, XMLNode, getValue, 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, getValue = ref.getValue;
  7. XMLNode = require('./XMLNode');
  8. NodeType = require('./NodeType');
  9. XMLAttribute = require('./XMLAttribute');
  10. XMLNamedNodeMap = require('./XMLNamedNodeMap');
  11. module.exports = XMLElement = (function(superClass) {
  12. extend(XMLElement, superClass);
  13. function XMLElement(parent, name, attributes) {
  14. var child, j, len, ref1;
  15. XMLElement.__super__.constructor.call(this, parent);
  16. if (name == null) {
  17. throw new Error("Missing element name. " + this.debugInfo());
  18. }
  19. this.name = this.stringify.name(name);
  20. this.type = NodeType.Element;
  21. this.attribs = {};
  22. this.schemaTypeInfo = null;
  23. if (attributes != null) {
  24. this.attribute(attributes);
  25. }
  26. if (parent.type === NodeType.Document) {
  27. this.isRoot = true;
  28. this.documentObject = parent;
  29. parent.rootObject = this;
  30. if (parent.children) {
  31. ref1 = parent.children;
  32. for (j = 0, len = ref1.length; j < len; j++) {
  33. child = ref1[j];
  34. if (child.type === NodeType.DocType) {
  35. child.name = this.name;
  36. break;
  37. }
  38. }
  39. }
  40. }
  41. }
  42. Object.defineProperty(XMLElement.prototype, 'tagName', {
  43. get: function() {
  44. return this.name;
  45. }
  46. });
  47. Object.defineProperty(XMLElement.prototype, 'namespaceURI', {
  48. get: function() {
  49. return '';
  50. }
  51. });
  52. Object.defineProperty(XMLElement.prototype, 'prefix', {
  53. get: function() {
  54. return '';
  55. }
  56. });
  57. Object.defineProperty(XMLElement.prototype, 'localName', {
  58. get: function() {
  59. return this.name;
  60. }
  61. });
  62. Object.defineProperty(XMLElement.prototype, 'id', {
  63. get: function() {
  64. throw new Error("This DOM method is not implemented." + this.debugInfo());
  65. }
  66. });
  67. Object.defineProperty(XMLElement.prototype, 'className', {
  68. get: function() {
  69. throw new Error("This DOM method is not implemented." + this.debugInfo());
  70. }
  71. });
  72. Object.defineProperty(XMLElement.prototype, 'classList', {
  73. get: function() {
  74. throw new Error("This DOM method is not implemented." + this.debugInfo());
  75. }
  76. });
  77. Object.defineProperty(XMLElement.prototype, 'attributes', {
  78. get: function() {
  79. if (!this.attributeMap || !this.attributeMap.nodes) {
  80. this.attributeMap = new XMLNamedNodeMap(this.attribs);
  81. }
  82. return this.attributeMap;
  83. }
  84. });
  85. XMLElement.prototype.clone = function() {
  86. var att, attName, clonedSelf, ref1;
  87. clonedSelf = Object.create(this);
  88. if (clonedSelf.isRoot) {
  89. clonedSelf.documentObject = null;
  90. }
  91. clonedSelf.attribs = {};
  92. ref1 = this.attribs;
  93. for (attName in ref1) {
  94. if (!hasProp.call(ref1, attName)) continue;
  95. att = ref1[attName];
  96. clonedSelf.attribs[attName] = att.clone();
  97. }
  98. clonedSelf.children = [];
  99. this.children.forEach(function(child) {
  100. var clonedChild;
  101. clonedChild = child.clone();
  102. clonedChild.parent = clonedSelf;
  103. return clonedSelf.children.push(clonedChild);
  104. });
  105. return clonedSelf;
  106. };
  107. XMLElement.prototype.attribute = function(name, value) {
  108. var attName, attValue;
  109. if (name != null) {
  110. name = getValue(name);
  111. }
  112. if (isObject(name)) {
  113. for (attName in name) {
  114. if (!hasProp.call(name, attName)) continue;
  115. attValue = name[attName];
  116. this.attribute(attName, attValue);
  117. }
  118. } else {
  119. if (isFunction(value)) {
  120. value = value.apply();
  121. }
  122. if (this.options.keepNullAttributes && (value == null)) {
  123. this.attribs[name] = new XMLAttribute(this, name, "");
  124. } else if (value != null) {
  125. this.attribs[name] = new XMLAttribute(this, name, value);
  126. }
  127. }
  128. return this;
  129. };
  130. XMLElement.prototype.removeAttribute = function(name) {
  131. var attName, j, len;
  132. if (name == null) {
  133. throw new Error("Missing attribute name. " + this.debugInfo());
  134. }
  135. name = getValue(name);
  136. if (Array.isArray(name)) {
  137. for (j = 0, len = name.length; j < len; j++) {
  138. attName = name[j];
  139. delete this.attribs[attName];
  140. }
  141. } else {
  142. delete this.attribs[name];
  143. }
  144. return this;
  145. };
  146. XMLElement.prototype.toString = function(options) {
  147. return this.options.writer.element(this, this.options.writer.filterOptions(options));
  148. };
  149. XMLElement.prototype.att = function(name, value) {
  150. return this.attribute(name, value);
  151. };
  152. XMLElement.prototype.a = function(name, value) {
  153. return this.attribute(name, value);
  154. };
  155. XMLElement.prototype.getAttribute = function(name) {
  156. if (this.attribs.hasOwnProperty(name)) {
  157. return this.attribs[name].value;
  158. } else {
  159. return null;
  160. }
  161. };
  162. XMLElement.prototype.setAttribute = function(name, value) {
  163. throw new Error("This DOM method is not implemented." + this.debugInfo());
  164. };
  165. XMLElement.prototype.getAttributeNode = function(name) {
  166. if (this.attribs.hasOwnProperty(name)) {
  167. return this.attribs[name];
  168. } else {
  169. return null;
  170. }
  171. };
  172. XMLElement.prototype.setAttributeNode = function(newAttr) {
  173. throw new Error("This DOM method is not implemented." + this.debugInfo());
  174. };
  175. XMLElement.prototype.removeAttributeNode = function(oldAttr) {
  176. throw new Error("This DOM method is not implemented." + this.debugInfo());
  177. };
  178. XMLElement.prototype.getElementsByTagName = function(name) {
  179. throw new Error("This DOM method is not implemented." + this.debugInfo());
  180. };
  181. XMLElement.prototype.getAttributeNS = function(namespaceURI, localName) {
  182. throw new Error("This DOM method is not implemented." + this.debugInfo());
  183. };
  184. XMLElement.prototype.setAttributeNS = function(namespaceURI, qualifiedName, value) {
  185. throw new Error("This DOM method is not implemented." + this.debugInfo());
  186. };
  187. XMLElement.prototype.removeAttributeNS = function(namespaceURI, localName) {
  188. throw new Error("This DOM method is not implemented." + this.debugInfo());
  189. };
  190. XMLElement.prototype.getAttributeNodeNS = function(namespaceURI, localName) {
  191. throw new Error("This DOM method is not implemented." + this.debugInfo());
  192. };
  193. XMLElement.prototype.setAttributeNodeNS = function(newAttr) {
  194. throw new Error("This DOM method is not implemented." + this.debugInfo());
  195. };
  196. XMLElement.prototype.getElementsByTagNameNS = function(namespaceURI, localName) {
  197. throw new Error("This DOM method is not implemented." + this.debugInfo());
  198. };
  199. XMLElement.prototype.hasAttribute = function(name) {
  200. return this.attribs.hasOwnProperty(name);
  201. };
  202. XMLElement.prototype.hasAttributeNS = function(namespaceURI, localName) {
  203. throw new Error("This DOM method is not implemented." + this.debugInfo());
  204. };
  205. XMLElement.prototype.setIdAttribute = function(name, isId) {
  206. if (this.attribs.hasOwnProperty(name)) {
  207. return this.attribs[name].isId;
  208. } else {
  209. return isId;
  210. }
  211. };
  212. XMLElement.prototype.setIdAttributeNS = function(namespaceURI, localName, isId) {
  213. throw new Error("This DOM method is not implemented." + this.debugInfo());
  214. };
  215. XMLElement.prototype.setIdAttributeNode = function(idAttr, isId) {
  216. throw new Error("This DOM method is not implemented." + this.debugInfo());
  217. };
  218. XMLElement.prototype.getElementsByTagName = function(tagname) {
  219. throw new Error("This DOM method is not implemented." + this.debugInfo());
  220. };
  221. XMLElement.prototype.getElementsByTagNameNS = function(namespaceURI, localName) {
  222. throw new Error("This DOM method is not implemented." + this.debugInfo());
  223. };
  224. XMLElement.prototype.getElementsByClassName = function(classNames) {
  225. throw new Error("This DOM method is not implemented." + this.debugInfo());
  226. };
  227. XMLElement.prototype.isEqualNode = function(node) {
  228. var i, j, ref1;
  229. if (!XMLElement.__super__.isEqualNode.apply(this, arguments).isEqualNode(node)) {
  230. return false;
  231. }
  232. if (node.namespaceURI !== this.namespaceURI) {
  233. return false;
  234. }
  235. if (node.prefix !== this.prefix) {
  236. return false;
  237. }
  238. if (node.localName !== this.localName) {
  239. return false;
  240. }
  241. if (node.attribs.length !== this.attribs.length) {
  242. return false;
  243. }
  244. for (i = j = 0, ref1 = this.attribs.length - 1; 0 <= ref1 ? j <= ref1 : j >= ref1; i = 0 <= ref1 ? ++j : --j) {
  245. if (!this.attribs[i].isEqualNode(node.attribs[i])) {
  246. return false;
  247. }
  248. }
  249. return true;
  250. };
  251. return XMLElement;
  252. })(XMLNode);
  253. }).call(this);