aboutsummaryrefslogtreecommitdiff
path: root/js/lib/node.js
blob: 81bda0582c74e99fbb0d5a536ba6231757988054 (plain)
  1. "use strict";
  2. function isContainer(node) {
  3. var t = node.t;
  4. return (t === 'Document' ||
  5. t === 'BlockQuote' ||
  6. t === 'List' ||
  7. t === 'ListItem' ||
  8. t === 'Paragraph' ||
  9. t === 'Header' ||
  10. t === 'Emph' ||
  11. t === 'Strong' ||
  12. t === 'Link' ||
  13. t === 'Image');
  14. }
  15. function NodeWalker(root) {
  16. this.current = root;
  17. this.root = root;
  18. this.entering = true;
  19. }
  20. NodeWalker.prototype.resumeAt = function(node, entering) {
  21. this.current = node;
  22. this.entering = (entering === true);
  23. };
  24. NodeWalker.prototype.next = function(){
  25. var cur = this.current;
  26. var entering = this.entering;
  27. if (!cur) {
  28. return null;
  29. }
  30. var container = isContainer(cur);
  31. if (entering && container) {
  32. if (cur.firstChild) {
  33. this.current = cur.firstChild;
  34. this.entering = true;
  35. } else {
  36. // stay on node but exit
  37. this.entering = false;
  38. }
  39. } else if (!entering && cur === this.root) {
  40. // don't move past root
  41. this.current = null;
  42. } else if (cur.next) {
  43. this.current = cur.next;
  44. this.entering = true;
  45. } else {
  46. this.current = cur.parent;
  47. this.entering = false;
  48. }
  49. return {entering: entering, node: cur};
  50. };
  51. function Node(nodeType, pos) {
  52. this.t = nodeType;
  53. this.parent = null;
  54. this.firstChild = null;
  55. this.lastChild = null;
  56. this.prev = null;
  57. this.next = null;
  58. this.pos = pos || {};
  59. this.last_line_blank = false;
  60. this.open = true;
  61. this.strings = [];
  62. this.string_content = "";
  63. this.c = undefined;
  64. this.list_data = undefined;
  65. this.tight = undefined;
  66. this.info = undefined;
  67. this.destination = undefined;
  68. this.title = undefined;
  69. }
  70. Node.prototype.isContainer = function() {
  71. return isContainer(this);
  72. };
  73. Node.prototype.appendChild = function(child) {
  74. child.unlink();
  75. child.parent = this;
  76. if (this.lastChild) {
  77. this.lastChild.next = child;
  78. child.prev = this.lastChild;
  79. this.lastChild = child;
  80. } else {
  81. this.firstChild = child;
  82. this.lastChild = child;
  83. }
  84. };
  85. Node.prototype.prependChild = function(child) {
  86. child.unlink();
  87. child.parent = this;
  88. if (this.firstChild) {
  89. this.firstChild.prev = child;
  90. child.next = this.firstChild;
  91. this.firstChild = child;
  92. } else {
  93. this.firstChild = child;
  94. this.lastChild = child;
  95. }
  96. };
  97. Node.prototype.unlink = function() {
  98. if (this.prev) {
  99. this.prev.next = this.next;
  100. } else if (this.parent) {
  101. this.parent.firstChild = this.next;
  102. }
  103. if (this.next) {
  104. this.next.prev = this.prev;
  105. } else if (this.parent) {
  106. this.parent.lastChild = this.prev;
  107. }
  108. this.parent = null;
  109. this.next = null;
  110. this.prev = null;
  111. };
  112. Node.prototype.insertAfter = function(sibling) {
  113. sibling.unlink();
  114. sibling.next = this.next;
  115. if (sibling.next) {
  116. sibling.next.prev = sibling;
  117. }
  118. sibling.prev = this;
  119. this.next = sibling;
  120. sibling.parent = this.parent;
  121. if (!sibling.next) {
  122. sibling.parent.lastChild = sibling;
  123. }
  124. };
  125. Node.prototype.insertBefore = function(sibling) {
  126. sibling.unlink();
  127. sibling.prev = this.prev;
  128. if (sibling.prev) {
  129. sibling.prev.next = sibling;
  130. }
  131. sibling.next = this;
  132. this.prev = sibling;
  133. sibling.parent = this.parent;
  134. if (!sibling.prev) {
  135. sibling.parent.firstChild = sibling;
  136. }
  137. };
  138. Node.prototype.walker = function() {
  139. var walker = new NodeWalker(this);
  140. return walker;
  141. };
  142. Node.prototype.toAST = function() {
  143. var children;
  144. var cur;
  145. var result = { t: this.t };
  146. var propsToShow = ['t', 'c', 'list_data', 'string_content',
  147. 'pos', 'tight', 'info'];
  148. for (var i = 0; i < propsToShow.length; i++) {
  149. var prop = propsToShow[i];
  150. if (this[prop] !== undefined) {
  151. result[prop] = this[prop];
  152. }
  153. }
  154. if (isContainer(this)) {
  155. children = [];
  156. if (this.firstChild) {
  157. cur = this.firstChild;
  158. while (cur) {
  159. // TODO avoid recursion here...
  160. children.push(cur.toAST());
  161. cur = cur.next;
  162. }
  163. result.children = children;
  164. }
  165. }
  166. return result;
  167. };
  168. module.exports = Node;
  169. /* Example of use of walker:
  170. var walker = w.walker();
  171. var event;
  172. while (event = walker.next()) {
  173. console.log(event.entering, event.node.t);
  174. }
  175. */