From 164472abe694fadc86ea38be90cfccecaffbfedd Mon Sep 17 00:00:00 2001 From: Tim Beyer Date: Tue, 7 Jul 2015 17:08:27 +0200 Subject: [PATCH] When converting sibling nodes without enclosing tags, return an array instead of throwing --- README.md | 2 ++ lib/html-to-vdom.js | 12 ++++++++---- test/html-to-vdom/index.js | 9 ++++++--- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 67e8751..0260861 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,8 @@ It's used in conjunction with [virtual-dom](https://github.com/Matt-Esch/virtual Note ---- +As of v0.6.0, converting sibling nodes without an enclosing parent tag returns an array of type `VNode` instead of throwing an error + As of v0.5.1, `html-to-vdom` no longer supports browsers without a full ES5 implementation. As of v0.3.0, the VNode and VText classes need to be passed in during library initialization from the `virtual-dom` module you are using. diff --git a/lib/html-to-vdom.js b/lib/html-to-vdom.js index 7867556..5135d39 100644 --- a/lib/html-to-vdom.js +++ b/lib/html-to-vdom.js @@ -13,12 +13,16 @@ module.exports = function initializeHtmlToVdom (VTree, VText) { var tags = parseHTML(htmlToConvert); + var convertedHTML; if (tags.length > 1) { - throw new Error('Input must always have only one root node. You cannot convert multiple siblings without a wrapping tag around them.'); + convertedHTML = tags.map(function (tag) { + return htmlparserToVdom.convert(tag, getVNodeKey); + }); } - - var convertedHTML = htmlparserToVdom.convert(tags[0], getVNodeKey); - + else { + convertedHTML = htmlparserToVdom.convert(tags[0], getVNodeKey); + } + return convertedHTML; }; }; diff --git a/test/html-to-vdom/index.js b/test/html-to-vdom/index.js index 580e4bd..b80ed5d 100644 --- a/test/html-to-vdom/index.js +++ b/test/html-to-vdom/index.js @@ -18,10 +18,13 @@ describe('htmlparser-to-vdom', function () { }); describe('when converting multiple sibling nodes without a wrapper', function () { - it('throws', function () { - var html = '
'; + it('returns an array of vnodes', function () { + var html = '
'; - should.throw(convertHTML.bind(null, html), 'Input must always have only one root node. You cannot convert multiple siblings without a wrapping tag around them.'); + var converted = convertHTML(html); + converted.should.be.an('array'); + converted[0].properties.id.should.equal('foo'); + converted[1].properties.id.should.equal('bar'); }); });