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