aboutsummaryrefslogtreecommitdiff
path: root/js/lib/node.js
blob: 7e236d02dcd8f5fb4b22d1f822683af351b78107 (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. }
  68. Node.prototype.isContainer = function() {
  69. return isContainer(this);
  70. };
  71. Node.prototype.appendChild = function(child) {
  72. child.unlink();
  73. child.parent = this;
  74. if (this.lastChild) {
  75. this.lastChild.next = child;
  76. child.prev = this.lastChild;
  77. this.lastChild = child;
  78. } else {
  79. this.firstChild = child;
  80. this.lastChild = child;
  81. }
  82. };
  83. Node.prototype.prependChild = function(child) {
  84. child.unlink();
  85. child.parent = this;
  86. if (this.firstChild) {
  87. this.firstChild.prev = child;
  88. child.next = this.firstChild;
  89. this.firstChild = child;
  90. } else {
  91. this.firstChild = child;
  92. this.lastChild = child;
  93. }
  94. };
  95. Node.prototype.unlink = function() {
  96. if (this.prev) {
  97. this.prev.next = this.next;
  98. } else if (this.parent) {
  99. this.parent.firstChild = this.next;
  100. }
  101. if (this.next) {
  102. this.next.prev = this.prev;
  103. } else if (this.parent) {
  104. this.parent.lastChild = this.prev;
  105. }
  106. this.parent = null;
  107. this.next = null;
  108. this.prev = null;
  109. };
  110. Node.prototype.insertAfter = function(sibling) {
  111. sibling.unlink();
  112. sibling.next = this.next;
  113. if (sibling.next) {
  114. sibling.next.prev = sibling;
  115. }
  116. sibling.prev = this;
  117. this.next = sibling;
  118. sibling.parent = this.parent;
  119. if (!sibling.next) {
  120. sibling.parent.lastChild = sibling;
  121. }
  122. };
  123. Node.prototype.insertBefore = function(sibling) {
  124. sibling.unlink();
  125. sibling.prev = this.prev;
  126. if (sibling.prev) {
  127. sibling.prev.next = sibling;
  128. }
  129. sibling.next = this;
  130. this.prev = sibling;
  131. sibling.parent = this.parent;
  132. if (!sibling.prev) {
  133. sibling.parent.firstChild = sibling;
  134. }
  135. };
  136. Node.prototype.walker = function() {
  137. var walker = new NodeWalker(this);
  138. return walker;
  139. };
  140. Node.prototype.toAST = function() {
  141. var children;
  142. var cur;
  143. var result = { t: this.t };
  144. var propsToShow = ['t', 'c', 'list_data', 'string_content',
  145. 'pos', 'tight', 'info'];
  146. for (var i = 0; i < propsToShow.length; i++) {
  147. var prop = propsToShow[i];
  148. if (this[prop] !== undefined) {
  149. result[prop] = this[prop];
  150. }
  151. }
  152. if (isContainer(this)) {
  153. children = [];
  154. if (this.firstChild) {
  155. cur = this.firstChild;
  156. while (cur) {
  157. // TODO avoid recursion here...
  158. children.push(cur.toAST());
  159. cur = cur.next;
  160. }
  161. result.children = children;
  162. }
  163. }
  164. return result;
  165. };
  166. module.exports = Node;
  167. /* Example of use of walker:
  168. var walker = w.walker();
  169. var event;
  170. while (event = walker.next()) {
  171. console.log(event.entering, event.node.t);
  172. }
  173. */