npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

neutron-html5parser

v0.2.0

Published

Small Pure-JS HTML5 Parser

Downloads

104

Readme

Neutron HTML5 Parser

Here is a small pure-JavaScript HTML5 parser that can run on browsers as well as NodeJS with jsdom.

Credit goes to John Resig for his code written back in 2008 and Erik Arvidsson for his code written piror to that. This code has been updated to work with HTML 5 to fix several problems.

Use case

For parsing templates on both client and server side. This library may soon be used internally in htmlizer.

For only server-side use case, you may like to use htmlparser2 or high5. Note: DOCTYPE gets ignored by htmlparser2.

For only client-side use case, you can look into jQuery.parseHTML() or native DOMParser (IE10+).

Usage

Add htmlparser.js to head tag or require with nodejs.

DOM Builder

//Returns DocumentFragment.
var documentfragment = HTMLtoDOM("<p>Hello <b>World");

//If doctype is given then returns HTMLDocument
var doc = HTMLtoDOM("<!DOCTYPE html><htm><body>test</body></html>");

//on NodeJS
var factory = require('neutron-html5parser'),
    jsdom = require('jsdom'),
    HTMLtoDOM = factory(jsdom.jsdom('').parentWindow);

While this library doesn’t cover the full gamut of possible weirdness that HTML provides, it does handle a lot of the most obvious stuff. All of the following are accounted for:

Unclosed Tags:

HTMLtoDOM("<p><b>Hello") == '<p><b>Hello</b></p>'

Empty Elements:

HTMLtoDOM("<img src=test.jpg>") == '<img src="test.jpg">'

Block vs. Inline Elements:

HTMLtoDOM("<b>Hello <p>John") == '<b>Hello </b><p>John</p>'

Self-closing Elements:

HTMLtoDOM("<p>Hello<p>World") == '<p>Hello</p><p>World</p>'

Attributes Without Values:

HTMLtoDOM("<input disabled>") == '<input disabled="disabled">'

Following should be supported again in future: ~~A couple points are enforced by this method:~~

~~- There will always be a html, head, body, and title element.~~ ~~- There will only be one html, head, body, and title element (if the user specifies more, then will be moved to the appropriate locations and merged).~~ ~~link and base elements are forced into the head.~~

Advanced: SAX-style API

Handles tag, text, and comments with callbacks. For example, let’s say you wanted to implement a simple HTML to XML serialization scheme – you could do so using the following:

var results = "";

HTMLtoDOM.Parser("<p id=test>hello <i>world", {
  start: function( tag, attrs, unary ) {
    results += "<" + tag;

    for ( var i = 0; i < attrs.length; i++ )
      results += " " + attrs[i].name + '="' + attrs[i].escaped + '"';

    results += ">";
  },
  end: function( tag ) {
    results += "</" + tag + ">";
  },
  chars: function( text ) {
    results += text;
  },
  comment: function( text ) {
    results += "<!--" + text + "-->";
  }
});

results == '<p id="test">hello <i>world</i></p>"

Benchmarking

Benchmark done using htmlparser-benchmark.

htmlparser2         : 3.77256 ms/file ± 2.29339
high5               : 4.96011 ms/file ± 2.71494
neutron-html5parser : 5.41695 ms/file ± 3.26307
htmlparser2-dom     : 6.43134 ms/file ± 3.63845
libxmljs            : 7.37534 ms/file ± 9.60274
parse5              : 12.2405 ms/file ± 7.82065
html-parser         : 12.6268 ms/file ± 8.30923
hubbub              : 15.0666 ms/file ± 7.80456
htmlparser          : 28.7801 ms/file ± 178.500
gumbo-parser        : 29.8096 ms/file ± 15.5291
html5               : 196.083 ms/file ± 248.159
sax                 : <Error>