UNPKG

6.12 kBMarkdownView Raw
1# html-parse-stringify
2
3This is an _experimental lightweight approach_ to enable quickly parsing HTML into an AST and stringify'ing it back to the original string.
4
5As 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.
6
7"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 ?!?!"
8
9Why yes, yes I have :)
10
11But 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?
12
13Yes.
14
15React.js essentially pioneered the approach. With React 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.
16
17As 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.
18
19Cool idea, right?!
20
21## So why this?
22
23Well, 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.
24
25If, 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.
26
27This 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).
28
29But 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).
30
31This lib does exactly that.
32
33It has two methods:
34
351. parse
362. stringify
37
38## `.parse(htmlString, options)`
39
40Takes 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.
41
42## `.stringify(AST)`
43
44Takes an AST and turns it back into a string of HTML.
45
46## What does the AST look like?
47
48See comments in the following example:
49
50```js
51var HTML = require('html-parse-stringify')
52
53// this html:
54var html = '<div class="oh"><p>hi</p></div>'
55
56// becomes this AST:
57var ast = HTML.parse(html)
58
59console.log(ast)
60/*
61{
62 // can be `tag`, `text` or `component`
63 type: 'tag',
64
65 // name of tag if relevant
66 name: 'div',
67
68 // parsed attribute object
69 attrs: {
70 class: 'oh'
71 },
72
73 // whether this is a self-closing tag
74 // such as <img/>
75 voidElement: false,
76
77 // an array of child nodes
78 // we see the same structure
79 // repeated in each of these
80 children: [
81 {
82 type: 'tag',
83 name: 'p',
84 attrs: {},
85 voidElement: false,
86 children: [
87 // this is a text node
88 // it also has a `type`
89 // but nothing other than
90 // a `content` containing
91 // its text.
92 {
93 type: 'text',
94 content: 'hi'
95 }
96 ]
97 }
98 ]
99}
100*/
101```
102
103## the AST node types
104
105### 1. tag
106
107properties:
108
109- `type` - will always be `tag` for this type of node
110- `name` - tag name, such as 'div'
111- `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"`
112- `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).
113- `children` - array of child nodes. Note that any continuous string of text is a text node child, see below.
114
115### 2. text
116
117properties:
118
119- `type` - will always be `text` for this type of node
120- `content` - text content of the node
121
122### 3. component
123
124If 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.
125
126This 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.
127
128properties:
129
130- `type` - will always be `component` for this type of node
131- `name` - tag name, such as 'div'
132- `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"`
133- `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).
134- `children` - it will still have a `children` array, but it will always be empty.
135
136## changelog
137
138- `2.0.0` updated to more modern dependencies/build system. Switched to prettier, etc. No big feature differences, just new build system/project structure. Added support for top level text nodes thanks to @jperl. Added support for comments thanks to @pconerly.
139- `1.0.0 - 1.0.3` no big changes, bug fixes and speed improvements.
140
141## credits
142
143If this sounds interesting you should probably follow [@HenrikJoreteg](https://twitter.com/henrikjoreteg) and [@Philip_Roberts](https://twitter.com/philip_roberts) on twitter to see how this all turns out.
144
145## license
146
147MIT