A simple HTML/ERB parser based on [htmlparser2](https://github.com/fb55/htmlparser2.git), which in turn is based on [node-htmlparser](https://github.com/tautologistics/node-htmlparser.git).
npm install @joecarstairs-fa/erbparserHTML/ERB parser, based on htmlparse2: the fast & forgiving HTML/XML parser.
Beware!!! This package is still in active development and is liable to change frequently.
npm install @joecarstairs-fa/erbparser
The remainder of this README, with the exception of the Parsing ERB section, is copy-and-pasted from htmlparser2.
A live demo of htmlparser2 is available here.
| Name | Description |
| ------------------------------------------------------------- | ------------------------------------------------------- |
| htmlparser2 | Fast & forgiving HTML/XML parser |
| domhandler | Handler for htmlparser2 that turns documents into a DOM |
| domutils | Utilities for working with domhandler's DOM |
| css-select | CSS selector engine, compatible with domhandler's DOM |
| cheerio | The jQuery API for domhandler's DOM |
| dom-serializer | Serializer for domhandler's DOM |
htmlparser2 itself provides a callback interface that allows consumption of documents with minimal allocations.
For a more ergonomic experience, read Getting a DOM below.
``javascriptattributes
const htmlparser2 = require("htmlparser2");
const parser = new htmlparser2.Parser({
onopentag(name, attributes) {
/*
* This fires when a new tag is opened.
*
* If you don't need an aggregated object,onopentagname
* have a look at the and onattribute events.
*/
if (name === "script" && attributes.type === "text/javascript") {
console.log("JS! Hooray!");
}
},
ontext(text) {
/*
* Fires whenever a section of text was processed.
*
* Note that this can fire at any point within text and you might
* have to stich together multiple pieces.
*/
console.log("-->", text);
},
onclosetag(tagname) {
/*
* Fires when a tag is closed.
*
* You can rely on this event only firing when you have received an
* equivalent opening tag before. Closing tags without corresponding
* opening tags will be ignored.
*/
if (tagname === "script") {
console.log("That's it?!");
}
},
});
parser.write(
"Xyz