index.js 6.1 KB

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