Skip to content
This repository has been archived by the owner on Apr 10, 2024. It is now read-only.

added support to json2html for custom empty tags #38

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/html2json.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,17 @@
return results;
};

global.json2html = function json2html(json) {
global.json2html = function json2html(json, options) {
// Empty Elements - HTML 4.01
var empty = ['area', 'base', 'basefont', 'br', 'col', 'frame', 'hr', 'img', 'input', 'isindex', 'link', 'meta', 'param', 'embed'];
if(options && options.empty && Array.isArray(options.empty)) {
empty = empty.concat(options.empty);
}

var child = '';
if (json.child) {
child = json.child.map(function(c) {
return json2html(c);
return json2html(c, options);
}).join('');
}

Expand Down
15 changes: 15 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ describe('html2json', function() {
assert.deepEqual(html, json2html(json));
});

it('should parse custom empty elements', function() {
var json = {
node: 'root',
child: [
{ node: 'element', tag : 'customtag' }
]
};
var html = '<customtag/>';
var options = {
empty: ['customtag']
};

assert.deepEqual(json, html2json(html));
assert.deepEqual(html, json2html(json, options));
});

it('should parse multi div', function() {
var json = {
Expand Down