Skip to content

Commit

Permalink
When converting sibling nodes without enclosing tags, return an array…
Browse files Browse the repository at this point in the history
… instead of throwing
  • Loading branch information
TimBeyer committed Jul 7, 2015
1 parent 39a3bec commit 164472a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 8 additions & 4 deletions lib/html-to-vdom.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
};
9 changes: 6 additions & 3 deletions test/html-to-vdom/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ describe('htmlparser-to-vdom', function () {
});

describe('when converting multiple sibling nodes without a wrapper', function () {
it('throws', function () {
var html = ' <div></div>';
it('returns an array of vnodes', function () {
var html = '<div id="foo"></div><div id="bar"></div>';

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');
});

});
Expand Down

0 comments on commit 164472a

Please sign in to comment.