From dbdab03853cbc728efbf0857f02f0b3fcfd3b21e Mon Sep 17 00:00:00 2001 From: Alan Matkorski Date: Thu, 27 Aug 2020 15:00:21 -0300 Subject: [PATCH] added support to json2html for custom empty tags --- src/html2json.js | 7 +++++-- test/test.js | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/html2json.js b/src/html2json.js index 65a3595..5319dc0 100644 --- a/src/html2json.js +++ b/src/html2json.js @@ -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(''); } diff --git a/test/test.js b/test/test.js index eff1f16..33cd01b 100644 --- a/test/test.js +++ b/test/test.js @@ -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 = ''; + var options = { + empty: ['customtag'] + }; + + assert.deepEqual(json, html2json(html)); + assert.deepEqual(html, json2html(json, options)); + }); it('should parse multi div', function() { var json = {