Skip to content

Commit 45e8391

Browse files
committed
initial commit
0 parents  commit 45e8391

File tree

12 files changed

+541
-0
lines changed

12 files changed

+541
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

.jshintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

.jshintrc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"curly": true,
3+
"latedef": true,
4+
"quotmark": true,
5+
"undef": true,
6+
"unused": true,
7+
"trailing": true,
8+
"predef": [
9+
"module",
10+
"require"
11+
]
12+
}

README.md

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
# html-parse-stringify
2+
3+
This is an *experimental lightweight approach* to enable quickly parsing HTML into an AST and stringify'ing it back to the original string.
4+
5+
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.
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+
9+
Why yes, yes I have :)
10+
11+
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?
12+
13+
Yes.
14+
15+
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.
16+
17+
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.
18+
19+
Cool idea, right?!
20+
21+
## So why this?
22+
23+
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.
24+
25+
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.
26+
27+
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).
28+
29+
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).
30+
31+
This lib does exactly that.
32+
33+
It has two methods:
34+
35+
1. parse
36+
2. stringify
37+
38+
## `.parse(htmlString, options)`
39+
40+
Takes a sting 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+
44+
Takes an AST and turns it back into a string of HTML.
45+
46+
## What does the AST look like?
47+
48+
See comments in the following example:
49+
50+
```js
51+
var HTML = require('html-parse-stringify')
52+
53+
// this html:
54+
var html = '<div class="oh"><p>hi</p></div>';
55+
56+
// becomes this AST:
57+
var ast = HTML.parse(html);
58+
59+
60+
console.log(ast);
61+
/*
62+
{
63+
// can be `tag`, `text` or `component`
64+
type: 'tag',
65+
66+
// name of tag if relevant
67+
name: 'div',
68+
69+
// parsed attribute object
70+
attrs: {
71+
class: 'oh'
72+
},
73+
74+
// whether this is a self-closing tag
75+
// such as <img/>
76+
selfClosing: false,
77+
78+
// an array of child nodes
79+
// we see the same structure
80+
// repeated in each of these
81+
children: [
82+
{
83+
type: 'tag',
84+
name: 'p',
85+
attrs: {},
86+
selfClosing: false,
87+
children: [
88+
// this is a text node
89+
// it also has a `type`
90+
// but nothing other than
91+
// a `content` containing
92+
// its text.
93+
{
94+
type: 'text',
95+
content: 'hi'
96+
}
97+
]
98+
}
99+
]
100+
}
101+
*/
102+
```
103+
104+
## the AST node types
105+
106+
### 1. tag
107+
108+
properties:
109+
110+
- `type` - will always be `tag` for this type of node
111+
- `name` - tag name, such as 'div'
112+
- `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"`
113+
- `selfClosing` - `true` or `false`. Whether this tag has a self-closing slash such as: `<img/>`, or `<input/>`
114+
- `children` - array of child nodes. Note that any continuous string of text is a text node child, see below.
115+
116+
### 2. text
117+
118+
properties:
119+
120+
- `type` - will always be `text` for this type of node
121+
- `content` - text content of the node
122+
123+
### 3. component
124+
125+
If 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.
126+
127+
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.
128+
129+
properties:
130+
131+
- `type` - will always be `component` for this type of node
132+
- `name` - tag name, such as 'div'
133+
- `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"`
134+
- `selfClosing` - `true` or `false`. Whether this tag has a self-closing slash such as: `<img/>`, or `<input/>`
135+
- `children` - it will still have a `children` array, but it will always be empty.
136+
137+
138+
## license
139+
140+
MIT

index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var parse = require('./lib/parse');
2+
var stringify = require('./lib/stringify');
3+
4+
5+
module.exports = {
6+
parse: parse,
7+
stringify: stringify
8+
};

