How to use the htmlparser2.DomHandler function in htmlparser2

To help you get started, we’ve selected a few htmlparser2 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github daliwali / simulacra / lib / render.js View on Github external
function render (obj, def, html) {
  var i, nodes, handler, parser, element, elementPrototype

  // If given bindings with a root node, pick only the binding keys.
  if (Array.isArray(def)) def = def[1]

  // Generating the render function is processing intensive. Skip if possible.
  if (renderFnKey in def) return def[renderFnKey](obj)

  // Callback API looks weird. This is actually synchronous, not asynchronous.
  handler = new htmlParser.DomHandler(function (error, result) {
    if (error) throw error
    nodes = result
  }, handlerOptions)

  parser = new htmlParser.Parser(handler)
  parser.write(html)
  parser.end()

  for (i = nodes.length; i--;)
    if (nodes[i].type === 'tag') {
      element = nodes[i]
      break
    }

  if (!element) throw new Error('No element found!')
github alibaba / structure-view / lib / tag-generators / html.js View on Github external
return new Promise(resolve => {
      const handler = new HtmlParser.DomHandler((err, dom) => {
        if (err) console.log(err);
        else {
          let tags = self.parseTags(dom);
          resolve(tags);
        }
      });
      const parser = new HtmlParser.Parser(handler);
      parser.write(ctx.content);
      parser.end();
    });
  },
github bradoyler / googledoc-to-json / index.js View on Github external
this.gDrive.files.export({ fileId, mimeType }, (err, response) => {
    if (err) {
      return callback(err)
    }

    const docHtml = response.data

    const handler = new htmlparser.DomHandler((error, dom) => {
      if (error) {
        console.error('(DomHandler)', error)
      }
      const tagHandlers = {
        _base: (tag) => {
          let str = ''
          tag.children.forEach(function (child) {
            const transform = tagHandlers[child.name || child.type]
            if (transform) {
              str += transform(child)
            }
          })
          return str
        },
        text: (textTag) => {
          return textTag.data
github fabiosantoscode / tjsx / lib / parse.js View on Github external
module.exports = function parseHTML (source) {
  var handler = new htmlparser2.DomHandler()
  var parser = new htmlparser2.Parser(handler, { lowerCaseTags: false, lowerCaseAttributeNames: false, xmlMode: true })
  parser.write(source)
  parser.done()
  trimWhitespace(handler.dom)
  if (handler.dom.length === 0) {
    assert(false, 'rel: no DOM elements returned!')
  }
  assert.strictEqual(handler.dom[0].type, 'tag',
    'rel: you need to pass HTML elements into rel`...`!'
  )
  assert.strictEqual(handler.dom.length, 1,
    'rel: like with JSX, you can only create a single element. Try returning an array of rel`...` strings instead!')
  return handler.dom[0]
}
github disoul / hanayo / hanayo / model / previewParse.js View on Github external
this.getPreview = function(html, callback){
  switch(this.previewStyle) {
    case 'short':
    var handler = new htmlparser.DomHandler(function(err, dom) {
      if (err) {
      throw err;
      } else {
      callback(select(dom, 'p')[0].children[0].data);
      }
    });
    var parse = new htmlparser.Parser(handler);
    parse.write(html);
    parse.done();
    break;
  }
  };
github alibaba / rax / packages / element-loader / src / parserHTML.js View on Github external
export function getDomObject(html) {
  const handler = new htmlparser.DomHandler();
  const parser = new htmlparser.Parser(handler, {
    xmlMode: true
  });

  parser.parseComplete(html);

  return handler.dom;
}
github alibaba / rax / packages / sfc-loader / src / sfc / parser.js View on Github external
function getDomObject(html) {
  const handler = new htmlparser.DomHandler();
  const parser = new htmlparser.Parser(handler, {
    xmlMode: true
  });

  parser.parseComplete(html);

  return handler.dom;
}
github superfly / fly / packages / v8env / src / document.ts View on Github external
function parseDOMStreaming(elemCb) {
  const handler = new htmlparser.DomHandler(
    () => {
      logger.debug("done parsing dom")
    },
    undefined,
    elem => {
      logger.debug("got an element!", elem.attribs)
      elemCb(elem)
    }
  )
  return new WritableParser(handler)
}
github GCE-NEIIST / GCE-NEIIST-webapp / server / services / thesis / thesis-services.js View on Github external
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data) {

        let handler = new htmlparser.DomHandler(function (error, dom) {
            if (error)  {
                callback(error,null,null);
            }
            else    {

                //Path to table responsive is children[3].children[7].children[1].children[3].children[9].children[7].children[3].children[3]
                //Path to table  is  dom[4].children[3].children[7].children[1].children[3].children[9].children[7].children[3].children[3].children[1]
                //Path to table head is dom[4].children[3].children[7].children[1].children[3].children[9].children[7].children[3].children[3].children[1].children[1].name);
                //Path to table body is dom[4].children[3].children[7].children[1].children[3].children[9].children[7].children[3].children[3].children[1].children[3].name);

                //Table body holds tr. Each tr corresponds to one master thesis
                let tableBody = dom[4].children[3].children[7].children[1].children[3].children[9].children[7].children[3].children[3].children[1].children[3];


                tableBody.children.forEach((element)=>  {
                    if (element.type === "tag" && element.name === "tr")  {