{"_id":"html-parse-stringify2","_rev":"9-ae5fc54787f553de6c6f9c9aa5e16941","name":"html-parse-stringify2","dist-tags":{"latest":"2.0.2"},"versions":{"1.1.0":{"name":"html-parse-stringify2","version":"1.1.0","keywords":["html","parse","stringify","ast"],"author":{"name":"Ray Di Ciaccio","email":"ray.diciaccio@gmail.com"},"license":"MIT","_id":"html-parse-stringify2@1.1.0","maintainers":[{"name":"rayd","email":"ray@appcues.com"}],"homepage":"https://github.com/rayd/html-parse-stringify2","bugs":{"url":"https://github.com/rayd/html-parse-stringify2/issues"},"dist":{"shasum":"10a84d787b0b2c2aa1caebafcab104037bf04231","tarball":"https://registry.npmjs.org/html-parse-stringify2/-/html-parse-stringify2-1.1.0.tgz","integrity":"sha512-NYadrQcisqmHUFCKdegwpas/kpqxB1gbbMmb0p+uUQMPMZ3jnvRp0kz5LohSeXiwM2PsOpf0f23YPnFBhj8n0Q==","signatures":[{"sig":"MEUCIQD36xdRefvHBY9lbye+fyv+LPEEHU2WHofPS9N75wmyHgIgFqYPktviZdDsGkB26ZA0nhYEK6vR+6zSGUieaBFW6Tk=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}]},"main":"index.js","_from":".","_shasum":"10a84d787b0b2c2aa1caebafcab104037bf04231","gitHead":"1a57b363de58872ffb4a1d0792d36bb7c1e09738","scripts":{"test":"zuul --phantom -- test/index.js","start":"run-browser test/*","test-ci":"zuul -- test/index.js"},"_npmUser":{"name":"rayd","email":"ray@appcues.com"},"repository":{"url":"git+https://github.com/rayd/html-parse-stringify2.git","type":"git"},"_npmVersion":"3.3.12","description":"Parses well-formed HTML (meaning all tags closed) into an AST and back. quickly.","_nodeVersion":"5.3.0","dependencies":{"void-elements":"^1.0.0"},"devDependencies":{"tape":"^3.0.3","zuul":"^1.16.5","jshint":"^2.5.10","tap-spec":"^2.1.2","phantomjs":"^1.9.13","run-browser":"^2.0.1","precommit-hook":"^1.0.7"},"_npmOperationalInternal":{"tmp":"tmp/html-parse-stringify2-1.1.0.tgz_1463078662643_0.11425643833354115","host":"packages-12-west.internal.npmjs.com"},"directories":{}},"1.2.0":{"name":"html-parse-stringify2","version":"1.2.0","keywords":["html","parse","stringify","ast"],"author":{"name":"Ray Di Ciaccio","email":"ray.diciaccio@gmail.com"},"license":"MIT","_id":"html-parse-stringify2@1.2.0","maintainers":[{"name":"rayd","email":"ray@appcues.com"}],"homepage":"https://github.com/rayd/html-parse-stringify2","bugs":{"url":"https://github.com/rayd/html-parse-stringify2/issues"},"dist":{"shasum":"eda8f18328085082c32951fce2115c8c18eb4f50","tarball":"https://registry.npmjs.org/html-parse-stringify2/-/html-parse-stringify2-1.2.0.tgz","integrity":"sha512-JCOHsBIwxIDA+la8rnZdsF53l1/xxv1D3NAasqAg2u9HqVID+jZpXwXPn5d1Cw8B/SAAa9TSJhmzth1oX6x92g==","signatures":[{"sig":"MEQCIGwyTmacytqx4ECQ/b+9R8MRlJ6tnKOwAmpDkB/nV3Q9AiAjRsEuhAEFfN1aFc7ZFw8Top9UOmVPme+oq3YTEIBxgQ==","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}]},"main":"index.js","_from":".","_shasum":"eda8f18328085082c32951fce2115c8c18eb4f50","gitHead":"e01115003ba2f774dc1e9ff1f777e01bccaa6258","scripts":{"test":"zuul --phantom -- test/index.js","start":"run-browser test/*","test-ci":"zuul -- test/index.js"},"_npmUser":{"name":"rayd","email":"ray@appcues.com"},"repository":{"url":"git+https://github.com/rayd/html-parse-stringify2.git","type":"git"},"_npmVersion":"2.11.3","description":"Parses well-formed HTML (meaning all tags closed) into an AST and back. quickly.","_nodeVersion":"0.12.7","dependencies":{"void-elements":"^1.0.0"},"devDependencies":{"tape":"^3.0.3","zuul":"^1.16.5","jshint":"^2.5.10","tap-spec":"^2.1.2","phantomjs":"^1.9.13","run-browser":"^2.0.1","precommit-hook":"^1.0.7"},"_npmOperationalInternal":{"tmp":"tmp/html-parse-stringify2-1.2.0.tgz_1467061838435_0.164528070949018","host":"packages-16-east.internal.npmjs.com"},"directories":{}},"1.2.1":{"name":"html-parse-stringify2","version":"1.2.1","keywords":["html","parse","stringify","ast"],"author":{"name":"Ray Di Ciaccio","email":"ray.diciaccio@gmail.com"},"license":"MIT","_id":"html-parse-stringify2@1.2.1","maintainers":[{"name":"rayd","email":"ray@appcues.com"}],"homepage":"https://github.com/rayd/html-parse-stringify2","bugs":{"url":"https://github.com/rayd/html-parse-stringify2/issues"},"dist":{"shasum":"3d97afe806eac2b4e6963d52d190cf2638cd9c56","tarball":"https://registry.npmjs.org/html-parse-stringify2/-/html-parse-stringify2-1.2.1.tgz","integrity":"sha512-RSFzplwJ1+FbeM2v84Klqfmz9yC5omgQf5Rblv0BeUF6c8mN4s93Uz0c9C3WebRJ7+wdwRnhrGYjg9mCqYwz0A==","signatures":[{"sig":"MEUCIFgpndkaGelbxQznoal0xvDDgm0blDWNhtE34xpHyvYmAiEA1DA7cbLMJT2hT1Co/R9JdZrnV/o5kIaO/Sk6PHEOZ0Y=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}]},"main":"index.js","_from":".","_shasum":"3d97afe806eac2b4e6963d52d190cf2638cd9c56","gitHead":"79ddeb87fb88e190987b4edd943835506dabcaa9","scripts":{"test":"zuul --phantom -- test/index.js","start":"run-browser test/*","test-ci":"zuul -- test/index.js"},"_npmUser":{"name":"rayd","email":"ray@appcues.com"},"repository":{"url":"git+https://github.com/rayd/html-parse-stringify2.git","type":"git"},"_npmVersion":"2.11.3","description":"Parses well-formed HTML (meaning all tags closed) into an AST and back. quickly.","_nodeVersion":"0.12.7","dependencies":{"void-elements":"^1.0.0"},"devDependencies":{"tape":"^3.0.3","zuul":"^1.16.5","jshint":"^2.5.10","tap-spec":"^2.1.2","phantomjs":"^1.9.13","run-browser":"^2.0.1","precommit-hook":"^1.0.7"},"_npmOperationalInternal":{"tmp":"tmp/html-parse-stringify2-1.2.1.tgz_1467094858751_0.023786191130056977","host":"packages-16-east.internal.npmjs.com"},"directories":{}},"2.0.0":{"name":"html-parse-stringify2","version":"2.0.0","keywords":["html","parse","stringify","ast"],"author":{"name":"Ray Di Ciaccio","email":"ray.diciaccio@gmail.com"},"license":"MIT","_id":"html-parse-stringify2@2.0.0","maintainers":[{"name":"rayd","email":"ray@appcues.com"}],"homepage":"https://github.com/rayd/html-parse-stringify2","bugs":{"url":"https://github.com/rayd/html-parse-stringify2/issues"},"dist":{"shasum":"064ef0576a82b471bbf90b4729390c7af32e81d0","tarball":"https://registry.npmjs.org/html-parse-stringify2/-/html-parse-stringify2-2.0.0.tgz","integrity":"sha512-5HqnHMQEugSxGFk7g5buiMM7ZpsfECR8j7H45VmzlG07j8qMw55o3evyVanpYAO888GLYOhcRCXvnchNJES1Fw==","signatures":[{"sig":"MEUCIQDqN9L8v4U6LWZQhglMDaG86S7HfRev/4bLulWgiZK/QgIgBa1AFFYoMXHq8EjvHq6xqPU8rKoqIBzNW3ESS0NrIZI=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}]},"main":"index.js","_from":".","_shasum":"064ef0576a82b471bbf90b4729390c7af32e81d0","gitHead":"0460814f20ca1591e749d52f7dfff435249033c8","scripts":{"test":"zuul --phantom -- test/index.js","start":"run-browser test/*","test-ci":"zuul -- test/index.js"},"_npmUser":{"name":"rayd","email":"ray@appcues.com"},"repository":{"url":"git+https://github.com/rayd/html-parse-stringify2.git","type":"git"},"_npmVersion":"3.10.10","description":"Parses well-formed HTML (meaning all tags closed) into an AST and back. quickly.","_nodeVersion":"6.9.4","dependencies":{"void-elements":"^2.0.1"},"devDependencies":{"tape":"^3.0.3","zuul":"^1.16.5","jshint":"^2.5.10","tap-spec":"^2.1.2","phantomjs":"^1.9.13","run-browser":"^2.0.1","precommit-hook":"^1.0.7"},"_npmOperationalInternal":{"tmp":"tmp/html-parse-stringify2-2.0.0.tgz_1500927972316_0.8569023807067424","host":"s3://npm-registry-packages"},"directories":{}},"2.0.1":{"name":"html-parse-stringify2","version":"2.0.1","keywords":["html","parse","stringify","ast"],"author":{"name":"Ray Di Ciaccio","email":"ray.diciaccio@gmail.com"},"license":"MIT","_id":"html-parse-stringify2@2.0.1","maintainers":[{"name":"rayd","email":"ray@appcues.com"}],"homepage":"https://github.com/rayd/html-parse-stringify2","bugs":{"url":"https://github.com/rayd/html-parse-stringify2/issues"},"dist":{"shasum":"dc5670b7292ca158b7bc916c9a6735ac8872834a","tarball":"https://registry.npmjs.org/html-parse-stringify2/-/html-parse-stringify2-2.0.1.tgz","integrity":"sha512-wMKQ3aJ/dwXzDHPpA7XgsRXXCkEhHkAF6Ioh7D51lgZO7Qy0LmcFddC9TI/qNQJvSM1KL8KbcR3FtuybsrzFlQ==","signatures":[{"sig":"MEUCIQCwXN/d4XE/66GxEPWMECEBs1FLZSXCHIJWiAxOgc50wgIgJZq7K5O101AIbn5uFQps0tEgYsOEY6f7mSFmedQfIcE=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}]},"main":"index.js","_from":".","_shasum":"dc5670b7292ca158b7bc916c9a6735ac8872834a","gitHead":"dbf026f9010a14167e5a9e8589464f660233e446","scripts":{"test":"zuul --phantom -- test/index.js","start":"run-browser test/*","test-ci":"zuul -- test/index.js"},"_npmUser":{"name":"rayd","email":"ray@appcues.com"},"repository":{"url":"git+https://github.com/rayd/html-parse-stringify2.git","type":"git"},"_npmVersion":"3.10.10","description":"Parses well-formed HTML (meaning all tags closed) into an AST and back. quickly.","_nodeVersion":"6.9.4","dependencies":{"void-elements":"^2.0.1"},"devDependencies":{"tape":"^3.0.3","zuul":"^1.16.5","jshint":"^2.5.10","tap-spec":"^2.1.2","phantomjs":"^1.9.13","run-browser":"^2.0.1","precommit-hook":"^1.0.7"},"_npmOperationalInternal":{"tmp":"tmp/html-parse-stringify2-2.0.1.tgz_1502379709482_0.9873573931399733","host":"s3://npm-registry-packages"},"directories":{}},"2.0.2":{"name":"html-parse-stringify2","description":"Parses well-formed HTML (meaning all tags closed) into an AST and back. quickly.","version":"2.0.2","author":{"name":"Ray Di Ciaccio","email":"ray.diciaccio@gmail.com"},"bugs":{"url":"https://github.com/rayd/html-parse-stringify2/issues"},"dependencies":{"void-elements":"^2.0.1"},"devDependencies":{"jshint":"^2.5.10","phantomjs":"^1.9.13","precommit-hook":"^1.0.7","run-browser":"^2.0.1","tap-spec":"^2.1.2","tape":"^3.0.3","zuul":"^1.16.5"},"homepage":"https://github.com/rayd/html-parse-stringify2","keywords":["html","parse","stringify","ast"],"license":"MIT","main":"index.js","repository":{"type":"git","url":"git+https://github.com/rayd/html-parse-stringify2.git"},"scripts":{"start":"run-browser test/*","test":"zuul --phantom -- test/index.js","test-ci":"zuul -- test/index.js"},"gitHead":"7c48dbd3b0023504ac21a26ff3d00e04f3823d71","_id":"html-parse-stringify2@2.0.2","_nodeVersion":"12.13.0","_npmVersion":"6.12.0","dist":{"integrity":"sha512-joxbFw4zKkHQpNZW/fs8SenVR/+vKFd+UMqwy77XODhoo7z6BKfbxIgmmlq4GDR3wev6kwjtXgf78calPYadWg==","shasum":"3179aa42203ee916556dfe2161055f3b8b0bfd0d","tarball":"https://registry.npmjs.org/html-parse-stringify2/-/html-parse-stringify2-2.0.2.tgz","fileCount":14,"unpackedSize":32061,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEQCIGVdfif+lZaL0YiW+GHiEIVmVZQMZ+vUB9o9eEIIMdlgAiB4LmZAsNogilvquar4xdljFdFO15tmPcQgkr9Qu0/bew=="}]},"_npmUser":{"name":"rayd","email":"ray@appcues.com"},"directories":{},"maintainers":[{"name":"rayd","email":"ray@appcues.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/html-parse-stringify2_2.0.2_1763737107641_0.11115353037662823"},"_hasShrinkwrap":false}},"time":{"created":"2016-05-12T18:44:25.218Z","modified":"2025-11-21T14:58:28.025Z","1.1.0":"2016-05-12T18:44:25.218Z","1.2.0":"2016-06-27T21:10:39.360Z","1.2.1":"2016-06-28T06:20:59.721Z","2.0.0":"2017-07-24T20:26:13.376Z","2.0.1":"2017-08-10T15:41:50.582Z","2.0.2":"2025-11-21T14:58:27.836Z"},"bugs":{"url":"https://github.com/rayd/html-parse-stringify2/issues"},"author":{"name":"Ray Di Ciaccio","email":"ray.diciaccio@gmail.com"},"license":"MIT","homepage":"https://github.com/rayd/html-parse-stringify2","keywords":["html","parse","stringify","ast"],"repository":{"type":"git","url":"git+https://github.com/rayd/html-parse-stringify2.git"},"description":"Parses well-formed HTML (meaning all tags closed) into an AST and back. quickly.","maintainers":[{"name":"rayd","email":"ray@appcues.com"}],"readme":"# html-parse-stringify2\n\n## Quick note\n\nThis 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:\n\n```\nnpm install --save html-parse-stringify2\n```\n\nUse as documented below...\n\n### OK, on to the original README...\n\nThis is an *experimental lightweight approach* to enable quickly parsing HTML into an AST and stringify'ing it back to the original string.\n\nAs 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.\n\n\"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 ?!?!\"\n\nWhy yes, yes I have :)\n\nBut 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?\n\nYes.\n\nReact.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.\n\nAs 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.\n\nCool idea, right?!\n\n## So why this?\n\nWell, 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.\n\nIf, 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.\n\nThis 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).\n\nBut 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).\n\nThis lib does exactly that.\n\nIt has two methods:\n\n1. parse\n2. stringify\n\n## `.parse(htmlString, options)`\n\nTakes 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.\n\n## `.stringify(AST)`\n\nTakes an AST and turns it back into a string of HTML.\n\n## What does the AST look like?\n\nSee comments in the following example:\n\n```js\nvar HTML = require('html-parse-stringify2')\n\n// this html:\nvar html = '<div class=\"oh\"><p>hi</p></div>';\n\n// becomes this AST:\nvar ast = HTML.parse(html);\n\n\nconsole.log(ast);\n/*\n{\n    // can be `tag`, `text` or `component`\n    type: 'tag',\n\n    // name of tag if relevant\n    name: 'div',\n\n    // parsed attribute object\n    attrs: {\n        class: 'oh'\n    },\n\n    // whether this is a self-closing tag\n    // such as <img/>\n    voidElement: false,\n\n    // an array of child nodes\n    // we see the same structure\n    // repeated in each of these\n    children: [\n        {\n            type: 'tag',\n            name: 'p',\n            attrs: {},\n            voidElement: false,\n            children: [\n                // this is a text node\n                // it also has a `type`\n                // but nothing other than\n                // a `content` containing\n                // its text.\n                {\n                    type: 'text',\n                    content: 'hi'\n                }\n            ]\n        }\n    ]\n}\n*/\n```\n\n## the AST node types\n\n### 1. tag\n\nproperties:\n\n- `type` - will always be `tag` for this type of node\n- `name` - tag name, such as 'div'\n- `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\"`\n- `voidElement` - `true` or `false`. Whether this tag is a known void element as defined by [spec](http://www.w3.org/html/wg/drafts/html/master/syntax.html#void-elements).\n- `children` - array of child nodes. Note that any continuous string of text is a text node child, see below.\n\n### 2. text\n\nproperties:\n\n- `type` - will always be `text` for this type of node\n- `content` - text content of the node\n\n### 3. component\n\nIf 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.\n\nThis 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.\n\nproperties:\n\n- `type` - will always be `component` for this type of node\n- `name` - tag name, such as 'div'\n- `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\"`\n- `voidElement` - `true` or `false`. Whether this tag is a known void element as defined by [spec](http://www.w3.org/html/wg/drafts/html/master/syntax.html#void-elements).\n- `children` - it will still have a `children` array, but it will always be empty.\n\n\n## credits\n\nIf this sounds interesting you should probably follow [@HenrikJoreteg](http://twitter.com/henrikjoreteg) and [@Philip_Roberts](twitter.com/philip_roberts) on twitter to see how this all turns out.\n\n## license\n\nMIT\n","readmeFilename":"README.md"}