Gathering detailed insights and metrics for html-parse-stringify2
Gathering detailed insights and metrics for html-parse-stringify2
Gathering detailed insights and metrics for html-parse-stringify2
Gathering detailed insights and metrics for html-parse-stringify2
Parses well-formed HTML (meaning all tags closed) into an AST and back. quickly.
npm install html-parse-stringify2
Typescript
Module System
Node Version
NPM Version
JavaScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
21 Stars
54 Commits
10 Forks
2 Watchers
4 Branches
1 Contributors
Updated on Sep 12, 2022
Latest Version
2.0.1
Package Id
html-parse-stringify2@2.0.1
Size
9.00 kB
NPM Version
3.10.10
Node Version
6.9.4
Published on
Aug 10, 2017
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
1
7
This is a fork of 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...
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?!
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.
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:
.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.
See comments in the following example:
1var HTML = require('html-parse-stringify2') 2 3// this html: 4var html = '<div class="oh"><p>hi</p></div>'; 5 6// becomes this AST: 7var ast = HTML.parse(html); 8 9 10console.log(ast); 11/* 12{ 13 // can be `tag`, `text` or `component` 14 type: 'tag', 15 16 // name of tag if relevant 17 name: 'div', 18 19 // parsed attribute object 20 attrs: { 21 class: 'oh' 22 }, 23 24 // whether this is a self-closing tag 25 // such as <img/> 26 voidElement: false, 27 28 // an array of child nodes 29 // we see the same structure 30 // repeated in each of these 31 children: [ 32 { 33 type: 'tag', 34 name: 'p', 35 attrs: {}, 36 voidElement: false, 37 children: [ 38 // this is a text node 39 // it also has a `type` 40 // but nothing other than 41 // a `content` containing 42 // its text. 43 { 44 type: 'text', 45 content: 'hi' 46 } 47 ] 48 } 49 ] 50} 51*/
properties:
type
- will always be tag
for this type of nodename
- tag name, such as 'div'attrs
- an object of key/value pairs. If an attribute has multiple space-separated items such as classes, they'll still be in a single string, for example: class: "class1 class2"
voidElement
- true
or false
. Whether this tag is a known void element as defined by spec.children
- array of child nodes. Note that any continuous string of text is a text node child, see below.properties:
type
- will always be text
for this type of nodecontent
- text content of the nodeIf you pass an object of components
as part of the options
object passed as the second argument to .parse()
then the AST won't keep parsing that branch of the DOM tree when it one of those registered components.
This is so that it's possible to ignore sections of the tree that you may want to handle by another "subview" in your application that handles it's own DOM diffing.
properties:
type
- will always be component
for this type of nodename
- tag name, such as 'div'attrs
- an object of key/value pairs. If an attribute has multiple space-separated items such as classes, they'll still be in a single string, for example: class: "class1 class2"
voidElement
- true
or false
. Whether this tag is a known void element as defined by spec.children
- it will still have a children
array, but it will always be empty.If this sounds interesting you should probably follow @HenrikJoreteg and @Philip_Roberts on twitter to see how this all turns out.
MIT
5.3/10
Summary
html-parse-stringify and html-parse-stringify2 vulnerable to Regular expression denial of service (ReDoS)
Affected Versions
<= 2.0.1
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
Found 4/25 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
license file not detected
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More