index.js 5.1 KB

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