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