lib/parse-tag.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
var attrRE = /([\w-]+)|['"]{1}([^'"]*)['"]{1}/g;
2+
3+
4+
module.exports = function (tag) {
5+
var i = 0;
6+
var key;
7+
var res = {
8+
selfClosing: tag.slice(-2, -1) === '/',
9+
attrs: {},
10+
type: 'tag',
11+
children: [],
12+
name: ''
13+
};
14+
15+
tag.replace(attrRE, function (match) {
16+
if (i % 2) {
17+
key = match;
18+
} else {
19+
if (i === 0) {
20+
res.name = match;
21+
} else {
22+
res.attrs[key] = match.replace(/['"]/g, '');
23+
}
24+
}
25+
i++;
26+
});
27+
28+
return res;
29+
};

lib/parse.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
var tagRE = /<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>/g;
2+
var parseTag = require('./parse-tag');
3+
4+
5+
module.exports = function parse(html, options) {
6+
options = options || {};
7+
options.components = options.components || {};
8+
var result;
9+
var current;
10+
var previous;
11+
var level = -1;
12+
var arr = [];
13+
var byTag = {};
14+
var inComponent = false;
15+
16+
html.replace(tagRE, function (tag, index) {
17+
if (inComponent && tag !== ('</' + current.name + '>')) {
18+
return;
19+
}
20+
var isOpen = tag.charAt(1) !== '/';
21+
var start = index + tag.length;
22+
var nextChar = html.charAt(start);
23+
var parent;
24+
25+
previous = current;
26+
27+
if (isOpen) {
28+
level++;
29+
30+
current = parseTag(tag);
31+
if (current.type === 'tag' && options.components[current.name]) {
32+
current.type = 'component';
33+
inComponent = true;
34+
}
35+
36+
if (nextChar !== '<') {
37+
current.children.push({
38+
type: 'text',
39+
content: html.slice(start, html.indexOf('<', start))
40+
});
41+
}
42+
43+
byTag[current.tagName] = current;
44+
45+
// this is our base if we don't already have one
46+
if (!previous) {
47+
result = current;
48+
}
49+
50+
parent = arr[level - 1];
51+
52+
if (parent) {
53+
parent.children.push(current);
54+
}
55+
56+
arr[level] = current;
57+
}
58+
59+
if (!isOpen || current.selfClosing) {
60+
level--;
61+
if (!inComponent && nextChar !== '<' && nextChar) {
62+
// trailing text node
63+
arr[level].children.push({
64+
type: 'text',
65+
content: html.slice(start, html.indexOf('<', start))
66+
});
67+
}
68+
}
69+
});
70+
71+
return result;
72+
};

lib/stringify.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function attrString(attrs) {
2+
var buff = [];
3+
for (var key in attrs) {
4+
buff.push(key + '="' + attrs[key] + '"');
5+
}
6+
if (!buff.length) {
7+
return '';
8+
}
9+
return ' ' + buff.join(' ');
10+
}
11+
12+
function stringify(buff, doc) {
13+
switch (doc.type) {
14+
case 'text':
15+
return buff + doc.content;
16+
case 'tag':
17+
buff += '<' + doc.name + (doc.attrs ? attrString(doc.attrs) : '') + (doc.selfClosing ? '/>' : '>');
18+
if (doc.selfClosing) {
19+
return buff;
20+
}
21+
return buff + doc.children.reduce(stringify, '') + '</' + doc.name + '>';
22+
}
23+
}
24+
25+
module.exports = function (doc) {
26+
return stringify('', doc);
27+
};

package.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "html-parse-stringify",
3+
"description": "Parses well-formed HTML (meaning all tags closed) into an AST and back. quickly.",
4+
"version": "0.0.0",
5+
"author": "Henrik Joreteg <[email protected]>",
6+
"bugs": {
7+
"url": "https://github.com/henrikjoreteg/html-parse-stringify/issues"
8+
},
9+
"devDependencies": {
10+
"jshint": "^2.5.10",
11+
"precommit-hook": "^1.0.7",
12+
"tape": "^3.0.3"
13+
},
14+
"homepage": "https://github.com/henrikjoreteg/html-parse-stringify",
15+
"keywords": [
16+
"html",
17+
"parse",
18+
"stringify",
19+
"ast"
20+
],
21+
"license": "MIT",
22+
"main": "index.js",
23+
"repository": {
24+
"type": "git",
25+
"url": "https://github.com/henrikjoreteg/html-parse-stringify"
26+
},
27+
"scripts": {
28+
"test": "node test/index.js | tap-spec"
29+
}
30+
}

test/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require('./parse-tag');
2+
require('./parse');

0 commit comments

Comments
 (0)