Skip to content

Commit

Permalink
Add a findElement helper to normalise the method of finding an elem…
Browse files Browse the repository at this point in the history
…ent within a plugin
  • Loading branch information
Thomas Erbe committed Jul 27, 2018
1 parent e1f01cc commit 95903fe
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,30 @@ const Bulma = {
});

return elem;
},

/**
* Helper method to normalise a plugin finding an element.
* @param {string} query
* @param {HTMLElement|null} context
* @param {boolean} nullable
* @returns {null|HTMLElement}
* @throws {TypeError}
*/
findElement(query, context = document, nullable = false) {
if(!query && !nullable) {
throw new TypeError('First argument to `findElement` required. Null given.');
}

if(!query) {
return null;
}

if(query.toString() === '[object HTMLElement]') {
return query;
}

return context.querySelector(query);
}
};

Expand Down

0 comments on commit 95903fe

Please # to comment.