jQuery like selection helper
/*
* jQuery like selection helper
*
* Waits for a selector, HTML or DOM elements
* Second argument is an optional context
*
* Returns an array
*/
function $ (selector, context) {
if (!selector) {
return [];
}
// If selector is a function
else if (typeof selector === 'function') {
return document.addEventListener("DOMContentLoaded", selector);
}
// If selector it's an array we assume it's an array of elements
else if (selector instanceof Array) {
return selector;
}
// If selector DOM element
else if (selector.nodeType === 1) {
return [ selector ];
}
// If selector is HTML
else if (selector.match(/^\s*<(\w+|!)[^>]*>/)) {
var container;
container = document.createElement('div');
container.innerHTML = selector;
return [].slice.call(container.childNodes);
}
// Otherwise we assume it's a string selector
else {
if (context) {
context = $(context)[0];
} else {
context = document;
}
return [].slice.call(context.querySelectorAll(selector));
}
};