html to json & json to html
npm install html2json_2Based on this.
* Fixed htmlparser issue
* Separated style attr and parsed it
* Added option to remove non-rendering spaces in block nodes.
``sh`
$ npm install html2json
`javascript`
var html2json = require('html2json_2').html2json;
var json2html = require('html2json_2').json2html;
`javascript
json === html2json(document.body.innerHTML);
html === json2html(json);
console.assert(json === html);
`
`javascript`
json === html2json(document.body.innerHTML, {removeNonRenderingSpaces: true});
every json has node
members of node are
- rootelement
- text
- comment
-
root node is the root of JSON, every JSON must have only one root root, could have child.
element node represents html element, could have tag, attr, child, style.
text node represents text element, could have text.
comment node represents commment element, could have text.
html:
`html`sample text with
inline tagfoo
goo
json:
`javascript`
{
node: 'root',
child: [
{
node: 'element',
tag: 'div',
attr: { id: '1', class: 'foo' },
style: {margin: '0'},
child: [
{
node: 'element',
tag: 'h2',
child: [
{ node: 'text', text: 'sample text with ' },
{ node: 'element', tag: 'code', child: [{ node: 'text', text: 'inline tag' }] }
]
},
{
node: 'element',
tag: 'pre',
attr: { id: 'demo', class: ['foo', 'bar'] },
child: [{ node: 'text', text: 'foo' }]
},
{
node: 'element',
tag: 'pre',
attr: { id: 'output', class: 'goo' },
child: [{ node: 'text', text: 'goo' }]
},
{
node: 'element',
tag: 'input',
attr: { id: 'execute', type: 'button', value: 'execute' }
}
]
}
]
}
#### With spaces removed
html:
` test6 test6html`
Test 1
test 2
test 3test 4
test 5
test 7 test 8
test 9
Test 1
test 2
test 3test 4
test 5
test 7 test 8
test 9
json:
`javascript`
{
"node": "root",
"child": [
{
"node": "element",
"tag": "html",
"attr": {
"lang": "en"
},
"child": [
{
"node": "element",
"tag": "head",
"child": [
{
"node": "element",
"tag": "meta",
"attr": {
"charset": "UTF-8"
}
},
{
"node": "text",
"text": " "
},
{
"node": "element",
"tag": "title",
"child": [
{
"node": "text",
"text": "Space Tests"
}
]
}
]
},
{
"node": "element",
"tag": "body",
"child": [
{
"node": "element",
"tag": "div",
"child": [
{
"node": "element",
"tag": "p",
"child": [
{
"node": "element",
"tag": "font",
"child": [
{
"node": "text",
"text": "Test 1"
}
]
}
]
},
{
"node": "element",
"tag": "p",
"child": [
{
"node": "text",
"text": "test 2\n "
},
{
"node": "element",
"tag": "font",
"child": [
{
"node": "text",
"text": "test 3"
}
]
},
{
"node": "text",
"text": "test 4\n test 5"
}
]
},
{
"node": "element",
"tag": "p",
"child": [
{
"node": "text",
"text": "test6\n "
},
{
"node": "element",
"tag": "font",
"child": [
{
"node": "text",
"text": " test 7 "
}
]
},
{
"node": "element",
"tag": "font",
"child": [
{
"node": "text",
"text": "test 8"
}
]
},
{
"node": "text",
"text": "\n test 9"
}
]
},
{
"node": "element",
"tag": "p",
"child": [
{
"node": "element",
"tag": "font",
"style": {
"display": "inline-block"
},
"attr": {},
"child": [
{
"node": "text",
"text": "Test 1"
}
]
}
]
},
{
"node": "element",
"tag": "p",
"child": [
{
"node": "text",
"text": "test 2\n "
},
{
"node": "element",
"tag": "font",
"style": {
"display": "inline-block"
},
"attr": {},
"child": [
{
"node": "text",
"text": "test 3"
}
]
},
{
"node": "text",
"text": "test 4\n test 5"
}
]
},
{
"node": "element",
"tag": "p",
"child": [
{
"node": "text",
"text": "test6\n "
},
{
"node": "element",
"tag": "font",
"style": {
"display": "inline-block"
},
"attr": {},
"child": [
{
"node": "text",
"text": " test 7 "
}
]
},
{
"node": "element",
"tag": "font",
"style": {
"display": "inline-block"
},
"attr": {},
"child": [
{
"node": "text",
"text": "test 8"
}
]
},
{
"node": "text",
"text": "\n test 9"
}
]
}
]
}
]
}
]
}
]
}
Originally here
- 1.0.10
- Added option noSelfClosing to not self close tags in json2html.
- 1.0.2
- fix #16 with merge #17
- 1.0.1
- fix #14
before 1.0.0
- Basically inline tag is melted into text
- Input, textarea, image` tags are act like block tag
MIT