XMLNode.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. // Generated by CoffeeScript 1.12.7
  2. (function() {
  3. var XMLCData, XMLComment, XMLDeclaration, XMLDocType, XMLDummy, XMLElement, XMLNode, XMLProcessingInstruction, XMLRaw, XMLText, getValue, isEmpty, isFunction, isObject, ref,
  4. hasProp = {}.hasOwnProperty;
  5. ref = require('./Utility'), isObject = ref.isObject, isFunction = ref.isFunction, isEmpty = ref.isEmpty, getValue = ref.getValue;
  6. XMLElement = null;
  7. XMLCData = null;
  8. XMLComment = null;
  9. XMLDeclaration = null;
  10. XMLDocType = null;
  11. XMLRaw = null;
  12. XMLText = null;
  13. XMLProcessingInstruction = null;
  14. XMLDummy = null;
  15. module.exports = XMLNode = (function() {
  16. function XMLNode(parent) {
  17. this.parent = parent;
  18. if (this.parent) {
  19. this.options = this.parent.options;
  20. this.stringify = this.parent.stringify;
  21. }
  22. this.children = [];
  23. if (!XMLElement) {
  24. XMLElement = require('./XMLElement');
  25. XMLCData = require('./XMLCData');
  26. XMLComment = require('./XMLComment');
  27. XMLDeclaration = require('./XMLDeclaration');
  28. XMLDocType = require('./XMLDocType');
  29. XMLRaw = require('./XMLRaw');
  30. XMLText = require('./XMLText');
  31. XMLProcessingInstruction = require('./XMLProcessingInstruction');
  32. XMLDummy = require('./XMLDummy');
  33. }
  34. }
  35. XMLNode.prototype.element = function(name, attributes, text) {
  36. var childNode, item, j, k, key, lastChild, len, len1, ref1, ref2, val;
  37. lastChild = null;
  38. if (attributes === null && (text == null)) {
  39. ref1 = [{}, null], attributes = ref1[0], text = ref1[1];
  40. }
  41. if (attributes == null) {
  42. attributes = {};
  43. }
  44. attributes = getValue(attributes);
  45. if (!isObject(attributes)) {
  46. ref2 = [attributes, text], text = ref2[0], attributes = ref2[1];
  47. }
  48. if (name != null) {
  49. name = getValue(name);
  50. }
  51. if (Array.isArray(name)) {
  52. for (j = 0, len = name.length; j < len; j++) {
  53. item = name[j];
  54. lastChild = this.element(item);
  55. }
  56. } else if (isFunction(name)) {
  57. lastChild = this.element(name.apply());
  58. } else if (isObject(name)) {
  59. for (key in name) {
  60. if (!hasProp.call(name, key)) continue;
  61. val = name[key];
  62. if (isFunction(val)) {
  63. val = val.apply();
  64. }
  65. if ((isObject(val)) && (isEmpty(val))) {
  66. val = null;
  67. }
  68. if (!this.options.ignoreDecorators && this.stringify.convertAttKey && key.indexOf(this.stringify.convertAttKey) === 0) {
  69. lastChild = this.attribute(key.substr(this.stringify.convertAttKey.length), val);
  70. } else if (!this.options.separateArrayItems && Array.isArray(val)) {
  71. for (k = 0, len1 = val.length; k < len1; k++) {
  72. item = val[k];
  73. childNode = {};
  74. childNode[key] = item;
  75. lastChild = this.element(childNode);
  76. }
  77. } else if (isObject(val)) {
  78. lastChild = this.element(key);
  79. lastChild.element(val);
  80. } else {
  81. lastChild = this.element(key, val);
  82. }
  83. }
  84. } else if (this.options.skipNullNodes && text === null) {
  85. lastChild = this.dummy();
  86. } else {
  87. if (!this.options.ignoreDecorators && this.stringify.convertTextKey && name.indexOf(this.stringify.convertTextKey) === 0) {
  88. lastChild = this.text(text);
  89. } else if (!this.options.ignoreDecorators && this.stringify.convertCDataKey && name.indexOf(this.stringify.convertCDataKey) === 0) {
  90. lastChild = this.cdata(text);
  91. } else if (!this.options.ignoreDecorators && this.stringify.convertCommentKey && name.indexOf(this.stringify.convertCommentKey) === 0) {
  92. lastChild = this.comment(text);
  93. } else if (!this.options.ignoreDecorators && this.stringify.convertRawKey && name.indexOf(this.stringify.convertRawKey) === 0) {
  94. lastChild = this.raw(text);
  95. } else if (!this.options.ignoreDecorators && this.stringify.convertPIKey && name.indexOf(this.stringify.convertPIKey) === 0) {
  96. lastChild = this.instruction(name.substr(this.stringify.convertPIKey.length), text);
  97. } else {
  98. lastChild = this.node(name, attributes, text);
  99. }
  100. }
  101. if (lastChild == null) {
  102. throw new Error("Could not create any elements with: " + name + ". " + this.debugInfo());
  103. }
  104. return lastChild;
  105. };
  106. XMLNode.prototype.insertBefore = function(name, attributes, text) {
  107. var child, i, removed;
  108. if (this.isRoot) {
  109. throw new Error("Cannot insert elements at root level. " + this.debugInfo(name));
  110. }
  111. i = this.parent.children.indexOf(this);
  112. removed = this.parent.children.splice(i);
  113. child = this.parent.element(name, attributes, text);
  114. Array.prototype.push.apply(this.parent.children, removed);
  115. return child;
  116. };
  117. XMLNode.prototype.insertAfter = function(name, attributes, text) {
  118. var child, i, removed;
  119. if (this.isRoot) {
  120. throw new Error("Cannot insert elements at root level. " + this.debugInfo(name));
  121. }
  122. i = this.parent.children.indexOf(this);
  123. removed = this.parent.children.splice(i + 1);
  124. child = this.parent.element(name, attributes, text);
  125. Array.prototype.push.apply(this.parent.children, removed);
  126. return child;
  127. };
  128. XMLNode.prototype.remove = function() {
  129. var i, ref1;
  130. if (this.isRoot) {
  131. throw new Error("Cannot remove the root element. " + this.debugInfo());
  132. }
  133. i = this.parent.children.indexOf(this);
  134. [].splice.apply(this.parent.children, [i, i - i + 1].concat(ref1 = [])), ref1;
  135. return this.parent;
  136. };
  137. XMLNode.prototype.node = function(name, attributes, text) {
  138. var child, ref1;
  139. if (name != null) {
  140. name = getValue(name);
  141. }
  142. attributes || (attributes = {});
  143. attributes = getValue(attributes);
  144. if (!isObject(attributes)) {
  145. ref1 = [attributes, text], text = ref1[0], attributes = ref1[1];
  146. }
  147. child = new XMLElement(this, name, attributes);
  148. if (text != null) {
  149. child.text(text);
  150. }
  151. this.children.push(child);
  152. return child;
  153. };
  154. XMLNode.prototype.text = function(value) {
  155. var child;
  156. child = new XMLText(this, value);
  157. this.children.push(child);
  158. return this;
  159. };
  160. XMLNode.prototype.cdata = function(value) {
  161. var child;
  162. child = new XMLCData(this, value);
  163. this.children.push(child);
  164. return this;
  165. };
  166. XMLNode.prototype.comment = function(value) {
  167. var child;
  168. child = new XMLComment(this, value);
  169. this.children.push(child);
  170. return this;
  171. };
  172. XMLNode.prototype.commentBefore = function(value) {
  173. var child, i, removed;
  174. i = this.parent.children.indexOf(this);
  175. removed = this.parent.children.splice(i);
  176. child = this.parent.comment(value);
  177. Array.prototype.push.apply(this.parent.children, removed);
  178. return this;
  179. };
  180. XMLNode.prototype.commentAfter = function(value) {
  181. var child, i, removed;
  182. i = this.parent.children.indexOf(this);
  183. removed = this.parent.children.splice(i + 1);
  184. child = this.parent.comment(value);
  185. Array.prototype.push.apply(this.parent.children, removed);
  186. return this;
  187. };
  188. XMLNode.prototype.raw = function(value) {
  189. var child;
  190. child = new XMLRaw(this, value);
  191. this.children.push(child);
  192. return this;
  193. };
  194. XMLNode.prototype.dummy = function() {
  195. var child;
  196. child = new XMLDummy(this);
  197. this.children.push(child);
  198. return child;
  199. };
  200. XMLNode.prototype.instruction = function(target, value) {
  201. var insTarget, insValue, instruction, j, len;
  202. if (target != null) {
  203. target = getValue(target);
  204. }
  205. if (value != null) {
  206. value = getValue(value);
  207. }
  208. if (Array.isArray(target)) {
  209. for (j = 0, len = target.length; j < len; j++) {
  210. insTarget = target[j];
  211. this.instruction(insTarget);
  212. }
  213. } else if (isObject(target)) {
  214. for (insTarget in target) {
  215. if (!hasProp.call(target, insTarget)) continue;
  216. insValue = target[insTarget];
  217. this.instruction(insTarget, insValue);
  218. }
  219. } else {
  220. if (isFunction(value)) {
  221. value = value.apply();
  222. }
  223. instruction = new XMLProcessingInstruction(this, target, value);
  224. this.children.push(instruction);
  225. }
  226. return this;
  227. };
  228. XMLNode.prototype.instructionBefore = function(target, value) {
  229. var child, i, removed;
  230. i = this.parent.children.indexOf(this);
  231. removed = this.parent.children.splice(i);
  232. child = this.parent.instruction(target, value);
  233. Array.prototype.push.apply(this.parent.children, removed);
  234. return this;
  235. };
  236. XMLNode.prototype.instructionAfter = function(target, value) {
  237. var child, i, removed;
  238. i = this.parent.children.indexOf(this);
  239. removed = this.parent.children.splice(i + 1);
  240. child = this.parent.instruction(target, value);
  241. Array.prototype.push.apply(this.parent.children, removed);
  242. return this;
  243. };
  244. XMLNode.prototype.declaration = function(version, encoding, standalone) {
  245. var doc, xmldec;
  246. doc = this.document();
  247. xmldec = new XMLDeclaration(doc, version, encoding, standalone);
  248. if (doc.children[0] instanceof XMLDeclaration) {
  249. doc.children[0] = xmldec;
  250. } else {
  251. doc.children.unshift(xmldec);
  252. }
  253. return doc.root() || doc;
  254. };
  255. XMLNode.prototype.doctype = function(pubID, sysID) {
  256. var child, doc, doctype, i, j, k, len, len1, ref1, ref2;
  257. doc = this.document();
  258. doctype = new XMLDocType(doc, pubID, sysID);
  259. ref1 = doc.children;
  260. for (i = j = 0, len = ref1.length; j < len; i = ++j) {
  261. child = ref1[i];
  262. if (child instanceof XMLDocType) {
  263. doc.children[i] = doctype;
  264. return doctype;
  265. }
  266. }
  267. ref2 = doc.children;
  268. for (i = k = 0, len1 = ref2.length; k < len1; i = ++k) {
  269. child = ref2[i];
  270. if (child.isRoot) {
  271. doc.children.splice(i, 0, doctype);
  272. return doctype;
  273. }
  274. }
  275. doc.children.push(doctype);
  276. return doctype;
  277. };
  278. XMLNode.prototype.up = function() {
  279. if (this.isRoot) {
  280. throw new Error("The root node has no parent. Use doc() if you need to get the document object.");
  281. }
  282. return this.parent;
  283. };
  284. XMLNode.prototype.root = function() {
  285. var node;
  286. node = this;
  287. while (node) {
  288. if (node.isDocument) {
  289. return node.rootObject;
  290. } else if (node.isRoot) {
  291. return node;
  292. } else {
  293. node = node.parent;
  294. }
  295. }
  296. };
  297. XMLNode.prototype.document = function() {
  298. var node;
  299. node = this;
  300. while (node) {
  301. if (node.isDocument) {
  302. return node;
  303. } else {
  304. node = node.parent;
  305. }
  306. }
  307. };
  308. XMLNode.prototype.end = function(options) {
  309. return this.document().end(options);
  310. };
  311. XMLNode.prototype.prev = function() {
  312. var i;
  313. i = this.parent.children.indexOf(this);
  314. while (i > 0 && this.parent.children[i - 1].isDummy) {
  315. i = i - 1;
  316. }
  317. if (i < 1) {
  318. throw new Error("Already at the first node. " + this.debugInfo());
  319. }
  320. return this.parent.children[i - 1];
  321. };
  322. XMLNode.prototype.next = function() {
  323. var i;
  324. i = this.parent.children.indexOf(this);
  325. while (i < this.parent.children.length - 1 && this.parent.children[i + 1].isDummy) {
  326. i = i + 1;
  327. }
  328. if (i === -1 || i === this.parent.children.length - 1) {
  329. throw new Error("Already at the last node. " + this.debugInfo());
  330. }
  331. return this.parent.children[i + 1];
  332. };
  333. XMLNode.prototype.importDocument = function(doc) {
  334. var clonedRoot;
  335. clonedRoot = doc.root().clone();
  336. clonedRoot.parent = this;
  337. clonedRoot.isRoot = false;
  338. this.children.push(clonedRoot);
  339. return this;
  340. };
  341. XMLNode.prototype.debugInfo = function(name) {
  342. var ref1, ref2;
  343. name = name || this.name;
  344. if ((name == null) && !((ref1 = this.parent) != null ? ref1.name : void 0)) {
  345. return "";
  346. } else if (name == null) {
  347. return "parent: <" + this.parent.name + ">";
  348. } else if (!((ref2 = this.parent) != null ? ref2.name : void 0)) {
  349. return "node: <" + name + ">";
  350. } else {
  351. return "node: <" + name + ">, parent: <" + this.parent.name + ">";
  352. }
  353. };
  354. XMLNode.prototype.ele = function(name, attributes, text) {
  355. return this.element(name, attributes, text);
  356. };
  357. XMLNode.prototype.nod = function(name, attributes, text) {
  358. return this.node(name, attributes, text);
  359. };
  360. XMLNode.prototype.txt = function(value) {
  361. return this.text(value);
  362. };
  363. XMLNode.prototype.dat = function(value) {
  364. return this.cdata(value);
  365. };
  366. XMLNode.prototype.com = function(value) {
  367. return this.comment(value);
  368. };
  369. XMLNode.prototype.ins = function(target, value) {
  370. return this.instruction(target, value);
  371. };
  372. XMLNode.prototype.doc = function() {
  373. return this.document();
  374. };
  375. XMLNode.prototype.dec = function(version, encoding, standalone) {
  376. return this.declaration(version, encoding, standalone);
  377. };
  378. XMLNode.prototype.dtd = function(pubID, sysID) {
  379. return this.doctype(pubID, sysID);
  380. };
  381. XMLNode.prototype.e = function(name, attributes, text) {
  382. return this.element(name, attributes, text);
  383. };
  384. XMLNode.prototype.n = function(name, attributes, text) {
  385. return this.node(name, attributes, text);
  386. };
  387. XMLNode.prototype.t = function(value) {
  388. return this.text(value);
  389. };
  390. XMLNode.prototype.d = function(value) {
  391. return this.cdata(value);
  392. };
  393. XMLNode.prototype.c = function(value) {
  394. return this.comment(value);
  395. };
  396. XMLNode.prototype.r = function(value) {
  397. return this.raw(value);
  398. };
  399. XMLNode.prototype.i = function(target, value) {
  400. return this.instruction(target, value);
  401. };
  402. XMLNode.prototype.u = function() {
  403. return this.up();
  404. };
  405. XMLNode.prototype.importXMLBuilder = function(doc) {
  406. return this.importDocument(doc);
  407. };
  408. return XMLNode;
  409. })();
  410. }).call(this);