index.js 6.4 KB

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