index.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import { ElementType } from "domelementtype";
  2. import { Element, Text, Comment, CDATA, Document, ProcessingInstruction, } from "./node.js";
  3. export * from "./node.js";
  4. // Default options
  5. const defaultOpts = {
  6. withStartIndices: false,
  7. withEndIndices: false,
  8. xmlMode: false,
  9. };
  10. export class DomHandler {
  11. /**
  12. * @param callback Called once parsing has completed.
  13. * @param options Settings for the handler.
  14. * @param elementCB Callback whenever a tag is closed.
  15. */
  16. constructor(callback, options, elementCB) {
  17. /** The elements of the DOM */
  18. this.dom = [];
  19. /** The root element for the DOM */
  20. this.root = new Document(this.dom);
  21. /** Indicated whether parsing has been completed. */
  22. this.done = false;
  23. /** Stack of open tags. */
  24. this.tagStack = [this.root];
  25. /** A data node that is still being written to. */
  26. this.lastNode = null;
  27. /** Reference to the parser instance. Used for location information. */
  28. this.parser = null;
  29. // Make it possible to skip arguments, for backwards-compatibility
  30. if (typeof options === "function") {
  31. elementCB = options;
  32. options = defaultOpts;
  33. }
  34. if (typeof callback === "object") {
  35. options = callback;
  36. callback = undefined;
  37. }
  38. this.callback = callback !== null && callback !== void 0 ? callback : null;
  39. this.options = options !== null && options !== void 0 ? options : defaultOpts;
  40. this.elementCB = elementCB !== null && elementCB !== void 0 ? elementCB : null;
  41. }
  42. onparserinit(parser) {
  43. this.parser = parser;
  44. }
  45. // Resets the handler back to starting state
  46. onreset() {
  47. this.dom = [];
  48. this.root = new Document(this.dom);
  49. this.done = false;
  50. this.tagStack = [this.root];
  51. this.lastNode = null;
  52. this.parser = null;
  53. }
  54. // Signals the handler that parsing is done
  55. onend() {
  56. if (this.done)
  57. return;
  58. this.done = true;
  59. this.parser = null;
  60. this.handleCallback(null);
  61. }
  62. onerror(error) {
  63. this.handleCallback(error);
  64. }
  65. onclosetag() {
  66. this.lastNode = null;
  67. const elem = this.tagStack.pop();
  68. if (this.options.withEndIndices) {
  69. elem.endIndex = this.parser.endIndex;
  70. }
  71. if (this.elementCB)
  72. this.elementCB(elem);
  73. }
  74. onopentag(name, attribs) {
  75. const type = this.options.xmlMode ? ElementType.Tag : undefined;
  76. const element = new Element(name, attribs, undefined, type);
  77. this.addNode(element);
  78. this.tagStack.push(element);
  79. }
  80. ontext(data) {
  81. const { lastNode } = this;
  82. if (lastNode && lastNode.type === ElementType.Text) {
  83. lastNode.data += data;
  84. if (this.options.withEndIndices) {
  85. lastNode.endIndex = this.parser.endIndex;
  86. }
  87. }
  88. else {
  89. const node = new Text(data);
  90. this.addNode(node);
  91. this.lastNode = node;
  92. }
  93. }
  94. oncomment(data) {
  95. if (this.lastNode && this.lastNode.type === ElementType.Comment) {
  96. this.lastNode.data += data;
  97. return;
  98. }
  99. const node = new Comment(data);
  100. this.addNode(node);
  101. this.lastNode = node;
  102. }
  103. oncommentend() {
  104. this.lastNode = null;
  105. }
  106. oncdatastart() {
  107. const text = new Text("");
  108. const node = new CDATA([text]);
  109. this.addNode(node);
  110. text.parent = node;
  111. this.lastNode = text;
  112. }
  113. oncdataend() {
  114. this.lastNode = null;
  115. }
  116. onprocessinginstruction(name, data) {
  117. const node = new ProcessingInstruction(name, data);
  118. this.addNode(node);
  119. }
  120. handleCallback(error) {
  121. if (typeof this.callback === "function") {
  122. this.callback(error, this.dom);
  123. }
  124. else if (error) {
  125. throw error;
  126. }
  127. }
  128. addNode(node) {
  129. const parent = this.tagStack[this.tagStack.length - 1];
  130. const previousSibling = parent.children[parent.children.length - 1];
  131. if (this.options.withStartIndices) {
  132. node.startIndex = this.parser.startIndex;
  133. }
  134. if (this.options.withEndIndices) {
  135. node.endIndex = this.parser.endIndex;
  136. }
  137. parent.children.push(node);
  138. if (previousSibling) {
  139. node.prev = previousSibling;
  140. previousSibling.next = node;
  141. }
  142. node.parent = parent;
  143. this.lastNode = null;
  144. }
  145. }
  146. export default DomHandler;