XMLNode.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. // Generated by CoffeeScript 1.12.7
  2. (function() {
  3. var DocumentPosition, NodeType, XMLCData, XMLComment, XMLDeclaration, XMLDocType, XMLDummy, XMLElement, XMLNamedNodeMap, XMLNode, XMLNodeList, XMLProcessingInstruction, XMLRaw, XMLText, getValue, isEmpty, isFunction, isObject, ref1,
  4. hasProp = {}.hasOwnProperty;
  5. ref1 = require('./Utility'), isObject = ref1.isObject, isFunction = ref1.isFunction, isEmpty = ref1.isEmpty, getValue = ref1.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. NodeType = null;
  16. XMLNodeList = null;
  17. XMLNamedNodeMap = null;
  18. DocumentPosition = null;
  19. module.exports = XMLNode = (function() {
  20. function XMLNode(parent1) {
  21. this.parent = parent1;
  22. if (this.parent) {
  23. this.options = this.parent.options;
  24. this.stringify = this.parent.stringify;
  25. }
  26. this.value = null;
  27. this.children = [];
  28. this.baseURI = null;
  29. if (!XMLElement) {
  30. XMLElement = require('./XMLElement');
  31. XMLCData = require('./XMLCData');
  32. XMLComment = require('./XMLComment');
  33. XMLDeclaration = require('./XMLDeclaration');
  34. XMLDocType = require('./XMLDocType');
  35. XMLRaw = require('./XMLRaw');
  36. XMLText = require('./XMLText');
  37. XMLProcessingInstruction = require('./XMLProcessingInstruction');
  38. XMLDummy = require('./XMLDummy');
  39. NodeType = require('./NodeType');
  40. XMLNodeList = require('./XMLNodeList');
  41. XMLNamedNodeMap = require('./XMLNamedNodeMap');
  42. DocumentPosition = require('./DocumentPosition');
  43. }
  44. }
  45. Object.defineProperty(XMLNode.prototype, 'nodeName', {
  46. get: function() {
  47. return this.name;
  48. }
  49. });
  50. Object.defineProperty(XMLNode.prototype, 'nodeType', {
  51. get: function() {
  52. return this.type;
  53. }
  54. });
  55. Object.defineProperty(XMLNode.prototype, 'nodeValue', {
  56. get: function() {
  57. return this.value;
  58. }
  59. });
  60. Object.defineProperty(XMLNode.prototype, 'parentNode', {
  61. get: function() {
  62. return this.parent;
  63. }
  64. });
  65. Object.defineProperty(XMLNode.prototype, 'childNodes', {
  66. get: function() {
  67. if (!this.childNodeList || !this.childNodeList.nodes) {
  68. this.childNodeList = new XMLNodeList(this.children);
  69. }
  70. return this.childNodeList;
  71. }
  72. });
  73. Object.defineProperty(XMLNode.prototype, 'firstChild', {
  74. get: function() {
  75. return this.children[0] || null;
  76. }
  77. });
  78. Object.defineProperty(XMLNode.prototype, 'lastChild', {
  79. get: function() {
  80. return this.children[this.children.length - 1] || null;
  81. }
  82. });
  83. Object.defineProperty(XMLNode.prototype, 'previousSibling', {
  84. get: function() {
  85. var i;
  86. i = this.parent.children.indexOf(this);
  87. return this.parent.children[i - 1] || null;
  88. }
  89. });
  90. Object.defineProperty(XMLNode.prototype, 'nextSibling', {
  91. get: function() {
  92. var i;
  93. i = this.parent.children.indexOf(this);
  94. return this.parent.children[i + 1] || null;
  95. }
  96. });
  97. Object.defineProperty(XMLNode.prototype, 'ownerDocument', {
  98. get: function() {
  99. return this.document() || null;
  100. }
  101. });
  102. Object.defineProperty(XMLNode.prototype, 'textContent', {
  103. get: function() {
  104. var child, j, len, ref2, str;
  105. if (this.nodeType === NodeType.Element || this.nodeType === NodeType.DocumentFragment) {
  106. str = '';
  107. ref2 = this.children;
  108. for (j = 0, len = ref2.length; j < len; j++) {
  109. child = ref2[j];
  110. if (child.textContent) {
  111. str += child.textContent;
  112. }
  113. }
  114. return str;
  115. } else {
  116. return null;
  117. }
  118. },
  119. set: function(value) {
  120. throw new Error("This DOM method is not implemented." + this.debugInfo());
  121. }
  122. });
  123. XMLNode.prototype.setParent = function(parent) {
  124. var child, j, len, ref2, results;
  125. this.parent = parent;
  126. if (parent) {
  127. this.options = parent.options;
  128. this.stringify = parent.stringify;
  129. }
  130. ref2 = this.children;
  131. results = [];
  132. for (j = 0, len = ref2.length; j < len; j++) {
  133. child = ref2[j];
  134. results.push(child.setParent(this));
  135. }
  136. return results;
  137. };
  138. XMLNode.prototype.element = function(name, attributes, text) {
  139. var childNode, item, j, k, key, lastChild, len, len1, ref2, ref3, val;
  140. lastChild = null;
  141. if (attributes === null && (text == null)) {
  142. ref2 = [{}, null], attributes = ref2[0], text = ref2[1];
  143. }
  144. if (attributes == null) {
  145. attributes = {};
  146. }
  147. attributes = getValue(attributes);
  148. if (!isObject(attributes)) {
  149. ref3 = [attributes, text], text = ref3[0], attributes = ref3[1];
  150. }
  151. if (name != null) {
  152. name = getValue(name);
  153. }
  154. if (Array.isArray(name)) {
  155. for (j = 0, len = name.length; j < len; j++) {
  156. item = name[j];
  157. lastChild = this.element(item);
  158. }
  159. } else if (isFunction(name)) {
  160. lastChild = this.element(name.apply());
  161. } else if (isObject(name)) {
  162. for (key in name) {
  163. if (!hasProp.call(name, key)) continue;
  164. val = name[key];
  165. if (isFunction(val)) {
  166. val = val.apply();
  167. }
  168. if (!this.options.ignoreDecorators && this.stringify.convertAttKey && key.indexOf(this.stringify.convertAttKey) === 0) {
  169. lastChild = this.attribute(key.substr(this.stringify.convertAttKey.length), val);
  170. } else if (!this.options.separateArrayItems && Array.isArray(val) && isEmpty(val)) {
  171. lastChild = this.dummy();
  172. } else if (isObject(val) && isEmpty(val)) {
  173. lastChild = this.element(key);
  174. } else if (!this.options.keepNullNodes && (val == null)) {
  175. lastChild = this.dummy();
  176. } else if (!this.options.separateArrayItems && Array.isArray(val)) {
  177. for (k = 0, len1 = val.length; k < len1; k++) {
  178. item = val[k];
  179. childNode = {};
  180. childNode[key] = item;
  181. lastChild = this.element(childNode);
  182. }
  183. } else if (isObject(val)) {
  184. if (!this.options.ignoreDecorators && this.stringify.convertTextKey && key.indexOf(this.stringify.convertTextKey) === 0) {
  185. lastChild = this.element(val);
  186. } else {
  187. lastChild = this.element(key);
  188. lastChild.element(val);
  189. }
  190. } else {
  191. lastChild = this.element(key, val);
  192. }
  193. }
  194. } else if (!this.options.keepNullNodes && text === null) {
  195. lastChild = this.dummy();
  196. } else {
  197. if (!this.options.ignoreDecorators && this.stringify.convertTextKey && name.indexOf(this.stringify.convertTextKey) === 0) {
  198. lastChild = this.text(text);
  199. } else if (!this.options.ignoreDecorators && this.stringify.convertCDataKey && name.indexOf(this.stringify.convertCDataKey) === 0) {
  200. lastChild = this.cdata(text);
  201. } else if (!this.options.ignoreDecorators && this.stringify.convertCommentKey && name.indexOf(this.stringify.convertCommentKey) === 0) {
  202. lastChild = this.comment(text);
  203. } else if (!this.options.ignoreDecorators && this.stringify.convertRawKey && name.indexOf(this.stringify.convertRawKey) === 0) {
  204. lastChild = this.raw(text);
  205. } else if (!this.options.ignoreDecorators && this.stringify.convertPIKey && name.indexOf(this.stringify.convertPIKey) === 0) {
  206. lastChild = this.instruction(name.substr(this.stringify.convertPIKey.length), text);
  207. } else {
  208. lastChild = this.node(name, attributes, text);
  209. }
  210. }
  211. if (lastChild == null) {
  212. throw new Error("Could not create any elements with: " + name + ". " + this.debugInfo());
  213. }
  214. return lastChild;
  215. };
  216. XMLNode.prototype.insertBefore = function(name, attributes, text) {
  217. var child, i, newChild, refChild, removed;
  218. if (name != null ? name.type : void 0) {
  219. newChild = name;
  220. refChild = attributes;
  221. newChild.setParent(this);
  222. if (refChild) {
  223. i = children.indexOf(refChild);
  224. removed = children.splice(i);
  225. children.push(newChild);
  226. Array.prototype.push.apply(children, removed);
  227. } else {
  228. children.push(newChild);
  229. }
  230. return newChild;
  231. } else {
  232. if (this.isRoot) {
  233. throw new Error("Cannot insert elements at root level. " + this.debugInfo(name));
  234. }
  235. i = this.parent.children.indexOf(this);
  236. removed = this.parent.children.splice(i);
  237. child = this.parent.element(name, attributes, text);
  238. Array.prototype.push.apply(this.parent.children, removed);
  239. return child;
  240. }
  241. };
  242. XMLNode.prototype.insertAfter = function(name, attributes, text) {
  243. var child, i, removed;
  244. if (this.isRoot) {
  245. throw new Error("Cannot insert elements at root level. " + this.debugInfo(name));
  246. }
  247. i = this.parent.children.indexOf(this);
  248. removed = this.parent.children.splice(i + 1);
  249. child = this.parent.element(name, attributes, text);
  250. Array.prototype.push.apply(this.parent.children, removed);
  251. return child;
  252. };
  253. XMLNode.prototype.remove = function() {
  254. var i, ref2;
  255. if (this.isRoot) {
  256. throw new Error("Cannot remove the root element. " + this.debugInfo());
  257. }
  258. i = this.parent.children.indexOf(this);
  259. [].splice.apply(this.parent.children, [i, i - i + 1].concat(ref2 = [])), ref2;
  260. return this.parent;
  261. };
  262. XMLNode.prototype.node = function(name, attributes, text) {
  263. var child, ref2;
  264. if (name != null) {
  265. name = getValue(name);
  266. }
  267. attributes || (attributes = {});
  268. attributes = getValue(attributes);
  269. if (!isObject(attributes)) {
  270. ref2 = [attributes, text], text = ref2[0], attributes = ref2[1];
  271. }
  272. child = new XMLElement(this, name, attributes);
  273. if (text != null) {
  274. child.text(text);
  275. }
  276. this.children.push(child);
  277. return child;
  278. };
  279. XMLNode.prototype.text = function(value) {
  280. var child;
  281. if (isObject(value)) {
  282. this.element(value);
  283. }
  284. child = new XMLText(this, value);
  285. this.children.push(child);
  286. return this;
  287. };
  288. XMLNode.prototype.cdata = function(value) {
  289. var child;
  290. child = new XMLCData(this, value);
  291. this.children.push(child);
  292. return this;
  293. };
  294. XMLNode.prototype.comment = function(value) {
  295. var child;
  296. child = new XMLComment(this, value);
  297. this.children.push(child);
  298. return this;
  299. };
  300. XMLNode.prototype.commentBefore = function(value) {
  301. var child, i, removed;
  302. i = this.parent.children.indexOf(this);
  303. removed = this.parent.children.splice(i);
  304. child = this.parent.comment(value);
  305. Array.prototype.push.apply(this.parent.children, removed);
  306. return this;
  307. };
  308. XMLNode.prototype.commentAfter = function(value) {
  309. var child, i, removed;
  310. i = this.parent.children.indexOf(this);
  311. removed = this.parent.children.splice(i + 1);
  312. child = this.parent.comment(value);
  313. Array.prototype.push.apply(this.parent.children, removed);
  314. return this;
  315. };
  316. XMLNode.prototype.raw = function(value) {
  317. var child;
  318. child = new XMLRaw(this, value);
  319. this.children.push(child);
  320. return this;
  321. };
  322. XMLNode.prototype.dummy = function() {
  323. var child;
  324. child = new XMLDummy(this);
  325. return child;
  326. };
  327. XMLNode.prototype.instruction = function(target, value) {
  328. var insTarget, insValue, instruction, j, len;
  329. if (target != null) {
  330. target = getValue(target);
  331. }
  332. if (value != null) {
  333. value = getValue(value);
  334. }
  335. if (Array.isArray(target)) {
  336. for (j = 0, len = target.length; j < len; j++) {
  337. insTarget = target[j];
  338. this.instruction(insTarget);
  339. }
  340. } else if (isObject(target)) {
  341. for (insTarget in target) {
  342. if (!hasProp.call(target, insTarget)) continue;
  343. insValue = target[insTarget];
  344. this.instruction(insTarget, insValue);
  345. }
  346. } else {
  347. if (isFunction(value)) {
  348. value = value.apply();
  349. }
  350. instruction = new XMLProcessingInstruction(this, target, value);
  351. this.children.push(instruction);
  352. }
  353. return this;
  354. };
  355. XMLNode.prototype.instructionBefore = function(target, value) {
  356. var child, i, removed;
  357. i = this.parent.children.indexOf(this);
  358. removed = this.parent.children.splice(i);
  359. child = this.parent.instruction(target, value);
  360. Array.prototype.push.apply(this.parent.children, removed);
  361. return this;
  362. };
  363. XMLNode.prototype.instructionAfter = function(target, value) {
  364. var child, i, removed;
  365. i = this.parent.children.indexOf(this);
  366. removed = this.parent.children.splice(i + 1);
  367. child = this.parent.instruction(target, value);
  368. Array.prototype.push.apply(this.parent.children, removed);
  369. return this;
  370. };
  371. XMLNode.prototype.declaration = function(version, encoding, standalone) {
  372. var doc, xmldec;
  373. doc = this.document();
  374. xmldec = new XMLDeclaration(doc, version, encoding, standalone);
  375. if (doc.children.length === 0) {
  376. doc.children.unshift(xmldec);
  377. } else if (doc.children[0].type === NodeType.Declaration) {
  378. doc.children[0] = xmldec;
  379. } else {
  380. doc.children.unshift(xmldec);
  381. }
  382. return doc.root() || doc;
  383. };
  384. XMLNode.prototype.dtd = function(pubID, sysID) {
  385. var child, doc, doctype, i, j, k, len, len1, ref2, ref3;
  386. doc = this.document();
  387. doctype = new XMLDocType(doc, pubID, sysID);
  388. ref2 = doc.children;
  389. for (i = j = 0, len = ref2.length; j < len; i = ++j) {
  390. child = ref2[i];
  391. if (child.type === NodeType.DocType) {
  392. doc.children[i] = doctype;
  393. return doctype;
  394. }
  395. }
  396. ref3 = doc.children;
  397. for (i = k = 0, len1 = ref3.length; k < len1; i = ++k) {
  398. child = ref3[i];
  399. if (child.isRoot) {
  400. doc.children.splice(i, 0, doctype);
  401. return doctype;
  402. }
  403. }
  404. doc.children.push(doctype);
  405. return doctype;
  406. };
  407. XMLNode.prototype.up = function() {
  408. if (this.isRoot) {
  409. throw new Error("The root node has no parent. Use doc() if you need to get the document object.");
  410. }
  411. return this.parent;
  412. };
  413. XMLNode.prototype.root = function() {
  414. var node;
  415. node = this;
  416. while (node) {
  417. if (node.type === NodeType.Document) {
  418. return node.rootObject;
  419. } else if (node.isRoot) {
  420. return node;
  421. } else {
  422. node = node.parent;
  423. }
  424. }
  425. };
  426. XMLNode.prototype.document = function() {
  427. var node;
  428. node = this;
  429. while (node) {
  430. if (node.type === NodeType.Document) {
  431. return node;
  432. } else {
  433. node = node.parent;
  434. }
  435. }
  436. };
  437. XMLNode.prototype.end = function(options) {
  438. return this.document().end(options);
  439. };
  440. XMLNode.prototype.prev = function() {
  441. var i;
  442. i = this.parent.children.indexOf(this);
  443. if (i < 1) {
  444. throw new Error("Already at the first node. " + this.debugInfo());
  445. }
  446. return this.parent.children[i - 1];
  447. };
  448. XMLNode.prototype.next = function() {
  449. var i;
  450. i = this.parent.children.indexOf(this);
  451. if (i === -1 || i === this.parent.children.length - 1) {
  452. throw new Error("Already at the last node. " + this.debugInfo());
  453. }
  454. return this.parent.children[i + 1];
  455. };
  456. XMLNode.prototype.importDocument = function(doc) {
  457. var clonedRoot;
  458. clonedRoot = doc.root().clone();
  459. clonedRoot.parent = this;
  460. clonedRoot.isRoot = false;
  461. this.children.push(clonedRoot);
  462. return this;
  463. };
  464. XMLNode.prototype.debugInfo = function(name) {
  465. var ref2, ref3;
  466. name = name || this.name;
  467. if ((name == null) && !((ref2 = this.parent) != null ? ref2.name : void 0)) {
  468. return "";
  469. } else if (name == null) {
  470. return "parent: <" + this.parent.name + ">";
  471. } else if (!((ref3 = this.parent) != null ? ref3.name : void 0)) {
  472. return "node: <" + name + ">";
  473. } else {
  474. return "node: <" + name + ">, parent: <" + this.parent.name + ">";
  475. }
  476. };
  477. XMLNode.prototype.ele = function(name, attributes, text) {
  478. return this.element(name, attributes, text);
  479. };
  480. XMLNode.prototype.nod = function(name, attributes, text) {
  481. return this.node(name, attributes, text);
  482. };
  483. XMLNode.prototype.txt = function(value) {
  484. return this.text(value);
  485. };
  486. XMLNode.prototype.dat = function(value) {
  487. return this.cdata(value);
  488. };
  489. XMLNode.prototype.com = function(value) {
  490. return this.comment(value);
  491. };
  492. XMLNode.prototype.ins = function(target, value) {
  493. return this.instruction(target, value);
  494. };
  495. XMLNode.prototype.doc = function() {
  496. return this.document();
  497. };
  498. XMLNode.prototype.dec = function(version, encoding, standalone) {
  499. return this.declaration(version, encoding, standalone);
  500. };
  501. XMLNode.prototype.e = function(name, attributes, text) {
  502. return this.element(name, attributes, text);
  503. };
  504. XMLNode.prototype.n = function(name, attributes, text) {
  505. return this.node(name, attributes, text);
  506. };
  507. XMLNode.prototype.t = function(value) {
  508. return this.text(value);
  509. };
  510. XMLNode.prototype.d = function(value) {
  511. return this.cdata(value);
  512. };
  513. XMLNode.prototype.c = function(value) {
  514. return this.comment(value);
  515. };
  516. XMLNode.prototype.r = function(value) {
  517. return this.raw(value);
  518. };
  519. XMLNode.prototype.i = function(target, value) {
  520. return this.instruction(target, value);
  521. };
  522. XMLNode.prototype.u = function() {
  523. return this.up();
  524. };
  525. XMLNode.prototype.importXMLBuilder = function(doc) {
  526. return this.importDocument(doc);
  527. };
  528. XMLNode.prototype.replaceChild = function(newChild, oldChild) {
  529. throw new Error("This DOM method is not implemented." + this.debugInfo());
  530. };
  531. XMLNode.prototype.removeChild = function(oldChild) {
  532. throw new Error("This DOM method is not implemented." + this.debugInfo());
  533. };
  534. XMLNode.prototype.appendChild = function(newChild) {
  535. throw new Error("This DOM method is not implemented." + this.debugInfo());
  536. };
  537. XMLNode.prototype.hasChildNodes = function() {
  538. return this.children.length !== 0;
  539. };
  540. XMLNode.prototype.cloneNode = function(deep) {
  541. throw new Error("This DOM method is not implemented." + this.debugInfo());
  542. };
  543. XMLNode.prototype.normalize = function() {
  544. throw new Error("This DOM method is not implemented." + this.debugInfo());
  545. };
  546. XMLNode.prototype.isSupported = function(feature, version) {
  547. return true;
  548. };
  549. XMLNode.prototype.hasAttributes = function() {
  550. return this.attribs.length !== 0;
  551. };
  552. XMLNode.prototype.compareDocumentPosition = function(other) {
  553. var ref, res;
  554. ref = this;
  555. if (ref === other) {
  556. return 0;
  557. } else if (this.document() !== other.document()) {
  558. res = DocumentPosition.Disconnected | DocumentPosition.ImplementationSpecific;
  559. if (Math.random() < 0.5) {
  560. res |= DocumentPosition.Preceding;
  561. } else {
  562. res |= DocumentPosition.Following;
  563. }
  564. return res;
  565. } else if (ref.isAncestor(other)) {
  566. return DocumentPosition.Contains | DocumentPosition.Preceding;
  567. } else if (ref.isDescendant(other)) {
  568. return DocumentPosition.Contains | DocumentPosition.Following;
  569. } else if (ref.isPreceding(other)) {
  570. return DocumentPosition.Preceding;
  571. } else {
  572. return DocumentPosition.Following;
  573. }
  574. };
  575. XMLNode.prototype.isSameNode = function(other) {
  576. throw new Error("This DOM method is not implemented." + this.debugInfo());
  577. };
  578. XMLNode.prototype.lookupPrefix = function(namespaceURI) {
  579. throw new Error("This DOM method is not implemented." + this.debugInfo());
  580. };
  581. XMLNode.prototype.isDefaultNamespace = function(namespaceURI) {
  582. throw new Error("This DOM method is not implemented." + this.debugInfo());
  583. };
  584. XMLNode.prototype.lookupNamespaceURI = function(prefix) {
  585. throw new Error("This DOM method is not implemented." + this.debugInfo());
  586. };
  587. XMLNode.prototype.isEqualNode = function(node) {
  588. var i, j, ref2;
  589. if (node.nodeType !== this.nodeType) {
  590. return false;
  591. }
  592. if (node.children.length !== this.children.length) {
  593. return false;
  594. }
  595. for (i = j = 0, ref2 = this.children.length - 1; 0 <= ref2 ? j <= ref2 : j >= ref2; i = 0 <= ref2 ? ++j : --j) {
  596. if (!this.children[i].isEqualNode(node.children[i])) {
  597. return false;
  598. }
  599. }
  600. return true;
  601. };
  602. XMLNode.prototype.getFeature = function(feature, version) {
  603. throw new Error("This DOM method is not implemented." + this.debugInfo());
  604. };
  605. XMLNode.prototype.setUserData = function(key, data, handler) {
  606. throw new Error("This DOM method is not implemented." + this.debugInfo());
  607. };
  608. XMLNode.prototype.getUserData = function(key) {
  609. throw new Error("This DOM method is not implemented." + this.debugInfo());
  610. };
  611. XMLNode.prototype.contains = function(other) {
  612. if (!other) {
  613. return false;
  614. }
  615. return other === this || this.isDescendant(other);
  616. };
  617. XMLNode.prototype.isDescendant = function(node) {
  618. var child, isDescendantChild, j, len, ref2;
  619. ref2 = this.children;
  620. for (j = 0, len = ref2.length; j < len; j++) {
  621. child = ref2[j];
  622. if (node === child) {
  623. return true;
  624. }
  625. isDescendantChild = child.isDescendant(node);
  626. if (isDescendantChild) {
  627. return true;
  628. }
  629. }
  630. return false;
  631. };
  632. XMLNode.prototype.isAncestor = function(node) {
  633. return node.isDescendant(this);
  634. };
  635. XMLNode.prototype.isPreceding = function(node) {
  636. var nodePos, thisPos;
  637. nodePos = this.treePosition(node);
  638. thisPos = this.treePosition(this);
  639. if (nodePos === -1 || thisPos === -1) {
  640. return false;
  641. } else {
  642. return nodePos < thisPos;
  643. }
  644. };
  645. XMLNode.prototype.isFollowing = function(node) {
  646. var nodePos, thisPos;
  647. nodePos = this.treePosition(node);
  648. thisPos = this.treePosition(this);
  649. if (nodePos === -1 || thisPos === -1) {
  650. return false;
  651. } else {
  652. return nodePos > thisPos;
  653. }
  654. };
  655. XMLNode.prototype.treePosition = function(node) {
  656. var found, pos;
  657. pos = 0;
  658. found = false;
  659. this.foreachTreeNode(this.document(), function(childNode) {
  660. pos++;
  661. if (!found && childNode === node) {
  662. return found = true;
  663. }
  664. });
  665. if (found) {
  666. return pos;
  667. } else {
  668. return -1;
  669. }
  670. };
  671. XMLNode.prototype.foreachTreeNode = function(node, func) {
  672. var child, j, len, ref2, res;
  673. node || (node = this.document());
  674. ref2 = node.children;
  675. for (j = 0, len = ref2.length; j < len; j++) {
  676. child = ref2[j];
  677. if (res = func(child)) {
  678. return res;
  679. } else {
  680. res = this.foreachTreeNode(child, func);
  681. if (res) {
  682. return res;
  683. }
  684. }
  685. }
  686. };
  687. return XMLNode;
  688. })();
  689. }).call(this);