index.js 6.0 KB

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