JSPM

  • Created
  • Published
  • Downloads 34246507
  • Score
    100M100P100Q222268F

Performance-optimized forgiving HTML/XML/RSS parser

Package Exports

  • htmlparser2
  • htmlparser2/lib
  • htmlparser2/lib/FeedHandler.js
  • htmlparser2/lib/Parser
  • htmlparser2/lib/Parser.js
  • htmlparser2/lib/WritableStream
  • htmlparser2/lib/WritableStream.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (htmlparser2) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

#htmlparser2 Build Status

A forgiving HTML/XML/RSS parser written in JS for NodeJS. The parser can handle streams (chunked data) and supports custom handlers for writing custom DOMs/output.

##Installing npm install htmlparser2

##Usage

var htmlparser = require("htmlparser2");
var parser = new htmlparser.Parser({
    onopentag: function(name, attribs){
        if(name === "script" && attribs["language"] === "javascript"){
            console.log("JS! Hooray!");
        }
    },
    ontext: function(text){
        console.log("-->", text);
    },
    onclosetag: function(tagname){
        if(tagname === "script"){
            console.log("That's it?!");
        }
    }
});
parser.write("Xyz <script language= javascript>var foo = '<<bar>>';< /  script>");
parser.done();

Output (simplified):

--> Xyz 
JS! Hooray!
--> var foo = '<<bar>>';
That's it?!

Read more about the parser in the wiki.

##Get a DOM The DomHandler (known as DefaultHandler in the original htmlparser module) produces a DOM (document object model) that can be manipulated using the DomUtils helper.

Read more about the DomHandler in the wiki.

##Parsing RSS/RDF/Atom Feeds

new htmlparser.FeedHandler(function(<error> error, <object> feed){
    ...
});

##Performance Using a slightly modified version of node-expats bench.js, I received the following results (on a MacBook (late 2010)):

The test may be found in tests/bench.js.

##How is this different from node-htmlparser? This is a fork of the project above. The main difference is that this is intended to be used only with node (it runs on other platforms using browserify). Besides, the code is much better structured, has less duplications and is remarkably faster than the original.

The parser now provides a callback interface close to sax.js (originally targeted at readabilitySAX). I also fixed a couple of bugs & included some pull requests for the original project (eg. RDF feed support).

The support for location data and verbose output was removed a couple of versions ago. It's still available in the verbose branch.

The DefaultHandler and the RssHandler were renamed to clarify their purpose (to DomHandler and FeedHandler). The old names are still available when requiring htmlparser2, so your code should work as expected.