Skip to content

Instantly share code, notes, and snippets.

@rkeVader
Created March 12, 2019 12:30
Show Gist options
  • Save rkeVader/dbb963b83c478dc0b82b06948d64084b to your computer and use it in GitHub Desktop.
Save rkeVader/dbb963b83c478dc0b82b06948d64084b to your computer and use it in GitHub Desktop.
// This JS file implements a selector that mimics the JQUERY selector '$'
// identical usage. It caches selectors for speed. If there is dynamic html
// you can call $clearElementCache to clear a specific entry in the cache
// or the entire cache structure.
// In addition to $ we have $1 which returns the first item found
var __cache_Elements = [];
function $(selector, context) {
if (!__cache_Elements.hasOwnProperty(selector)) {
__cache_Elements[selector] = (context || document).querySelectorAll(selector);
}
return __cache_Elements[selector];
} // $
function $1(selector, context) {
var result = null;
if (!__cache_Elements.hasOwnProperty(selector)) {
__cache_Elements[selector] = (context || document).querySelectorAll(selector);
}
return __cache_Elements[selector][0];
} // $1
function $clearElementCache(selector) {
if (selector) {
// selector supplied - clear just it
delete __cache_Elements[selector];
}
else {
// no selector supplied - clear entire cache
__cache_Elements = [];
}
} // $clearElementCache
function $next(el, filter) {
var siblings = [];
while (el = el.nextSibling) { if (!filter || filter(el)) siblings.push(el); }
return siblings;
} // $next
function $prev(el, filter) {
var siblings = [];
while (el = el.previousSibling) { if (!filter || filter(el)) siblings.push(el); }
return siblings;
} // $prev
// alias for existing insertBefore (for consistency with our implemented $insertAfter)
function $insertBefore(el, referenceNode) {
referenceNode.parentNode.insertBefore(el, referenceNode);
} // $insertBefore
function $insertAfter(el, referenceNode) {
referenceNode.parentNode.insertBefore(el, referenceNode.nextSibling);
} // $insertAfter
function $empty(el) {
el.innerHTML = '';
} // $empty
function $wrap(el, wrapper) {
el.parentNode.insertBefore(wrapper, el);
wrapper.appendChild(el);
} // $wrap
//////////////////////////////////////////////////////////////////
// PolyFills for Sucky Browsers (I'm looking at you, IE!!!)
//////////////////////////////////////////////////////////////////
// matches polyfill
this.Element && function(ElementPrototype) {
ElementPrototype.matches = ElementPrototype.matches ||
ElementPrototype.matchesSelector ||
ElementPrototype.webkitMatchesSelector ||
ElementPrototype.msMatchesSelector ||
function(selector) {
var node = this, nodes = (node.parentNode || node.document).querySelectorAll(selector), i = -1;
while (nodes[++i] && nodes[i] != node);
return !!nodes[i];
}
}(Element.prototype);
// closest polyfill
this.Element && function(ElementPrototype) {
ElementPrototype.closest = ElementPrototype.closest ||
function(selector) {
var el = this;
while (el.matches && !el.matches(selector)) el = el.parentNode;
return el.matches ? el : null;
}
}(Element.prototype);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment