How to use the parse5.SAXParser function in parse5

To help you get started, we’ve selected a few parse5 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 DefinitelyTyped / DefinitelyTyped / parse5 / parse5-tests.ts View on Github external
// Type definitions for parse5 2.1.5
// Project: https://github.com/inikulin/parse5
// Definitions by: Nico Jansen 
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped

/// 

import * as parse5 from 'parse5';

// parse5.SAXParser()
var parser = new parse5.SAXParser({ locationInfo: true });
parser.on('startTag', (name, attrs, selfClosing, location) => {
    console.log(name, attrs, selfClosing, location);
});
parser.on('text', (text, location)  => {
    console.log(text, location);
});

// parse5.parse()
parse5.parse('html', { locationInfo: true, treeAdapter: parse5.treeAdapters.default });
parse5.parse('html', {});
parse5.parse('html');

// parse5.ParserStream()
var parserStream = new parse5.ParserStream({ locationInfo: true, treeAdapter: parse5.treeAdapters.htmlparser2 });
parserStream = new parse5.ParserStream({ });
parserStream = new parse5.ParserStream();
github mvasilkov / readability2 / run_tests.js View on Github external
function comparePage(filename, done) {
    const a = path.basename(filename, '.html')
    const ref = fs.readFileSync(`${PAGES_DIR}/txt/${a}.txt`, { encoding: 'utf8' })

    const r = new Readability
    const parser = new Parse5.SAXParser
    const file = fs.createReadStream(filename + '.repair', { encoding: 'utf8' })

    connect(r, parser)

    parser.once('finish', function () {
        r.compute()
        const out = r.clean() + '\n'
        const n = levenshtein(out, ref)
        const k = (ref.length - n) / ref.length * 100

        console.log(`* ${a}.html k=${k.toFixed(2)}`)
        if (argv.v)
            printdiff(ref, out)

        results.files[a] = { k }
        done(null, filename)
github astoilkov / jsblocks / src / node / parseToVirtual.js View on Github external
function parseToVirtual(html) {
    var skip = 0;
    var root = VirtualElement('root');
    var parent = root;
    var parser = new parse5.SAXParser({
      decodeHtmlEntities: false
    });
    parser.on('doctype', function(name, publicId, systemId /*, [location] */) {
      root.children().push('');
    });

    parser.on('startTag', function(tagName, attrsArray, selfClosing /*, [location] */) {
      var attrs = {};
      var length = attrsArray.length;
      var index = -1;
      while (++index < length) {
        attrs[attrsArray[index].name] = attrsArray[index].value;
      }

      selfClosing = selfClosing || selfClosingTags[tagName];
github astoilkov / jsblocks / build / tasks / debug.js View on Github external
blocks.each(data.examples, function (example) {
            var root = {
              name: '',
              children: []
            };
            var current = root;
            var parents = [];
            var parser = new parse5.SAXParser();

            parser.on('startTag', function (tagName, attrs) {
              var node = {
                  name: attrs[0].value,
                  children: []
              };
              parents.push(current);
              current.children.push(node);
              current = node;
            });

            parser.on('endTag', function () {
              current = parents.pop();
            });

            parser.on('text', function (text) {
github aurelia / vscode-extension / src / server / FileParser / HTMLDocumentParser.ts View on Github external
return new Promise((resolve, reject) => {
      let stream = new Readable();
      stream.push(text);
      stream.push(null);

      let stack: Array = [];

      const parser = new SAXParser({ locationInfo: true });
      parser.on('startTag', (name, attrs, selfClosing, location) => {
        stack.push(new TagDefinition(
          true,
          name,
          location.startOffset,
          location.endOffset,
          location.line,
          selfClosing,
          attrs.map(i => new AttributeDefinition(i.name, i.value, location.attrs[i.name]))));
      });
      parser.on("endTag", (name, location) => {
        stack.push(new TagDefinition(
          false,
          name,
          location.startOffset,
          location.endOffset,
github LessWrong2 / Lesswrong2 / packages / server-render / server-register.js View on Github external
function rewrite(property) {
        const html = data[property];
        if (typeof html !== "string") {
          return;
        }

        const magic = new MagicString(html);
        const parser = new SAXParser({
          locationInfo: true
        });

        data[property] = parser;

        if (Object.keys(sink.htmlById).length) {
          const stream = createStream();

          let lastStart = magic.start;
          parser.on("startTag", (name, attrs, selfClosing, loc) => {
            attrs.some(attr => {
              if (attr.name === "id") {
                let html = sink.htmlById[attr.value];
                if (html) {
                  reallyMadeChanges = true;
                  const start = magic.slice(lastStart, loc.endOffset);
github meteor-vue / vue-meteor / packages / vue-component / plugin / tag-scanner.js View on Github external
const parseHtml = Meteor.wrapAsync(({
    sourceName,
    contents,
    tagNames
  }, cb) => {

  const tags = []

  const parser = new parse5.SAXParser({
    locationInfo: true,
  })

  let depth = 0
  let info

  function addTag() {
    const tagContents = contents.substring(info.start.index, info.end.index)

    const tag = {
      tagName: info.tag.name,
      attribs: info.tag.attrs,
      contents: tagContents,
      contentsStartIndex: info.start.index,
      tagStartIndex: info.tag.index,
      fileContents: contents,