aboutsummaryrefslogtreecommitdiff
path: root/js/lib/node.js
blob: 89172d70cb8d4a5f6c11d57251772658d867c8b9 (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.literal = 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. var toASTNode = function(node) {
  146. var result = {};
  147. var propsToShow = ['t', 'literal', 'list_data', 'sourcepos',
  148. 'info', 'level'];
  149. for (var i = 0, len = propsToShow.length; i < len; i++) {
  150. var prop = propsToShow[i];
  151. if (node[prop] !== undefined) {
  152. result[prop] = node[prop];
  153. }
  154. }
  155. return result;
  156. };
  157. Node.prototype.toAST = function() {
  158. var childrenStack = [];
  159. var walker = this.walker();
  160. var event;
  161. while ((event = walker.next())) {
  162. var node = event.node;
  163. var entering = event.entering;
  164. var container = node.isContainer();
  165. var astnode;
  166. if (container) {
  167. if (entering) {
  168. childrenStack.push([]);
  169. } else {
  170. astnode = toASTNode(node);
  171. astnode.children = childrenStack.pop();
  172. if (childrenStack.length > 0) {
  173. childrenStack[childrenStack.length - 1].push(astnode);
  174. }
  175. }
  176. } else {
  177. astnode = toASTNode(node);
  178. childrenStack[childrenStack.length - 1].push(astnode);
  179. }
  180. }
  181. return astnode;
  182. };
  183. module.exports = Node;
  184. /* Example of use of walker:
  185. var walker = w.walker();
  186. var event;
  187. while (event = walker.next()) {
  188. console.log(event.entering, event.node.t);
  189. }
  190. */