From 88a093733e8444d4df1c7044df11b3cfe62e7515 Mon Sep 17 00:00:00 2001 From: Coridyn Fitzgerald-Hood Date: Tue, 12 Sep 2017 09:41:05 +1000 Subject: [PATCH] allow comments directly under root --- src/html2json.js | 2 +- test/test.js | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/html2json.js b/src/html2json.js index 65a3595..4b8b14b 100644 --- a/src/html2json.js +++ b/src/html2json.js @@ -112,7 +112,7 @@ node: 'comment', text: text, }; - var parent = bufArray[0]; + var parent = bufArray[0] || results; if (parent.child === undefined) { parent.child = []; } diff --git a/test/test.js b/test/test.js index eff1f16..aebe91b 100644 --- a/test/test.js +++ b/test/test.js @@ -91,6 +91,22 @@ describe('html2json', function() { assert.deepEqual(json, html2json(html)); assert.deepEqual(html, json2html(json)); }); + + it('should parse top-level comment', function() { + var json = { + node: 'root', + child: [ + { + node: 'comment', + text: ' foo ' + } + ] + }; + var html = ''; + + assert.deepEqual(json, html2json(html)); + assert.deepEqual(html, json2html(json)); + }); it('should parse div with id', function() { var json = {