Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

html2json

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

html2json - npm Package Compare versions

Comparing version 0.0.4 to 0.1.0

.eslintrc.js

76

bench/bench.js

@@ -1,5 +0,15 @@

if (!console || !console.time) {
console.time = function() {};
console.timeEnd = function() {};
if (typeof window === 'undefined') {
json2html = require('../index').json2html;
html2json = require('../index').html2json;
}
if (typeof console.time === 'undefined') {
console.time = function(tag) {
this[tag] = Date.now();
};
console.timeEnd = function(tag) {
console.log(tag, Date.now() - this[tag], 'ms');
};
}
var json = {

@@ -9,34 +19,37 @@ tag: 'div',

id: '1',
class: ['foo']
class: ['foo'],
},
child: [{
tag: 'h2',
text: 'sample text with <code>inline tag</code>'
},{
tag: 'pre',
attr: {
id: 'demo',
class: ['foo', 'bar']
child: [
{
tag: 'h2',
text: 'sample text with <code>inline tag</code>',
},
{
tag: 'pre',
attr: {
id: 'demo',
class: ['foo', 'bar'],
}
},
{
tag: 'pre',
attr: {
id: 'output',
class: ['goo'],
}
},
{
tag: 'input',
attr: {
id: 'execute',
type: 'button',
value: 'execute',
}
}
},{
tag: 'pre',
attr: {
id: 'output',
class: ['goo']
}
},{
tag: 'input',
attr: {
id: 'execute',
type: 'button',
value: 'execute'
}
}]
]
};
console.time('json2html');
var json2html_start = (new Date()).getTime();
for (var i = 0; i < 1000; i++) {
json2html(json);
}
var json2html_end = (new Date()).getTime();
console.timeEnd('json2html');

@@ -53,12 +66,5 @@

console.time('html2json');
var html2json_start = (new Date()).getTime();
for (var j = 0; j < 1000; j++) {
html2json(html);
}
var html2json_end = (new Date()).getTime();
console.timeEnd('html2json');
$(function() {
$('#json2html').text(json2html_end - json2html_start);
$('#html2json').text(html2json_end - html2json_start);
});
{
"name": "html2json",
"version": "0.0.4",
"version": "0.1.0",
"description": "html to json & json to html",

@@ -10,3 +10,4 @@ "main": "index.js",

"scripts": {
"test": "mocha"
"test": "mocha",
"lint": "eslint -c .eslintrc.js src test bench"
},

@@ -28,4 +29,5 @@ "repository": {

"devDependencies": {
"eslint": "^2.6.0",
"mocha": "^2.4.5"
}
}

@@ -1,177 +0,217 @@

if (typeof HTMLParser === 'undefined') {
require('../lib/Pure-JavaScript-HTML5-Parser/htmlparser.js');
}
(function(global) {
this.parseHtml = function parseHtml(html) {
var results = '';
HTMLParser(html, {
start: function(tag, attrs, unary) {
results += '<' + tag;
for (var i = 0; i < attrs.length; i++) {
results += ' ' + attrs[i].name + '="' + attrs[i].escaped + '"';
}
results += (unary ? '/' : '') + '>';
},
end: function(tag) {
results += '</' + tag + '>';
},
chars: function(text) {
results += text;
},
comment: function(text) {
results += '<!--' + text + '-->';
}
});
return results;
}
if (typeof window === 'undefined') {
require('../lib/Pure-JavaScript-HTML5-Parser/htmlparser.js');
}
function makeMap(str) {
var obj = {}, items = str.split(',');
for (var i = 0; i < items.length; i++) {
obj[items[i]] = true;
function q(v) {
return '"' + v + '"';
}
return obj;
}
this.html2json = function html2json(html) {
// Inline Elements - HTML 4.01
var inline = makeMap('a,abbr,acronym,applet,b,basefont,bdo,big,br,button,cite,code,del,dfn,em,font,i,iframe,img,input,ins,kbd,label,map,object,q,s,samp,script,select,small,span,strike,strong,sub,sup,textarea,tt,u,var');
// but I want to handle some tag like block tag
inline.textarea = false;
inline.input = false;
inline.img = false;
global.parseHtml = function parseHtml(html) {
var results = '';
HTMLParser(html, {
start: function(tag, attrs, unary) {
results += '<' + tag;
html = html.replace(/<!DOCTYPE[\s\S]+?>/, '');
var bufArray = [];
var results = {};
var inlineBuf = [];
bufArray.last = function() {
return this[ this.length - 1];
};
HTMLParser(html, {
start: function(tag, attrs, unary) {
if (inline[tag]) {
// inline tag is melted into text
// because syntacs higlight became dirty
// if support it.
// 'hoge <inline>tag</inline> fuga'
var attributes = '';
for (var i = 0; i < attrs.length; i++) {
attributes += ' ' + attrs[i].name + '="' + attrs[i].value + '"';
if (attrs.length > 0) {
attributes = ' ' + attrs.map(function(attr) {
return attr.name + '=' + q(attr.escaped);
}).join(' ');
}
inlineBuf.push('<' + tag + attributes + '>');
} else {
var buf = {}; // buffer for single tag
buf.tag = tag;
if (attrs.length !== 0) {
var attr = {};
for (var i = 0; i < attrs.length; i++) {
var attr_name = attrs[i].name;
var attr_value = attrs[i].value;
if (attr_name === 'class') {
attr_value = attr_value.split(' ');
results += attributes;
results += (unary ? '/' : '') + '>';
},
end: function(tag) {
results += '</' + tag + '>';
},
chars: function(text) {
results += text;
},
comment: function(text) {
results += '<!--' + text + '-->';
}
});
return results;
};
global.html2json = function html2json(html) {
// Inline Elements - HTML 4.01
var inline = [
'a',
'abbr',
'acronym',
'applet',
'b',
'basefont',
'bdo',
'big',
'br',
'button',
'cite',
'code',
'del',
'dfn',
'em',
'font',
'i',
'iframe',
'ins',
'kbd',
'label',
'map',
'object',
'q',
's',
'samp',
'script',
'select',
'small',
'span',
'strike',
'strong',
'sub',
'sup',
'tt',
'u',
'var'
// wanna handle tag below as block tag
// 'img',
// 'input',
// 'textarea',
];
html = html.replace(/<!DOCTYPE[\s\S]+?>/, '');
var bufArray = [];
var results = {};
var inlineBuf = [];
bufArray.last = function() {
return this[ this.length - 1];
};
HTMLParser(html, {
start: function(tag, attrs, unary) {
if (inline.indexOf(tag) > -1) {
// inline tag is melted into text
// 'foo <span>bar</span> buz'
var attributes = '';
if (attrs.length > 0) {
attributes = attrs.forEach(function (attr) {
return attr.name + '=' + q(attr.value);
});
}
inlineBuf.push('<' + tag + attributes + '>');
} else {
var buf = {}; // buffer for single tag
buf.tag = tag;
if (attrs.length !== 0) {
var attributes = {};
attrs.forEach(function(attr) {
var attr_name = attr.name;
var attr_value = attr.value;
if (attr_name === 'class') {
attr_value = attr_value.split(' ');
}
attributes[attr_name] = attr_value;
});
buf['attr'] = attributes;
}
if (unary) {
// if this tag don't has end tag
// like <img src="hoge.png"/>
// add last parents
var last = bufArray.last();
if (!(Array.isArray(last.child))) {
last.child = [];
}
attr[attr_name] = attr_value;
last.child.push(buf);
} else {
bufArray.push(buf);
}
buf['attr'] = attr;
}
if (unary) {
// if this tag don't has end tag
// like <img src="hoge.png"/>
// add last parents
},
end: function(tag) {
if (inline.indexOf(tag) > 0) {
// if end of inline tag
// inlineBuf is now '<inline>tag'
// melt into last node text
var last = bufArray.last();
if (!(last.child instanceof Array)) {
inlineBuf.push('</' + tag + '>');
// inlineBuf became '<inline>tag</inline>'
if (!last.text) last.text = '';
last.text += inlineBuf.join('');
// clear inlineBuf
inlineBuf = [];
} else {
// if block tag
var buf = bufArray.pop();
if (bufArray.length === 0) {
return results = buf;
}
var last = bufArray.last();
if (!(Array.isArray(last.child))) {
last.child = [];
}
last.child.push(buf);
}
},
chars: function(text) {
if (inlineBuf.length !== 0) {
// if inlineBuf exists
// this cace inlineBuf is maybe like this
// 'hoge <inline>tag</inline>'
// so append to last
inlineBuf.push(text);
} else {
bufArray.push(buf);
}
}
},
end: function(tag) {
if (inline[tag]) {
// if end of inline tag
// inlineBuf is now '<inline>tag'
// melt into last node text
var last = bufArray.last();
inlineBuf.push('</' + tag + '>');
// inlineBuf became '<inline>tag</inline>'
if (!last.text) last.text = '';
last.text += inlineBuf.join('');
// clear inlineBuf
inlineBuf = [];
} else {
// if block tag
var buf = bufArray.pop();
if (bufArray.length === 0) {
return results = buf;
}
var last = bufArray.last();
if (!(last.child instanceof Array)) {
last.child = [];
}
last.child.push(buf);
}
},
chars: function(text) {
if (inlineBuf.length !== 0) {
// if inlineBuf exists
// this cace inlineBuf is maybe like this
// 'hoge <inline>tag</inline>'
// so append to last
inlineBuf.push(text);
} else {
var last = bufArray.last();
if (last) {
if (!last.text) {
last.text = '';
var last = bufArray.last();
if (last) {
if (!last.text) {
last.text = '';
}
last.text += text;
}
last.text += text;
}
},
comment: function(text) {
// results += "<!--" + text + "-->";
}
},
comment: function(text) {
// results += "<!--" + text + "-->";
}
});
return results;
}
});
return results;
};
this.json2html = function json2html(json) {
var html = '';
var tag = json.tag;
var text = json.text;
var children = json.child;
var buf = [];
global.json2html = function json2html(json) {
// Empty Elements - HTML 4.01
var empty = ['area', 'base', 'basefont', 'br', 'col', 'frame', 'hr', 'img', 'input', 'isindex', 'link', 'meta', 'param', 'embed'];
// Empty Elements - HTML 4.01
var empty = makeMap('area,base,basefont,br,col,frame,hr,img,input,isindex,link,meta,param,embed');
var child = '';
if (json.child) {
child = json.child.map(function(c) {
return json2html(c);
}).join('');
}
var buildAttr = function(attr) {
for (var k in attr) {
buf.push(' ' + k + '="');
if (attr[k] instanceof Array) {
buf.push(attr[k].join(' '));
} else {
buf.push(attr[k]);
}
buf.push('"');
var attr = '';
if (json.attr) {
attr = Object.keys(json.attr).map(function(key) {
var value = json.attr[key];
if (Array.isArray(value)) value = value.join(' ');
return key + '=' + q(value);
}).join(' ');
if (attr !== '') attr = ' ' + attr;
}
}
buf.push('<');
buf.push(tag);
json.attr ? buf.push(buildAttr(json.attr)) : null;
if (empty[tag]) buf.push('/');
buf.push('>');
text ? buf.push(text) : null;
if (children) {
for (var j = 0; j < children.length; j++) {
buf.push(json2html(children[j]));
var tag = json.tag;
if (empty.indexOf(tag) > -1) {
// empty element
return '<' + json.tag + attr + '/>';
} else {
// non empty element
var open = '<' + json.tag + attr + '>';
var close = '</' + json.tag + '>';
var text = json.text || '';
return [open, text, child, close].join('');
}
}
if (!empty[tag]) buf.push('</' + tag + '>');
return buf.join('');
}
};
})(this);

@@ -7,8 +7,8 @@ if (typeof window === 'undefined') {

describe('html2json', () => {
it('test of test', () => {
describe('html2json', function() {
it('test of test', function() {
assert.strictEqual(typeof html2json, 'function');
});
it('should parse div', () => {
it('should parse div', function() {
var json = { 'tag' : 'div' };

@@ -24,7 +24,4 @@ var html = '<div></div>';

it('should parse div with text', () => {
var json = {
tag: 'div',
text: 'this is div'
};
it('should parse div with text', function() {
var json = { tag: 'div', text: 'this is div' };
var html = '<div>this is div</div>';

@@ -39,3 +36,3 @@

it('should parse div with id', () => {
it('should parse div with id', function() {
var json = { tag: 'div', attr: { id: 'foo'} };

@@ -51,3 +48,3 @@ var html = '<div id="foo"></div>';

it('should parse div with id and class', () => {
it('should parse div with id and class', function() {
var json = {

@@ -67,10 +64,10 @@ tag: 'div',

it('should parse div with child', () => {
it('should parse div with child', function() {
var json = {
tag: 'div',
child: [{
tag: 'p'
}]
child: [
{ tag: 'p', text: 'child' }
]
};
var html = '<div><p></p></div>';
var html = '<div><p>child</p></div>';

@@ -84,12 +81,11 @@ var parsedHtml = parseHtml(html);

it('should parse div with 2 child', () => {
it('should parse div with 2 child', function() {
var json = {
tag: 'div',
child: [{
tag: 'p'
},{
tag: 'p'
}]
child: [
{ tag: 'p', text: 'child1' },
{ tag: 'p', text: 'child2' },
]
};
var html = '<div><p></p><p></p></div>';
var html = '<div><p>child1</p><p>child2</p></div>';

@@ -103,13 +99,18 @@ var parsedHtml = parseHtml(html);

it('should parse div with nested child', () => {
it('should parse div with nested child', function() {
var json = {
tag: 'div',
child: [{
tag: 'p',
child: [{
tag: 'textarea'
}]
}]
child: [
{
tag: 'p',
child: [
{
tag: 'textarea',
text: 'alert(1);',
}
]
}
]
};
var html = '<div><p><textarea></textarea></p></div>';
var html = '<div><p><textarea>alert(1);</textarea></p></div>';

@@ -123,15 +124,22 @@ var parsedHtml = parseHtml(html);

it('should parse div with 2 nested child', () => {
it('should parse div with 2 nested child', function() {
var json = {
tag: 'div',
child: [{
tag: 'p',
child: [{
tag: 'textarea'
}]
},{
tag: 'p'
}]
child: [
{
tag: 'p',
child: [
{
tag: 'textarea',
text: 'alert(1);',
}
]
},
{
tag: 'p',
text: 'child of div',
}
]
};
var html = '<div><p><textarea></textarea></p><p></p></div>';
var html = '<div><p><textarea>alert(1);</textarea></p><p>child of div</p></div>';

@@ -145,3 +153,3 @@ var parsedHtml = parseHtml(html);

it('should parse div with unary & ingored inline tag', () => {
it('should parse div with unary & ingored inline tag', function() {
var json = {

@@ -151,21 +159,25 @@ tag: 'div',

id: '1',
class: ['foo', 'bar']
class: ['foo', 'bar'],
},
child: [{
tag: 'h2',
text: 'sample text'
},{
tag: 'input',
attr: {
id: 'execute',
type: 'button',
value: 'execute'
child: [
{
tag: 'h2',
text: 'sample text',
},
{
tag: 'input',
attr: {
id: 'execute',
type: 'button',
value: 'execute',
}
},
{
tag: 'img',
attr: {
src: 'photo.jpg',
alt: 'photo',
}
}
},{
tag: 'img',
attr: {
src: 'photo.jpg',
alt: 'photo'
}
}]
]
};

@@ -187,3 +199,3 @@

it('should parse div with inline tag', () => {
it('should parse div with inline tag', function() {
var json = {

@@ -193,11 +205,14 @@ tag: 'div',

id: '1',
class: ['foo', 'bar']
class: ['foo', 'bar'],
},
child: [{
tag: 'p',
text: 'sample text with tag <strong>like</strong> this'
},{
tag: 'p',
text: '<strong>start</strong> with inline tag'
}]
child: [
{
tag: 'p',
text: 'sample text with tag <strong>like</strong> this'
},
{
tag: 'p',
text: '<strong>start</strong> with inline tag'
}
]
};

@@ -218,3 +233,3 @@

it('should parse I want to :)', () => {
it('should parse I want to :)', function() {
var json = {

@@ -226,25 +241,30 @@ tag: 'div',

},
child: [{
tag: 'h2',
text: 'sample text with <code>inline tag</code>'
},{
tag: 'pre',
attr: {
id: 'demo',
class: ['foo', 'bar']
child: [
{
tag: 'h2',
text: 'sample text with <code>inline tag</code>'
},
{
tag: 'pre',
attr: {
id: 'demo',
class: ['foo', 'bar']
}
},
{
tag: 'pre',
attr: {
id: 'output',
class: ['goo']
}
},
{
tag: 'input',
attr: {
id: 'execute',
type: 'button',
value: 'execute'
}
}
},{
tag: 'pre',
attr: {
id: 'output',
class: ['goo']
}
},{
tag: 'input',
attr: {
id: 'execute',
type: 'button',
value: 'execute'
}
}]
]
};

@@ -251,0 +271,0 @@ var html = ''

@@ -6,8 +6,8 @@ if (typeof window === 'undefined') {

describe('json2html', () => {
it('test of test', () => {
describe('json2html', function() {
it('test of test', function() {
assert.strictEqual(typeof json2html, 'function');
});
it('should parse div', () => {
it('should parse div', function() {
var json = { tag: 'div' };

@@ -21,7 +21,4 @@ var html = '<div></div>';

it('should parse div with text', () => {
var json = {
tag: 'div',
text: 'this is div'
};
it('should parse div with text', function() {
var json = { tag: 'div', text: 'this is div' };
var html = '<div>this is div</div>';

@@ -34,3 +31,3 @@

it('should parse div with id', () => {
it('should parse div with id', function() {
var json = { tag: 'div', attr: { id: 'foo'} };

@@ -44,7 +41,7 @@ var html = '<div id="foo"></div>';

it('should parse div with id and class', () => {
it('should parse div with id and class', function() {
var json = {
tag: 'div',
attr: { id: 'foo', class: ['bar', 'goo'] },
text: 'this is div'
text: 'this is div',
};

@@ -58,10 +55,10 @@ var html = '<div id="foo" class="bar goo">this is div</div>';

it('should parse div with child', () => {
it('should parse div with child', function() {
var json = {
tag: 'div',
child: [{
tag: 'p'
}]
child: [
{ tag: 'p', text: 'child' }
]
};
var html = '<div><p></p></div>';
var html = '<div><p>child</p></div>';

@@ -73,13 +70,11 @@ var actual = json2html(json);

it('should parse div with 2 child', () => {
it('should parse div with 2 child', function() {
var json = {
tag: 'div',
child: [{
tag: 'p'
},
{
tag: 'p'
}]
child: [
{ tag: 'p', text: 'child1' },
{ tag: 'p', text: 'child2' },
]
};
var html = '<div><p></p><p></p></div>';
var html = '<div><p>child1</p><p>child2</p></div>';

@@ -91,13 +86,18 @@ var actual = json2html(json);

it('should parse div with nested child', () => {
it('should parse div with nested child', function() {
var json = {
tag: 'div',
child: [{
tag: 'p',
child: [{
tag: 'textarea'
}]
}]
child: [
{
tag: 'p',
child: [
{
tag: 'textarea',
text: 'alert(1);',
}
]
}
]
};
var html = '<div><p><textarea></textarea></p></div>';
var html = '<div><p><textarea>alert(1);</textarea></p></div>';

@@ -109,15 +109,22 @@ var actual = json2html(json);

it('should parse div with 2 nested child', () => {
it('should parse div with 2 nested child', function() {
var json = {
tag: 'div',
child: [{
tag: 'p',
child: [{
tag: 'textarea'
}]
},{
tag: 'p'
}]
child: [
{
tag: 'p',
child: [
{
tag: 'textarea',
text: 'alert(1);',
}
]
},
{
tag: 'p',
text: 'child of div',
}
]
};
var html = '<div><p><textarea></textarea></p><p></p></div>';
var html = '<div><p><textarea>alert(1);</textarea></p><p>child of div</p></div>';

@@ -129,3 +136,3 @@ var actual = json2html(json);

it('should parse div with unary & ingored inline tag', () => {
it('should parse div with unary & ingored inline tag', function() {
var json = {

@@ -135,21 +142,25 @@ tag: 'div',

id: '1',
class: ['foo', 'bar']
class: ['foo', 'bar'],
},
child: [{
tag: 'h2',
text: 'sample text'
},{
tag: 'input',
attr: {
id: 'execute',
type: 'button',
value: 'execute'
child: [
{
tag: 'h2',
text: 'sample text',
},
{
tag: 'input',
attr: {
id: 'execute',
type: 'button',
value: 'execute',
}
},
{
tag: 'img',
attr: {
src: 'photo.jpg',
alt: 'photo',
}
}
},{
tag: 'img',
attr: {
src: 'photo.jpg',
alt: 'photo'
}
}]
]
};

@@ -169,3 +180,3 @@

it('should parse div with inline tag', () => {
it('should parse div with inline tag', function() {
var json = {

@@ -175,11 +186,14 @@ tag: 'div',

id: '1',
class: ['foo', 'bar']
class: ['foo', 'bar'],
},
child: [{
tag: 'p',
text: 'sample text with tag <strong>like</strong> this'
},{
tag: 'p',
text: '<strong>start</strong> with inline tag'
}]
child: [
{
tag: 'p',
text: 'sample text with tag <strong>like</strong> this'
},
{
tag: 'p',
text: '<strong>start</strong> with inline tag'
}
]
};

@@ -198,3 +212,3 @@

it('should parse I want to :)', () => {
it('should parse I want to :)', function() {
var json = {

@@ -206,25 +220,30 @@ tag: 'div',

},
child: [{
tag: 'h2',
text: 'sample text with <code>inline tag</code>'
},{
tag: 'pre',
attr: {
id: 'demo',
class: ['foo', 'bar']
child: [
{
tag: 'h2',
text: 'sample text with <code>inline tag</code>'
},
{
tag: 'pre',
attr: {
id: 'demo',
class: ['foo', 'bar']
}
},
{
tag: 'pre',
attr: {
id: 'output',
class: ['goo']
}
},
{
tag: 'input',
attr: {
id: 'execute',
type: 'button',
value: 'execute'
}
}
},{
tag: 'pre',
attr: {
id: 'output',
class: ['goo']
}
},{
tag: 'input',
attr: {
id: 'execute',
type: 'button',
value: 'execute'
}
}]
]
};

@@ -231,0 +250,0 @@ var html = ''

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc