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