aboutsummaryrefslogtreecommitdiff
path: root/js/lib/node.js
blob: 8e424a213de3f51133885eb7c14cb41a57feb0ad (plain)
  1. "use strict";
  2. function isContainer(node) {
  3. switch (node._type) {
  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 === null) {
  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 === null) {
  39. this.current = cur.parent;
  40. this.entering = false;
  41. } else {
  42. this.current = cur.next;
  43. this.entering = true;
  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._type = 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 = null;
  65. this.string_content = null;
  66. this.literal = null;
  67. this.list_data = null;
  68. this.info = null;
  69. this.destination = null;
  70. this.title = null;
  71. this.fence_char = null;
  72. this.fence_length = 0;
  73. this.fence_offset = null;
  74. this.level = null;
  75. };
  76. Node.prototype.isContainer = function() {
  77. return isContainer(this);
  78. };
  79. Node.prototype.type = function() {
  80. return this._type;
  81. };
  82. Node.prototype.appendChild = function(child) {
  83. child.unlink();
  84. child.parent = this;
  85. if (this.lastChild) {
  86. this.lastChild.next = child;
  87. child.prev = this.lastChild;
  88. this.lastChild = child;
  89. } else {
  90. this.firstChild = child;
  91. this.lastChild = child;
  92. }
  93. };
  94. Node.prototype.prependChild = function(child) {
  95. child.unlink();
  96. child.parent = this;
  97. if (this.firstChild) {
  98. this.firstChild.prev = child;
  99. child.next = this.firstChild;
  100. this.firstChild = child;
  101. } else {
  102. this.firstChild = child;
  103. this.lastChild = child;
  104. }
  105. };
  106. Node.prototype.unlink = function() {
  107. if (this.prev) {
  108. this.prev.next = this.next;
  109. } else if (this.parent) {
  110. this.parent.firstChild = this.next;
  111. }
  112. if (this.next) {
  113. this.next.prev = this.prev;
  114. } else if (this.parent) {
  115. this.parent.lastChild = this.prev;
  116. }
  117. this.parent = null;
  118. this.next = null;
  119. this.prev = null;
  120. };
  121. Node.prototype.insertAfter = function(sibling) {
  122. sibling.unlink();
  123. sibling.next = this.next;
  124. if (sibling.next) {
  125. sibling.next.prev = sibling;
  126. }
  127. sibling.prev = this;
  128. this.next = sibling;
  129. sibling.parent = this.parent;
  130. if (!sibling.next) {
  131. sibling.parent.lastChild = sibling;
  132. }
  133. };
  134. Node.prototype.insertBefore = function(sibling) {
  135. sibling.unlink();
  136. sibling.prev = this.prev;
  137. if (sibling.prev) {
  138. sibling.prev.next = sibling;
  139. }
  140. sibling.next = this;
  141. this.prev = sibling;
  142. sibling.parent = this.parent;
  143. if (!sibling.prev) {
  144. sibling.parent.firstChild = sibling;
  145. }
  146. };
  147. Node.prototype.walker = function() {
  148. var walker = new NodeWalker(this);
  149. return walker;
  150. };
  151. module.exports = Node;
  152. /* Example of use of walker:
  153. var walker = w.walker();
  154. var event;
  155. while (event = walker.next()) {
  156. console.log(event.entering, event.node.type());
  157. }
  158. */