# html-parse-stringify2 ## Quick note This is a fork of [html-parse-stringify](https://github.com/HenrikJoreteg/html-parse-stringify) that I made because I wanted to get some important fixes into the repo and available as an NPM package and I'm not sure whether the old repo is still being maintained. Hence this. This could be temporary - I'll gladly drop this if activity picks back up on the original repo. But for now we've got a new npm package `html-parse-stringify2`. Install with: ``` npm install --save html-parse-stringify2 ``` Use as documented below... ### OK, on to the original README... This is an *experimental lightweight approach* to enable quickly parsing HTML into an AST and stringify'ing it back to the original string. As it turns out, if you can make a the simplifying assumptions about HTML that all tags must be closed or self-closing. Which is OK for *this* particular application. You can write a super light/fast parser in JS with regex. "Why on earth would you do this?! Haven't you read: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags ?!?!" Why yes, yes I have :) But the truth is. If you *could* do this in a whopping grand total of ~600 bytes (min+gzip) as this repo shows. It potentially enables DOM diffing based on a HTML strings to be super light and fast in a browser. What is that you say? DOM-diffing? Yes. React.js essentially pioneered the approach. With Reach you render to a "virtual DOM" whenever you want to, and the virtual DOM can then diff against the real DOM (or the last virtual DOM) and then turn that diff into whatever transformations are necessary to get the *real* DOM to match what you rendered as efficiently as possible. As a result, when you're building a single page app, you don't have to worry so much about bindings. Instead, you simple re-render to the virtual DOM whenever you know something's changed. All of a sudden being able to have `change` events for individual properties becomes less important, instead you can just reference those values in your template whenever you think something changed. Cool idea, right?! ## So why this? Well, there are other things React expects me to do if I use it that I don't like. Such as the custom templating and syntax you have to use. If, hypothetically, you could instead diff an HTML string (generated by *whatever* templating language of your choice) against the DOM, then you'd get the same benefit, sans React's impositions. This may all turn out to be a bad idea altogether, but initial results seem promising when paired with [virtual-dom](https://github.com/Matt-Esch/virtual-dom). But you can't just diff HTML strings, as simple strings, very easily, in order to diff two HTML node trees you have to first turn that string into a tree structure of some sort. Typically, the thing you generate from parsing something like this is called an AST (abstract syntax tree). This lib does exactly that. It has two methods: 1. parse 2. stringify ## `.parse(htmlString, options)` Takes a string of HTML and turns it into an AST, the only option you can currently pass is an object of registered `components` whose children will be ignored when generating the AST. ## `.stringify(AST)` Takes an AST and turns it back into a string of HTML. ## What does the AST look like? See comments in the following example: ```js var HTML = require('html-parse-stringify2') // this html: var html = '
hi