Source: parser.js

var HtmlParser = require('htmlparser2')
  , Utils = require('src/utils')
  , AST = require('src/ast')
  , DomHandler = require('src/dom-handler')
  , stream = require('stream')
  , util = require('util')
;

/**
 * The document parser
 * @class Parser
 * @param {Object|Function} [opts] The parser options
 * @param {Function} done The callback once parsing is complete
 */
var Parser = module.exports = function Parser(opts)
{
  if(!(this instanceof Parser))
    return new Parser(opts);

  stream.Transform.call(this);

  var self = this
    , opts = Utils.defaults({}, opts)
  ;

  this._readableState.objectMode = true;
  this._writableState.objectMode = false;
  this.htmlparser = new HtmlParser.Parser(new DomHandler(this));
}
util.inherits(Parser, stream.Transform);

/**
 * Handles the transform stream
 * @function Parser#_transform
 * @param  {Buffer} chunk - The data buffer
 * @param  {string} enc - The data encoding
 * @param  {Function} fn - The callback function
 */
Parser.prototype._transform = function(chunk, enc, fn)
{
  this.htmlparser.write(chunk);
  fn();
}

Parser.prototype.end = function()
{
  this.htmlparser.end();
  this.push(null);
}