Skip to content

Commit

Permalink
Merge pull request #109 from algolia/fix/get-container-node-error
Browse files Browse the repository at this point in the history
fix: More explicit error message when DOM selector is invalid
  • Loading branch information
pixelastic committed Sep 22, 2015
2 parents d84af23 + d36a2ad commit 5ebfe75
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
function getContainerNode(selectorOrHTMLElement) {
if (typeof selectorOrHTMLElement === 'string') {
return document.querySelector(selectorOrHTMLElement);
var isFromString = (typeof selectorOrHTMLElement === 'string');
var domElement;
if (isFromString) {
domElement = document.querySelector(selectorOrHTMLElement);
} else {
domElement = selectorOrHTMLElement;
}

if (!isDomElement(selectorOrHTMLElement)) {
throw new Error('Container must be `string` or `HTMLElement`');
if (!isDomElement(domElement)) {
var errorMessage = 'Container must be `string` or `HTMLElement`.';
if (isFromString) {
errorMessage += ' Unable to find ' + selectorOrHTMLElement;
}
throw new Error(errorMessage);
}

return selectorOrHTMLElement;
return domElement;
}

function isDomElement(o) {
Expand Down

0 comments on commit 5ebfe75

Please sign in to comment.