XMLElement.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. // Generated by CoffeeScript 2.4.1
  2. (function() {
  3. var NodeType, XMLAttribute, XMLElement, XMLNamedNodeMap, XMLNode, getValue, isFunction, isObject,
  4. hasProp = {}.hasOwnProperty;
  5. ({isObject, isFunction, getValue} = require('./Utility'));
  6. XMLNode = require('./XMLNode');
  7. NodeType = require('./NodeType');
  8. XMLAttribute = require('./XMLAttribute');
  9. XMLNamedNodeMap = require('./XMLNamedNodeMap');
  10. // Represents an element of the XML document
  11. module.exports = XMLElement = (function() {
  12. class XMLElement extends XMLNode {
  13. // Initializes a new instance of `XMLElement`
  14. // `parent` the parent node
  15. // `name` element name
  16. // `attributes` an object containing name/value pairs of attributes
  17. constructor(parent, name, attributes) {
  18. var child, j, len, ref;
  19. super(parent);
  20. if (name == null) {
  21. throw new Error("Missing element name. " + this.debugInfo());
  22. }
  23. this.name = this.stringify.name(name);
  24. this.type = NodeType.Element;
  25. this.attribs = {};
  26. this.schemaTypeInfo = null;
  27. if (attributes != null) {
  28. this.attribute(attributes);
  29. }
  30. // set properties if this is the root node
  31. if (parent.type === NodeType.Document) {
  32. this.isRoot = true;
  33. this.documentObject = parent;
  34. parent.rootObject = this;
  35. // set dtd name
  36. if (parent.children) {
  37. ref = parent.children;
  38. for (j = 0, len = ref.length; j < len; j++) {
  39. child = ref[j];
  40. if (child.type === NodeType.DocType) {
  41. child.name = this.name;
  42. break;
  43. }
  44. }
  45. }
  46. }
  47. }
  48. // Creates and returns a deep clone of `this`
  49. clone() {
  50. var att, attName, clonedSelf, ref;
  51. clonedSelf = Object.create(this);
  52. // remove document element
  53. if (clonedSelf.isRoot) {
  54. clonedSelf.documentObject = null;
  55. }
  56. // clone attributes
  57. clonedSelf.attribs = {};
  58. ref = this.attribs;
  59. for (attName in ref) {
  60. if (!hasProp.call(ref, attName)) continue;
  61. att = ref[attName];
  62. clonedSelf.attribs[attName] = att.clone();
  63. }
  64. // clone child nodes
  65. clonedSelf.children = [];
  66. this.children.forEach(function(child) {
  67. var clonedChild;
  68. clonedChild = child.clone();
  69. clonedChild.parent = clonedSelf;
  70. return clonedSelf.children.push(clonedChild);
  71. });
  72. return clonedSelf;
  73. }
  74. // Adds or modifies an attribute
  75. // `name` attribute name
  76. // `value` attribute value
  77. attribute(name, value) {
  78. var attName, attValue;
  79. if (name != null) {
  80. name = getValue(name);
  81. }
  82. if (isObject(name)) { // expand if object
  83. for (attName in name) {
  84. if (!hasProp.call(name, attName)) continue;
  85. attValue = name[attName];
  86. this.attribute(attName, attValue);
  87. }
  88. } else {
  89. if (isFunction(value)) {
  90. value = value.apply();
  91. }
  92. if (this.options.keepNullAttributes && (value == null)) {
  93. this.attribs[name] = new XMLAttribute(this, name, "");
  94. } else if (value != null) {
  95. this.attribs[name] = new XMLAttribute(this, name, value);
  96. }
  97. }
  98. return this;
  99. }
  100. // Removes an attribute
  101. // `name` attribute name
  102. removeAttribute(name) {
  103. var attName, j, len;
  104. // Also defined in DOM level 1
  105. // removeAttribute(name) removes an attribute by name.
  106. if (name == null) {
  107. throw new Error("Missing attribute name. " + this.debugInfo());
  108. }
  109. name = getValue(name);
  110. if (Array.isArray(name)) { // expand if array
  111. for (j = 0, len = name.length; j < len; j++) {
  112. attName = name[j];
  113. delete this.attribs[attName];
  114. }
  115. } else {
  116. delete this.attribs[name];
  117. }
  118. return this;
  119. }
  120. // Converts the XML fragment to string
  121. // `options.pretty` pretty prints the result
  122. // `options.indent` indentation for pretty print
  123. // `options.offset` how many indentations to add to every line for pretty print
  124. // `options.newline` newline sequence for pretty print
  125. // `options.allowEmpty` do not self close empty element tags
  126. toString(options) {
  127. return this.options.writer.element(this, this.options.writer.filterOptions(options));
  128. }
  129. // Aliases
  130. att(name, value) {
  131. return this.attribute(name, value);
  132. }
  133. a(name, value) {
  134. return this.attribute(name, value);
  135. }
  136. // DOM Level 1
  137. getAttribute(name) {
  138. if (this.attribs.hasOwnProperty(name)) {
  139. return this.attribs[name].value;
  140. } else {
  141. return null;
  142. }
  143. }
  144. setAttribute(name, value) {
  145. throw new Error("This DOM method is not implemented." + this.debugInfo());
  146. }
  147. getAttributeNode(name) {
  148. if (this.attribs.hasOwnProperty(name)) {
  149. return this.attribs[name];
  150. } else {
  151. return null;
  152. }
  153. }
  154. setAttributeNode(newAttr) {
  155. throw new Error("This DOM method is not implemented." + this.debugInfo());
  156. }
  157. removeAttributeNode(oldAttr) {
  158. throw new Error("This DOM method is not implemented." + this.debugInfo());
  159. }
  160. getElementsByTagName(name) {
  161. throw new Error("This DOM method is not implemented." + this.debugInfo());
  162. }
  163. // DOM Level 2
  164. getAttributeNS(namespaceURI, localName) {
  165. throw new Error("This DOM method is not implemented." + this.debugInfo());
  166. }
  167. setAttributeNS(namespaceURI, qualifiedName, value) {
  168. throw new Error("This DOM method is not implemented." + this.debugInfo());
  169. }
  170. removeAttributeNS(namespaceURI, localName) {
  171. throw new Error("This DOM method is not implemented." + this.debugInfo());
  172. }
  173. getAttributeNodeNS(namespaceURI, localName) {
  174. throw new Error("This DOM method is not implemented." + this.debugInfo());
  175. }
  176. setAttributeNodeNS(newAttr) {
  177. throw new Error("This DOM method is not implemented." + this.debugInfo());
  178. }
  179. getElementsByTagNameNS(namespaceURI, localName) {
  180. throw new Error("This DOM method is not implemented." + this.debugInfo());
  181. }
  182. hasAttribute(name) {
  183. return this.attribs.hasOwnProperty(name);
  184. }
  185. hasAttributeNS(namespaceURI, localName) {
  186. throw new Error("This DOM method is not implemented." + this.debugInfo());
  187. }
  188. // DOM Level 3
  189. setIdAttribute(name, isId) {
  190. if (this.attribs.hasOwnProperty(name)) {
  191. return this.attribs[name].isId;
  192. } else {
  193. return isId;
  194. }
  195. }
  196. setIdAttributeNS(namespaceURI, localName, isId) {
  197. throw new Error("This DOM method is not implemented." + this.debugInfo());
  198. }
  199. setIdAttributeNode(idAttr, isId) {
  200. throw new Error("This DOM method is not implemented." + this.debugInfo());
  201. }
  202. // DOM Level 4
  203. getElementsByTagName(tagname) {
  204. throw new Error("This DOM method is not implemented." + this.debugInfo());
  205. }
  206. getElementsByTagNameNS(namespaceURI, localName) {
  207. throw new Error("This DOM method is not implemented." + this.debugInfo());
  208. }
  209. getElementsByClassName(classNames) {
  210. throw new Error("This DOM method is not implemented." + this.debugInfo());
  211. }
  212. isEqualNode(node) {
  213. var i, j, ref;
  214. if (!super.isEqualNode(node)) {
  215. return false;
  216. }
  217. if (node.namespaceURI !== this.namespaceURI) {
  218. return false;
  219. }
  220. if (node.prefix !== this.prefix) {
  221. return false;
  222. }
  223. if (node.localName !== this.localName) {
  224. return false;
  225. }
  226. if (node.attribs.length !== this.attribs.length) {
  227. return false;
  228. }
  229. for (i = j = 0, ref = this.attribs.length - 1; (0 <= ref ? j <= ref : j >= ref); i = 0 <= ref ? ++j : --j) {
  230. if (!this.attribs[i].isEqualNode(node.attribs[i])) {
  231. return false;
  232. }
  233. }
  234. return true;
  235. }
  236. };
  237. // DOM level 1
  238. Object.defineProperty(XMLElement.prototype, 'tagName', {
  239. get: function() {
  240. return this.name;
  241. }
  242. });
  243. // DOM level 4
  244. Object.defineProperty(XMLElement.prototype, 'namespaceURI', {
  245. get: function() {
  246. return '';
  247. }
  248. });
  249. Object.defineProperty(XMLElement.prototype, 'prefix', {
  250. get: function() {
  251. return '';
  252. }
  253. });
  254. Object.defineProperty(XMLElement.prototype, 'localName', {
  255. get: function() {
  256. return this.name;
  257. }
  258. });
  259. Object.defineProperty(XMLElement.prototype, 'id', {
  260. get: function() {
  261. throw new Error("This DOM method is not implemented." + this.debugInfo());
  262. }
  263. });
  264. Object.defineProperty(XMLElement.prototype, 'className', {
  265. get: function() {
  266. throw new Error("This DOM method is not implemented." + this.debugInfo());
  267. }
  268. });
  269. Object.defineProperty(XMLElement.prototype, 'classList', {
  270. get: function() {
  271. throw new Error("This DOM method is not implemented." + this.debugInfo());
  272. }
  273. });
  274. Object.defineProperty(XMLElement.prototype, 'attributes', {
  275. get: function() {
  276. if (!this.attributeMap || !this.attributeMap.nodes) {
  277. this.attributeMap = new XMLNamedNodeMap(this.attribs);
  278. }
  279. return this.attributeMap;
  280. }
  281. });
  282. return XMLElement;
  283. }).call(this);
  284. }).call(this);