Skip to content

Commit

Permalink
Throw if no elements are generated.
Browse files Browse the repository at this point in the history
  • Loading branch information
domenic committed Aug 1, 2012
1 parent 82ca560 commit dd0e787
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/domify.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ var map = {

module.exports = function(html){
html = String(html);
var tag = /<([\w:]+)/.exec(html)[1];
var tagMatch = /<([\w:]+)/.exec(html);

if (!tagMatch) {
throw new Error('No elements were generated.');
}

var tag = tagMatch[1];

if (tag == 'body') {
html = html.replace(/^\s*<body[^>]*>/, '').replace(/<\/body>\s*$/, '');
Expand All @@ -44,7 +50,7 @@ module.exports = function(html){
el.innerHTML = prefix + html + suffix;
while (depth--) el = el.lastChild;

if (el.lastChild.nextElementSibling || el.lastChild.previousElementSibling){
if (el.lastChild.nextElementSibling || el.lastChild.previousElementSibling) {
throw new Error('More than one element was generated.');
}

Expand Down
11 changes: 11 additions & 0 deletions test/domify.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,15 @@ describe('domify(html)', function(){

assert(thrown && /more than one element/i.test(thrown.message));
})

it('should throw if no tag is given', function(){
var thrown = null;
try {
domify(' ');
} catch (err) {
thrown = err;
}

assert(thrown && /no elements/i.test(thrown.message));
})
})

0 comments on commit dd0e787

Please sign in to comment.